Fix handling of non-ascii characters!

Many thanks to an anonymous submitter for this fix!!



git-svn-id: http://svn.code.sf.net/p/cjson/code@29 e3330c51-1366-4df0-8b21-3ccf24e3d50e
This commit is contained in:
Dave Gamble
2010-01-23 12:44:58 +00:00
parent d9840479f5
commit 7dd9280267

10
cJSON.c
View File

@@ -138,13 +138,13 @@ static const char *parse_string(cJSON *item,const char *str)
const char *ptr=str+1;char *ptr2;char *out;int len=0;unsigned uc; const char *ptr=str+1;char *ptr2;char *out;int len=0;unsigned uc;
if (*str!='\"') return 0; // not a string! if (*str!='\"') return 0; // not a string!
while (*ptr!='\"' && *ptr>31 && ++len) if (*ptr++ == '\\') ptr++; // Skip escaped quotes. while (*ptr!='\"' && (unsigned char)*ptr>31 && ++len) if (*ptr++ == '\\') ptr++; // Skip escaped quotes.
out=(char*)cJSON_malloc(len+1); // This is how long we need for the string, roughly. out=(char*)cJSON_malloc(len+1); // This is how long we need for the string, roughly.
if (!out) return 0; if (!out) return 0;
ptr=str+1;ptr2=out; ptr=str+1;ptr2=out;
while (*ptr!='\"' && *ptr>31) while (*ptr!='\"' && (unsigned char)*ptr>31)
{ {
if (*ptr!='\\') *ptr2++=*ptr++; if (*ptr!='\\') *ptr2++=*ptr++;
else else
@@ -186,14 +186,14 @@ static char *print_string_ptr(const char *str)
const char *ptr;char *ptr2,*out;int len=0; const char *ptr;char *ptr2,*out;int len=0;
if (!str) return cJSON_strdup(""); if (!str) return cJSON_strdup("");
ptr=str;while (*ptr && ++len) {if (*ptr<32 || *ptr=='\"' || *ptr=='\\') len++;ptr++;} ptr=str;while (*ptr && ++len) {if ((unsigned char)*ptr<32 || *ptr=='\"' || *ptr=='\\') len++;ptr++;}
out=(char*)cJSON_malloc(len+3); out=(char*)cJSON_malloc(len+3);
ptr2=out;ptr=str; ptr2=out;ptr=str;
*ptr2++='\"'; *ptr2++='\"';
while (*ptr) while (*ptr)
{ {
if (*ptr>31 && *ptr!='\"' && *ptr!='\\') *ptr2++=*ptr++; if ((unsigned char)*ptr>31 && *ptr!='\"' && *ptr!='\\') *ptr2++=*ptr++;
else else
{ {
*ptr2++='\\'; *ptr2++='\\';
@@ -225,7 +225,7 @@ static const char *parse_object(cJSON *item,const char *value);
static char *print_object(cJSON *item,int depth,int fmt); static char *print_object(cJSON *item,int depth,int fmt);
// Utility to jump whitespace and cr/lf // Utility to jump whitespace and cr/lf
static const char *skip(const char *in) {while (in && *in<=32) in++; return in;} static const char *skip(const char *in) {while (in && (unsigned char)*in<=32) in++; return in;}
// Parse an object - create a new root, and populate. // Parse an object - create a new root, and populate.
cJSON *cJSON_Parse(const char *value) cJSON *cJSON_Parse(const char *value)