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

Go to the source code of this file.

Classes

struct  RzStrpool
 

Macros

#define RZ_STRPOOL_INC   1024
 

Functions

RZ_API RzStrpoolrz_strpool_new (int sz)
 
RZ_API char * rz_strpool_alloc (RzStrpool *p, int l)
 
RZ_API int rz_strpool_memcat (RzStrpool *p, const char *s, int len)
 
RZ_API int rz_strpool_ansi_chop (RzStrpool *p, int n)
 
RZ_API int rz_strpool_append (RzStrpool *p, const char *s)
 
RZ_API void rz_strpool_free (RzStrpool *p)
 
RZ_API int rz_strpool_fit (RzStrpool *p)
 
RZ_API char * rz_strpool_get (RzStrpool *p, int index)
 
RZ_API char * rz_strpool_get_i (RzStrpool *p, int index)
 
RZ_API int rz_strpool_get_index (RzStrpool *p, const char *s)
 
RZ_API char * rz_strpool_next (RzStrpool *p, int index)
 
RZ_API char * rz_strpool_slice (RzStrpool *p, int index)
 
RZ_API char * rz_strpool_empty (RzStrpool *p)
 

Macro Definition Documentation

◆ RZ_STRPOOL_INC

#define RZ_STRPOOL_INC   1024

Definition at line 6 of file rz_strpool.h.

Function Documentation

◆ rz_strpool_alloc()

RZ_API char* rz_strpool_alloc ( RzStrpool p,
int  l 
)

Definition at line 34 of file strpool.c.

34  {
35  char *ret = p->str + p->len;
36  if ((p->len + l) >= p->size) {
37  ut64 osize = p->size;
38  if (l >= RZ_STRPOOL_INC) {
39  p->size += l + RZ_STRPOOL_INC;
40  } else {
41  p->size += RZ_STRPOOL_INC;
42  }
43  if (p->size < osize) {
44  eprintf("Underflow!\n");
45  p->size = osize;
46  return NULL;
47  }
48  ret = realloc(p->str, p->size);
49  if (!ret) {
50  eprintf("Realloc failed!\n");
51  RZ_FREE(p->str);
52  return NULL;
53  }
54  p->str = ret;
55  ret += p->len;
56  }
57  p->len += l;
58  return ret;
59 }
#define NULL
Definition: cris-opc.c:27
void * p
Definition: libc.cpp:67
void * realloc(void *ptr, size_t size)
Definition: malloc.c:144
#define eprintf(x, y...)
Definition: rlcc.c:7
#define RZ_STRPOOL_INC
Definition: rz_strpool.h:6
#define RZ_FREE(x)
Definition: rz_types.h:369
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References eprintf, NULL, p, realloc(), RZ_FREE, RZ_STRPOOL_INC, and ut64().

Referenced by rz_strpool_memcat().

◆ rz_strpool_ansi_chop()

RZ_API int rz_strpool_ansi_chop ( RzStrpool p,
int  n 
)

Definition at line 75 of file strpool.c.

75  {
76  /* p->str need not be a c-string */
77  int i = rz_str_ansi_trim(p->str, p->len, n);
78  p->len = i;
79  return i;
80 }
lzma_index ** i
Definition: index.h:629
int n
Definition: mipsasm.c:19
RZ_API int rz_str_ansi_trim(char *str, int str_len, int n)
Definition: str_trim.c:204

References i, n, p, and rz_str_ansi_trim().

Referenced by pager_printpage().

◆ rz_strpool_append()

RZ_API int rz_strpool_append ( RzStrpool p,
const char *  s 
)

Definition at line 70 of file strpool.c.

70  {
71  int l = strlen(s) + 1;
72  return rz_strpool_memcat(p, s, l);
73 }
static RzSocket * s
Definition: rtr.c:28
RZ_API int rz_strpool_memcat(RzStrpool *p, const char *s, int len)
Definition: strpool.c:61

References p, rz_strpool_memcat(), and s.

Referenced by pager_color_line().

◆ rz_strpool_empty()

RZ_API char* rz_strpool_empty ( RzStrpool p)

Definition at line 27 of file strpool.c.

27  {
28  p->len = 0;
29  p->str[0] = 0;
30  p->str[1] = 0;
31  return p->str;
32 }

References p.

Referenced by pager_color_line().

◆ rz_strpool_fit()

RZ_API int rz_strpool_fit ( RzStrpool p)

Definition at line 87 of file strpool.c.

87  {
88  char *s;
89  if (p->len == p->size) {
90  return false;
91  }
92  s = realloc(p->str, p->len);
93  if (!s) {
94  eprintf("Realloc failed!\n");
95  RZ_FREE(p->str);
96  return false;
97  }
98  p->str = s;
99  p->size = p->len;
100  return true;
101 }

References eprintf, p, realloc(), RZ_FREE, and s.

◆ rz_strpool_free()

