| 162 | /* Parse the input text into an unescaped cstring, and populate item. */ |
| 163 | static const unsigned char firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC }; |
| 164 | static const char *parse_string(Json* item,const char *str) |
| 165 | { |
| 166 | const char *ptr=str+1;char *ptr2;char *out;int len=0;unsigned uc,uc2; |
| 167 | if (*str!='\"') {ep=str;return 0;} /* not a string! */ |
| 168 | |
| 169 | while (*ptr!='\"' && *ptr && ++len) if (*ptr++ == '\\') ptr++; /* Skip escaped quotes. */ |
| 170 | |
| 171 | out=(char*)Json::malloc(len+1); /* This is how long we need for the string, roughly. */ |
| 172 | if (!out) return 0; |
| 173 | |
| 174 | ptr=str+1;ptr2=out; |
| 175 | while (*ptr!='\"' && *ptr) |
| 176 | { |
| 177 | if (*ptr!='\\') *ptr2++=*ptr++; |
| 178 | else |
| 179 | { |
| 180 | ptr++; |
| 181 | switch (*ptr) |
| 182 | { |
| 183 | case 'b': *ptr2++='\b'; break; |
| 184 | case 'f': *ptr2++='\f'; break; |
| 185 | case 'n': *ptr2++='\n'; break; |
| 186 | case 'r': *ptr2++='\r'; break; |
| 187 | case 't': *ptr2++='\t'; break; |
| 188 | case 'u': /* transcode utf16 to utf8. */ |
| 189 | uc=parse_hex4(ptr+1);ptr+=4; /* get the unicode char. */ |
| 190 | |
| 191 | if ((uc>=0xDC00 && uc<=0xDFFF) || uc==0) break; /* check for invalid. */ |
| 192 | |
| 193 | if (uc>=0xD800 && uc<=0xDBFF) /* UTF16 surrogate pairs. */ |
| 194 | { |
| 195 | if (ptr[1]!='\\' || ptr[2]!='u') break; /* missing second-half of surrogate. */ |
| 196 | uc2=parse_hex4(ptr+3);ptr+=6; |
| 197 | if (uc2<0xDC00 || uc2>0xDFFF) break; /* invalid second-half of surrogate. */ |
| 198 | uc=0x10000 + (((uc&0x3FF)<<10) | (uc2&0x3FF)); |
| 199 | } |
| 200 | |
| 201 | len=4;if (uc<0x80) len=1;else if (uc<0x800) len=2;else if (uc<0x10000) len=3; ptr2+=len; |
| 202 | |
| 203 | switch (len) { |
| 204 | case 4: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6; |
| 205 | case 3: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6; |
| 206 | case 2: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6; |
| 207 | case 1: *--ptr2 =(uc | firstByteMark[len]); |
| 208 | } |
| 209 | ptr2+=len; |
| 210 | break; |
| 211 | default: *ptr2++=*ptr; break; |
| 212 | } |
| 213 | ptr++; |
| 214 | } |
| 215 | } |
| 216 | *ptr2=0; |
| 217 | if (*ptr=='\"') ptr++; |
| 218 | item->mValueString=out; |
| 219 | item->mType=Json::Type_String; |
| 220 | return ptr; |
| 221 | } |
no test coverage detected