From b5d57642c22811e0b14808265c2b8941c87d5987 Mon Sep 17 00:00:00 2001 From: Dave Gamble Date: Wed, 2 Mar 2011 21:04:46 +0000 Subject: [PATCH] Handle control-chars. In a certain sense, we shouldn't be seeing them, but this way we at least handle them well. git-svn-id: http://svn.code.sf.net/p/cjson/code@37 e3330c51-1366-4df0-8b21-3ccf24e3d50e --- cJSON.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cJSON.c b/cJSON.c index 2f68785..ec728ca 100644 --- a/cJSON.c +++ b/cJSON.c @@ -141,13 +141,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; if (*str!='\"') return 0; /* not a string! */ - while (*ptr!='\"' && (unsigned char)*ptr>31 && ++len) if (*ptr++ == '\\') ptr++; /* Skip escaped quotes. */ + while (*ptr!='\"' && *ptr && ++len) if (*ptr++ == '\\') ptr++; /* Skip escaped quotes. */ out=(char*)cJSON_malloc(len+1); /* This is how long we need for the string, roughly. */ if (!out) return 0; ptr=str+1;ptr2=out; - while (*ptr!='\"' && (unsigned char)*ptr>31) + while (*ptr!='\"' && *ptr) { if (*ptr!='\\') *ptr2++=*ptr++; else @@ -186,10 +186,10 @@ static const char *parse_string(cJSON *item,const char *str) /* Render the cstring provided to an escaped version that can be printed. */ 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;unsigned char token; if (!str) return cJSON_strdup(""); - ptr=str;while (*ptr && ++len) {if ((unsigned char)*ptr<32 || *ptr=='\"' || *ptr=='\\') len++;ptr++;} + ptr=str;while ((token=*ptr) && ++len) {if (strchr("\"\\\b\f\n\r\t",token)) len++; else if (token<32) len+=5;ptr++;} out=(char*)cJSON_malloc(len+3); if (!out) return 0; @@ -202,7 +202,7 @@ static char *print_string_ptr(const char *str) else { *ptr2++='\\'; - switch (*ptr++) + switch (token=*ptr++) { case '\\': *ptr2++='\\'; break; case '\"': *ptr2++='\"'; break; @@ -211,7 +211,7 @@ static char *print_string_ptr(const char *str) case '\n': *ptr2++='n'; break; case '\r': *ptr2++='r'; break; case '\t': *ptr2++='t'; break; - default: ptr2--; break; /* eviscerate with prejudice. */ + default: sprintf(ptr2,"u%04x",token);ptr2+=5; break; /* escape and print */ } } }