1515 ********************************************************************************/
1616
1717#include "metadata_reader.h"
18-
19- #include <stdbool.h>
20- #include <stddef.h>
21- #include <stdint.h>
22-
2318#include "borsh.h"
24- #include "parser_common.h"
25- #include "parser_impl.h"
26- #include "parser_txdef.h"
27- #include "zxmacros.h"
2819
2920#if defined(TARGET_NANOS ) || defined(TARGET_NANOX ) || defined(TARGET_NANOS2 ) || defined(TARGET_STAX ) || defined(TARGET_FLEX )
3021#define STACK_SHIFT 20
@@ -78,21 +69,21 @@ parser_error_t read_fixed_point_display(parser_context_t *ctx, fixed_point_displ
7869
7970 // read type
8071 CHECK_ERROR (read_u8 (ctx , (uint8_t * )& display -> type ));
81- printf ("type fixed point display: %d\n " , display -> type );
72+ print_u8 ("type fixed point display:" , display -> type );
8273
8374 switch (display -> type ) {
8475 case FIXED_POINT_DISPLAY_DECIMALS :
8576 CHECK_ERROR (read_u8 (ctx , (uint8_t * )& display -> decimals ));
86- printf ("fixed point display decimals: %d\n " , display -> decimals );
77+ print_u8 ("fixed point display decimals:" , display -> decimals );
8778 break ;
8879 case FIXED_POINT_DISPLAY_FROM_SIBLING_FIELD :
8980 CHECK_ERROR (read_u64 (ctx , (uint64_t * )& display -> from_sibling_field .field_index ));
9081 CHECK_ERROR (read_u64 (ctx , (uint64_t * )& display -> from_sibling_field .byte_offset ));
91- printf ("fixed point display from sibling field: %llu\n " , display -> from_sibling_field .field_index );
92- printf ("fixed point display from sibling field: %llu\n " , display -> from_sibling_field .byte_offset );
82+ print_u64 ("fixed point display from sibling field:" , display -> from_sibling_field .field_index );
83+ print_u64 ("fixed point display from sibling field:" , display -> from_sibling_field .byte_offset );
9384 break ;
9485 default :
95- printf ("fixed point display unknown!!!!" );
86+ print_string ("fixed point display unknown!!!!" );
9687 return parser_no_data ;
9788 }
9889
@@ -105,21 +96,21 @@ parser_error_t read_integer_display(parser_context_t *ctx, integer_display_t *di
10596
10697 // read type
10798 CHECK_ERROR (read_u8 (ctx , (uint8_t * )& display -> type ));
108- printf ("type integer display: %d\n " , display -> type );
99+ print_u8 ("type integer display:" , display -> type );
109100
110101 switch (display -> type ) {
111102 case INTEGER_DISPLAY_HEX :
112- printf ("integer display hex\n " );
103+ print_string ("integer display hex" );
113104 break ;
114105 case INTEGER_DISPLAY_DECIMAL :
115- printf ("integer display decimal\n " );
106+ print_string ("integer display decimal" );
116107 break ;
117108 case INTEGER_DISPLAY_FIXED_POINT :
118- printf ("integer display fixed point\n " );
109+ print_string ("integer display fixed point" );
119110 CHECK_ERROR (read_fixed_point_display (ctx , & display -> fixed_point ));
120111 break ;
121112 default :
122- printf ("integer display unknown!!!!" );
113+ print_string ("integer display unknown!!!!" );
123114 return parser_no_data ;
124115 }
125116 return parser_ok ;
@@ -130,7 +121,7 @@ parser_error_t read_hrp(parser_context_t *ctx, hrp_t *hrp) {
130121 CHECK_INPUT (ctx );
131122
132123 CHECK_ERROR (read_u32 (ctx , (uint32_t * )& hrp -> prefix .len ));
133- printf ("hrp size: %d\n " , hrp -> prefix .len );
124+ print_u16 ("hrp size:" , hrp -> prefix .len );
134125 if (hrp -> prefix .len > 0 ) {
135126 hrp -> prefix .ptr = ctx -> buffer + ctx -> offset ;
136127 CTX_CHECK_AND_ADVANCE (ctx , hrp -> prefix .len );
@@ -147,28 +138,28 @@ parser_error_t read_byte_display(parser_context_t *ctx, byte_display_t *display)
147138
148139 // read type
149140 CHECK_ERROR (read_u8 (ctx , (uint8_t * )& display -> type ));
150- printf ("type byte display: %d\n " , display -> type );
141+ print_u8 ("type byte display:" , display -> type );
151142
152143 switch (display -> type ) {
153144 case BYTE_DISPLAY_HEX :
154- printf ("byte display hex\n " );
145+ print_string ("byte display hex" );
155146 break ;
156147 case BYTE_DISPLAY_DECIMAL :
157- printf ("byte display decimal\n " );
148+ print_string ("byte display decimal" );
158149 break ;
159150 case BYTE_DISPLAY_BECH32 :
160- printf ("byte display bech32\n " );
151+ print_string ("byte display bech32" );
161152 CHECK_ERROR (read_hrp (ctx , & display -> bech32 .prefix ));
162153 break ;
163154 case BYTE_DISPLAY_BECH32M :
164- printf ("byte display bech32m\n " );
155+ print_string ("byte display bech32m" );
165156 CHECK_ERROR (read_hrp (ctx , & display -> bech32m .prefix ));
166157 break ;
167158 case BYTE_DISPLAY_BASE58 :
168- printf ("byte display base58\n " );
159+ print_string ("byte display base58" );
169160 break ;
170161 default :
171- printf ("byte display unknown!!!!" );
162+ print_string ("byte display unknown!!!!" );
172163 return parser_no_data ;
173164 }
174165
@@ -181,7 +172,7 @@ parser_error_t read_primitive_integer(parser_context_t *ctx, primitive_integer_t
181172
182173 // read type
183174 CHECK_ERROR (read_u8 (ctx , (uint8_t * )& primitive -> type ));
184- printf ("type primitive integer: %d\n " , primitive -> type );
175+ print_u8 ("type primitive integer:" , primitive -> type );
185176
186177 // read display
187178 CHECK_ERROR (read_integer_display (ctx , & primitive -> display ));
@@ -195,7 +186,7 @@ parser_error_t read_primitive_byte_array(parser_context_t *ctx, primitive_byte_a
195186
196187 // read length
197188 CHECK_ERROR (read_u64 (ctx , (uint64_t * )& primitive -> len ));
198- printf ("length primitive byte array: %llu\n " , primitive -> len );
189+ print_u64 ("length primitive byte array:" , primitive -> len );
199190
200191 // read display
201192 CHECK_ERROR (read_byte_display (ctx , & primitive -> display ));
@@ -209,7 +200,7 @@ parser_error_t read_immediate(parser_context_t *ctx, primitive_t *primitive) {
209200
210201 // read type
211202 CHECK_ERROR (read_u8 (ctx , (uint8_t * )& primitive -> type ));
212- printf ("type immediate: %d\n " , primitive -> type );
203+ print_u8 ("type immediate:" , primitive -> type );
213204
214205 // read data
215206 switch (primitive -> type ) {
@@ -241,7 +232,7 @@ parser_error_t read_immediate(parser_context_t *ctx, primitive_t *primitive) {
241232 print_string ("Primitive skip" );
242233 break ;
243234 default :
244- printf ("link unknown!!!!" );
235+ print_string ("link unknown!!!!" );
245236 return parser_no_data ;
246237 }
247238 return parser_ok ;
@@ -253,20 +244,20 @@ parser_error_t read_link(parser_context_t *ctx, link_t *link) {
253244
254245 // read tag
255246 CHECK_ERROR (read_u8 (ctx , (uint8_t * )& link -> tag ));
256- printf ("link tag: %d\n " , link -> tag );
247+ print_u8 ("link tag:" , link -> tag );
257248
258249 // read data
259250 switch (link -> tag ) {
260251 case LINK_BY_INDEX :
261252 CHECK_ERROR (read_u64 (ctx , (uint64_t * )& link -> data .by_index ));
262- printf ("link by index: %llu\n " , link -> data .by_index );
253+ print_u64 ("link by index:" , link -> data .by_index );
263254 break ;
264255 case LINK_IMMEDIATE :
265256 CHECK_ERROR (read_immediate (ctx , & link -> data .immediate ));
266257 break ;
267258 default :
268259 // TODO: change this
269- printf ("link unknown!!!!" );
260+ print_string ("link unknown!!!!" );
270261 return parser_no_data ;
271262 }
272263
@@ -288,11 +279,11 @@ parser_error_t read_enum_variant(parser_context_t *ctx, enum_variant_t *variant)
288279
289280 // read discriminant
290281 CHECK_ERROR (read_u8 (ctx , (uint8_t * )& variant -> discriminant ));
291- printf ("discriminant: %d\n " , variant -> discriminant );
282+ print_u8 ("discriminant:" , variant -> discriminant );
292283
293284 // read has_value
294285 CHECK_ERROR (read_u8 (ctx , (uint8_t * )& variant -> has_value ));
295- printf ("has_value: %d\n " , variant -> has_value );
286+ print_u8 ("has_value:" , variant -> has_value );
296287 if (variant -> has_value ) {
297288 // read value
298289 CHECK_ERROR (read_link (ctx , & variant -> value ));
@@ -316,7 +307,7 @@ parser_error_t read_named_field(parser_context_t *ctx, named_field_t *field) {
316307
317308 // read silent
318309 CHECK_ERROR (read_u8 (ctx , (uint8_t * )& field -> silent ));
319- printf ("silent: %d\n " , field -> silent );
310+ print_u8 ("silent:" , field -> silent );
320311
321312 // read value
322313 CHECK_ERROR (read_link (ctx , & field -> value ));
@@ -341,7 +332,7 @@ parser_error_t read_unnamed_field(parser_context_t *ctx, unnamed_field_t *field)
341332
342333 // read silent
343334 CHECK_ERROR (read_u8 (ctx , (uint8_t * )& field -> silent ));
344- printf ("silent: %d\n " , field -> silent );
335+ print_u8 ("silent:" , field -> silent );
345336
346337 // read doc
347338 CHECK_ERROR (read_u32 (ctx , (uint32_t * )& field -> doc .len ));
@@ -369,7 +360,7 @@ parser_error_t read_enum(parser_context_t *ctx, schema_enum_t *schema_enum) {
369360
370361 // read variants_qty
371362 CHECK_ERROR (read_u32 (ctx , & schema_enum -> variants_qty ));
372- printf ("variants_qty: %d\n " , schema_enum -> variants_qty );
363+ print_u32 ("variants_qty:" , schema_enum -> variants_qty );
373364 if (schema_enum -> variants_qty > MAX_VARIANTS_QTY ) {
374365 return parser_too_many_variants ;
375366 }
@@ -381,7 +372,7 @@ parser_error_t read_enum(parser_context_t *ctx, schema_enum_t *schema_enum) {
381372
382373 // read hide_tag
383374 CHECK_ERROR (read_u8 (ctx , (uint8_t * )& schema_enum -> hide_tag ));
384- printf ("hide_tag: %d\n " , schema_enum -> hide_tag );
375+ print_u8 ("hide_tag:" , schema_enum -> hide_tag );
385376
386377 return parser_ok ;
387378}
@@ -401,7 +392,7 @@ parser_error_t read_struct(parser_context_t *ctx, schema_struct_t *schema_struct
401392
402393 // read has_show_as
403394 CHECK_ERROR (read_u8 (ctx , (uint8_t * )& schema_struct -> has_show_as ));
404- printf ("has_show_as: %d\n " , schema_struct -> has_show_as );
395+ print_u8 ("has_show_as:" , schema_struct -> has_show_as );
405396 if (schema_struct -> has_show_as ) {
406397 CHECK_ERROR (read_u32 (ctx , (uint32_t * )& schema_struct -> show_as .len ));
407398 if (schema_struct -> show_as .len == 0 ) {
@@ -417,7 +408,7 @@ parser_error_t read_struct(parser_context_t *ctx, schema_struct_t *schema_struct
417408
418409 // read has_structured_show_as
419410 CHECK_ERROR (read_u8 (ctx , (uint8_t * )& schema_struct -> has_structured_show_as ));
420- printf ("has_structured_show_as: %d\n " , schema_struct -> has_structured_show_as );
411+ print_u8 ("has_structured_show_as:" , schema_struct -> has_structured_show_as );
421412 if (schema_struct -> has_structured_show_as ) {
422413 CHECK_ERROR (read_u32 (ctx , (uint32_t * )& schema_struct -> structured_show_as .len ));
423414 if (schema_struct -> structured_show_as .len == 0 ) {
@@ -433,14 +424,14 @@ parser_error_t read_struct(parser_context_t *ctx, schema_struct_t *schema_struct
433424
434425 // read peekable
435426 CHECK_ERROR (read_u8 (ctx , (uint8_t * )& schema_struct -> peekable ));
436- printf ("peekable: %d\n " , schema_struct -> peekable );
427+ print_u8 ("peekable:" , schema_struct -> peekable );
437428
438429 // read fields
439430 CHECK_ERROR (read_u32 (ctx , & schema_struct -> fields_qty ));
440431 if (schema_struct -> fields_qty > MAX_FIELDS_QTY ) {
441432 return parser_too_many_fields ;
442433 }
443- printf ("fields_qty: %d\n " , schema_struct -> fields_qty );
434+ print_u32 ("fields_qty:" , schema_struct -> fields_qty );
444435 for (uint32_t i = 0 ; i < schema_struct -> fields_qty ; i ++ ) {
445436 named_field_t field = {0 };
446437 CHECK_ERROR (read_named_field (ctx , & field ));
@@ -456,7 +447,7 @@ parser_error_t read_tuple(parser_context_t *ctx, schema_tuple_t *schema_tuple) {
456447
457448 // read has_show_as
458449 CHECK_ERROR (read_u8 (ctx , (uint8_t * )& schema_tuple -> has_show_as ));
459- printf ("has_show_as: %d\n " , schema_tuple -> has_show_as );
450+ print_u8 ("has_show_as:" , schema_tuple -> has_show_as );
460451 if (schema_tuple -> has_show_as ) {
461452 CHECK_ERROR (read_u32 (ctx , (uint32_t * )& schema_tuple -> show_as .len ));
462453 if (schema_tuple -> show_as .len == 0 ) {
@@ -472,7 +463,7 @@ parser_error_t read_tuple(parser_context_t *ctx, schema_tuple_t *schema_tuple) {
472463
473464 // read has_structured_show_as
474465 CHECK_ERROR (read_u8 (ctx , (uint8_t * )& schema_tuple -> has_structured_show_as ));
475- printf ("has_structured_show_as: %d\n " , schema_tuple -> has_structured_show_as );
466+ print_u8 ("has_structured_show_as:" , schema_tuple -> has_structured_show_as );
476467 if (schema_tuple -> has_structured_show_as ) {
477468 CHECK_ERROR (read_u32 (ctx , (uint32_t * )& schema_tuple -> structured_show_as .len ));
478469 if (schema_tuple -> structured_show_as .len == 0 ) {
@@ -488,14 +479,14 @@ parser_error_t read_tuple(parser_context_t *ctx, schema_tuple_t *schema_tuple) {
488479
489480 // read peekable
490481 CHECK_ERROR (read_u8 (ctx , (uint8_t * )& schema_tuple -> peekable ));
491- printf ("peekable: %d\n " , schema_tuple -> peekable );
482+ print_u8 ("peekable:" , schema_tuple -> peekable );
492483
493484 // read fields
494485 CHECK_ERROR (read_u32 (ctx , & schema_tuple -> fields_qty ));
495486 if (schema_tuple -> fields_qty > MAX_FIELDS_QTY ) {
496487 return parser_too_many_fields ;
497488 }
498- printf ("fields_qty: %d\n " , schema_tuple -> fields_qty );
489+ print_u32 ("fields_qty:" , schema_tuple -> fields_qty );
499490 for (uint32_t i = 0 ; i < schema_tuple -> fields_qty ; i ++ ) {
500491 unnamed_field_t field = {0 };
501492 CHECK_ERROR (read_unnamed_field (ctx , & field ));
@@ -511,7 +502,7 @@ parser_error_t read_array(parser_context_t *ctx, schema_array_t *schema_array) {
511502
512503 // read length
513504 CHECK_ERROR (read_u64 (ctx , (uint64_t * )& schema_array -> len ));
514- printf ("array length: %llu\n " , schema_array -> len );
505+ print_u64 ("array length:" , schema_array -> len );
515506
516507 // read vec_type
517508 CHECK_ERROR (read_link (ctx , & schema_array -> value ));
@@ -524,7 +515,7 @@ parser_error_t metadata_read(parser_context_t *ctx, parser_tx_t *txObj) {
524515 CHECK_INPUT (txObj );
525516
526517 CHECK_ERROR (read_u32 (ctx , & txObj -> schema .types .qty ));
527- printf ("types.qty: %d\n " , txObj -> schema .types .qty );
518+ print_u32 ("types.qty:" , txObj -> schema .types .qty );
528519 if (txObj -> schema .types .qty > MAX_SCHEMES_QTY ) {
529520 return parser_too_many_schemes ;
530521 }
@@ -578,7 +569,7 @@ parser_error_t metadata_read(parser_context_t *ctx, parser_tx_t *txObj) {
578569 case LINKING_SCHEME_SKIP :
579570 print_string ("READING SKIP" );
580571 CHECK_ERROR (read_u64 (ctx , (uint64_t * )& txObj -> schema .types .schemes [i ].skip_type ));
581- printf ("skip_type: %llu\n " , txObj -> schema .types .schemes [i ].skip_type );
572+ print_u64 ("skip_type:" , txObj -> schema .types .schemes [i ].skip_type );
582573 print_string ("READING SKIP DONE\n" );
583574 break ;
584575 case LINKING_SCHEME_BYTE_VEC :
@@ -603,22 +594,22 @@ parser_error_t metadata_read(parser_context_t *ctx, parser_tx_t *txObj) {
603594 print_string ("READING MAP DONE\n" );
604595 break ;
605596 default :
606- printf ("UNKNOWN TYPE: %d\n " , txObj -> schema .types .schemes [i ].type );
597+ print_u8 ("UNKNOWN TYPE:" , txObj -> schema .types .schemes [i ].type );
607598 return parser_no_data ;
608599 }
609600 }
610601
611602 // read root_type_indices
612603 CHECK_ERROR (read_u32 (ctx , & txObj -> schema .root_type_indices .qty ));
613- printf ("root_type_indices.qty: %d\n " , txObj -> schema .root_type_indices .qty );
604+ print_u32 ("root_type_indices.qty:" , txObj -> schema .root_type_indices .qty );
614605 for (uint32_t i = 0 ; i < txObj -> schema .root_type_indices .qty ; i ++ ) {
615606 CHECK_ERROR (read_u64 (ctx , & txObj -> schema .root_type_indices .indices [i ]));
616- printf ("root_type_indices[%d]: %llu\n" , i , txObj -> schema .root_type_indices .indices [i ]);
607+ print_u64 ("root_type_indices[%d]:" , txObj -> schema .root_type_indices .indices [i ]);
617608 }
618609
619610 // read chain_data
620611 CHECK_ERROR (read_u64 (ctx , & txObj -> schema .chain_data .chain_id ));
621- printf ("chain_data.chain_id: %llu\n " , txObj -> schema .chain_data .chain_id );
612+ print_u64 ("chain_data.chain_id:" , txObj -> schema .chain_data .chain_id );
622613 CHECK_ERROR (read_u32 (ctx , (uint32_t * )& txObj -> schema .chain_data .chain_name .len ));
623614 if (txObj -> schema .chain_data .chain_name .len > 0 ) {
624615 txObj -> schema .chain_data .chain_name .ptr = ctx -> buffer + ctx -> offset ;
@@ -633,5 +624,13 @@ parser_error_t metadata_read(parser_context_t *ctx, parser_tx_t *txObj) {
633624 CTX_CHECK_AND_ADVANCE (ctx , txObj -> schema .extra_metadata_hash .len );
634625 print_buffer (& txObj -> schema .extra_metadata_hash , "extra_metadata_hash" );
635626
627+ // TODO: check that we have consumed all data
628+ if (ctx -> offset != ctx -> bufferLen ) {
629+ print_string ("Failed to parse metadata\n" );
630+ return parser_unexpected_error ;
631+ } else {
632+ print_string ("Successfully parsed metadata\n" );
633+ }
634+
636635 return parser_ok ;
637636}
0 commit comments