Rizin
unix-like reverse engineering framework and cli tools
rz_cf_dict.h File Reference

Go to the source code of this file.

Classes

struct  _CFValue
 
struct  _CFKeyValue
 
struct  _CFValueDict
 
struct  _CFValueArray
 
struct  _CFValueString
 
struct  _CFValueInteger
 
struct  _CFValueData
 
struct  _CFValueBool
 
struct  _CFValueNULL
 

Macros

#define RZ_CF_OPTION_NONE   0
 
#define RZ_CF_OPTION_SKIP_NSDATA   1
 

Typedefs

typedef struct _CFValue RCFValue
 
typedef struct _CFKeyValue RCFKeyValue
 
typedef struct _CFValueDict RCFValueDict
 
typedef struct _CFValueArray RCFValueArray
 
typedef struct _CFValueString RCFValueString
 
typedef struct _CFValueInteger RCFValueInteger
 
typedef struct _CFValueData RCFValueData
 
typedef struct _CFValueBool RCFValueBool
 
typedef struct _CFValueNULL RCFValueNULL
 

Enumerations

enum  RCFValueType {
  RZ_CF_INVALID , RZ_CF_DICT , RZ_CF_ARRAY , RZ_CF_STRING ,
  RZ_CF_INTEGER , RZ_CF_DATA , RZ_CF_NULL , RZ_CF_TRUE ,
  RZ_CF_FALSE
}
 

Functions

RZ_API RCFValueDictrz_cf_value_dict_parse (RzBuffer *file_buf, ut64 offset, ut64 size, int options)
 
RZ_API void rz_cf_value_dict_free (RCFValueDict *dict)
 
RZ_API void rz_cf_value_print (RCFValue *value)
 

Macro Definition Documentation

◆ RZ_CF_OPTION_NONE

#define RZ_CF_OPTION_NONE   0

Definition at line 7 of file rz_cf_dict.h.

◆ RZ_CF_OPTION_SKIP_NSDATA

#define RZ_CF_OPTION_SKIP_NSDATA   1

Definition at line 8 of file rz_cf_dict.h.

Typedef Documentation

◆ RCFKeyValue

typedef struct _CFKeyValue RCFKeyValue

◆ RCFValue

typedef struct _CFValue RCFValue

◆ RCFValueArray

typedef struct _CFValueArray RCFValueArray

◆ RCFValueBool

typedef struct _CFValueBool RCFValueBool

◆ RCFValueData

typedef struct _CFValueData RCFValueData

◆ RCFValueDict

typedef struct _CFValueDict RCFValueDict

◆ RCFValueInteger

◆ RCFValueNULL

typedef struct _CFValueNULL RCFValueNULL

◆ RCFValueString

Enumeration Type Documentation

◆ RCFValueType

Enumerator
RZ_CF_INVALID 
RZ_CF_DICT 
RZ_CF_ARRAY 
RZ_CF_STRING 
RZ_CF_INTEGER 
RZ_CF_DATA 
RZ_CF_NULL 
RZ_CF_TRUE 
RZ_CF_FALSE 

Definition at line 10 of file rz_cf_dict.h.

10  {
12  RZ_CF_DICT,
16  RZ_CF_DATA,
17  RZ_CF_NULL,
18  RZ_CF_TRUE,
20 } RCFValueType;
RCFValueType
Definition: rz_cf_dict.h:10
@ RZ_CF_DICT
Definition: rz_cf_dict.h:12
@ RZ_CF_STRING
Definition: rz_cf_dict.h:14
@ RZ_CF_TRUE
Definition: rz_cf_dict.h:18
@ RZ_CF_INTEGER
Definition: rz_cf_dict.h:15
@ RZ_CF_FALSE
Definition: rz_cf_dict.h:19
@ RZ_CF_DATA
Definition: rz_cf_dict.h:16
@ RZ_CF_NULL
Definition: rz_cf_dict.h:17
@ RZ_CF_INVALID
Definition: rz_cf_dict.h:11
@ RZ_CF_ARRAY
Definition: rz_cf_dict.h:13

