JSON parsing library by Andrii Mamchur https://github.com/amamchur/jsonlite

Dependents:   M2X_dev MTS_M2x_Example1 MTS_M2x_Example m2x-demo-all ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers jsonlite.c Source File

jsonlite.c

00001 #define JSONLITE_AMALGAMATED
00002 #include "jsonlite.h"
00003 //
00004 //  Copyright 2012-2013, Andrii Mamchur
00005 //
00006 //  Licensed under the Apache License, Version 2.0 (the "License");
00007 //  you may not use this file except in compliance with the License.
00008 //  You may obtain a copy of the License at
00009 //
00010 //  http://www.apache.org/licenses/LICENSE-2.0
00011 //
00012 //  Unless required by applicable law or agreed to in writing, software
00013 //  distributed under the License is distributed on an "AS IS" BASIS,
00014 //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 //  See the License for the specific language governing permissions and
00016 //  limitations under the License
00017 
00018 const char *jsonlite_version = "1.1.2";
00019 //
00020 //  Copyright 2012-2013, Andrii Mamchur
00021 //
00022 //  Licensed under the Apache License, Version 2.0 (the "License");
00023 //  you may not use this file except in compliance with the License.
00024 //  You may obtain a copy of the License at
00025 //
00026 //  http://www.apache.org/licenses/LICENSE-2.0
00027 //
00028 //  Unless required by applicable law or agreed to in writing, software
00029 //  distributed under the License is distributed on an "AS IS" BASIS,
00030 //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00031 //  See the License for the specific language governing permissions and
00032 //  limitations under the License
00033 
00034 #ifndef JSONLITE_AMALGAMATED
00035 #include "../include/jsonlite_builder.h"
00036 #endif
00037 
00038 #include <stdlib.h>
00039 #include <stddef.h>
00040 #include <string.h>
00041 
00042 #define jsonlite_builder_check_depth()          \
00043 do {                                            \
00044     if (builder->state >= builder->limit) {     \
00045         return jsonlite_result_depth_limit;     \
00046     }                                           \
00047 } while (0)
00048 
00049 enum {
00050     jsonlite_accept_object_begin    = 0x0001,
00051     jsonlite_accept_object_end      = 0x0002,
00052     jsonlite_accept_array_begin     = 0x0004,
00053     jsonlite_accept_array_end       = 0x0008,
00054     jsonlite_accept_key             = 0x0010,
00055     jsonlite_accept_string          = 0x0020,
00056     jsonlite_accept_number          = 0x0040,
00057     jsonlite_accept_boolean         = 0x0080,
00058     jsonlite_accept_null            = 0x0100,
00059     jsonlite_accept_values_only     = 0x0200,
00060     jsonlite_accept_next            = 0x0400,
00061 
00062     jsonlite_accept_value           = 0
00063                                     | jsonlite_accept_object_begin
00064                                     | jsonlite_accept_array_begin
00065                                     | jsonlite_accept_string
00066                                     | jsonlite_accept_number
00067                                     | jsonlite_accept_boolean
00068                                     | jsonlite_accept_null,
00069     jsonlite_accept_continue_object = 0
00070                                     | jsonlite_accept_next
00071                                     | jsonlite_accept_key
00072                                     | jsonlite_accept_object_end,
00073     jsonlite_accept_continue_array = 0
00074                                     | jsonlite_accept_next
00075                                     | jsonlite_accept_values_only
00076                                     | jsonlite_accept_value
00077                                     | jsonlite_accept_array_end
00078 };
00079 
00080 typedef uint16_t jsonlite_write_state;
00081 typedef struct jsonlite_builder_struct {
00082     jsonlite_write_state *state;
00083     jsonlite_write_state *limit;
00084     jsonlite_write_state *stack;
00085     jsonlite_stream stream;
00086 
00087     size_t indentation;
00088     char *doubleFormat;
00089 } jsonlite_builder_struct;
00090 
00091 static int jsonlite_builder_accept(jsonlite_builder builder, jsonlite_write_state a);
00092 static void jsonlite_builder_pop_state(jsonlite_builder builder);
00093 static void jsonlite_builder_prepare_value_writing(jsonlite_builder builder);
00094 static void jsonlite_builder_raw_char(jsonlite_builder builder, char data);
00095 static void jsonlite_builder_write_uft8(jsonlite_builder builder, const char *data, size_t length);
00096 static void jsonlite_builder_raw(jsonlite_builder builder, const void *data, size_t length);
00097 static void jsonlite_builder_repeat(jsonlite_builder builder, const char ch, size_t count);
00098 static void jsonlite_builder_write_base64(jsonlite_builder builder, const void *data, size_t length);
00099 
00100 jsonlite_builder jsonlite_builder_init(size_t depth, jsonlite_stream stream) {
00101     jsonlite_builder builder;
00102 
00103     depth = depth < 2 ? 2 : depth;
00104 
00105     builder = malloc(sizeof(jsonlite_builder_struct) + depth * sizeof(jsonlite_write_state));
00106     builder->state = (jsonlite_write_state *)((uint8_t *)builder + sizeof(jsonlite_builder_struct));
00107     builder->limit = builder->state + depth - 1;
00108     builder->stack = builder->state;
00109     builder->stream = stream;
00110     builder->indentation = 0;
00111     *builder->state = jsonlite_accept_object_begin | jsonlite_accept_array_begin;
00112     jsonlite_builder_set_double_format(builder, "%.16g");
00113     return builder;
00114 }
00115 
00116 jsonlite_result jsonlite_builder_release(jsonlite_builder builder) {
00117     if (builder == NULL) {
00118         return jsonlite_result_invalid_argument;
00119     }
00120 
00121     free(builder->doubleFormat);
00122     free(builder);
00123     return jsonlite_result_ok;
00124 }
00125 
00126 jsonlite_result jsonlite_builder_set_indentation(jsonlite_builder builder, size_t indentation) {
00127     if (builder != NULL) {
00128         builder->indentation = indentation;
00129         return jsonlite_result_ok;
00130     }
00131     return jsonlite_result_invalid_argument;
00132 }
00133 
00134 jsonlite_result jsonlite_builder_set_double_format(jsonlite_builder builder, const char *format) {
00135     if (builder != NULL && format != NULL) {
00136         builder->doubleFormat = strdup(format);
00137         return jsonlite_result_ok;
00138     }
00139     return jsonlite_result_invalid_argument;
00140 }
00141 
00142 static int jsonlite_builder_accept(jsonlite_builder builder, jsonlite_write_state a) {
00143     return (*builder->state & a) == a;
00144 }
00145 
00146 static void jsonlite_builder_pop_state(jsonlite_builder builder) {
00147     jsonlite_write_state *ws = --builder->state;
00148     if (jsonlite_builder_accept(builder, jsonlite_accept_values_only)) {
00149         *ws = jsonlite_accept_continue_array;
00150     } else {
00151         *ws = jsonlite_accept_continue_object;
00152     }
00153 }
00154 
00155 static void jsonlite_builder_prepare_value_writing(jsonlite_builder builder) {
00156     jsonlite_write_state *ws = builder->state;
00157     if (jsonlite_builder_accept(builder, jsonlite_accept_values_only)) {
00158         if (jsonlite_builder_accept(builder, jsonlite_accept_next)) {
00159             jsonlite_builder_raw_char(builder, ',');
00160         }
00161 
00162         if (builder->indentation != 0) {
00163             jsonlite_builder_raw_char(builder, '\r');
00164             jsonlite_builder_repeat(builder, ' ', (builder->state - builder->stack) * builder->indentation);
00165         }
00166     } else {
00167         *ws &= ~jsonlite_accept_value;
00168         *ws |= jsonlite_accept_key;
00169     }
00170     *ws |= jsonlite_accept_next;
00171 }
00172 
00173 jsonlite_result jsonlite_builder_object_begin(jsonlite_builder builder) {
00174     if (builder == NULL) {
00175         return jsonlite_result_invalid_argument;
00176     }
00177 
00178     jsonlite_builder_check_depth();
00179 
00180     if (jsonlite_builder_accept(builder, jsonlite_accept_object_begin)) {
00181         jsonlite_builder_prepare_value_writing(builder);
00182         *++builder->state = jsonlite_accept_object_end | jsonlite_accept_key;
00183         jsonlite_builder_raw_char(builder, '{');
00184         return jsonlite_result_ok;
00185     }
00186 
00187     return jsonlite_result_not_allowed;
00188 }
00189 
00190 jsonlite_result jsonlite_builder_object_end(jsonlite_builder builder) {
00191     if (builder == NULL) {
00192         return jsonlite_result_invalid_argument;
00193     }
00194 
00195     if (jsonlite_builder_accept(builder, jsonlite_accept_object_end)) {
00196         jsonlite_builder_pop_state(builder);
00197         if (builder->indentation != 0) {
00198             jsonlite_builder_raw_char(builder, '\r');
00199             jsonlite_builder_repeat(builder, ' ', (builder->state - builder->stack) * builder->indentation);
00200         }
00201         jsonlite_builder_raw_char(builder, '}');
00202         return jsonlite_result_ok;
00203     }
00204 
00205     return jsonlite_result_not_allowed;
00206 }
00207 
00208 jsonlite_result jsonlite_builder_array_begin(jsonlite_builder builder) {
00209     if (builder == NULL) {
00210         return jsonlite_result_invalid_argument;
00211     }
00212 
00213     jsonlite_builder_check_depth();
00214 
00215     if (jsonlite_builder_accept(builder, jsonlite_accept_array_begin)) {
00216         jsonlite_builder_prepare_value_writing(builder);
00217         *++builder->state = jsonlite_accept_array_end
00218             | jsonlite_accept_value
00219             | jsonlite_accept_values_only;
00220         jsonlite_builder_raw_char(builder, '[');
00221         return jsonlite_result_ok;
00222     }
00223 
00224     return jsonlite_result_not_allowed;
00225 }
00226 
00227 jsonlite_result jsonlite_builder_array_end(jsonlite_builder builder) {
00228     if (builder == NULL) {
00229         return jsonlite_result_invalid_argument;
00230     }
00231 
00232     if (jsonlite_builder_accept(builder, jsonlite_accept_array_end)) {
00233         jsonlite_builder_pop_state(builder);
00234         if (builder->indentation != 0) {
00235             jsonlite_builder_raw_char(builder, '\r');
00236             jsonlite_builder_repeat(builder, ' ', (builder->state - builder->stack) * builder->indentation);
00237         }
00238         jsonlite_builder_raw_char(builder, ']');
00239         return jsonlite_result_ok;
00240     }
00241 
00242     return jsonlite_result_not_allowed;
00243 }
00244 
00245 static void jsonlite_builder_write_uft8(jsonlite_builder builder, const char *data, size_t length) {
00246     char b[2] = {'\\', '?'};
00247     const char *c = data;
00248     const char *p = data;
00249     const char *l = data + length;
00250 
00251     jsonlite_builder_raw_char(builder, '\"');
00252 next:
00253     if (c == l)                     goto end;
00254     switch (*c) {
00255         case '"':   b[1] = '"';     goto flush;
00256         case '\\':  b[1] = '\\';    goto flush;
00257         case '\b':  b[1] = 'b';     goto flush;
00258         case '\f':  b[1] = 'f';     goto flush;
00259         case '\n':  b[1] = 'n';     goto flush;
00260         case '\r':  b[1] = 'r';     goto flush;
00261         case '\t':  b[1] = 't';     goto flush;
00262         default:    c++;            goto next;
00263     }
00264 flush:
00265     jsonlite_stream_write(builder->stream, p, c - p);
00266     jsonlite_stream_write(builder->stream, b, 2);
00267     p = ++c;
00268     goto next;
00269 end:
00270     jsonlite_stream_write(builder->stream, p, c - p);
00271     jsonlite_builder_raw_char(builder, '\"');
00272 }
00273 
00274 jsonlite_result jsonlite_builder_key(jsonlite_builder builder, const char *data, size_t length) {
00275     if (builder == NULL || data == NULL) {
00276         return jsonlite_result_invalid_argument;
00277     }
00278 
00279     jsonlite_write_state *ws = builder->state;
00280     if (jsonlite_builder_accept(builder, jsonlite_accept_key)) {
00281         if (jsonlite_builder_accept(builder, jsonlite_accept_next)) {
00282             jsonlite_builder_raw_char(builder, ',');
00283         }
00284 
00285         if (builder->indentation != 0) {
00286             jsonlite_builder_raw_char(builder, '\r');
00287             jsonlite_builder_repeat(builder, ' ', (builder->state - builder->stack) * builder->indentation);
00288         }
00289 
00290         jsonlite_builder_write_uft8(builder, data, length);
00291         if (builder->indentation != 0) {
00292             jsonlite_builder_raw(builder, ": ", 2);
00293         } else {
00294             jsonlite_builder_raw_char(builder, ':');
00295         }
00296         *ws = jsonlite_accept_value;
00297         return jsonlite_result_ok;
00298     }
00299 
00300     return jsonlite_result_not_allowed;
00301 }
00302 
00303 jsonlite_result jsonlite_builder_string(jsonlite_builder builder, const char *data, size_t length) {
00304     if (builder == NULL || data == NULL) {
00305         return jsonlite_result_invalid_argument;
00306     }
00307 
00308     jsonlite_write_state *ws = builder->state;
00309     if (jsonlite_builder_accept(builder, jsonlite_accept_value)) {
00310         jsonlite_builder_prepare_value_writing(builder);
00311         jsonlite_builder_write_uft8(builder, data, length);
00312         if (jsonlite_builder_accept(builder, jsonlite_accept_values_only)) {
00313             *ws = jsonlite_accept_continue_array;
00314         } else {
00315             *ws = jsonlite_accept_continue_object;
00316         }
00317         return jsonlite_result_ok;
00318     }
00319 
00320     return jsonlite_result_not_allowed;
00321 }
00322 
00323 jsonlite_result jsonlite_builder_int(jsonlite_builder builder, long long value) {
00324     if (builder == NULL) {
00325         return jsonlite_result_invalid_argument;
00326     }
00327 
00328     char buff[64];
00329     size_t size = 0;
00330     jsonlite_write_state *ws = builder->state;
00331     if (jsonlite_builder_accept(builder, jsonlite_accept_value)) {
00332         jsonlite_builder_prepare_value_writing(builder);
00333         size = sprintf(buff, "%lld", value);
00334         jsonlite_builder_raw(builder, buff, size);
00335         if (jsonlite_builder_accept(builder, jsonlite_accept_values_only)) {
00336             *ws = jsonlite_accept_continue_array;
00337         } else {
00338             *ws = jsonlite_accept_continue_object;
00339         }
00340         return jsonlite_result_ok;
00341     }
00342 
00343     return jsonlite_result_not_allowed;
00344 }
00345 
00346 jsonlite_result jsonlite_builder_double(jsonlite_builder builder, double value) {
00347     if (builder == NULL) {
00348         return jsonlite_result_invalid_argument;
00349     }
00350 
00351     char buff[64];
00352     size_t size = 0;
00353     jsonlite_write_state *ws = builder->state;
00354     if (jsonlite_builder_accept(builder, jsonlite_accept_value)) {
00355         jsonlite_builder_prepare_value_writing(builder);
00356         size = sprintf(buff, builder->doubleFormat, value);
00357         jsonlite_builder_raw(builder, buff, size);
00358         if (jsonlite_builder_accept(builder, jsonlite_accept_values_only)) {
00359             *ws = jsonlite_accept_continue_array;
00360         } else {
00361             *ws = jsonlite_accept_continue_object;
00362         }
00363         return jsonlite_result_ok;
00364     }
00365 
00366     return jsonlite_result_not_allowed;
00367 }
00368 
00369 jsonlite_result jsonlite_builder_true(jsonlite_builder builder) {
00370     if (builder == NULL) {
00371         return jsonlite_result_invalid_argument;
00372     }
00373 
00374     static const char value[] = "true";
00375     jsonlite_write_state *ws = builder->state;
00376     if (!(jsonlite_builder_accept(builder, jsonlite_accept_value))) {
00377 
00378         return jsonlite_result_not_allowed;
00379     }
00380 
00381     jsonlite_builder_prepare_value_writing(builder);
00382     jsonlite_builder_raw(builder, (char *)value, sizeof(value) - 1);
00383     if (jsonlite_builder_accept(builder, jsonlite_accept_values_only)) {
00384         *ws = jsonlite_accept_continue_array;
00385     } else {
00386         *ws = jsonlite_accept_continue_object;
00387     }
00388     return jsonlite_result_ok;
00389 }
00390 
00391 jsonlite_result jsonlite_builder_false(jsonlite_builder builder) {
00392     if (builder == NULL) {
00393         return jsonlite_result_invalid_argument;
00394     }
00395 
00396     static const char value[] = "false";
00397     jsonlite_write_state *ws = builder->state;
00398     if (!(jsonlite_builder_accept(builder, jsonlite_accept_value))) {
00399         return jsonlite_result_not_allowed;
00400     }
00401 
00402     jsonlite_builder_prepare_value_writing(builder);
00403     jsonlite_builder_raw(builder, (char *)value, sizeof(value) - 1);
00404     if (jsonlite_builder_accept(builder, jsonlite_accept_values_only)) {
00405         *ws = jsonlite_accept_continue_array;
00406     } else {
00407         *ws = jsonlite_accept_continue_object;
00408     }
00409     return jsonlite_result_ok;
00410 }
00411 
00412 jsonlite_result jsonlite_builder_null(jsonlite_builder builder) {
00413     if (builder == NULL) {
00414         return jsonlite_result_invalid_argument;
00415     }
00416 
00417     static const char value[] = "null";
00418     jsonlite_write_state *ws = builder->state;
00419     if (!(jsonlite_builder_accept(builder, jsonlite_accept_value))) {
00420 
00421         return jsonlite_result_not_allowed;
00422     }
00423 
00424     jsonlite_builder_prepare_value_writing(builder);
00425     jsonlite_builder_raw(builder, (char *)value, sizeof(value) - 1);
00426     if (jsonlite_builder_accept(builder, jsonlite_accept_values_only)) {
00427         *ws = jsonlite_accept_continue_array;
00428     } else {
00429         *ws = jsonlite_accept_continue_object;
00430     }
00431     return jsonlite_result_ok;
00432 }
00433 
00434 static void jsonlite_builder_raw(jsonlite_builder builder, const void *data, size_t length) {
00435     jsonlite_stream_write(builder->stream, data, length);
00436 }
00437 
00438 static void jsonlite_builder_repeat(jsonlite_builder builder, const char ch, size_t count) {
00439     ptrdiff_t i = 0;
00440     for (; i < count; i++) {
00441         jsonlite_stream_write(builder->stream, &ch, 1);
00442     }
00443 }
00444 
00445 static  void jsonlite_builder_raw_char(jsonlite_builder builder, char data) {
00446     jsonlite_stream_write(builder->stream, &data, 1);
00447 }
00448 
00449 jsonlite_result jsonlite_builder_raw_key(jsonlite_builder builder, const void *data, size_t length) {
00450     if (builder == NULL || data == NULL || length == 0) {
00451         return jsonlite_result_invalid_argument;
00452     }
00453 
00454     jsonlite_write_state *ws = builder->state;
00455     if (jsonlite_builder_accept(builder, jsonlite_accept_key)) {
00456         if (jsonlite_builder_accept(builder, jsonlite_accept_next)) {
00457             jsonlite_builder_raw(builder, ",", 1);
00458         }
00459 
00460         if (builder->indentation != 0) {
00461             jsonlite_builder_raw_char(builder, '\r');
00462             jsonlite_builder_repeat(builder, ' ', (builder->state - builder->stack) * builder->indentation);
00463         }
00464 
00465         jsonlite_builder_raw_char(builder, '\"');
00466         jsonlite_builder_raw(builder, data, length);
00467         jsonlite_builder_raw_char(builder, '\"');
00468         if (builder->indentation != 0) {
00469             jsonlite_builder_raw(builder, ": ", 2);
00470         } else {
00471             jsonlite_builder_raw_char(builder, ':');
00472         }
00473         *ws = jsonlite_accept_value;
00474         return jsonlite_result_ok;
00475     }
00476 
00477     return jsonlite_result_not_allowed;
00478 }
00479 
00480 jsonlite_result jsonlite_builder_raw_string(jsonlite_builder builder, const void *data, size_t length) {
00481     if (builder == NULL || data == NULL || length == 0) {
00482         return jsonlite_result_invalid_argument;
00483     }
00484 
00485     jsonlite_write_state *ws = builder->state;
00486     if (jsonlite_builder_accept(builder, jsonlite_accept_value)) {
00487         jsonlite_builder_prepare_value_writing(builder);
00488         jsonlite_builder_raw_char(builder, '\"');
00489         jsonlite_builder_raw(builder, data, length);
00490         jsonlite_builder_raw_char(builder, '\"');
00491         if (jsonlite_builder_accept(builder, jsonlite_accept_values_only)) {
00492             *ws = jsonlite_accept_continue_array;
00493         } else {
00494             *ws = jsonlite_accept_continue_object;
00495         }
00496         return jsonlite_result_ok;
00497     }
00498 
00499     return jsonlite_result_not_allowed;
00500 }
00501 
00502 jsonlite_result jsonlite_builder_raw_value(jsonlite_builder builder, const void *data, size_t length) {
00503     if (builder == NULL || data == NULL || length == 0) {
00504         return jsonlite_result_invalid_argument;
00505     }
00506 
00507     jsonlite_write_state *ws = builder->state;
00508     if (jsonlite_builder_accept(builder, jsonlite_accept_value)) {
00509         jsonlite_builder_prepare_value_writing(builder);
00510         jsonlite_builder_raw(builder, data, length);
00511         if (jsonlite_builder_accept(builder, jsonlite_accept_values_only)) {
00512             *ws = jsonlite_accept_continue_array;
00513         } else {
00514             *ws = jsonlite_accept_continue_object;
00515         }
00516         return jsonlite_result_ok;
00517     }
00518 
00519     return jsonlite_result_not_allowed;
00520 }
00521 
00522 static void jsonlite_builder_write_base64(jsonlite_builder builder, const void *data, size_t length) {
00523     static const char encode[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
00524     char buffer[5] = {0};
00525     const uint8_t *c = data;
00526     const uint8_t *l = data + length;
00527     uint32_t bits;
00528     jsonlite_stream_write(builder->stream, "\"", 1);
00529 next:
00530     switch (l - c) {
00531         case 0:
00532             goto done;
00533         case 1:
00534             bits = *c++ << 16;
00535             buffer[0] = encode[(bits & 0x00FC0000) >> 18];
00536             buffer[1] = encode[(bits & 0x0003F000) >> 12];
00537             buffer[2] = '=';
00538             buffer[3] = '=';
00539             l = c;
00540             goto write;
00541         case 2:
00542             bits = *c++ << 16;
00543             bits |= *c++ << 8;
00544             buffer[0] = encode[(bits & 0x00FC0000) >> 18];
00545             buffer[1] = encode[(bits & 0x0003F000) >> 12];
00546             buffer[2] = encode[(bits & 0x00000FC0) >> 6];
00547             buffer[3] = '=';
00548             l = c;
00549             goto write;
00550         default:
00551             bits = *c++ << 16;
00552             bits |= *c++ << 8;
00553             bits |= *c++;
00554             buffer[0] = encode[(bits & 0x00FC0000) >> 18];
00555             buffer[1] = encode[(bits & 0x0003F000) >> 12];
00556             buffer[2] = encode[(bits & 0x00000FC0) >> 6];
00557             buffer[3] = encode[(bits & 0x0000003F)];
00558             goto write;
00559     }
00560 write:
00561     jsonlite_stream_write(builder->stream, buffer, 4);
00562     goto next;
00563 done:
00564     jsonlite_stream_write(builder->stream, "\"", 1);
00565 }
00566 
00567 jsonlite_result jsonlite_builder_base64_value(jsonlite_builder builder, const void *data, size_t length) {
00568     if (builder == NULL || data == NULL || length == 0) {
00569         return jsonlite_result_invalid_argument;
00570     }
00571 
00572     jsonlite_write_state *ws = builder->state;
00573     if (jsonlite_builder_accept(builder, jsonlite_accept_value)) {
00574         jsonlite_builder_prepare_value_writing(builder);
00575         jsonlite_builder_write_base64(builder, data, length);
00576         if (jsonlite_builder_accept(builder, jsonlite_accept_values_only)) {
00577             *ws = jsonlite_accept_continue_array;
00578         } else {
00579             *ws = jsonlite_accept_continue_object;
00580         }
00581         return jsonlite_result_ok;
00582     }
00583 
00584     return jsonlite_result_not_allowed;
00585 
00586 }
00587 //
00588 //  Copyright 2012-2013, Andrii Mamchur
00589 //
00590 //  Licensed under the Apache License, Version 2.0 (the "License");
00591 //  you may not use this file except in compliance with the License.
00592 //  You may obtain a copy of the License at
00593 //
00594 //  http://www.apache.org/licenses/LICENSE-2.0
00595 //
00596 //  Unless required by applicable law or agreed to in writing, software
00597 //  distributed under the License is distributed on an "AS IS" BASIS,
00598 //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00599 //  See the License for the specific language governing permissions and
00600 //  limitations under the License
00601 
00602 #ifndef JSONLITE_AMALGAMATED
00603 #include "jsonlite_parser.h"
00604 #endif
00605 
00606 #include <stdlib.h>
00607 #include <string.h>
00608 
00609 #ifdef _MSC_VER
00610 
00611 #include <intrin.h>
00612 
00613 static uint32_t __inline jsonlite_clz(uint32_t x) {
00614    unsigned long r = 0;
00615    _BitScanForward(&r, x);
00616    return r;
00617 }
00618 
00619 #else
00620 
00621 #define jsonlite_clz(x) __builtin_clz((x))
00622 
00623 #endif
00624 
00625 #define MIN_DEPTH 2
00626 #define CALL_VALUE_CALLBACK(cbs, type, token)   (cbs.type(&cbs.context, token))
00627 #define CALL_STATE_CALLBACK(cbs, type)          (cbs.type(&cbs.context))
00628 
00629 #define HEX_CHAR_TO_INT(c, step)        \
00630 if (0x30 <= *c && *c <= 0x39) {         \
00631     hex_value = *c - 0x30;              \
00632 } else if (0x41 <= *c && *c <= 0x46) {  \
00633     hex_value = *c - 0x37;              \
00634 } else if (0x61 <= *c && *c <= 0x66) {  \
00635     hex_value = *c - 0x57;              \
00636 } else goto error_escape;               \
00637 c += step
00638 
00639 #define CASE_NUMBER_TOKEN_END   \
00640 case 0x09: goto number_parsed;  \
00641 case 0x0A: goto number_parsed;  \
00642 case 0x0D: goto number_parsed;  \
00643 case 0x20: goto number_parsed;  \
00644 case 0x2C: goto number_parsed;  \
00645 case 0x5D: goto number_parsed;  \
00646 case 0x7D: goto number_parsed
00647 
00648 enum {
00649     state_start,
00650     state_object_key,
00651     state_object_key_end,
00652     state_colon,
00653     state_object_comma_end,
00654     state_array_value_end,
00655     state_array_comma_end,
00656     state_key,
00657     state_value,
00658     state_end,
00659 
00660     state_stop = 1 << 7
00661 };
00662 
00663 typedef uint8_t parse_state;
00664 struct jsonlite_parser_struct {
00665     const uint8_t *cursor;
00666     const uint8_t *limit;
00667     const uint8_t *buffer;
00668     uint8_t *buffer_own;
00669 
00670     parse_state *current;
00671     parse_state *last;
00672     parse_state **control;
00673 
00674     jsonlite_result result;
00675     jsonlite_parser_callbacks callbacks;
00676 } jsonlite_parser_struct;
00677 
00678 static void jsonlite_do_parse(jsonlite_parser parser);
00679 static void empty_value_callback(jsonlite_callback_context *ctx, jsonlite_token *t) {}
00680 static void empty_state_callback(jsonlite_callback_context *ctx) {}
00681 
00682 const jsonlite_parser_callbacks jsonlite_default_callbacks = {
00683     &empty_state_callback,
00684     &empty_state_callback,
00685     &empty_state_callback,
00686     &empty_state_callback,
00687     &empty_state_callback,
00688     &empty_state_callback,
00689     &empty_state_callback,
00690     &empty_state_callback,
00691     &empty_value_callback,
00692     &empty_value_callback,
00693     &empty_value_callback,
00694     {NULL, NULL}
00695 };
00696 
00697 size_t jsonlite_parser_estimate_size(size_t depth) {
00698     depth = depth < MIN_DEPTH ? 32 : depth;
00699     return sizeof(jsonlite_parser_struct) + depth * sizeof(parse_state);
00700 }
00701 
00702 static jsonlite_parser jsonlite_parser_configure(void *memory, size_t size) {
00703     size_t depth = (size - sizeof(jsonlite_parser_struct)) / sizeof(parse_state);
00704     jsonlite_parser parser = (jsonlite_parser)memory;
00705     parser->result = jsonlite_result_unknown;
00706     parser->buffer_own = NULL;
00707     parser->callbacks = jsonlite_default_callbacks;
00708     parser->control = NULL;
00709     parser->current = ((uint8_t *)parser + sizeof(jsonlite_parser_struct));
00710     parser->current[0] = state_end;
00711     parser->current[1] = state_start;
00712     parser->last = parser->current + depth;
00713     parser->current++;
00714     return parser;
00715 }
00716 
00717 jsonlite_parser jsonlite_parser_init(size_t depth) {
00718     size_t size = jsonlite_parser_estimate_size(depth);
00719     void *memory = malloc(size);
00720     return jsonlite_parser_configure(memory, size);
00721 }
00722 
00723 jsonlite_parser jsonlite_parser_init_memory(void *memory, size_t size) {
00724     if (memory == NULL) {
00725         return NULL;
00726     }
00727 
00728     if (size < jsonlite_parser_estimate_size(MIN_DEPTH)) {
00729         return NULL;
00730     }
00731 
00732     return jsonlite_parser_configure(memory, size);
00733 }
00734 
00735 jsonlite_result jsonlite_parser_set_callback(jsonlite_parser parser, const jsonlite_parser_callbacks *cbs) {
00736     if (parser == NULL || cbs == NULL) {
00737         return jsonlite_result_invalid_argument;
00738     }
00739 
00740     parser->callbacks = *cbs;
00741     parser->callbacks.context.parser = parser;
00742     return jsonlite_result_ok;
00743 }
00744 
00745 jsonlite_result jsonlite_parser_get_result(jsonlite_parser parser) {
00746     if (parser == NULL) {
00747         return jsonlite_result_invalid_argument;
00748     }
00749 
00750     return parser->result;
00751 }
00752 
00753 jsonlite_result jsonlite_parser_tokenize(jsonlite_parser parser, const void *buffer, size_t size) {
00754     if (parser == NULL || buffer == NULL || size == 0) {
00755         return jsonlite_result_invalid_argument;
00756     }
00757 
00758     if (parser->buffer_own != NULL) {
00759         size_t total_size = size + parser->limit - parser->buffer_own;
00760         uint8_t *b = (uint8_t *)malloc(total_size);
00761         memcpy(b, parser->buffer_own, parser->limit - parser->buffer_own);  // LCOV_EXCL_LINE
00762         memcpy(b + (parser->limit - parser->buffer_own), buffer, size);     // LCOV_EXCL_LINE
00763 
00764         free(parser->buffer_own);
00765 
00766         parser->buffer = b;
00767         parser->buffer_own = b;
00768         parser->cursor = b;
00769         parser->limit = b + total_size;
00770     } else {
00771         parser->buffer = buffer;
00772         parser->cursor = parser->buffer;
00773         parser->limit = parser->buffer + size;
00774     }
00775 
00776     jsonlite_do_parse(parser);
00777     return parser->result;
00778 }
00779 
00780 jsonlite_result jsonlite_parser_resume(jsonlite_parser parser) {
00781     if (parser == NULL) {
00782         return jsonlite_result_invalid_argument;
00783     }
00784 
00785     if (parser->result != jsonlite_result_suspended) {
00786         return jsonlite_result_not_allowed;
00787     }
00788 
00789     jsonlite_do_parse(parser);
00790     return parser->result;
00791 }
00792 
00793 jsonlite_result jsonlite_parser_suspend(jsonlite_parser parser) {
00794     return jsonlite_parser_terminate(parser, jsonlite_result_suspended);
00795 }
00796 
00797 jsonlite_result jsonlite_parser_terminate(jsonlite_parser parser, jsonlite_result result) {
00798     if (parser == NULL) {
00799         return jsonlite_result_invalid_argument;
00800     }
00801 
00802     if (parser->control == NULL) {
00803         return jsonlite_result_not_allowed;
00804     }
00805 
00806     parser->result = result;
00807     **parser->control |= state_stop;
00808     return jsonlite_result_ok;
00809 }
00810 
00811 void jsonlite_parser_release(jsonlite_parser parser) {
00812     jsonlite_parser_cleanup(parser);
00813     free(parser);
00814 }
00815 
00816 void jsonlite_parser_cleanup(jsonlite_parser parser) {
00817     if (parser == NULL) {
00818         return;
00819     }
00820 
00821     free(parser->buffer_own);
00822 }
00823 
00824 static void jsonlite_do_parse(jsonlite_parser parser) {
00825     const uint8_t *c = parser->cursor;
00826     const uint8_t *l = parser->limit;
00827     const uint8_t *token_start = NULL;
00828     const parse_state *last = parser->last;
00829     parse_state *state = parser->current;
00830     jsonlite_token token = {NULL, NULL, NULL, 0};
00831     jsonlite_result result = jsonlite_result_ok;
00832     uint32_t value, utf32;
00833     uint8_t hex_value;
00834 
00835     *state &= ~state_stop;
00836     parser->control = &state;
00837     goto select_state;
00838 
00839 structure_finished:
00840     if (*state == state_end)            goto end;
00841 skip_char_and_whitespaces:
00842     c++;
00843 select_state:
00844     if (c == l)     goto end_of_stream_whitespaces;
00845     if (*c == 0x20) goto skip_char_and_whitespaces;
00846     if (*c == 0x0A) goto skip_char_and_whitespaces;
00847     if (*c == 0x0D) goto skip_char_and_whitespaces;
00848     if (*c == 0x09) goto skip_char_and_whitespaces;
00849     token_start = c;
00850 
00851     switch (*state) {
00852         case state_value:               goto parse_value;
00853         case state_colon:               goto parse_colon;
00854         case state_object_comma_end:    goto parse_object_comma_end;
00855         case state_object_key:          goto parse_key;
00856         case state_object_key_end:      goto parse_key_end;
00857         case state_array_comma_end:     goto parse_array_comma_end;
00858         case state_array_value_end:     goto parse_array_value_end;
00859         case state_key:                 goto parse_string_token;
00860         case state_start:
00861             if (*c == 0x7B)             goto parse_object;
00862             if (*c == 0x5B)             goto parse_array_state;
00863             goto error_exp_ooa;
00864         case state_end:                 goto end;
00865         default:
00866             result = parser->result;
00867             goto end;
00868     }
00869 parse_object:
00870     *state = state_object_key_end;
00871     CALL_STATE_CALLBACK(parser->callbacks, object_start);
00872     goto skip_char_and_whitespaces;
00873 parse_array_state:
00874     *state = state_array_value_end;
00875     CALL_STATE_CALLBACK(parser->callbacks, array_start);
00876     goto skip_char_and_whitespaces;
00877 parse_key:
00878     if (*c != 0x22) goto error_exp_key;
00879     *state = state_colon;
00880     *++state = state_key;
00881     goto parse_string_token;
00882 parse_colon:
00883     if (*c != 0x3A) goto error_exp_colon;
00884     *state = state_object_comma_end;
00885     *++state = state_value;
00886     goto skip_char_and_whitespaces;
00887 parse_key_end:
00888     switch (*c) {
00889         case 0x22:
00890             *state = state_colon;
00891             if (++state == last) goto error_depth;
00892             *state = state_key;
00893             goto parse_string_token;
00894         case 0x7D:
00895             state--;
00896             CALL_STATE_CALLBACK(parser->callbacks, object_end);
00897             goto structure_finished;
00898         default: goto error_exp_koe;
00899     }
00900 parse_object_comma_end:
00901     switch (*c) {
00902         case 0x2C:
00903             *state = state_object_key;
00904             goto skip_char_and_whitespaces;
00905         case 0x7D:
00906             state--;
00907             CALL_STATE_CALLBACK(parser->callbacks, object_end);
00908             goto structure_finished;
00909         default: goto error_exp_coe;
00910     }
00911 parse_array_value_end:
00912     switch (*c) {
00913         case 0x5D:
00914             state--;
00915             CALL_STATE_CALLBACK(parser->callbacks, array_end);
00916             goto structure_finished;
00917         default:
00918             *state = state_array_comma_end;
00919             if (++state == last) goto error_depth;
00920             *state = state_value;
00921             goto parse_value;
00922     }
00923 parse_array_comma_end:
00924     switch (*c) {
00925         case 0x2C:
00926             *++state = state_value;
00927             goto skip_char_and_whitespaces;
00928         case 0x5D:
00929             state--;
00930             CALL_STATE_CALLBACK(parser->callbacks, array_end);
00931             goto structure_finished;
00932         default: goto error_exp_coe;
00933     }
00934 parse_value:
00935     if (0x31 <= *c && *c <= 0x39)   goto parse_digit_leading_number;
00936     if (*c == 0x30)                 goto parse_zero_leading_number;
00937     if (*c == 0x2D)                 goto parse_negative_number;
00938     if (*c == 0x22)                 goto parse_string_token;
00939     if (*c == 0x74)                 goto parse_true_token;
00940     if (*c == 0x66)                 goto parse_false_token;
00941     if (*c == 0x6E)                 goto parse_null_token;
00942     if (*c == 0x7B)                 goto parse_object;
00943     if (*c == 0x5B)                 goto parse_array_state;
00944     goto error_exp_value;
00945 
00946 // Number parsing
00947 parse_negative_number:
00948     token.start = c;
00949     token.type.number = jsonlite_number_int | jsonlite_number_negative;
00950     if (++c == l)                   goto end_of_stream;
00951     if (0x31 <= *c && *c <= 0x39)   goto parse_digits;
00952     if (*c == 0x30)                 goto parse_exponent_fraction;
00953     goto error_number;
00954 parse_zero_leading_number:
00955     token.start = c;
00956     token.type.number = jsonlite_number_int | jsonlite_number_zero_leading;
00957 parse_exponent_fraction:
00958     if (++c == l)                   goto end_of_stream;
00959     switch (*c) {
00960         CASE_NUMBER_TOKEN_END;
00961         case 0x2E:                  goto parse_fraction;
00962         case 0x45:                  goto parse_exponent;
00963         case 0x65:                  goto parse_exponent;
00964         default:                    goto error_number;
00965     }
00966 parse_fraction:
00967     token.type.number |= jsonlite_number_frac;
00968     if (++c == l)                   goto end_of_stream;
00969     if (0x30 <= *c && *c <= 0x39)   goto parse_frac_number;
00970     goto error_number;
00971 parse_frac_number:
00972     if (++c == l)                   goto end_of_stream;
00973     if (0x30 <= *c && *c <= 0x39)   goto parse_frac_number;
00974     switch (*c) {
00975         CASE_NUMBER_TOKEN_END;
00976         case 0x45:                  goto parse_exponent;
00977         case 0x65:                  goto parse_exponent;
00978         default:                    goto error_number;
00979     }
00980 parse_exponent:
00981     token.type.number |= jsonlite_number_exp;
00982     if (++c == l)                   goto end_of_stream;
00983     if (0x30 <= *c && *c <= 0x39)   goto parse_exponent_number;
00984     switch(*c) {
00985         case 0x2B:                  goto parse_exponent_sign;
00986         case 0x2D:                  goto parse_exponent_sign;
00987         default:                    goto error_number;
00988     }
00989 parse_exponent_sign:
00990     if (++c == l)                   goto end_of_stream;
00991     if (0x30 <= *c && *c <= 0x39)   goto parse_exponent_number;
00992     goto error_number;
00993 parse_exponent_number:
00994     if (++c == l)                   goto end_of_stream;
00995     if (0x30 <= *c && *c <= 0x39)   goto parse_exponent_number;
00996     switch(*c) {
00997         CASE_NUMBER_TOKEN_END;
00998         default:                    goto error_number;
00999     }
01000 parse_digit_leading_number:
01001     token.start = c;
01002     token.type.number = jsonlite_number_int | jsonlite_number_digit_leading;
01003 parse_digits:
01004     if (++c == l)                   goto end_of_stream;
01005     if (0x30 <= *c && *c <= 0x39)   goto parse_digits;
01006     switch(*c) {
01007         CASE_NUMBER_TOKEN_END;
01008         case 0x2E:                  goto parse_fraction;
01009         case 0x45:                  goto parse_exponent;
01010         case 0x65:                  goto parse_exponent;
01011         default:                    goto error_number;
01012     }
01013 number_parsed:
01014     token.end = c;
01015     state--;
01016     CALL_VALUE_CALLBACK(parser->callbacks, number_found, &token);
01017     goto select_state;
01018 
01019 // String parsing
01020 parse_string_token:
01021     token.type.string = jsonlite_string_ascii;
01022     token.start = c + 1;
01023 next_char:
01024     if (++c == l)       goto end_of_stream;
01025     if (*c == 0x22)     goto string_parsed;
01026     if (*c == 0x5C)     goto escaped;
01027     if (*c > 0x7F)      goto utf8;
01028     if (*c < 0x20)      goto error_token;
01029     goto next_char;
01030 escaped:
01031     token.type.string |= jsonlite_string_escape;
01032     if (++c == l)       goto end_of_stream;
01033     switch (*c) {
01034         case 0x22:      goto next_char;     // "    quotation mark  U+0022
01035         case 0x2F:      goto next_char;     // /    solidus         U+002F
01036         case 0x5C:      goto next_char;     // \    reverse solidus U+005C
01037         case 0x62:      goto next_char;     // b    backspace       U+0008
01038         case 0x66:      goto next_char;     // f    form feed       U+000C
01039         case 0x6E:      goto next_char;     // n    line feed       U+000A
01040         case 0x72:      goto next_char;     // r    carriage return U+000D
01041         case 0x74:      goto next_char;     // t    tab             U+0009
01042         case 0x75:      goto hex;           // uXXXX                U+XXXX
01043         default:        goto error_escape;
01044     }
01045 hex:
01046     token.type.string |= jsonlite_string_unicode_escape;
01047     if (c++ + 4 >= l)       goto end_of_stream;
01048     HEX_CHAR_TO_INT(c, 1);  value = hex_value;
01049     HEX_CHAR_TO_INT(c, 1);  value = (uint32_t)(value << 4) | hex_value;
01050     HEX_CHAR_TO_INT(c, 1);  value = (uint32_t)(value << 4) | hex_value;
01051     HEX_CHAR_TO_INT(c, 0);  value = (uint32_t)(value << 4) | hex_value;
01052 
01053     if ((value & 0xFFFFu) >= 0xFFFEu)           token.type.string |= jsonlite_string_unicode_noncharacter;
01054     if (value >= 0xFDD0u && value <= 0xFDEFu)   token.type.string |= jsonlite_string_unicode_noncharacter;
01055     if (0xD800 > value || value > 0xDBFF)       goto next_char;
01056 
01057     // UTF-16 Surrogate
01058     utf32 = (value - 0xD800) << 10;
01059     if (c++ + 6 >= l)       goto end_of_stream;
01060     if (*c++ != 0x5C)       goto error_escape;
01061     if (*c++ != 0x75)       goto error_escape;
01062     HEX_CHAR_TO_INT(c, 1);  value = hex_value;
01063     HEX_CHAR_TO_INT(c, 1);  value = (uint32_t)(value << 4) | hex_value;
01064     HEX_CHAR_TO_INT(c, 1);  value = (uint32_t)(value << 4) | hex_value;
01065     HEX_CHAR_TO_INT(c, 0);  value = (uint32_t)(value << 4) | hex_value;
01066 
01067     if (value < 0xDC00 || value > 0xDFFF)       goto error_escape;
01068     utf32 += value - 0xDC00 + 0x10000;
01069     if ((utf32 & 0x0FFFFu) >= 0x0FFFEu)         token.type.string |= jsonlite_string_unicode_noncharacter;
01070     goto next_char;
01071 utf8:
01072     token.type.string |= jsonlite_string_utf8;
01073     int res = jsonlite_clz((unsigned int)((*c) ^ 0xFF) << 0x19);
01074     utf32 = (*c & (0xFF >> (res + 1)));
01075     value = 0xAAAAAAAA; // == 1010...
01076     if (c + res >= l) goto end_of_stream;
01077     switch (res) {
01078         case 3: value = (value << 2) | (*++c >> 6); utf32 = (utf32 << 6) | (*c & 0x3F);
01079         case 2: value = (value << 2) | (*++c >> 6); utf32 = (utf32 << 6) | (*c & 0x3F);
01080         case 1: value = (value << 2) | (*++c >> 6); utf32 = (utf32 << 6) | (*c & 0x3F);
01081             if (value != 0xAAAAAAAA)                    goto error_utf8;
01082             if ((utf32 & 0xFFFFu) >= 0xFFFEu)           token.type.string |= jsonlite_string_unicode_noncharacter;
01083             if (utf32 >= 0xFDD0u && utf32 <= 0xFDEFu)   token.type.string |= jsonlite_string_unicode_noncharacter;
01084     }
01085     goto next_char;
01086 string_parsed:
01087     token.end = c;
01088     parser->cursor = c + 1;
01089     if (*state-- == state_value) {
01090         CALL_VALUE_CALLBACK(parser->callbacks, string_found, &token);
01091     } else {
01092         CALL_VALUE_CALLBACK(parser->callbacks, key_found, &token);
01093     }
01094     goto skip_char_and_whitespaces;
01095 
01096 // Primitive tokens
01097 parse_true_token:
01098     if (c++ + 3 >= l)   goto end_of_stream;
01099     if (*c++ != 0x72)   goto error_token;
01100     if (*c++ != 0x75)   goto error_token;
01101     if (*c++ != 0x65)   goto error_token;
01102     state--;
01103     CALL_STATE_CALLBACK(parser->callbacks, true_found);
01104     goto select_state;
01105 parse_false_token:
01106     if (c++ + 4 >= l)   goto end_of_stream;
01107     if (*c++ != 0x61)   goto error_token;
01108     if (*c++ != 0x6C)   goto error_token;
01109     if (*c++ != 0x73)   goto error_token;
01110     if (*c++ != 0x65)   goto error_token;
01111     state--;
01112     CALL_STATE_CALLBACK(parser->callbacks, false_found);
01113     goto select_state;
01114 parse_null_token:
01115     if (c++ + 3 >= l)   goto end_of_stream;
01116     if (*c++ != 0x75)   goto error_token;
01117     if (*c++ != 0x6C)   goto error_token;
01118     if (*c++ != 0x6C)   goto error_token;
01119     state--;
01120     CALL_STATE_CALLBACK(parser->callbacks, null_found);
01121     goto select_state;
01122 
01123 // Error states.
01124 error_depth:        result = jsonlite_result_depth_limit;               goto end;
01125 error_exp_ooa:      result = jsonlite_result_expected_object_or_array;  goto end;
01126 error_exp_value:    result = jsonlite_result_expected_value;            goto end;
01127 error_exp_koe:      result = jsonlite_result_expected_key_or_end;       goto end;
01128 error_exp_key:      result = jsonlite_result_expected_key;              goto end;
01129 error_exp_colon:    result = jsonlite_result_expected_colon;            goto end;
01130 error_exp_coe:      result = jsonlite_result_expected_comma_or_end;     goto end;
01131 error_escape:       result = jsonlite_result_invalid_escape;            goto end;
01132 error_number:       result = jsonlite_result_invalid_number;            goto end;
01133 error_token:        result = jsonlite_result_invalid_token;             goto end;
01134 error_utf8:         result = jsonlite_result_invalid_utf8;              goto end;
01135 
01136 // End of stream states.
01137 end_of_stream_whitespaces:
01138     token_start = l;
01139 end_of_stream:
01140     result = jsonlite_result_end_of_stream;
01141 end:
01142     parser->result = result;
01143     parser->current = state;
01144     parser->control = NULL;
01145     parser->cursor = c;
01146     parser->callbacks.parse_finished(&parser->callbacks.context);
01147     if (result != jsonlite_result_end_of_stream) {
01148         return;
01149     }
01150 
01151     res = parser->buffer_own != NULL;
01152     if ((parser->limit - token_start) > 0) {
01153         parser->buffer_own = malloc(parser->limit - token_start);       // LCOV_EXCL_LINE
01154         parser->limit = parser->buffer_own + (parser->limit - token_start);
01155         memcpy(parser->buffer_own, token_start, parser->limit - parser->buffer_own);
01156         if (res) {
01157             free((void *)parser->buffer);
01158             parser->buffer = parser->buffer_own;
01159         }
01160     } else {
01161         if (res) {
01162             free((void *)parser->buffer_own);
01163             parser->buffer = NULL;
01164             parser->buffer_own = NULL;
01165         }
01166     }
01167 }
01168 //
01169 //  Copyright 2012-2013, Andrii Mamchur
01170 //
01171 //  Licensed under the Apache License, Version 2.0 (the "License");
01172 //  you may not use this file except in compliance with the License.
01173 //  You may obtain a copy of the License at
01174 //
01175 //  http://www.apache.org/licenses/LICENSE-2.0
01176 //
01177 //  Unless required by applicable law or agreed to in writing, software
01178 //  distributed under the License is distributed on an "AS IS" BASIS,
01179 //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
01180 //  See the License for the specific language governing permissions and
01181 //  limitations under the License
01182 
01183 #ifndef JSONLITE_AMALGAMATED
01184 #include "../include/jsonlite_stream.h"
01185 #endif
01186 
01187 #include <stdlib.h>
01188 #include <string.h>
01189 
01190 struct jsonlite_stream_struct {
01191     jsonlite_stream_write_fn write;
01192     jsonlite_stream_release_fn release;
01193 } jsonlite_stream_struct;
01194 
01195 int jsonlite_stream_write(jsonlite_stream stream, const void *data, size_t length) {
01196     return stream->write(stream, data, length);
01197 }
01198 
01199 void jsonlite_stream_release(jsonlite_stream stream) {
01200     if (stream == NULL) {
01201         return;
01202     }
01203 
01204     if (stream->release != NULL) {
01205         stream->release(stream);
01206     }
01207 }
01208 
01209 static void jsonlite_stream_free_mem(jsonlite_stream stream) {
01210     free((void *)stream);
01211 }
01212 
01213 #define CAST_TO_MEM_STREAM(S)   (jsonlite_mem_stream *)((uint8_t *)(S) + sizeof(jsonlite_stream_struct))
01214 #define SIZE_OF_MEM_STREAM()    (sizeof(jsonlite_stream_struct) + sizeof(jsonlite_mem_stream))
01215 
01216 typedef struct jsonlite_mem_stream_block {
01217     struct jsonlite_mem_stream_block *next;
01218     uint8_t *data;
01219 } jsonlite_mem_stream_block;
01220 
01221 typedef struct jsonlite_mem_stream {
01222     size_t block_size;
01223     uint8_t *cursor;
01224     uint8_t *limit;
01225     struct jsonlite_mem_stream_block *current;
01226     struct jsonlite_mem_stream_block *first;
01227 } jsonlite_mem_stream;
01228 
01229 static int jsonlite_mem_stream_write(jsonlite_stream stream, const void *data, size_t length) {
01230     jsonlite_mem_stream *mem_stream = CAST_TO_MEM_STREAM(stream);
01231     size_t write_limit = mem_stream->limit - mem_stream->cursor;
01232     if (write_limit >= length) {
01233         memcpy(mem_stream->cursor, data, length); // LCOV_EXCL_LINE
01234         mem_stream->cursor += length;
01235     } else {
01236         memcpy(mem_stream->cursor, data, write_limit); // LCOV_EXCL_LINE
01237         mem_stream->cursor += write_limit;
01238 
01239         size_t size = sizeof(jsonlite_mem_stream_block) + mem_stream->block_size;
01240         jsonlite_mem_stream_block *block = malloc(size);
01241         block->data = (uint8_t *)block + sizeof(jsonlite_mem_stream_block);
01242         block->next = NULL;
01243 
01244         mem_stream->current->next = block;
01245         mem_stream->current = block;
01246         mem_stream->cursor = block->data;
01247         mem_stream->limit = block->data + mem_stream->block_size;
01248 
01249         jsonlite_mem_stream_write(stream, (char *)data + write_limit, length - write_limit);
01250     }
01251 
01252     return (int)length;
01253 }
01254 
01255 static void jsonlite_mem_stream_release(jsonlite_stream stream) {
01256     jsonlite_mem_stream *mem_stream = CAST_TO_MEM_STREAM(stream);
01257     jsonlite_mem_stream_block *block = mem_stream->first;
01258     void *prev;
01259     for (; block != NULL;) {
01260         prev = block;
01261         block = block->next;
01262         free(prev);
01263     }
01264 
01265     free((void *)stream);
01266 }
01267 
01268 jsonlite_stream jsonlite_mem_stream_init(size_t block_size) {
01269     size_t size = SIZE_OF_MEM_STREAM();
01270     struct jsonlite_stream_struct *stream = malloc(size);
01271     stream->write = jsonlite_mem_stream_write;
01272     stream->release = jsonlite_mem_stream_release;
01273 
01274     jsonlite_mem_stream_block *first = malloc(sizeof(jsonlite_mem_stream_block) + block_size);
01275     first->data = (uint8_t *)first + sizeof(jsonlite_mem_stream_block);
01276     first->next = NULL;
01277 
01278     jsonlite_mem_stream *mem_stream = CAST_TO_MEM_STREAM(stream);
01279     mem_stream->block_size = block_size;
01280     mem_stream->cursor = first->data;
01281     mem_stream->limit = first->data + block_size;
01282     mem_stream->current = first;
01283     mem_stream->first = first;
01284     return stream;
01285 }
01286 
01287 size_t jsonlite_mem_stream_data(jsonlite_stream stream, uint8_t **data, size_t extra_bytes) {
01288     jsonlite_mem_stream *mem_stream = CAST_TO_MEM_STREAM(stream);
01289     jsonlite_mem_stream_block *block = NULL;
01290     uint8_t *buff = NULL;
01291     size_t size = 0;
01292 
01293     for (block = mem_stream->first; block != NULL; block = block->next) {
01294         if (block->next != NULL) {
01295             size += mem_stream->block_size;
01296         } else {
01297             size += mem_stream->cursor - block->data;
01298         }
01299     }
01300 
01301     if (size == 0) {
01302         *data = NULL;
01303     } else {
01304         *data = (uint8_t *)malloc(size + extra_bytes);
01305         buff = *data;
01306         for (block = mem_stream->first; block != NULL; block = block->next) {
01307             if (block->next != NULL) {
01308                 memcpy(buff, block->data, mem_stream->block_size); // LCOV_EXCL_LINE
01309                 buff += mem_stream->block_size;
01310             } else {
01311                 memcpy(buff, block->data, mem_stream->cursor - block->data); // LCOV_EXCL_LINE
01312             }
01313         }
01314     }
01315 
01316     return size;
01317 }
01318 
01319 #define CAST_TO_STATIC_MEM_STREAM(S)   (jsonlite_static_mem_stream *)((uint8_t *)(S) + sizeof(jsonlite_stream_struct))
01320 #define SIZE_OF_STATIC_MEM_STREAM()    (sizeof(jsonlite_stream_struct) + sizeof(jsonlite_static_mem_stream))
01321 
01322 typedef struct jsonlite_static_mem_stream {
01323     uint8_t *buffer;
01324     size_t size;
01325     size_t written;
01326     uint8_t *limit;
01327     int enabled;
01328 } jsonlite_static_mem_stream;
01329 
01330 static int jsonlite_static_mem_stream_write(jsonlite_stream stream, const void *data, size_t length) {
01331     jsonlite_static_mem_stream *mem_stream = CAST_TO_STATIC_MEM_STREAM(stream);
01332     size_t write_limit = mem_stream->size - mem_stream->written;
01333     if (mem_stream->enabled && write_limit >= length) {
01334         memcpy(mem_stream->buffer + mem_stream->written, data, length); // LCOV_EXCL_LINE
01335         mem_stream->written += length;
01336     } else {
01337         mem_stream->enabled = 0;
01338         return 0;
01339     }
01340 
01341     return (int)length;
01342 }
01343 
01344 jsonlite_stream jsonlite_static_mem_stream_init(void *buffer, size_t size) {
01345     size_t s = SIZE_OF_STATIC_MEM_STREAM();
01346     struct jsonlite_stream_struct *stream = malloc(s);
01347     stream->write = jsonlite_static_mem_stream_write;
01348     stream->release = jsonlite_stream_free_mem;
01349 
01350     jsonlite_static_mem_stream *mem_stream = CAST_TO_STATIC_MEM_STREAM(stream);
01351     mem_stream->buffer = buffer;
01352     mem_stream->size = size;
01353     mem_stream->written = 0;
01354     mem_stream->enabled = 1;
01355     return stream;
01356 }
01357 
01358 size_t jsonlite_static_mem_stream_written_bytes(jsonlite_stream stream) {
01359     jsonlite_static_mem_stream *mem_stream = CAST_TO_STATIC_MEM_STREAM(stream);
01360     return mem_stream->written;
01361 }
01362 
01363 #define CAST_TO_FILE_STREAM(S)   (jsonlite_file_stream *)((uint8_t *)(S) + sizeof(jsonlite_stream_struct))
01364 #define SIZE_OF_FILE_STREAM()    (sizeof(jsonlite_stream_struct) + sizeof(jsonlite_file_stream))
01365 
01366 typedef struct jsonlite_file_stream {
01367     FILE *file;
01368 } jsonlite_file_stream;
01369 
01370 static int jsonlite_file_stream_write(jsonlite_stream stream, const void *data, size_t length) {
01371     jsonlite_file_stream *file_stream = CAST_TO_FILE_STREAM(stream);
01372     return (int)fwrite(data, 1, length, file_stream->file);
01373 }
01374 
01375 jsonlite_stream jsonlite_file_stream_init(FILE *file) {
01376     size_t size = SIZE_OF_FILE_STREAM();
01377     struct jsonlite_stream_struct *stream = malloc(size);
01378     stream->write = jsonlite_file_stream_write;
01379     stream->release = jsonlite_stream_free_mem;
01380 
01381     jsonlite_file_stream *file_stream = CAST_TO_FILE_STREAM(stream);
01382     file_stream->file = file;
01383     return stream;
01384 }
01385 
01386 static int jsonlite_null_stream_write(jsonlite_stream stream, const void *data, size_t length) {
01387     return (int)length;
01388 }
01389 
01390 static int jsonlite_stdout_stream_write(jsonlite_stream stream, const void *data, size_t length) {
01391     return (int)fwrite(data, 1, length, stdout);
01392 }
01393 
01394 static struct jsonlite_stream_struct jsonlite_stdout_stream_struct = {jsonlite_stdout_stream_write, NULL};
01395 static struct jsonlite_stream_struct jsonlite_null_stream_struct = {jsonlite_null_stream_write, NULL};
01396 
01397 jsonlite_stream jsonlite_stdout_stream = &jsonlite_stdout_stream_struct;
01398 jsonlite_stream jsonlite_null_stream = &jsonlite_null_stream_struct;
01399 //
01400 //  Copyright 2012-2013, Andrii Mamchur
01401 //
01402 //  Licensed under the Apache License, Version 2.0 (the "License");
01403 //  you may not use this file except in compliance with the License.
01404 //  You may obtain a copy of the License at
01405 //
01406 //  http://www.apache.org/licenses/LICENSE-2.0
01407 //
01408 //  Unless required by applicable law or agreed to in writing, software
01409 //  distributed under the License is distributed on an "AS IS" BASIS,
01410 //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
01411 //  See the License for the specific language governing permissions and
01412 //  limitations under the License
01413 
01414 #ifndef JSONLITE_AMALGAMATED
01415 #include "../include/jsonlite_token.h"
01416 #endif
01417 
01418 #include <stdlib.h>
01419 
01420 #ifdef _MSC_VER
01421 #include <intrin.h>
01422 
01423 static uint32_t __inline jsonlite_clz( uint32_t x ) {
01424    unsigned long r = 0;
01425    _BitScanForward(&r, x);
01426    return r;
01427 }
01428 
01429 #else
01430 
01431 #define jsonlite_clz(x) __builtin_clz((x))
01432 
01433 #endif
01434 
01435 static uint8_t jsonlite_hex_char_to_uint8(uint8_t c) {
01436     if (c >= 'a') {
01437         return c - 'a' + 10;
01438     }
01439 
01440     if (c >= 'A') {
01441         return c - 'A' + 10;
01442     }
01443 
01444     return c - '0';
01445 }
01446 
01447 static int unicode_char_to_utf16(uint32_t ch, uint16_t *utf16) {
01448     uint32_t v = ch - 0x10000;
01449     uint32_t vh = v >> 10;
01450     uint32_t vl = v & 0x3FF;
01451     if (ch <= 0xFFFF) {
01452         *utf16 = (uint16_t)ch;
01453         return 1;
01454     }
01455 
01456     *utf16++ = (uint16_t)(0xD800 + vh);
01457     *utf16 = (uint16_t)(0xDC00 + vl);
01458     return 2;
01459 }
01460 
01461 size_t jsonlite_token_size_of_uft8(jsonlite_token *ts) {
01462     return (size_t)(ts->end - ts->start + 1);
01463 }
01464 
01465 size_t jsonlite_token_to_uft8(jsonlite_token *ts, uint8_t **buffer) {
01466     size_t size = jsonlite_token_size_of_uft8(ts);
01467     const uint8_t *p = ts->start;
01468     const uint8_t *l = ts->end;
01469     uint32_t value, utf32;
01470     uint8_t *c = *buffer = (uint8_t *)malloc(size);
01471     int res;
01472 step:
01473     if (p == l)         goto done;
01474     if (*p == '\\')     goto escaped;
01475     if (*p >= 0x80)     goto utf8;
01476     *c++ = *p++;
01477     goto step;
01478 escaped:
01479     switch (*++p) {
01480         case 34:    *c++ = '"';     p++; goto step;
01481         case 47:    *c++ = '/';     p++; goto step;
01482         case 92:    *c++ = '\\';    p++; goto step;
01483         case 98:    *c++ = '\b';    p++; goto step;
01484         case 102:   *c++ = '\f';    p++; goto step;
01485         case 110:   *c++ = '\n';    p++; goto step;
01486         case 114:   *c++ = '\r';    p++; goto step;
01487         case 116:   *c++ = '\t';    p++; goto step;
01488     }
01489 
01490     // UTF-16
01491     p++;
01492     utf32 = jsonlite_hex_char_to_uint8(*p++);
01493     utf32 = (uint32_t)(utf32 << 4) | jsonlite_hex_char_to_uint8(*p++);
01494     utf32 = (uint32_t)(utf32 << 4) | jsonlite_hex_char_to_uint8(*p++);
01495     utf32 = (uint32_t)(utf32 << 4) | jsonlite_hex_char_to_uint8(*p++);
01496     if (0xD800 > utf32 || utf32 > 0xDBFF) goto encode;
01497 
01498     // UTF-16 Surrogate
01499     p += 2;
01500     utf32 = (utf32 - 0xD800) << 10;
01501     value = jsonlite_hex_char_to_uint8(*p++);
01502     value = (uint32_t)(value << 4) | jsonlite_hex_char_to_uint8(*p++);
01503     value = (uint32_t)(value << 4) | jsonlite_hex_char_to_uint8(*p++);
01504     value = (uint32_t)(value << 4) | jsonlite_hex_char_to_uint8(*p++);
01505     utf32 += value - 0xDC00 + 0x10000;
01506 encode:
01507     if (utf32 < 0x80) {
01508         *c++ = (uint8_t)utf32;
01509     } else if (utf32 < 0x0800) {
01510         c[1] = (uint8_t)(utf32 & 0x3F) | 0x80;
01511         utf32 = utf32 >> 6;
01512         c[0] = (uint8_t)utf32 | 0xC0;
01513         c += 2;
01514     } else if (utf32 < 0x10000) {
01515         c[2] = (uint8_t)(utf32 & 0x3F) | 0x80;
01516         utf32 = utf32 >> 6;
01517         c[1] = (uint8_t)(utf32 & 0x3F) | 0x80;
01518         utf32 = utf32 >> 6;
01519         c[0] = (uint8_t)utf32 | 0xE0;
01520         c += 3;
01521     } else {
01522         c[3] = (uint8_t)(utf32 & 0x3F) | 0x80;
01523         utf32 = utf32 >> 6;
01524         c[2] = (uint8_t)(utf32 & 0x3F) | 0x80;
01525         utf32 = utf32 >> 6;
01526         c[1] = (uint8_t)(utf32 & 0x3F) | 0x80;
01527         utf32 = utf32 >> 6;
01528         c[0] = (uint8_t)utf32 | 0xF0;
01529         c += 4;
01530     }
01531     goto step;
01532 utf8:
01533     res = jsonlite_clz(((*p) ^ 0xFF) << 0x19);
01534     *c++ = *p++;
01535     switch (res) {
01536         case 3: *c++ = *p++;
01537         case 2: *c++ = *p++;
01538         case 1: *c++ = *p++;
01539     }
01540     goto step;
01541 done:
01542     *c = 0;
01543     return c - *buffer;
01544 }
01545 
01546 size_t jsonlite_token_size_of_uft16(jsonlite_token *ts) {
01547     return (ts->end - ts->start + 1) * sizeof(uint16_t);
01548 }
01549 
01550 size_t jsonlite_token_to_uft16(jsonlite_token *ts, uint16_t **buffer) {
01551     size_t size = jsonlite_token_size_of_uft16(ts);
01552     const uint8_t *p = ts->start;
01553     const uint8_t *l = ts->end;
01554     uint16_t utf16;
01555     uint16_t *c = *buffer = (uint16_t *)malloc(size);
01556     int res;
01557 step:
01558     if (p == l)         goto done;
01559     if (*p == '\\')     goto escaped;
01560     if (*p >= 0x80)     goto utf8;
01561     *c++ = *p++;
01562     goto step;
01563 escaped:
01564     switch (*++p) {
01565         case 34:    *c++ = '"';     p++; goto step;
01566         case 47:    *c++ = '/';     p++; goto step;
01567         case 92:    *c++ = '\\';    p++; goto step;
01568         case 98:    *c++ = '\b';    p++; goto step;
01569         case 102:   *c++ = '\f';    p++; goto step;
01570         case 110:   *c++ = '\n';    p++; goto step;
01571         case 114:   *c++ = '\r';    p++; goto step;
01572         case 116:   *c++ = '\t';    p++; goto step;
01573     }
01574 
01575     // UTF-16
01576     p++;
01577     utf16 = jsonlite_hex_char_to_uint8(*p++);
01578     utf16 = (uint16_t)(utf16 << 4) | jsonlite_hex_char_to_uint8(*p++);
01579     utf16 = (uint16_t)(utf16 << 4) | jsonlite_hex_char_to_uint8(*p++);
01580     utf16 = (uint16_t)(utf16 << 4) | jsonlite_hex_char_to_uint8(*p++);
01581     *c++ = utf16;
01582     if (0xD800 > utf16 || utf16 > 0xDBFF) goto step;
01583 
01584     // UTF-16 Surrogate
01585     p += 2;
01586     utf16 = jsonlite_hex_char_to_uint8(*p++);
01587     utf16 = (uint16_t)(utf16 << 4) | jsonlite_hex_char_to_uint8(*p++);
01588     utf16 = (uint16_t)(utf16 << 4) | jsonlite_hex_char_to_uint8(*p++);
01589     utf16 = (uint16_t)(utf16 << 4) | jsonlite_hex_char_to_uint8(*p++);
01590     *c++ = utf16;
01591     goto step;
01592 utf8:
01593     res = jsonlite_clz(((*p) ^ 0xFF) << 0x19);
01594     uint32_t code = (*p & (0xFF >> (res + 1)));
01595     switch (res) {
01596         case 3: code = (code << 6) | (*++p & 0x3F);
01597         case 2: code = (code << 6) | (*++p & 0x3F);
01598         case 1: code = (code << 6) | (*++p & 0x3F);
01599         case 0: ++p;
01600     }
01601 
01602     c += unicode_char_to_utf16(code, c);
01603     goto step;
01604 done:
01605     *c = 0;
01606     return (c - *buffer) * sizeof(uint16_t);
01607 }
01608 
01609 size_t jsonlite_token_size_of_base64_binary(jsonlite_token *ts) {
01610     return (((ts->end - ts->start) * 3) / 4 + 3) & ~3;
01611 }
01612 
01613 size_t jsonlite_token_base64_to_binary(jsonlite_token *ts, void **buffer) {
01614     size_t length = 0;
01615     size_t size = jsonlite_token_size_of_base64_binary(ts);
01616     const uint8_t *p = ts->start;
01617     const uint8_t *l = ts->end;
01618     uint8_t *c;
01619     size_t bytes, i;
01620     if (size > 0) {
01621         c = *buffer = (uint16_t *)malloc(size);
01622     } else {
01623         *buffer = NULL;
01624         goto error;
01625     }
01626 next:
01627     bytes = 0;
01628     i = 0;
01629     do {
01630         if (p == l) goto error;
01631         i++;
01632         bytes <<= 6;
01633         if (0x41 <= *p && *p <= 0x5A) { bytes |= *p++ - 0x41; continue; }
01634         if (0x61 <= *p && *p <= 0x7A) { bytes |= *p++ - 0x47; continue; }
01635         if (0x30 <= *p && *p <= 0x39) { bytes |= *p++ + 0x04; continue; }
01636         if (*p == 0x2B) { bytes |= 0x3E; p++; continue; }
01637         if (*p == 0x2F) { bytes |= 0x3F; p++; continue; }
01638         if (*p == '=') {
01639             switch (l - p) {
01640                 case 1:
01641                     *c++ = (uint8_t)((bytes >> 16) & 0x000000FF);
01642                     *c = (uint8_t)((bytes >> 8) & 0x000000FF);
01643                     return length + 2;
01644                 case 2:
01645                     *c = (uint8_t)((bytes >> 10) & 0x000000FF);
01646                     return length + 1;
01647             }
01648         }
01649         if (*p == 0x5C && *++p == 0x2F) { bytes |= 0x3F; p++; continue; }
01650         goto error;
01651     } while (i < 4);
01652 
01653     *c++ = (uint8_t)((bytes >> 16)  & 0x000000FF);
01654     *c++ = (uint8_t)((bytes >> 8)   & 0x000000FF);
01655     *c++ = (uint8_t)((bytes)        & 0x000000FF);
01656     length += 3;
01657 
01658     if (p == l) goto done;
01659     goto next;
01660 error:
01661     free(*buffer);
01662     *buffer = NULL;
01663     length = 0;
01664 done:
01665     return length;
01666 }
01667 
01668 long jsonlite_token_to_long(jsonlite_token *token) {
01669     long res = 0;
01670     int negative = (token->type.number & jsonlite_number_negative) == jsonlite_number_negative;
01671     ptrdiff_t length = token->end - token->start - negative;
01672     const uint8_t *c = token->start + negative;
01673     switch (length & 3) {
01674         for (; length > 0; length -= 4) {
01675             case 0: res = res * 10 + *c++ - '0';
01676             case 3: res = res * 10 + *c++ - '0';
01677             case 2: res = res * 10 + *c++ - '0';
01678             case 1: res = res * 10 + *c++ - '0';
01679         }
01680     }
01681 
01682     return negative ? -res : res;
01683 }
01684 
01685 long long jsonlite_token_to_long_long(jsonlite_token *token) {
01686     long long res = 0;
01687     int negative = (token->type.number & jsonlite_number_negative) == jsonlite_number_negative;
01688     ptrdiff_t length = token->end - token->start - negative;
01689     const uint8_t *c = token->start + negative;
01690     switch (length & 7) {
01691         for (; length > 0; length -= 8) {
01692             case 0: res = res * 10 + *c++ - '0';
01693             case 7: res = res * 10 + *c++ - '0';
01694             case 6: res = res * 10 + *c++ - '0';
01695             case 5: res = res * 10 + *c++ - '0';
01696             case 4: res = res * 10 + *c++ - '0';
01697             case 3: res = res * 10 + *c++ - '0';
01698             case 2: res = res * 10 + *c++ - '0';
01699             case 1: res = res * 10 + *c++ - '0';
01700         }
01701     }
01702 
01703     return negative ? -res : res;
01704 }
01705 //
01706 //  Copyright 2012-2013, Andrii Mamchur
01707 //
01708 //  Licensed under the Apache License, Version 2.0 (the "License");
01709 //  you may not use this file except in compliance with the License.
01710 //  You may obtain a copy of the License at
01711 //
01712 //  http://www.apache.org/licenses/LICENSE-2.0
01713 //
01714 //  Unless required by applicable law or agreed to in writing, software
01715 //  distributed under the License is distributed on an "AS IS" BASIS,
01716 //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
01717 //  See the License for the specific language governing permissions and
01718 //  limitations under the License
01719 
01720 #ifndef JSONLITE_AMALGAMATED
01721 #include "../include/jsonlite_token_pool.h"
01722 #endif
01723 
01724 #include <stdlib.h>
01725 #include <string.h>
01726 
01727 #define JSONLITE_TOKEN_POOL_FRONT 0x80
01728 #define JSONLITE_TOKEN_POOL_FRONT_MASK (JSONLITE_TOKEN_POOL_FRONT - 1)
01729 
01730 typedef struct jsonlite_token_block {
01731     jsonlite_token_bucket *buckets;
01732     size_t capacity;
01733 } jsonlite_token_block;
01734 
01735 typedef struct jsonlite_token_pool_struct {
01736     jsonlite_token_block blocks[JSONLITE_TOKEN_POOL_FRONT];
01737     uint8_t *content_pool;
01738     size_t content_pool_size;
01739     jsonlite_token_pool_release_value_fn release_fn;
01740 
01741 } jsonlite_token_pool_struct;
01742 
01743 static void jsonlite_extend_capacity(jsonlite_token_pool pool, ptrdiff_t index);
01744 static uint32_t jsonlite_hash(const uint8_t *data, size_t len);
01745 static jsonlite_token_bucket terminate_bucket = {NULL, NULL, 0, 0, NULL};
01746 
01747 jsonlite_token_pool jsonlite_token_pool_create(jsonlite_token_pool_release_value_fn release_fn) {
01748     jsonlite_token_pool pool = (jsonlite_token_pool)malloc(sizeof(jsonlite_token_pool_struct));
01749     int i;
01750     for (i = 0; i < JSONLITE_TOKEN_POOL_FRONT; i++) {
01751         pool->blocks[i].buckets = &terminate_bucket;
01752         pool->blocks[i].capacity = 0;
01753     }
01754     pool->release_fn = release_fn;
01755     pool->content_pool = NULL;
01756     pool->content_pool_size = 0;
01757     return pool;
01758 }
01759 
01760 void jsonlite_token_pool_copy_tokens(jsonlite_token_pool pool) {
01761     jsonlite_token_bucket *bucket;
01762     size_t size = 0;
01763     int i;
01764     for (i = 0; i < JSONLITE_TOKEN_POOL_FRONT; i++) {
01765         bucket = pool->blocks[i].buckets;
01766         while (bucket->start != NULL) {
01767             size += bucket->end - bucket->start;
01768             bucket++;
01769         }
01770     }
01771 
01772     if (size == pool->content_pool_size) {
01773         return;
01774     }
01775 
01776     uint8_t *buffer = (uint8_t *)malloc(size);
01777     uint8_t *p = buffer;
01778     for (i = 0; i < JSONLITE_TOKEN_POOL_FRONT; i++) {
01779         bucket = pool->blocks[i].buckets;
01780         while (bucket->start != NULL) {
01781             size_t length = bucket->end - bucket->start;
01782             memcpy(p, bucket->start, length); // LCOV_EXCL_LINE
01783             bucket->start = p;
01784             bucket->end = p + length;
01785             p += length;
01786             bucket++;
01787         }
01788     }
01789 
01790     free(pool->content_pool);
01791     pool->content_pool = buffer;
01792     pool->content_pool_size = size;
01793 }
01794 
01795 void jsonlite_token_pool_release(jsonlite_token_pool pool) {
01796     int i;
01797     for (i = 0; i < JSONLITE_TOKEN_POOL_FRONT; i++) {
01798         jsonlite_token_bucket *bucket = pool->blocks[i].buckets;
01799         if (bucket->start == NULL) {
01800             continue;
01801         }
01802 
01803         if (pool->release_fn != NULL) {
01804             for (; bucket->start != NULL; bucket++) {
01805                 pool->release_fn((void *)bucket->value);
01806             }
01807         }
01808 
01809         free(pool->blocks[i].buckets);
01810     }
01811 
01812     free(pool->content_pool);
01813     free(pool);
01814 }
01815 
01816 jsonlite_token_bucket* jsonlite_token_pool_get_bucket(jsonlite_token_pool pool, jsonlite_token *token) {
01817     ptrdiff_t length = token->end - token->start;
01818     ptrdiff_t hash = jsonlite_hash(token->start, (size_t)length);
01819     ptrdiff_t index = hash & JSONLITE_TOKEN_POOL_FRONT_MASK;
01820     size_t count = 0;
01821     jsonlite_token_bucket *bucket = pool->blocks[index].buckets;
01822     for (; bucket->start != NULL; count++, bucket++) {
01823         if (bucket->hash != hash) {
01824             continue;
01825         }
01826 
01827         if (length != bucket->end - bucket->start) {
01828             continue;
01829         }
01830 
01831         if (memcmp(token->start, bucket->start, (size_t)length) == 0) {
01832             return bucket;
01833         }
01834     }
01835 
01836     size_t capacity = pool->blocks[index].capacity;
01837     if (count + 1 >= capacity) {
01838         jsonlite_extend_capacity(pool, index);
01839     }
01840 
01841     bucket = pool->blocks[index].buckets + count;
01842     bucket->hash = hash;
01843     bucket->start = token->start;
01844     bucket->end = token->end;
01845     bucket->value = NULL;
01846     bucket[1].start = NULL;
01847     return bucket;
01848 }
01849 
01850 static void jsonlite_extend_capacity(jsonlite_token_pool pool, ptrdiff_t index) {
01851     size_t capacity = pool->blocks[index].capacity;
01852     if (capacity == 0) {
01853         capacity = 0x10;
01854     }
01855 
01856     size_t size = capacity * sizeof(jsonlite_token_bucket);
01857     jsonlite_token_bucket *buckets = pool->blocks[index].buckets;
01858     jsonlite_token_bucket *extended = (jsonlite_token_bucket *)malloc(2 * size);
01859 
01860     if (buckets->start != NULL) {
01861         memcpy(extended, buckets, size); // LCOV_EXCL_LINE
01862         free(buckets);
01863     }
01864 
01865     pool->blocks[index].buckets = extended;
01866     pool->blocks[index].capacity = 2 * capacity;
01867 }
01868 
01869 // Used MurmurHash2 function by Austin Appleby
01870 // http://code.google.com/p/smhasher/ revision 147
01871 
01872 //-----------------------------------------------------------------------------
01873 // MurmurHash2 was written by Austin Appleby, and is placed in the public
01874 // domain. The author hereby disclaims copyright to this source code.
01875 
01876 // Note - This code makes a few assumptions about how your machine behaves -
01877 
01878 // 1. We can read a 4-byte value from any address without crashing
01879 // 2. sizeof(int) == 4
01880 
01881 // And it has a few limitations -
01882 
01883 // 1. It will not work incrementally.
01884 // 2. It will not produce the same results on little-endian and big-endian
01885 //    machines.
01886 
01887 static uint32_t MurmurHash2(const void * key, size_t len)
01888 {
01889     // 'm' and 'r' are mixing constants generated offline.
01890     // They're not really 'magic', they just happen to work well.
01891 
01892     const uint32_t m = 0x5bd1e995;
01893     const int r = 24;
01894 
01895     // Initialize the hash to a 'random' value
01896 
01897     uint32_t h = (uint32_t)len;
01898 
01899     // Mix 4 bytes at a time into the hash
01900 
01901     const unsigned char * data = (const unsigned char *)key;
01902 
01903     while(len >= 4)
01904     {
01905         uint32_t k = *(uint32_t*)data;
01906 
01907         k *= m;
01908         k ^= k >> r;
01909         k *= m;
01910 
01911         h *= m;
01912         h ^= k;
01913 
01914         data += 4;
01915         len -= 4;
01916     }
01917 
01918     // Handle the last few bytes of the input array
01919 
01920     switch(len)
01921     {
01922         case 3: h ^= data[2] << 16;
01923         case 2: h ^= data[1] << 8;
01924         case 1: h ^= data[0];
01925             h *= m;
01926     };
01927 
01928     // Do a few final mixes of the hash to ensure the last few
01929     // bytes are well-incorporated.
01930 
01931     h ^= h >> 13;
01932     h *= m;
01933     h ^= h >> 15;
01934 
01935     return h;
01936 }
01937 
01938 //-----------------------------------------------------------------------------
01939 
01940 static uint32_t jsonlite_hash(const uint8_t *data, size_t len) {
01941     return MurmurHash2(data, len);
01942 }