| 1164 | } |
| 1165 | |
| 1166 | static void json_next_string_token(json_parse_t *json, json_token_t *token) |
| 1167 | { |
| 1168 | char *escape2char = json->cfg->escape2char; |
| 1169 | char ch; |
| 1170 | |
| 1171 | /* Caller must ensure a string is next */ |
| 1172 | assert(*json->ptr == '"'); |
| 1173 | |
| 1174 | /* Skip " */ |
| 1175 | json->ptr++; |
| 1176 | |
| 1177 | /* json->tmp is the temporary strbuf used to accumulate the |
| 1178 | * decoded string value. |
| 1179 | * json->tmp is sized to handle JSON containing only a string value. |
| 1180 | */ |
| 1181 | strbuf_reset(json->tmp); |
| 1182 | |
| 1183 | while ((ch = *json->ptr) != '"') { |
| 1184 | if (!ch) { |
| 1185 | /* Premature end of the string */ |
| 1186 | json_set_token_error(token, json, "unexpected end of string"); |
| 1187 | return; |
| 1188 | } |
| 1189 | |
| 1190 | /* Handle escapes */ |
| 1191 | if (ch == '\\') { |
| 1192 | /* Fetch escape character */ |
| 1193 | ch = *(json->ptr + 1); |
| 1194 | |
| 1195 | /* Translate escape code and append to tmp string */ |
| 1196 | ch = escape2char[(unsigned char)ch]; |
| 1197 | if (ch == 'u') { |
| 1198 | if (json_append_unicode_escape(json) == 0) |
| 1199 | continue; |
| 1200 | |
| 1201 | json_set_token_error(token, json, |
| 1202 | "invalid unicode escape code"); |
| 1203 | return; |
| 1204 | } |
| 1205 | if (!ch) { |
| 1206 | json_set_token_error(token, json, "invalid escape code"); |
| 1207 | return; |
| 1208 | } |
| 1209 | |
| 1210 | /* Skip '\' */ |
| 1211 | json->ptr++; |
| 1212 | } |
| 1213 | /* Append normal character or translated single character |
| 1214 | * Unicode escapes are handled above */ |
| 1215 | strbuf_append_char_unsafe(json->tmp, ch); |
| 1216 | json->ptr++; |
| 1217 | } |
| 1218 | json->ptr++; /* Eat final quote (") */ |
| 1219 | |
| 1220 | strbuf_ensure_null(json->tmp); |
| 1221 | |
| 1222 | token->type = T_STRING; |
| 1223 | token->value.string = strbuf_string(json->tmp, &token->string_len); |
no test coverage detected