Function Documentation

◆ rz_cf_value_dict_free()

RZ_API void rz_cf_value_dict_free ( RCFValueDict dict)

Definition at line 352 of file rz_cf_dict.c.

352  {
353  rz_return_if_fail(dict);
354 
355  if (dict->pairs) {
356  rz_list_free(dict->pairs);
357  dict->pairs = NULL;
358  }
359  dict->type = RZ_CF_INVALID;
360  RZ_FREE(dict);
361 }
#define NULL
Definition: cris-opc.c:27
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100
#define RZ_FREE(x)
Definition: rz_types.h:369
RCFValueType type
Definition: rz_cf_dict.h:32
RzList * pairs
Definition: rz_cf_dict.h:33

References NULL, _CFValueDict::pairs, RZ_CF_INVALID, RZ_FREE, rz_list_free(), rz_return_if_fail, and _CFValueDict::type.

Referenced by rz_cf_value_free(), and rz_kernel_cache_free().

◆ rz_cf_value_dict_parse()

RZ_API RCFValueDict* rz_cf_value_dict_parse ( RzBuffer file_buf,
ut64  offset,
ut64  size,
int  options 
)

Definition at line 68 of file rz_cf_dict.c.

68  {
69  RCFValueDict *result = NULL;
70  yxml_t x;
71  int i, depth = 0;
72  char *content = NULL;
73 
74  void *xml_buf = malloc(XMLBUFSIZE);
75  if (!xml_buf) {
76  return NULL;
77  }
78 
79  yxml_init(&x, xml_buf, XMLBUFSIZE);
80 
82  if (!stack) {
83  goto beach;
84  }
85 
87 
88  for (i = 0; i < size; i++) {
89  ut8 doc = 0;
90  rz_buf_read_at(file_buf, offset + i, &doc, 1);
91  if (!doc) {
92  break;
93  }
94 
95  yxml_ret_t r = yxml_parse(&x, doc);
96  if (r < 0) {
97  RZ_LOG_ERROR("Parsing error at :%" PRIu32 ":%" PRIu64 " byte offset %" PRIu64 "\n",
98  x.line, x.byte, x.total);
99  goto beach;
100  }
101 
102  switch (r) {
103  case YXML_ELEMSTART: {
105  RCFParseState *next_state = NULL;
106 
107  if (!strcmp(x.elem, "dict")) {
109  if (!next_state) {
110  goto beach;
111  }
112  next_state->dict = rz_cf_value_dict_new();
113  } else if (!strcmp(x.elem, "array")) {
115  if (!next_state) {
116  goto beach;
117  }
118  next_state->array = rz_cf_value_array_new();
119  } else if (!strcmp(x.elem, "key") && state->phase == RZ_CF_STATE_IN_DICT) {
121  if (!next_state) {
122  goto beach;
123  }
124  next_state->dict = state->dict;
125  } else if (!strcmp(x.elem, "string")) {
127  if (!next_state) {
128  goto beach;
129  }
130  next_state->value_type = RZ_CF_STRING;
131  } else if (!strcmp(x.elem, "integer")) {
133  if (!next_state) {
134  goto beach;
135  }
136  next_state->value_type = RZ_CF_INTEGER;
137  } else if (!strcmp(x.elem, "data")) {
140  } else {
142  if (!next_state) {
143  goto beach;
144  }
145  next_state->value_type = RZ_CF_DATA;
146  }
147  } else if (!strcmp(x.elem, "true")) {
149  if (!next_state) {
150  goto beach;
151  }
152  next_state->value_type = RZ_CF_TRUE;
153  } else if (!strcmp(x.elem, "false")) {
155  if (!next_state) {
156  goto beach;
157  }
158  next_state->value_type = RZ_CF_FALSE;
159  }
160 
161  if (next_state) {
162  rz_list_push(stack, next_state);
163  } else {
164  RZ_LOG_ERROR("Missing next state for elem: %s phase: %d\n", x.elem, state->phase);
165  break;
166  }
167  depth++;
168 
169  break;
170  }
171  case YXML_ELEMEND: {
174  if (!state || !next_state) {
175  goto beach;
176  }
177 
178  if (next_state->phase == RZ_CF_STATE_ROOT) {
179  if (state->phase == RZ_CF_STATE_IN_DICT) {
180  result = state->dict;
182  break;
183  } else {
184  RZ_LOG_ERROR("Root element is not a dict\n");
185  goto beach;
186  }
187  }
188 
189  if (next_state->phase == RZ_CF_STATE_IN_DICT && state->phase == RZ_CF_STATE_IN_KEY) {
190  if (!content) {
191  RZ_LOG_ERROR("NULL key is not supported");
192  goto beach;
193  }
194  next_state->key = content;
195  }
196 
197  if (state->phase != RZ_CF_STATE_IN_KEY) {
198  RCFValue *value = NULL;
199 
200  switch (state->phase) {
201  case RZ_CF_STATE_IN_DICT:
202  value = (RCFValue *)state->dict;
203  break;
205  value = (RCFValue *)state->array;
206  break;
208  if (!content && state->value_type != RZ_CF_FALSE && state->value_type != RZ_CF_TRUE) {
210  } else {
211  switch (state->value_type) {
212  case RZ_CF_STRING:
213  value = (RCFValue *)rz_cf_value_string_new(content);
214  break;
215  case RZ_CF_INTEGER:
216  value = (RCFValue *)rz_cf_value_integer_new(content);
217  RZ_FREE(content);
218  break;
219  case RZ_CF_DATA:
220  value = (RCFValue *)rz_cf_value_data_new(content);
221  RZ_FREE(content);
222  break;
223  case RZ_CF_TRUE:
225  break;
226  case RZ_CF_FALSE:
227  value = (RCFValue *)rz_cf_value_bool_new(false);
228  break;
229  default:
230  break;
231  }
232  }
233  break;
234  default:
235  break;
236  }
237 
238  if (next_state->phase == RZ_CF_STATE_IN_DICT) {
239  if (value) {
240  RCFKeyValue *key_value = rz_cf_key_value_new(next_state->key, value);
241  rz_cf_value_dict_add(next_state->dict, key_value);
242  } else if (state->phase != RZ_CF_STATE_IN_IGNORE) {
243  RZ_LOG_ERROR("Missing value for key %s\n", next_state->key);
245  goto beach;
246  }
247  } else if (next_state->phase == RZ_CF_STATE_IN_ARRAY) {
248  if (value) {
249  rz_cf_value_array_add(next_state->array, value);
250  } else if (state->phase != RZ_CF_STATE_IN_IGNORE) {
251  RZ_LOG_ERROR("Missing value for array\n");
253  goto beach;
254  }
255  }
256  }
257 
258  depth--;
259  content = NULL;
261  break;
262  }
263  case YXML_CONTENT: {
265  if (state->phase == RZ_CF_STATE_IN_IGNORE) {
266  break;
267  }
268  if (!content) {
269  content = rz_str_new(x.data);
270  } else {
271  content = rz_str_append(content, x.data);
272  }
273  break;
274  }
275  default:
276  break;
277  }
278 
279  if (result) {
280  break;
281  }
282  }
283 
284  yxml_ret_t r = yxml_eof(&x);
285  if (r < 0) {
286  RZ_LOG_ERROR("Invalid xml\n");
287  }
288 
289 beach:
290  RZ_FREE(xml_buf);
291  if (stack) {
293  }
294 
295  return result;
296 }
lzma_index ** i
Definition: index.h:629
static int value
Definition: cmd_api.c:93
#define r
Definition: crypto_rc6.c:12
voidpf void uLong size
Definition: ioapi.h:138
voidpf uLong offset
Definition: ioapi.h:144
uint8_t ut8
Definition: lh5801.h:11
#define PRIu32
Definition: macros.h:20
#define PRIu64
Definition: macros.h:18
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 RZ_BORROW void * rz_list_get_top(RZ_NONNULL const RzList *list)
Returns the last element of the list.
Definition: list.c:457
RZ_API RZ_OWN void * rz_list_pop(RZ_NONNULL RzList *list)
Removes and returns the last element of the list.
Definition: list.c:376
RZ_API RZ_BORROW RzListIter * rz_list_push(RZ_NONNULL RzList *list, void *item)
Alias for rz_list_append.
Definition: list.c:60
void * malloc(size_t size)
Definition: malloc.c:123
static const char struct stat static buf struct stat static buf static vhangup int options
Definition: sflib.h:145
int x
Definition: mipsasm.c:20
RZ_API st64 rz_buf_read_at(RZ_NONNULL RzBuffer *b, ut64 addr, RZ_NONNULL RZ_OUT ut8 *buf, ut64 len)
Read len bytes of the buffer at the specified address.
Definition: buf.c:1136
#define XMLBUFSIZE
Definition: rz_cf_dict.c:12
static RCFValueBool * rz_cf_value_bool_new(bool value)
Definition: rz_cf_dict.c:556
static void rz_cf_value_array_add(RCFValueArray *array, RCFValue *value)
Definition: rz_cf_dict.c:413
static RCFValueData * rz_cf_value_data_new(char *string)
Definition: rz_cf_dict.c:493
static RCFValueDict * rz_cf_value_dict_new(void)
Definition: rz_cf_dict.c:340
static RCFValueNULL * rz_cf_value_null_new(void)
Definition: rz_cf_dict.c:532
@ RZ_CF_STATE_IN_KEY
Definition: rz_cf_dict.c:18
@ RZ_CF_STATE_ROOT
Definition: rz_cf_dict.c:15
@ RZ_CF_STATE_IN_ARRAY
Definition: rz_cf_dict.c:17
@ RZ_CF_STATE_IN_SCALAR
Definition: rz_cf_dict.c:19
@ RZ_CF_STATE_IN_IGNORE
Definition: rz_cf_dict.c:20
@ RZ_CF_STATE_IN_DICT
Definition: rz_cf_dict.c:16
static void rz_cf_value_dict_add(RCFValueDict *dict, RCFKeyValue *key_value)
Definition: rz_cf_dict.c:363
static RCFValueArray * rz_cf_value_array_new(void)
Definition: rz_cf_dict.c:387
static RCFValueString * rz_cf_value_string_new(char *string)
Definition: rz_cf_dict.c:436
static RCFKeyValue * rz_cf_key_value_new(char *key, RCFValue *value)
Definition: rz_cf_dict.c:312
static void rz_cf_parse_state_free(RCFParseState *state)
Definition: rz_cf_dict.c:306
static RCFValueInteger * rz_cf_value_integer_new(char *string)
Definition: rz_cf_dict.c:468
static void rz_cf_value_free(RCFValue *value)
Definition: rz_cf_dict.c:580
static RCFParseState * rz_cf_parse_state_new(RCFParsePhase phase)
Definition: rz_cf_dict.c:298
#define RZ_CF_OPTION_SKIP_NSDATA
Definition: rz_cf_dict.h:8
void(* RzListFree)(void *ptr)
Definition: rz_list.h:11
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
RZ_API char * rz_str_append(char *ptr, const char *string)
Definition: str.c:1063
RZ_API char * rz_str_new(const char *str)
Definition: str.c:865
RCFValueDict * dict
Definition: rz_cf_dict.c:27
RCFValueArray * array
Definition: rz_cf_dict.c:28
RCFParsePhase phase
Definition: rz_cf_dict.c:24
RCFValueType value_type
Definition: rz_cf_dict.c:26
Definition: z80asm.h:140
Definition: dis.h:43
Definition: yxml.h:79
yxml_ret_t yxml_eof(yxml_t *x)
Definition: yxml.c:1056
yxml_ret_t yxml_parse(yxml_t *x, int _ch)
Definition: yxml.c:338
void yxml_init(yxml_t *x, void *stack, size_t stacksize)
Definition: yxml.c:327
yxml_ret_t
Definition: yxml.h:39
@ YXML_ELEMSTART
Definition: yxml.h:46
@ YXML_ELEMEND
Definition: yxml.h:48
@ YXML_CONTENT
Definition: yxml.h:47

