Rizin
unix-like reverse engineering framework and cli tools
cdb_make.h File Reference
#include "buffer.h"
#include <rz_types.h>

Go to the source code of this file.

Classes

struct  cdb_hp
 
struct  cdb_hplist
 
struct  cdb_make
 

Macros

#define CDB_HPLIST   1000
 

Functions

int cdb_make_start (struct cdb_make *, int)
 
int cdb_make_addbegin (struct cdb_make *, unsigned int, unsigned int)
 
int cdb_make_addend (struct cdb_make *, unsigned int, unsigned int, ut32)
 
int cdb_make_add (struct cdb_make *, const char *, unsigned int, const char *, unsigned int)
 
int cdb_make_finish (struct cdb_make *)
 

Macro Definition Documentation

◆ CDB_HPLIST

#define CDB_HPLIST   1000

Definition at line 14 of file cdb_make.h.

Function Documentation

◆ cdb_make_add()

int cdb_make_add ( struct cdb_make c,
const char *  key,
unsigned int  keylen,
const char *  data,
unsigned int  datalen 
)

Definition at line 108 of file cdb_make.c.

108  {
109  /* add tailing \0 to allow mmap to work later */
110  keylen++;
111  datalen++;
112  if (!cdb_make_addbegin(c, keylen, datalen)) {
113  return 0;
114  }
115  if (!buffer_putalign(&c->b, key, keylen)) {
116  return 0;
117  }
118  if (!buffer_putalign(&c->b, data, datalen)) {
119  return 0;
120  }
121  return cdb_make_addend(c, keylen, datalen, sdb_hash(key));
122 }
int buffer_putalign(buffer *s, const char *buf, ut32 len)
Definition: buffer.c:36
int cdb_make_addend(struct cdb_make *c, ut32 keylen, ut32 datalen, ut32 h)
Definition: cdb_make.c:63
int cdb_make_addbegin(struct cdb_make *c, ut32 keylen, ut32 datalen)
Definition: cdb_make.c:100
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 ut32 sdb_hash(const char *key)
Definition: util.c:22
#define c(i)
Definition: sha256.c:43

References buffer_putalign(), c, cdb_make_addbegin(), cdb_make_addend(), key, and sdb_hash().

Referenced by sdb_disk_insert().

◆ cdb_make_addbegin()

int cdb_make_addbegin ( struct cdb_make c,
unsigned int  keylen,
unsigned int  datalen 
)

Definition at line 100 of file cdb_make.c.

100  {
101  ut8 buf[KVLSZ];
102  if (!pack_kvlen(buf, keylen, datalen)) {
103  return 0;
104  }
105  return buffer_putalign(&c->b, (const char *)buf, KVLSZ);
106 }
#define KVLSZ
Definition: cdb.h:14
static int pack_kvlen(ut8 *buf, ut32 klen, ut32 vlen)
Definition: cdb_make.c:86
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11

References buffer_putalign(), c, KVLSZ, and pack_kvlen().

Referenced by cdb_make_add().

◆ cdb_make_addend()

int cdb_make_addend ( struct cdb_make c,
unsigned int  keylen,
unsigned int  datalen,
ut32  h 
)

Definition at line 63 of file cdb_make.c.

63  {
64  ut32 u;
65  struct cdb_hplist *head = c->head;
66  if (!head || (head->num >= CDB_HPLIST)) {
67  if (!(head = R_ANEW(struct cdb_hplist))) {
68  return 0;
69  }
70  head->num = 0;
71  head->next = c->head;
72  c->head = head;
73  }
74  head->hp[head->num].h = h;
75  head->hp[head->num].p = c->pos;
76  head->num++;
77  c->numentries++;
78  c->count[255 & h]++;
79  u = c->count[255 & h] * 2;
80  if (u > c->memsize) {
81  c->memsize = u;
82  }
83  return incpos(c, KVLSZ + keylen + datalen);
84 }
#define R_ANEW(x)
Definition: cdb_make.c:62
static int incpos(struct cdb_make *c, ut32 len)
Definition: cdb_make.c:53
#define CDB_HPLIST
Definition: cdb_make.h:14
uint32_t ut32
#define h(i)
Definition: sha256.c:48

References c, CDB_HPLIST, h, test-lz4-versions::head, incpos(), KVLSZ, and R_ANEW.

Referenced by cdb_make_add().

◆ cdb_make_finish()

int cdb_make_finish ( struct cdb_make c)

