Rizin
unix-like reverse engineering framework and cli tools
serialize_io.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2020 thestr4ng3r
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_util/rz_serialize.h>
5 #include <rz_io.h>
6 
7 #include <errno.h>
8 
9 /*
10  *
11  * SDB Format:
12  *
13  * /
14  * /files
15  * <fd>={perm:<int>, uri:<str>, name:<str>, referer?:<str>}
16  * /pcache
17  * <fd>.<ut64>={cached:<ut64>, data:<base64>}
18  */
19 
20 typedef struct {
21  int fd;
22  int perm;
23  char *uri;
24  char *name;
25  char *referer;
26  HtUP *cache;
27  void *data;
29  RzIO *io;
30 } RzIODescasd;
31 
32 typedef struct {
33  int fd;
34  Sdb *db;
36 
37 static bool pcache_save_cb(void *user, const ut64 k, const void *v) {
38  PCacheSaveCtx *ctx = user;
39  const RzIODescCache *cache = v;
40  char key[0x30];
41  if (snprintf(key, sizeof(key), "%d.0x%" PFMT64x, ctx->fd, k) < 0) {
42  return false;
43  }
44  char val[RZ_IO_DESC_CACHE_SIZE * 4 + 1];
46  return true;
47 }
48 
49 static bool file_save_cb(void *user, void *data, ut32 id) {
50  Sdb *db = user;
51  RzIODesc *desc = (RzIODesc *)data;
52 
53  char key[0x20];
54  if (snprintf(key, sizeof(key), "%d", desc->fd) < 0) {
55  return false;
56  }
57 
58  PJ *j = pj_new();
59  if (!j) {
60  return false;
61  }
62  pj_o(j);
63 
64  pj_ki(j, "perm", desc->perm);
65  // obsz is irrelevant (never written, always 0)
66  pj_ks(j, "uri", desc->uri);
67  pj_ks(j, "name", desc->name);
68  if (desc->referer) {
69  pj_ks(j, "referer", desc->referer);
70  }
71  // TODO: plugin
72 
73  pj_end(j);
74  sdb_set(db, key, pj_string(j), 0);
75  pj_free(j);
76 
77  if (desc->cache->count) {
78  PCacheSaveCtx ctx = {
79  .fd = desc->fd,
80  .db = sdb_ns(db, "pcache", true)
81  };
82  ht_up_foreach(desc->cache, pcache_save_cb, &ctx);
83  }
84  return true;
85 }
86 
88  sdb_ns(db, "pcache", true);
89  rz_id_storage_foreach(io->files, file_save_cb, db);
90 }
91 
93  // TODO
94  return true;
95 }
96 
98  rz_serialize_io_files_save(sdb_ns(db, "files", true), io);
99 }
100 
102  // TODO: purge RzIO?
103  bool ret = false;
104  Sdb *subdb;
105 #define SUB(ns, call) RZ_SERIALIZE_SUB_DO(db, subdb, res, ns, call, goto beach;)
106  SUB("files", rz_serialize_io_files_load(subdb, io, res));
107 #undef SUB
108  ret = true;
109 beach:
110  return ret;
111 }
ut16 val
Definition: armass64_const.h:6
const char * desc
Definition: bin_vsf.c:19
#define RZ_API
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
uint32_t ut32
const char * k
Definition: dsignal.c:11
const char * v
Definition: dsignal.c:12
snprintf
Definition: kernel.h:364
RZ_API Sdb * sdb_ns(Sdb *s, const char *name, int create)
Definition: ns.c:186
RZ_API size_t rz_base64_encode(char *bout, const ut8 *bin, size_t sz)
Definition: ubase64.c:81
RZ_API bool rz_id_storage_foreach(RzIDStorage *storage, RzIDStorageForeachCb cb, void *user)
Definition: idpool.c:254
#define RZ_IO_DESC_CACHE_SIZE
Definition: rz_io.h:169
RZ_API PJ * pj_new(void)
Definition: pj.c:25
RZ_API PJ * pj_ki(PJ *j, const char *k, int d)
Definition: pj.c:149
RZ_API PJ * pj_end(PJ *j)
Definition: pj.c:87
RZ_API const char * pj_string(PJ *pj)
Definition: pj.c:57
RZ_API void pj_free(PJ *j)
Definition: pj.c:34
RZ_API PJ * pj_o(PJ *j)
Definition: pj.c:75
RZ_API PJ * pj_ks(PJ *j, const char *k, const char *v)
Definition: pj.c:170
#define RZ_NULLABLE
Definition: rz_types.h:65
#define RZ_NONNULL
Definition: rz_types.h:64
#define PFMT64x
Definition: rz_types.h:393
RZ_API int sdb_set(Sdb *s, const char *key, const char *val, ut32 cas)
Definition: sdb.c:611
static bool pcache_save_cb(void *user, const ut64 k, const void *v)
Definition: serialize_io.c:37
static bool file_save_cb(void *user, void *data, ut32 id)
Definition: serialize_io.c:49
RZ_API void rz_serialize_io_save(RZ_NONNULL Sdb *db, RZ_NONNULL RzIO *io)
Definition: serialize_io.c:97
RZ_API bool rz_serialize_io_files_load(RZ_NONNULL Sdb *db, RZ_NONNULL RzIO *io, RZ_NULLABLE RzSerializeResultInfo *res)
Definition: serialize_io.c:92
RZ_API void rz_serialize_io_files_save(RZ_NONNULL Sdb *db, RZ_NONNULL RzIO *io)
Definition: serialize_io.c:87
RZ_API bool rz_serialize_io_load(RZ_NONNULL Sdb *db, RZ_NONNULL RzIO *io, RZ_NULLABLE RzSerializeResultInfo *res)
Definition: serialize_io.c:101
#define SUB(ns, call)
char * referer
Definition: serialize_io.c:25
HtUP * cache
Definition: serialize_io.c:26
struct rz_io_plugin_t * plugin
Definition: serialize_io.c:28
char * name
Definition: serialize_io.c:24
char * uri
Definition: serialize_io.c:23
void * data
Definition: serialize_io.c:27
Definition: rz_pj.h:12
ut8 cdata[RZ_IO_DESC_CACHE_SIZE]
Definition: rz_io.h:172
Definition: rz_io.h:59
Definition: sdb.h:63
ut64(WINAPI *w32_GetEnabledXStateFeatures)()