References _RCFParseState::array, _RCFParseState::dict, i, _RCFParseState::key, malloc(), NULL, options, _RCFParseState::phase, PRIu32, PRIu64, r, rz_buf_read_at(), RZ_CF_DATA, RZ_CF_FALSE, RZ_CF_INTEGER, rz_cf_key_value_new(), RZ_CF_OPTION_SKIP_NSDATA, rz_cf_parse_state_free(), rz_cf_parse_state_new(), RZ_CF_STATE_IN_ARRAY, RZ_CF_STATE_IN_DICT, RZ_CF_STATE_IN_IGNORE, RZ_CF_STATE_IN_KEY, RZ_CF_STATE_IN_SCALAR, RZ_CF_STATE_ROOT, RZ_CF_STRING, RZ_CF_TRUE, rz_cf_value_array_add(), rz_cf_value_array_new(), rz_cf_value_bool_new(), rz_cf_value_data_new(), rz_cf_value_dict_add(), rz_cf_value_dict_new(), rz_cf_value_free(), rz_cf_value_integer_new(), rz_cf_value_null_new(), rz_cf_value_string_new(), RZ_FREE, rz_list_free(), rz_list_get_top(), rz_list_newf(), rz_list_pop(), rz_list_push(), RZ_LOG_ERROR, rz_str_append(), rz_str_new(), value, _RCFParseState::value_type, x, XMLBUFSIZE, YXML_CONTENT, YXML_ELEMEND, YXML_ELEMSTART, yxml_eof(), yxml_init(), and yxml_parse().

