1 /**
2  * The MIT License (MIT)
3  *
4  * Copyright (c) 2015-2018 Nicholas Fraser
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  */
25 
26 /*
27  * This is the MPack 1.0 amalgamation package.
28  *
29  * http://github.com/ludocode/mpack
30  */
31 
32 #define MPACK_INTERNAL 1
33 #define MPACK_EMIT_INLINE_DEFS 1
34 
35 #include "mpack.h"
36 
37 
38 /* mpack/mpack-platform.c.c */
39 
40 
41 // We define MPACK_EMIT_INLINE_DEFS and include mpack.h to emit
42 // standalone definitions of all (non-static) inline functions in MPack.
43 
44 #define MPACK_INTERNAL 1
45 #define MPACK_EMIT_INLINE_DEFS 1
46 
47 /* #include "mpack-platform.h" */
48 /* #include "mpack.h" */
49 
50 
51 #if MPACK_DEBUG && MPACK_STDIO
52 #include <stdarg.h>
53 #endif
54 
55 
56 
57 #if MPACK_DEBUG
58 
59 #if MPACK_STDIO
mpack_assert_fail_format(const char * format,...)60 void mpack_assert_fail_format(const char* format, ...) {
61     char buffer[512];
62     va_list args;
63     va_start(args, format);
64     vsnprintf(buffer, sizeof(buffer), format, args);
65     va_end(args);
66     buffer[sizeof(buffer) - 1] = 0;
67     mpack_assert_fail_wrapper(buffer);
68 }
69 
mpack_break_hit_format(const char * format,...)70 void mpack_break_hit_format(const char* format, ...) {
71     char buffer[512];
72     va_list args;
73     va_start(args, format);
74     vsnprintf(buffer, sizeof(buffer), format, args);
75     va_end(args);
76     buffer[sizeof(buffer) - 1] = 0;
77     mpack_break_hit(buffer);
78 }
79 #endif
80 
81 #if !MPACK_CUSTOM_ASSERT
mpack_assert_fail(const char * message)82 void mpack_assert_fail(const char* message) {
83     MPACK_UNUSED(message);
84 
85     #if MPACK_STDIO
86     fprintf(stderr, "%s\n", message);
87     #endif
88 }
89 #endif
90 
91 // We split the assert failure from the wrapper so that a
92 // custom assert function can return.
mpack_assert_fail_wrapper(const char * message)93 void mpack_assert_fail_wrapper(const char* message) {
94 
95     #ifdef MPACK_GCOV
96     // gcov marks even __builtin_unreachable() as an uncovered line. this
97     // silences it.
98     (mpack_assert_fail(message), __builtin_unreachable());
99 
100     #else
101     mpack_assert_fail(message);
102 
103     // mpack_assert_fail() is not supposed to return. in case it does, we
104     // abort.
105 
106     #if !MPACK_NO_BUILTINS
107     #if defined(__GNUC__) || defined(__clang__)
108     __builtin_trap();
109     #elif defined(WIN32)
110     __debugbreak();
111     #endif
112     #endif
113 
114     #if (defined(__GNUC__) || defined(__clang__)) && !MPACK_NO_BUILTINS
115     __builtin_abort();
116     #elif MPACK_STDLIB
117     abort();
118     #endif
119 
120     MPACK_UNREACHABLE;
121     #endif
122 }
123 
124 #if !MPACK_CUSTOM_BREAK
125 
126 // If we have a custom assert handler, break wraps it by default.
127 // This allows users of MPack to only implement mpack_assert_fail() without
128 // having to worry about the difference between assert and break.
129 //
130 // MPACK_CUSTOM_BREAK is available to define a separate break handler
131 // (which is needed by the unit test suite), but this is not offered in
132 // mpack-config.h for simplicity.
133 
134 #if MPACK_CUSTOM_ASSERT
mpack_break_hit(const char * message)135 void mpack_break_hit(const char* message) {
136     mpack_assert_fail_wrapper(message);
137 }
138 #else
mpack_break_hit(const char * message)139 void mpack_break_hit(const char* message) {
140     MPACK_UNUSED(message);
141 
142     #if MPACK_STDIO
143     fprintf(stderr, "%s\n", message);
144     #endif
145 
146     #if defined(__GNUC__) || defined(__clang__) && !MPACK_NO_BUILTINS
147     __builtin_trap();
148     #elif defined(WIN32) && !MPACK_NO_BUILTINS
149     __debugbreak();
150     #elif MPACK_STDLIB
151     abort();
152     #endif
153 }
154 #endif
155 
156 #endif
157 
158 #endif
159 
160 
161 
162 // The below are adapted from the C wikibook:
163 //     https://en.wikibooks.org/wiki/C_Programming/Strings
164 
165 #ifndef mpack_memcmp
mpack_memcmp(const void * s1,const void * s2,size_t n)166 int mpack_memcmp(const void* s1, const void* s2, size_t n) {
167      const unsigned char *us1 = (const unsigned char *) s1;
168      const unsigned char *us2 = (const unsigned char *) s2;
169      while (n-- != 0) {
170          if (*us1 != *us2)
171              return (*us1 < *us2) ? -1 : +1;
172          us1++;
173          us2++;
174      }
175      return 0;
176 }
177 #endif
178 
179 #ifndef mpack_memcpy
mpack_memcpy(void * MPACK_RESTRICT s1,const void * MPACK_RESTRICT s2,size_t n)180 void* mpack_memcpy(void* MPACK_RESTRICT s1, const void* MPACK_RESTRICT s2, size_t n) {
181     char* MPACK_RESTRICT dst = (char *)s1;
182     const char* MPACK_RESTRICT src = (const char *)s2;
183     while (n-- != 0)
184         *dst++ = *src++;
185     return s1;
186 }
187 #endif
188 
189 #ifndef mpack_memmove
mpack_memmove(void * s1,const void * s2,size_t n)190 void* mpack_memmove(void* s1, const void* s2, size_t n) {
191     char *p1 = (char *)s1;
192     const char *p2 = (const char *)s2;
193     if (p2 < p1 && p1 < p2 + n) {
194         p2 += n;
195         p1 += n;
196         while (n-- != 0)
197             *--p1 = *--p2;
198     } else
199         while (n-- != 0)
200             *p1++ = *p2++;
201     return s1;
202 }
203 #endif
204 
205 #ifndef mpack_memset
mpack_memset(void * s,int c,size_t n)206 void* mpack_memset(void* s, int c, size_t n) {
207     unsigned char *us = (unsigned char *)s;
208     unsigned char uc = (unsigned char)c;
209     while (n-- != 0)
210         *us++ = uc;
211     return s;
212 }
213 #endif
214 
215 #ifndef mpack_strlen
mpack_strlen(const char * s)216 size_t mpack_strlen(const char* s) {
217     const char* p = s;
218     while (*p != '\0')
219         p++;
220     return (size_t)(p - s);
221 }
222 #endif
223 
224 
225 
226 #if defined(MPACK_MALLOC) && !defined(MPACK_REALLOC)
mpack_realloc(void * old_ptr,size_t used_size,size_t new_size)227 void* mpack_realloc(void* old_ptr, size_t used_size, size_t new_size) {
228     if (new_size == 0) {
229         if (old_ptr)
230             MPACK_FREE(old_ptr);
231         return NULL;
232     }
233 
234     void* new_ptr = MPACK_MALLOC(new_size);
235     if (new_ptr == NULL)
236         return NULL;
237 
238     mpack_memcpy(new_ptr, old_ptr, used_size);
239     MPACK_FREE(old_ptr);
240     return new_ptr;
241 }
242 #endif
243 
244 /* mpack/mpack-common.c.c */
245 
246 #define MPACK_INTERNAL 1
247 
248 /* #include "mpack-common.h" */
249 
250 #if MPACK_DEBUG && MPACK_STDIO
251 #include <stdarg.h>
252 #endif
253 
mpack_error_to_string(mpack_error_t error)254 const char* mpack_error_to_string(mpack_error_t error) {
255     #if MPACK_STRINGS
256     switch (error) {
257         #define MPACK_ERROR_STRING_CASE(e) case e: return #e
258         MPACK_ERROR_STRING_CASE(mpack_ok);
259         MPACK_ERROR_STRING_CASE(mpack_error_io);
260         MPACK_ERROR_STRING_CASE(mpack_error_invalid);
261         MPACK_ERROR_STRING_CASE(mpack_error_unsupported);
262         MPACK_ERROR_STRING_CASE(mpack_error_type);
263         MPACK_ERROR_STRING_CASE(mpack_error_too_big);
264         MPACK_ERROR_STRING_CASE(mpack_error_memory);
265         MPACK_ERROR_STRING_CASE(mpack_error_bug);
266         MPACK_ERROR_STRING_CASE(mpack_error_data);
267         MPACK_ERROR_STRING_CASE(mpack_error_eof);
268         #undef MPACK_ERROR_STRING_CASE
269     }
270     mpack_assert(0, "unrecognized error %i", (int)error);
271     return "(unknown mpack_error_t)";
272     #else
273     MPACK_UNUSED(error);
274     return "";
275     #endif
276 }
277 
mpack_type_to_string(mpack_type_t type)278 const char* mpack_type_to_string(mpack_type_t type) {
279     #if MPACK_STRINGS
280     switch (type) {
281         #define MPACK_TYPE_STRING_CASE(e) case e: return #e
282         MPACK_TYPE_STRING_CASE(mpack_type_missing);
283         MPACK_TYPE_STRING_CASE(mpack_type_nil);
284         MPACK_TYPE_STRING_CASE(mpack_type_bool);
285         MPACK_TYPE_STRING_CASE(mpack_type_float);
286         MPACK_TYPE_STRING_CASE(mpack_type_double);
287         MPACK_TYPE_STRING_CASE(mpack_type_int);
288         MPACK_TYPE_STRING_CASE(mpack_type_uint);
289         MPACK_TYPE_STRING_CASE(mpack_type_str);
290         MPACK_TYPE_STRING_CASE(mpack_type_bin);
291         MPACK_TYPE_STRING_CASE(mpack_type_array);
292         MPACK_TYPE_STRING_CASE(mpack_type_map);
293         #if MPACK_EXTENSIONS
294         MPACK_TYPE_STRING_CASE(mpack_type_ext);
295         #endif
296         #undef MPACK_TYPE_STRING_CASE
297     }
298     mpack_assert(0, "unrecognized type %i", (int)type);
299     return "(unknown mpack_type_t)";
300     #else
301     MPACK_UNUSED(type);
302     return "";
303     #endif
304 }
305 
mpack_tag_cmp(mpack_tag_t left,mpack_tag_t right)306 int mpack_tag_cmp(mpack_tag_t left, mpack_tag_t right) {
307 
308     // positive numbers may be stored as int; convert to uint
309     if (left.type == mpack_type_int && left.v.i >= 0) {
310         left.type = mpack_type_uint;
311         left.v.u = (uint64_t)left.v.i;
312     }
313     if (right.type == mpack_type_int && right.v.i >= 0) {
314         right.type = mpack_type_uint;
315         right.v.u = (uint64_t)right.v.i;
316     }
317 
318     if (left.type != right.type)
319         return ((int)left.type < (int)right.type) ? -1 : 1;
320 
321     switch (left.type) {
322         case mpack_type_missing: // fallthrough
323         case mpack_type_nil:
324             return 0;
325 
326         case mpack_type_bool:
327             return (int)left.v.b - (int)right.v.b;
328 
329         case mpack_type_int:
330             if (left.v.i == right.v.i)
331                 return 0;
332             return (left.v.i < right.v.i) ? -1 : 1;
333 
334         case mpack_type_uint:
335             if (left.v.u == right.v.u)
336                 return 0;
337             return (left.v.u < right.v.u) ? -1 : 1;
338 
339         case mpack_type_array:
340         case mpack_type_map:
341             if (left.v.n == right.v.n)
342                 return 0;
343             return (left.v.n < right.v.n) ? -1 : 1;
344 
345         case mpack_type_str:
346         case mpack_type_bin:
347             if (left.v.l == right.v.l)
348                 return 0;
349             return (left.v.l < right.v.l) ? -1 : 1;
350 
351         #if MPACK_EXTENSIONS
352         case mpack_type_ext:
353             if (left.exttype == right.exttype) {
354                 if (left.v.l == right.v.l)
355                     return 0;
356                 return (left.v.l < right.v.l) ? -1 : 1;
357             }
358             return (int)left.exttype - (int)right.exttype;
359         #endif
360 
361         // floats should not normally be compared for equality. we compare
362         // with memcmp() to silence compiler warnings, but this will return
363         // equal if both are NaNs with the same representation (though we may
364         // want this, for instance if you are for some bizarre reason using
365         // floats as map keys.) i'm not sure what the right thing to
366         // do is here. check for NaN first? always return false if the type
367         // is float? use operator== and pragmas to silence compiler warning?
368         // please send me your suggestions.
369         // note also that we don't convert floats to doubles, so when this is
370         // used for ordering purposes, all floats are ordered before all
371         // doubles.
372         case mpack_type_float:
373             return mpack_memcmp(&left.v.f, &right.v.f, sizeof(left.v.f));
374         case mpack_type_double:
375             return mpack_memcmp(&left.v.d, &right.v.d, sizeof(left.v.d));
376     }
377 
378     mpack_assert(0, "unrecognized type %i", (int)left.type);
379     return false;
380 }
381 
382 #if MPACK_DEBUG && MPACK_STDIO
mpack_hex_char(uint8_t hex_value)383 static char mpack_hex_char(uint8_t hex_value) {
384     // Older compilers (e.g. GCC 4.4.7) promote the result of this ternary to
385     // int and warn under -Wconversion, so we have to cast it back to char.
386     return (char)((hex_value < 10) ? (char)('0' + hex_value) : (char)('a' + (hex_value - 10)));
387 }
388 
mpack_tag_debug_complete_bin_ext(mpack_tag_t tag,size_t string_length,char * buffer,size_t buffer_size,const char * prefix,size_t prefix_size)389 static void mpack_tag_debug_complete_bin_ext(mpack_tag_t tag, size_t string_length, char* buffer, size_t buffer_size,
390         const char* prefix, size_t prefix_size)
391 {
392     // If at any point in this function we run out of space in the buffer, we
393     // bail out. The outer tag print wrapper will make sure we have a
394     // null-terminator.
395 
396     if (string_length == 0 || string_length >= buffer_size)
397         return;
398     buffer += string_length;
399     buffer_size -= string_length;
400 
401     size_t total = mpack_tag_bytes(&tag);
402     if (total == 0) {
403         strncpy(buffer, ">", buffer_size);
404         return;
405     }
406 
407     strncpy(buffer, ": ", buffer_size);
408     if (buffer_size < 2)
409         return;
410     buffer += 2;
411     buffer_size -= 2;
412 
413     size_t hex_bytes = 0;
414     for (size_t i = 0; i < MPACK_PRINT_BYTE_COUNT && i < prefix_size && buffer_size > 2; ++i) {
415         uint8_t byte = (uint8_t)prefix[i];
416         buffer[0] = mpack_hex_char((uint8_t)(byte >> 4));
417         buffer[1] = mpack_hex_char((uint8_t)(byte & 0xfu));
418         buffer += 2;
419         buffer_size -= 2;
420         ++hex_bytes;
421     }
422 
423     if (buffer_size != 0)
424         mpack_snprintf(buffer, buffer_size, "%s>", (total > hex_bytes) ? "..." : "");
425 }
426 
mpack_tag_debug_pseudo_json_bin(mpack_tag_t tag,char * buffer,size_t buffer_size,const char * prefix,size_t prefix_size)427 static void mpack_tag_debug_pseudo_json_bin(mpack_tag_t tag, char* buffer, size_t buffer_size,
428         const char* prefix, size_t prefix_size)
429 {
430     mpack_assert(mpack_tag_type(&tag) == mpack_type_bin);
431     size_t length = (size_t)mpack_snprintf(buffer, buffer_size, "<binary data of length %u", tag.v.l);
432     mpack_tag_debug_complete_bin_ext(tag, length, buffer, buffer_size, prefix, prefix_size);
433 }
434 
435 #if MPACK_EXTENSIONS
mpack_tag_debug_pseudo_json_ext(mpack_tag_t tag,char * buffer,size_t buffer_size,const char * prefix,size_t prefix_size)436 static void mpack_tag_debug_pseudo_json_ext(mpack_tag_t tag, char* buffer, size_t buffer_size,
437         const char* prefix, size_t prefix_size)
438 {
439     mpack_assert(mpack_tag_type(&tag) == mpack_type_ext);
440     size_t length = (size_t)mpack_snprintf(buffer, buffer_size, "<ext data of type %i and length %u",
441             mpack_tag_ext_exttype(&tag), mpack_tag_ext_length(&tag));
442     mpack_tag_debug_complete_bin_ext(tag, length, buffer, buffer_size, prefix, prefix_size);
443 }
444 #endif
445 
mpack_tag_debug_pseudo_json_impl(mpack_tag_t tag,char * buffer,size_t buffer_size,const char * prefix,size_t prefix_size)446 static void mpack_tag_debug_pseudo_json_impl(mpack_tag_t tag, char* buffer, size_t buffer_size,
447         const char* prefix, size_t prefix_size)
448 {
449     switch (tag.type) {
450         case mpack_type_missing:
451             mpack_snprintf(buffer, buffer_size, "<missing!>");
452             return;
453         case mpack_type_nil:
454             mpack_snprintf(buffer, buffer_size, "null");
455             return;
456         case mpack_type_bool:
457             mpack_snprintf(buffer, buffer_size, tag.v.b ? "true" : "false");
458             return;
459         case mpack_type_int:
460             mpack_snprintf(buffer, buffer_size, "%" PRIi64, tag.v.i);
461             return;
462         case mpack_type_uint:
463             mpack_snprintf(buffer, buffer_size, "%" PRIu64, tag.v.u);
464             return;
465         case mpack_type_float:
466             mpack_snprintf(buffer, buffer_size, "%f", tag.v.f);
467             return;
468         case mpack_type_double:
469             mpack_snprintf(buffer, buffer_size, "%f", tag.v.d);
470             return;
471 
472         case mpack_type_str:
473             mpack_snprintf(buffer, buffer_size, "<string of %u bytes>", tag.v.l);
474             return;
475         case mpack_type_bin:
476             mpack_tag_debug_pseudo_json_bin(tag, buffer, buffer_size, prefix, prefix_size);
477             return;
478         #if MPACK_EXTENSIONS
479         case mpack_type_ext:
480             mpack_tag_debug_pseudo_json_ext(tag, buffer, buffer_size, prefix, prefix_size);
481             return;
482         #endif
483 
484         case mpack_type_array:
485             mpack_snprintf(buffer, buffer_size, "<array of %u elements>", tag.v.n);
486             return;
487         case mpack_type_map:
488             mpack_snprintf(buffer, buffer_size, "<map of %u key-value pairs>", tag.v.n);
489             return;
490     }
491 
492     mpack_snprintf(buffer, buffer_size, "<unknown!>");
493 }
494 
mpack_tag_debug_pseudo_json(mpack_tag_t tag,char * buffer,size_t buffer_size,const char * prefix,size_t prefix_size)495 void mpack_tag_debug_pseudo_json(mpack_tag_t tag, char* buffer, size_t buffer_size,
496         const char* prefix, size_t prefix_size)
497 {
498     mpack_assert(buffer_size > 0, "buffer size cannot be zero!");
499     buffer[0] = 0;
500 
501     mpack_tag_debug_pseudo_json_impl(tag, buffer, buffer_size, prefix, prefix_size);
502 
503     // We always null-terminate the buffer manually just in case the snprintf()
504     // function doesn't null-terminate when the string doesn't fit.
505     buffer[buffer_size - 1] = 0;
506 }
507 
mpack_tag_debug_describe_impl(mpack_tag_t tag,char * buffer,size_t buffer_size)508 static void mpack_tag_debug_describe_impl(mpack_tag_t tag, char* buffer, size_t buffer_size) {
509     switch (tag.type) {
510         case mpack_type_missing:
511             mpack_snprintf(buffer, buffer_size, "missing");
512             return;
513         case mpack_type_nil:
514             mpack_snprintf(buffer, buffer_size, "nil");
515             return;
516         case mpack_type_bool:
517             mpack_snprintf(buffer, buffer_size, tag.v.b ? "true" : "false");
518             return;
519         case mpack_type_int:
520             mpack_snprintf(buffer, buffer_size, "int %" PRIi64, tag.v.i);
521             return;
522         case mpack_type_uint:
523             mpack_snprintf(buffer, buffer_size, "uint %" PRIu64, tag.v.u);
524             return;
525         case mpack_type_float:
526             mpack_snprintf(buffer, buffer_size, "float %f", tag.v.f);
527             return;
528         case mpack_type_double:
529             mpack_snprintf(buffer, buffer_size, "double %f", tag.v.d);
530             return;
531         case mpack_type_str:
532             mpack_snprintf(buffer, buffer_size, "str of %u bytes", tag.v.l);
533             return;
534         case mpack_type_bin:
535             mpack_snprintf(buffer, buffer_size, "bin of %u bytes", tag.v.l);
536             return;
537         #if MPACK_EXTENSIONS
538         case mpack_type_ext:
539             mpack_snprintf(buffer, buffer_size, "ext of type %i, %u bytes",
540                     mpack_tag_ext_exttype(&tag), mpack_tag_ext_length(&tag));
541             return;
542         #endif
543         case mpack_type_array:
544             mpack_snprintf(buffer, buffer_size, "array of %u elements", tag.v.n);
545             return;
546         case mpack_type_map:
547             mpack_snprintf(buffer, buffer_size, "map of %u key-value pairs", tag.v.n);
548             return;
549     }
550 
551     mpack_snprintf(buffer, buffer_size, "unknown!");
552 }
553 
mpack_tag_debug_describe(mpack_tag_t tag,char * buffer,size_t buffer_size)554 void mpack_tag_debug_describe(mpack_tag_t tag, char* buffer, size_t buffer_size) {
555     mpack_assert(buffer_size > 0, "buffer size cannot be zero!");
556     buffer[0] = 0;
557 
558     mpack_tag_debug_describe_impl(tag, buffer, buffer_size);
559 
560     // We always null-terminate the buffer manually just in case the snprintf()
561     // function doesn't null-terminate when the string doesn't fit.
562     buffer[buffer_size - 1] = 0;
563 }
564 #endif
565 
566 
567 
568 #if MPACK_READ_TRACKING || MPACK_WRITE_TRACKING
569 
570 #ifndef MPACK_TRACKING_INITIAL_CAPACITY
571 // seems like a reasonable number. we grow by doubling, and it only
572 // needs to be as long as the maximum depth of the message.
573 #define MPACK_TRACKING_INITIAL_CAPACITY 8
574 #endif
575 
mpack_track_init(mpack_track_t * track)576 mpack_error_t mpack_track_init(mpack_track_t* track) {
577     track->count = 0;
578     track->capacity = MPACK_TRACKING_INITIAL_CAPACITY;
579     track->elements = (mpack_track_element_t*)MPACK_MALLOC(sizeof(mpack_track_element_t) * track->capacity);
580     if (track->elements == NULL)
581         return mpack_error_memory;
582     return mpack_ok;
583 }
584 
mpack_track_grow(mpack_track_t * track)585 mpack_error_t mpack_track_grow(mpack_track_t* track) {
586     mpack_assert(track->elements, "null track elements!");
587     mpack_assert(track->count == track->capacity, "incorrect growing?");
588 
589     size_t new_capacity = track->capacity * 2;
590 
591     mpack_track_element_t* new_elements = (mpack_track_element_t*)mpack_realloc(track->elements,
592             sizeof(mpack_track_element_t) * track->count, sizeof(mpack_track_element_t) * new_capacity);
593     if (new_elements == NULL)
594         return mpack_error_memory;
595 
596     track->elements = new_elements;
597     track->capacity = new_capacity;
598     return mpack_ok;
599 }
600 
mpack_track_push(mpack_track_t * track,mpack_type_t type,uint64_t count)601 mpack_error_t mpack_track_push(mpack_track_t* track, mpack_type_t type, uint64_t count) {
602     mpack_assert(track->elements, "null track elements!");
603     mpack_log("track pushing %s count %i\n", mpack_type_to_string(type), (int)count);
604 
605     // maps have twice the number of elements (key/value pairs)
606     if (type == mpack_type_map)
607         count *= 2;
608 
609     // grow if needed
610     if (track->count == track->capacity) {
611         mpack_error_t error = mpack_track_grow(track);
612         if (error != mpack_ok)
613             return error;
614     }
615 
616     // insert new track
617     track->elements[track->count].type = type;
618     track->elements[track->count].left = count;
619     ++track->count;
620     return mpack_ok;
621 }
622 
mpack_track_pop(mpack_track_t * track,mpack_type_t type)623 mpack_error_t mpack_track_pop(mpack_track_t* track, mpack_type_t type) {
624     mpack_assert(track->elements, "null track elements!");
625     mpack_log("track popping %s\n", mpack_type_to_string(type));
626 
627     if (track->count == 0) {
628         mpack_break("attempting to close a %s but nothing was opened!", mpack_type_to_string(type));
629         return mpack_error_bug;
630     }
631 
632     mpack_track_element_t* element = &track->elements[track->count - 1];
633 
634     if (element->type != type) {
635         mpack_break("attempting to close a %s but the open element is a %s!",
636                 mpack_type_to_string(type), mpack_type_to_string(element->type));
637         return mpack_error_bug;
638     }
639 
640     if (element->left != 0) {
641         mpack_break("attempting to close a %s but there are %" PRIu64 " %s left",
642                 mpack_type_to_string(type), element->left,
643                 (type == mpack_type_map || type == mpack_type_array) ? "elements" : "bytes");
644         return mpack_error_bug;
645     }
646 
647     --track->count;
648     return mpack_ok;
649 }
650 
mpack_track_peek_element(mpack_track_t * track,bool read)651 mpack_error_t mpack_track_peek_element(mpack_track_t* track, bool read) {
652     MPACK_UNUSED(read);
653     mpack_assert(track->elements, "null track elements!");
654 
655     // if there are no open elements, that's fine, we can read/write elements at will
656     if (track->count == 0)
657         return mpack_ok;
658 
659     mpack_track_element_t* element = &track->elements[track->count - 1];
660 
661     if (element->type != mpack_type_map && element->type != mpack_type_array) {
662         mpack_break("elements cannot be %s within an %s", read ? "read" : "written",
663                 mpack_type_to_string(element->type));
664         return mpack_error_bug;
665     }
666 
667     if (element->left == 0) {
668         mpack_break("too many elements %s for %s", read ? "read" : "written",
669                 mpack_type_to_string(element->type));
670         return mpack_error_bug;
671     }
672 
673     return mpack_ok;
674 }
675 
mpack_track_element(mpack_track_t * track,bool read)676 mpack_error_t mpack_track_element(mpack_track_t* track, bool read) {
677     mpack_error_t error = mpack_track_peek_element(track, read);
678     if (track->count > 0 && error == mpack_ok)
679         --track->elements[track->count - 1].left;
680     return error;
681 }
682 
mpack_track_bytes(mpack_track_t * track,bool read,uint64_t count)683 mpack_error_t mpack_track_bytes(mpack_track_t* track, bool read, uint64_t count) {
684     MPACK_UNUSED(read);
685     mpack_assert(track->elements, "null track elements!");
686 
687     if (track->count == 0) {
688         mpack_break("bytes cannot be %s with no open bin, str or ext", read ? "read" : "written");
689         return mpack_error_bug;
690     }
691 
692     mpack_track_element_t* element = &track->elements[track->count - 1];
693 
694     if (element->type == mpack_type_map || element->type == mpack_type_array) {
695         mpack_break("bytes cannot be %s within an %s", read ? "read" : "written",
696                 mpack_type_to_string(element->type));
697         return mpack_error_bug;
698     }
699 
700     if (element->left < count) {
701         mpack_break("too many bytes %s for %s", read ? "read" : "written",
702                 mpack_type_to_string(element->type));
703         return mpack_error_bug;
704     }
705 
706     element->left -= count;
707     return mpack_ok;
708 }
709 
mpack_track_str_bytes_all(mpack_track_t * track,bool read,uint64_t count)710 mpack_error_t mpack_track_str_bytes_all(mpack_track_t* track, bool read, uint64_t count) {
711     mpack_error_t error = mpack_track_bytes(track, read, count);
712     if (error != mpack_ok)
713         return error;
714 
715     mpack_track_element_t* element = &track->elements[track->count - 1];
716 
717     if (element->type != mpack_type_str) {
718         mpack_break("the open type must be a string, not a %s", mpack_type_to_string(element->type));
719         return mpack_error_bug;
720     }
721 
722     if (element->left != 0) {
723         mpack_break("not all bytes were read; the wrong byte count was requested for a string read.");
724         return mpack_error_bug;
725     }
726 
727     return mpack_ok;
728 }
729 
mpack_track_check_empty(mpack_track_t * track)730 mpack_error_t mpack_track_check_empty(mpack_track_t* track) {
731     if (track->count != 0) {
732         mpack_break("unclosed %s", mpack_type_to_string(track->elements[0].type));
733         return mpack_error_bug;
734     }
735     return mpack_ok;
736 }
737 
mpack_track_destroy(mpack_track_t * track,bool cancel)738 mpack_error_t mpack_track_destroy(mpack_track_t* track, bool cancel) {
739     mpack_error_t error = cancel ? mpack_ok : mpack_track_check_empty(track);
740     if (track->elements) {
741         MPACK_FREE(track->elements);
742         track->elements = NULL;
743     }
744     return error;
745 }
746 #endif
747 
748 
749 
mpack_utf8_check_impl(const uint8_t * str,size_t count,bool allow_null)750 static bool mpack_utf8_check_impl(const uint8_t* str, size_t count, bool allow_null) {
751     while (count > 0) {
752         uint8_t lead = str[0];
753 
754         // NUL
755         if (!allow_null && lead == '\0') // we don't allow NUL bytes in MPack C-strings
756             return false;
757 
758         // ASCII
759         if (lead <= 0x7F) {
760             ++str;
761             --count;
762 
763         // 2-byte sequence
764         } else if ((lead & 0xE0) == 0xC0) {
765             if (count < 2) // truncated sequence
766                 return false;
767 
768             uint8_t cont = str[1];
769             if ((cont & 0xC0) != 0x80) // not a continuation byte
770                 return false;
771 
772             str += 2;
773             count -= 2;
774 
775             uint32_t z = ((uint32_t)(lead & ~0xE0) << 6) |
776                           (uint32_t)(cont & ~0xC0);
777 
778             if (z < 0x80) // overlong sequence
779                 return false;
780 
781         // 3-byte sequence
782         } else if ((lead & 0xF0) == 0xE0) {
783             if (count < 3) // truncated sequence
784                 return false;
785 
786             uint8_t cont1 = str[1];
787             if ((cont1 & 0xC0) != 0x80) // not a continuation byte
788                 return false;
789             uint8_t cont2 = str[2];
790             if ((cont2 & 0xC0) != 0x80) // not a continuation byte
791                 return false;
792 
793             str += 3;
794             count -= 3;
795 
796             uint32_t z = ((uint32_t)(lead  & ~0xF0) << 12) |
797                          ((uint32_t)(cont1 & ~0xC0) <<  6) |
798                           (uint32_t)(cont2 & ~0xC0);
799 
800             if (z < 0x800) // overlong sequence
801                 return false;
802             if (z >= 0xD800 && z <= 0xDFFF) // surrogate
803                 return false;
804 
805         // 4-byte sequence
806         } else if ((lead & 0xF8) == 0xF0) {
807             if (count < 4) // truncated sequence
808                 return false;
809 
810             uint8_t cont1 = str[1];
811             if ((cont1 & 0xC0) != 0x80) // not a continuation byte
812                 return false;
813             uint8_t cont2 = str[2];
814             if ((cont2 & 0xC0) != 0x80) // not a continuation byte
815                 return false;
816             uint8_t cont3 = str[3];
817             if ((cont3 & 0xC0) != 0x80) // not a continuation byte
818                 return false;
819 
820             str += 4;
821             count -= 4;
822 
823             uint32_t z = ((uint32_t)(lead  & ~0xF8) << 18) |
824                          ((uint32_t)(cont1 & ~0xC0) << 12) |
825                          ((uint32_t)(cont2 & ~0xC0) <<  6) |
826                           (uint32_t)(cont3 & ~0xC0);
827 
828             if (z < 0x10000) // overlong sequence
829                 return false;
830             if (z > 0x10FFFF) // codepoint limit
831                 return false;
832 
833         } else {
834             return false; // continuation byte without a lead, or lead for a 5-byte sequence or longer
835         }
836     }
837     return true;
838 }
839 
mpack_utf8_check(const char * str,size_t bytes)840 bool mpack_utf8_check(const char* str, size_t bytes) {
841     return mpack_utf8_check_impl((const uint8_t*)str, bytes, true);
842 }
843 
mpack_utf8_check_no_null(const char * str,size_t bytes)844 bool mpack_utf8_check_no_null(const char* str, size_t bytes) {
845     return mpack_utf8_check_impl((const uint8_t*)str, bytes, false);
846 }
847 
mpack_str_check_no_null(const char * str,size_t bytes)848 bool mpack_str_check_no_null(const char* str, size_t bytes) {
849     for (size_t i = 0; i < bytes; ++i)
850         if (str[i] == '\0')
851             return false;
852     return true;
853 }
854 
855 #if MPACK_DEBUG && MPACK_STDIO
mpack_print_append(mpack_print_t * print,const char * data,size_t count)856 void mpack_print_append(mpack_print_t* print, const char* data, size_t count) {
857 
858     // copy whatever fits into the buffer
859     size_t copy = print->size - print->count;
860     if (copy > count)
861         copy = count;
862     mpack_memcpy(print->buffer + print->count, data, copy);
863     print->count += copy;
864     data += copy;
865     count -= copy;
866 
867     // if we don't need to flush or can't flush there's nothing else to do
868     if (count == 0 || print->callback == NULL)
869         return;
870 
871     // flush the buffer
872     print->callback(print->context, print->buffer, print->count);
873 
874     if (count > print->size / 2) {
875         // flush the rest of the data
876         print->count = 0;
877         print->callback(print->context, data, count);
878     } else {
879         // copy the rest of the data into the buffer
880         mpack_memcpy(print->buffer, data, count);
881         print->count = count;
882     }
883 
884 }
885 
mpack_print_flush(mpack_print_t * print)886 void mpack_print_flush(mpack_print_t* print) {
887     if (print->count > 0 && print->callback != NULL) {
888         print->callback(print->context, print->buffer, print->count);
889         print->count = 0;
890     }
891 }
892 
mpack_print_file_callback(void * context,const char * data,size_t count)893 void mpack_print_file_callback(void* context, const char* data, size_t count) {
894     FILE* file = (FILE*)context;
895     fwrite(data, 1, count, file);
896 }
897 #endif
898 
899 /* mpack/mpack-writer.c.c */
900 
901 #define MPACK_INTERNAL 1
902 
903 /* #include "mpack-writer.h" */
904 
905 #if MPACK_WRITER
906 
907 #if MPACK_WRITE_TRACKING
mpack_writer_flag_if_error(mpack_writer_t * writer,mpack_error_t error)908 static void mpack_writer_flag_if_error(mpack_writer_t* writer, mpack_error_t error) {
909     if (error != mpack_ok)
910         mpack_writer_flag_error(writer, error);
911 }
912 
mpack_writer_track_push(mpack_writer_t * writer,mpack_type_t type,uint64_t count)913 void mpack_writer_track_push(mpack_writer_t* writer, mpack_type_t type, uint64_t count) {
914     if (writer->error == mpack_ok)
915         mpack_writer_flag_if_error(writer, mpack_track_push(&writer->track, type, count));
916 }
917 
mpack_writer_track_pop(mpack_writer_t * writer,mpack_type_t type)918 void mpack_writer_track_pop(mpack_writer_t* writer, mpack_type_t type) {
919     if (writer->error == mpack_ok)
920         mpack_writer_flag_if_error(writer, mpack_track_pop(&writer->track, type));
921 }
922 
mpack_writer_track_element(mpack_writer_t * writer)923 void mpack_writer_track_element(mpack_writer_t* writer) {
924     if (writer->error == mpack_ok)
925         mpack_writer_flag_if_error(writer, mpack_track_element(&writer->track, false));
926 }
927 
mpack_writer_track_bytes(mpack_writer_t * writer,size_t count)928 void mpack_writer_track_bytes(mpack_writer_t* writer, size_t count) {
929     if (writer->error == mpack_ok)
930         mpack_writer_flag_if_error(writer, mpack_track_bytes(&writer->track, false, count));
931 }
932 #endif
933 
mpack_writer_clear(mpack_writer_t * writer)934 static void mpack_writer_clear(mpack_writer_t* writer) {
935     #if MPACK_COMPATIBILITY
936     writer->version = mpack_version_current;
937     #endif
938     writer->flush = NULL;
939     writer->error_fn = NULL;
940     writer->teardown = NULL;
941     writer->context = NULL;
942 
943     writer->buffer = NULL;
944     writer->current = NULL;
945     writer->end = NULL;
946     writer->error = mpack_ok;
947 
948     #if MPACK_WRITE_TRACKING
949     mpack_memset(&writer->track, 0, sizeof(writer->track));
950     #endif
951 }
952 
mpack_writer_init(mpack_writer_t * writer,char * buffer,size_t size)953 void mpack_writer_init(mpack_writer_t* writer, char* buffer, size_t size) {
954     mpack_assert(buffer != NULL, "cannot initialize writer with empty buffer");
955     mpack_writer_clear(writer);
956     writer->buffer = buffer;
957     writer->current = buffer;
958     writer->end = writer->buffer + size;
959 
960     #if MPACK_WRITE_TRACKING
961     mpack_writer_flag_if_error(writer, mpack_track_init(&writer->track));
962     #endif
963 
964     mpack_log("===========================\n");
965     mpack_log("initializing writer with buffer size %i\n", (int)size);
966 }
967 
mpack_writer_init_error(mpack_writer_t * writer,mpack_error_t error)968 void mpack_writer_init_error(mpack_writer_t* writer, mpack_error_t error) {
969     mpack_writer_clear(writer);
970     writer->error = error;
971 
972     mpack_log("===========================\n");
973     mpack_log("initializing writer in error state %i\n", (int)error);
974 }
975 
mpack_writer_set_flush(mpack_writer_t * writer,mpack_writer_flush_t flush)976 void mpack_writer_set_flush(mpack_writer_t* writer, mpack_writer_flush_t flush) {
977     MPACK_STATIC_ASSERT(MPACK_WRITER_MINIMUM_BUFFER_SIZE >= MPACK_MAXIMUM_TAG_SIZE,
978             "minimum buffer size must fit any tag!");
979     MPACK_STATIC_ASSERT(31 + MPACK_TAG_SIZE_FIXSTR >= MPACK_WRITER_MINIMUM_BUFFER_SIZE,
980             "minimum buffer size must fit the largest possible fixstr!");
981 
982     if (mpack_writer_buffer_size(writer) < MPACK_WRITER_MINIMUM_BUFFER_SIZE) {
983         mpack_break("buffer size is %i, but minimum buffer size for flush is %i",
984                 (int)mpack_writer_buffer_size(writer), MPACK_WRITER_MINIMUM_BUFFER_SIZE);
985         mpack_writer_flag_error(writer, mpack_error_bug);
986         return;
987     }
988 
989     writer->flush = flush;
990 }
991 
992 #ifdef MPACK_MALLOC
993 typedef struct mpack_growable_writer_t {
994     char** target_data;
995     size_t* target_size;
996 } mpack_growable_writer_t;
997 
mpack_writer_get_reserved(mpack_writer_t * writer)998 static char* mpack_writer_get_reserved(mpack_writer_t* writer) {
999     // This is in a separate function in order to avoid false strict aliasing
1000     // warnings. We aren't actually violating strict aliasing (the reserved
1001     // space is only ever dereferenced as an mpack_growable_writer_t.)
1002     return (char*)writer->reserved;
1003 }
1004 
mpack_growable_writer_flush(mpack_writer_t * writer,const char * data,size_t count)1005 static void mpack_growable_writer_flush(mpack_writer_t* writer, const char* data, size_t count) {
1006 
1007     // This is an intrusive flush function which modifies the writer's buffer
1008     // in response to a flush instead of emptying it in order to add more
1009     // capacity for data. This removes the need to copy data from a fixed buffer
1010     // into a growable one, improving performance.
1011     //
1012     // There are three ways flush can be called:
1013     //   - flushing the buffer during writing (used is zero, count is all data, data is buffer)
1014     //   - flushing extra data during writing (used is all flushed data, count is extra data, data is not buffer)
1015     //   - flushing during teardown (used and count are both all flushed data, data is buffer)
1016     //
1017     // In the first two cases, we grow the buffer by at least double, enough
1018     // to ensure that new data will fit. We ignore the teardown flush.
1019 
1020     if (data == writer->buffer) {
1021 
1022         // teardown, do nothing
1023         if (mpack_writer_buffer_used(writer) == count)
1024             return;
1025 
1026         // otherwise leave the data in the buffer and just grow
1027         writer->current = writer->buffer + count;
1028         count = 0;
1029     }
1030 
1031     size_t used = mpack_writer_buffer_used(writer);
1032     size_t size = mpack_writer_buffer_size(writer);
1033 
1034     mpack_log("flush size %i used %i data %p buffer %p\n",
1035             (int)count, (int)used, data, writer->buffer);
1036 
1037     mpack_assert(data == writer->buffer || used + count > size,
1038             "extra flush for %i but there is %i space left in the buffer! (%i/%i)",
1039             (int)count, (int)mpack_writer_buffer_left(writer), (int)used, (int)size);
1040 
1041     // grow to fit the data
1042     // TODO: this really needs to correctly test for overflow
1043     size_t new_size = size * 2;
1044     while (new_size < used + count)
1045         new_size *= 2;
1046 
1047     mpack_log("flush growing buffer size from %i to %i\n", (int)size, (int)new_size);
1048 
1049     // grow the buffer
1050     char* new_buffer = (char*)mpack_realloc(writer->buffer, used, new_size);
1051     if (new_buffer == NULL) {
1052         mpack_writer_flag_error(writer, mpack_error_memory);
1053         return;
1054     }
1055     writer->current = new_buffer + used;
1056     writer->buffer = new_buffer;
1057     writer->end = writer->buffer + new_size;
1058 
1059     // append the extra data
1060     if (count > 0) {
1061         mpack_memcpy(writer->current, data, count);
1062         writer->current += count;
1063     }
1064 
1065     mpack_log("new buffer %p, used %i\n", new_buffer, (int)mpack_writer_buffer_used(writer));
1066 }
1067 
mpack_growable_writer_teardown(mpack_writer_t * writer)1068 static void mpack_growable_writer_teardown(mpack_writer_t* writer) {
1069     mpack_growable_writer_t* growable_writer = (mpack_growable_writer_t*)mpack_writer_get_reserved(writer);
1070 
1071     if (mpack_writer_error(writer) == mpack_ok) {
1072 
1073         // shrink the buffer to an appropriate size if the data is
1074         // much smaller than the buffer
1075         if (mpack_writer_buffer_used(writer) < mpack_writer_buffer_size(writer) / 2) {
1076             size_t used = mpack_writer_buffer_used(writer);
1077 
1078             // We always return a non-null pointer that must be freed, even if
1079             // nothing was written. malloc() and realloc() do not necessarily
1080             // do this so we enforce it ourselves.
1081             size_t size = (used != 0) ? used : 1;
1082 
1083             char* buffer = (char*)mpack_realloc(writer->buffer, used, size);
1084             if (!buffer) {
1085                 MPACK_FREE(writer->buffer);
1086                 mpack_writer_flag_error(writer, mpack_error_memory);
1087                 return;
1088             }
1089             writer->buffer = buffer;
1090             writer->end = (writer->current = writer->buffer + used);
1091         }
1092 
1093         *growable_writer->target_data = writer->buffer;
1094         *growable_writer->target_size = mpack_writer_buffer_used(writer);
1095         writer->buffer = NULL;
1096 
1097     } else if (writer->buffer) {
1098         MPACK_FREE(writer->buffer);
1099         writer->buffer = NULL;
1100     }
1101 
1102     writer->context = NULL;
1103 }
1104 
mpack_writer_init_growable(mpack_writer_t * writer,char ** target_data,size_t * target_size)1105 void mpack_writer_init_growable(mpack_writer_t* writer, char** target_data, size_t* target_size) {
1106     mpack_assert(target_data != NULL, "cannot initialize writer without a destination for the data");
1107     mpack_assert(target_size != NULL, "cannot initialize writer without a destination for the size");
1108 
1109     *target_data = NULL;
1110     *target_size = 0;
1111 
1112     MPACK_STATIC_ASSERT(sizeof(mpack_growable_writer_t) <= sizeof(writer->reserved),
1113             "not enough reserved space for growable writer!");
1114     mpack_growable_writer_t* growable_writer = (mpack_growable_writer_t*)mpack_writer_get_reserved(writer);
1115 
1116     growable_writer->target_data = target_data;
1117     growable_writer->target_size = target_size;
1118 
1119     size_t capacity = MPACK_BUFFER_SIZE;
1120     char* buffer = (char*)MPACK_MALLOC(capacity);
1121     if (buffer == NULL) {
1122         mpack_writer_init_error(writer, mpack_error_memory);
1123         return;
1124     }
1125 
1126     mpack_writer_init(writer, buffer, capacity);
1127     mpack_writer_set_flush(writer, mpack_growable_writer_flush);
1128     mpack_writer_set_teardown(writer, mpack_growable_writer_teardown);
1129 }
1130 #endif
1131 
1132 #if MPACK_STDIO
mpack_file_writer_flush(mpack_writer_t * writer,const char * buffer,size_t count)1133 static void mpack_file_writer_flush(mpack_writer_t* writer, const char* buffer, size_t count) {
1134     FILE* file = (FILE*)writer->context;
1135     size_t written = fwrite((const void*)buffer, 1, count, file);
1136     if (written != count)
1137         mpack_writer_flag_error(writer, mpack_error_io);
1138 }
1139 
mpack_file_writer_teardown(mpack_writer_t * writer)1140 static void mpack_file_writer_teardown(mpack_writer_t* writer) {
1141     MPACK_FREE(writer->buffer);
1142     writer->buffer = NULL;
1143     writer->context = NULL;
1144 }
1145 
mpack_file_writer_teardown_close(mpack_writer_t * writer)1146 static void mpack_file_writer_teardown_close(mpack_writer_t* writer) {
1147     FILE* file = (FILE*)writer->context;
1148 
1149     if (file) {
1150         int ret = fclose(file);
1151         if (ret != 0)
1152             mpack_writer_flag_error(writer, mpack_error_io);
1153     }
1154 
1155     mpack_file_writer_teardown(writer);
1156 }
1157 
mpack_writer_init_stdfile(mpack_writer_t * writer,FILE * file,bool close_when_done)1158 void mpack_writer_init_stdfile(mpack_writer_t* writer, FILE* file, bool close_when_done) {
1159     mpack_assert(file != NULL, "file is NULL");
1160 
1161     size_t capacity = MPACK_BUFFER_SIZE;
1162     char* buffer = (char*)MPACK_MALLOC(capacity);
1163     if (buffer == NULL) {
1164         mpack_writer_init_error(writer, mpack_error_memory);
1165         if (close_when_done) {
1166             fclose(file);
1167         }
1168         return;
1169     }
1170 
1171     mpack_writer_init(writer, buffer, capacity);
1172     mpack_writer_set_context(writer, file);
1173     mpack_writer_set_flush(writer, mpack_file_writer_flush);
1174     mpack_writer_set_teardown(writer, close_when_done ?
1175             mpack_file_writer_teardown_close :
1176             mpack_file_writer_teardown);
1177 }
1178 
mpack_writer_init_filename(mpack_writer_t * writer,const char * filename)1179 void mpack_writer_init_filename(mpack_writer_t* writer, const char* filename) {
1180     mpack_assert(filename != NULL, "filename is NULL");
1181 
1182     FILE* file = fopen(filename, "wb");
1183     if (file == NULL) {
1184         mpack_writer_init_error(writer, mpack_error_io);
1185         return;
1186     }
1187 
1188     mpack_writer_init_stdfile(writer, file, true);
1189 }
1190 #endif
1191 
mpack_writer_flag_error(mpack_writer_t * writer,mpack_error_t error)1192 void mpack_writer_flag_error(mpack_writer_t* writer, mpack_error_t error) {
1193     mpack_log("writer %p setting error %i: %s\n", writer, (int)error, mpack_error_to_string(error));
1194 
1195     if (writer->error == mpack_ok) {
1196         writer->error = error;
1197         if (writer->error_fn)
1198             writer->error_fn(writer, writer->error);
1199     }
1200 }
1201 
mpack_writer_flush_unchecked(mpack_writer_t * writer)1202 MPACK_STATIC_INLINE void mpack_writer_flush_unchecked(mpack_writer_t* writer) {
1203     // This is a bit ugly; we reset used before calling flush so that
1204     // a flush function can distinguish between flushing the buffer
1205     // versus flushing external data. see mpack_growable_writer_flush()
1206     size_t used = mpack_writer_buffer_used(writer);
1207     writer->current = writer->buffer;
1208     writer->flush(writer, writer->buffer, used);
1209 }
1210 
mpack_writer_flush_message(mpack_writer_t * writer)1211 void mpack_writer_flush_message(mpack_writer_t* writer) {
1212     if (writer->error != mpack_ok)
1213         return;
1214 
1215     #if MPACK_WRITE_TRACKING
1216     mpack_writer_flag_if_error(writer, mpack_track_check_empty(&writer->track));
1217     if (writer->error != mpack_ok)
1218         return;
1219     #endif
1220 
1221     if (writer->flush == NULL) {
1222         mpack_break("cannot call mpack_writer_flush_message() without a flush function!");
1223         mpack_writer_flag_error(writer, mpack_error_bug);
1224         return;
1225     }
1226 
1227     if (mpack_writer_buffer_used(writer) > 0)
1228         mpack_writer_flush_unchecked(writer);
1229 }
1230 
1231 // Ensures there are at least count bytes free in the buffer. This
1232 // will flag an error if the flush function fails to make enough
1233 // room in the buffer.
mpack_writer_ensure(mpack_writer_t * writer,size_t count)1234 MPACK_NOINLINE static bool mpack_writer_ensure(mpack_writer_t* writer, size_t count) {
1235     mpack_assert(count != 0, "cannot ensure zero bytes!");
1236     mpack_assert(count <= MPACK_WRITER_MINIMUM_BUFFER_SIZE,
1237             "cannot ensure %i bytes, this is more than the minimum buffer size %i!",
1238             (int)count, (int)MPACK_WRITER_MINIMUM_BUFFER_SIZE);
1239     mpack_assert(count > mpack_writer_buffer_left(writer),
1240             "request to ensure %i bytes but there are already %i left in the buffer!",
1241             (int)count, (int)mpack_writer_buffer_left(writer));
1242 
1243     mpack_log("ensuring %i bytes, %i left\n", (int)count, (int)mpack_writer_buffer_left(writer));
1244 
1245     if (mpack_writer_error(writer) != mpack_ok)
1246         return false;
1247 
1248     if (writer->flush == NULL) {
1249         mpack_writer_flag_error(writer, mpack_error_too_big);
1250         return false;
1251     }
1252 
1253     mpack_writer_flush_unchecked(writer);
1254     if (mpack_writer_error(writer) != mpack_ok)
1255         return false;
1256 
1257     if (mpack_writer_buffer_left(writer) >= count)
1258         return true;
1259 
1260     mpack_writer_flag_error(writer, mpack_error_io);
1261     return false;
1262 }
1263 
1264 // Writes encoded bytes to the buffer when we already know the data
1265 // does not fit in the buffer (i.e. it straddles the edge of the
1266 // buffer.) If there is a flush function, it is guaranteed to be
1267 // called; otherwise mpack_error_too_big is raised.
mpack_write_native_straddle(mpack_writer_t * writer,const char * p,size_t count)1268 MPACK_NOINLINE static void mpack_write_native_straddle(mpack_writer_t* writer, const char* p, size_t count) {
1269     mpack_assert(count == 0 || p != NULL, "data pointer for %i bytes is NULL", (int)count);
1270 
1271     if (mpack_writer_error(writer) != mpack_ok)
1272         return;
1273     mpack_log("big write for %i bytes from %p, %i space left in buffer\n",
1274             (int)count, p, (int)mpack_writer_buffer_left(writer));
1275     mpack_assert(count > mpack_writer_buffer_left(writer),
1276             "big write requested for %i bytes, but there is %i available "
1277             "space in buffer. should have called mpack_write_native() instead",
1278             (int)count, (int)(mpack_writer_buffer_left(writer)));
1279 
1280     // we'll need a flush function
1281     if (!writer->flush) {
1282         mpack_writer_flag_error(writer, mpack_error_too_big);
1283         return;
1284     }
1285 
1286     // flush the buffer
1287     mpack_writer_flush_unchecked(writer);
1288     if (mpack_writer_error(writer) != mpack_ok)
1289         return;
1290 
1291     // note that an intrusive flush function (such as mpack_growable_writer_flush())
1292     // may have changed size and/or reset used to a non-zero value. we treat both as
1293     // though they may have changed, and there may still be data in the buffer.
1294 
1295     // flush the extra data directly if it doesn't fit in the buffer
1296     if (count > mpack_writer_buffer_left(writer)) {
1297         writer->flush(writer, p, count);
1298         if (mpack_writer_error(writer) != mpack_ok)
1299             return;
1300     } else {
1301         mpack_memcpy(writer->current, p, count);
1302         writer->current += count;
1303     }
1304 }
1305 
1306 // Writes encoded bytes to the buffer, flushing if necessary.
mpack_write_native(mpack_writer_t * writer,const char * p,size_t count)1307 MPACK_STATIC_INLINE void mpack_write_native(mpack_writer_t* writer, const char* p, size_t count) {
1308     mpack_assert(count == 0 || p != NULL, "data pointer for %i bytes is NULL", (int)count);
1309 
1310     if (mpack_writer_buffer_left(writer) < count) {
1311         mpack_write_native_straddle(writer, p, count);
1312     } else {
1313         mpack_memcpy(writer->current, p, count);
1314         writer->current += count;
1315     }
1316 }
1317 
mpack_writer_destroy(mpack_writer_t * writer)1318 mpack_error_t mpack_writer_destroy(mpack_writer_t* writer) {
1319 
1320     // clean up tracking, asserting if we're not already in an error state
1321     #if MPACK_WRITE_TRACKING
1322     mpack_track_destroy(&writer->track, writer->error != mpack_ok);
1323     #endif
1324 
1325     // flush any outstanding data
1326     if (mpack_writer_error(writer) == mpack_ok && mpack_writer_buffer_used(writer) != 0 && writer->flush != NULL) {
1327         writer->flush(writer, writer->buffer, mpack_writer_buffer_used(writer));
1328         writer->flush = NULL;
1329     }
1330 
1331     if (writer->teardown) {
1332         writer->teardown(writer);
1333         writer->teardown = NULL;
1334     }
1335 
1336     return writer->error;
1337 }
1338 
mpack_write_tag(mpack_writer_t * writer,mpack_tag_t value)1339 void mpack_write_tag(mpack_writer_t* writer, mpack_tag_t value) {
1340     switch (value.type) {
1341         case mpack_type_missing:
1342             mpack_break("cannot write a missing value!");
1343             mpack_writer_flag_error(writer, mpack_error_bug);
1344             return;
1345 
1346         case mpack_type_nil:    mpack_write_nil   (writer);            return;
1347         case mpack_type_bool:   mpack_write_bool  (writer, value.v.b); return;
1348         case mpack_type_float:  mpack_write_float (writer, value.v.f); return;
1349         case mpack_type_double: mpack_write_double(writer, value.v.d); return;
1350         case mpack_type_int:    mpack_write_int   (writer, value.v.i); return;
1351         case mpack_type_uint:   mpack_write_uint  (writer, value.v.u); return;
1352 
1353         case mpack_type_str: mpack_start_str(writer, value.v.l); return;
1354         case mpack_type_bin: mpack_start_bin(writer, value.v.l); return;
1355 
1356         #if MPACK_EXTENSIONS
1357         case mpack_type_ext:
1358             mpack_start_ext(writer, mpack_tag_ext_exttype(&value), mpack_tag_ext_length(&value));
1359             return;
1360         #endif
1361 
1362         case mpack_type_array: mpack_start_array(writer, value.v.n); return;
1363         case mpack_type_map:   mpack_start_map(writer, value.v.n);   return;
1364     }
1365 
1366     mpack_break("unrecognized type %i", (int)value.type);
1367     mpack_writer_flag_error(writer, mpack_error_bug);
1368 }
1369 
mpack_write_byte_element(mpack_writer_t * writer,char value)1370 MPACK_STATIC_INLINE void mpack_write_byte_element(mpack_writer_t* writer, char value) {
1371     mpack_writer_track_element(writer);
1372     if (MPACK_LIKELY(mpack_writer_buffer_left(writer) >= 1) || mpack_writer_ensure(writer, 1))
1373         *(writer->current++) = value;
1374 }
1375 
mpack_write_nil(mpack_writer_t * writer)1376 void mpack_write_nil(mpack_writer_t* writer) {
1377     mpack_write_byte_element(writer, (char)0xc0);
1378 }
1379 
mpack_write_bool(mpack_writer_t * writer,bool value)1380 void mpack_write_bool(mpack_writer_t* writer, bool value) {
1381     mpack_write_byte_element(writer, (char)(0xc2 | (value ? 1 : 0)));
1382 }
1383 
mpack_write_true(mpack_writer_t * writer)1384 void mpack_write_true(mpack_writer_t* writer) {
1385     mpack_write_byte_element(writer, (char)0xc3);
1386 }
1387 
mpack_write_false(mpack_writer_t * writer)1388 void mpack_write_false(mpack_writer_t* writer) {
1389     mpack_write_byte_element(writer, (char)0xc2);
1390 }
1391 
mpack_write_object_bytes(mpack_writer_t * writer,const char * data,size_t bytes)1392 void mpack_write_object_bytes(mpack_writer_t* writer, const char* data, size_t bytes) {
1393     mpack_writer_track_element(writer);
1394     mpack_write_native(writer, data, bytes);
1395 }
1396 
1397 /*
1398  * Encode functions
1399  */
1400 
mpack_encode_fixuint(char * p,uint8_t value)1401 MPACK_STATIC_INLINE void mpack_encode_fixuint(char* p, uint8_t value) {
1402     mpack_assert(value <= 127);
1403     mpack_store_u8(p, value);
1404 }
1405 
mpack_encode_u8(char * p,uint8_t value)1406 MPACK_STATIC_INLINE void mpack_encode_u8(char* p, uint8_t value) {
1407     mpack_assert(value > 127);
1408     mpack_store_u8(p, 0xcc);
1409     mpack_store_u8(p + 1, value);
1410 }
1411 
mpack_encode_u16(char * p,uint16_t value)1412 MPACK_STATIC_INLINE void mpack_encode_u16(char* p, uint16_t value) {
1413     mpack_assert(value > UINT8_MAX);
1414     mpack_store_u8(p, 0xcd);
1415     mpack_store_u16(p + 1, value);
1416 }
1417 
mpack_encode_u32(char * p,uint32_t value)1418 MPACK_STATIC_INLINE void mpack_encode_u32(char* p, uint32_t value) {
1419     mpack_assert(value > UINT16_MAX);
1420     mpack_store_u8(p, 0xce);
1421     mpack_store_u32(p + 1, value);
1422 }
1423 
mpack_encode_u64(char * p,uint64_t value)1424 MPACK_STATIC_INLINE void mpack_encode_u64(char* p, uint64_t value) {
1425     mpack_assert(value > UINT32_MAX);
1426     mpack_store_u8(p, 0xcf);
1427     mpack_store_u64(p + 1, value);
1428 }
1429 
mpack_encode_fixint(char * p,int8_t value)1430 MPACK_STATIC_INLINE void mpack_encode_fixint(char* p, int8_t value) {
1431     // this can encode positive or negative fixints
1432     mpack_assert(value >= -32);
1433     mpack_store_i8(p, value);
1434 }
1435 
mpack_encode_i8(char * p,int8_t value)1436 MPACK_STATIC_INLINE void mpack_encode_i8(char* p, int8_t value) {
1437     mpack_assert(value < -32);
1438     mpack_store_u8(p, 0xd0);
1439     mpack_store_i8(p + 1, value);
1440 }
1441 
mpack_encode_i16(char * p,int16_t value)1442 MPACK_STATIC_INLINE void mpack_encode_i16(char* p, int16_t value) {
1443     mpack_assert(value < INT8_MIN);
1444     mpack_store_u8(p, 0xd1);
1445     mpack_store_i16(p + 1, value);
1446 }
1447 
mpack_encode_i32(char * p,int32_t value)1448 MPACK_STATIC_INLINE void mpack_encode_i32(char* p, int32_t value) {
1449     mpack_assert(value < INT16_MIN);
1450     mpack_store_u8(p, 0xd2);
1451     mpack_store_i32(p + 1, value);
1452 }
1453 
mpack_encode_i64(char * p,int64_t value)1454 MPACK_STATIC_INLINE void mpack_encode_i64(char* p, int64_t value) {
1455     mpack_assert(value < INT32_MIN);
1456     mpack_store_u8(p, 0xd3);
1457     mpack_store_i64(p + 1, value);
1458 }
1459 
mpack_encode_float(char * p,float value)1460 MPACK_STATIC_INLINE void mpack_encode_float(char* p, float value) {
1461     mpack_store_u8(p, 0xca);
1462     mpack_store_float(p + 1, value);
1463 }
1464 
mpack_encode_double(char * p,double value)1465 MPACK_STATIC_INLINE void mpack_encode_double(char* p, double value) {
1466     mpack_store_u8(p, 0xcb);
1467     mpack_store_double(p + 1, value);
1468 }
1469 
mpack_encode_fixarray(char * p,uint8_t count)1470 MPACK_STATIC_INLINE void mpack_encode_fixarray(char* p, uint8_t count) {
1471     mpack_assert(count <= 15);
1472     mpack_store_u8(p, (uint8_t)(0x90 | count));
1473 }
1474 
mpack_encode_array16(char * p,uint16_t count)1475 MPACK_STATIC_INLINE void mpack_encode_array16(char* p, uint16_t count) {
1476     mpack_assert(count > 15);
1477     mpack_store_u8(p, 0xdc);
1478     mpack_store_u16(p + 1, count);
1479 }
1480 
mpack_encode_array32(char * p,uint32_t count)1481 MPACK_STATIC_INLINE void mpack_encode_array32(char* p, uint32_t count) {
1482     mpack_assert(count > UINT16_MAX);
1483     mpack_store_u8(p, 0xdd);
1484     mpack_store_u32(p + 1, count);
1485 }
1486 
mpack_encode_fixmap(char * p,uint8_t count)1487 MPACK_STATIC_INLINE void mpack_encode_fixmap(char* p, uint8_t count) {
1488     mpack_assert(count <= 15);
1489     mpack_store_u8(p, (uint8_t)(0x80 | count));
1490 }
1491 
mpack_encode_map16(char * p,uint16_t count)1492 MPACK_STATIC_INLINE void mpack_encode_map16(char* p, uint16_t count) {
1493     mpack_assert(count > 15);
1494     mpack_store_u8(p, 0xde);
1495     mpack_store_u16(p + 1, count);
1496 }
1497 
mpack_encode_map32(char * p,uint32_t count)1498 MPACK_STATIC_INLINE void mpack_encode_map32(char* p, uint32_t count) {
1499     mpack_assert(count > UINT16_MAX);
1500     mpack_store_u8(p, 0xdf);
1501     mpack_store_u32(p + 1, count);
1502 }
1503 
mpack_encode_fixstr(char * p,uint8_t count)1504 MPACK_STATIC_INLINE void mpack_encode_fixstr(char* p, uint8_t count) {
1505     mpack_assert(count <= 31);
1506     mpack_store_u8(p, (uint8_t)(0xa0 | count));
1507 }
1508 
mpack_encode_str8(char * p,uint8_t count)1509 MPACK_STATIC_INLINE void mpack_encode_str8(char* p, uint8_t count) {
1510     mpack_assert(count > 31);
1511     mpack_store_u8(p, 0xd9);
1512     mpack_store_u8(p + 1, count);
1513 }
1514 
mpack_encode_str16(char * p,uint16_t count)1515 MPACK_STATIC_INLINE void mpack_encode_str16(char* p, uint16_t count) {
1516     // we might be encoding a raw in compatibility mode, so we
1517     // allow count to be in the range [32, UINT8_MAX].
1518     mpack_assert(count > 31);
1519     mpack_store_u8(p, 0xda);
1520     mpack_store_u16(p + 1, count);
1521 }
1522 
mpack_encode_str32(char * p,uint32_t count)1523 MPACK_STATIC_INLINE void mpack_encode_str32(char* p, uint32_t count) {
1524     mpack_assert(count > UINT16_MAX);
1525     mpack_store_u8(p, 0xdb);
1526     mpack_store_u32(p + 1, count);
1527 }
1528 
mpack_encode_bin8(char * p,uint8_t count)1529 MPACK_STATIC_INLINE void mpack_encode_bin8(char* p, uint8_t count) {
1530     mpack_store_u8(p, 0xc4);
1531     mpack_store_u8(p + 1, count);
1532 }
1533 
mpack_encode_bin16(char * p,uint16_t count)1534 MPACK_STATIC_INLINE void mpack_encode_bin16(char* p, uint16_t count) {
1535     mpack_assert(count > UINT8_MAX);
1536     mpack_store_u8(p, 0xc5);
1537     mpack_store_u16(p + 1, count);
1538 }
1539 
mpack_encode_bin32(char * p,uint32_t count)1540 MPACK_STATIC_INLINE void mpack_encode_bin32(char* p, uint32_t count) {
1541     mpack_assert(count > UINT16_MAX);
1542     mpack_store_u8(p, 0xc6);
1543     mpack_store_u32(p + 1, count);
1544 }
1545 
1546 #if MPACK_EXTENSIONS
mpack_encode_fixext1(char * p,int8_t exttype)1547 MPACK_STATIC_INLINE void mpack_encode_fixext1(char* p, int8_t exttype) {
1548     mpack_store_u8(p, 0xd4);
1549     mpack_store_i8(p + 1, exttype);
1550 }
1551 
mpack_encode_fixext2(char * p,int8_t exttype)1552 MPACK_STATIC_INLINE void mpack_encode_fixext2(char* p, int8_t exttype) {
1553     mpack_store_u8(p, 0xd5);
1554     mpack_store_i8(p + 1, exttype);
1555 }
1556 
mpack_encode_fixext4(char * p,int8_t exttype)1557 MPACK_STATIC_INLINE void mpack_encode_fixext4(char* p, int8_t exttype) {
1558     mpack_store_u8(p, 0xd6);
1559     mpack_store_i8(p + 1, exttype);
1560 }
1561 
mpack_encode_fixext8(char * p,int8_t exttype)1562 MPACK_STATIC_INLINE void mpack_encode_fixext8(char* p, int8_t exttype) {
1563     mpack_store_u8(p, 0xd7);
1564     mpack_store_i8(p + 1, exttype);
1565 }
1566 
mpack_encode_fixext16(char * p,int8_t exttype)1567 MPACK_STATIC_INLINE void mpack_encode_fixext16(char* p, int8_t exttype) {
1568     mpack_store_u8(p, 0xd8);
1569     mpack_store_i8(p + 1, exttype);
1570 }
1571 
mpack_encode_ext8(char * p,int8_t exttype,uint8_t count)1572 MPACK_STATIC_INLINE void mpack_encode_ext8(char* p, int8_t exttype, uint8_t count) {
1573     mpack_assert(count != 1 && count != 2 && count != 4 && count != 8 && count != 16);
1574     mpack_store_u8(p, 0xc7);
1575     mpack_store_u8(p + 1, count);
1576     mpack_store_i8(p + 2, exttype);
1577 }
1578 
mpack_encode_ext16(char * p,int8_t exttype,uint16_t count)1579 MPACK_STATIC_INLINE void mpack_encode_ext16(char* p, int8_t exttype, uint16_t count) {
1580     mpack_assert(count > UINT8_MAX);
1581     mpack_store_u8(p, 0xc8);
1582     mpack_store_u16(p + 1, count);
1583     mpack_store_i8(p + 3, exttype);
1584 }
1585 
mpack_encode_ext32(char * p,int8_t exttype,uint32_t count)1586 MPACK_STATIC_INLINE void mpack_encode_ext32(char* p, int8_t exttype, uint32_t count) {
1587     mpack_assert(count > UINT16_MAX);
1588     mpack_store_u8(p, 0xc9);
1589     mpack_store_u32(p + 1, count);
1590     mpack_store_i8(p + 5, exttype);
1591 }
1592 
mpack_encode_timestamp_4(char * p,uint32_t seconds)1593 MPACK_STATIC_INLINE void mpack_encode_timestamp_4(char* p, uint32_t seconds) {
1594     mpack_encode_fixext4(p, MPACK_EXTTYPE_TIMESTAMP);
1595     mpack_store_u32(p + MPACK_TAG_SIZE_FIXEXT4, seconds);
1596 }
1597 
mpack_encode_timestamp_8(char * p,int64_t seconds,uint32_t nanoseconds)1598 MPACK_STATIC_INLINE void mpack_encode_timestamp_8(char* p, int64_t seconds, uint32_t nanoseconds) {
1599     mpack_assert(nanoseconds <= MPACK_TIMESTAMP_NANOSECONDS_MAX);
1600     mpack_encode_fixext8(p, MPACK_EXTTYPE_TIMESTAMP);
1601     uint64_t encoded = ((uint64_t)nanoseconds << 34) | (uint64_t)seconds;
1602     mpack_store_u64(p + MPACK_TAG_SIZE_FIXEXT8, encoded);
1603 }
1604 
mpack_encode_timestamp_12(char * p,int64_t seconds,uint32_t nanoseconds)1605 MPACK_STATIC_INLINE void mpack_encode_timestamp_12(char* p, int64_t seconds, uint32_t nanoseconds) {
1606     mpack_assert(nanoseconds <= MPACK_TIMESTAMP_NANOSECONDS_MAX);
1607     mpack_encode_ext8(p, MPACK_EXTTYPE_TIMESTAMP, 12);
1608     mpack_store_u32(p + MPACK_TAG_SIZE_EXT8, nanoseconds);
1609     mpack_store_i64(p + MPACK_TAG_SIZE_EXT8 + 4, seconds);
1610 }
1611 #endif
1612 
1613 
1614 
1615 /*
1616  * Write functions
1617  */
1618 
1619 // This is a macro wrapper to the encode functions to encode
1620 // directly into the buffer. If mpack_writer_ensure() fails
1621 // it will flag an error so we don't have to do anything.
1622 #define MPACK_WRITE_ENCODED(encode_fn, size, ...) do {                                                 \
1623     if (MPACK_LIKELY(mpack_writer_buffer_left(writer) >= size) || mpack_writer_ensure(writer, size)) { \
1624         MPACK_EXPAND(encode_fn(writer->current, __VA_ARGS__));                                         \
1625         writer->current += size;                                                                       \
1626     }                                                                                                  \
1627 } while (0)
1628 
mpack_write_u8(mpack_writer_t * writer,uint8_t value)1629 void mpack_write_u8(mpack_writer_t* writer, uint8_t value) {
1630     #if MPACK_OPTIMIZE_FOR_SIZE
1631     mpack_write_u64(writer, value);
1632     #else
1633     mpack_writer_track_element(writer);
1634     if (value <= 127) {
1635         MPACK_WRITE_ENCODED(mpack_encode_fixuint, MPACK_TAG_SIZE_FIXUINT, value);
1636     } else {
1637         MPACK_WRITE_ENCODED(mpack_encode_u8, MPACK_TAG_SIZE_U8, value);
1638     }
1639     #endif
1640 }
1641 
mpack_write_u16(mpack_writer_t * writer,uint16_t value)1642 void mpack_write_u16(mpack_writer_t* writer, uint16_t value) {
1643     #if MPACK_OPTIMIZE_FOR_SIZE
1644     mpack_write_u64(writer, value);
1645     #else
1646     mpack_writer_track_element(writer);
1647     if (value <= 127) {
1648         MPACK_WRITE_ENCODED(mpack_encode_fixuint, MPACK_TAG_SIZE_FIXUINT, (uint8_t)value);
1649     } else if (value <= UINT8_MAX) {
1650         MPACK_WRITE_ENCODED(mpack_encode_u8, MPACK_TAG_SIZE_U8, (uint8_t)value);
1651     } else {
1652         MPACK_WRITE_ENCODED(mpack_encode_u16, MPACK_TAG_SIZE_U16, value);
1653     }
1654     #endif
1655 }
1656 
mpack_write_u32(mpack_writer_t * writer,uint32_t value)1657 void mpack_write_u32(mpack_writer_t* writer, uint32_t value) {
1658     #if MPACK_OPTIMIZE_FOR_SIZE
1659     mpack_write_u64(writer, value);
1660     #else
1661     mpack_writer_track_element(writer);
1662     if (value <= 127) {
1663         MPACK_WRITE_ENCODED(mpack_encode_fixuint, MPACK_TAG_SIZE_FIXUINT, (uint8_t)value);
1664     } else if (value <= UINT8_MAX) {
1665         MPACK_WRITE_ENCODED(mpack_encode_u8, MPACK_TAG_SIZE_U8, (uint8_t)value);
1666     } else if (value <= UINT16_MAX) {
1667         MPACK_WRITE_ENCODED(mpack_encode_u16, MPACK_TAG_SIZE_U16, (uint16_t)value);
1668     } else {
1669         MPACK_WRITE_ENCODED(mpack_encode_u32, MPACK_TAG_SIZE_U32, value);
1670     }
1671     #endif
1672 }
1673 
mpack_write_u64(mpack_writer_t * writer,uint64_t value)1674 void mpack_write_u64(mpack_writer_t* writer, uint64_t value) {
1675     mpack_writer_track_element(writer);
1676 
1677     if (value <= 127) {
1678         MPACK_WRITE_ENCODED(mpack_encode_fixuint, MPACK_TAG_SIZE_FIXUINT, (uint8_t)value);
1679     } else if (value <= UINT8_MAX) {
1680         MPACK_WRITE_ENCODED(mpack_encode_u8, MPACK_TAG_SIZE_U8, (uint8_t)value);
1681     } else if (value <= UINT16_MAX) {
1682         MPACK_WRITE_ENCODED(mpack_encode_u16, MPACK_TAG_SIZE_U16, (uint16_t)value);
1683     } else if (value <= UINT32_MAX) {
1684         MPACK_WRITE_ENCODED(mpack_encode_u32, MPACK_TAG_SIZE_U32, (uint32_t)value);
1685     } else {
1686         MPACK_WRITE_ENCODED(mpack_encode_u64, MPACK_TAG_SIZE_U64, value);
1687     }
1688 }
1689 
mpack_write_i8(mpack_writer_t * writer,int8_t value)1690 void mpack_write_i8(mpack_writer_t* writer, int8_t value) {
1691     #if MPACK_OPTIMIZE_FOR_SIZE
1692     mpack_write_i64(writer, value);
1693     #else
1694     mpack_writer_track_element(writer);
1695     if (value >= -32) {
1696         // we encode positive and negative fixints together
1697         MPACK_WRITE_ENCODED(mpack_encode_fixint, MPACK_TAG_SIZE_FIXINT, (int8_t)value);
1698     } else {
1699         MPACK_WRITE_ENCODED(mpack_encode_i8, MPACK_TAG_SIZE_I8, (int8_t)value);
1700     }
1701     #endif
1702 }
1703 
mpack_write_i16(mpack_writer_t * writer,int16_t value)1704 void mpack_write_i16(mpack_writer_t* writer, int16_t value) {
1705     #if MPACK_OPTIMIZE_FOR_SIZE
1706     mpack_write_i64(writer, value);
1707     #else
1708     mpack_writer_track_element(writer);
1709     if (value >= -32) {
1710         if (value <= 127) {
1711             // we encode positive and negative fixints together
1712             MPACK_WRITE_ENCODED(mpack_encode_fixint, MPACK_TAG_SIZE_FIXINT, (int8_t)value);
1713         } else if (value <= UINT8_MAX) {
1714             MPACK_WRITE_ENCODED(mpack_encode_u8, MPACK_TAG_SIZE_U8, (uint8_t)value);
1715         } else {
1716             MPACK_WRITE_ENCODED(mpack_encode_u16, MPACK_TAG_SIZE_U16, (uint16_t)value);
1717         }
1718     } else if (value >= INT8_MIN) {
1719         MPACK_WRITE_ENCODED(mpack_encode_i8, MPACK_TAG_SIZE_I8, (int8_t)value);
1720     } else {
1721         MPACK_WRITE_ENCODED(mpack_encode_i16, MPACK_TAG_SIZE_I16, (int16_t)value);
1722     }
1723     #endif
1724 }
1725 
mpack_write_i32(mpack_writer_t * writer,int32_t value)1726 void mpack_write_i32(mpack_writer_t* writer, int32_t value) {
1727     #if MPACK_OPTIMIZE_FOR_SIZE
1728     mpack_write_i64(writer, value);
1729     #else
1730     mpack_writer_track_element(writer);
1731     if (value >= -32) {
1732         if (value <= 127) {
1733             // we encode positive and negative fixints together
1734             MPACK_WRITE_ENCODED(mpack_encode_fixint, MPACK_TAG_SIZE_FIXINT, (int8_t)value);
1735         } else if (value <= UINT8_MAX) {
1736             MPACK_WRITE_ENCODED(mpack_encode_u8, MPACK_TAG_SIZE_U8, (uint8_t)value);
1737         } else if (value <= UINT16_MAX) {
1738             MPACK_WRITE_ENCODED(mpack_encode_u16, MPACK_TAG_SIZE_U16, (uint16_t)value);
1739         } else {
1740             MPACK_WRITE_ENCODED(mpack_encode_u32, MPACK_TAG_SIZE_U32, (uint32_t)value);
1741         }
1742     } else if (value >= INT8_MIN) {
1743         MPACK_WRITE_ENCODED(mpack_encode_i8, MPACK_TAG_SIZE_I8, (int8_t)value);
1744     } else if (value >= INT16_MIN) {
1745         MPACK_WRITE_ENCODED(mpack_encode_i16, MPACK_TAG_SIZE_I16, (int16_t)value);
1746     } else {
1747         MPACK_WRITE_ENCODED(mpack_encode_i32, MPACK_TAG_SIZE_I32, value);
1748     }
1749     #endif
1750 }
1751 
mpack_write_i64(mpack_writer_t * writer,int64_t value)1752 void mpack_write_i64(mpack_writer_t* writer, int64_t value) {
1753     #if MPACK_OPTIMIZE_FOR_SIZE
1754     if (value > 127) {
1755         // for non-fix positive ints we call the u64 writer to save space
1756         mpack_write_u64(writer, (uint64_t)value);
1757         return;
1758     }
1759     #endif
1760 
1761     mpack_writer_track_element(writer);
1762     if (value >= -32) {
1763         #if MPACK_OPTIMIZE_FOR_SIZE
1764         MPACK_WRITE_ENCODED(mpack_encode_fixint, MPACK_TAG_SIZE_FIXINT, (int8_t)value);
1765         #else
1766         if (value <= 127) {
1767             MPACK_WRITE_ENCODED(mpack_encode_fixint, MPACK_TAG_SIZE_FIXINT, (int8_t)value);
1768         } else if (value <= UINT8_MAX) {
1769             MPACK_WRITE_ENCODED(mpack_encode_u8, MPACK_TAG_SIZE_U8, (uint8_t)value);
1770         } else if (value <= UINT16_MAX) {
1771             MPACK_WRITE_ENCODED(mpack_encode_u16, MPACK_TAG_SIZE_U16, (uint16_t)value);
1772         } else if (value <= UINT32_MAX) {
1773             MPACK_WRITE_ENCODED(mpack_encode_u32, MPACK_TAG_SIZE_U32, (uint32_t)value);
1774         } else {
1775             MPACK_WRITE_ENCODED(mpack_encode_u64, MPACK_TAG_SIZE_U64, (uint64_t)value);
1776         }
1777         #endif
1778     } else if (value >= INT8_MIN) {
1779         MPACK_WRITE_ENCODED(mpack_encode_i8, MPACK_TAG_SIZE_I8, (int8_t)value);
1780     } else if (value >= INT16_MIN) {
1781         MPACK_WRITE_ENCODED(mpack_encode_i16, MPACK_TAG_SIZE_I16, (int16_t)value);
1782     } else if (value >= INT32_MIN) {
1783         MPACK_WRITE_ENCODED(mpack_encode_i32, MPACK_TAG_SIZE_I32, (int32_t)value);
1784     } else {
1785         MPACK_WRITE_ENCODED(mpack_encode_i64, MPACK_TAG_SIZE_I64, value);
1786     }
1787 }
1788 
mpack_write_float(mpack_writer_t * writer,float value)1789 void mpack_write_float(mpack_writer_t* writer, float value) {
1790     mpack_writer_track_element(writer);
1791     MPACK_WRITE_ENCODED(mpack_encode_float, MPACK_TAG_SIZE_FLOAT, value);
1792 }
1793 
mpack_write_double(mpack_writer_t * writer,double value)1794 void mpack_write_double(mpack_writer_t* writer, double value) {
1795     mpack_writer_track_element(writer);
1796     MPACK_WRITE_ENCODED(mpack_encode_double, MPACK_TAG_SIZE_DOUBLE, value);
1797 }
1798 
1799 #if MPACK_EXTENSIONS
mpack_write_timestamp(mpack_writer_t * writer,int64_t seconds,uint32_t nanoseconds)1800 void mpack_write_timestamp(mpack_writer_t* writer, int64_t seconds, uint32_t nanoseconds) {
1801     #if MPACK_COMPATIBILITY
1802     if (writer->version <= mpack_version_v4) {
1803         mpack_break("Timestamps require spec version v5 or later. This writer is in v%i mode.", (int)writer->version);
1804         mpack_writer_flag_error(writer, mpack_error_bug);
1805         return;
1806     }
1807     #endif
1808 
1809     if (nanoseconds > MPACK_TIMESTAMP_NANOSECONDS_MAX) {
1810         mpack_break("timestamp nanoseconds out of bounds: %u", nanoseconds);
1811         mpack_writer_flag_error(writer, mpack_error_bug);
1812         return;
1813     }
1814 
1815     mpack_writer_track_element(writer);
1816 
1817     if (seconds < 0 || seconds >= (INT64_C(1) << 34)) {
1818         MPACK_WRITE_ENCODED(mpack_encode_timestamp_12, MPACK_EXT_SIZE_TIMESTAMP12, seconds, nanoseconds);
1819     } else if (seconds > UINT32_MAX || nanoseconds > 0) {
1820         MPACK_WRITE_ENCODED(mpack_encode_timestamp_8, MPACK_EXT_SIZE_TIMESTAMP8, seconds, nanoseconds);
1821     } else {
1822         MPACK_WRITE_ENCODED(mpack_encode_timestamp_4, MPACK_EXT_SIZE_TIMESTAMP4, (uint32_t)seconds);
1823     }
1824 }
1825 #endif
1826 
mpack_start_array(mpack_writer_t * writer,uint32_t count)1827 void mpack_start_array(mpack_writer_t* writer, uint32_t count) {
1828     mpack_writer_track_element(writer);
1829 
1830     if (count <= 15) {
1831         MPACK_WRITE_ENCODED(mpack_encode_fixarray, MPACK_TAG_SIZE_FIXARRAY, (uint8_t)count);
1832     } else if (count <= UINT16_MAX) {
1833         MPACK_WRITE_ENCODED(mpack_encode_array16, MPACK_TAG_SIZE_ARRAY16, (uint16_t)count);
1834     } else {
1835         MPACK_WRITE_ENCODED(mpack_encode_array32, MPACK_TAG_SIZE_ARRAY32, (uint32_t)count);
1836     }
1837 
1838     mpack_writer_track_push(writer, mpack_type_array, count);
1839 }
1840 
mpack_start_map(mpack_writer_t * writer,uint32_t count)1841 void mpack_start_map(mpack_writer_t* writer, uint32_t count) {
1842     mpack_writer_track_element(writer);
1843 
1844     if (count <= 15) {
1845         MPACK_WRITE_ENCODED(mpack_encode_fixmap, MPACK_TAG_SIZE_FIXMAP, (uint8_t)count);
1846     } else if (count <= UINT16_MAX) {
1847         MPACK_WRITE_ENCODED(mpack_encode_map16, MPACK_TAG_SIZE_MAP16, (uint16_t)count);
1848     } else {
1849         MPACK_WRITE_ENCODED(mpack_encode_map32, MPACK_TAG_SIZE_MAP32, (uint32_t)count);
1850     }
1851 
1852     mpack_writer_track_push(writer, mpack_type_map, count);
1853 }
1854 
mpack_start_str_notrack(mpack_writer_t * writer,uint32_t count)1855 static void mpack_start_str_notrack(mpack_writer_t* writer, uint32_t count) {
1856     if (count <= 31) {
1857         MPACK_WRITE_ENCODED(mpack_encode_fixstr, MPACK_TAG_SIZE_FIXSTR, (uint8_t)count);
1858 
1859     // str8 is only supported in v5 or later.
1860     } else if (count <= UINT8_MAX
1861             #if MPACK_COMPATIBILITY
1862             && writer->version >= mpack_version_v5
1863             #endif
1864             ) {
1865         MPACK_WRITE_ENCODED(mpack_encode_str8, MPACK_TAG_SIZE_STR8, (uint8_t)count);
1866 
1867     } else if (count <= UINT16_MAX) {
1868         MPACK_WRITE_ENCODED(mpack_encode_str16, MPACK_TAG_SIZE_STR16, (uint16_t)count);
1869     } else {
1870         MPACK_WRITE_ENCODED(mpack_encode_str32, MPACK_TAG_SIZE_STR32, (uint32_t)count);
1871     }
1872 }
1873 
mpack_start_bin_notrack(mpack_writer_t * writer,uint32_t count)1874 static void mpack_start_bin_notrack(mpack_writer_t* writer, uint32_t count) {
1875     #if MPACK_COMPATIBILITY
1876     // In the v4 spec, there was only the raw type for any kind of
1877     // variable-length data. In v4 mode, we support the bin functions,
1878     // but we produce an old-style raw.
1879     if (writer->version <= mpack_version_v4) {
1880         mpack_start_str_notrack(writer, count);
1881         return;
1882     }
1883     #endif
1884 
1885     if (count <= UINT8_MAX) {
1886         MPACK_WRITE_ENCODED(mpack_encode_bin8, MPACK_TAG_SIZE_BIN8, (uint8_t)count);
1887     } else if (count <= UINT16_MAX) {
1888         MPACK_WRITE_ENCODED(mpack_encode_bin16, MPACK_TAG_SIZE_BIN16, (uint16_t)count);
1889     } else {
1890         MPACK_WRITE_ENCODED(mpack_encode_bin32, MPACK_TAG_SIZE_BIN32, (uint32_t)count);
1891     }
1892 }
1893 
mpack_start_str(mpack_writer_t * writer,uint32_t count)1894 void mpack_start_str(mpack_writer_t* writer, uint32_t count) {
1895     mpack_writer_track_element(writer);
1896     mpack_start_str_notrack(writer, count);
1897     mpack_writer_track_push(writer, mpack_type_str, count);
1898 }
1899 
mpack_start_bin(mpack_writer_t * writer,uint32_t count)1900 void mpack_start_bin(mpack_writer_t* writer, uint32_t count) {
1901     mpack_writer_track_element(writer);
1902     mpack_start_bin_notrack(writer, count);
1903     mpack_writer_track_push(writer, mpack_type_bin, count);
1904 }
1905 
1906 #if MPACK_EXTENSIONS
mpack_start_ext(mpack_writer_t * writer,int8_t exttype,uint32_t count)1907 void mpack_start_ext(mpack_writer_t* writer, int8_t exttype, uint32_t count) {
1908     #if MPACK_COMPATIBILITY
1909     if (writer->version <= mpack_version_v4) {
1910         mpack_break("Ext types require spec version v5 or later. This writer is in v%i mode.", (int)writer->version);
1911         mpack_writer_flag_error(writer, mpack_error_bug);
1912         return;
1913     }
1914     #endif
1915 
1916     mpack_writer_track_element(writer);
1917 
1918     if (count == 1) {
1919         MPACK_WRITE_ENCODED(mpack_encode_fixext1, MPACK_TAG_SIZE_FIXEXT1, exttype);
1920     } else if (count == 2) {
1921         MPACK_WRITE_ENCODED(mpack_encode_fixext2, MPACK_TAG_SIZE_FIXEXT2, exttype);
1922     } else if (count == 4) {
1923         MPACK_WRITE_ENCODED(mpack_encode_fixext4, MPACK_TAG_SIZE_FIXEXT4, exttype);
1924     } else if (count == 8) {
1925         MPACK_WRITE_ENCODED(mpack_encode_fixext8, MPACK_TAG_SIZE_FIXEXT8, exttype);
1926     } else if (count == 16) {
1927         MPACK_WRITE_ENCODED(mpack_encode_fixext16, MPACK_TAG_SIZE_FIXEXT16, exttype);
1928     } else if (count <= UINT8_MAX) {
1929         MPACK_WRITE_ENCODED(mpack_encode_ext8, MPACK_TAG_SIZE_EXT8, exttype, (uint8_t)count);
1930     } else if (count <= UINT16_MAX) {
1931         MPACK_WRITE_ENCODED(mpack_encode_ext16, MPACK_TAG_SIZE_EXT16, exttype, (uint16_t)count);
1932     } else {
1933         MPACK_WRITE_ENCODED(mpack_encode_ext32, MPACK_TAG_SIZE_EXT32, exttype, (uint32_t)count);
1934     }
1935 
1936     mpack_writer_track_push(writer, mpack_type_ext, count);
1937 }
1938 #endif
1939 
1940 
1941 
1942 /*
1943  * Compound helpers and other functions
1944  */
1945 
mpack_write_str(mpack_writer_t * writer,const char * data,uint32_t count)1946 void mpack_write_str(mpack_writer_t* writer, const char* data, uint32_t count) {
1947     mpack_assert(data != NULL, "data for string of length %i is NULL", (int)count);
1948 
1949     #if MPACK_OPTIMIZE_FOR_SIZE
1950     mpack_writer_track_element(writer);
1951     mpack_start_str_notrack(writer, count);
1952     mpack_write_native(writer, data, count);
1953     #else
1954 
1955     mpack_writer_track_element(writer);
1956 
1957     if (count <= 31) {
1958         // The minimum buffer size when using a flush function is guaranteed to
1959         // fit the largest possible fixstr.
1960         size_t size = count + MPACK_TAG_SIZE_FIXSTR;
1961         if (MPACK_LIKELY(mpack_writer_buffer_left(writer) >= size) || mpack_writer_ensure(writer, size)) {
1962             char* MPACK_RESTRICT p = writer->current;
1963             mpack_encode_fixstr(p, (uint8_t)count);
1964             mpack_memcpy(p + MPACK_TAG_SIZE_FIXSTR, data, count);
1965             writer->current += count + MPACK_TAG_SIZE_FIXSTR;
1966         }
1967         return;
1968     }
1969 
1970     if (count <= UINT8_MAX
1971             #if MPACK_COMPATIBILITY
1972             && writer->version >= mpack_version_v5
1973             #endif
1974             ) {
1975         if (count + MPACK_TAG_SIZE_STR8 <= mpack_writer_buffer_left(writer)) {
1976             char* MPACK_RESTRICT p = writer->current;
1977             mpack_encode_str8(p, (uint8_t)count);
1978             mpack_memcpy(p + MPACK_TAG_SIZE_STR8, data, count);
1979             writer->current += count + MPACK_TAG_SIZE_STR8;
1980         } else {
1981             MPACK_WRITE_ENCODED(mpack_encode_str8, MPACK_TAG_SIZE_STR8, (uint8_t)count);
1982             mpack_write_native(writer, data, count);
1983         }
1984         return;
1985     }
1986 
1987     // str16 and str32 are likely to be a significant fraction of the buffer
1988     // size, so we don't bother with a combined space check in order to
1989     // minimize code size.
1990     if (count <= UINT16_MAX) {
1991         MPACK_WRITE_ENCODED(mpack_encode_str16, MPACK_TAG_SIZE_STR16, (uint16_t)count);
1992         mpack_write_native(writer, data, count);
1993     } else {
1994         MPACK_WRITE_ENCODED(mpack_encode_str32, MPACK_TAG_SIZE_STR32, (uint32_t)count);
1995         mpack_write_native(writer, data, count);
1996     }
1997 
1998     #endif
1999 }
2000 
mpack_write_bin(mpack_writer_t * writer,const char * data,uint32_t count)2001 void mpack_write_bin(mpack_writer_t* writer, const char* data, uint32_t count) {
2002     mpack_assert(data != NULL, "data pointer for bin of %i bytes is NULL", (int)count);
2003     mpack_start_bin(writer, count);
2004     mpack_write_bytes(writer, data, count);
2005     mpack_finish_bin(writer);
2006 }
2007 
2008 #if MPACK_EXTENSIONS
mpack_write_ext(mpack_writer_t * writer,int8_t exttype,const char * data,uint32_t count)2009 void mpack_write_ext(mpack_writer_t* writer, int8_t exttype, const char* data, uint32_t count) {
2010     mpack_assert(data != NULL, "data pointer for ext of type %i and %i bytes is NULL", exttype, (int)count);
2011     mpack_start_ext(writer, exttype, count);
2012     mpack_write_bytes(writer, data, count);
2013     mpack_finish_ext(writer);
2014 }
2015 #endif
2016 
mpack_write_bytes(mpack_writer_t * writer,const char * data,size_t count)2017 void mpack_write_bytes(mpack_writer_t* writer, const char* data, size_t count) {
2018     mpack_assert(data != NULL, "data pointer for %i bytes is NULL", (int)count);
2019     mpack_writer_track_bytes(writer, count);
2020     mpack_write_native(writer, data, count);
2021 }
2022 
mpack_write_cstr(mpack_writer_t * writer,const char * cstr)2023 void mpack_write_cstr(mpack_writer_t* writer, const char* cstr) {
2024     mpack_assert(cstr != NULL, "cstr pointer is NULL");
2025     size_t length = mpack_strlen(cstr);
2026     if (length > UINT32_MAX)
2027         mpack_writer_flag_error(writer, mpack_error_invalid);
2028     mpack_write_str(writer, cstr, (uint32_t)length);
2029 }
2030 
mpack_write_cstr_or_nil(mpack_writer_t * writer,const char * cstr)2031 void mpack_write_cstr_or_nil(mpack_writer_t* writer, const char* cstr) {
2032     if (cstr)
2033         mpack_write_cstr(writer, cstr);
2034     else
2035         mpack_write_nil(writer);
2036 }
2037 
mpack_write_utf8(mpack_writer_t * writer,const char * str,uint32_t length)2038 void mpack_write_utf8(mpack_writer_t* writer, const char* str, uint32_t length) {
2039     mpack_assert(str != NULL, "data for string of length %i is NULL", (int)length);
2040     if (!mpack_utf8_check(str, length)) {
2041         mpack_writer_flag_error(writer, mpack_error_invalid);
2042         return;
2043     }
2044     mpack_write_str(writer, str, length);
2045 }
2046 
mpack_write_utf8_cstr(mpack_writer_t * writer,const char * cstr)2047 void mpack_write_utf8_cstr(mpack_writer_t* writer, const char* cstr) {
2048     mpack_assert(cstr != NULL, "cstr pointer is NULL");
2049     size_t length = mpack_strlen(cstr);
2050     if (length > UINT32_MAX) {
2051         mpack_writer_flag_error(writer, mpack_error_invalid);
2052         return;
2053     }
2054     mpack_write_utf8(writer, cstr, (uint32_t)length);
2055 }
2056 
mpack_write_utf8_cstr_or_nil(mpack_writer_t * writer,const char * cstr)2057 void mpack_write_utf8_cstr_or_nil(mpack_writer_t* writer, const char* cstr) {
2058     if (cstr)
2059         mpack_write_utf8_cstr(writer, cstr);
2060     else
2061         mpack_write_nil(writer);
2062 }
2063 
2064 #endif
2065 
2066 
2067 /* mpack/mpack-reader.c.c */
2068 
2069 #define MPACK_INTERNAL 1
2070 
2071 /* #include "mpack-reader.h" */
2072 
2073 #if MPACK_READER
2074 
2075 static void mpack_reader_skip_using_fill(mpack_reader_t* reader, size_t count);
2076 
mpack_reader_init(mpack_reader_t * reader,char * buffer,size_t size,size_t count)2077 void mpack_reader_init(mpack_reader_t* reader, char* buffer, size_t size, size_t count) {
2078     mpack_assert(buffer != NULL, "buffer is NULL");
2079 
2080     mpack_memset(reader, 0, sizeof(*reader));
2081     reader->buffer = buffer;
2082     reader->size = size;
2083     reader->data = buffer;
2084     reader->end = buffer + count;
2085 
2086     #if MPACK_READ_TRACKING
2087     mpack_reader_flag_if_error(reader, mpack_track_init(&reader->track));
2088     #endif
2089 
2090     mpack_log("===========================\n");
2091     mpack_log("initializing reader with buffer size %i\n", (int)size);
2092 }
2093 
mpack_reader_init_error(mpack_reader_t * reader,mpack_error_t error)2094 void mpack_reader_init_error(mpack_reader_t* reader, mpack_error_t error) {
2095     mpack_memset(reader, 0, sizeof(*reader));
2096     reader->error = error;
2097 
2098     mpack_log("===========================\n");
2099     mpack_log("initializing reader error state %i\n", (int)error);
2100 }
2101 
mpack_reader_init_data(mpack_reader_t * reader,const char * data,size_t count)2102 void mpack_reader_init_data(mpack_reader_t* reader, const char* data, size_t count) {
2103     mpack_assert(data != NULL, "data is NULL");
2104 
2105     mpack_memset(reader, 0, sizeof(*reader));
2106     reader->data = data;
2107     reader->end = data + count;
2108 
2109     #if MPACK_READ_TRACKING
2110     mpack_reader_flag_if_error(reader, mpack_track_init(&reader->track));
2111     #endif
2112 
2113     mpack_log("===========================\n");
2114     mpack_log("initializing reader with data size %i\n", (int)count);
2115 }
2116 
mpack_reader_set_fill(mpack_reader_t * reader,mpack_reader_fill_t fill)2117 void mpack_reader_set_fill(mpack_reader_t* reader, mpack_reader_fill_t fill) {
2118     MPACK_STATIC_ASSERT(MPACK_READER_MINIMUM_BUFFER_SIZE >= MPACK_MAXIMUM_TAG_SIZE,
2119             "minimum buffer size must fit any tag!");
2120 
2121     if (reader->size == 0) {
2122         mpack_break("cannot use fill function without a writeable buffer!");
2123         mpack_reader_flag_error(reader, mpack_error_bug);
2124         return;
2125     }
2126 
2127     if (reader->size < MPACK_READER_MINIMUM_BUFFER_SIZE) {
2128         mpack_break("buffer size is %i, but minimum buffer size for fill is %i",
2129                 (int)reader->size, MPACK_READER_MINIMUM_BUFFER_SIZE);
2130         mpack_reader_flag_error(reader, mpack_error_bug);
2131         return;
2132     }
2133 
2134     reader->fill = fill;
2135 }
2136 
mpack_reader_set_skip(mpack_reader_t * reader,mpack_reader_skip_t skip)2137 void mpack_reader_set_skip(mpack_reader_t* reader, mpack_reader_skip_t skip) {
2138     mpack_assert(reader->size != 0, "cannot use skip function without a writeable buffer!");
2139     reader->skip = skip;
2140 }
2141 
2142 #if MPACK_STDIO
mpack_file_reader_fill(mpack_reader_t * reader,char * buffer,size_t count)2143 static size_t mpack_file_reader_fill(mpack_reader_t* reader, char* buffer, size_t count) {
2144     if (feof((FILE *)reader->context)) {
2145        mpack_reader_flag_error(reader, mpack_error_eof);
2146        return 0;
2147     }
2148     return fread((void*)buffer, 1, count, (FILE*)reader->context);
2149 }
2150 
mpack_file_reader_skip(mpack_reader_t * reader,size_t count)2151 static void mpack_file_reader_skip(mpack_reader_t* reader, size_t count) {
2152     if (mpack_reader_error(reader) != mpack_ok)
2153         return;
2154     FILE* file = (FILE*)reader->context;
2155 
2156     // We call ftell() to test whether the stream is seekable
2157     // without causing a file error.
2158     if (ftell(file) >= 0) {
2159         mpack_log("seeking forward %i bytes\n", (int)count);
2160         if (fseek(file, (long int)count, SEEK_CUR) == 0)
2161             return;
2162         mpack_log("fseek() didn't return zero!\n");
2163         if (ferror(file)) {
2164             mpack_reader_flag_error(reader, mpack_error_io);
2165             return;
2166         }
2167     }
2168 
2169     // If the stream is not seekable, fall back to the fill function.
2170     mpack_reader_skip_using_fill(reader, count);
2171 }
2172 
mpack_file_reader_teardown(mpack_reader_t * reader)2173 static void mpack_file_reader_teardown(mpack_reader_t* reader) {
2174     MPACK_FREE(reader->buffer);
2175     reader->buffer = NULL;
2176     reader->context = NULL;
2177     reader->size = 0;
2178     reader->fill = NULL;
2179     reader->skip = NULL;
2180     reader->teardown = NULL;
2181 }
2182 
mpack_file_reader_teardown_close(mpack_reader_t * reader)2183 static void mpack_file_reader_teardown_close(mpack_reader_t* reader) {
2184     FILE* file = (FILE*)reader->context;
2185 
2186     if (file) {
2187         int ret = fclose(file);
2188         if (ret != 0)
2189             mpack_reader_flag_error(reader, mpack_error_io);
2190     }
2191 
2192     mpack_file_reader_teardown(reader);
2193 }
2194 
mpack_reader_init_stdfile(mpack_reader_t * reader,FILE * file,bool close_when_done)2195 void mpack_reader_init_stdfile(mpack_reader_t* reader, FILE* file, bool close_when_done) {
2196     mpack_assert(file != NULL, "file is NULL");
2197 
2198     size_t capacity = MPACK_BUFFER_SIZE;
2199     char* buffer = (char*)MPACK_MALLOC(capacity);
2200     if (buffer == NULL) {
2201         mpack_reader_init_error(reader, mpack_error_memory);
2202         if (close_when_done) {
2203             fclose(file);
2204         }
2205         return;
2206     }
2207 
2208     mpack_reader_init(reader, buffer, capacity, 0);
2209     mpack_reader_set_context(reader, file);
2210     mpack_reader_set_fill(reader, mpack_file_reader_fill);
2211     mpack_reader_set_skip(reader, mpack_file_reader_skip);
2212     mpack_reader_set_teardown(reader, close_when_done ?
2213             mpack_file_reader_teardown_close :
2214             mpack_file_reader_teardown);
2215 }
2216 
mpack_reader_init_filename(mpack_reader_t * reader,const char * filename)2217 void mpack_reader_init_filename(mpack_reader_t* reader, const char* filename) {
2218     mpack_assert(filename != NULL, "filename is NULL");
2219 
2220     FILE* file = fopen(filename, "rb");
2221     if (file == NULL) {
2222         mpack_reader_init_error(reader, mpack_error_io);
2223         return;
2224     }
2225 
2226     mpack_reader_init_stdfile(reader, file, true);
2227 }
2228 #endif
2229 
mpack_reader_destroy(mpack_reader_t * reader)2230 mpack_error_t mpack_reader_destroy(mpack_reader_t* reader) {
2231 
2232     // clean up tracking, asserting if we're not already in an error state
2233     #if MPACK_READ_TRACKING
2234     mpack_reader_flag_if_error(reader, mpack_track_destroy(&reader->track, mpack_reader_error(reader) != mpack_ok));
2235     #endif
2236 
2237     if (reader->teardown)
2238         reader->teardown(reader);
2239     reader->teardown = NULL;
2240 
2241     return reader->error;
2242 }
2243 
mpack_reader_remaining(mpack_reader_t * reader,const char ** data)2244 size_t mpack_reader_remaining(mpack_reader_t* reader, const char** data) {
2245     if (mpack_reader_error(reader) != mpack_ok)
2246         return 0;
2247 
2248     #if MPACK_READ_TRACKING
2249     if (mpack_reader_flag_if_error(reader, mpack_track_check_empty(&reader->track)) != mpack_ok)
2250         return 0;
2251     #endif
2252 
2253     if (data)
2254         *data = reader->data;
2255     return (size_t)(reader->end - reader->data);
2256 }
2257 
mpack_reader_flag_error(mpack_reader_t * reader,mpack_error_t error)2258 void mpack_reader_flag_error(mpack_reader_t* reader, mpack_error_t error) {
2259     mpack_log("reader %p setting error %i: %s\n", reader, (int)error, mpack_error_to_string(error));
2260 
2261     if (reader->error == mpack_ok) {
2262         reader->error = error;
2263         reader->end = reader->data;
2264         if (reader->error_fn)
2265             reader->error_fn(reader, error);
2266     }
2267 }
2268 
2269 // Loops on the fill function, reading between the minimum and
2270 // maximum number of bytes and flagging an error if it fails.
mpack_fill_range(mpack_reader_t * reader,char * p,size_t min_bytes,size_t max_bytes)2271 MPACK_NOINLINE static size_t mpack_fill_range(mpack_reader_t* reader, char* p, size_t min_bytes, size_t max_bytes) {
2272     mpack_assert(reader->fill != NULL, "mpack_fill_range() called with no fill function?");
2273     mpack_assert(min_bytes > 0, "cannot fill zero bytes!");
2274     mpack_assert(max_bytes >= min_bytes, "min_bytes %i cannot be larger than max_bytes %i!",
2275             (int)min_bytes, (int)max_bytes);
2276 
2277     size_t count = 0;
2278     while (count < min_bytes) {
2279         size_t read = reader->fill(reader, p + count, max_bytes - count);
2280 
2281         // Reader fill functions can flag an error or return 0 on failure. We
2282         // also guard against functions that -1 just in case.
2283         if (mpack_reader_error(reader) != mpack_ok)
2284             return 0;
2285         if (read == 0 || read == ((size_t)(-1))) {
2286             mpack_reader_flag_error(reader, mpack_error_io);
2287             return 0;
2288         }
2289 
2290         count += read;
2291     }
2292     return count;
2293 }
2294 
mpack_reader_ensure_straddle(mpack_reader_t * reader,size_t count)2295 MPACK_NOINLINE bool mpack_reader_ensure_straddle(mpack_reader_t* reader, size_t count) {
2296     mpack_assert(count != 0, "cannot ensure zero bytes!");
2297     mpack_assert(reader->error == mpack_ok, "reader cannot be in an error state!");
2298 
2299     mpack_assert(count > (size_t)(reader->end - reader->data),
2300             "straddling ensure requested for %i bytes, but there are %i bytes "
2301             "left in buffer. call mpack_reader_ensure() instead",
2302             (int)count, (int)(reader->end - reader->data));
2303 
2304     // we'll need a fill function to get more data. if there's no
2305     // fill function, the buffer should contain an entire MessagePack
2306     // object, so we raise mpack_error_invalid instead of mpack_error_io
2307     // on truncated data.
2308     if (reader->fill == NULL) {
2309         mpack_reader_flag_error(reader, mpack_error_invalid);
2310         return false;
2311     }
2312 
2313     // we need enough space in the buffer. if the buffer is not
2314     // big enough, we return mpack_error_too_big (since this is
2315     // for an in-place read larger than the buffer size.)
2316     if (count > reader->size) {
2317         mpack_reader_flag_error(reader, mpack_error_too_big);
2318         return false;
2319     }
2320 
2321     // move the existing data to the start of the buffer
2322     size_t left = (size_t)(reader->end - reader->data);
2323     mpack_memmove(reader->buffer, reader->data, left);
2324     reader->end -= reader->data - reader->buffer;
2325     reader->data = reader->buffer;
2326 
2327     // read at least the necessary number of bytes, accepting up to the
2328     // buffer size
2329     size_t read = mpack_fill_range(reader, reader->buffer + left,
2330             count - left, reader->size - left);
2331     if (mpack_reader_error(reader) != mpack_ok)
2332         return false;
2333     reader->end += read;
2334     return true;
2335 }
2336 
2337 // Reads count bytes into p. Used when there are not enough bytes
2338 // left in the buffer to satisfy a read.
mpack_read_native_straddle(mpack_reader_t * reader,char * p,size_t count)2339 MPACK_NOINLINE void mpack_read_native_straddle(mpack_reader_t* reader, char* p, size_t count) {
2340     mpack_assert(count == 0 || p != NULL, "data pointer for %i bytes is NULL", (int)count);
2341 
2342     if (mpack_reader_error(reader) != mpack_ok) {
2343         mpack_memset(p, 0, count);
2344         return;
2345     }
2346 
2347     size_t left = (size_t)(reader->end - reader->data);
2348     mpack_log("big read for %i bytes into %p, %i left in buffer, buffer size %i\n",
2349             (int)count, p, (int)left, (int)reader->size);
2350 
2351     if (count <= left) {
2352         mpack_assert(0,
2353                 "big read requested for %i bytes, but there are %i bytes "
2354                 "left in buffer. call mpack_read_native() instead",
2355                 (int)count, (int)left);
2356         mpack_reader_flag_error(reader, mpack_error_bug);
2357         mpack_memset(p, 0, count);
2358         return;
2359     }
2360 
2361     // we'll need a fill function to get more data. if there's no
2362     // fill function, the buffer should contain an entire MessagePack
2363     // object, so we raise mpack_error_invalid instead of mpack_error_io
2364     // on truncated data.
2365     if (reader->fill == NULL) {
2366         mpack_reader_flag_error(reader, mpack_error_invalid);
2367         mpack_memset(p, 0, count);
2368         return;
2369     }
2370 
2371     if (reader->size == 0) {
2372         // somewhat debatable what error should be returned here. when
2373         // initializing a reader with an in-memory buffer it's not
2374         // necessarily a bug if the data is blank; it might just have
2375         // been truncated to zero. for this reason we return the same
2376         // error as if the data was truncated.
2377         mpack_reader_flag_error(reader, mpack_error_io);
2378         mpack_memset(p, 0, count);
2379         return;
2380     }
2381 
2382     // flush what's left of the buffer
2383     if (left > 0) {
2384         mpack_log("flushing %i bytes remaining in buffer\n", (int)left);
2385         mpack_memcpy(p, reader->data, left);
2386         count -= left;
2387         p += left;
2388         reader->data += left;
2389     }
2390 
2391     // if the remaining data needed is some small fraction of the
2392     // buffer size, we'll try to fill the buffer as much as possible
2393     // and copy the needed data out.
2394     if (count <= reader->size / MPACK_READER_SMALL_FRACTION_DENOMINATOR) {
2395         size_t read = mpack_fill_range(reader, reader->buffer, count, reader->size);
2396         if (mpack_reader_error(reader) != mpack_ok)
2397             return;
2398         mpack_memcpy(p, reader->buffer, count);
2399         reader->data = reader->buffer + count;
2400         reader->end = reader->buffer + read;
2401 
2402     // otherwise we read the remaining data directly into the target.
2403     } else {
2404         mpack_log("reading %i additional bytes\n", (int)count);
2405         mpack_fill_range(reader, p, count, count);
2406     }
2407 }
2408 
mpack_skip_bytes_straddle(mpack_reader_t * reader,size_t count)2409 MPACK_NOINLINE static void mpack_skip_bytes_straddle(mpack_reader_t* reader, size_t count) {
2410 
2411     // we'll need at least a fill function to skip more data. if there's
2412     // no fill function, the buffer should contain an entire MessagePack
2413     // object, so we raise mpack_error_invalid instead of mpack_error_io
2414     // on truncated data. (see mpack_read_native_straddle())
2415     if (reader->fill == NULL) {
2416         mpack_log("reader has no fill function!\n");
2417         mpack_reader_flag_error(reader, mpack_error_invalid);
2418         return;
2419     }
2420 
2421     // discard whatever's left in the buffer
2422     size_t left = (size_t)(reader->end - reader->data);
2423     mpack_log("discarding %i bytes still in buffer\n", (int)left);
2424     count -= left;
2425     reader->data = reader->end;
2426 
2427     // use the skip function if we've got one, and if we're trying
2428     // to skip a lot of data. if we only need to skip some tiny
2429     // fraction of the buffer size, it's probably better to just
2430     // fill the buffer and skip from it instead of trying to seek.
2431     if (reader->skip && count > reader->size / 16) {
2432         mpack_log("calling skip function for %i bytes\n", (int)count);
2433         reader->skip(reader, count);
2434         return;
2435     }
2436 
2437     mpack_reader_skip_using_fill(reader, count);
2438 }
2439 
mpack_skip_bytes(mpack_reader_t * reader,size_t count)2440 void mpack_skip_bytes(mpack_reader_t* reader, size_t count) {
2441     if (mpack_reader_error(reader) != mpack_ok)
2442         return;
2443     mpack_log("skip requested for %i bytes\n", (int)count);
2444     mpack_reader_track_bytes(reader, count);
2445 
2446     // check if we have enough in the buffer already
2447     size_t left = (size_t)(reader->end - reader->data);
2448     if (left >= count) {
2449         mpack_log("skipping %i bytes still in buffer\n", (int)count);
2450         reader->data += count;
2451         return;
2452     }
2453 
2454     mpack_skip_bytes_straddle(reader, count);
2455 }
2456 
mpack_reader_skip_using_fill(mpack_reader_t * reader,size_t count)2457 MPACK_NOINLINE static void mpack_reader_skip_using_fill(mpack_reader_t* reader, size_t count) {
2458     mpack_assert(reader->fill != NULL, "missing fill function!");
2459     mpack_assert(reader->data == reader->end, "there are bytes left in the buffer!");
2460     mpack_assert(reader->error == mpack_ok, "should not have called this in an error state (%i)", reader->error);
2461     mpack_log("skip using fill for %i bytes\n", (int)count);
2462 
2463     // fill and discard multiples of the buffer size
2464     while (count > reader->size) {
2465         mpack_log("filling and discarding buffer of %i bytes\n", (int)reader->size);
2466         if (mpack_fill_range(reader, reader->buffer, reader->size, reader->size) < reader->size) {
2467             mpack_reader_flag_error(reader, mpack_error_io);
2468             return;
2469         }
2470         count -= reader->size;
2471     }
2472 
2473     // fill the buffer as much as possible
2474     reader->data = reader->buffer;
2475     size_t read = mpack_fill_range(reader, reader->buffer, count, reader->size);
2476     if (read < count) {
2477         mpack_reader_flag_error(reader, mpack_error_io);
2478         return;
2479     }
2480     reader->end = reader->data + read;
2481     mpack_log("filled %i bytes into buffer; discarding %i bytes\n", (int)read, (int)count);
2482     reader->data += count;
2483 }
2484 
mpack_read_bytes(mpack_reader_t * reader,char * p,size_t count)2485 void mpack_read_bytes(mpack_reader_t* reader, char* p, size_t count) {
2486     mpack_assert(p != NULL, "destination for read of %i bytes is NULL", (int)count);
2487     mpack_reader_track_bytes(reader, count);
2488     mpack_read_native(reader, p, count);
2489 }
2490 
mpack_read_utf8(mpack_reader_t * reader,char * p,size_t byte_count)2491 void mpack_read_utf8(mpack_reader_t* reader, char* p, size_t byte_count) {
2492     mpack_assert(p != NULL, "destination for read of %i bytes is NULL", (int)byte_count);
2493     mpack_reader_track_str_bytes_all(reader, byte_count);
2494     mpack_read_native(reader, p, byte_count);
2495 
2496     if (mpack_reader_error(reader) == mpack_ok && !mpack_utf8_check(p, byte_count))
2497         mpack_reader_flag_error(reader, mpack_error_type);
2498 }
2499 
mpack_read_cstr_unchecked(mpack_reader_t * reader,char * buf,size_t buffer_size,size_t byte_count)2500 static void mpack_read_cstr_unchecked(mpack_reader_t* reader, char* buf, size_t buffer_size, size_t byte_count) {
2501     mpack_assert(buf != NULL, "destination for read of %i bytes is NULL", (int)byte_count);
2502     mpack_assert(buffer_size >= 1, "buffer size is zero; you must have room for at least a null-terminator");
2503 
2504     if (mpack_reader_error(reader)) {
2505         buf[0] = 0;
2506         return;
2507     }
2508 
2509     if (byte_count > buffer_size - 1) {
2510         mpack_reader_flag_error(reader, mpack_error_too_big);
2511         buf[0] = 0;
2512         return;
2513     }
2514 
2515     mpack_reader_track_str_bytes_all(reader, byte_count);
2516     mpack_read_native(reader, buf, byte_count);
2517     buf[byte_count] = 0;
2518 }
2519 
mpack_read_cstr(mpack_reader_t * reader,char * buf,size_t buffer_size,size_t byte_count)2520 void mpack_read_cstr(mpack_reader_t* reader, char* buf, size_t buffer_size, size_t byte_count) {
2521     mpack_read_cstr_unchecked(reader, buf, buffer_size, byte_count);
2522 
2523     // check for null bytes
2524     if (mpack_reader_error(reader) == mpack_ok && !mpack_str_check_no_null(buf, byte_count)) {
2525         buf[0] = 0;
2526         mpack_reader_flag_error(reader, mpack_error_type);
2527     }
2528 }
2529 
mpack_read_utf8_cstr(mpack_reader_t * reader,char * buf,size_t buffer_size,size_t byte_count)2530 void mpack_read_utf8_cstr(mpack_reader_t* reader, char* buf, size_t buffer_size, size_t byte_count) {
2531     mpack_read_cstr_unchecked(reader, buf, buffer_size, byte_count);
2532 
2533     // check encoding
2534     if (mpack_reader_error(reader) == mpack_ok && !mpack_utf8_check_no_null(buf, byte_count)) {
2535         buf[0] = 0;
2536         mpack_reader_flag_error(reader, mpack_error_type);
2537     }
2538 }
2539 
2540 #ifdef MPACK_MALLOC
2541 // Reads native bytes with error callback disabled. This allows MPack reader functions
2542 // to hold an allocated buffer and read native data into it without leaking it in
2543 // case of a non-local jump (longjmp, throw) out of an error handler.
mpack_read_native_noerrorfn(mpack_reader_t * reader,char * p,size_t count)2544 static void mpack_read_native_noerrorfn(mpack_reader_t* reader, char* p, size_t count) {
2545     mpack_assert(reader->error == mpack_ok, "cannot call if an error is already flagged!");
2546     mpack_reader_error_t error_fn = reader->error_fn;
2547     reader->error_fn = NULL;
2548     mpack_read_native(reader, p, count);
2549     reader->error_fn = error_fn;
2550 }
2551 
mpack_read_bytes_alloc_impl(mpack_reader_t * reader,size_t count,bool null_terminated)2552 char* mpack_read_bytes_alloc_impl(mpack_reader_t* reader, size_t count, bool null_terminated) {
2553 
2554     // track the bytes first in case it jumps
2555     mpack_reader_track_bytes(reader, count);
2556     if (mpack_reader_error(reader) != mpack_ok)
2557         return NULL;
2558 
2559     // cannot allocate zero bytes. this is not an error.
2560     if (count == 0 && null_terminated == false)
2561         return NULL;
2562 
2563     // allocate data
2564     char* data = (char*)MPACK_MALLOC(count + (null_terminated ? 1 : 0)); // TODO: can this overflow?
2565     if (data == NULL) {
2566         mpack_reader_flag_error(reader, mpack_error_memory);
2567         return NULL;
2568     }
2569 
2570     // read with error callback disabled so we don't leak our buffer
2571     mpack_read_native_noerrorfn(reader, data, count);
2572 
2573     // report flagged errors
2574     if (mpack_reader_error(reader) != mpack_ok) {
2575         MPACK_FREE(data);
2576         if (reader->error_fn)
2577             reader->error_fn(reader, mpack_reader_error(reader));
2578         return NULL;
2579     }
2580 
2581     if (null_terminated)
2582         data[count] = '\0';
2583     return data;
2584 }
2585 #endif
2586 
2587 // read inplace without tracking (since there are different
2588 // tracking modes for different inplace readers)
mpack_read_bytes_inplace_notrack(mpack_reader_t * reader,size_t count)2589 static const char* mpack_read_bytes_inplace_notrack(mpack_reader_t* reader, size_t count) {
2590     if (mpack_reader_error(reader) != mpack_ok)
2591         return NULL;
2592 
2593     // if we have enough bytes already in the buffer, we can return it directly.
2594     if ((size_t)(reader->end - reader->data) >= count) {
2595         const char* bytes = reader->data;
2596         reader->data += count;
2597         return bytes;
2598     }
2599 
2600     if (!mpack_reader_ensure(reader, count))
2601         return NULL;
2602 
2603     const char* bytes = reader->data;
2604     reader->data += count;
2605     return bytes;
2606 }
2607 
mpack_read_bytes_inplace(mpack_reader_t * reader,size_t count)2608 const char* mpack_read_bytes_inplace(mpack_reader_t* reader, size_t count) {
2609     mpack_reader_track_bytes(reader, count);
2610     return mpack_read_bytes_inplace_notrack(reader, count);
2611 }
2612 
mpack_read_utf8_inplace(mpack_reader_t * reader,size_t count)2613 const char* mpack_read_utf8_inplace(mpack_reader_t* reader, size_t count) {
2614     mpack_reader_track_str_bytes_all(reader, count);
2615     const char* str = mpack_read_bytes_inplace_notrack(reader, count);
2616 
2617     if (mpack_reader_error(reader) == mpack_ok && !mpack_utf8_check(str, count)) {
2618         mpack_reader_flag_error(reader, mpack_error_type);
2619         return NULL;
2620     }
2621 
2622     return str;
2623 }
2624 
mpack_parse_tag(mpack_reader_t * reader,mpack_tag_t * tag)2625 static size_t mpack_parse_tag(mpack_reader_t* reader, mpack_tag_t* tag) {
2626     mpack_assert(reader->error == mpack_ok, "reader cannot be in an error state!");
2627 
2628     if (!mpack_reader_ensure(reader, 1))
2629         return 0;
2630     uint8_t type = mpack_load_u8(reader->data);
2631 
2632     // unfortunately, by far the fastest way to parse a tag is to switch
2633     // on the first byte, and to explicitly list every possible byte. so for
2634     // infix types, the list of cases is quite large.
2635     //
2636     // in size-optimized builds, we switch on the top four bits first to
2637     // handle most infix types with a smaller jump table to save space.
2638 
2639     #if MPACK_OPTIMIZE_FOR_SIZE
2640     switch (type >> 4) {
2641 
2642         // positive fixnum
2643         case 0x0: case 0x1: case 0x2: case 0x3:
2644         case 0x4: case 0x5: case 0x6: case 0x7:
2645             *tag = mpack_tag_make_uint(type);
2646             return 1;
2647 
2648         // negative fixnum
2649         case 0xe: case 0xf:
2650             *tag = mpack_tag_make_int((int8_t)type);
2651             return 1;
2652 
2653         // fixmap
2654         case 0x8:
2655             *tag = mpack_tag_make_map(type & ~0xf0u);
2656             return 1;
2657 
2658         // fixarray
2659         case 0x9:
2660             *tag = mpack_tag_make_array(type & ~0xf0u);
2661             return 1;
2662 
2663         // fixstr
2664         case 0xa: case 0xb:
2665             *tag = mpack_tag_make_str(type & ~0xe0u);
2666             return 1;
2667 
2668         // not one of the common infix types
2669         default:
2670             break;
2671 
2672     }
2673     #endif
2674 
2675     // handle individual type tags
2676     switch (type) {
2677 
2678         #if !MPACK_OPTIMIZE_FOR_SIZE
2679         // positive fixnum
2680         case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
2681         case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
2682         case 0x10: case 0x11: case 0x12: case 0x13: case 0x14: case 0x15: case 0x16: case 0x17:
2683         case 0x18: case 0x19: case 0x1a: case 0x1b: case 0x1c: case 0x1d: case 0x1e: case 0x1f:
2684         case 0x20: case 0x21: case 0x22: case 0x23: case 0x24: case 0x25: case 0x26: case 0x27:
2685         case 0x28: case 0x29: case 0x2a: case 0x2b: case 0x2c: case 0x2d: case 0x2e: case 0x2f:
2686         case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: case 0x35: case 0x36: case 0x37:
2687         case 0x38: case 0x39: case 0x3a: case 0x3b: case 0x3c: case 0x3d: case 0x3e: case 0x3f:
2688         case 0x40: case 0x41: case 0x42: case 0x43: case 0x44: case 0x45: case 0x46: case 0x47:
2689         case 0x48: case 0x49: case 0x4a: case 0x4b: case 0x4c: case 0x4d: case 0x4e: case 0x4f:
2690         case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57:
2691         case 0x58: case 0x59: case 0x5a: case 0x5b: case 0x5c: case 0x5d: case 0x5e: case 0x5f:
2692         case 0x60: case 0x61: case 0x62: case 0x63: case 0x64: case 0x65: case 0x66: case 0x67:
2693         case 0x68: case 0x69: case 0x6a: case 0x6b: case 0x6c: case 0x6d: case 0x6e: case 0x6f:
2694         case 0x70: case 0x71: case 0x72: case 0x73: case 0x74: case 0x75: case 0x76: case 0x77:
2695         case 0x78: case 0x79: case 0x7a: case 0x7b: case 0x7c: case 0x7d: case 0x7e: case 0x7f:
2696             *tag = mpack_tag_make_uint(type);
2697             return 1;
2698 
2699         // negative fixnum
2700         case 0xe0: case 0xe1: case 0xe2: case 0xe3: case 0xe4: case 0xe5: case 0xe6: case 0xe7:
2701         case 0xe8: case 0xe9: case 0xea: case 0xeb: case 0xec: case 0xed: case 0xee: case 0xef:
2702         case 0xf0: case 0xf1: case 0xf2: case 0xf3: case 0xf4: case 0xf5: case 0xf6: case 0xf7:
2703         case 0xf8: case 0xf9: case 0xfa: case 0xfb: case 0xfc: case 0xfd: case 0xfe: case 0xff:
2704             *tag = mpack_tag_make_int((int8_t)type);
2705             return 1;
2706 
2707         // fixmap
2708         case 0x80: case 0x81: case 0x82: case 0x83: case 0x84: case 0x85: case 0x86: case 0x87:
2709         case 0x88: case 0x89: case 0x8a: case 0x8b: case 0x8c: case 0x8d: case 0x8e: case 0x8f:
2710             *tag = mpack_tag_make_map(type & ~0xf0u);
2711             return 1;
2712 
2713         // fixarray
2714         case 0x90: case 0x91: case 0x92: case 0x93: case 0x94: case 0x95: case 0x96: case 0x97:
2715         case 0x98: case 0x99: case 0x9a: case 0x9b: case 0x9c: case 0x9d: case 0x9e: case 0x9f:
2716             *tag = mpack_tag_make_array(type & ~0xf0u);
2717             return 1;
2718 
2719         // fixstr
2720         case 0xa0: case 0xa1: case 0xa2: case 0xa3: case 0xa4: case 0xa5: case 0xa6: case 0xa7:
2721         case 0xa8: case 0xa9: case 0xaa: case 0xab: case 0xac: case 0xad: case 0xae: case 0xaf:
2722         case 0xb0: case 0xb1: case 0xb2: case 0xb3: case 0xb4: case 0xb5: case 0xb6: case 0xb7:
2723         case 0xb8: case 0xb9: case 0xba: case 0xbb: case 0xbc: case 0xbd: case 0xbe: case 0xbf:
2724             *tag = mpack_tag_make_str(type & ~0xe0u);
2725             return 1;
2726         #endif
2727 
2728         // nil
2729         case 0xc0:
2730             *tag = mpack_tag_make_nil();
2731             return 1;
2732 
2733         // bool
2734         case 0xc2: case 0xc3:
2735             *tag = mpack_tag_make_bool((bool)(type & 1));
2736             return 1;
2737 
2738         // bin8
2739         case 0xc4:
2740             if (!mpack_reader_ensure(reader, MPACK_TAG_SIZE_BIN8))
2741                 return 0;
2742             *tag = mpack_tag_make_bin(mpack_load_u8(reader->data + 1));
2743             return MPACK_TAG_SIZE_BIN8;
2744 
2745         // bin16
2746         case 0xc5:
2747             if (!mpack_reader_ensure(reader, MPACK_TAG_SIZE_BIN16))
2748                 return 0;
2749             *tag = mpack_tag_make_bin(mpack_load_u16(reader->data + 1));
2750             return MPACK_TAG_SIZE_BIN16;
2751 
2752         // bin32
2753         case 0xc6:
2754             if (!mpack_reader_ensure(reader, MPACK_TAG_SIZE_BIN32))
2755                 return 0;
2756             *tag = mpack_tag_make_bin(mpack_load_u32(reader->data + 1));
2757             return MPACK_TAG_SIZE_BIN32;
2758 
2759         #if MPACK_EXTENSIONS
2760         // ext8
2761         case 0xc7:
2762             if (!mpack_reader_ensure(reader, MPACK_TAG_SIZE_EXT8))
2763                 return 0;
2764             *tag = mpack_tag_make_ext(mpack_load_i8(reader->data + 2), mpack_load_u8(reader->data + 1));
2765             return MPACK_TAG_SIZE_EXT8;
2766 
2767         // ext16
2768         case 0xc8:
2769             if (!mpack_reader_ensure(reader, MPACK_TAG_SIZE_EXT16))
2770                 return 0;
2771             *tag = mpack_tag_make_ext(mpack_load_i8(reader->data + 3), mpack_load_u16(reader->data + 1));
2772             return MPACK_TAG_SIZE_EXT16;
2773 
2774         // ext32
2775         case 0xc9:
2776             if (!mpack_reader_ensure(reader, MPACK_TAG_SIZE_EXT32))
2777                 return 0;
2778             *tag = mpack_tag_make_ext(mpack_load_i8(reader->data + 5), mpack_load_u32(reader->data + 1));
2779             return MPACK_TAG_SIZE_EXT32;
2780         #endif
2781 
2782         // float
2783         case 0xca:
2784             if (!mpack_reader_ensure(reader, MPACK_TAG_SIZE_FLOAT))
2785                 return 0;
2786             *tag = mpack_tag_make_float(mpack_load_float(reader->data + 1));
2787             return MPACK_TAG_SIZE_FLOAT;
2788 
2789         // double
2790         case 0xcb:
2791             if (!mpack_reader_ensure(reader, MPACK_TAG_SIZE_DOUBLE))
2792                 return 0;
2793             *tag = mpack_tag_make_double(mpack_load_double(reader->data + 1));
2794             return MPACK_TAG_SIZE_DOUBLE;
2795 
2796         // uint8
2797         case 0xcc:
2798             if (!mpack_reader_ensure(reader, MPACK_TAG_SIZE_U8))
2799                 return 0;
2800             *tag = mpack_tag_make_uint(mpack_load_u8(reader->data + 1));
2801             return MPACK_TAG_SIZE_U8;
2802 
2803         // uint16
2804         case 0xcd:
2805             if (!mpack_reader_ensure(reader, MPACK_TAG_SIZE_U16))
2806                 return 0;
2807             *tag = mpack_tag_make_uint(mpack_load_u16(reader->data + 1));
2808             return MPACK_TAG_SIZE_U16;
2809 
2810         // uint32
2811         case 0xce:
2812             if (!mpack_reader_ensure(reader, MPACK_TAG_SIZE_U32))
2813                 return 0;
2814             *tag = mpack_tag_make_uint(mpack_load_u32(reader->data + 1));
2815             return MPACK_TAG_SIZE_U32;
2816 
2817         // uint64
2818         case 0xcf:
2819             if (!mpack_reader_ensure(reader, MPACK_TAG_SIZE_U64))
2820                 return 0;
2821             *tag = mpack_tag_make_uint(mpack_load_u64(reader->data + 1));
2822             return MPACK_TAG_SIZE_U64;
2823 
2824         // int8
2825         case 0xd0:
2826             if (!mpack_reader_ensure(reader, MPACK_TAG_SIZE_I8))
2827                 return 0;
2828             *tag = mpack_tag_make_int(mpack_load_i8(reader->data + 1));
2829             return MPACK_TAG_SIZE_I8;
2830 
2831         // int16
2832         case 0xd1:
2833             if (!mpack_reader_ensure(reader, MPACK_TAG_SIZE_I16))
2834                 return 0;
2835             *tag = mpack_tag_make_int(mpack_load_i16(reader->data + 1));
2836             return MPACK_TAG_SIZE_I16;
2837 
2838         // int32
2839         case 0xd2:
2840             if (!mpack_reader_ensure(reader, MPACK_TAG_SIZE_I32))
2841                 return 0;
2842             *tag = mpack_tag_make_int(mpack_load_i32(reader->data + 1));
2843             return MPACK_TAG_SIZE_I32;
2844 
2845         // int64
2846         case 0xd3:
2847             if (!mpack_reader_ensure(reader, MPACK_TAG_SIZE_I64))
2848                 return 0;
2849             *tag = mpack_tag_make_int(mpack_load_i64(reader->data + 1));
2850             return MPACK_TAG_SIZE_I64;
2851 
2852         #if MPACK_EXTENSIONS
2853         // fixext1
2854         case 0xd4:
2855             if (!mpack_reader_ensure(reader, MPACK_TAG_SIZE_FIXEXT1))
2856                 return 0;
2857             *tag = mpack_tag_make_ext(mpack_load_i8(reader->data + 1), 1);
2858             return MPACK_TAG_SIZE_FIXEXT1;
2859 
2860         // fixext2
2861         case 0xd5:
2862             if (!mpack_reader_ensure(reader, MPACK_TAG_SIZE_FIXEXT2))
2863                 return 0;
2864             *tag = mpack_tag_make_ext(mpack_load_i8(reader->data + 1), 2);
2865             return MPACK_TAG_SIZE_FIXEXT2;
2866 
2867         // fixext4
2868         case 0xd6:
2869             if (!mpack_reader_ensure(reader, MPACK_TAG_SIZE_FIXEXT4))
2870                 return 0;
2871             *tag = mpack_tag_make_ext(mpack_load_i8(reader->data + 1), 4);
2872             return 2;
2873 
2874         // fixext8
2875         case 0xd7:
2876             if (!mpack_reader_ensure(reader, MPACK_TAG_SIZE_FIXEXT8))
2877                 return 0;
2878             *tag = mpack_tag_make_ext(mpack_load_i8(reader->data + 1), 8);
2879             return MPACK_TAG_SIZE_FIXEXT8;
2880 
2881         // fixext16
2882         case 0xd8:
2883             if (!mpack_reader_ensure(reader, MPACK_TAG_SIZE_FIXEXT16))
2884                 return 0;
2885             *tag = mpack_tag_make_ext(mpack_load_i8(reader->data + 1), 16);
2886             return MPACK_TAG_SIZE_FIXEXT16;
2887         #endif
2888 
2889         // str8
2890         case 0xd9:
2891             if (!mpack_reader_ensure(reader, MPACK_TAG_SIZE_STR8))
2892                 return 0;
2893             *tag = mpack_tag_make_str(mpack_load_u8(reader->data + 1));
2894             return MPACK_TAG_SIZE_STR8;
2895 
2896         // str16
2897         case 0xda:
2898             if (!mpack_reader_ensure(reader, MPACK_TAG_SIZE_STR16))
2899                 return 0;
2900             *tag = mpack_tag_make_str(mpack_load_u16(reader->data + 1));
2901             return MPACK_TAG_SIZE_STR16;
2902 
2903         // str32
2904         case 0xdb:
2905             if (!mpack_reader_ensure(reader, MPACK_TAG_SIZE_STR32))
2906                 return 0;
2907             *tag = mpack_tag_make_str(mpack_load_u32(reader->data + 1));
2908             return MPACK_TAG_SIZE_STR32;
2909 
2910         // array16
2911         case 0xdc:
2912             if (!mpack_reader_ensure(reader, MPACK_TAG_SIZE_ARRAY16))
2913                 return 0;
2914             *tag = mpack_tag_make_array(mpack_load_u16(reader->data + 1));
2915             return MPACK_TAG_SIZE_ARRAY16;
2916 
2917         // array32
2918         case 0xdd:
2919             if (!mpack_reader_ensure(reader, MPACK_TAG_SIZE_ARRAY32))
2920                 return 0;
2921             *tag = mpack_tag_make_array(mpack_load_u32(reader->data + 1));
2922             return MPACK_TAG_SIZE_ARRAY32;
2923 
2924         // map16
2925         case 0xde:
2926             if (!mpack_reader_ensure(reader, MPACK_TAG_SIZE_MAP16))
2927                 return 0;
2928             *tag = mpack_tag_make_map(mpack_load_u16(reader->data + 1));
2929             return MPACK_TAG_SIZE_MAP16;
2930 
2931         // map32
2932         case 0xdf:
2933             if (!mpack_reader_ensure(reader, MPACK_TAG_SIZE_MAP32))
2934                 return 0;
2935             *tag = mpack_tag_make_map(mpack_load_u32(reader->data + 1));
2936             return MPACK_TAG_SIZE_MAP32;
2937 
2938         // reserved
2939         case 0xc1:
2940             mpack_reader_flag_error(reader, mpack_error_invalid);
2941             return 0;
2942 
2943         #if !MPACK_EXTENSIONS
2944         // ext
2945         case 0xc7: // fallthrough
2946         case 0xc8: // fallthrough
2947         case 0xc9: // fallthrough
2948         // fixext
2949         case 0xd4: // fallthrough
2950         case 0xd5: // fallthrough
2951         case 0xd6: // fallthrough
2952         case 0xd7: // fallthrough
2953         case 0xd8:
2954             mpack_reader_flag_error(reader, mpack_error_unsupported);
2955             return 0;
2956         #endif
2957 
2958         #if MPACK_OPTIMIZE_FOR_SIZE
2959         // any other bytes should have been handled by the infix switch
2960         default:
2961             break;
2962         #endif
2963     }
2964 
2965     mpack_assert(0, "unreachable");
2966     return 0;
2967 }
2968 
mpack_read_tag(mpack_reader_t * reader)2969 mpack_tag_t mpack_read_tag(mpack_reader_t* reader) {
2970     mpack_log("reading tag\n");
2971 
2972     // make sure we can read a tag
2973     if (mpack_reader_error(reader) != mpack_ok)
2974         return mpack_tag_nil();
2975     if (mpack_reader_track_element(reader) != mpack_ok)
2976         return mpack_tag_nil();
2977 
2978     mpack_tag_t tag = MPACK_TAG_ZERO;
2979     size_t count = mpack_parse_tag(reader, &tag);
2980     if (count == 0)
2981         return mpack_tag_nil();
2982 
2983     #if MPACK_READ_TRACKING
2984     mpack_error_t track_error = mpack_ok;
2985 
2986     switch (tag.type) {
2987         case mpack_type_map:
2988         case mpack_type_array:
2989             track_error = mpack_track_push(&reader->track, tag.type, tag.v.n);
2990             break;
2991         #if MPACK_EXTENSIONS
2992         case mpack_type_ext:
2993         #endif
2994         case mpack_type_str:
2995         case mpack_type_bin:
2996             track_error = mpack_track_push(&reader->track, tag.type, tag.v.l);
2997             break;
2998         default:
2999             break;
3000     }
3001 
3002     if (track_error != mpack_ok) {
3003         mpack_reader_flag_error(reader, track_error);
3004         return mpack_tag_nil();
3005     }
3006     #endif
3007 
3008     reader->data += count;
3009     return tag;
3010 }
3011 
mpack_peek_tag(mpack_reader_t * reader)3012 mpack_tag_t mpack_peek_tag(mpack_reader_t* reader) {
3013     mpack_log("peeking tag\n");
3014 
3015     // make sure we can peek a tag
3016     if (mpack_reader_error(reader) != mpack_ok)
3017         return mpack_tag_nil();
3018     if (mpack_reader_track_peek_element(reader) != mpack_ok)
3019         return mpack_tag_nil();
3020 
3021     mpack_tag_t tag = MPACK_TAG_ZERO;
3022     if (mpack_parse_tag(reader, &tag) == 0)
3023         return mpack_tag_nil();
3024     return tag;
3025 }
3026 
mpack_discard(mpack_reader_t * reader)3027 void mpack_discard(mpack_reader_t* reader) {
3028     mpack_tag_t var = mpack_read_tag(reader);
3029     if (mpack_reader_error(reader))
3030         return;
3031     switch (var.type) {
3032         case mpack_type_str:
3033             mpack_skip_bytes(reader, var.v.l);
3034             mpack_done_str(reader);
3035             break;
3036         case mpack_type_bin:
3037             mpack_skip_bytes(reader, var.v.l);
3038             mpack_done_bin(reader);
3039             break;
3040         #if MPACK_EXTENSIONS
3041         case mpack_type_ext:
3042             mpack_skip_bytes(reader, var.v.l);
3043             mpack_done_ext(reader);
3044             break;
3045         #endif
3046         case mpack_type_array: {
3047             for (; var.v.n > 0; --var.v.n) {
3048                 mpack_discard(reader);
3049                 if (mpack_reader_error(reader))
3050                     break;
3051             }
3052             mpack_done_array(reader);
3053             break;
3054         }
3055         case mpack_type_map: {
3056             for (; var.v.n > 0; --var.v.n) {
3057                 mpack_discard(reader);
3058                 mpack_discard(reader);
3059                 if (mpack_reader_error(reader))
3060                     break;
3061             }
3062             mpack_done_map(reader);
3063             break;
3064         }
3065         default:
3066             break;
3067     }
3068 }
3069 
3070 #if MPACK_EXTENSIONS
mpack_read_timestamp(mpack_reader_t * reader,size_t size)3071 mpack_timestamp_t mpack_read_timestamp(mpack_reader_t* reader, size_t size) {
3072     mpack_timestamp_t timestamp = {0, 0};
3073 
3074     if (size != 4 && size != 8 && size != 12) {
3075         mpack_reader_flag_error(reader, mpack_error_invalid);
3076         return timestamp;
3077     }
3078 
3079     char buf[12];
3080     mpack_read_bytes(reader, buf, size);
3081     mpack_done_ext(reader);
3082     if (mpack_reader_error(reader) != mpack_ok)
3083         return timestamp;
3084 
3085     switch (size) {
3086         case 4:
3087             timestamp.seconds = (int64_t)(uint64_t)mpack_load_u32(buf);
3088             break;
3089 
3090         case 8: {
3091             uint64_t packed = mpack_load_u64(buf);
3092             timestamp.seconds = (int64_t)(packed & ((UINT64_C(1) << 34) - 1));
3093             timestamp.nanoseconds = (uint32_t)(packed >> 34);
3094             break;
3095         }
3096 
3097         case 12:
3098             timestamp.nanoseconds = mpack_load_u32(buf);
3099             timestamp.seconds = mpack_load_i64(buf + 4);
3100             break;
3101 
3102         default:
3103             mpack_assert(false, "unreachable");
3104             break;
3105     }
3106 
3107     if (timestamp.nanoseconds > MPACK_TIMESTAMP_NANOSECONDS_MAX) {
3108         mpack_reader_flag_error(reader, mpack_error_invalid);
3109         mpack_timestamp_t zero = {0, 0};
3110         return zero;
3111     }
3112 
3113     return timestamp;
3114 }
3115 #endif
3116 
3117 #if MPACK_READ_TRACKING
mpack_done_type(mpack_reader_t * reader,mpack_type_t type)3118 void mpack_done_type(mpack_reader_t* reader, mpack_type_t type) {
3119     if (mpack_reader_error(reader) == mpack_ok)
3120         mpack_reader_flag_if_error(reader, mpack_track_pop(&reader->track, type));
3121 }
3122 #endif
3123 
3124 #if MPACK_DEBUG && MPACK_STDIO
mpack_print_read_prefix(mpack_reader_t * reader,size_t length,char * buffer,size_t buffer_size)3125 static size_t mpack_print_read_prefix(mpack_reader_t* reader, size_t length, char* buffer, size_t buffer_size) {
3126     if (length == 0)
3127         return 0;
3128 
3129     size_t read = (length < buffer_size) ? length : buffer_size;
3130     mpack_read_bytes(reader, buffer, read);
3131     if (mpack_reader_error(reader) != mpack_ok)
3132         return 0;
3133 
3134     mpack_skip_bytes(reader, length - read);
3135     return read;
3136 }
3137 
mpack_print_element(mpack_reader_t * reader,mpack_print_t * print,size_t depth)3138 static void mpack_print_element(mpack_reader_t* reader, mpack_print_t* print, size_t depth) {
3139     mpack_tag_t val = mpack_read_tag(reader);
3140     if (mpack_reader_error(reader) != mpack_ok)
3141         return;
3142 
3143     // We read some bytes from bin and ext so we can print its prefix in hex.
3144     char buffer[MPACK_PRINT_BYTE_COUNT];
3145     size_t count = 0;
3146 
3147     switch (val.type) {
3148         case mpack_type_str:
3149             mpack_print_append_cstr(print, "\"");
3150             for (size_t i = 0; i < val.v.l; ++i) {
3151                 char c;
3152                 mpack_read_bytes(reader, &c, 1);
3153                 if (mpack_reader_error(reader) != mpack_ok)
3154                     return;
3155                 switch (c) {
3156                     case '\n': mpack_print_append_cstr(print, "\\n"); break;
3157                     case '\\': mpack_print_append_cstr(print, "\\\\"); break;
3158                     case '"': mpack_print_append_cstr(print, "\\\""); break;
3159                     default: mpack_print_append(print, &c, 1); break;
3160                 }
3161             }
3162             mpack_print_append_cstr(print, "\"");
3163             mpack_done_str(reader);
3164             return;
3165 
3166         case mpack_type_array:
3167             mpack_print_append_cstr(print, "[\n");
3168             for (size_t i = 0; i < val.v.n; ++i) {
3169                 for (size_t j = 0; j < depth + 1; ++j)
3170                     mpack_print_append_cstr(print, "    ");
3171                 mpack_print_element(reader, print, depth + 1);
3172                 if (mpack_reader_error(reader) != mpack_ok)
3173                     return;
3174                 if (i != val.v.n - 1)
3175                     mpack_print_append_cstr(print, ",");
3176                 mpack_print_append_cstr(print, "\n");
3177             }
3178             for (size_t i = 0; i < depth; ++i)
3179                 mpack_print_append_cstr(print, "    ");
3180             mpack_print_append_cstr(print, "]");
3181             mpack_done_array(reader);
3182             return;
3183 
3184         case mpack_type_map:
3185             mpack_print_append_cstr(print, "{\n");
3186             for (size_t i = 0; i < val.v.n; ++i) {
3187                 for (size_t j = 0; j < depth + 1; ++j)
3188                     mpack_print_append_cstr(print, "    ");
3189                 mpack_print_element(reader, print, depth + 1);
3190                 if (mpack_reader_error(reader) != mpack_ok)
3191                     return;
3192                 mpack_print_append_cstr(print, ": ");
3193                 mpack_print_element(reader, print, depth + 1);
3194                 if (mpack_reader_error(reader) != mpack_ok)
3195                     return;
3196                 if (i != val.v.n - 1)
3197                     mpack_print_append_cstr(print, ",");
3198                 mpack_print_append_cstr(print, "\n");
3199             }
3200             for (size_t i = 0; i < depth; ++i)
3201                 mpack_print_append_cstr(print, "    ");
3202             mpack_print_append_cstr(print, "}");
3203             mpack_done_map(reader);
3204             return;
3205 
3206         // The above cases return so as not to print a pseudo-json value. The
3207         // below cases break and print pseudo-json.
3208 
3209         case mpack_type_bin:
3210             count = mpack_print_read_prefix(reader, mpack_tag_bin_length(&val), buffer, sizeof(buffer));
3211             mpack_done_bin(reader);
3212             break;
3213 
3214         #if MPACK_EXTENSIONS
3215         case mpack_type_ext:
3216             count = mpack_print_read_prefix(reader, mpack_tag_ext_length(&val), buffer, sizeof(buffer));
3217             mpack_done_ext(reader);
3218             break;
3219         #endif
3220 
3221         default:
3222             break;
3223     }
3224 
3225     char buf[256];
3226     mpack_tag_debug_pseudo_json(val, buf, sizeof(buf), buffer, count);
3227     mpack_print_append_cstr(print, buf);
3228 }
3229 
mpack_print_and_destroy(mpack_reader_t * reader,mpack_print_t * print,size_t depth)3230 static void mpack_print_and_destroy(mpack_reader_t* reader, mpack_print_t* print, size_t depth) {
3231     for (size_t i = 0; i < depth; ++i)
3232         mpack_print_append_cstr(print, "    ");
3233     mpack_print_element(reader, print, depth);
3234 
3235     size_t remaining = mpack_reader_remaining(reader, NULL);
3236 
3237     char buf[256];
3238     if (mpack_reader_destroy(reader) != mpack_ok) {
3239         mpack_snprintf(buf, sizeof(buf), "\n<mpack parsing error %s>", mpack_error_to_string(mpack_reader_error(reader)));
3240         buf[sizeof(buf) - 1] = '\0';
3241         mpack_print_append_cstr(print, buf);
3242     } else if (remaining > 0) {
3243         mpack_snprintf(buf, sizeof(buf), "\n<%i extra bytes at end of message>", (int)remaining);
3244         buf[sizeof(buf) - 1] = '\0';
3245         mpack_print_append_cstr(print, buf);
3246     }
3247 }
3248 
mpack_print_data(const char * data,size_t len,mpack_print_t * print,size_t depth)3249 static void mpack_print_data(const char* data, size_t len, mpack_print_t* print, size_t depth) {
3250     mpack_reader_t reader;
3251     mpack_reader_init_data(&reader, data, len);
3252     mpack_print_and_destroy(&reader, print, depth);
3253 }
3254 
mpack_print_data_to_buffer(const char * data,size_t data_size,char * buffer,size_t buffer_size)3255 void mpack_print_data_to_buffer(const char* data, size_t data_size, char* buffer, size_t buffer_size) {
3256     if (buffer_size == 0) {
3257         mpack_assert(false, "buffer size is zero!");
3258         return;
3259     }
3260 
3261     mpack_print_t print;
3262     mpack_memset(&print, 0, sizeof(print));
3263     print.buffer = buffer;
3264     print.size = buffer_size;
3265     mpack_print_data(data, data_size, &print, 0);
3266     mpack_print_append(&print, "",  1); // null-terminator
3267     mpack_print_flush(&print);
3268 
3269     // we always make sure there's a null-terminator at the end of the buffer
3270     // in case we ran out of space.
3271     print.buffer[print.size - 1] = '\0';
3272 }
3273 
mpack_print_data_to_callback(const char * data,size_t size,mpack_print_callback_t callback,void * context)3274 void mpack_print_data_to_callback(const char* data, size_t size, mpack_print_callback_t callback, void* context) {
3275     char buffer[1024];
3276     mpack_print_t print;
3277     mpack_memset(&print, 0, sizeof(print));
3278     print.buffer = buffer;
3279     print.size = sizeof(buffer);
3280     print.callback = callback;
3281     print.context = context;
3282     mpack_print_data(data, size, &print, 0);
3283     mpack_print_flush(&print);
3284 }
3285 
mpack_print_data_to_file(const char * data,size_t len,FILE * file)3286 void mpack_print_data_to_file(const char* data, size_t len, FILE* file) {
3287     mpack_assert(data != NULL, "data is NULL");
3288     mpack_assert(file != NULL, "file is NULL");
3289 
3290     char buffer[1024];
3291     mpack_print_t print;
3292     mpack_memset(&print, 0, sizeof(print));
3293     print.buffer = buffer;
3294     print.size = sizeof(buffer);
3295     print.callback = &mpack_print_file_callback;
3296     print.context = file;
3297 
3298     mpack_print_data(data, len, &print, 2);
3299     mpack_print_append_cstr(&print, "\n");
3300     mpack_print_flush(&print);
3301 }
3302 
mpack_print_stdfile_to_callback(FILE * file,mpack_print_callback_t callback,void * context)3303 void mpack_print_stdfile_to_callback(FILE* file, mpack_print_callback_t callback, void* context) {
3304     char buffer[1024];
3305     mpack_print_t print;
3306     mpack_memset(&print, 0, sizeof(print));
3307     print.buffer = buffer;
3308     print.size = sizeof(buffer);
3309     print.callback = callback;
3310     print.context = context;
3311 
3312     mpack_reader_t reader;
3313     mpack_reader_init_stdfile(&reader, file, false);
3314     mpack_print_and_destroy(&reader, &print, 0);
3315     mpack_print_flush(&print);
3316 }
3317 #endif
3318 
3319 #endif
3320 
3321 /* mpack/mpack-expect.c.c */
3322 
3323 #define MPACK_INTERNAL 1
3324 
3325 /* #include "mpack-expect.h" */
3326 
3327 #if MPACK_EXPECT
3328 
3329 
3330 // Helpers
3331 
mpack_expect_native_u8(mpack_reader_t * reader)3332 MPACK_STATIC_INLINE uint8_t mpack_expect_native_u8(mpack_reader_t* reader) {
3333     if (mpack_reader_error(reader) != mpack_ok)
3334         return 0;
3335     uint8_t type;
3336     if (!mpack_reader_ensure(reader, sizeof(type)))
3337         return 0;
3338     type = mpack_load_u8(reader->data);
3339     reader->data += sizeof(type);
3340     return type;
3341 }
3342 
3343 #if !MPACK_OPTIMIZE_FOR_SIZE
mpack_expect_native_u16(mpack_reader_t * reader)3344 MPACK_STATIC_INLINE uint16_t mpack_expect_native_u16(mpack_reader_t* reader) {
3345     if (mpack_reader_error(reader) != mpack_ok)
3346         return 0;
3347     uint16_t type;
3348     if (!mpack_reader_ensure(reader, sizeof(type)))
3349         return 0;
3350     type = mpack_load_u16(reader->data);
3351     reader->data += sizeof(type);
3352     return type;
3353 }
3354 
mpack_expect_native_u32(mpack_reader_t * reader)3355 MPACK_STATIC_INLINE uint32_t mpack_expect_native_u32(mpack_reader_t* reader) {
3356     if (mpack_reader_error(reader) != mpack_ok)
3357         return 0;
3358     uint32_t type;
3359     if (!mpack_reader_ensure(reader, sizeof(type)))
3360         return 0;
3361     type = mpack_load_u32(reader->data);
3362     reader->data += sizeof(type);
3363     return type;
3364 }
3365 #endif
3366 
mpack_expect_type_byte(mpack_reader_t * reader)3367 MPACK_STATIC_INLINE uint8_t mpack_expect_type_byte(mpack_reader_t* reader) {
3368     mpack_reader_track_element(reader);
3369     return mpack_expect_native_u8(reader);
3370 }
3371 
3372 
3373 // Basic Number Functions
3374 
mpack_expect_u8(mpack_reader_t * reader)3375 uint8_t mpack_expect_u8(mpack_reader_t* reader) {
3376     mpack_tag_t var = mpack_read_tag(reader);
3377     if (var.type == mpack_type_uint) {
3378         if (var.v.u <= UINT8_MAX)
3379             return (uint8_t)var.v.u;
3380     } else if (var.type == mpack_type_int) {
3381         if (var.v.i >= 0 && var.v.i <= UINT8_MAX)
3382             return (uint8_t)var.v.i;
3383     }
3384     mpack_reader_flag_error(reader, mpack_error_type);
3385     return 0;
3386 }
3387 
mpack_expect_u16(mpack_reader_t * reader)3388 uint16_t mpack_expect_u16(mpack_reader_t* reader) {
3389     mpack_tag_t var = mpack_read_tag(reader);
3390     if (var.type == mpack_type_uint) {
3391         if (var.v.u <= UINT16_MAX)
3392             return (uint16_t)var.v.u;
3393     } else if (var.type == mpack_type_int) {
3394         if (var.v.i >= 0 && var.v.i <= UINT16_MAX)
3395             return (uint16_t)var.v.i;
3396     }
3397     mpack_reader_flag_error(reader, mpack_error_type);
3398     return 0;
3399 }
3400 
mpack_expect_u32(mpack_reader_t * reader)3401 uint32_t mpack_expect_u32(mpack_reader_t* reader) {
3402     mpack_tag_t var = mpack_read_tag(reader);
3403     if (var.type == mpack_type_uint) {
3404         if (var.v.u <= UINT32_MAX)
3405             return (uint32_t)var.v.u;
3406     } else if (var.type == mpack_type_int) {
3407         if (var.v.i >= 0 && var.v.i <= UINT32_MAX)
3408             return (uint32_t)var.v.i;
3409     }
3410     mpack_reader_flag_error(reader, mpack_error_type);
3411     return 0;
3412 }
3413 
mpack_expect_u64(mpack_reader_t * reader)3414 uint64_t mpack_expect_u64(mpack_reader_t* reader) {
3415     mpack_tag_t var = mpack_read_tag(reader);
3416     if (var.type == mpack_type_uint) {
3417         return var.v.u;
3418     } else if (var.type == mpack_type_int) {
3419         if (var.v.i >= 0)
3420             return (uint64_t)var.v.i;
3421     }
3422     mpack_reader_flag_error(reader, mpack_error_type);
3423     return 0;
3424 }
3425 
mpack_expect_i8(mpack_reader_t * reader)3426 int8_t mpack_expect_i8(mpack_reader_t* reader) {
3427     mpack_tag_t var = mpack_read_tag(reader);
3428     if (var.type == mpack_type_uint) {
3429         if (var.v.u <= INT8_MAX)
3430             return (int8_t)var.v.u;
3431     } else if (var.type == mpack_type_int) {
3432         if (var.v.i >= INT8_MIN && var.v.i <= INT8_MAX)
3433             return (int8_t)var.v.i;
3434     }
3435     mpack_reader_flag_error(reader, mpack_error_type);
3436     return 0;
3437 }
3438 
mpack_expect_i16(mpack_reader_t * reader)3439 int16_t mpack_expect_i16(mpack_reader_t* reader) {
3440     mpack_tag_t var = mpack_read_tag(reader);
3441     if (var.type == mpack_type_uint) {
3442         if (var.v.u <= INT16_MAX)
3443             return (int16_t)var.v.u;
3444     } else if (var.type == mpack_type_int) {
3445         if (var.v.i >= INT16_MIN && var.v.i <= INT16_MAX)
3446             return (int16_t)var.v.i;
3447     }
3448     mpack_reader_flag_error(reader, mpack_error_type);
3449     return 0;
3450 }
3451 
mpack_expect_i32(mpack_reader_t * reader)3452 int32_t mpack_expect_i32(mpack_reader_t* reader) {
3453     mpack_tag_t var = mpack_read_tag(reader);
3454     if (var.type == mpack_type_uint) {
3455         if (var.v.u <= INT32_MAX)
3456             return (int32_t)var.v.u;
3457     } else if (var.type == mpack_type_int) {
3458         if (var.v.i >= INT32_MIN && var.v.i <= INT32_MAX)
3459             return (int32_t)var.v.i;
3460     }
3461     mpack_reader_flag_error(reader, mpack_error_type);
3462     return 0;
3463 }
3464 
mpack_expect_i64(mpack_reader_t * reader)3465 int64_t mpack_expect_i64(mpack_reader_t* reader) {
3466     mpack_tag_t var = mpack_read_tag(reader);
3467     if (var.type == mpack_type_uint) {
3468         if (var.v.u <= INT64_MAX)
3469             return (int64_t)var.v.u;
3470     } else if (var.type == mpack_type_int) {
3471         return var.v.i;
3472     }
3473     mpack_reader_flag_error(reader, mpack_error_type);
3474     return 0;
3475 }
3476 
mpack_expect_float(mpack_reader_t * reader)3477 float mpack_expect_float(mpack_reader_t* reader) {
3478     mpack_tag_t var = mpack_read_tag(reader);
3479     if (var.type == mpack_type_uint)
3480         return (float)var.v.u;
3481     else if (var.type == mpack_type_int)
3482         return (float)var.v.i;
3483     else if (var.type == mpack_type_float)
3484         return var.v.f;
3485     else if (var.type == mpack_type_double)
3486         return (float)var.v.d;
3487     mpack_reader_flag_error(reader, mpack_error_type);
3488     return 0.0f;
3489 }
3490 
mpack_expect_double(mpack_reader_t * reader)3491 double mpack_expect_double(mpack_reader_t* reader) {
3492     mpack_tag_t var = mpack_read_tag(reader);
3493     if (var.type == mpack_type_uint)
3494         return (double)var.v.u;
3495     else if (var.type == mpack_type_int)
3496         return (double)var.v.i;
3497     else if (var.type == mpack_type_float)
3498         return (double)var.v.f;
3499     else if (var.type == mpack_type_double)
3500         return var.v.d;
3501     mpack_reader_flag_error(reader, mpack_error_type);
3502     return 0.0;
3503 }
3504 
mpack_expect_float_strict(mpack_reader_t * reader)3505 float mpack_expect_float_strict(mpack_reader_t* reader) {
3506     mpack_tag_t var = mpack_read_tag(reader);
3507     if (var.type == mpack_type_float)
3508         return var.v.f;
3509     mpack_reader_flag_error(reader, mpack_error_type);
3510     return 0.0f;
3511 }
3512 
mpack_expect_double_strict(mpack_reader_t * reader)3513 double mpack_expect_double_strict(mpack_reader_t* reader) {
3514     mpack_tag_t var = mpack_read_tag(reader);
3515     if (var.type == mpack_type_float)
3516         return (double)var.v.f;
3517     else if (var.type == mpack_type_double)
3518         return var.v.d;
3519     mpack_reader_flag_error(reader, mpack_error_type);
3520     return 0.0;
3521 }
3522 
3523 
3524 // Ranged Number Functions
3525 //
3526 // All ranged functions are identical other than the type, so we
3527 // define their content with a macro. The prototypes are still written
3528 // out in full to support ctags/IDE tools.
3529 
3530 #define MPACK_EXPECT_RANGE_IMPL(name, type_t)                           \
3531                                                                         \
3532     /* make sure the range is sensible */                               \
3533     mpack_assert(min_value <= max_value,                                \
3534             "min_value %i must be less than or equal to max_value %i",  \
3535             min_value, max_value);                                      \
3536                                                                         \
3537     /* read the value */                                                \
3538     type_t val = mpack_expect_##name(reader);                           \
3539     if (mpack_reader_error(reader) != mpack_ok)                         \
3540         return min_value;                                               \
3541                                                                         \
3542     /* make sure it fits */                                             \
3543     if (val < min_value || val > max_value) {                           \
3544         mpack_reader_flag_error(reader, mpack_error_type);              \
3545         return min_value;                                               \
3546     }                                                                   \
3547                                                                         \
3548     return val;
3549 
mpack_expect_u8_range(mpack_reader_t * reader,uint8_t min_value,uint8_t max_value)3550 uint8_t mpack_expect_u8_range(mpack_reader_t* reader, uint8_t min_value, uint8_t max_value) {MPACK_EXPECT_RANGE_IMPL(u8, uint8_t)}
mpack_expect_u16_range(mpack_reader_t * reader,uint16_t min_value,uint16_t max_value)3551 uint16_t mpack_expect_u16_range(mpack_reader_t* reader, uint16_t min_value, uint16_t max_value) {MPACK_EXPECT_RANGE_IMPL(u16, uint16_t)}
mpack_expect_u32_range(mpack_reader_t * reader,uint32_t min_value,uint32_t max_value)3552 uint32_t mpack_expect_u32_range(mpack_reader_t* reader, uint32_t min_value, uint32_t max_value) {MPACK_EXPECT_RANGE_IMPL(u32, uint32_t)}
mpack_expect_u64_range(mpack_reader_t * reader,uint64_t min_value,uint64_t max_value)3553 uint64_t mpack_expect_u64_range(mpack_reader_t* reader, uint64_t min_value, uint64_t max_value) {MPACK_EXPECT_RANGE_IMPL(u64, uint64_t)}
3554 
mpack_expect_i8_range(mpack_reader_t * reader,int8_t min_value,int8_t max_value)3555 int8_t mpack_expect_i8_range(mpack_reader_t* reader, int8_t min_value, int8_t max_value) {MPACK_EXPECT_RANGE_IMPL(i8, int8_t)}
mpack_expect_i16_range(mpack_reader_t * reader,int16_t min_value,int16_t max_value)3556 int16_t mpack_expect_i16_range(mpack_reader_t* reader, int16_t min_value, int16_t max_value) {MPACK_EXPECT_RANGE_IMPL(i16, int16_t)}
mpack_expect_i32_range(mpack_reader_t * reader,int32_t min_value,int32_t max_value)3557 int32_t mpack_expect_i32_range(mpack_reader_t* reader, int32_t min_value, int32_t max_value) {MPACK_EXPECT_RANGE_IMPL(i32, int32_t)}
mpack_expect_i64_range(mpack_reader_t * reader,int64_t min_value,int64_t max_value)3558 int64_t mpack_expect_i64_range(mpack_reader_t* reader, int64_t min_value, int64_t max_value) {MPACK_EXPECT_RANGE_IMPL(i64, int64_t)}
3559 
mpack_expect_float_range(mpack_reader_t * reader,float min_value,float max_value)3560 float mpack_expect_float_range(mpack_reader_t* reader, float min_value, float max_value) {MPACK_EXPECT_RANGE_IMPL(float, float)}
mpack_expect_double_range(mpack_reader_t * reader,double min_value,double max_value)3561 double mpack_expect_double_range(mpack_reader_t* reader, double min_value, double max_value) {MPACK_EXPECT_RANGE_IMPL(double, double)}
3562 
mpack_expect_map_range(mpack_reader_t * reader,uint32_t min_value,uint32_t max_value)3563 uint32_t mpack_expect_map_range(mpack_reader_t* reader, uint32_t min_value, uint32_t max_value) {MPACK_EXPECT_RANGE_IMPL(map, uint32_t)}
mpack_expect_array_range(mpack_reader_t * reader,uint32_t min_value,uint32_t max_value)3564 uint32_t mpack_expect_array_range(mpack_reader_t* reader, uint32_t min_value, uint32_t max_value) {MPACK_EXPECT_RANGE_IMPL(array, uint32_t)}
3565 
3566 
3567 // Matching Number Functions
3568 
mpack_expect_uint_match(mpack_reader_t * reader,uint64_t value)3569 void mpack_expect_uint_match(mpack_reader_t* reader, uint64_t value) {
3570     if (mpack_expect_u64(reader) != value)
3571         mpack_reader_flag_error(reader, mpack_error_type);
3572 }
3573 
mpack_expect_int_match(mpack_reader_t * reader,int64_t value)3574 void mpack_expect_int_match(mpack_reader_t* reader, int64_t value) {
3575     if (mpack_expect_i64(reader) != value)
3576         mpack_reader_flag_error(reader, mpack_error_type);
3577 }
3578 
3579 
3580 // Other Basic Types
3581 
mpack_expect_nil(mpack_reader_t * reader)3582 void mpack_expect_nil(mpack_reader_t* reader) {
3583     if (mpack_expect_type_byte(reader) != 0xc0)
3584         mpack_reader_flag_error(reader, mpack_error_type);
3585 }
3586 
mpack_expect_bool(mpack_reader_t * reader)3587 bool mpack_expect_bool(mpack_reader_t* reader) {
3588     uint8_t type = mpack_expect_type_byte(reader);
3589     if ((type & ~1) != 0xc2)
3590         mpack_reader_flag_error(reader, mpack_error_type);
3591     return (bool)(type & 1);
3592 }
3593 
mpack_expect_true(mpack_reader_t * reader)3594 void mpack_expect_true(mpack_reader_t* reader) {
3595     if (mpack_expect_bool(reader) != true)
3596         mpack_reader_flag_error(reader, mpack_error_type);
3597 }
3598 
mpack_expect_false(mpack_reader_t * reader)3599 void mpack_expect_false(mpack_reader_t* reader) {
3600     if (mpack_expect_bool(reader) != false)
3601         mpack_reader_flag_error(reader, mpack_error_type);
3602 }
3603 
3604 #if MPACK_EXTENSIONS
mpack_expect_timestamp(mpack_reader_t * reader)3605 mpack_timestamp_t mpack_expect_timestamp(mpack_reader_t* reader) {
3606     mpack_timestamp_t zero = {0, 0};
3607 
3608     mpack_tag_t tag = mpack_read_tag(reader);
3609     if (tag.type != mpack_type_ext) {
3610         mpack_reader_flag_error(reader, mpack_error_type);
3611         return zero;
3612     }
3613     if (mpack_tag_ext_exttype(&tag) != MPACK_EXTTYPE_TIMESTAMP) {
3614         mpack_reader_flag_error(reader, mpack_error_type);
3615         return zero;
3616     }
3617 
3618     return mpack_read_timestamp(reader, mpack_tag_ext_length(&tag));
3619 }
3620 
mpack_expect_timestamp_truncate(mpack_reader_t * reader)3621 int64_t mpack_expect_timestamp_truncate(mpack_reader_t* reader) {
3622     return mpack_expect_timestamp(reader).seconds;
3623 }
3624 #endif
3625 
3626 
3627 // Compound Types
3628 
mpack_expect_map(mpack_reader_t * reader)3629 uint32_t mpack_expect_map(mpack_reader_t* reader) {
3630     mpack_tag_t var = mpack_read_tag(reader);
3631     if (var.type == mpack_type_map)
3632         return var.v.n;
3633     mpack_reader_flag_error(reader, mpack_error_type);
3634     return 0;
3635 }
3636 
mpack_expect_map_match(mpack_reader_t * reader,uint32_t count)3637 void mpack_expect_map_match(mpack_reader_t* reader, uint32_t count) {
3638     if (mpack_expect_map(reader) != count)
3639         mpack_reader_flag_error(reader, mpack_error_type);
3640 }
3641 
mpack_expect_map_or_nil(mpack_reader_t * reader,uint32_t * count)3642 bool mpack_expect_map_or_nil(mpack_reader_t* reader, uint32_t* count) {
3643     mpack_assert(count != NULL, "count cannot be NULL");
3644 
3645     mpack_tag_t var = mpack_read_tag(reader);
3646     if (var.type == mpack_type_nil) {
3647         *count = 0;
3648         return false;
3649     }
3650     if (var.type == mpack_type_map) {
3651         *count = var.v.n;
3652         return true;
3653     }
3654     mpack_reader_flag_error(reader, mpack_error_type);
3655     *count = 0;
3656     return false;
3657 }
3658 
mpack_expect_map_max_or_nil(mpack_reader_t * reader,uint32_t max_count,uint32_t * count)3659 bool mpack_expect_map_max_or_nil(mpack_reader_t* reader, uint32_t max_count, uint32_t* count) {
3660     mpack_assert(count != NULL, "count cannot be NULL");
3661 
3662     bool has_map = mpack_expect_map_or_nil(reader, count);
3663     if (has_map && *count > max_count) {
3664         *count = 0;
3665         mpack_reader_flag_error(reader, mpack_error_type);
3666         return false;
3667     }
3668     return has_map;
3669 }
3670 
mpack_expect_array(mpack_reader_t * reader)3671 uint32_t mpack_expect_array(mpack_reader_t* reader) {
3672     mpack_tag_t var = mpack_read_tag(reader);
3673     if (var.type == mpack_type_array)
3674         return var.v.n;
3675     mpack_reader_flag_error(reader, mpack_error_type);
3676     return 0;
3677 }
3678 
mpack_expect_array_match(mpack_reader_t * reader,uint32_t count)3679 void mpack_expect_array_match(mpack_reader_t* reader, uint32_t count) {
3680     if (mpack_expect_array(reader) != count)
3681         mpack_reader_flag_error(reader, mpack_error_type);
3682 }
3683 
mpack_expect_array_or_nil(mpack_reader_t * reader,uint32_t * count)3684 bool mpack_expect_array_or_nil(mpack_reader_t* reader, uint32_t* count) {
3685     mpack_assert(count != NULL, "count cannot be NULL");
3686 
3687     mpack_tag_t var = mpack_read_tag(reader);
3688     if (var.type == mpack_type_nil) {
3689         *count = 0;
3690         return false;
3691     }
3692     if (var.type == mpack_type_array) {
3693         *count = var.v.n;
3694         return true;
3695     }
3696     mpack_reader_flag_error(reader, mpack_error_type);
3697     *count = 0;
3698     return false;
3699 }
3700 
mpack_expect_array_max_or_nil(mpack_reader_t * reader,uint32_t max_count,uint32_t * count)3701 bool mpack_expect_array_max_or_nil(mpack_reader_t* reader, uint32_t max_count, uint32_t* count) {
3702     mpack_assert(count != NULL, "count cannot be NULL");
3703 
3704     bool has_array = mpack_expect_array_or_nil(reader, count);
3705     if (has_array && *count > max_count) {
3706         *count = 0;
3707         mpack_reader_flag_error(reader, mpack_error_type);
3708         return false;
3709     }
3710     return has_array;
3711 }
3712 
3713 #ifdef MPACK_MALLOC
mpack_expect_array_alloc_impl(mpack_reader_t * reader,size_t element_size,uint32_t max_count,uint32_t * out_count,bool allow_nil)3714 void* mpack_expect_array_alloc_impl(mpack_reader_t* reader, size_t element_size, uint32_t max_count, uint32_t* out_count, bool allow_nil) {
3715     mpack_assert(out_count != NULL, "out_count cannot be NULL");
3716     *out_count = 0;
3717 
3718     uint32_t count;
3719     bool has_array = true;
3720     if (allow_nil)
3721         has_array = mpack_expect_array_max_or_nil(reader, max_count, &count);
3722     else
3723         count = mpack_expect_array_max(reader, max_count);
3724     if (mpack_reader_error(reader))
3725         return NULL;
3726 
3727     // size 0 is not an error; we return NULL for no elements.
3728     if (count == 0) {
3729         // we call mpack_done_array() automatically ONLY if we are using
3730         // the _or_nil variant. this is the only way to allow nil and empty
3731         // to work the same way.
3732         if (allow_nil && has_array)
3733             mpack_done_array(reader);
3734         return NULL;
3735     }
3736 
3737     void* p = MPACK_MALLOC(element_size * count);
3738     if (p == NULL) {
3739         mpack_reader_flag_error(reader, mpack_error_memory);
3740         return NULL;
3741     }
3742 
3743     *out_count = count;
3744     return p;
3745 }
3746 #endif
3747 
3748 
3749 // Str, Bin and Ext Functions
3750 
mpack_expect_str(mpack_reader_t * reader)3751 uint32_t mpack_expect_str(mpack_reader_t* reader) {
3752     #if MPACK_OPTIMIZE_FOR_SIZE
3753     mpack_tag_t var = mpack_read_tag(reader);
3754     if (var.type == mpack_type_str)
3755         return var.v.l;
3756     mpack_reader_flag_error(reader, mpack_error_type);
3757     return 0;
3758     #else
3759     uint8_t type = mpack_expect_type_byte(reader);
3760     uint32_t count;
3761 
3762     if ((type >> 5) == 5) {
3763         count = type & (uint8_t)~0xe0;
3764     } else if (type == 0xd9) {
3765         count = mpack_expect_native_u8(reader);
3766     } else if (type == 0xda) {
3767         count = mpack_expect_native_u16(reader);
3768     } else if (type == 0xdb) {
3769         count = mpack_expect_native_u32(reader);
3770     } else {
3771         mpack_reader_flag_error(reader, mpack_error_type);
3772         return 0;
3773     }
3774 
3775     #if MPACK_READ_TRACKING
3776     mpack_reader_flag_if_error(reader, mpack_track_push(&reader->track, mpack_type_str, count));
3777     #endif
3778     return count;
3779     #endif
3780 }
3781 
mpack_expect_str_buf(mpack_reader_t * reader,char * buf,size_t bufsize)3782 size_t mpack_expect_str_buf(mpack_reader_t* reader, char* buf, size_t bufsize) {
3783     mpack_assert(buf != NULL, "buf cannot be NULL");
3784 
3785     size_t length = mpack_expect_str(reader);
3786     if (mpack_reader_error(reader))
3787         return 0;
3788 
3789     if (length > bufsize) {
3790         mpack_reader_flag_error(reader, mpack_error_too_big);
3791         return 0;
3792     }
3793 
3794     mpack_read_bytes(reader, buf, length);
3795     if (mpack_reader_error(reader))
3796         return 0;
3797 
3798     mpack_done_str(reader);
3799     return length;
3800 }
3801 
mpack_expect_utf8(mpack_reader_t * reader,char * buf,size_t size)3802 size_t mpack_expect_utf8(mpack_reader_t* reader, char* buf, size_t size) {
3803     mpack_assert(buf != NULL, "buf cannot be NULL");
3804 
3805     size_t length = mpack_expect_str_buf(reader, buf, size);
3806 
3807     if (!mpack_utf8_check(buf, length)) {
3808         mpack_reader_flag_error(reader, mpack_error_type);
3809         return 0;
3810     }
3811 
3812     return length;
3813 }
3814 
mpack_expect_bin(mpack_reader_t * reader)3815 uint32_t mpack_expect_bin(mpack_reader_t* reader) {
3816     mpack_tag_t var = mpack_read_tag(reader);
3817     if (var.type == mpack_type_bin)
3818         return var.v.l;
3819     mpack_reader_flag_error(reader, mpack_error_type);
3820     return 0;
3821 }
3822 
mpack_expect_bin_buf(mpack_reader_t * reader,char * buf,size_t bufsize)3823 size_t mpack_expect_bin_buf(mpack_reader_t* reader, char* buf, size_t bufsize) {
3824     mpack_assert(buf != NULL, "buf cannot be NULL");
3825 
3826     size_t binsize = mpack_expect_bin(reader);
3827     if (mpack_reader_error(reader))
3828         return 0;
3829     if (binsize > bufsize) {
3830         mpack_reader_flag_error(reader, mpack_error_too_big);
3831         return 0;
3832     }
3833     mpack_read_bytes(reader, buf, binsize);
3834     if (mpack_reader_error(reader))
3835         return 0;
3836     mpack_done_bin(reader);
3837     return binsize;
3838 }
3839 
3840 #if MPACK_EXTENSIONS
mpack_expect_ext(mpack_reader_t * reader,int8_t * type)3841 uint32_t mpack_expect_ext(mpack_reader_t* reader, int8_t* type) {
3842     mpack_tag_t var = mpack_read_tag(reader);
3843     if (var.type == mpack_type_ext) {
3844         *type = mpack_tag_ext_exttype(&var);
3845         return mpack_tag_ext_length(&var);
3846     }
3847     *type = 0;
3848     mpack_reader_flag_error(reader, mpack_error_type);
3849     return 0;
3850 }
3851 
mpack_expect_ext_buf(mpack_reader_t * reader,int8_t * type,char * buf,size_t bufsize)3852 size_t mpack_expect_ext_buf(mpack_reader_t* reader, int8_t* type, char* buf, size_t bufsize) {
3853     mpack_assert(buf != NULL, "buf cannot be NULL");
3854 
3855     size_t extsize = mpack_expect_ext(reader, type);
3856     if (mpack_reader_error(reader))
3857         return 0;
3858     if (extsize > bufsize) {
3859         *type = 0;
3860         mpack_reader_flag_error(reader, mpack_error_too_big);
3861         return 0;
3862     }
3863     mpack_read_bytes(reader, buf, extsize);
3864     if (mpack_reader_error(reader)) {
3865         *type = 0;
3866         return 0;
3867     }
3868     mpack_done_ext(reader);
3869     return extsize;
3870 }
3871 #endif
3872 
mpack_expect_cstr(mpack_reader_t * reader,char * buf,size_t bufsize)3873 void mpack_expect_cstr(mpack_reader_t* reader, char* buf, size_t bufsize) {
3874     uint32_t length = mpack_expect_str(reader);
3875     mpack_read_cstr(reader, buf, bufsize, length);
3876     mpack_done_str(reader);
3877 }
3878 
mpack_expect_utf8_cstr(mpack_reader_t * reader,char * buf,size_t bufsize)3879 void mpack_expect_utf8_cstr(mpack_reader_t* reader, char* buf, size_t bufsize) {
3880     uint32_t length = mpack_expect_str(reader);
3881     mpack_read_utf8_cstr(reader, buf, bufsize, length);
3882     mpack_done_str(reader);
3883 }
3884 
3885 #ifdef MPACK_MALLOC
mpack_expect_cstr_alloc_unchecked(mpack_reader_t * reader,size_t maxsize,size_t * out_length)3886 static char* mpack_expect_cstr_alloc_unchecked(mpack_reader_t* reader, size_t maxsize, size_t* out_length) {
3887     mpack_assert(out_length != NULL, "out_length cannot be NULL");
3888     *out_length = 0;
3889 
3890     // make sure argument makes sense
3891     if (maxsize < 1) {
3892         mpack_break("maxsize is zero; you must have room for at least a null-terminator");
3893         mpack_reader_flag_error(reader, mpack_error_bug);
3894         return NULL;
3895     }
3896 
3897     if (maxsize > UINT32_MAX)
3898         maxsize = UINT32_MAX;
3899 
3900     size_t length = mpack_expect_str_max(reader, (uint32_t)maxsize - 1);
3901     char* str = mpack_read_bytes_alloc_impl(reader, length, true);
3902     mpack_done_str(reader);
3903 
3904     if (str)
3905         *out_length = length;
3906     return str;
3907 }
3908 
mpack_expect_cstr_alloc(mpack_reader_t * reader,size_t maxsize)3909 char* mpack_expect_cstr_alloc(mpack_reader_t* reader, size_t maxsize) {
3910     size_t length;
3911     char* str = mpack_expect_cstr_alloc_unchecked(reader, maxsize, &length);
3912 
3913     if (str && !mpack_str_check_no_null(str, length)) {
3914         MPACK_FREE(str);
3915         mpack_reader_flag_error(reader, mpack_error_type);
3916         return NULL;
3917     }
3918 
3919     return str;
3920 }
3921 
mpack_expect_utf8_cstr_alloc(mpack_reader_t * reader,size_t maxsize)3922 char* mpack_expect_utf8_cstr_alloc(mpack_reader_t* reader, size_t maxsize) {
3923     size_t length;
3924     char* str = mpack_expect_cstr_alloc_unchecked(reader, maxsize, &length);
3925 
3926     if (str && !mpack_utf8_check_no_null(str, length)) {
3927         MPACK_FREE(str);
3928         mpack_reader_flag_error(reader, mpack_error_type);
3929         return NULL;
3930     }
3931 
3932     return str;
3933 }
3934 #endif
3935 
mpack_expect_str_match(mpack_reader_t * reader,const char * str,size_t len)3936 void mpack_expect_str_match(mpack_reader_t* reader, const char* str, size_t len) {
3937     mpack_assert(str != NULL, "str cannot be NULL");
3938 
3939     // expect a str the correct length
3940     if (len > UINT32_MAX)
3941         mpack_reader_flag_error(reader, mpack_error_type);
3942     mpack_expect_str_length(reader, (uint32_t)len);
3943     if (mpack_reader_error(reader))
3944         return;
3945     mpack_reader_track_bytes(reader, len);
3946 
3947     // check each byte one by one (matched strings are likely to be very small)
3948     for (; len > 0; --len) {
3949         if (mpack_expect_native_u8(reader) != *str++) {
3950             mpack_reader_flag_error(reader, mpack_error_type);
3951             return;
3952         }
3953     }
3954 
3955     mpack_done_str(reader);
3956 }
3957 
mpack_expect_tag(mpack_reader_t * reader,mpack_tag_t expected)3958 void mpack_expect_tag(mpack_reader_t* reader, mpack_tag_t expected) {
3959     mpack_tag_t actual = mpack_read_tag(reader);
3960     if (!mpack_tag_equal(actual, expected))
3961         mpack_reader_flag_error(reader, mpack_error_type);
3962 }
3963 
3964 #ifdef MPACK_MALLOC
mpack_expect_bin_alloc(mpack_reader_t * reader,size_t maxsize,size_t * size)3965 char* mpack_expect_bin_alloc(mpack_reader_t* reader, size_t maxsize, size_t* size) {
3966     mpack_assert(size != NULL, "size cannot be NULL");
3967     *size = 0;
3968 
3969     if (maxsize > UINT32_MAX)
3970         maxsize = UINT32_MAX;
3971 
3972     size_t length = mpack_expect_bin_max(reader, (uint32_t)maxsize);
3973     if (mpack_reader_error(reader))
3974         return NULL;
3975 
3976     char* data = mpack_read_bytes_alloc(reader, length);
3977     mpack_done_bin(reader);
3978 
3979     if (data)
3980         *size = length;
3981     return data;
3982 }
3983 #endif
3984 
3985 #if MPACK_EXTENSIONS && defined(MPACK_MALLOC)
mpack_expect_ext_alloc(mpack_reader_t * reader,int8_t * type,size_t maxsize,size_t * size)3986 char* mpack_expect_ext_alloc(mpack_reader_t* reader, int8_t* type, size_t maxsize, size_t* size) {
3987     mpack_assert(size != NULL, "size cannot be NULL");
3988     *size = 0;
3989 
3990     if (maxsize > UINT32_MAX)
3991         maxsize = UINT32_MAX;
3992 
3993     size_t length = mpack_expect_ext_max(reader, type, (uint32_t)maxsize);
3994     if (mpack_reader_error(reader))
3995         return NULL;
3996 
3997     char* data = mpack_read_bytes_alloc(reader, length);
3998     mpack_done_ext(reader);
3999 
4000     if (data) {
4001         *size = length;
4002     } else {
4003         *type = 0;
4004     }
4005     return data;
4006 }
4007 #endif
4008 
mpack_expect_enum(mpack_reader_t * reader,const char * strings[],size_t count)4009 size_t mpack_expect_enum(mpack_reader_t* reader, const char* strings[], size_t count) {
4010 
4011     // read the string in-place
4012     size_t keylen = mpack_expect_str(reader);
4013     const char* key = mpack_read_bytes_inplace(reader, keylen);
4014     mpack_done_str(reader);
4015     if (mpack_reader_error(reader) != mpack_ok)
4016         return count;
4017 
4018     // find what key it matches
4019     for (size_t i = 0; i < count; ++i) {
4020         const char* other = strings[i];
4021         size_t otherlen = mpack_strlen(other);
4022         if (keylen == otherlen && mpack_memcmp(key, other, keylen) == 0)
4023             return i;
4024     }
4025 
4026     // no matches
4027     mpack_reader_flag_error(reader, mpack_error_type);
4028     return count;
4029 }
4030 
mpack_expect_enum_optional(mpack_reader_t * reader,const char * strings[],size_t count)4031 size_t mpack_expect_enum_optional(mpack_reader_t* reader, const char* strings[], size_t count) {
4032     if (mpack_reader_error(reader) != mpack_ok)
4033         return count;
4034 
4035     mpack_assert(count != 0, "count cannot be zero; no strings are valid!");
4036     mpack_assert(strings != NULL, "strings cannot be NULL");
4037 
4038     // the key is only recognized if it is a string
4039     if (mpack_peek_tag(reader).type != mpack_type_str) {
4040         mpack_discard(reader);
4041         return count;
4042     }
4043 
4044     // read the string in-place
4045     size_t keylen = mpack_expect_str(reader);
4046     const char* key = mpack_read_bytes_inplace(reader, keylen);
4047     mpack_done_str(reader);
4048     if (mpack_reader_error(reader) != mpack_ok)
4049         return count;
4050 
4051     // find what key it matches
4052     for (size_t i = 0; i < count; ++i) {
4053         const char* other = strings[i];
4054         size_t otherlen = mpack_strlen(other);
4055         if (keylen == otherlen && mpack_memcmp(key, other, keylen) == 0)
4056             return i;
4057     }
4058 
4059     // no matches
4060     return count;
4061 }
4062 
mpack_expect_key_uint(mpack_reader_t * reader,bool found[],size_t count)4063 size_t mpack_expect_key_uint(mpack_reader_t* reader, bool found[], size_t count) {
4064     if (mpack_reader_error(reader) != mpack_ok)
4065         return count;
4066 
4067     if (count == 0) {
4068         mpack_break("count cannot be zero; no keys are valid!");
4069         mpack_reader_flag_error(reader, mpack_error_bug);
4070         return count;
4071     }
4072     mpack_assert(found != NULL, "found cannot be NULL");
4073 
4074     // the key is only recognized if it is an unsigned int
4075     if (mpack_peek_tag(reader).type != mpack_type_uint) {
4076         mpack_discard(reader);
4077         return count;
4078     }
4079 
4080     // read the key
4081     uint64_t value = mpack_expect_u64(reader);
4082     if (mpack_reader_error(reader) != mpack_ok)
4083         return count;
4084 
4085     // unrecognized keys are fine, we just return count
4086     if (value >= count)
4087         return count;
4088 
4089     // check if this key is a duplicate
4090     if (found[value]) {
4091         mpack_reader_flag_error(reader, mpack_error_invalid);
4092         return count;
4093     }
4094 
4095     found[value] = true;
4096     return (size_t)value;
4097 }
4098 
mpack_expect_key_cstr(mpack_reader_t * reader,const char * keys[],bool found[],size_t count)4099 size_t mpack_expect_key_cstr(mpack_reader_t* reader, const char* keys[], bool found[], size_t count) {
4100     size_t i = mpack_expect_enum_optional(reader, keys, count);
4101 
4102     // unrecognized keys are fine, we just return count
4103     if (i == count)
4104         return count;
4105 
4106     // check if this key is a duplicate
4107     mpack_assert(found != NULL, "found cannot be NULL");
4108     if (found[i]) {
4109         mpack_reader_flag_error(reader, mpack_error_invalid);
4110         return count;
4111     }
4112 
4113     found[i] = true;
4114     return i;
4115 }
4116 
4117 #endif
4118 
4119 
4120 /* mpack/mpack-node.c.c */
4121 
4122 #define MPACK_INTERNAL 1
4123 
4124 /* #include "mpack-node.h" */
4125 
4126 #if MPACK_NODE
4127 
mpack_node_data_unchecked(mpack_node_t node)4128 MPACK_STATIC_INLINE const char* mpack_node_data_unchecked(mpack_node_t node) {
4129     mpack_assert(mpack_node_error(node) == mpack_ok, "tree is in an error state!");
4130 
4131     mpack_type_t type = node.data->type;
4132     MPACK_UNUSED(type);
4133     #if MPACK_EXTENSIONS
4134     mpack_assert(type == mpack_type_str || type == mpack_type_bin || type == mpack_type_ext,
4135             "node of type %i (%s) is not a data type!", type, mpack_type_to_string(type));
4136     #else
4137     mpack_assert(type == mpack_type_str || type == mpack_type_bin,
4138             "node of type %i (%s) is not a data type!", type, mpack_type_to_string(type));
4139     #endif
4140 
4141     return node.tree->data + node.data->value.offset;
4142 }
4143 
4144 #if MPACK_EXTENSIONS
mpack_node_exttype_unchecked(mpack_node_t node)4145 MPACK_STATIC_INLINE int8_t mpack_node_exttype_unchecked(mpack_node_t node) {
4146     mpack_assert(mpack_node_error(node) == mpack_ok, "tree is in an error state!");
4147 
4148     mpack_type_t type = node.data->type;
4149     MPACK_UNUSED(type);
4150     mpack_assert(type == mpack_type_ext, "node of type %i (%s) is not an ext type!",
4151             type, mpack_type_to_string(type));
4152 
4153     // the exttype of an ext node is stored in the byte preceding the data
4154     return mpack_load_i8(mpack_node_data_unchecked(node) - 1);
4155 }
4156 #endif
4157 
4158 
4159 
4160 /*
4161  * Tree Parsing
4162  */
4163 
4164 #ifdef MPACK_MALLOC
4165 
4166 // fix up the alloc size to make sure it exactly fits the
4167 // maximum number of nodes it can contain (the allocator will
4168 // waste it back anyway, but we round it down just in case)
4169 
4170 #define MPACK_NODES_PER_PAGE \
4171     ((MPACK_NODE_PAGE_SIZE - sizeof(mpack_tree_page_t)) / sizeof(mpack_node_data_t) + 1)
4172 
4173 #define MPACK_PAGE_ALLOC_SIZE \
4174     (sizeof(mpack_tree_page_t) + sizeof(mpack_node_data_t) * (MPACK_NODES_PER_PAGE - 1))
4175 
4176 #endif
4177 
4178 #ifdef MPACK_MALLOC
4179 /*
4180  * Fills the tree until we have at least enough bytes for the current node.
4181  */
mpack_tree_reserve_fill(mpack_tree_t * tree)4182 static bool mpack_tree_reserve_fill(mpack_tree_t* tree) {
4183     mpack_assert(tree->parser.state == mpack_tree_parse_state_in_progress);
4184 
4185     size_t bytes = tree->parser.current_node_reserved;
4186     mpack_assert(bytes > tree->parser.possible_nodes_left,
4187             "there are already enough bytes! call mpack_tree_ensure() instead.");
4188     mpack_log("filling to reserve %i bytes\n", (int)bytes);
4189 
4190     // if the necessary bytes would put us over the maximum tree
4191     // size, fail right away.
4192     // TODO: check for overflow?
4193     if (tree->data_length + bytes > tree->max_size) {
4194         mpack_tree_flag_error(tree, mpack_error_too_big);
4195         return false;
4196     }
4197 
4198     // we'll need a read function to fetch more data. if there's
4199     // no read function, the data should contain an entire message
4200     // (or messages), so we flag it as invalid.
4201     if (tree->read_fn == NULL) {
4202         mpack_log("tree has no read function!\n");
4203         mpack_tree_flag_error(tree, mpack_error_invalid);
4204         return false;
4205     }
4206 
4207     // expand the buffer if needed
4208     if (tree->data_length + bytes > tree->buffer_capacity) {
4209 
4210         // TODO: check for overflow?
4211         size_t new_capacity = (tree->buffer_capacity == 0) ? MPACK_BUFFER_SIZE : tree->buffer_capacity;
4212         while (new_capacity < tree->data_length + bytes)
4213             new_capacity *= 2;
4214         if (new_capacity > tree->max_size)
4215             new_capacity = tree->max_size;
4216 
4217         mpack_log("expanding buffer from %i to %i\n", (int)tree->buffer_capacity, (int)new_capacity);
4218 
4219         char* new_buffer;
4220         if (tree->buffer == NULL)
4221             new_buffer = (char*)MPACK_MALLOC(new_capacity);
4222         else
4223             new_buffer = (char*)mpack_realloc(tree->buffer, tree->data_length, new_capacity);
4224 
4225         if (new_buffer == NULL) {
4226             mpack_tree_flag_error(tree, mpack_error_memory);
4227             return false;
4228         }
4229 
4230         tree->data = new_buffer;
4231         tree->buffer = new_buffer;
4232         tree->buffer_capacity = new_capacity;
4233     }
4234 
4235     // request as much data as possible, looping until we have
4236     // all the data we need
4237     do {
4238         size_t read = tree->read_fn(tree, tree->buffer + tree->data_length, tree->buffer_capacity - tree->data_length);
4239 
4240         // If the fill function encounters an error, it should flag an error on
4241         // the tree.
4242         if (mpack_tree_error(tree) != mpack_ok)
4243             return false;
4244 
4245         // We guard against fill functions that return -1 just in case.
4246         if (read == (size_t)(-1)) {
4247             mpack_tree_flag_error(tree, mpack_error_io);
4248             return false;
4249         }
4250 
4251         // If the fill function returns 0, the data is not available yet. We
4252         // return false to stop parsing the current node.
4253         if (read == 0) {
4254             mpack_log("not enough data.\n");
4255             return false;
4256         }
4257 
4258         mpack_log("read %u more bytes\n", (uint32_t)read);
4259         tree->data_length += read;
4260         tree->parser.possible_nodes_left += read;
4261     } while (tree->parser.possible_nodes_left < bytes);
4262 
4263     return true;
4264 }
4265 #endif
4266 
4267 /*
4268  * Ensures there are enough additional bytes in the tree for the current node
4269  * (including reserved bytes for the children of this node, and in addition to
4270  * the reserved bytes for children of previous compound nodes), reading more
4271  * data if needed.
4272  *
4273  * extra_bytes is the number of additional bytes to reserve for the current
4274  * node beyond the type byte (since one byte is already reserved for each node
4275  * by its parent array or map.)
4276  *
4277  * This may reallocate the tree, which means the tree->data pointer may change!
4278  *
4279  * Returns false if not enough bytes could be read.
4280  */
mpack_tree_reserve_bytes(mpack_tree_t * tree,size_t extra_bytes)4281 MPACK_STATIC_INLINE bool mpack_tree_reserve_bytes(mpack_tree_t* tree, size_t extra_bytes) {
4282     mpack_assert(tree->parser.state == mpack_tree_parse_state_in_progress);
4283 
4284     // We guard against overflow here. A compound type could declare more than
4285     // UINT32_MAX contents which overflows SIZE_MAX on 32-bit platforms. We
4286     // flag mpack_error_invalid instead of mpack_error_too_big since it's far
4287     // more likely that the message is corrupt than that the data is valid but
4288     // not parseable on this architecture (see test_read_node_possible() in
4289     // test-node.c .)
4290     if ((uint64_t)tree->parser.current_node_reserved + (uint64_t)extra_bytes > SIZE_MAX) {
4291         mpack_tree_flag_error(tree, mpack_error_invalid);
4292         return false;
4293     }
4294 
4295     tree->parser.current_node_reserved += extra_bytes;
4296 
4297     // Note that possible_nodes_left already accounts for reserved bytes for
4298     // children of previous compound nodes. So even if there are hundreds of
4299     // bytes left in the buffer, we might need to read anyway.
4300     if (tree->parser.current_node_reserved <= tree->parser.possible_nodes_left)
4301         return true;
4302 
4303     #ifdef MPACK_MALLOC
4304     return mpack_tree_reserve_fill(tree);
4305     #else
4306     return false;
4307     #endif
4308 }
4309 
mpack_tree_parser_stack_capacity(mpack_tree_t * tree)4310 MPACK_STATIC_INLINE size_t mpack_tree_parser_stack_capacity(mpack_tree_t* tree) {
4311     #ifdef MPACK_MALLOC
4312     return tree->parser.stack_capacity;
4313     #else
4314     return sizeof(tree->parser.stack) / sizeof(tree->parser.stack[0]);
4315     #endif
4316 }
4317 
mpack_tree_push_stack(mpack_tree_t * tree,mpack_node_data_t * first_child,size_t total)4318 static bool mpack_tree_push_stack(mpack_tree_t* tree, mpack_node_data_t* first_child, size_t total) {
4319     mpack_tree_parser_t* parser = &tree->parser;
4320     mpack_assert(parser->state == mpack_tree_parse_state_in_progress);
4321 
4322     // No need to push empty containers
4323     if (total == 0)
4324         return true;
4325 
4326     // Make sure we have enough room in the stack
4327     if (parser->level + 1 == mpack_tree_parser_stack_capacity(tree)) {
4328         #ifdef MPACK_MALLOC
4329         size_t new_capacity = parser->stack_capacity * 2;
4330         mpack_log("growing parse stack to capacity %i\n", (int)new_capacity);
4331 
4332         // Replace the stack-allocated parsing stack
4333         if (!parser->stack_owned) {
4334             mpack_level_t* new_stack = (mpack_level_t*)MPACK_MALLOC(sizeof(mpack_level_t) * new_capacity);
4335             if (!new_stack) {
4336                 mpack_tree_flag_error(tree, mpack_error_memory);
4337                 return false;
4338             }
4339             mpack_memcpy(new_stack, parser->stack, sizeof(mpack_level_t) * parser->stack_capacity);
4340             parser->stack = new_stack;
4341             parser->stack_owned = true;
4342 
4343         // Realloc the allocated parsing stack
4344         } else {
4345             mpack_level_t* new_stack = (mpack_level_t*)mpack_realloc(parser->stack,
4346                     sizeof(mpack_level_t) * parser->stack_capacity, sizeof(mpack_level_t) * new_capacity);
4347             if (!new_stack) {
4348                 mpack_tree_flag_error(tree, mpack_error_memory);
4349                 return false;
4350             }
4351             parser->stack = new_stack;
4352         }
4353         parser->stack_capacity = new_capacity;
4354         #else
4355         mpack_tree_flag_error(tree, mpack_error_too_big);
4356         return false;
4357         #endif
4358     }
4359 
4360     // Push the contents of this node onto the parsing stack
4361     ++parser->level;
4362     parser->stack[parser->level].child = first_child;
4363     parser->stack[parser->level].left = total;
4364     return true;
4365 }
4366 
mpack_tree_parse_children(mpack_tree_t * tree,mpack_node_data_t * node)4367 static bool mpack_tree_parse_children(mpack_tree_t* tree, mpack_node_data_t* node) {
4368     mpack_tree_parser_t* parser = &tree->parser;
4369     mpack_assert(parser->state == mpack_tree_parse_state_in_progress);
4370 
4371     mpack_type_t type = node->type;
4372     size_t total = node->len;
4373 
4374     // Calculate total elements to read
4375     if (type == mpack_type_map) {
4376         if ((uint64_t)total * 2 > SIZE_MAX) {
4377             mpack_tree_flag_error(tree, mpack_error_too_big);
4378             return false;
4379         }
4380         total *= 2;
4381     }
4382 
4383     // Make sure we are under our total node limit (TODO can this overflow?)
4384     tree->node_count += total;
4385     if (tree->node_count > tree->max_nodes) {
4386         mpack_tree_flag_error(tree, mpack_error_too_big);
4387         return false;
4388     }
4389 
4390     // Each node is at least one byte. Count these bytes now to make
4391     // sure there is enough data left.
4392     if (!mpack_tree_reserve_bytes(tree, total))
4393         return false;
4394 
4395     // If there are enough nodes left in the current page, no need to grow
4396     if (total <= parser->nodes_left) {
4397         node->value.children = parser->nodes;
4398         parser->nodes += total;
4399         parser->nodes_left -= total;
4400 
4401     } else {
4402 
4403         #ifdef MPACK_MALLOC
4404 
4405         // We can't grow if we're using a fixed pool (i.e. we didn't start with a page)
4406         if (!tree->next) {
4407             mpack_tree_flag_error(tree, mpack_error_too_big);
4408             return false;
4409         }
4410 
4411         // Otherwise we need to grow, and the node's children need to be contiguous.
4412         // This is a heuristic to decide whether we should waste the remaining space
4413         // in the current page and start a new one, or give the children their
4414         // own page. With a fraction of 1/8, this causes at most 12% additional
4415         // waste. Note that reducing this too much causes less cache coherence and
4416         // more malloc() overhead due to smaller allocations, so there's a tradeoff
4417         // here. This heuristic could use some improvement, especially with custom
4418         // page sizes.
4419 
4420         mpack_tree_page_t* page;
4421 
4422         if (total > MPACK_NODES_PER_PAGE || parser->nodes_left > MPACK_NODES_PER_PAGE / 8) {
4423             // TODO: this should check for overflow
4424             page = (mpack_tree_page_t*)MPACK_MALLOC(
4425                     sizeof(mpack_tree_page_t) + sizeof(mpack_node_data_t) * (total - 1));
4426             if (page == NULL) {
4427                 mpack_tree_flag_error(tree, mpack_error_memory);
4428                 return false;
4429             }
4430             mpack_log("allocated seperate page %p for %i children, %i left in page of %i total\n",
4431                     page, (int)total, (int)parser->nodes_left, (int)MPACK_NODES_PER_PAGE);
4432 
4433             node->value.children = page->nodes;
4434 
4435         } else {
4436             page = (mpack_tree_page_t*)MPACK_MALLOC(MPACK_PAGE_ALLOC_SIZE);
4437             if (page == NULL) {
4438                 mpack_tree_flag_error(tree, mpack_error_memory);
4439                 return false;
4440             }
4441             mpack_log("allocated new page %p for %i children, wasting %i in page of %i total\n",
4442                     page, (int)total, (int)parser->nodes_left, (int)MPACK_NODES_PER_PAGE);
4443 
4444             node->value.children = page->nodes;
4445             parser->nodes = page->nodes + total;
4446             parser->nodes_left = MPACK_NODES_PER_PAGE - total;
4447         }
4448 
4449         page->next = tree->next;
4450         tree->next = page;
4451 
4452         #else
4453         // We can't grow if we don't have an allocator
4454         mpack_tree_flag_error(tree, mpack_error_too_big);
4455         return false;
4456         #endif
4457     }
4458 
4459     return mpack_tree_push_stack(tree, node->value.children, total);
4460 }
4461 
mpack_tree_parse_bytes(mpack_tree_t * tree,mpack_node_data_t * node)4462 static bool mpack_tree_parse_bytes(mpack_tree_t* tree, mpack_node_data_t* node) {
4463     node->value.offset = tree->size + tree->parser.current_node_reserved + 1;
4464     return mpack_tree_reserve_bytes(tree, node->len);
4465 }
4466 
4467 #if MPACK_EXTENSIONS
mpack_tree_parse_ext(mpack_tree_t * tree,mpack_node_data_t * node)4468 static bool mpack_tree_parse_ext(mpack_tree_t* tree, mpack_node_data_t* node) {
4469     // reserve space for exttype
4470     tree->parser.current_node_reserved += sizeof(int8_t);
4471     node->type = mpack_type_ext;
4472     return mpack_tree_parse_bytes(tree, node);
4473 }
4474 #endif
4475 
mpack_tree_parse_node_contents(mpack_tree_t * tree,mpack_node_data_t * node)4476 static bool mpack_tree_parse_node_contents(mpack_tree_t* tree, mpack_node_data_t* node) {
4477     mpack_assert(tree->parser.state == mpack_tree_parse_state_in_progress);
4478     mpack_assert(node != NULL, "null node?");
4479 
4480     // read the type. we've already accounted for this byte in
4481     // possible_nodes_left, so we already know it is in bounds, and we don't
4482     // need to reserve it for this node.
4483     mpack_assert(tree->data_length > tree->size);
4484     uint8_t type = mpack_load_u8(tree->data + tree->size);
4485     mpack_log("node type %x\n", type);
4486     tree->parser.current_node_reserved = 0;
4487 
4488     // as with mpack_read_tag(), the fastest way to parse a node is to switch
4489     // on the first byte, and to explicitly list every possible byte. we switch
4490     // on the first four bits in size-optimized builds.
4491 
4492     #if MPACK_OPTIMIZE_FOR_SIZE
4493     switch (type >> 4) {
4494 
4495         // positive fixnum
4496         case 0x0: case 0x1: case 0x2: case 0x3:
4497         case 0x4: case 0x5: case 0x6: case 0x7:
4498             node->type = mpack_type_uint;
4499             node->value.u = type;
4500             return true;
4501 
4502         // negative fixnum
4503         case 0xe: case 0xf:
4504             node->type = mpack_type_int;
4505             node->value.i = (int8_t)type;
4506             return true;
4507 
4508         // fixmap
4509         case 0x8:
4510             node->type = mpack_type_map;
4511             node->len = (uint32_t)(type & ~0xf0);
4512             return mpack_tree_parse_children(tree, node);
4513 
4514         // fixarray
4515         case 0x9:
4516             node->type = mpack_type_array;
4517             node->len = (uint32_t)(type & ~0xf0);
4518             return mpack_tree_parse_children(tree, node);
4519 
4520         // fixstr
4521         case 0xa: case 0xb:
4522             node->type = mpack_type_str;
4523             node->len = (uint32_t)(type & ~0xe0);
4524             return mpack_tree_parse_bytes(tree, node);
4525 
4526         // not one of the common infix types
4527         default:
4528             break;
4529     }
4530     #endif
4531 
4532     switch (type) {
4533 
4534         #if !MPACK_OPTIMIZE_FOR_SIZE
4535         // positive fixnum
4536         case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
4537         case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
4538         case 0x10: case 0x11: case 0x12: case 0x13: case 0x14: case 0x15: case 0x16: case 0x17:
4539         case 0x18: case 0x19: case 0x1a: case 0x1b: case 0x1c: case 0x1d: case 0x1e: case 0x1f:
4540         case 0x20: case 0x21: case 0x22: case 0x23: case 0x24: case 0x25: case 0x26: case 0x27:
4541         case 0x28: case 0x29: case 0x2a: case 0x2b: case 0x2c: case 0x2d: case 0x2e: case 0x2f:
4542         case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: case 0x35: case 0x36: case 0x37:
4543         case 0x38: case 0x39: case 0x3a: case 0x3b: case 0x3c: case 0x3d: case 0x3e: case 0x3f:
4544         case 0x40: case 0x41: case 0x42: case 0x43: case 0x44: case 0x45: case 0x46: case 0x47:
4545         case 0x48: case 0x49: case 0x4a: case 0x4b: case 0x4c: case 0x4d: case 0x4e: case 0x4f:
4546         case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57:
4547         case 0x58: case 0x59: case 0x5a: case 0x5b: case 0x5c: case 0x5d: case 0x5e: case 0x5f:
4548         case 0x60: case 0x61: case 0x62: case 0x63: case 0x64: case 0x65: case 0x66: case 0x67:
4549         case 0x68: case 0x69: case 0x6a: case 0x6b: case 0x6c: case 0x6d: case 0x6e: case 0x6f:
4550         case 0x70: case 0x71: case 0x72: case 0x73: case 0x74: case 0x75: case 0x76: case 0x77:
4551         case 0x78: case 0x79: case 0x7a: case 0x7b: case 0x7c: case 0x7d: case 0x7e: case 0x7f:
4552             node->type = mpack_type_uint;
4553             node->value.u = type;
4554             return true;
4555 
4556         // negative fixnum
4557         case 0xe0: case 0xe1: case 0xe2: case 0xe3: case 0xe4: case 0xe5: case 0xe6: case 0xe7:
4558         case 0xe8: case 0xe9: case 0xea: case 0xeb: case 0xec: case 0xed: case 0xee: case 0xef:
4559         case 0xf0: case 0xf1: case 0xf2: case 0xf3: case 0xf4: case 0xf5: case 0xf6: case 0xf7:
4560         case 0xf8: case 0xf9: case 0xfa: case 0xfb: case 0xfc: case 0xfd: case 0xfe: case 0xff:
4561             node->type = mpack_type_int;
4562             node->value.i = (int8_t)type;
4563             return true;
4564 
4565         // fixmap
4566         case 0x80: case 0x81: case 0x82: case 0x83: case 0x84: case 0x85: case 0x86: case 0x87:
4567         case 0x88: case 0x89: case 0x8a: case 0x8b: case 0x8c: case 0x8d: case 0x8e: case 0x8f:
4568             node->type = mpack_type_map;
4569             node->len = (uint32_t)(type & ~0xf0);
4570             return mpack_tree_parse_children(tree, node);
4571 
4572         // fixarray
4573         case 0x90: case 0x91: case 0x92: case 0x93: case 0x94: case 0x95: case 0x96: case 0x97:
4574         case 0x98: case 0x99: case 0x9a: case 0x9b: case 0x9c: case 0x9d: case 0x9e: case 0x9f:
4575             node->type = mpack_type_array;
4576             node->len = (uint32_t)(type & ~0xf0);
4577             return mpack_tree_parse_children(tree, node);
4578 
4579         // fixstr
4580         case 0xa0: case 0xa1: case 0xa2: case 0xa3: case 0xa4: case 0xa5: case 0xa6: case 0xa7:
4581         case 0xa8: case 0xa9: case 0xaa: case 0xab: case 0xac: case 0xad: case 0xae: case 0xaf:
4582         case 0xb0: case 0xb1: case 0xb2: case 0xb3: case 0xb4: case 0xb5: case 0xb6: case 0xb7:
4583         case 0xb8: case 0xb9: case 0xba: case 0xbb: case 0xbc: case 0xbd: case 0xbe: case 0xbf:
4584             node->type = mpack_type_str;
4585             node->len = (uint32_t)(type & ~0xe0);
4586             return mpack_tree_parse_bytes(tree, node);
4587         #endif
4588 
4589         // nil
4590         case 0xc0:
4591             node->type = mpack_type_nil;
4592             return true;
4593 
4594         // bool
4595         case 0xc2: case 0xc3:
4596             node->type = mpack_type_bool;
4597             node->value.b = type & 1;
4598             return true;
4599 
4600         // bin8
4601         case 0xc4:
4602             node->type = mpack_type_bin;
4603             if (!mpack_tree_reserve_bytes(tree, sizeof(uint8_t)))
4604                 return false;
4605             node->len = mpack_load_u8(tree->data + tree->size + 1);
4606             return mpack_tree_parse_bytes(tree, node);
4607 
4608         // bin16
4609         case 0xc5:
4610             node->type = mpack_type_bin;
4611             if (!mpack_tree_reserve_bytes(tree, sizeof(uint16_t)))
4612                 return false;
4613             node->len = mpack_load_u16(tree->data + tree->size + 1);
4614             return mpack_tree_parse_bytes(tree, node);
4615 
4616         // bin32
4617         case 0xc6:
4618             node->type = mpack_type_bin;
4619             if (!mpack_tree_reserve_bytes(tree, sizeof(uint32_t)))
4620                 return false;
4621             node->len = mpack_load_u32(tree->data + tree->size + 1);
4622             return mpack_tree_parse_bytes(tree, node);
4623 
4624         #if MPACK_EXTENSIONS
4625         // ext8
4626         case 0xc7:
4627             if (!mpack_tree_reserve_bytes(tree, sizeof(uint8_t)))
4628                 return false;
4629             node->len = mpack_load_u8(tree->data + tree->size + 1);
4630             return mpack_tree_parse_ext(tree, node);
4631 
4632         // ext16
4633         case 0xc8:
4634             if (!mpack_tree_reserve_bytes(tree, sizeof(uint16_t)))
4635                 return false;
4636             node->len = mpack_load_u16(tree->data + tree->size + 1);
4637             return mpack_tree_parse_ext(tree, node);
4638 
4639         // ext32
4640         case 0xc9:
4641             if (!mpack_tree_reserve_bytes(tree, sizeof(uint32_t)))
4642                 return false;
4643             node->len = mpack_load_u32(tree->data + tree->size + 1);
4644             return mpack_tree_parse_ext(tree, node);
4645         #endif
4646 
4647         // float
4648         case 0xca:
4649             if (!mpack_tree_reserve_bytes(tree, sizeof(float)))
4650                 return false;
4651             node->value.f = mpack_load_float(tree->data + tree->size + 1);
4652             node->type = mpack_type_float;
4653             return true;
4654 
4655         // double
4656         case 0xcb:
4657             if (!mpack_tree_reserve_bytes(tree, sizeof(double)))
4658                 return false;
4659             node->value.d = mpack_load_double(tree->data + tree->size + 1);
4660             node->type = mpack_type_double;
4661             return true;
4662 
4663         // uint8
4664         case 0xcc:
4665             node->type = mpack_type_uint;
4666             if (!mpack_tree_reserve_bytes(tree, sizeof(uint8_t)))
4667                 return false;
4668             node->value.u = mpack_load_u8(tree->data + tree->size + 1);
4669             return true;
4670 
4671         // uint16
4672         case 0xcd:
4673             node->type = mpack_type_uint;
4674             if (!mpack_tree_reserve_bytes(tree, sizeof(uint16_t)))
4675                 return false;
4676             node->value.u = mpack_load_u16(tree->data + tree->size + 1);
4677             return true;
4678 
4679         // uint32
4680         case 0xce:
4681             node->type = mpack_type_uint;
4682             if (!mpack_tree_reserve_bytes(tree, sizeof(uint32_t)))
4683                 return false;
4684             node->value.u = mpack_load_u32(tree->data + tree->size + 1);
4685             return true;
4686 
4687         // uint64
4688         case 0xcf:
4689             node->type = mpack_type_uint;
4690             if (!mpack_tree_reserve_bytes(tree, sizeof(uint64_t)))
4691                 return false;
4692             node->value.u = mpack_load_u64(tree->data + tree->size + 1);
4693             return true;
4694 
4695         // int8
4696         case 0xd0:
4697             node->type = mpack_type_int;
4698             if (!mpack_tree_reserve_bytes(tree, sizeof(int8_t)))
4699                 return false;
4700             node->value.i = mpack_load_i8(tree->data + tree->size + 1);
4701             return true;
4702 
4703         // int16
4704         case 0xd1:
4705             node->type = mpack_type_int;
4706             if (!mpack_tree_reserve_bytes(tree, sizeof(int16_t)))
4707                 return false;
4708             node->value.i = mpack_load_i16(tree->data + tree->size + 1);
4709             return true;
4710 
4711         // int32
4712         case 0xd2:
4713             node->type = mpack_type_int;
4714             if (!mpack_tree_reserve_bytes(tree, sizeof(int32_t)))
4715                 return false;
4716             node->value.i = mpack_load_i32(tree->data + tree->size + 1);
4717             return true;
4718 
4719         // int64
4720         case 0xd3:
4721             node->type = mpack_type_int;
4722             if (!mpack_tree_reserve_bytes(tree, sizeof(int64_t)))
4723                 return false;
4724             node->value.i = mpack_load_i64(tree->data + tree->size + 1);
4725             return true;
4726 
4727         #if MPACK_EXTENSIONS
4728         // fixext1
4729         case 0xd4:
4730             node->len = 1;
4731             return mpack_tree_parse_ext(tree, node);
4732 
4733         // fixext2
4734         case 0xd5:
4735             node->len = 2;
4736             return mpack_tree_parse_ext(tree, node);
4737 
4738         // fixext4
4739         case 0xd6:
4740             node->len = 4;
4741             return mpack_tree_parse_ext(tree, node);
4742 
4743         // fixext8
4744         case 0xd7:
4745             node->len = 8;
4746             return mpack_tree_parse_ext(tree, node);
4747 
4748         // fixext16
4749         case 0xd8:
4750             node->len = 16;
4751             return mpack_tree_parse_ext(tree, node);
4752         #endif
4753 
4754         // str8
4755         case 0xd9:
4756             if (!mpack_tree_reserve_bytes(tree, sizeof(uint8_t)))
4757                 return false;
4758             node->len = mpack_load_u8(tree->data + tree->size + 1);
4759             node->type = mpack_type_str;
4760             return mpack_tree_parse_bytes(tree, node);
4761 
4762         // str16
4763         case 0xda:
4764             if (!mpack_tree_reserve_bytes(tree, sizeof(uint16_t)))
4765                 return false;
4766             node->len = mpack_load_u16(tree->data + tree->size + 1);
4767             node->type = mpack_type_str;
4768             return mpack_tree_parse_bytes(tree, node);
4769 
4770         // str32
4771         case 0xdb:
4772             if (!mpack_tree_reserve_bytes(tree, sizeof(uint32_t)))
4773                 return false;
4774             node->len = mpack_load_u32(tree->data + tree->size + 1);
4775             node->type = mpack_type_str;
4776             return mpack_tree_parse_bytes(tree, node);
4777 
4778         // array16
4779         case 0xdc:
4780             if (!mpack_tree_reserve_bytes(tree, sizeof(uint16_t)))
4781                 return false;
4782             node->len = mpack_load_u16(tree->data + tree->size + 1);
4783             node->type = mpack_type_array;
4784             return mpack_tree_parse_children(tree, node);
4785 
4786         // array32
4787         case 0xdd:
4788             if (!mpack_tree_reserve_bytes(tree, sizeof(uint32_t)))
4789                 return false;
4790             node->len = mpack_load_u32(tree->data + tree->size + 1);
4791             node->type = mpack_type_array;
4792             return mpack_tree_parse_children(tree, node);
4793 
4794         // map16
4795         case 0xde:
4796             if (!mpack_tree_reserve_bytes(tree, sizeof(uint16_t)))
4797                 return false;
4798             node->len = mpack_load_u16(tree->data + tree->size + 1);
4799             node->type = mpack_type_map;
4800             return mpack_tree_parse_children(tree, node);
4801 
4802         // map32
4803         case 0xdf:
4804             if (!mpack_tree_reserve_bytes(tree, sizeof(uint32_t)))
4805                 return false;
4806             node->len = mpack_load_u32(tree->data + tree->size + 1);
4807             node->type = mpack_type_map;
4808             return mpack_tree_parse_children(tree, node);
4809 
4810         // reserved
4811         case 0xc1:
4812             mpack_tree_flag_error(tree, mpack_error_invalid);
4813             return false;
4814 
4815         #if !MPACK_EXTENSIONS
4816         // ext
4817         case 0xc7: // fallthrough
4818         case 0xc8: // fallthrough
4819         case 0xc9: // fallthrough
4820         // fixext
4821         case 0xd4: // fallthrough
4822         case 0xd5: // fallthrough
4823         case 0xd6: // fallthrough
4824         case 0xd7: // fallthrough
4825         case 0xd8:
4826             mpack_tree_flag_error(tree, mpack_error_unsupported);
4827             return false;
4828         #endif
4829 
4830         #if MPACK_OPTIMIZE_FOR_SIZE
4831         // any other bytes should have been handled by the infix switch
4832         default:
4833             break;
4834         #endif
4835     }
4836 
4837     mpack_assert(0, "unreachable");
4838     return false;
4839 }
4840 
mpack_tree_parse_node(mpack_tree_t * tree,mpack_node_data_t * node)4841 static bool mpack_tree_parse_node(mpack_tree_t* tree, mpack_node_data_t* node) {
4842     mpack_log("parsing a node at position %i in level %i\n",
4843             (int)tree->size, (int)tree->parser.level);
4844 
4845     if (!mpack_tree_parse_node_contents(tree, node)) {
4846         mpack_log("node parsing returned false\n");
4847         return false;
4848     }
4849 
4850     tree->parser.possible_nodes_left -= tree->parser.current_node_reserved;
4851 
4852     // The reserve for the current node does not include the initial byte
4853     // previously reserved as part of its parent.
4854     size_t node_size = tree->parser.current_node_reserved + 1;
4855 
4856     // If the parsed type is a map or array, the reserve includes one byte for
4857     // each child. We want to subtract these out of possible_nodes_left, but
4858     // not out of the current size of the tree.
4859     if (node->type == mpack_type_array)
4860         node_size -= node->len;
4861     else if (node->type == mpack_type_map)
4862         node_size -= node->len * 2;
4863     tree->size += node_size;
4864 
4865     mpack_log("parsed a node of type %s of %i bytes and "
4866             "%i additional bytes reserved for children.\n",
4867             mpack_type_to_string(node->type), (int)node_size,
4868             (int)tree->parser.current_node_reserved + 1 - (int)node_size);
4869 
4870     return true;
4871 }
4872 
4873 /*
4874  * We read nodes in a loop instead of recursively for maximum performance. The
4875  * stack holds the amount of children left to read in each level of the tree.
4876  * Parsing can pause and resume when more data becomes available.
4877  */
mpack_tree_continue_parsing(mpack_tree_t * tree)4878 static bool mpack_tree_continue_parsing(mpack_tree_t* tree) {
4879     if (mpack_tree_error(tree) != mpack_ok)
4880         return false;
4881 
4882     mpack_tree_parser_t* parser = &tree->parser;
4883     mpack_assert(parser->state == mpack_tree_parse_state_in_progress);
4884     mpack_log("parsing tree elements, %i bytes in buffer\n", (int)tree->data_length);
4885 
4886     // we loop parsing nodes until the parse stack is empty. we break
4887     // by returning out of the function.
4888     while (true) {
4889         mpack_node_data_t* node = parser->stack[parser->level].child;
4890         size_t level = parser->level;
4891         if (!mpack_tree_parse_node(tree, node))
4892             return false;
4893         --parser->stack[level].left;
4894         ++parser->stack[level].child;
4895 
4896         mpack_assert(mpack_tree_error(tree) == mpack_ok,
4897                 "mpack_tree_parse_node() should have returned false due to error!");
4898 
4899         // pop empty stack levels, exiting the outer loop when the stack is empty.
4900         // (we could tail-optimize containers by pre-emptively popping empty
4901         // stack levels before reading the new element, this way we wouldn't
4902         // have to loop. but we eventually want to use the parse stack to give
4903         // better error messages that contain the location of the error, so
4904         // it needs to be complete.)
4905         while (parser->stack[parser->level].left == 0) {
4906             if (parser->level == 0)
4907                 return true;
4908             --parser->level;
4909         }
4910     }
4911 }
4912 
mpack_tree_cleanup(mpack_tree_t * tree)4913 static void mpack_tree_cleanup(mpack_tree_t* tree) {
4914     MPACK_UNUSED(tree);
4915 
4916     #ifdef MPACK_MALLOC
4917     if (tree->parser.stack_owned) {
4918         MPACK_FREE(tree->parser.stack);
4919         tree->parser.stack = NULL;
4920         tree->parser.stack_owned = false;
4921     }
4922 
4923     mpack_tree_page_t* page = tree->next;
4924     while (page != NULL) {
4925         mpack_tree_page_t* next = page->next;
4926         mpack_log("freeing page %p\n", page);
4927         MPACK_FREE(page);
4928         page = next;
4929     }
4930     tree->next = NULL;
4931     #endif
4932 }
4933 
mpack_tree_parse_start(mpack_tree_t * tree)4934 static bool mpack_tree_parse_start(mpack_tree_t* tree) {
4935     if (mpack_tree_error(tree) != mpack_ok)
4936         return false;
4937 
4938     mpack_tree_parser_t* parser = &tree->parser;
4939     mpack_assert(parser->state != mpack_tree_parse_state_in_progress,
4940             "previous parsing was not finished!");
4941 
4942     if (parser->state == mpack_tree_parse_state_parsed)
4943         mpack_tree_cleanup(tree);
4944 
4945     mpack_log("starting parse\n");
4946     tree->parser.state = mpack_tree_parse_state_in_progress;
4947     tree->parser.current_node_reserved = 0;
4948 
4949     // check if we previously parsed a tree
4950     if (tree->size > 0) {
4951         #ifdef MPACK_MALLOC
4952         // if we're buffered, move the remaining data back to the
4953         // start of the buffer
4954         // TODO: This is not ideal performance-wise. We should only move data
4955         // when we need to call the fill function.
4956         // TODO: We could consider shrinking the buffer here, especially if we
4957         // determine that the fill function is providing less than a quarter of
4958         // the buffer size or if messages take up less than a quarter of the
4959         // buffer size. Maybe this should be configurable.
4960         if (tree->buffer != NULL) {
4961             mpack_memmove(tree->buffer, tree->buffer + tree->size, tree->data_length - tree->size);
4962         }
4963         else
4964         #endif
4965         // otherwise advance past the parsed data
4966         {
4967             tree->data += tree->size;
4968         }
4969         tree->data_length -= tree->size;
4970         tree->size = 0;
4971         tree->node_count = 0;
4972     }
4973 
4974     // make sure we have at least one byte available before allocating anything
4975     parser->possible_nodes_left = tree->data_length;
4976     if (!mpack_tree_reserve_bytes(tree, sizeof(uint8_t))) {
4977         tree->parser.state = mpack_tree_parse_state_not_started;
4978         return false;
4979     }
4980     mpack_log("parsing tree at %p starting with byte %x\n", tree->data, (uint8_t)tree->data[0]);
4981     parser->possible_nodes_left -= 1;
4982     tree->node_count = 1;
4983 
4984     #ifdef MPACK_MALLOC
4985     parser->stack = parser->stack_local;
4986     parser->stack_owned = false;
4987     parser->stack_capacity = sizeof(parser->stack_local) / sizeof(*parser->stack_local);
4988 
4989     if (tree->pool == NULL) {
4990 
4991         // allocate first page
4992         mpack_tree_page_t* page = (mpack_tree_page_t*)MPACK_MALLOC(MPACK_PAGE_ALLOC_SIZE);
4993         mpack_log("allocated initial page %p of size %i count %i\n",
4994                 page, (int)MPACK_PAGE_ALLOC_SIZE, (int)MPACK_NODES_PER_PAGE);
4995         if (page == NULL) {
4996             tree->error = mpack_error_memory;
4997             return false;
4998         }
4999         page->next = NULL;
5000         tree->next = page;
5001 
5002         parser->nodes = page->nodes;
5003         parser->nodes_left = MPACK_NODES_PER_PAGE;
5004     }
5005     else
5006     #endif
5007     {
5008         // otherwise use the provided pool
5009         mpack_assert(tree->pool != NULL, "no pool provided?");
5010         parser->nodes = tree->pool;
5011         parser->nodes_left = tree->pool_count;
5012     }
5013 
5014     tree->root = parser->nodes;
5015     ++parser->nodes;
5016     --parser->nodes_left;
5017 
5018     parser->level = 0;
5019     parser->stack[0].child = tree->root;
5020     parser->stack[0].left = 1;
5021 
5022     return true;
5023 }
5024 
mpack_tree_parse(mpack_tree_t * tree)5025 void mpack_tree_parse(mpack_tree_t* tree) {
5026     if (mpack_tree_error(tree) != mpack_ok)
5027         return;
5028 
5029     if (tree->parser.state != mpack_tree_parse_state_in_progress) {
5030         if (!mpack_tree_parse_start(tree)) {
5031             mpack_tree_flag_error(tree, (tree->read_fn == NULL) ?
5032                     mpack_error_invalid : mpack_error_io);
5033             return;
5034         }
5035     }
5036 
5037     if (!mpack_tree_continue_parsing(tree)) {
5038         if (mpack_tree_error(tree) != mpack_ok)
5039             return;
5040 
5041         // We're parsing synchronously on a blocking fill function. If we
5042         // didn't completely finish parsing the tree, it's an error.
5043         mpack_log("tree parsing incomplete. flagging error.\n");
5044         mpack_tree_flag_error(tree, (tree->read_fn == NULL) ?
5045                 mpack_error_invalid : mpack_error_io);
5046         return;
5047     }
5048 
5049     mpack_assert(mpack_tree_error(tree) == mpack_ok);
5050     mpack_assert(tree->parser.level == 0);
5051     tree->parser.state = mpack_tree_parse_state_parsed;
5052     mpack_log("parsed tree of %i bytes, %i bytes left\n", (int)tree->size, (int)tree->parser.possible_nodes_left);
5053     mpack_log("%i nodes in final page\n", (int)tree->parser.nodes_left);
5054 }
5055 
mpack_tree_try_parse(mpack_tree_t * tree)5056 bool mpack_tree_try_parse(mpack_tree_t* tree) {
5057     if (mpack_tree_error(tree) != mpack_ok)
5058         return false;
5059 
5060     if (tree->parser.state != mpack_tree_parse_state_in_progress)
5061         if (!mpack_tree_parse_start(tree))
5062             return false;
5063 
5064     if (!mpack_tree_continue_parsing(tree))
5065         return false;
5066 
5067     mpack_assert(mpack_tree_error(tree) == mpack_ok);
5068     mpack_assert(tree->parser.level == 0);
5069     tree->parser.state = mpack_tree_parse_state_parsed;
5070     return true;
5071 }
5072 
5073 
5074 
5075 /*
5076  * Tree functions
5077  */
5078 
mpack_tree_root(mpack_tree_t * tree)5079 mpack_node_t mpack_tree_root(mpack_tree_t* tree) {
5080     if (mpack_tree_error(tree) != mpack_ok)
5081         return mpack_tree_nil_node(tree);
5082 
5083     // We check that a tree was parsed successfully and assert if not. You must
5084     // call mpack_tree_parse() (or mpack_tree_try_parse() with a success
5085     // result) in order to access the root node.
5086     if (tree->parser.state != mpack_tree_parse_state_parsed) {
5087         mpack_break("Tree has not been parsed! "
5088                 "Did you call mpack_tree_parse() or mpack_tree_try_parse()?");
5089         mpack_tree_flag_error(tree, mpack_error_bug);
5090         return mpack_tree_nil_node(tree);
5091     }
5092 
5093     return mpack_node(tree, tree->root);
5094 }
5095 
mpack_tree_init_clear(mpack_tree_t * tree)5096 static void mpack_tree_init_clear(mpack_tree_t* tree) {
5097     mpack_memset(tree, 0, sizeof(*tree));
5098     tree->nil_node.type = mpack_type_nil;
5099     tree->missing_node.type = mpack_type_missing;
5100     tree->max_size = SIZE_MAX;
5101     tree->max_nodes = SIZE_MAX;
5102 }
5103 
5104 #ifdef MPACK_MALLOC
mpack_tree_init_data(mpack_tree_t * tree,const char * data,size_t length)5105 void mpack_tree_init_data(mpack_tree_t* tree, const char* data, size_t length) {
5106     mpack_tree_init_clear(tree);
5107 
5108     MPACK_STATIC_ASSERT(MPACK_NODE_PAGE_SIZE >= sizeof(mpack_tree_page_t),
5109             "MPACK_NODE_PAGE_SIZE is too small");
5110 
5111     MPACK_STATIC_ASSERT(MPACK_PAGE_ALLOC_SIZE <= MPACK_NODE_PAGE_SIZE,
5112             "incorrect page rounding?");
5113 
5114     tree->data = data;
5115     tree->data_length = length;
5116     tree->pool = NULL;
5117     tree->pool_count = 0;
5118     tree->next = NULL;
5119 
5120     mpack_log("===========================\n");
5121     mpack_log("initializing tree with data of size %i\n", (int)length);
5122 }
5123 #endif
5124 
mpack_tree_init_pool(mpack_tree_t * tree,const char * data,size_t length,mpack_node_data_t * node_pool,size_t node_pool_count)5125 void mpack_tree_init_pool(mpack_tree_t* tree, const char* data, size_t length,
5126         mpack_node_data_t* node_pool, size_t node_pool_count)
5127 {
5128     mpack_tree_init_clear(tree);
5129     #ifdef MPACK_MALLOC
5130     tree->next = NULL;
5131     #endif
5132 
5133     if (node_pool_count == 0) {
5134         mpack_break("initial page has no nodes!");
5135         mpack_tree_flag_error(tree, mpack_error_bug);
5136         return;
5137     }
5138 
5139     tree->data = data;
5140     tree->data_length = length;
5141     tree->pool = node_pool;
5142     tree->pool_count = node_pool_count;
5143 
5144     mpack_log("===========================\n");
5145     mpack_log("initializing tree with data of size %i and pool of count %i\n",
5146             (int)length, (int)node_pool_count);
5147 }
5148 
mpack_tree_init_error(mpack_tree_t * tree,mpack_error_t error)5149 void mpack_tree_init_error(mpack_tree_t* tree, mpack_error_t error) {
5150     mpack_tree_init_clear(tree);
5151     tree->error = error;
5152 
5153     mpack_log("===========================\n");
5154     mpack_log("initializing tree error state %i\n", (int)error);
5155 }
5156 
5157 #ifdef MPACK_MALLOC
mpack_tree_init_stream(mpack_tree_t * tree,mpack_tree_read_t read_fn,void * context,size_t max_message_size,size_t max_message_nodes)5158 void mpack_tree_init_stream(mpack_tree_t* tree, mpack_tree_read_t read_fn, void* context,
5159         size_t max_message_size, size_t max_message_nodes) {
5160     mpack_tree_init_clear(tree);
5161 
5162     tree->read_fn = read_fn;
5163     tree->context = context;
5164 
5165     mpack_tree_set_limits(tree, max_message_size, max_message_nodes);
5166     tree->max_size = max_message_size;
5167     tree->max_nodes = max_message_nodes;
5168 
5169     mpack_log("===========================\n");
5170     mpack_log("initializing tree with stream, max size %i max nodes %i\n",
5171             (int)max_message_size, (int)max_message_nodes);
5172 }
5173 #endif
5174 
mpack_tree_set_limits(mpack_tree_t * tree,size_t max_message_size,size_t max_message_nodes)5175 void mpack_tree_set_limits(mpack_tree_t* tree, size_t max_message_size, size_t max_message_nodes) {
5176     mpack_assert(max_message_size > 0);
5177     mpack_assert(max_message_nodes > 0);
5178     tree->max_size = max_message_size;
5179     tree->max_nodes = max_message_nodes;
5180 }
5181 
5182 #if MPACK_STDIO
5183 typedef struct mpack_file_tree_t {
5184     char* data;
5185     size_t size;
5186     char buffer[MPACK_BUFFER_SIZE];
5187 } mpack_file_tree_t;
5188 
mpack_file_tree_teardown(mpack_tree_t * tree)5189 static void mpack_file_tree_teardown(mpack_tree_t* tree) {
5190     mpack_file_tree_t* file_tree = (mpack_file_tree_t*)tree->context;
5191     MPACK_FREE(file_tree->data);
5192     MPACK_FREE(file_tree);
5193 }
5194 
mpack_file_tree_read(mpack_tree_t * tree,mpack_file_tree_t * file_tree,FILE * file,size_t max_bytes)5195 static bool mpack_file_tree_read(mpack_tree_t* tree, mpack_file_tree_t* file_tree, FILE* file, size_t max_bytes) {
5196 
5197     // get the file size
5198     errno = 0;
5199     int error = 0;
5200     fseek(file, 0, SEEK_END);
5201     error |= errno;
5202     long size = ftell(file);
5203     error |= errno;
5204     fseek(file, 0, SEEK_SET);
5205     error |= errno;
5206 
5207     // check for errors
5208     if (error != 0 || size < 0) {
5209         mpack_tree_init_error(tree, mpack_error_io);
5210         return false;
5211     }
5212     if (size == 0) {
5213         mpack_tree_init_error(tree, mpack_error_invalid);
5214         return false;
5215     }
5216 
5217     // make sure the size is less than max_bytes
5218     // (this mess exists to safely convert between long and size_t regardless of their widths)
5219     if (max_bytes != 0 && (((uint64_t)LONG_MAX > (uint64_t)SIZE_MAX && size > (long)SIZE_MAX) || (size_t)size > max_bytes)) {
5220         mpack_tree_init_error(tree, mpack_error_too_big);
5221         return false;
5222     }
5223 
5224     // allocate data
5225     file_tree->data = (char*)MPACK_MALLOC((size_t)size);
5226     if (file_tree->data == NULL) {
5227         mpack_tree_init_error(tree, mpack_error_memory);
5228         return false;
5229     }
5230 
5231     // read the file
5232     long total = 0;
5233     while (total < size) {
5234         size_t read = fread(file_tree->data + total, 1, (size_t)(size - total), file);
5235         if (read <= 0) {
5236             mpack_tree_init_error(tree, mpack_error_io);
5237             MPACK_FREE(file_tree->data);
5238             return false;
5239         }
5240         total += (long)read;
5241     }
5242 
5243     file_tree->size = (size_t)size;
5244     return true;
5245 }
5246 
mpack_tree_file_check_max_bytes(mpack_tree_t * tree,size_t max_bytes)5247 static bool mpack_tree_file_check_max_bytes(mpack_tree_t* tree, size_t max_bytes) {
5248 
5249     // the C STDIO family of file functions use long (e.g. ftell)
5250     if (max_bytes > LONG_MAX) {
5251         mpack_break("max_bytes of %" PRIu64 " is invalid, maximum is LONG_MAX", (uint64_t)max_bytes);
5252         mpack_tree_init_error(tree, mpack_error_bug);
5253         return false;
5254     }
5255 
5256     return true;
5257 }
5258 
mpack_tree_init_stdfile_noclose(mpack_tree_t * tree,FILE * stdfile,size_t max_bytes)5259 static void mpack_tree_init_stdfile_noclose(mpack_tree_t* tree, FILE* stdfile, size_t max_bytes) {
5260 
5261     // allocate file tree
5262     mpack_file_tree_t* file_tree = (mpack_file_tree_t*) MPACK_MALLOC(sizeof(mpack_file_tree_t));
5263     if (file_tree == NULL) {
5264         mpack_tree_init_error(tree, mpack_error_memory);
5265         return;
5266     }
5267 
5268     // read all data
5269     if (!mpack_file_tree_read(tree, file_tree, stdfile, max_bytes)) {
5270         MPACK_FREE(file_tree);
5271         return;
5272     }
5273 
5274     mpack_tree_init_data(tree, file_tree->data, file_tree->size);
5275     mpack_tree_set_context(tree, file_tree);
5276     mpack_tree_set_teardown(tree, mpack_file_tree_teardown);
5277 }
5278 
mpack_tree_init_stdfile(mpack_tree_t * tree,FILE * stdfile,size_t max_bytes,bool close_when_done)5279 void mpack_tree_init_stdfile(mpack_tree_t* tree, FILE* stdfile, size_t max_bytes, bool close_when_done) {
5280     if (!mpack_tree_file_check_max_bytes(tree, max_bytes))
5281         return;
5282 
5283     mpack_tree_init_stdfile_noclose(tree, stdfile, max_bytes);
5284 
5285     if (close_when_done)
5286         fclose(stdfile);
5287 }
5288 
mpack_tree_init_filename(mpack_tree_t * tree,const char * filename,size_t max_bytes)5289 void mpack_tree_init_filename(mpack_tree_t* tree, const char* filename, size_t max_bytes) {
5290     if (!mpack_tree_file_check_max_bytes(tree, max_bytes))
5291         return;
5292 
5293     // open the file
5294     FILE* file = fopen(filename, "rb");
5295     if (!file) {
5296         mpack_tree_init_error(tree, mpack_error_io);
5297         return;
5298     }
5299 
5300     mpack_tree_init_stdfile(tree, file, max_bytes, true);
5301 }
5302 #endif
5303 
mpack_tree_destroy(mpack_tree_t * tree)5304 mpack_error_t mpack_tree_destroy(mpack_tree_t* tree) {
5305     mpack_tree_cleanup(tree);
5306 
5307     #ifdef MPACK_MALLOC
5308     if (tree->buffer)
5309         MPACK_FREE(tree->buffer);
5310     #endif
5311 
5312     if (tree->teardown)
5313         tree->teardown(tree);
5314     tree->teardown = NULL;
5315 
5316     return tree->error;
5317 }
5318 
mpack_tree_flag_error(mpack_tree_t * tree,mpack_error_t error)5319 void mpack_tree_flag_error(mpack_tree_t* tree, mpack_error_t error) {
5320     if (tree->error == mpack_ok) {
5321         mpack_log("tree %p setting error %i: %s\n", tree, (int)error, mpack_error_to_string(error));
5322         tree->error = error;
5323         if (tree->error_fn)
5324             tree->error_fn(tree, error);
5325     }
5326 
5327 }
5328 
5329 
5330 
5331 /*
5332  * Node misc functions
5333  */
5334 
mpack_node_flag_error(mpack_node_t node,mpack_error_t error)5335 void mpack_node_flag_error(mpack_node_t node, mpack_error_t error) {
5336     mpack_tree_flag_error(node.tree, error);
5337 }
5338 
mpack_node_tag(mpack_node_t node)5339 mpack_tag_t mpack_node_tag(mpack_node_t node) {
5340     if (mpack_node_error(node) != mpack_ok)
5341         return mpack_tag_nil();
5342 
5343     mpack_tag_t tag = MPACK_TAG_ZERO;
5344 
5345     tag.type = node.data->type;
5346     switch (node.data->type) {
5347         case mpack_type_missing:
5348             // If a node is missing, I don't know if it makes sense to ask for
5349             // a tag for it. We'll return a missing tag to match the missing
5350             // node I guess, but attempting to use the tag for anything (like
5351             // writing it for example) will flag mpack_error_bug.
5352             break;
5353         case mpack_type_nil:                                            break;
5354         case mpack_type_bool:    tag.v.b = node.data->value.b;          break;
5355         case mpack_type_float:   tag.v.f = node.data->value.f;          break;
5356         case mpack_type_double:  tag.v.d = node.data->value.d;          break;
5357         case mpack_type_int:     tag.v.i = node.data->value.i;          break;
5358         case mpack_type_uint:    tag.v.u = node.data->value.u;          break;
5359 
5360         case mpack_type_str:     tag.v.l = node.data->len;     break;
5361         case mpack_type_bin:     tag.v.l = node.data->len;     break;
5362 
5363         #if MPACK_EXTENSIONS
5364         case mpack_type_ext:
5365             tag.v.l = node.data->len;
5366             tag.exttype = mpack_node_exttype_unchecked(node);
5367             break;
5368         #endif
5369 
5370         case mpack_type_array:   tag.v.n = node.data->len;  break;
5371         case mpack_type_map:     tag.v.n = node.data->len;  break;
5372 
5373         default:
5374             mpack_assert(0, "unrecognized type %i", (int)node.data->type);
5375             break;
5376     }
5377     return tag;
5378 }
5379 
5380 #if MPACK_DEBUG && MPACK_STDIO
mpack_node_print_element(mpack_node_t node,mpack_print_t * print,size_t depth)5381 static void mpack_node_print_element(mpack_node_t node, mpack_print_t* print, size_t depth) {
5382     mpack_node_data_t* data = node.data;
5383     switch (data->type) {
5384         case mpack_type_str:
5385             {
5386                 mpack_print_append_cstr(print, "\"");
5387                 const char* bytes = mpack_node_data_unchecked(node);
5388                 for (size_t i = 0; i < data->len; ++i) {
5389                     char c = bytes[i];
5390                     switch (c) {
5391                         case '\n': mpack_print_append_cstr(print, "\\n"); break;
5392                         case '\\': mpack_print_append_cstr(print, "\\\\"); break;
5393                         case '"': mpack_print_append_cstr(print, "\\\""); break;
5394                         default: mpack_print_append(print, &c, 1); break;
5395                     }
5396                 }
5397                 mpack_print_append_cstr(print, "\"");
5398             }
5399             break;
5400 
5401         case mpack_type_array:
5402             mpack_print_append_cstr(print, "[\n");
5403             for (size_t i = 0; i < data->len; ++i) {
5404                 for (size_t j = 0; j < depth + 1; ++j)
5405                     mpack_print_append_cstr(print, "    ");
5406                 mpack_node_print_element(mpack_node_array_at(node, i), print, depth + 1);
5407                 if (i != data->len - 1)
5408                     mpack_print_append_cstr(print, ",");
5409                 mpack_print_append_cstr(print, "\n");
5410             }
5411             for (size_t i = 0; i < depth; ++i)
5412                 mpack_print_append_cstr(print, "    ");
5413             mpack_print_append_cstr(print, "]");
5414             break;
5415 
5416         case mpack_type_map:
5417             mpack_print_append_cstr(print, "{\n");
5418             for (size_t i = 0; i < data->len; ++i) {
5419                 for (size_t j = 0; j < depth + 1; ++j)
5420                     mpack_print_append_cstr(print, "    ");
5421                 mpack_node_print_element(mpack_node_map_key_at(node, i), print, depth + 1);
5422                 mpack_print_append_cstr(print, ": ");
5423                 mpack_node_print_element(mpack_node_map_value_at(node, i), print, depth + 1);
5424                 if (i != data->len - 1)
5425                     mpack_print_append_cstr(print, ",");
5426                 mpack_print_append_cstr(print, "\n");
5427             }
5428             for (size_t i = 0; i < depth; ++i)
5429                 mpack_print_append_cstr(print, "    ");
5430             mpack_print_append_cstr(print, "}");
5431             break;
5432 
5433         default:
5434             {
5435                 const char* prefix = NULL;
5436                 size_t prefix_length = 0;
5437                 if (mpack_node_type(node) == mpack_type_bin
5438                         #if MPACK_EXTENSIONS
5439                         || mpack_node_type(node) == mpack_type_ext
5440                         #endif
5441                 ) {
5442                     prefix = mpack_node_data(node);
5443                     prefix_length = mpack_node_data_len(node);
5444                 }
5445 
5446                 char buf[256];
5447                 mpack_tag_t tag = mpack_node_tag(node);
5448                 mpack_tag_debug_pseudo_json(tag, buf, sizeof(buf), prefix, prefix_length);
5449                 mpack_print_append_cstr(print, buf);
5450             }
5451             break;
5452     }
5453 }
5454 
mpack_node_print_to_buffer(mpack_node_t node,char * buffer,size_t buffer_size)5455 void mpack_node_print_to_buffer(mpack_node_t node, char* buffer, size_t buffer_size) {
5456     if (buffer_size == 0) {
5457         mpack_assert(false, "buffer size is zero!");
5458         return;
5459     }
5460 
5461     mpack_print_t print;
5462     mpack_memset(&print, 0, sizeof(print));
5463     print.buffer = buffer;
5464     print.size = buffer_size;
5465     mpack_node_print_element(node, &print, 0);
5466     mpack_print_append(&print, "",  1); // null-terminator
5467     mpack_print_flush(&print);
5468 
5469     // we always make sure there's a null-terminator at the end of the buffer
5470     // in case we ran out of space.
5471     print.buffer[print.size - 1] = '\0';
5472 }
5473 
mpack_node_print_to_callback(mpack_node_t node,mpack_print_callback_t callback,void * context)5474 void mpack_node_print_to_callback(mpack_node_t node, mpack_print_callback_t callback, void* context) {
5475     char buffer[1024];
5476     mpack_print_t print;
5477     mpack_memset(&print, 0, sizeof(print));
5478     print.buffer = buffer;
5479     print.size = sizeof(buffer);
5480     print.callback = callback;
5481     print.context = context;
5482     mpack_node_print_element(node, &print, 0);
5483     mpack_print_flush(&print);
5484 }
5485 
mpack_node_print_to_file(mpack_node_t node,FILE * file)5486 void mpack_node_print_to_file(mpack_node_t node, FILE* file) {
5487     mpack_assert(file != NULL, "file is NULL");
5488 
5489     char buffer[1024];
5490     mpack_print_t print;
5491     mpack_memset(&print, 0, sizeof(print));
5492     print.buffer = buffer;
5493     print.size = sizeof(buffer);
5494     print.callback = &mpack_print_file_callback;
5495     print.context = file;
5496 
5497     size_t depth = 2;
5498     for (size_t i = 0; i < depth; ++i)
5499         mpack_print_append_cstr(&print, "    ");
5500     mpack_node_print_element(node, &print, depth);
5501     mpack_print_append_cstr(&print, "\n");
5502     mpack_print_flush(&print);
5503 }
5504 #endif
5505 
5506 
5507 
5508 /*
5509  * Node Value Functions
5510  */
5511 
5512 #if MPACK_EXTENSIONS
mpack_node_timestamp(mpack_node_t node)5513 mpack_timestamp_t mpack_node_timestamp(mpack_node_t node) {
5514     mpack_timestamp_t timestamp = {0, 0};
5515 
5516     // we'll let mpack_node_exttype() do most checks
5517     if (mpack_node_exttype(node) != MPACK_EXTTYPE_TIMESTAMP) {
5518         mpack_log("exttype %i\n", mpack_node_exttype(node));
5519         mpack_node_flag_error(node, mpack_error_type);
5520         return timestamp;
5521     }
5522 
5523     const char* p = mpack_node_data_unchecked(node);
5524 
5525     switch (node.data->len) {
5526         case 4:
5527             timestamp.nanoseconds = 0;
5528             timestamp.seconds = mpack_load_u32(p);
5529             break;
5530 
5531         case 8: {
5532             uint64_t value = mpack_load_u64(p);
5533             timestamp.nanoseconds = (uint32_t)(value >> 34);
5534             timestamp.seconds = value & ((UINT64_C(1) << 34) - 1);
5535             break;
5536         }
5537 
5538         case 12:
5539             timestamp.nanoseconds = mpack_load_u32(p);
5540             timestamp.seconds = mpack_load_i64(p + 4);
5541             break;
5542 
5543         default:
5544             mpack_tree_flag_error(node.tree, mpack_error_invalid);
5545             return timestamp;
5546     }
5547 
5548     if (timestamp.nanoseconds > MPACK_TIMESTAMP_NANOSECONDS_MAX) {
5549         mpack_tree_flag_error(node.tree, mpack_error_invalid);
5550         mpack_timestamp_t zero = {0, 0};
5551         return zero;
5552     }
5553 
5554     return timestamp;
5555 }
5556 
mpack_node_timestamp_seconds(mpack_node_t node)5557 int64_t mpack_node_timestamp_seconds(mpack_node_t node) {
5558     return mpack_node_timestamp(node).seconds;
5559 }
5560 
mpack_node_timestamp_nanoseconds(mpack_node_t node)5561 uint32_t mpack_node_timestamp_nanoseconds(mpack_node_t node) {
5562     return mpack_node_timestamp(node).nanoseconds;
5563 }
5564 #endif
5565 
5566 
5567 
5568 /*
5569  * Node Data Functions
5570  */
5571 
mpack_node_check_utf8(mpack_node_t node)5572 void mpack_node_check_utf8(mpack_node_t node) {
5573     if (mpack_node_error(node) != mpack_ok)
5574         return;
5575     mpack_node_data_t* data = node.data;
5576     if (data->type != mpack_type_str || !mpack_utf8_check(mpack_node_data_unchecked(node), data->len))
5577         mpack_node_flag_error(node, mpack_error_type);
5578 }
5579 
mpack_node_check_utf8_cstr(mpack_node_t node)5580 void mpack_node_check_utf8_cstr(mpack_node_t node) {
5581     if (mpack_node_error(node) != mpack_ok)
5582         return;
5583     mpack_node_data_t* data = node.data;
5584     if (data->type != mpack_type_str || !mpack_utf8_check_no_null(mpack_node_data_unchecked(node), data->len))
5585         mpack_node_flag_error(node, mpack_error_type);
5586 }
5587 
mpack_node_copy_data(mpack_node_t node,char * buffer,size_t bufsize)5588 size_t mpack_node_copy_data(mpack_node_t node, char* buffer, size_t bufsize) {
5589     if (mpack_node_error(node) != mpack_ok)
5590         return 0;
5591 
5592     mpack_assert(bufsize == 0 || buffer != NULL, "buffer is NULL for maximum of %i bytes", (int)bufsize);
5593 
5594     mpack_type_t type = node.data->type;
5595     if (type != mpack_type_str && type != mpack_type_bin
5596             #if MPACK_EXTENSIONS
5597             && type != mpack_type_ext
5598             #endif
5599     ) {
5600         mpack_node_flag_error(node, mpack_error_type);
5601         return 0;
5602     }
5603 
5604     if (node.data->len > bufsize) {
5605         mpack_node_flag_error(node, mpack_error_too_big);
5606         return 0;
5607     }
5608 
5609     mpack_memcpy(buffer, mpack_node_data_unchecked(node), node.data->len);
5610     return (size_t)node.data->len;
5611 }
5612 
mpack_node_copy_utf8(mpack_node_t node,char * buffer,size_t bufsize)5613 size_t mpack_node_copy_utf8(mpack_node_t node, char* buffer, size_t bufsize) {
5614     if (mpack_node_error(node) != mpack_ok)
5615         return 0;
5616 
5617     mpack_assert(bufsize == 0 || buffer != NULL, "buffer is NULL for maximum of %i bytes", (int)bufsize);
5618 
5619     mpack_type_t type = node.data->type;
5620     if (type != mpack_type_str) {
5621         mpack_node_flag_error(node, mpack_error_type);
5622         return 0;
5623     }
5624 
5625     if (node.data->len > bufsize) {
5626         mpack_node_flag_error(node, mpack_error_too_big);
5627         return 0;
5628     }
5629 
5630     if (!mpack_utf8_check(mpack_node_data_unchecked(node), node.data->len)) {
5631         mpack_node_flag_error(node, mpack_error_type);
5632         return 0;
5633     }
5634 
5635     mpack_memcpy(buffer, mpack_node_data_unchecked(node), node.data->len);
5636     return (size_t)node.data->len;
5637 }
5638 
mpack_node_copy_cstr(mpack_node_t node,char * buffer,size_t bufsize)5639 void mpack_node_copy_cstr(mpack_node_t node, char* buffer, size_t bufsize) {
5640 
5641     // we can't break here because the error isn't recoverable; we
5642     // have to add a null-terminator.
5643     mpack_assert(buffer != NULL, "buffer is NULL");
5644     mpack_assert(bufsize >= 1, "buffer size is zero; you must have room for at least a null-terminator");
5645 
5646     if (mpack_node_error(node) != mpack_ok) {
5647         buffer[0] = '\0';
5648         return;
5649     }
5650 
5651     if (node.data->type != mpack_type_str) {
5652         buffer[0] = '\0';
5653         mpack_node_flag_error(node, mpack_error_type);
5654         return;
5655     }
5656 
5657     if (node.data->len > bufsize - 1) {
5658         buffer[0] = '\0';
5659         mpack_node_flag_error(node, mpack_error_too_big);
5660         return;
5661     }
5662 
5663     if (!mpack_str_check_no_null(mpack_node_data_unchecked(node), node.data->len)) {
5664         buffer[0] = '\0';
5665         mpack_node_flag_error(node, mpack_error_type);
5666         return;
5667     }
5668 
5669     mpack_memcpy(buffer, mpack_node_data_unchecked(node), node.data->len);
5670     buffer[node.data->len] = '\0';
5671 }
5672 
mpack_node_copy_utf8_cstr(mpack_node_t node,char * buffer,size_t bufsize)5673 void mpack_node_copy_utf8_cstr(mpack_node_t node, char* buffer, size_t bufsize) {
5674 
5675     // we can't break here because the error isn't recoverable; we
5676     // have to add a null-terminator.
5677     mpack_assert(buffer != NULL, "buffer is NULL");
5678     mpack_assert(bufsize >= 1, "buffer size is zero; you must have room for at least a null-terminator");
5679 
5680     if (mpack_node_error(node) != mpack_ok) {
5681         buffer[0] = '\0';
5682         return;
5683     }
5684 
5685     if (node.data->type != mpack_type_str) {
5686         buffer[0] = '\0';
5687         mpack_node_flag_error(node, mpack_error_type);
5688         return;
5689     }
5690 
5691     if (node.data->len > bufsize - 1) {
5692         buffer[0] = '\0';
5693         mpack_node_flag_error(node, mpack_error_too_big);
5694         return;
5695     }
5696 
5697     if (!mpack_utf8_check_no_null(mpack_node_data_unchecked(node), node.data->len)) {
5698         buffer[0] = '\0';
5699         mpack_node_flag_error(node, mpack_error_type);
5700         return;
5701     }
5702 
5703     mpack_memcpy(buffer, mpack_node_data_unchecked(node), node.data->len);
5704     buffer[node.data->len] = '\0';
5705 }
5706 
5707 #ifdef MPACK_MALLOC
mpack_node_data_alloc(mpack_node_t node,size_t maxlen)5708 char* mpack_node_data_alloc(mpack_node_t node, size_t maxlen) {
5709     if (mpack_node_error(node) != mpack_ok)
5710         return NULL;
5711 
5712     // make sure this is a valid data type
5713     mpack_type_t type = node.data->type;
5714     if (type != mpack_type_str && type != mpack_type_bin
5715             #if MPACK_EXTENSIONS
5716             && type != mpack_type_ext
5717             #endif
5718     ) {
5719         mpack_node_flag_error(node, mpack_error_type);
5720         return NULL;
5721     }
5722 
5723     if (node.data->len > maxlen) {
5724         mpack_node_flag_error(node, mpack_error_too_big);
5725         return NULL;
5726     }
5727 
5728     char* ret = (char*) MPACK_MALLOC((size_t)node.data->len);
5729     if (ret == NULL) {
5730         mpack_node_flag_error(node, mpack_error_memory);
5731         return NULL;
5732     }
5733 
5734     mpack_memcpy(ret, mpack_node_data_unchecked(node), node.data->len);
5735     return ret;
5736 }
5737 
mpack_node_cstr_alloc(mpack_node_t node,size_t maxlen)5738 char* mpack_node_cstr_alloc(mpack_node_t node, size_t maxlen) {
5739     if (mpack_node_error(node) != mpack_ok)
5740         return NULL;
5741 
5742     // make sure maxlen makes sense
5743     if (maxlen < 1) {
5744         mpack_break("maxlen is zero; you must have room for at least a null-terminator");
5745         mpack_node_flag_error(node, mpack_error_bug);
5746         return NULL;
5747     }
5748 
5749     if (node.data->type != mpack_type_str) {
5750         mpack_node_flag_error(node, mpack_error_type);
5751         return NULL;
5752     }
5753 
5754     if (node.data->len > maxlen - 1) {
5755         mpack_node_flag_error(node, mpack_error_too_big);
5756         return NULL;
5757     }
5758 
5759     if (!mpack_str_check_no_null(mpack_node_data_unchecked(node), node.data->len)) {
5760         mpack_node_flag_error(node, mpack_error_type);
5761         return NULL;
5762     }
5763 
5764     char* ret = (char*) MPACK_MALLOC((size_t)(node.data->len + 1));
5765     if (ret == NULL) {
5766         mpack_node_flag_error(node, mpack_error_memory);
5767         return NULL;
5768     }
5769 
5770     mpack_memcpy(ret, mpack_node_data_unchecked(node), node.data->len);
5771     ret[node.data->len] = '\0';
5772     return ret;
5773 }
5774 
mpack_node_utf8_cstr_alloc(mpack_node_t node,size_t maxlen)5775 char* mpack_node_utf8_cstr_alloc(mpack_node_t node, size_t maxlen) {
5776     if (mpack_node_error(node) != mpack_ok)
5777         return NULL;
5778 
5779     // make sure maxlen makes sense
5780     if (maxlen < 1) {
5781         mpack_break("maxlen is zero; you must have room for at least a null-terminator");
5782         mpack_node_flag_error(node, mpack_error_bug);
5783         return NULL;
5784     }
5785 
5786     if (node.data->type != mpack_type_str) {
5787         mpack_node_flag_error(node, mpack_error_type);
5788         return NULL;
5789     }
5790 
5791     if (node.data->len > maxlen - 1) {
5792         mpack_node_flag_error(node, mpack_error_too_big);
5793         return NULL;
5794     }
5795 
5796     if (!mpack_utf8_check_no_null(mpack_node_data_unchecked(node), node.data->len)) {
5797         mpack_node_flag_error(node, mpack_error_type);
5798         return NULL;
5799     }
5800 
5801     char* ret = (char*) MPACK_MALLOC((size_t)(node.data->len + 1));
5802     if (ret == NULL) {
5803         mpack_node_flag_error(node, mpack_error_memory);
5804         return NULL;
5805     }
5806 
5807     mpack_memcpy(ret, mpack_node_data_unchecked(node), node.data->len);
5808     ret[node.data->len] = '\0';
5809     return ret;
5810 }
5811 #endif
5812 
5813 
5814 /*
5815  * Compound Node Functions
5816  */
5817 
mpack_node_map_int_impl(mpack_node_t node,int64_t num)5818 static mpack_node_data_t* mpack_node_map_int_impl(mpack_node_t node, int64_t num) {
5819     if (mpack_node_error(node) != mpack_ok)
5820         return NULL;
5821 
5822     if (node.data->type != mpack_type_map) {
5823         mpack_node_flag_error(node, mpack_error_type);
5824         return NULL;
5825     }
5826 
5827     mpack_node_data_t* found = NULL;
5828 
5829     for (size_t i = 0; i < node.data->len; ++i) {
5830         mpack_node_data_t* key = mpack_node_child(node, i * 2);
5831 
5832         if ((key->type == mpack_type_int && key->value.i == num) ||
5833             (key->type == mpack_type_uint && num >= 0 && key->value.u == (uint64_t)num))
5834         {
5835             if (found) {
5836                 mpack_node_flag_error(node, mpack_error_data);
5837                 return NULL;
5838             }
5839             found = mpack_node_child(node, i * 2 + 1);
5840         }
5841     }
5842 
5843     if (found)
5844         return found;
5845 
5846     return NULL;
5847 }
5848 
mpack_node_map_uint_impl(mpack_node_t node,uint64_t num)5849 static mpack_node_data_t* mpack_node_map_uint_impl(mpack_node_t node, uint64_t num) {
5850     if (mpack_node_error(node) != mpack_ok)
5851         return NULL;
5852 
5853     if (node.data->type != mpack_type_map) {
5854         mpack_node_flag_error(node, mpack_error_type);
5855         return NULL;
5856     }
5857 
5858     mpack_node_data_t* found = NULL;
5859 
5860     for (size_t i = 0; i < node.data->len; ++i) {
5861         mpack_node_data_t* key = mpack_node_child(node, i * 2);
5862 
5863         if ((key->type == mpack_type_uint && key->value.u == num) ||
5864             (key->type == mpack_type_int && key->value.i >= 0 && (uint64_t)key->value.i == num))
5865         {
5866             if (found) {
5867                 mpack_node_flag_error(node, mpack_error_data);
5868                 return NULL;
5869             }
5870             found = mpack_node_child(node, i * 2 + 1);
5871         }
5872     }
5873 
5874     if (found)
5875         return found;
5876 
5877     return NULL;
5878 }
5879 
mpack_node_map_str_impl(mpack_node_t node,const char * str,size_t length)5880 static mpack_node_data_t* mpack_node_map_str_impl(mpack_node_t node, const char* str, size_t length) {
5881     if (mpack_node_error(node) != mpack_ok)
5882         return NULL;
5883 
5884     mpack_assert(length == 0 || str != NULL, "str of length %i is NULL", (int)length);
5885 
5886     if (node.data->type != mpack_type_map) {
5887         mpack_node_flag_error(node, mpack_error_type);
5888         return NULL;
5889     }
5890 
5891     mpack_tree_t* tree = node.tree;
5892     mpack_node_data_t* found = NULL;
5893 
5894     for (size_t i = 0; i < node.data->len; ++i) {
5895         mpack_node_data_t* key = mpack_node_child(node, i * 2);
5896 
5897         if (key->type == mpack_type_str && key->len == length &&
5898                 mpack_memcmp(str, mpack_node_data_unchecked(mpack_node(tree, key)), length) == 0) {
5899             if (found) {
5900                 mpack_node_flag_error(node, mpack_error_data);
5901                 return NULL;
5902             }
5903             found = mpack_node_child(node, i * 2 + 1);
5904         }
5905     }
5906 
5907     if (found)
5908         return found;
5909 
5910     return NULL;
5911 }
5912 
mpack_node_wrap_lookup(mpack_tree_t * tree,mpack_node_data_t * data)5913 static mpack_node_t mpack_node_wrap_lookup(mpack_tree_t* tree, mpack_node_data_t* data) {
5914     if (!data) {
5915         if (tree->error == mpack_ok)
5916             mpack_tree_flag_error(tree, mpack_error_data);
5917         return mpack_tree_nil_node(tree);
5918     }
5919     return mpack_node(tree, data);
5920 }
5921 
mpack_node_wrap_lookup_optional(mpack_tree_t * tree,mpack_node_data_t * data)5922 static mpack_node_t mpack_node_wrap_lookup_optional(mpack_tree_t* tree, mpack_node_data_t* data) {
5923     if (!data) {
5924         if (tree->error == mpack_ok)
5925             return mpack_tree_missing_node(tree);
5926         return mpack_tree_nil_node(tree);
5927     }
5928     return mpack_node(tree, data);
5929 }
5930 
mpack_node_map_int(mpack_node_t node,int64_t num)5931 mpack_node_t mpack_node_map_int(mpack_node_t node, int64_t num) {
5932     return mpack_node_wrap_lookup(node.tree, mpack_node_map_int_impl(node, num));
5933 }
5934 
mpack_node_map_int_optional(mpack_node_t node,int64_t num)5935 mpack_node_t mpack_node_map_int_optional(mpack_node_t node, int64_t num) {
5936     return mpack_node_wrap_lookup_optional(node.tree, mpack_node_map_int_impl(node, num));
5937 }
5938 
mpack_node_map_uint(mpack_node_t node,uint64_t num)5939 mpack_node_t mpack_node_map_uint(mpack_node_t node, uint64_t num) {
5940     return mpack_node_wrap_lookup(node.tree, mpack_node_map_uint_impl(node, num));
5941 }
5942 
mpack_node_map_uint_optional(mpack_node_t node,uint64_t num)5943 mpack_node_t mpack_node_map_uint_optional(mpack_node_t node, uint64_t num) {
5944     return mpack_node_wrap_lookup_optional(node.tree, mpack_node_map_uint_impl(node, num));
5945 }
5946 
mpack_node_map_str(mpack_node_t node,const char * str,size_t length)5947 mpack_node_t mpack_node_map_str(mpack_node_t node, const char* str, size_t length) {
5948     return mpack_node_wrap_lookup(node.tree, mpack_node_map_str_impl(node, str, length));
5949 }
5950 
mpack_node_map_str_optional(mpack_node_t node,const char * str,size_t length)5951 mpack_node_t mpack_node_map_str_optional(mpack_node_t node, const char* str, size_t length) {
5952     return mpack_node_wrap_lookup_optional(node.tree, mpack_node_map_str_impl(node, str, length));
5953 }
5954 
mpack_node_map_cstr(mpack_node_t node,const char * cstr)5955 mpack_node_t mpack_node_map_cstr(mpack_node_t node, const char* cstr) {
5956     mpack_assert(cstr != NULL, "cstr is NULL");
5957     return mpack_node_map_str(node, cstr, mpack_strlen(cstr));
5958 }
5959 
mpack_node_map_cstr_optional(mpack_node_t node,const char * cstr)5960 mpack_node_t mpack_node_map_cstr_optional(mpack_node_t node, const char* cstr) {
5961     mpack_assert(cstr != NULL, "cstr is NULL");
5962     return mpack_node_map_str_optional(node, cstr, mpack_strlen(cstr));
5963 }
5964 
mpack_node_map_contains_int(mpack_node_t node,int64_t num)5965 bool mpack_node_map_contains_int(mpack_node_t node, int64_t num) {
5966     return mpack_node_map_int_impl(node, num) != NULL;
5967 }
5968 
mpack_node_map_contains_uint(mpack_node_t node,uint64_t num)5969 bool mpack_node_map_contains_uint(mpack_node_t node, uint64_t num) {
5970     return mpack_node_map_uint_impl(node, num) != NULL;
5971 }
5972 
mpack_node_map_contains_str(mpack_node_t node,const char * str,size_t length)5973 bool mpack_node_map_contains_str(mpack_node_t node, const char* str, size_t length) {
5974     return mpack_node_map_str_impl(node, str, length) != NULL;
5975 }
5976 
mpack_node_map_contains_cstr(mpack_node_t node,const char * cstr)5977 bool mpack_node_map_contains_cstr(mpack_node_t node, const char* cstr) {
5978     mpack_assert(cstr != NULL, "cstr is NULL");
5979     return mpack_node_map_contains_str(node, cstr, mpack_strlen(cstr));
5980 }
5981 
mpack_node_enum_optional(mpack_node_t node,const char * strings[],size_t count)5982 size_t mpack_node_enum_optional(mpack_node_t node, const char* strings[], size_t count) {
5983     if (mpack_node_error(node) != mpack_ok)
5984         return count;
5985 
5986     // the value is only recognized if it is a string
5987     if (mpack_node_type(node) != mpack_type_str)
5988         return count;
5989 
5990     // fetch the string
5991     const char* key = mpack_node_str(node);
5992     size_t keylen = mpack_node_strlen(node);
5993     mpack_assert(mpack_node_error(node) == mpack_ok, "these should not fail");
5994 
5995     // find what key it matches
5996     for (size_t i = 0; i < count; ++i) {
5997         const char* other = strings[i];
5998         size_t otherlen = mpack_strlen(other);
5999         if (keylen == otherlen && mpack_memcmp(key, other, keylen) == 0)
6000             return i;
6001     }
6002 
6003     // no matches
6004     return count;
6005 }
6006 
mpack_node_enum(mpack_node_t node,const char * strings[],size_t count)6007 size_t mpack_node_enum(mpack_node_t node, const char* strings[], size_t count) {
6008     size_t value = mpack_node_enum_optional(node, strings, count);
6009     if (value == count)
6010         mpack_node_flag_error(node, mpack_error_type);
6011     return value;
6012 }
6013 
mpack_node_type(mpack_node_t node)6014 mpack_type_t mpack_node_type(mpack_node_t node) {
6015     if (mpack_node_error(node) != mpack_ok)
6016         return mpack_type_nil;
6017     return node.data->type;
6018 }
6019 
mpack_node_is_nil(mpack_node_t node)6020 bool mpack_node_is_nil(mpack_node_t node) {
6021     if (mpack_node_error(node) != mpack_ok) {
6022         // All nodes are treated as nil nodes when we are in error.
6023         return true;
6024     }
6025     return node.data->type == mpack_type_nil;
6026 }
6027 
mpack_node_is_missing(mpack_node_t node)6028 bool mpack_node_is_missing(mpack_node_t node) {
6029     if (mpack_node_error(node) != mpack_ok) {
6030         // errors still return nil nodes, not missing nodes.
6031         return false;
6032     }
6033     return node.data->type == mpack_type_missing;
6034 }
6035 
mpack_node_nil(mpack_node_t node)6036 void mpack_node_nil(mpack_node_t node) {
6037     if (mpack_node_error(node) != mpack_ok)
6038         return;
6039     if (node.data->type != mpack_type_nil)
6040         mpack_node_flag_error(node, mpack_error_type);
6041 }
6042 
mpack_node_missing(mpack_node_t node)6043 void mpack_node_missing(mpack_node_t node) {
6044     if (mpack_node_error(node) != mpack_ok)
6045         return;
6046     if (node.data->type != mpack_type_missing)
6047         mpack_node_flag_error(node, mpack_error_type);
6048 }
6049 
mpack_node_bool(mpack_node_t node)6050 bool mpack_node_bool(mpack_node_t node) {
6051     if (mpack_node_error(node) != mpack_ok)
6052         return false;
6053 
6054     if (node.data->type == mpack_type_bool)
6055         return node.data->value.b;
6056 
6057     mpack_node_flag_error(node, mpack_error_type);
6058     return false;
6059 }
6060 
mpack_node_true(mpack_node_t node)6061 void mpack_node_true(mpack_node_t node) {
6062     if (mpack_node_bool(node) != true)
6063         mpack_node_flag_error(node, mpack_error_type);
6064 }
6065 
mpack_node_false(mpack_node_t node)6066 void mpack_node_false(mpack_node_t node) {
6067     if (mpack_node_bool(node) != false)
6068         mpack_node_flag_error(node, mpack_error_type);
6069 }
6070 
mpack_node_u8(mpack_node_t node)6071 uint8_t mpack_node_u8(mpack_node_t node) {
6072     if (mpack_node_error(node) != mpack_ok)
6073         return 0;
6074 
6075     if (node.data->type == mpack_type_uint) {
6076         if (node.data->value.u <= UINT8_MAX)
6077             return (uint8_t)node.data->value.u;
6078     } else if (node.data->type == mpack_type_int) {
6079         if (node.data->value.i >= 0 && node.data->value.i <= UINT8_MAX)
6080             return (uint8_t)node.data->value.i;
6081     }
6082 
6083     mpack_node_flag_error(node, mpack_error_type);
6084     return 0;
6085 }
6086 
mpack_node_i8(mpack_node_t node)6087 int8_t mpack_node_i8(mpack_node_t node) {
6088     if (mpack_node_error(node) != mpack_ok)
6089         return 0;
6090 
6091     if (node.data->type == mpack_type_uint) {
6092         if (node.data->value.u <= INT8_MAX)
6093             return (int8_t)node.data->value.u;
6094     } else if (node.data->type == mpack_type_int) {
6095         if (node.data->value.i >= INT8_MIN && node.data->value.i <= INT8_MAX)
6096             return (int8_t)node.data->value.i;
6097     }
6098 
6099     mpack_node_flag_error(node, mpack_error_type);
6100     return 0;
6101 }
6102 
mpack_node_u16(mpack_node_t node)6103 uint16_t mpack_node_u16(mpack_node_t node) {
6104     if (mpack_node_error(node) != mpack_ok)
6105         return 0;
6106 
6107     if (node.data->type == mpack_type_uint) {
6108         if (node.data->value.u <= UINT16_MAX)
6109             return (uint16_t)node.data->value.u;
6110     } else if (node.data->type == mpack_type_int) {
6111         if (node.data->value.i >= 0 && node.data->value.i <= UINT16_MAX)
6112             return (uint16_t)node.data->value.i;
6113     }
6114 
6115     mpack_node_flag_error(node, mpack_error_type);
6116     return 0;
6117 }
6118 
mpack_node_i16(mpack_node_t node)6119 int16_t mpack_node_i16(mpack_node_t node) {
6120     if (mpack_node_error(node) != mpack_ok)
6121         return 0;
6122 
6123     if (node.data->type == mpack_type_uint) {
6124         if (node.data->value.u <= INT16_MAX)
6125             return (int16_t)node.data->value.u;
6126     } else if (node.data->type == mpack_type_int) {
6127         if (node.data->value.i >= INT16_MIN && node.data->value.i <= INT16_MAX)
6128             return (int16_t)node.data->value.i;
6129     }
6130 
6131     mpack_node_flag_error(node, mpack_error_type);
6132     return 0;
6133 }
6134 
mpack_node_u32(mpack_node_t node)6135 uint32_t mpack_node_u32(mpack_node_t node) {
6136     if (mpack_node_error(node) != mpack_ok)
6137         return 0;
6138 
6139     if (node.data->type == mpack_type_uint) {
6140         if (node.data->value.u <= UINT32_MAX)
6141             return (uint32_t)node.data->value.u;
6142     } else if (node.data->type == mpack_type_int) {
6143         if (node.data->value.i >= 0 && node.data->value.i <= UINT32_MAX)
6144             return (uint32_t)node.data->value.i;
6145     }
6146 
6147     mpack_node_flag_error(node, mpack_error_type);
6148     return 0;
6149 }
6150 
mpack_node_i32(mpack_node_t node)6151 int32_t mpack_node_i32(mpack_node_t node) {
6152     if (mpack_node_error(node) != mpack_ok)
6153         return 0;
6154 
6155     if (node.data->type == mpack_type_uint) {
6156         if (node.data->value.u <= INT32_MAX)
6157             return (int32_t)node.data->value.u;
6158     } else if (node.data->type == mpack_type_int) {
6159         if (node.data->value.i >= INT32_MIN && node.data->value.i <= INT32_MAX)
6160             return (int32_t)node.data->value.i;
6161     }
6162 
6163     mpack_node_flag_error(node, mpack_error_type);
6164     return 0;
6165 }
6166 
mpack_node_u64(mpack_node_t node)6167 uint64_t mpack_node_u64(mpack_node_t node) {
6168     if (mpack_node_error(node) != mpack_ok)
6169         return 0;
6170 
6171     if (node.data->type == mpack_type_uint) {
6172         return node.data->value.u;
6173     } else if (node.data->type == mpack_type_int) {
6174         if (node.data->value.i >= 0)
6175             return (uint64_t)node.data->value.i;
6176     }
6177 
6178     mpack_node_flag_error(node, mpack_error_type);
6179     return 0;
6180 }
6181 
mpack_node_i64(mpack_node_t node)6182 int64_t mpack_node_i64(mpack_node_t node) {
6183     if (mpack_node_error(node) != mpack_ok)
6184         return 0;
6185 
6186     if (node.data->type == mpack_type_uint) {
6187         if (node.data->value.u <= (uint64_t)INT64_MAX)
6188             return (int64_t)node.data->value.u;
6189     } else if (node.data->type == mpack_type_int) {
6190         return node.data->value.i;
6191     }
6192 
6193     mpack_node_flag_error(node, mpack_error_type);
6194     return 0;
6195 }
6196 
mpack_node_uint(mpack_node_t node)6197 unsigned int mpack_node_uint(mpack_node_t node) {
6198 
6199     // This should be true at compile-time, so this just wraps the 32-bit function.
6200     if (sizeof(unsigned int) == 4)
6201         return (unsigned int)mpack_node_u32(node);
6202 
6203     // Otherwise we use u64 and check the range.
6204     uint64_t val = mpack_node_u64(node);
6205     if (val <= UINT_MAX)
6206         return (unsigned int)val;
6207 
6208     mpack_node_flag_error(node, mpack_error_type);
6209     return 0;
6210 }
6211 
mpack_node_int(mpack_node_t node)6212 int mpack_node_int(mpack_node_t node) {
6213 
6214     // This should be true at compile-time, so this just wraps the 32-bit function.
6215     if (sizeof(int) == 4)
6216         return (int)mpack_node_i32(node);
6217 
6218     // Otherwise we use i64 and check the range.
6219     int64_t val = mpack_node_i64(node);
6220     if (val >= INT_MIN && val <= INT_MAX)
6221         return (int)val;
6222 
6223     mpack_node_flag_error(node, mpack_error_type);
6224     return 0;
6225 }
6226 
mpack_node_float(mpack_node_t node)6227 float mpack_node_float(mpack_node_t node) {
6228     if (mpack_node_error(node) != mpack_ok)
6229         return 0.0f;
6230 
6231     if (node.data->type == mpack_type_uint)
6232         return (float)node.data->value.u;
6233     else if (node.data->type == mpack_type_int)
6234         return (float)node.data->value.i;
6235     else if (node.data->type == mpack_type_float)
6236         return node.data->value.f;
6237     else if (node.data->type == mpack_type_double)
6238         return (float)node.data->value.d;
6239 
6240     mpack_node_flag_error(node, mpack_error_type);
6241     return 0.0f;
6242 }
6243 
mpack_node_double(mpack_node_t node)6244 double mpack_node_double(mpack_node_t node) {
6245     if (mpack_node_error(node) != mpack_ok)
6246         return 0.0;
6247 
6248     if (node.data->type == mpack_type_uint)
6249         return (double)node.data->value.u;
6250     else if (node.data->type == mpack_type_int)
6251         return (double)node.data->value.i;
6252     else if (node.data->type == mpack_type_float)
6253         return (double)node.data->value.f;
6254     else if (node.data->type == mpack_type_double)
6255         return node.data->value.d;
6256 
6257     mpack_node_flag_error(node, mpack_error_type);
6258     return 0.0;
6259 }
6260 
mpack_node_float_strict(mpack_node_t node)6261 float mpack_node_float_strict(mpack_node_t node) {
6262     if (mpack_node_error(node) != mpack_ok)
6263         return 0.0f;
6264 
6265     if (node.data->type == mpack_type_float)
6266         return node.data->value.f;
6267 
6268     mpack_node_flag_error(node, mpack_error_type);
6269     return 0.0f;
6270 }
6271 
mpack_node_double_strict(mpack_node_t node)6272 double mpack_node_double_strict(mpack_node_t node) {
6273     if (mpack_node_error(node) != mpack_ok)
6274         return 0.0;
6275 
6276     if (node.data->type == mpack_type_float)
6277         return (double)node.data->value.f;
6278     else if (node.data->type == mpack_type_double)
6279         return node.data->value.d;
6280 
6281     mpack_node_flag_error(node, mpack_error_type);
6282     return 0.0;
6283 }
6284 
6285 #if MPACK_EXTENSIONS
mpack_node_exttype(mpack_node_t node)6286 int8_t mpack_node_exttype(mpack_node_t node) {
6287     if (mpack_node_error(node) != mpack_ok)
6288         return 0;
6289 
6290     if (node.data->type == mpack_type_ext)
6291         return mpack_node_exttype_unchecked(node);
6292 
6293     mpack_node_flag_error(node, mpack_error_type);
6294     return 0;
6295 }
6296 #endif
6297 
mpack_node_data_len(mpack_node_t node)6298 uint32_t mpack_node_data_len(mpack_node_t node) {
6299     if (mpack_node_error(node) != mpack_ok)
6300         return 0;
6301 
6302     mpack_type_t type = node.data->type;
6303     if (type == mpack_type_str || type == mpack_type_bin
6304             #if MPACK_EXTENSIONS
6305             || type == mpack_type_ext
6306             #endif
6307             )
6308         return (uint32_t)node.data->len;
6309 
6310     mpack_node_flag_error(node, mpack_error_type);
6311     return 0;
6312 }
6313 
mpack_node_strlen(mpack_node_t node)6314 size_t mpack_node_strlen(mpack_node_t node) {
6315     if (mpack_node_error(node) != mpack_ok)
6316         return 0;
6317 
6318     if (node.data->type == mpack_type_str)
6319         return (size_t)node.data->len;
6320 
6321     mpack_node_flag_error(node, mpack_error_type);
6322     return 0;
6323 }
6324 
mpack_node_str(mpack_node_t node)6325 const char* mpack_node_str(mpack_node_t node) {
6326     if (mpack_node_error(node) != mpack_ok)
6327         return NULL;
6328 
6329     mpack_type_t type = node.data->type;
6330     if (type == mpack_type_str)
6331         return mpack_node_data_unchecked(node);
6332 
6333     mpack_node_flag_error(node, mpack_error_type);
6334     return NULL;
6335 }
6336 
mpack_node_data(mpack_node_t node)6337 const char* mpack_node_data(mpack_node_t node) {
6338     if (mpack_node_error(node) != mpack_ok)
6339         return NULL;
6340 
6341     mpack_type_t type = node.data->type;
6342     if (type == mpack_type_str || type == mpack_type_bin
6343             #if MPACK_EXTENSIONS
6344             || type == mpack_type_ext
6345             #endif
6346             )
6347         return mpack_node_data_unchecked(node);
6348 
6349     mpack_node_flag_error(node, mpack_error_type);
6350     return NULL;
6351 }
6352 
mpack_node_bin_data(mpack_node_t node)6353 const char* mpack_node_bin_data(mpack_node_t node) {
6354     if (mpack_node_error(node) != mpack_ok)
6355         return NULL;
6356 
6357     if (node.data->type == mpack_type_bin)
6358         return mpack_node_data_unchecked(node);
6359 
6360     mpack_node_flag_error(node, mpack_error_type);
6361     return NULL;
6362 }
6363 
mpack_node_bin_size(mpack_node_t node)6364 size_t mpack_node_bin_size(mpack_node_t node) {
6365     if (mpack_node_error(node) != mpack_ok)
6366         return 0;
6367 
6368     if (node.data->type == mpack_type_bin)
6369         return (size_t)node.data->len;
6370 
6371     mpack_node_flag_error(node, mpack_error_type);
6372     return 0;
6373 }
6374 
mpack_node_array_length(mpack_node_t node)6375 size_t mpack_node_array_length(mpack_node_t node) {
6376     if (mpack_node_error(node) != mpack_ok)
6377         return 0;
6378 
6379     if (node.data->type != mpack_type_array) {
6380         mpack_node_flag_error(node, mpack_error_type);
6381         return 0;
6382     }
6383 
6384     return (size_t)node.data->len;
6385 }
6386 
mpack_node_array_at(mpack_node_t node,size_t index)6387 mpack_node_t mpack_node_array_at(mpack_node_t node, size_t index) {
6388     if (mpack_node_error(node) != mpack_ok)
6389         return mpack_tree_nil_node(node.tree);
6390 
6391     if (node.data->type != mpack_type_array) {
6392         mpack_node_flag_error(node, mpack_error_type);
6393         return mpack_tree_nil_node(node.tree);
6394     }
6395 
6396     if (index >= node.data->len) {
6397         mpack_node_flag_error(node, mpack_error_data);
6398         return mpack_tree_nil_node(node.tree);
6399     }
6400 
6401     return mpack_node(node.tree, mpack_node_child(node, index));
6402 }
6403 
mpack_node_map_count(mpack_node_t node)6404 size_t mpack_node_map_count(mpack_node_t node) {
6405     if (mpack_node_error(node) != mpack_ok)
6406         return 0;
6407 
6408     if (node.data->type != mpack_type_map) {
6409         mpack_node_flag_error(node, mpack_error_type);
6410         return 0;
6411     }
6412 
6413     return node.data->len;
6414 }
6415 
6416 // internal node map lookup
mpack_node_map_at(mpack_node_t node,size_t index,size_t offset)6417 static mpack_node_t mpack_node_map_at(mpack_node_t node, size_t index, size_t offset) {
6418     if (mpack_node_error(node) != mpack_ok)
6419         return mpack_tree_nil_node(node.tree);
6420 
6421     if (node.data->type != mpack_type_map) {
6422         mpack_node_flag_error(node, mpack_error_type);
6423         return mpack_tree_nil_node(node.tree);
6424     }
6425 
6426     if (index >= node.data->len) {
6427         mpack_node_flag_error(node, mpack_error_data);
6428         return mpack_tree_nil_node(node.tree);
6429     }
6430 
6431     return mpack_node(node.tree, mpack_node_child(node, index * 2 + offset));
6432 }
6433 
mpack_node_map_key_at(mpack_node_t node,size_t index)6434 mpack_node_t mpack_node_map_key_at(mpack_node_t node, size_t index) {
6435     return mpack_node_map_at(node, index, 0);
6436 }
6437 
mpack_node_map_value_at(mpack_node_t node,size_t index)6438 mpack_node_t mpack_node_map_value_at(mpack_node_t node, size_t index) {
6439     return mpack_node_map_at(node, index, 1);
6440 }
6441 
6442 #endif
6443