| 886 | } |
| 887 | |
| 888 | static void json_next_string_token(json_parse_t *json, json_token_t *token) |
| 889 | { |
| 890 | char *escape2char = json->cfg->escape2char; |
| 891 | char ch; |
| 892 | |
| 893 | /* Caller must ensure a string is next */ |
| 894 | assert(*json->ptr == '"'); |
| 895 | |
| 896 | /* Skip " */ |
| 897 | json->ptr++; |
| 898 | |
| 899 | /* json->tmp is the temporary strbuf used to accumulate the |
| 900 | * decoded string value. |
| 901 | * json->tmp is sized to handle JSON containing only a string value. |
| 902 | */ |
| 903 | strbuf_reset(json->tmp); |
| 904 | |
| 905 | while ((ch = *json->ptr) != '"') { |
| 906 | if (!ch) { |
| 907 | /* Premature end of the string */ |
| 908 | json_set_token_error(token, json, "unexpected end of string"); |
| 909 | return; |
| 910 | } |
| 911 | |
| 912 | /* Handle escapes */ |
| 913 | if (ch == '\\') { |
| 914 | /* Fetch escape character */ |
| 915 | ch = *(json->ptr + 1); |
| 916 | |
| 917 | /* Translate escape code and append to tmp string */ |
| 918 | ch = escape2char[(unsigned char)ch]; |
| 919 | if (ch == 'u') { |
| 920 | if (json_append_unicode_escape(json) == 0) |
| 921 | continue; |
| 922 | |
| 923 | json_set_token_error(token, json, |
| 924 | "invalid unicode escape code"); |
| 925 | return; |
| 926 | } |
| 927 | if (!ch) { |
| 928 | json_set_token_error(token, json, "invalid escape code"); |
| 929 | return; |
| 930 | } |
| 931 | |
| 932 | /* Skip '\' */ |
| 933 | json->ptr++; |
| 934 | } |
| 935 | /* Append normal character or translated single character |
| 936 | * Unicode escapes are handled above */ |
| 937 | strbuf_append_char_unsafe(json->tmp, ch); |
| 938 | json->ptr++; |
| 939 | } |
| 940 | json->ptr++; /* Eat final quote (") */ |
| 941 | |
| 942 | strbuf_ensure_null(json->tmp); |
| 943 | |
| 944 | token->type = T_STRING; |
| 945 | token->value.string = strbuf_string(json->tmp, &token->string_len); |
no test coverage detected