Rizin
unix-like reverse engineering framework and cli tools
rz_json.h File Reference
#include <rz_types.h>
#include <rz_util/rz_pj.h>

Go to the source code of this file.

Classes

struct  rz_json_t
 

Typedefs

typedef enum rz_json_type_t RzJsonType
 
typedef struct rz_json_t RzJson
 

Enumerations

enum  rz_json_type_t {
  RZ_JSON_NULL , RZ_JSON_OBJECT , RZ_JSON_ARRAY , RZ_JSON_STRING ,
  RZ_JSON_INTEGER , RZ_JSON_DOUBLE , RZ_JSON_BOOLEAN
}
 

Functions

RZ_API RzJsonrz_json_parse (char *text)
 
RZ_API void rz_json_free (RzJson *js)
 
RZ_API const RzJsonrz_json_get (const RzJson *json, const char *key)
 
RZ_API const RzJsonrz_json_item (const RzJson *json, size_t idx)
 
RZ_API const RzJsonrz_json_get_path (const RzJson *json, const char *path)
 
RZ_API RZ_OWN char * rz_json_as_string (const RzJson *json, bool with_key)
 

Typedef Documentation

◆ RzJson

typedef struct rz_json_t RzJson

◆ RzJsonType

typedef enum rz_json_type_t RzJsonType

Enumeration Type Documentation

◆ rz_json_type_t

Enumerator
RZ_JSON_NULL 
RZ_JSON_OBJECT 
RZ_JSON_ARRAY 
RZ_JSON_STRING 
RZ_JSON_INTEGER 
RZ_JSON_DOUBLE 
RZ_JSON_BOOLEAN 

Definition at line 28 of file rz_json.h.

28  {
30  RZ_JSON_OBJECT, // properties can be found in child nodes
31  RZ_JSON_ARRAY, // items can be found in child nodes
32  RZ_JSON_STRING, // value can be found in the str_value field
33  RZ_JSON_INTEGER, // value can be found in the num.u_value/num.s_value fields
34  RZ_JSON_DOUBLE, // value can be found in the num.dbl_value field
35  RZ_JSON_BOOLEAN // value can be found in the num.u_value field
36 } RzJsonType;
@ RZ_JSON_INTEGER
Definition: rz_json.h:33
@ RZ_JSON_ARRAY
Definition: rz_json.h:31
@ RZ_JSON_DOUBLE
Definition: rz_json.h:34
@ RZ_JSON_OBJECT
Definition: rz_json.h:30
@ RZ_JSON_BOOLEAN
Definition: rz_json.h:35
@ RZ_JSON_STRING
Definition: rz_json.h:32
@ RZ_JSON_NULL
Definition: rz_json.h:29
enum rz_json_type_t RzJsonType

Function Documentation

◆ rz_json_as_string()

RZ_API RZ_OWN char* rz_json_as_string ( const RzJson json,
bool  with_key 
)

Definition at line 546 of file json_parser.c.

546  {
548  PJ *pj = pj_new();
549  if (json->type == RZ_JSON_STRING) {
550  if (with_key && json->key) {
551  pj_ks(pj, json->key, json->str_value);
552  } else {
553  // Printing string without surrounding quotes
554  pj_S(pj, json->str_value);
555  }
556  } else {
557  json_pj_recurse(json, pj, with_key);
558  }
559  char *str = pj_drain(pj);
560  return str;
561 }
#define NULL
Definition: cris-opc.c:27
static void json_pj_recurse(const RzJson *json, PJ *pj, bool with_key)
Definition: json_parser.c:470
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
RZ_API PJ * pj_S(PJ *j, const char *k)
Definition: pj.c:212
RZ_API PJ * pj_new(void)
Definition: pj.c:25
RZ_API char * pj_drain(PJ *j)
Definition: pj.c:50
RZ_API PJ * pj_ks(PJ *j, const char *k, const char *v)
Definition: pj.c:170
Definition: rz_pj.h:12
const char * str_value
Definition: rz_json.h:42
const char * key
Definition: rz_json.h:40
RzJsonType type
Definition: rz_json.h:39

References json_pj_recurse(), rz_json_t::key, NULL, pj_drain(), pj_ks(), pj_new(), pj_S(), RZ_JSON_STRING, rz_return_val_if_fail, cmd_descs_generate::str, rz_json_t::str_value, and rz_json_t::type.

Referenced by rz_cons_grepbuf().

◆ rz_json_free()

RZ_API void rz_json_free ( RzJson js)

Definition at line 45 of file json_parser.c.