RZ_API void rz_strpool_free ( RzStrpool p)

Definition at line 82 of file strpool.c.

82  {
83  free(p->str);
84  free(p);
85 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130

References free(), and p.

Referenced by pager_printpage().

◆ rz_strpool_get()

RZ_API char* rz_strpool_get ( RzStrpool p,
int  index 
)

Definition at line 103 of file strpool.c.

103  {
104  if (!p || !p->str || index < 0 || index >= p->len) {
105  return NULL;
106  }
107  return p->str + index;
108 }

References NULL, and p.

Referenced by rz_strpool_next().

◆ rz_strpool_get_i()

RZ_API char* rz_strpool_get_i ( RzStrpool p,
int  index 
)

Definition at line 110 of file strpool.c.

110  {
111  int i, n = 0;
112  if (index < 0 || index >= p->len) {
113  return NULL;
114  }
115  for (i = 0; i < index; i++) {
116  char *s = rz_strpool_next(p, n);
118  }
119  return p->str + n;
120 }
RZ_API int rz_strpool_get_index(RzStrpool *p, const char *s)
Definition: strpool.c:122
RZ_API char * rz_strpool_next(RzStrpool *p, int index)
Definition: strpool.c:127

References i, n, NULL, p, rz_strpool_get_index(), rz_strpool_next(), and s.

Referenced by rz_strpool_slice().

◆ rz_strpool_get_index()

RZ_API int rz_strpool_get_index ( RzStrpool p,
const char *  s 
)

Definition at line 122 of file strpool.c.

122  {
123  int ret = (size_t)(s - p->str);
124  return (ret > 0) ? ret : 0;
125 }
int size_t
Definition: sftypes.h:40

References p, and s.

Referenced by rz_strpool_get_i().

◆ rz_strpool_memcat()

RZ_API int rz_strpool_memcat ( RzStrpool p,
const char *  s,
int  len 
)

Definition at line 61 of file strpool.c.

61  {
62  char *ptr = rz_strpool_alloc(p, len);
63  if (!ptr) {
64  return -1;
65  }
66  memcpy(ptr, s, len);
67  return (size_t)(ptr - p->str);
68 }
size_t len
Definition: 6502dis.c:15
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
RZ_API char * rz_strpool_alloc(RzStrpool *p, int l)
Definition: strpool.c:34

References len, memcpy(), p, rz_strpool_alloc(), and s.

Referenced by pager_color_line(), and rz_strpool_append().

◆ rz_strpool_new()

RZ_API RzStrpool* rz_strpool_new ( int  sz)

Definition at line 6 of file strpool.c.

6  {
8  if (!p) {
9  eprintf("Malloc failed!\n");
10  return NULL;
11  }
12  if (sz < 1) {
13  sz = 1024;
14  }
15  p->str = malloc(sz);
16  if (!p->str) {
17  eprintf("Malloc failed!\n");
18  free(p);
19  return NULL;
20  }
21  p->size = sz;
22  p->len = 0;
23  p->str[0] = 0;
24  return p;
25 }
void * malloc(size_t size)
Definition: malloc.c:123
#define RZ_NEW(x)
Definition: rz_types.h:285

References eprintf, free(), malloc(), NULL, p, and RZ_NEW.

Referenced by pager_printpage().

◆ rz_strpool_next()

RZ_API char* rz_strpool_next ( RzStrpool p,
int  index 
)

Definition at line 127 of file strpool.c.

127  {
128  char *ptr = rz_strpool_get(p, index);
129  if (ptr) {
130  char *q = ptr + strlen(ptr) + 1;
131  if (q >= (p->str + p->len)) {
132  return NULL;
133  }
134  ptr = q;
135  if (!*ptr) {
136  ptr = NULL;
137  }
138  }
139  return ptr;
140 }
RZ_API char * rz_strpool_get(RzStrpool *p, int index)
Definition: strpool.c:103

References NULL, p, and rz_strpool_get().

Referenced by rz_strpool_get_i().

◆ rz_strpool_slice()

RZ_API char* rz_strpool_slice ( RzStrpool p,
int  index 
)

Definition at line 142 of file strpool.c.

142  {
143  int idx, len;
144  char *o, *x = rz_strpool_get_i(p, index + 1);
145  if (!x || !*x) {
146  return NULL;
147  }
148  idx = (size_t)(x - p->str);
149  len = p->len - idx;
150  o = malloc(len + 128);
151  if (!o) {
152  return NULL;
153  }
154  memcpy(o, x, len);
155  free(p->str);
156  p->str = o;
157  p->size = len + 128;
158  p->len = len;
159  return o;
160 }
int x
Definition: mipsasm.c:20
int idx
Definition: setup.py:197
RZ_API char * rz_strpool_get_i(RzStrpool *p, int index)
Definition: strpool.c:110

References free(), setup::idx, len, malloc(), memcpy(), NULL, p, rz_strpool_get_i(), and x.