Definition at line 124 of file cdb_make.c.

124  {
125  int i;
126  char buf[8];
127  struct cdb_hp *hp;
128  struct cdb_hplist *x, *n;
129  ut32 len, u, memsize, count, where;
130 
131  memsize = c->memsize + c->numentries;
132  if (memsize > (UT32_MAX / sizeof(struct cdb_hp))) {
133  return 0;
134  }
135  c->split = (struct cdb_hp *)cdb_alloc(memsize * sizeof(struct cdb_hp));
136  if (!c->split) {
137  return 0;
138  }
139  c->hash = c->split + c->numentries;
140 
141  for (u = i = 0; i < 256; i++) {
142  u += c->count[i]; /* bounded by numentries, so no overflow */
143  c->start[i] = u;
144  }
145 
146  for (x = c->head; x; x = x->next) {
147  i = x->num;
148  while (i--) {
149  c->split[--c->start[255 & x->hp[i].h]] = x->hp[i];
150  }
151  }
152 
153  for (i = 0; i < 256; i++) {
154  count = c->count[i];
155  len = count << 1;
156  ut32_pack(c->final + 4 * i, c->pos);
157  for (u = 0; u < len; u++) {
158  c->hash[u].h = c->hash[u].p = 0;
159  }
160  hp = c->split + c->start[i];
161  for (u = 0; u < count; u++) {
162  where = (hp->h >> 8) % len;
163  while (c->hash[where].p) {
164  if (++where == len) {
165  where = 0;
166  }
167  }
168  c->hash[where] = *hp++;
169  }
170  for (u = 0; u < len; u++) {
171  ut32_pack(buf, c->hash[u].h);
172  ut32_pack(buf + 4, c->hash[u].p);
173  if (!buffer_putalign(&c->b, buf, 8)) {
174  return 0;
175  }
176  if (!incpos(c, 8)) {
177  return 0;
178  }
179  }
180  }
181 
182  if (!buffer_flush(&c->b)) {
183  return 0;
184  }
185  if (!seek_set(c->fd, 0)) {
186  return 0;
187  }
188  // free childs
189  for (x = c->head; x;) {
190  n = x->next;
191  cdb_alloc_free(x);
192  x = n;
193  }
194  cdb_alloc_free(c->split);
195  return buffer_putflush(&c->b, c->final, sizeof c->final);
196 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
int buffer_putflush(buffer *s, const char *buf, ut32 len)
Definition: buffer.c:56
int buffer_flush(buffer *s)
Definition: buffer.c:27
void cdb_alloc_free(void *x)
Definition: cdb_make.c:29
char * cdb_alloc(ut32 n)
Definition: cdb_make.c:18
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 count
Definition: sflib.h:98
int x
Definition: mipsasm.c:20
int n
Definition: mipsasm.c:19
#define UT32_MAX
Definition: rz_types_base.h:99
static void ut32_pack(char s[4], ut32 u)
Definition: sdb_private.h:25
static int seek_set(int fd, off_t pos)
Definition: sdb_private.h:21
ut32 h
Definition: cdb_make.h:17

References buffer_flush(), buffer_putalign(), buffer_putflush(), c, cdb_alloc(), cdb_alloc_free(), count, cdb_hp::h, i, incpos(), len, n, seek_set(), UT32_MAX, ut32_pack(), and x.

Referenced by sdb_disk_finish().

◆ cdb_make_start()

int cdb_make_start ( struct cdb_make c,
int  fd 
)

Definition at line 37 of file cdb_make.c.

37  {
38  int i;
39  c->head = 0;
40  c->split = 0;
41  c->hash = 0;
42  c->numentries = 0;
43  c->fd = fd;
44  c->pos = sizeof(c->final);
45  buffer_init(&c->b, (BufferOp)write, fd, c->bspace, sizeof(c->bspace));
46  c->memsize = 1;
47  for (i = 0; i < 256; i++) {
48  c->count[i] = 0;
49  }
50  return seek_set(fd, c->pos);
51 }
void buffer_init(buffer *s, BufferOp op, int fd, char *buf, ut32 len)
Definition: buffer.c:6
int(* BufferOp)(int, const char *, int)
Definition: buffer.h:13
static static fork write
Definition: sflib.h:33
static const z80_opcode fd[]
Definition: z80_tab.h:997

References buffer_init(), c, fd, i, seek_set(), and write.

Referenced by sdb_disk_create().