Add UtilUtf8Encode() and UtilServerTs()

Also updated Json.c to use UtilUtf8Encode() when decoding JSON strings.
This commit is contained in:
Jordan Bancino 2022-07-27 13:47:12 -04:00
parent 360fb4cb2c
commit 987b1653f1
2 changed files with 125 additions and 31 deletions

View file

@ -1,8 +1,11 @@
#include <Json.h> #include <Json.h>
#include <Util.h>
#include <stdio.h> #include <stdio.h>
#include <stddef.h> #include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
struct JsonValue struct JsonValue
{ {
@ -282,16 +285,10 @@ JsonEncodeString(const char *str, FILE * out)
case '\r': case '\r':
fputs("\\r", out); fputs("\\r", out);
break; break;
default: default: /* Assume UTF-8 input */
if (c < ' ')
{
fprintf(out, "\\u%04x", c);
}
else
{
fputc(c, out); fputc(c, out);
}
break; break;
} }
i++; i++;
} }
@ -304,11 +301,15 @@ JsonDecodeString(FILE * in)
{ {
const size_t strBlockSize = 16; const size_t strBlockSize = 16;
size_t i;
size_t len; size_t len;
size_t allocated; size_t allocated;
char *str; char *str;
char c; char c;
char a; char a[5];
unsigned long utf8;
char *utf8Ptr;
len = 0; len = 0;
allocated = strBlockSize; allocated = strBlockSize;
@ -334,30 +335,52 @@ JsonDecodeString(FILE * in)
case '\\': case '\\':
case '"': case '"':
case '/': case '/':
a = c; a[0] = c;
a[1] = '\0';
break; break;
case 'b': case 'b':
a = '\b'; a[0] = '\b';
a[1] = '\0';
break; break;
case 't': case 't':
a = '\t'; a[0] = '\t';
a[1] = '\0';
break; break;
case 'n': case 'n':
a = '\n'; a[0] = '\n';
a[1] = '\0';
break; break;
case 'f': case 'f':
a = '\f'; a[0] = '\f';
a[1] = '\0';
break; break;
case 'r': case 'r':
a = '\r'; a[0] = '\r';
a[1] = '\0';
break; break;
case 'u': case 'u':
if (fscanf(in, "%04x", (unsigned int *) &a) != 1) /* Read \uXXXX point into a 4-byte buffer */
if (fscanf(in, "%04lx", &utf8) != 1)
{ {
/* Bad hex value */ /* Bad hex value */
free(str); free(str);
return NULL; return NULL;
} }
/* Encode the 4-byte UTF-8 buffer into a series
* of 1-byte characters */
utf8Ptr = UtilUtf8Encode(utf8);
if (!utf8Ptr)
{
/* Mem error */
free(str);
return NULL;
}
/* Move the output of UtilUtf8Encode() into our
* local buffer */
strcpy(a, utf8Ptr);
free(utf8Ptr);
break; break;
default: default:
/* Bad escape value */ /* Bad escape value */
@ -366,11 +389,15 @@ JsonDecodeString(FILE * in)
} }
break; break;
default: default:
a = c; a[0] = c;
a[1] = '\0';
break; break;
} }
/* Append a */ /* Append buffer a */
i = 0;
while (a[i] != '\0')
{
if (len >= allocated) if (len >= allocated)
{ {
char *tmp; char *tmp;
@ -386,8 +413,10 @@ JsonDecodeString(FILE * in)
str = tmp; str = tmp;
} }
str[len] = a; str[len] = a[i];
len++; len++;
i++;
}
} }
free(str); free(str);

65
src/Util.c Normal file
View file

@ -0,0 +1,65 @@
#include <Util.h>
#include <stdlib.h>
#include <sys/time.h>
long
UtilServerTs(void)
{
struct timeval tv;
long ts;
gettimeofday(&tv, NULL);
ts = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
return ts;
}
char *
UtilUtf8Encode(unsigned long utf8)
{
char *str;
str = malloc(5 * sizeof(char));
if (!str)
{
return NULL;
}
if (utf8 <= 0x7F) /* Plain ASCII */
{
str[0] = (char) utf8;
str[1] = '\0';
}
else if (utf8 <= 0x07FF) /* 2-byte */
{
str[0] = (char) (((utf8 >> 6) & 0x1F) | 0xC0);
str[1] = (char) (((utf8 >> 0) & 0x3F) | 0x80);
str[2] = '\0';
}
else if (utf8 <= 0xFFFF) /* 3-byte */
{
str[0] = (char) (((utf8 >> 12) & 0x0F) | 0xE0);
str[1] = (char) (((utf8 >> 6) & 0x3F) | 0x80);
str[2] = (char) (((utf8 >> 0) & 0x3F) | 0x80);
str[3] = '\0';
}
else if (utf8 <= 0x10FFFF) /* 4-byte */
{
str[0] = (char) (((utf8 >> 18) & 0x07) | 0xF0);
str[1] = (char) (((utf8 >> 12) & 0x3F) | 0x80);
str[2] = (char) (((utf8 >> 6) & 0x3F) | 0x80);
str[3] = (char) (((utf8 >> 0) & 0x3F) | 0x80);
str[4] = '\0';
}
else
{
/* Send replacement character */
str[0] = (char) 0xEF;
str[1] = (char) 0xBF;
str[2] = (char) 0xBD;
str[3] = '\0';
}
return str;
}