Rizin
unix-like reverse engineering framework and cli tools
rz_serialize.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Florian Märkl <info@florianmaerkl.de>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #ifndef RZ_SERIALIZE_H
5 #define RZ_SERIALIZE_H
6 
7 #include <rz_util/rz_json.h>
8 #include <rz_list.h>
9 
17 
19  return rz_list_newf(free);
20 }
21 
24 }
25 
26 // Common helpers for writing (de)serialization code
27 
33 #define RZ_SERIALIZE_ERR(res, ...) \
34  do { \
35  if (res) { \
36  rz_list_push(res, rz_str_newf(__VA_ARGS__)); \
37  } \
38  } while (0)
39 
46 typedef HtPP RzKeyParser;
47 
48 static inline RzKeyParser *rz_key_parser_new(void) {
49  return ht_pp_new0();
50 }
51 
52 static inline void rz_key_parser_free(RzKeyParser *parser) {
53  ht_pp_free(parser);
54 }
55 
56 static inline void rz_key_parser_add(RzKeyParser *parser, const char *key, int val) {
57  ht_pp_insert(parser, key, (void *)(size_t)val);
58 }
59 
60 #define RZ_KEY_PARSER_UNKNOWN -1
61 
67 #define RZ_KEY_PARSER_SWITCH(parser, key) \
68  bool key_parser_found = false; \
69  int key_parser_v = (int)(size_t)ht_pp_find(parser, key, &key_parser_found); \
70  if (!key_parser_found) { \
71  key_parser_v = RZ_KEY_PARSER_UNKNOWN; \
72  } \
73  switch (key_parser_v)
74 
82 #define RZ_KEY_PARSER_JSON(parser, json, child, body) \
83  if (json->type == RZ_JSON_OBJECT) { \
84  for (RzJson *child = json->children.first; child; child = child->next) { \
85  RZ_KEY_PARSER_SWITCH(parser, child->key) { body } \
86  } \
87  }
88 
104 #define RZ_SERIALIZE_SUB(db, subdb, res, ns, rip) \
105  do { \
106  subdb = sdb_ns(db, ns, false); \
107  if (!subdb) { \
108  RZ_SERIALIZE_ERR(res, "missing " ns " namespace"); \
109  rip \
110  } \
111  } while (0)
112 
129 #define RZ_SERIALIZE_SUB_DO(db, subdb, res, ns, call, rip) \
130  RZ_SERIALIZE_SUB(db, subdb, res, ns, rip); \
131  if (!(call)) { \
132  rip \
133  }
134 
135 #endif // RZ_SERIALIZE_H
ut16 val
Definition: armass64_const.h:6
RzBinInfo * info(RzBinFile *bf)
Definition: bin_ne.c:86
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len key
Definition: sflib.h:118
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API RZ_OWN RzList * rz_list_newf(RzListFree f)
Returns a new initialized RzList pointer and sets the free method.
Definition: list.c:248
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
static void rz_key_parser_add(RzKeyParser *parser, const char *key, int val)
Definition: rz_serialize.h:56
RzList RzSerializeResultInfo
Detailed info about a (de)serialization result.
Definition: rz_serialize.h:16
HtPP RzKeyParser
Hashtable-based key parser to prevent strcmp chains.
Definition: rz_serialize.h:46
static RzKeyParser * rz_key_parser_new(void)
Definition: rz_serialize.h:48
static RzSerializeResultInfo * rz_serialize_result_info_new(void)
Definition: rz_serialize.h:18
static void rz_serialize_result_info_free(RzSerializeResultInfo *info)
Definition: rz_serialize.h:22
static void rz_key_parser_free(RzKeyParser *parser)
Definition: rz_serialize.h:52