◆ rz_cf_value_print()

RZ_API void rz_cf_value_print ( RCFValue value)

Definition at line 613 of file rz_cf_dict.c.

613  {
614  if (!value) {
615  return;
616  }
617 
618  switch (value->type) {
619  case RZ_CF_DICT:
621  break;
622  case RZ_CF_ARRAY:
624  break;
625  case RZ_CF_STRING:
627  break;
628  case RZ_CF_INTEGER:
630  break;
631  case RZ_CF_DATA:
633  break;
634  case RZ_CF_NULL:
636  break;
637  case RZ_CF_TRUE:
638  case RZ_CF_FALSE:
640  break;
641  default:
642  break;
643  }
644 }
static void rz_cf_value_dict_print(RCFValueDict *dict)
Definition: rz_cf_dict.c:371
static void rz_cf_value_bool_print(RCFValueBool *bool_value)
Definition: rz_cf_dict.c:573
static void rz_cf_value_array_print(RCFValueArray *dict)
Definition: rz_cf_dict.c:421
static void rz_cf_value_integer_print(RCFValueInteger *integer)
Definition: rz_cf_dict.c:489
static void rz_cf_value_data_print(RCFValueData *data)
Definition: rz_cf_dict.c:528
static void rz_cf_value_null_print(RCFValueNULL *null)
Definition: rz_cf_dict.c:552
static void rz_cf_value_string_print(RCFValueString *string)
Definition: rz_cf_dict.c:461

References RZ_CF_ARRAY, RZ_CF_DATA, RZ_CF_DICT, RZ_CF_FALSE, RZ_CF_INTEGER, RZ_CF_NULL, RZ_CF_STRING, RZ_CF_TRUE, rz_cf_value_array_print(), rz_cf_value_bool_print(), rz_cf_value_data_print(), rz_cf_value_dict_print(), rz_cf_value_integer_print(), rz_cf_value_null_print(), rz_cf_value_string_print(), and value.

Referenced by rz_cf_value_array_print(), and rz_cf_value_dict_print().