45  {
46  if (!js) {
47  return;
48  }
49  if (js->type == RZ_JSON_OBJECT || js->type == RZ_JSON_ARRAY) {
50  RzJson *p = js->children.first;
51  RzJson *p1;
52  while (p) {
53  p1 = p->next;
54  rz_json_free(p);
55  p = p1;
56  }
57  }
58  free(js);
59 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API void rz_json_free(RzJson *js)
Definition: json_parser.c:45
void * p
Definition: libc.cpp:67
struct rz_json_t::@304::@307 children

References rz_json_t::children, free(), p, RZ_JSON_ARRAY, rz_json_free(), RZ_JSON_OBJECT, and rz_json_t::type.

Referenced by deserialize_checkpoints_cb(), deserialize_memory_cb(), deserialize_registers_cb(), meta_load_cb(), rz_json_free(), rz_json_parse(), rz_load_panels_layout(), rz_serialize_spaces_load(), winkd_download_module_and_pdb(), xrefs_load_cb(), and zone_load_cb().

◆ rz_json_get()

RZ_API const RzJson* rz_json_get ( const RzJson json,
const char *  key 
)

Definition at line 405 of file json_parser.c.

405  {
406  return rz_json_get_len(json, key, strlen(key));
407 }
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
static const RzJson * rz_json_get_len(const RzJson *json, const char *key, size_t keysize)
Definition: json_parser.c:395

References key, and rz_json_get_len().

Referenced by deserialize_checkpoints_cb(), deserialize_memory_cb(), deserialize_registers_cb(), GetHeapGlobalsOffset(), rz_serialize_analysis_var_load(), winkd_download_module_and_pdb(), and xrefs_load_cb().

◆ rz_json_get_path()

RZ_API const RzJson* rz_json_get_path ( const RzJson json,
const char *  path 
)

Definition at line 419 of file json_parser.c.

419  {
420  const RzJson *js = json;
421  const char *key;
422  size_t keysize;
423  ut64 index;
424 
425  while (*path) {
426  switch (*path++) {
427  case '\0':
428  break;
429  case '[':
430  // we could check if js->type != RZ_JSON_ARRAY but rz_json_item will
431  // fail in that case anyway
432  key = path;
433  index = (ut64)strtoull(key, (char **)&path, 10);
434  if (key == path || *path != ']') {
435  RZ_JSON_REPORT_ERROR("JSON path: expected ]", path - 1);
436  return NULL;
437  }
438  ++path;
439  js = rz_json_item(js, index);
440  if (!js) {
441  return NULL;
442  }
443  break;
444  case '.':
445  key = path;
446  for (keysize = 0; key[keysize]; ++keysize) {
447  if (strchr(".[", key[keysize])) {
448  break;
449  }
450  }
451  if (keysize == 0) {
452  RZ_JSON_REPORT_ERROR("JSON path: expected key", path - 1);
453  return NULL;
454  }
455  js = rz_json_get_len(js, key, keysize);
456  if (!js) {
457  return NULL;
458  }
459  path = key + keysize;
460  break;
461  default:
462  RZ_JSON_REPORT_ERROR("JSON path: unexpected char", path - 1);
463  return NULL;
464  }
465  }
466  // js == json means we've not done any access at all
467  return (js == json) ? NULL : js;
468 }
static static fork const void static count static fd const char const char static newpath const char static path const char path
Definition: sflib.h:35
#define RZ_JSON_REPORT_ERROR(msg, p)
Definition: json_parser.c:17
RZ_API const RzJson * rz_json_item(const RzJson *json, size_t idx)
Definition: json_parser.c:409
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References key, NULL, path, rz_json_get_len(), rz_json_item(), RZ_JSON_REPORT_ERROR, and ut64().

Referenced by rz_cons_grepbuf().

◆ rz_json_item()

RZ_API const RzJson* rz_json_item ( const RzJson json,
size_t  idx 
)

Definition at line 409 of file json_parser.c.

409  {
410  RzJson *js;
411  for (js = json->children.first; js; js = js->next) {
412  if (!idx--) {
413  return js;
414  }
415  }
416  return NULL;
417 }
int idx
Definition: setup.py:197
struct rz_json_t * next
Definition: rz_json.h:56

References rz_json_t::children, setup::idx, rz_json_t::next, and NULL.

Referenced by rz_json_get_path().

◆ rz_json_parse()

RZ_API RzJson* rz_json_parse ( char *  text)

Definition at line 382 of file json_parser.c.

382  {
383  RzJson js = { 0 };
384  if (!parse_value(&js, 0, text)) {
385  if (js.children.first) {
386  rz_json_free(js.children.first);
387  }
388  return 0;
389  }
390  return js.children.first;
391 }
static char * parse_value(RzJson *parent, const char *key, char *p)
Definition: json_parser.c:225

References rz_json_t::children, parse_value(), rz_json_free(), and create_tags_rz::text.

Referenced by block_load_cb(), bp_load_cb(), deserialize_checkpoints_cb(), deserialize_memory_cb(), deserialize_registers_cb(), flag_load_cb(), function_load_cb(), GetHeapGlobalsOffset(), global_var_load_cb(), hints_load_cb(), meta_load_cb(), rz_cons_grepbuf(), rz_load_panels_layout(), rz_serialize_spaces_load(), winkd_download_module_and_pdb(), xrefs_load_cb(), and zone_load_cb().