robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
353a4be7e8
master
1#include <string.h> 2 3#include "object.h" 4 5static int mpack_parser_full (mpack_parser_t * w ); 6static mpack_node_t * mpack_parser_push (mpack_parser_t * w ); 7static mpack_node_t * mpack_parser_pop (mpack_parser_t * w ); 8 9MPACK_API void mpack_parser_init (mpack_parser_t * parser , 10mpack_uint32_t capacity ) 11{ 12mpack_tokbuf_init (& parser -> tokbuf ); 13parser -> data .p = NULL ; 14parser -> capacity = capacity ?capacity :MPACK_MAX_OBJECT_DEPTH ; 15parser -> size = 0 ; 16parser -> exiting = 0 ; 17memset (parser -> items ,0 ,sizeof (mpack_node_t )* (parser -> capacity + 1 )); 18parser -> items [0 ].pos = (size_t )-1 ; 19parser -> status = 0 ; 20} 21 22#define MPACK_EXCEPTION_CHECK (parser ) \ 23 do { \ 24 if (parser->status == MPACK_EXCEPTION) { \ 25 return MPACK_EXCEPTION; \ 26 } \ 27 } while (0) 28 29#define MPACK_WALK (action ) \ 30 do { \ 31 mpack_node_t *n; \ 32 \ 33 if (parser->exiting) goto exit; \ 34 if (mpack_parser_full(parser)) return MPACK_NOMEM; \ 35 n = mpack_parser_push(parser); \ 36 action; \ 37 MPACK_EXCEPTION_CHECK(parser); \ 38 parser->exiting = 1; \ 39 return MPACK_EOF; \ 40 \ 41exit: \ 42 parser->exiting = 0; \ 43 while ((n = mpack_parser_pop(parser))) { \ 44 exit_cb(parser, n); \ 45 MPACK_EXCEPTION_CHECK(parser); \ 46 if (!parser->size) return MPACK_OK; \ 47 } \ 48 \ 49 return MPACK_EOF; \ 50 } while (0) 51 52MPACK_API int mpack_parse_tok (mpack_parser_t * parser ,mpack_token_t tok , 53mpack_walk_cb enter_cb ,mpack_walk_cb exit_cb ) 54{ 55MPACK_EXCEPTION_CHECK (parser ); 56MPACK_WALK ({n -> tok = tok ;enter_cb (parser ,n );}); 57} 58 59MPACK_API int mpack_unparse_tok (mpack_parser_t * parser ,mpack_token_t * tok , 60mpack_walk_cb enter_cb ,mpack_walk_cb exit_cb ) 61{ 62MPACK_EXCEPTION_CHECK (parser ); 63MPACK_WALK ({enter_cb (parser ,n );* tok = n -> tok ;}); 64} 65 66MPACK_API int mpack_parse (mpack_parser_t * parser ,const char ** buf , 67size_t * buflen ,mpack_walk_cb enter_cb ,mpack_walk_cb exit_cb ) 68{ 69int status = MPACK_EOF ; 70MPACK_EXCEPTION_CHECK (parser ); 71 72while (* buflen && status ) { 73mpack_token_t tok ; 74mpack_tokbuf_t * tb = & parser -> tokbuf ; 75const char * buf_save = * buf ; 76size_t buflen_save = * buflen ; 77 78if ((status = mpack_read (tb ,buf ,buflen ,& tok ))== MPACK_EOF )continue ; 79else if (status == MPACK_ERROR ) gotorollback ; 80 81do { 82status = mpack_parse_tok (parser ,tok ,enter_cb ,exit_cb ); 83MPACK_EXCEPTION_CHECK (parser ); 84 }while (parser -> exiting ); 85 86if (status != MPACK_NOMEM )continue ; 87 88rollback : 89/* restore buf/buflen so the next call will try to read the same token */ 90* buf = buf_save ; 91* buflen = buflen_save ; 92break ; 93 } 94 95return status ; 96} 97 98MPACK_API int mpack_unparse (mpack_parser_t * parser ,char ** buf ,size_t * buflen , 99mpack_walk_cb enter_cb ,mpack_walk_cb exit_cb ) 100{ 101int status = MPACK_EOF ; 102MPACK_EXCEPTION_CHECK (parser ); 103 104while (* buflen && status ) { 105int write_status ; 106mpack_token_t tok ; 107mpack_tokbuf_t * tb = & parser -> tokbuf ; 108 109if (!tb -> plen ) 110parser -> status = mpack_unparse_tok (parser ,& tok ,enter_cb ,exit_cb ); 111 112MPACK_EXCEPTION_CHECK (parser ); 113 114status = parser -> status ; 115 116if (status == MPACK_NOMEM ) 117break ; 118 119if (parser -> exiting ) { 120write_status = mpack_write (tb ,buf ,buflen ,& tok ); 121status = write_status ?write_status :status ; 122 } 123 } 124 125return status ; 126} 127 128MPACK_API void mpack_parser_copy (mpack_parser_t * d ,mpack_parser_t * s ) 129{ 130// workaround UBSAN being NOT happy with a flexible array member with arr[N>1] initial size 131mpack_one_parser_t * dst = (mpack_one_parser_t * )d ; 132mpack_one_parser_t * src = (mpack_one_parser_t * )s ; 133mpack_uint32_t i ; 134mpack_uint32_t dst_capacity = dst -> capacity ; 135assert (src -> capacity <=dst_capacity ); 136/* copy all fields except the stack */ 137memcpy (dst ,src ,sizeof (mpack_one_parser_t )- sizeof (mpack_node_t )); 138/* reset capacity */ 139dst -> capacity = dst_capacity ; 140/* copy the stack */ 141for (i = 0 ;i <=src -> capacity ;i ++ ) { 142dst -> items [i ]= src -> items [i ]; 143 } 144} 145 146static int mpack_parser_full (mpack_parser_t * parser ) 147{ 148return parser -> size == parser -> capacity ; 149} 150 151static mpack_node_t * mpack_parser_push (mpack_parser_t * p ) 152{ 153mpack_one_parser_t * parser = (mpack_one_parser_t * )p ; 154mpack_node_t * top ; 155assert (parser -> size < parser -> capacity ); 156top = parser -> items + parser -> size + 1 ; 157top -> data [0 ].p = NULL ; 158top -> data [1 ].p = NULL ; 159top -> pos = 0 ; 160top -> key_visited = 0 ; 161/* increase size and invoke callback, passing parent node if any */ 162parser -> size ++ ; 163return top ; 164} 165 166static mpack_node_t * mpack_parser_pop (mpack_parser_t * p ) 167{ 168mpack_one_parser_t * parser = (mpack_one_parser_t * )p ; 169mpack_node_t * top ,* parent ; 170assert (parser -> size ); 171top = parser -> items + parser -> size ; 172 173if (top -> tok .type > MPACK_TOKEN_CHUNK && top -> pos < top -> tok .length ) { 174/* continue processing children */ 175return NULL ; 176 } 177 178parent = MPACK_PARENT_NODE (top ); 179if (parent ) { 180/* we use parent->tok.length to keep track of how many children remain. 181* update it to reflect the processed node. */ 182if (top -> tok .type == MPACK_TOKEN_CHUNK ) { 183parent -> pos += top -> tok .length ; 184 }else if (parent -> tok .type == MPACK_TOKEN_MAP ) { 185/* maps allow up to 2^32 - 1 pairs, so to allow this many items in a 186* 32-bit length variable we use an additional flag to determine if the 187* key of a certain position was visited */ 188if (parent -> key_visited ) { 189parent -> pos ++ ; 190 } 191parent -> key_visited = !parent -> key_visited ; 192 }else { 193parent -> pos ++ ; 194 } 195 } 196 197parser -> size -- ; 198return top ; 199} 200