Rizin
unix-like reverse engineering framework and cli tools
buf.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2009-2020 ret2libc <sirmy15@gmail.com>
2 // SPDX-FileCopyrightText: 2009-2020 pancake <pancake@nopcode.org>
3 // SPDX-License-Identifier: LGPL-3.0-only
4 
5 #include <rz_types.h>
6 #include <rz_util.h>
7 #include <rz_io.h>
8 
9 typedef enum {
17 } RzBufferType;
18 
19 #include "buf_file.c"
20 #include "buf_sparse.c"
21 #include "buf_bytes.c"
22 #include "buf_mmap.c"
23 #include "buf_io_fd.c"
24 #include "buf_io.c"
25 #include "buf_ref.c"
26 
27 #define GET_STRING_BUFFER_SIZE 32
28 
29 static void buf_whole_buf_free(RzBuffer *b) {
30  // free the whole_buf only if it was initially allocated by the buf types
31  if (b->methods->get_whole_buf) {
32  if (b->methods->free_whole_buf) {
33  b->methods->free_whole_buf(b);
34  }
35  } else {
36  RZ_FREE(b->whole_buf);
37  }
38 }
39 
40 static bool buf_init(RzBuffer *b, const void *user) {
41  rz_return_val_if_fail(b && b->methods, false);
42 
43  return b->methods->init ? b->methods->init(b, user) : true;
44 }
45 
46 static bool buf_fini(RzBuffer *b) {
47  rz_return_val_if_fail(b && b->methods, false);
48 
49  return b->methods->fini ? b->methods->fini(b) : true;
50 }
51 
53  rz_return_val_if_fail(b && b->methods, UT64_MAX);
54 
55  return b->methods->get_size ? b->methods->get_size(b) : 0;
56 }
57 
58 static st64 buf_read(RzBuffer *b, ut8 *buf, size_t len) {
59  rz_return_val_if_fail(b && b->methods, -1);
60 
61  return b->methods->read ? b->methods->read(b, buf, len) : -1;
62 }
63 
64 static st64 buf_write(RzBuffer *b, const ut8 *buf, size_t len) {
65  rz_return_val_if_fail(b && b->methods, -1);
66 
68 
69  return b->methods->write ? b->methods->write(b, buf, len) : -1;
70 }
71 
72 static st64 buf_seek(RzBuffer *b, st64 addr, int whence) {
73  rz_return_val_if_fail(b && b->methods, -1);
74 
75  return b->methods->seek ? b->methods->seek(b, addr, whence) : -1;
76 }
77 
78 static bool buf_resize(RzBuffer *b, ut64 newsize) {
79  rz_return_val_if_fail(b && b->methods, -1);
80 
81  return b->methods->resize ? b->methods->resize(b, newsize) : false;
82 }
83 
84 static st64 buf_format(RzBuffer *dst, RzBuffer *src, const char *fmt, int n) {
85  st64 res = 0;
86 
87  for (int i = 0; i < n; i++) {
88  int m = 1;
89  int tsize = 2;
90  bool bigendian = true;
91 
92  for (int j = 0; fmt[j]; j++) {
93  switch (fmt[j]) {
94  case '0':
95  case '1':
96  case '2':
97  case '3':
98  case '4':
99  case '5':
100  case '6':
101  case '7':
102  case '8':
103  case '9':
104  if (m == 1) {
105  m = rz_num_get(NULL, &fmt[j]);
106  }
107  continue;
108  case 's':
109  tsize = 2;
110  bigendian = false;
111  break;
112  case 'S':
113  tsize = 2;
114  bigendian = true;
115  break;
116  case 'i':
117  tsize = 4;
118  bigendian = false;
119  break;
120  case 'I':
121  tsize = 4;
122  bigendian = true;
123  break;
124  case 'l':
125  tsize = 8;
126  bigendian = false;
127  break;
128  case 'L':
129  tsize = 8;
130  bigendian = true;
131  break;
132  case 'c':
133  tsize = 1;
134  bigendian = false;
135  break;
136  default: return -1;
137  }
138 
139  for (int k = 0; k < m; k++) {
140  ut8 tmp[sizeof(ut64)];
141  ut8 d1;
142  ut16 d2;
143  ut32 d3;
144  ut64 d4;
145  st64 r = rz_buf_read(src, tmp, tsize);
146  if (r < tsize) {
147  return -1;
148  }
149 
150  switch (tsize) {
151  case 1:
152  d1 = rz_read_ble8(tmp);
153  r = rz_buf_write(dst, (ut8 *)&d1, 1);
154  break;
155  case 2:
156  d2 = rz_read_ble16(tmp, bigendian);
157  r = rz_buf_write(dst, (ut8 *)&d2, 2);
158  break;
159  case 4:
160  d3 = rz_read_ble32(tmp, bigendian);
161  r = rz_buf_write(dst, (ut8 *)&d3, 4);
162  break;
163  case 8:
164  d4 = rz_read_ble64(tmp, bigendian);
165  r = rz_buf_write(dst, (ut8 *)&d4, 8);
166  break;
167  }
168  if (r < 0) {
169  return -1;
170  }
171  res += r;
172  }
173 
174  m = 1;
175  }
176  }
177 
178  return res;
179 }
180 
183 
184  ut64 size = rz_buf_size(b);
185  if (size < addr) {
186  return false;
187  }
188 
189  ut8 *tmp = RZ_NEWS(ut8, size - addr);
190  if (!tmp) {
191  return false;
192  }
193 
194  bool res = false;
195  st64 tmp_length = rz_buf_read_at(b, addr, tmp, size - addr);
196  if (tmp_length < 0) {
197  goto err;
198  }
199 
200  if (!rz_buf_resize(b, size + length)) {
201  goto err;
202  }
203 
204  if (rz_buf_write_at(b, addr + length, tmp, tmp_length) < 0) {
205  goto err;
206  }
207 
208  res = true;
209 err:
210  free(tmp);
211  return res;
212 }
213 
215  rz_return_val_if_fail(b && size && b->methods, NULL);
216 
218 
219  if (b->methods->get_whole_buf) {
220  return b->methods->get_whole_buf(b, size);
221  }
222 
224  // bsz = 4096; // FAKE MINIMUM SIZE TO READ THE BIN HEADER
225  if (buf_size == UT64_MAX) {
226  return NULL;
227  }
228 
229  b->whole_buf = RZ_NEWS(ut8, buf_size);
230  if (!b->whole_buf) {
231  return NULL;
232  }
233 
234  if (rz_buf_read_at(b, 0, b->whole_buf, buf_size) < 0) {
235  RZ_FREE(b->whole_buf);
236  return NULL;
237  }
238 
239  *size = buf_size;
240 
241  return b->whole_buf;
242 }
243 
244 static RzBuffer *new_buffer(RzBufferType type, void *user) {
245  const RzBufferMethods *methods = NULL;
246 
247  switch (type) {
248  case RZ_BUFFER_BYTES:
249  methods = &buffer_bytes_methods;
250  break;
251  case RZ_BUFFER_MMAP:
252  methods = &buffer_mmap_methods;
253  break;
254  case RZ_BUFFER_SPARSE:
255  methods = &buffer_sparse_methods;
256  break;
257  case RZ_BUFFER_FILE:
258  methods = &buffer_file_methods;
259  break;
260  case RZ_BUFFER_IO_FD:
261  methods = &buffer_io_fd_methods;
262  break;
263  case RZ_BUFFER_IO:
264  methods = &buffer_io_methods;
265  break;
266  case RZ_BUFFER_REF:
267  methods = &buffer_ref_methods;
268  break;
269  default:
271  return NULL;
272  }
273 
274  return rz_buf_new_with_methods(methods, user);
275 }
276 
286  ut8 *buf = RZ_NEWS0(ut8, len);
287  if (!buf) {
288  return NULL;
289  }
290 
291  struct buf_bytes_user u = { 0 };
292  u.data_steal = buf;
293  u.length = len;
294  u.steal = true;
295 
296  RzBuffer *res = new_buffer(RZ_BUFFER_BYTES, &u);
297  if (!res) {
298  free(buf);
299  }
300 
301  return res;
302 }
303 
317 RZ_API RZ_OWN RzBuffer *rz_buf_new_file(const char *file, int perm, int mode) {
318  struct buf_file_user u = { 0 };
319 
320  u.file = file;
321  u.perm = perm;
322  u.mode = mode;
323 
324  return new_buffer(RZ_BUFFER_FILE, &u);
325 }
326 
341 
342  struct buf_mmap_user u = { 0 };
343  u.filename = filename;
344  u.perm = perm;
345  u.mode = mode;
346 
347  return new_buffer(RZ_BUFFER_MMAP, &u);
348 }
349 
365  struct buf_ref_user u = { 0 };
366 
367  u.parent = b;
368  u.offset = offset;
369  u.size = size;
370 
371  return new_buffer(RZ_BUFFER_REF, &u);
372 }
373 
374 // TODO: rename to new_from_file ?
385  size_t len;
386  char *tmp = rz_file_slurp(file, &len);
387  if (!tmp) {
388  return NULL;
389  }
390 
391  struct buf_bytes_user u = { 0 };
392 
393  u.data_steal = (ut8 *)tmp;
394  u.length = (ut64)len;
395  u.steal = true;
396 
397  return new_buffer(RZ_BUFFER_BYTES, &u);
398 }
399 
410  if (b) {
411  b->Oxff_priv = Oxff;
412  }
413 
414  return b;
415 }
416 
428 
429  SparseInitConfig cfg = {
430  .base = b,
431  .write_mode = write_mode
432  };
433 
434  return new_buffer(RZ_BUFFER_SPARSE, &cfg);
435 }
436 
449  ut64 size = 0;
450  const ut8 *tmp = get_whole_buf(b, &size);
451 
452  return rz_buf_new_with_bytes(tmp, size);
453 }
454 
466  rz_return_val_if_fail(bytes || !len, NULL); // if bytes == NULL, then len must be 0
467 
468  struct buf_bytes_user u = { 0 };
469  u.data = bytes;
470  u.length = len;
471 
472  return new_buffer(RZ_BUFFER_BYTES, &u);
473 }
474 
475 // TODO: Optimize to use memcpy when buffers are not in range..
476 // check buf boundaries and offsets and use memcpy or memmove
477 
478 // copied from librz/io/cache.c:rz_io_cache_read
479 // ret # of bytes copied
480 
491  rz_return_val_if_fail(iob && fd >= 0, NULL);
492 
493  struct buf_io_fd_user u = { 0 };
494 
495  u.iob = (RzIOBind *)iob;
496  u.fd = fd;
497 
498  return new_buffer(RZ_BUFFER_IO_FD, &u);
499 }
500 
511  return new_buffer(RZ_BUFFER_IO, iob);
512 }
513 
527  if (!b) {
528  return NULL;
529  }
530 
531  b->methods = methods;
532 
533  if (!buf_init(b, init_user)) {
534  free(b);
535  return NULL;
536  }
537 
538  return b;
539 }
540 
553  struct buf_bytes_user u = { 0 };
554 
555  u.data_steal = bytes;
556  u.length = len;
557  u.steal = steal;
558 
559  return new_buffer(RZ_BUFFER_BYTES, &u);
560 }
561 
571  return rz_buf_new_with_bytes((const ut8 *)msg, (ut64)strlen(msg));
572 }
573 
587 
589 
590  while (true) {
591  char tmp[GET_STRING_BUFFER_SIZE + 1];
592  st64 r = rz_buf_read_at(b, addr, (ut8 *)tmp, sizeof(tmp) - 1);
593  if (r < 1) {
595  return NULL;
596  }
597 
598  size_t count = rz_str_nlen(tmp, r);
600 
601  if (count > size) {
603  return NULL;
604  }
605 
606  if (count != r) {
607  break;
608  }
609 
610  addr += r;
611  size -= count;
612  }
613 
614  char *result = rz_strbuf_drain(buf);
615  return result;
616 }
617 
630 
632 }
633 
644 
645  ut64 size = rz_buf_size(b);
646  char *result = RZ_NEWS(char, size + 1);
647  if (!result) {
648  return NULL;
649  }
650 
651  if (rz_buf_read_at(b, 0, (ut8 *)result, size) < 0) {
652  free(result);
653  return NULL;
654  }
655 
656  result[size] = '\0';
657 
658  return result;
659 }
660 
669  if (b) {
670  b->refctr++;
671  }
672 
673  return b;
674 }
675 
686  rz_return_val_if_fail(b && a && !b->readonly, false);
687 
688  ut64 size = 0;
689  const ut8 *tmp = get_whole_buf(a, &size);
690 
691  return rz_buf_append_bytes(b, tmp, size);
692 }
693 
706  rz_return_val_if_fail(b && a && !b->readonly, false);
707 
708  ut8 *tmp = RZ_NEWS(ut8, size);
709  if (!tmp) {
710  return false;
711  }
712 
714  if (r < 0) {
715  free(tmp);
716  return false;
717  }
718 
719  bool result = rz_buf_append_bytes(b, tmp, r);
720 
721  free(tmp);
722  return result;
723 }
724 
733  rz_return_val_if_fail(b && buf && !b->readonly, false);
734 
735  if (rz_buf_seek(b, 0, RZ_BUF_END) < 0) {
736  return false;
737  }
738 
739  return rz_buf_write(b, buf, length) == length;
740 }
741 
749  rz_return_val_if_fail(b && !b->readonly, false);
750 
751  return rz_buf_resize(b, rz_buf_size(b) + length);
752 }
753 
763  rz_return_val_if_fail(b && !b->readonly, false);
764 
765  return rz_buf_append_bytes(b, (const ut8 *)&n, sizeof(n));
766 }
767 
777  rz_return_val_if_fail(b && !b->readonly, false);
778 
779  return rz_buf_append_bytes(b, (const ut8 *)&n, sizeof(n));
780 }
781 
791  rz_return_val_if_fail(b && !b->readonly, false);
792 
793  return rz_buf_append_bytes(b, (const ut8 *)&n, sizeof(n));
794 }
795 
807  rz_return_val_if_fail(b && file, false);
808 
809  ut64 size = 0;
810  const ut8 *tmp = get_whole_buf(b, &size);
811 
812  return rz_file_dump(file, tmp, size, 0);
813 }
814 
823  if (!b) {
824  return false;
825  }
826 
827  if (b->refctr > 0) {
828  b->refctr--;
829  return false;
830  }
831 
833 
834  return buf_fini(b);
835 }
836 
847  rz_return_val_if_fail(b && buf && !b->readonly, false);
848 
849  return rz_buf_insert_bytes(b, 0, buf, length) >= 0;
850 }
851 
861  rz_return_val_if_fail(b && result, false);
862 
863  return rz_buf_read(b, result, sizeof(ut8)) == sizeof(ut8);
864 }
865 
877  rz_return_val_if_fail(b && result, false);
878 
879  return rz_buf_read_at(b, addr, result, sizeof(ut8)) == sizeof(ut8);
880 }
881 
891  rz_return_val_if_fail(b, false);
892 
893  return buf_resize(b, newsize);
894 }
895 
906  rz_return_val_if_fail(b && buf && !b->readonly, false);
907 
908  if (!rz_buf_resize(b, 0)) {
909  return false;
910  }
911 
912  if (rz_buf_seek(b, 0, RZ_BUF_SET) < 0) {
913  return false;
914  }
915 
916  if (!rz_buf_append_bytes(b, buf, length)) {
917  return false;
918  }
919 
920  return rz_buf_seek(b, 0, RZ_BUF_SET) >= 0;
921 }
922 
932  rz_return_val_if_fail(b, false);
933 
934  return rz_buf_write(b, &value, sizeof(ut8)) == sizeof(ut8);
935 }
936 
949  rz_return_val_if_fail(b, false);
950 
951  return rz_buf_write_at(b, addr, &value, sizeof(ut8)) == sizeof(ut8);
952 }
953 
963  rz_return_val_if_fail(b && str && !b->readonly, false);
964 
965  return rz_buf_append_bytes(b, (const ut8 *)str, strlen(str));
966 }
967 
979  rz_return_val_if_fail(b && buf && fmt, -1);
980 
981  // XXX: we assume the caller knows what he's doing
983  st64 res = buf_format(dst, b, fmt, n);
984 
985  rz_buf_free(dst);
986 
987  return res;
988 }
989 
1002  rz_return_val_if_fail(b && buf && fmt, -1);
1003 
1004  st64 tmp = rz_buf_tell(b);
1005  if (tmp < 0) {
1006  return -1;
1007  }
1008 
1009  if (rz_buf_seek(b, addr, RZ_BUF_SET) < 0) {
1010  return -1;
1011  }
1012 
1013  st64 result = rz_buf_fread(b, buf, fmt, n);
1014 
1015  if (rz_buf_seek(b, tmp, RZ_BUF_SET) < 0) {
1016  return -1;
1017  }
1018 
1019  return result;
1020 }
1021 
1034  rz_return_val_if_fail(b && buf && fmt && !b->readonly, -1);
1035 
1036  // XXX: we assume the caller knows what he's doing
1038  st64 result = buf_format(b, src, fmt, n);
1039 
1040  rz_buf_free(src);
1041 
1042  return result;
1043 }
1044 
1057  rz_return_val_if_fail(b && buf && fmt && !b->readonly, -1);
1058 
1059  st64 tmp = rz_buf_tell(b);
1060  if (tmp < 0) {
1061  return -1;
1062  }
1063 
1064  if (rz_buf_seek(b, addr, RZ_BUF_SET) < 0) {
1065  return -1;
1066  }
1067 
1068  st64 result = rz_buf_fwrite(b, buf, fmt, n);
1069 
1070  if (rz_buf_seek(b, tmp, RZ_BUF_SET) < 0) {
1071  return -1;
1072  }
1073 
1074  return result;
1075 }
1076 
1088  rz_return_val_if_fail(b && !b->readonly, -1);
1089 
1090  if (!buf_move_back(b, addr, length)) {
1091  return -1;
1092  }
1093 
1094  st64 result = rz_buf_write_at(b, addr, buf, length);
1095  if (result < 0) {
1096  return -1;
1097  }
1098 
1099  return result;
1100 }
1101 
1112  rz_return_val_if_fail(b && buf, -1);
1113 
1114  st64 result = buf_read(b, buf, len);
1115  if (result < 0) {
1116  return -1;
1117  }
1118 
1119  if (len > result) {
1120  memset(buf + result, b->Oxff_priv, len - result);
1121  }
1122 
1123  return result;
1124 }
1125 
1137  rz_return_val_if_fail(b && buf, -1);
1138 
1139  st64 tmp = rz_buf_tell(b);
1140  if (tmp < 0) {
1141  return -1;
1142  }
1143 
1144  if (rz_buf_seek(b, addr, RZ_BUF_SET) < 0) {
1145  return -1;
1146  }
1147 
1148  st64 result = rz_buf_read(b, buf, len);
1149 
1150  if (rz_buf_seek(b, tmp, RZ_BUF_SET) < 0) {
1151  return -1;
1152  }
1153 
1154  return result;
1155 }
1156 
1167  rz_return_val_if_fail(b, -1);
1168 
1169  return buf_seek(b, addr, whence);
1170 }
1171 
1182  rz_return_val_if_fail(b && buf && !b->readonly, -1);
1183 
1184  return buf_write(b, buf, len);
1185 }
1186 
1198  rz_return_val_if_fail(b && buf && !b->readonly, -1);
1199 
1200  st64 tmp = rz_buf_tell(b);
1201  if (tmp < 0) {
1202  return -1;
1203  }
1204 
1205  if (rz_buf_seek(b, addr, RZ_BUF_SET) < 0) {
1206  return -1;
1207  }
1208 
1209  st64 result = rz_buf_write(b, buf, len);
1210 
1211  if (rz_buf_seek(b, tmp, RZ_BUF_SET) < 0) {
1212  return -1;
1213  }
1214 
1215  return result;
1216 }
1217 
1227 
1228  return buf_get_size(b);
1229 }
1230 
1240 
1241  return rz_buf_seek(b, 0, RZ_BUF_CUR);
1242 }
1243 
1254  if (!b) {
1255  return;
1256  }
1257  if (rz_buf_fini(b)) {
1258  free(b);
1259  }
1260 }
1261 
1274 
1275  b->Oxff_priv = Oxff;
1276 }
1277 
1289 
1290  return get_whole_buf(b, size);
1291 }
1292 
1304  rz_return_val_if_fail(b && fwd_scan, 0);
1305  if (!amount) {
1306  return 0;
1307  }
1308  if (b->methods->get_whole_buf) {
1309  ut64 sz;
1310  const ut8 *buf = b->methods->get_whole_buf(b, &sz);
1311  if (buf) {
1312  if (sz <= start) {
1313  return 0;
1314  }
1315  return fwd_scan(buf + start, RZ_MIN(sz - start, amount), user);
1316  }
1317  }
1318  const ut64 size = rz_buf_size(b);
1319  if (size <= start) {
1320  return 0;
1321  }
1322  if (b->whole_buf) {
1323  return fwd_scan(b->whole_buf + start, RZ_MIN(size - start, amount), user);
1324  }
1325  ut64 addr = start;
1326  const ut64 user_end = UT64_ADD_OVFCHK(start, amount) ? UT64_MAX : start + amount;
1327  const ut64 end = RZ_MIN(user_end, size);
1328  const size_t buf_size = RZ_MIN(end - start, 0x1000);
1329  ut8 *buf = malloc(buf_size);
1330  if (!buf) {
1331  return 0;
1332  }
1333  while (addr < end) {
1334  const ut64 read_amount = RZ_MIN(buf_size, end - addr);
1335  rz_buf_read_at(b, addr, buf, read_amount);
1336  ut64 read = fwd_scan(buf, read_amount, user);
1337  if (!read) {
1338  break;
1339  }
1340  addr += read;
1341  }
1342  free(buf);
1343  return addr - start;
1344 }
1345 
1355 
1356  ut64 sum = 0, used = 0, slice;
1357  ut32 shift = 0;
1358  ut8 byte = 0;
1359  do {
1360  if (rz_buf_read(buffer, &byte, sizeof(byte)) < 1) {
1361  // malformed uleb128 due end of buffer
1362  return -1;
1363  }
1364  used++;
1365  slice = byte & 0x7f;
1366  if ((shift >= 64 && slice != 0) || ((slice << shift) >> shift) != slice) {
1367  // uleb128 too big for ut64
1368  return -1;
1369  }
1370  sum += slice << shift;
1371  shift += 7;
1372  } while (byte >= 128);
1373  *value = sum;
1374  return used;
1375 }
1376 
1386 
1387  ut64 used = 0, slice;
1388  st64 sum = 0;
1389  ut32 shift = 0;
1390  ut8 byte = 0;
1391  do {
1392  if (rz_buf_read(buffer, &byte, sizeof(byte)) < 1) {
1393  // malformed sleb128 due end of buffer
1394  return -1;
1395  }
1396  used++;
1397  slice = byte & 0x7f;
1398  if ((shift >= 64 && slice != (sum < 0 ? 0x7f : 0x00)) ||
1399  (shift == 63 && slice != 0 && slice != 0x7f)) {
1400  // sleb128 too big for st64
1401  return -1;
1402  }
1403  sum |= slice << shift;
1404  shift += 7;
1405  } while (byte >= 128);
1406 
1407  if (shift < 64 && (byte & 0x40)) {
1408  // extends negative sign
1409  sum |= (-1ull) << shift;
1410  }
1411  *value = sum;
1412  return used;
1413 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
lzma_index * src
Definition: index.h:567
static RZ_NULLABLE RzILOpBitVector * shift(RzILOpBitVector *val, RZ_NULLABLE RzILOpBool **carry_out, arm_shifter type, RZ_OWN RzILOpBitVector *dist)
Definition: arm_il32.c:190
static bool err
Definition: armass.c:435
static ut8 bytes[32]
Definition: asm_arc.c:23
RZ_API bool rz_buf_set_bytes(RZ_NONNULL RzBuffer *b, RZ_NONNULL const ut8 *buf, ut64 length)
Replace the content of the buffer with the bytes array.
Definition: buf.c:905
RZ_API bool rz_buf_append_nbytes(RZ_NONNULL RzBuffer *b, ut64 length)
Extend the size of the buffer.
Definition: buf.c:748
RZ_API RZ_OWN RzBuffer * rz_buf_new_mmap(const char *filename, int perm, int mode)
Creates a new buffer from a file using rz_file_mmap.
Definition: buf.c:339
RZ_API ut64 rz_buf_tell(RZ_NONNULL RzBuffer *b)
Return the current cursor position.
Definition: buf.c:1238
RZ_API st64 rz_buf_insert_bytes(RZ_NONNULL RzBuffer *b, ut64 addr, RZ_NONNULL const ut8 *buf, ut64 length)
Insert an array of bytes in the buffer.
Definition: buf.c:1087
RZ_API RZ_OWN RzBuffer * rz_buf_new_with_methods(RZ_NONNULL const RzBufferMethods *methods, void *init_user)
Creates a new buffer with a specific back end.
Definition: buf.c:525
RZ_API RZ_OWN char * rz_buf_get_nstring(RZ_NONNULL RzBuffer *b, ut64 addr, size_t size)
Get a string with a max length from the buffer.
Definition: buf.c:585
RZ_API bool rz_buf_resize(RZ_NONNULL RzBuffer *b, ut64 newsize)
Resize the buffer size.
Definition: buf.c:890
RZ_API bool rz_buf_append_bytes(RZ_NONNULL RzBuffer *b, RZ_NONNULL const ut8 *buf, ut64 length)
Append an array of bytes to the buffer.
Definition: buf.c:732
#define GET_STRING_BUFFER_SIZE
Definition: buf.c:27
static ut64 buf_get_size(RzBuffer *b)
Definition: buf.c:52
RZ_API bool rz_buf_append_ut32(RZ_NONNULL RzBuffer *b, ut32 n)
Add a ut32 number at the end of the buffer.
Definition: buf.c:776
RZ_API st64 rz_buf_fwrite_at(RZ_NONNULL RzBuffer *b, ut64 addr, RZ_NONNULL const ut8 *buf, RZ_NONNULL const char *fmt, int n)
...
Definition: buf.c:1056
RZ_API st64 rz_buf_seek(RZ_NONNULL RzBuffer *b, st64 addr, int whence)
Modify the current cursor position in the buffer.
Definition: buf.c:1166
RZ_API st64 rz_buf_fread(RZ_NONNULL RzBuffer *b, RZ_NONNULL ut8 *buf, RZ_NONNULL const char *fmt, int n)
...
Definition: buf.c:978
static bool buf_move_back(RZ_NONNULL RzBuffer *b, ut64 addr, ut64 length)
Definition: buf.c:181
RZ_API bool rz_buf_append_ut64(RZ_NONNULL RzBuffer *b, ut64 n)
Add a ut64 number at the end of the buffer.
Definition: buf.c:790
RZ_API bool rz_buf_dump(RZ_NONNULL RzBuffer *b, RZ_NONNULL const char *file)
Dump the content of the buffer to a file.
Definition: buf.c:806
RZ_API RZ_OWN char * rz_buf_get_string(RZ_NONNULL RzBuffer *b, ut64 addr)
Get a string from the buffer.
Definition: buf.c:628
static st64 buf_format(RzBuffer *dst, RzBuffer *src, const char *fmt, int n)
Definition: buf.c:84
static ut8 * get_whole_buf(RzBuffer *b, ut64 *size)
Definition: buf.c:214
static st64 buf_seek(RzBuffer *b, st64 addr, int whence)
Definition: buf.c:72
RZ_API st64 rz_buf_read(RZ_NONNULL RzBuffer *b, RZ_NONNULL ut8 RZ_OUT *buf, ut64 len)
Read len bytes of the buffer at the cursor.
Definition: buf.c:1111
RZ_API st64 rz_buf_fwrite(RZ_NONNULL RzBuffer *b, RZ_NONNULL const ut8 *buf, RZ_NONNULL const char *fmt, int n)
...
Definition: buf.c:1033
RZ_API bool rz_buf_fini(RzBuffer *b)
Free all internal data hold by the buffer.
Definition: buf.c:822
RZ_API bool rz_buf_prepend_bytes(RZ_NONNULL RzBuffer *b, RZ_NONNULL const ut8 *buf, ut64 length)
Prepend an array of bytes to the buffer.
Definition: buf.c:846
RZ_API st64 rz_buf_write_at(RZ_NONNULL RzBuffer *b, ut64 addr, RZ_NONNULL const ut8 *buf, ut64 len)
Write len bytes of the buffer at the specified address.
Definition: buf.c:1197
RZ_API RzBuffer * rz_buf_ref(RzBuffer *b)
Increment the reference count of the buffer.
Definition: buf.c:668
RZ_API bool rz_buf_read8_at(RzBuffer *b, ut64 addr, RZ_NONNULL RZ_OUT ut8 *result)
Read a byte at the specified address in the buffer.
Definition: buf.c:876
RZ_API RZ_OWN RzBuffer * rz_buf_new_slurp(const char *file)
Creates a new buffer from a file.
Definition: buf.c:384
RZ_API RZ_OWN RzBuffer * rz_buf_new_with_buf(RzBuffer *b)
Creates a new buffer from a source buffer.
Definition: buf.c:448
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
RZ_API RZ_OWN RzBuffer * rz_buf_new_with_io_fd(RZ_NONNULL void *iob, int fd)
Creates a new buffer wrapping a file descriptor accessed through RzIOBind.
Definition: buf.c:490
static st64 buf_read(RzBuffer *b, ut8 *buf, size_t len)
Definition: buf.c:58
static bool buf_fini(RzBuffer *b)
Definition: buf.c:46
RZ_API st64 rz_buf_uleb128(RZ_NONNULL RzBuffer *buffer, RZ_NONNULL ut64 *value)
Decodes ULEB128 from RzBuffer.
Definition: buf.c:1353
static RzBuffer * new_buffer(RzBufferType type, void *user)
Definition: buf.c:244
RZ_API st64 rz_buf_append_string(RZ_NONNULL RzBuffer *b, RZ_NONNULL const char *str)
Append a string to the buffer.
Definition: buf.c:962
RZ_API bool rz_buf_read8(RZ_NONNULL RzBuffer *b, RZ_NONNULL RZ_OUT ut8 *result)
Read a byte at the cursor in the buffer.
Definition: buf.c:860
RZ_API RZ_OWN RzBuffer * rz_buf_new_with_pointers(const ut8 *bytes, ut64 len, bool steal)
Creates a new buffer with a bytes array.
Definition: buf.c:552
RZ_API st64 rz_buf_fread_at(RZ_NONNULL RzBuffer *b, ut64 addr, RZ_NONNULL ut8 *buf, RZ_NONNULL const char *fmt, int n)
...
Definition: buf.c:1001
RZ_API bool rz_buf_write8_at(RZ_NONNULL RzBuffer *b, ut64 addr, ut8 value)
Write a byte at the specified address in the buffer.
Definition: buf.c:948
RZ_API st64 rz_buf_write(RZ_NONNULL RzBuffer *b, RZ_NONNULL const ut8 *buf, ut64 len)
Write len bytes of the buffer at the cursor.
Definition: buf.c:1181
RZ_API bool rz_buf_write8(RZ_NONNULL RzBuffer *b, ut8 value)
Write a byte at the cursor in the buffer.
Definition: buf.c:931
RZ_API RZ_OWN RzBuffer * rz_buf_new_with_string(RZ_NONNULL const char *msg)
Creates a new buffer from a string.
Definition: buf.c:570
RZ_API bool rz_buf_append_ut16(RZ_NONNULL RzBuffer *b, ut16 n)
Add a ut16 number at the end of the buffer.
Definition: buf.c:762
RZ_API void rz_buf_free(RzBuffer *b)
Free all internal data hold by the buffer and the buffer.
Definition: buf.c:1253
RZ_API RZ_OWN RzBuffer * rz_buf_new_with_io(RZ_NONNULL void *iob)
Creates a new buffer wrapping the memory map exposed by RzIOBind.
Definition: buf.c:509
RZ_API bool rz_buf_append_buf(RZ_NONNULL RzBuffer *b, RZ_NONNULL RzBuffer *a)
Append the content of the buffer a to the buffer b.
Definition: buf.c:685
RZ_API RZ_OWN RzBuffer * rz_buf_new_empty(ut64 len)
Creates a new empty buffer with a predefined size;.
Definition: buf.c:285
RZ_API RZ_OWN RzBuffer * rz_buf_new_slice(RzBuffer *b, ut64 offset, ut64 size)
Creates a new buffer from a slice of another buffer.
Definition: buf.c:364
RZ_API RZ_OWN RzBuffer * rz_buf_new_sparse(ut8 Oxff)
Creates a sparse buffer.
Definition: buf.c:408
RZ_API RZ_OWN RzBuffer * rz_buf_new_with_bytes(RZ_NULLABLE RZ_BORROW const ut8 *bytes, ut64 len)
Creates a new buffer with a bytes array.
Definition: buf.c:465
RZ_API ut64 rz_buf_fwd_scan(RZ_NONNULL RzBuffer *b, ut64 start, ut64 amount, RZ_NONNULL RzBufferFwdScan fwd_scan, RZ_NULLABLE void *user)
Scans buffer linearly in chunks calling fwd_scan for each chunk.
Definition: buf.c:1303
static bool buf_resize(RzBuffer *b, ut64 newsize)
Definition: buf.c:78
static bool buf_init(RzBuffer *b, const void *user)
Definition: buf.c:40
static st64 buf_write(RzBuffer *b, const ut8 *buf, size_t len)
Definition: buf.c:64
RZ_DEPRECATE RZ_API RZ_BORROW ut8 * rz_buf_data(RZ_NONNULL RzBuffer *b, RZ_NONNULL RZ_OUT ut64 *size)
Return a borrowed array of bytes representing the buffer data.
Definition: buf.c:1287
RZ_API ut64 rz_buf_size(RZ_NONNULL RzBuffer *b)
Return the size of the buffer.
Definition: buf.c:1225
static void buf_whole_buf_free(RzBuffer *b)
Definition: buf.c:29
RZ_API RZ_OWN RzBuffer * rz_buf_new_file(const char *file, int perm, int mode)
Creates a new buffer from a file.
Definition: buf.c:317
RZ_API void rz_buf_set_overflow_byte(RZ_NONNULL RzBuffer *b, ut8 Oxff)
Change the overflow byte used in the RZ_BUFFER_SPARSE.
Definition: buf.c:1272
RzBufferType
Definition: buf.c:9
@ RZ_BUFFER_SPARSE
Definition: buf.c:15
@ RZ_BUFFER_MMAP
Definition: buf.c:14
@ RZ_BUFFER_FILE
Definition: buf.c:10
@ RZ_BUFFER_IO_FD
Definition: buf.c:11
@ RZ_BUFFER_IO
Definition: buf.c:12
@ RZ_BUFFER_REF
Definition: buf.c:16
@ RZ_BUFFER_BYTES
Definition: buf.c:13
RZ_API RZ_OWN char * rz_buf_to_string(RZ_NONNULL RzBuffer *b)
Stringify the buffer.
Definition: buf.c:642
RZ_API RZ_OWN RzBuffer * rz_buf_new_sparse_overlay(RzBuffer *b, RzBufferSparseWriteMode write_mode)
Creates a sparse buffer from a already populated buffer.
Definition: buf.c:426
RZ_API st64 rz_buf_sleb128(RZ_NONNULL RzBuffer *buffer, RZ_NONNULL st64 *value)
Decodes SLEB128 from RzBuffer.
Definition: buf.c:1384
RZ_API bool rz_buf_append_buf_slice(RZ_NONNULL RzBuffer *b, RZ_NONNULL RzBuffer *a, ut64 offset, ut64 size)
Append a slice of the buffer a to the buffer b.
Definition: buf.c:705
static const RzBufferMethods buffer_bytes_methods
Definition: buf_bytes.c:118
static const RzBufferMethods buffer_file_methods
Definition: buf_file.c:81
static const RzBufferMethods buffer_io_methods
Definition: buf_io.c:54
static const RzBufferMethods buffer_io_fd_methods
Definition: buf_io_fd.c:88
static const RzBufferMethods buffer_mmap_methods
Definition: buf_mmap.c:73
static const RzBufferMethods buffer_ref_methods
Definition: buf_ref.c:96
static const RzBufferMethods buffer_sparse_methods
Definition: buf_sparse.c:302
static int value
Definition: cmd_api.c:93
#define RZ_API
#define NULL
Definition: cris-opc.c:27
#define r
Definition: crypto_rc6.c:12
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
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 static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void start
Definition: sflib.h:133
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 static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void length
Definition: sflib.h:133
#define ut8
Definition: dcpu16.h:8
static int buf_size
Definition: debug_qnx.c:35
uint16_t ut16
uint32_t ut32
const char * k
Definition: dsignal.c:11
d4
Definition: fp-armv8.s.cs:15
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void uLong size
Definition: ioapi.h:138
const char * filename
Definition: ioapi.h:137
voidpf uLong offset
Definition: ioapi.h:144
const char int mode
Definition: ioapi.h:137
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
return memset(p, 0, total)
void * malloc(size_t size)
Definition: malloc.c:123
char * dst
Definition: lz4.h:724
int n
Definition: mipsasm.c:19
int type
Definition: mipsasm.c:17
#define rz_warn_if_reached()
Definition: rz_assert.h:29
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
ut64(* RzBufferFwdScan)(RZ_BORROW RZ_NONNULL const ut8 *buf, ut64 len, RZ_NULLABLE void *user)
Definition: rz_buf.h:136
#define RZ_BUF_CUR
Definition: rz_buf.h:15
#define RZ_BUF_END
Definition: rz_buf.h:16
RzBufferSparseWriteMode
Definition: rz_buf.h:59
#define RZ_BUF_SET
Definition: rz_buf.h:14
static ut64 rz_read_ble64(const void *src, bool big_endian)
Definition: rz_endian.h:501
static ut8 rz_read_ble8(const void *src)
Definition: rz_endian.h:12
static ut32 rz_read_ble32(const void *src, bool big_endian)
Definition: rz_endian.h:497
static ut16 rz_read_ble16(const void *src, bool big_endian)
Definition: rz_endian.h:493
RZ_API RZ_OWN char * rz_file_slurp(const char *str, RZ_NULLABLE size_t *usz)
Definition: file.c:454
RZ_API bool rz_file_dump(const char *file, const ut8 *buf, int len, bool append)
Definition: file.c:838
RZ_API ut64 rz_num_get(RzNum *num, const char *str)
Definition: unum.c:172
RZ_API size_t rz_str_nlen(const char *s, size_t n)
Definition: str.c:1949
RZ_API RZ_OWN char * rz_strbuf_drain(RzStrBuf *sb)
Definition: strbuf.c:342
RZ_API RzStrBuf * rz_strbuf_new(const char *s)
Definition: strbuf.c:8
RZ_API void rz_strbuf_free(RzStrBuf *sb)
Definition: strbuf.c:358
RZ_API bool rz_strbuf_append_n(RzStrBuf *sb, const char *s, size_t l)
Definition: strbuf.c:229
#define RZ_NEWS(x, y)
Definition: rz_types.h:283
#define RZ_NULLABLE
Definition: rz_types.h:65
#define RZ_OWN
Definition: rz_types.h:62
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_OUT
Definition: rz_types.h:51
#define RZ_NONNULL
Definition: rz_types.h:64
#define RZ_NEWS0(x, y)
Definition: rz_types.h:282
#define RZ_FREE(x)
Definition: rz_types.h:369
#define RZ_BORROW
Definition: rz_types.h:63
#define RZ_DEPRECATE
Definition: rz_types.h:66
#define RZ_MIN(x, y)
#define st64
Definition: rz_types_base.h:10
#define UT64_MAX
Definition: rz_types_base.h:86
#define UT64_ADD_OVFCHK(x, y)
static struct sockaddr static addrlen static backlog const void msg
Definition: sfsocketcall.h:119
#define b(i)
Definition: sha256.c:42
#define a(i)
Definition: sha256.c:41
const ut8 * data
Definition: buf_bytes.c:7
const ut8 * data_steal
Definition: buf_bytes.c:8
const char * file
Definition: buf_file.c:8
RzIOBind * iob
Definition: buf_io_fd.c:8
const char * filename
Definition: buf_mmap.c:7
RzBuffer * parent
Definition: buf_ref.c:7
ut64 offset
Definition: buf_ref.c:8
ut64 size
Definition: buf_ref.c:9
Definition: buffer.h:15
Definition: gzappend.c:170
RzBuffer * base
Definition: buf_sparse.c:8
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static const z80_opcode fd[]
Definition: z80_tab.h:997
static int file
Definition: z80asm.c:58
static int addr
Definition: z80asm.c:58
int read(izstream &zs, T *x, Items items)
Definition: zstream.h:115