This commit is contained in:
CK Tan 2019-10-02 01:11:34 -07:00
parent c5907e1504
commit 15f44eba49

188
toml.c
View file

@ -42,7 +42,7 @@ static void* (*ppcalloc)(size_t, size_t) = calloc;
static void* (*pprealloc)(void*, size_t) = realloc; static void* (*pprealloc)(void*, size_t) = realloc;
void toml_set_memutil(void* (*xxmalloc)(size_t), void toml_set_memutil(void* (*xxmalloc)(size_t),
void (*xxfree)(void*), void (*xxfree)(void*),
void* (*xxcalloc)(size_t, size_t), void* (*xxcalloc)(size_t, size_t),
void* (*xxrealloc)(void*, size_t)) void* (*xxrealloc)(void*, size_t))
{ {
@ -53,31 +53,31 @@ void toml_set_memutil(void* (*xxmalloc)(size_t),
} }
#define MALLOC(a) ppmalloc(a) #define MALLOC(a) ppmalloc(a)
#define FREE(a) ppfree(a) #define FREE(a) ppfree(a)
#define CALLOC(a,b) ppcalloc(a,b) #define CALLOC(a,b) ppcalloc(a,b)
#define REALLOC(a,b) pprealloc(a,b) #define REALLOC(a,b) pprealloc(a,b)
char* STRDUP(const char* s) char* STRDUP(const char* s)
{ {
int len = strlen(s); int len = strlen(s);
char* p = MALLOC(len+1); char* p = MALLOC(len+1);
if (p) { if (p) {
memcpy(p, s, len); memcpy(p, s, len);
p[len] = 0; p[len] = 0;
} }
return p; return p;
} }
char* STRNDUP(const char* s, size_t n) char* STRNDUP(const char* s, size_t n)
{ {
size_t len = strnlen(s, n); size_t len = strnlen(s, n);
char* p = MALLOC(len+1); char* p = MALLOC(len+1);
if (p) { if (p) {
memcpy(p, s, len); memcpy(p, s, len);
p[len] = 0; p[len] = 0;
} }
return p; return p;
} }
@ -88,88 +88,88 @@ char* STRNDUP(const char* s, size_t n)
*/ */
int toml_utf8_to_ucs(const char* orig, int len, int64_t* ret) int toml_utf8_to_ucs(const char* orig, int len, int64_t* ret)
{ {
const unsigned char* buf = (const unsigned char*) orig; const unsigned char* buf = (const unsigned char*) orig;
unsigned i = *buf++; unsigned i = *buf++;
int64_t v; int64_t v;
/* 0x00000000 - 0x0000007F: /* 0x00000000 - 0x0000007F:
0xxxxxxx 0xxxxxxx
*/ */
if (0 == (i >> 7)) { if (0 == (i >> 7)) {
if (len < 1) return -1; if (len < 1) return -1;
v = i; v = i;
return *ret = v, 1; return *ret = v, 1;
} }
/* 0x00000080 - 0x000007FF: /* 0x00000080 - 0x000007FF:
110xxxxx 10xxxxxx 110xxxxx 10xxxxxx
*/ */
if (0x6 == (i >> 5)) { if (0x6 == (i >> 5)) {
if (len < 2) return -1; if (len < 2) return -1;
v = i & 0x1f; v = i & 0x1f;
for (int j = 0; j < 1; j++) { for (int j = 0; j < 1; j++) {
i = *buf++; i = *buf++;
if (0x2 != (i >> 6)) return -1; if (0x2 != (i >> 6)) return -1;
v = (v << 6) | (i & 0x3f); v = (v << 6) | (i & 0x3f);
} }
return *ret = v, (const char*) buf - orig; return *ret = v, (const char*) buf - orig;
} }
/* 0x00000800 - 0x0000FFFF: /* 0x00000800 - 0x0000FFFF:
1110xxxx 10xxxxxx 10xxxxxx 1110xxxx 10xxxxxx 10xxxxxx
*/ */
if (0xE == (i >> 4)) { if (0xE == (i >> 4)) {
if (len < 3) return -1; if (len < 3) return -1;
v = i & 0x0F; v = i & 0x0F;
for (int j = 0; j < 2; j++) { for (int j = 0; j < 2; j++) {
i = *buf++; i = *buf++;
if (0x2 != (i >> 6)) return -1; if (0x2 != (i >> 6)) return -1;
v = (v << 6) | (i & 0x3f); v = (v << 6) | (i & 0x3f);
} }
return *ret = v, (const char*) buf - orig; return *ret = v, (const char*) buf - orig;
} }
/* 0x00010000 - 0x001FFFFF: /* 0x00010000 - 0x001FFFFF:
11110xxx 10xxxxxx 10xxxxxx 10xxxxxx 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
*/ */
if (0x1E == (i >> 3)) { if (0x1E == (i >> 3)) {
if (len < 4) return -1; if (len < 4) return -1;
v = i & 0x07; v = i & 0x07;
for (int j = 0; j < 3; j++) { for (int j = 0; j < 3; j++) {
i = *buf++; i = *buf++;
if (0x2 != (i >> 6)) return -1; if (0x2 != (i >> 6)) return -1;
v = (v << 6) | (i & 0x3f); v = (v << 6) | (i & 0x3f);
} }
return *ret = v, (const char*) buf - orig; return *ret = v, (const char*) buf - orig;
} }
/* 0x00200000 - 0x03FFFFFF: /* 0x00200000 - 0x03FFFFFF:
111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
*/ */
if (0x3E == (i >> 2)) { if (0x3E == (i >> 2)) {
if (len < 5) return -1; if (len < 5) return -1;
v = i & 0x03; v = i & 0x03;
for (int j = 0; j < 4; j++) { for (int j = 0; j < 4; j++) {
i = *buf++; i = *buf++;
if (0x2 != (i >> 6)) return -1; if (0x2 != (i >> 6)) return -1;
v = (v << 6) | (i & 0x3f); v = (v << 6) | (i & 0x3f);
} }
return *ret = v, (const char*) buf - orig; return *ret = v, (const char*) buf - orig;
} }
/* 0x04000000 - 0x7FFFFFFF: /* 0x04000000 - 0x7FFFFFFF:
1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
*/ */
if (0x7e == (i >> 1)) { if (0x7e == (i >> 1)) {
if (len < 6) return -1; if (len < 6) return -1;
v = i & 0x01; v = i & 0x01;
for (int j = 0; j < 5; j++) { for (int j = 0; j < 5; j++) {
i = *buf++; i = *buf++;
if (0x2 != (i >> 6)) return -1; if (0x2 != (i >> 6)) return -1;
v = (v << 6) | (i & 0x3f); v = (v << 6) | (i & 0x3f);
} }
return *ret = v, (const char*) buf - orig; return *ret = v, (const char*) buf - orig;
} }
return -1; return -1;
} }