Rizin
unix-like reverse engineering framework and cli tools
gdata.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2014-2020 inisider <inisider@gmail.com>
2 // SPDX-FileCopyrightText: 2021 Basstorm <basstorm@nyist.edu.cn>
3 // SPDX-License-Identifier: LGPL-3.0-only
4 
5 #include "pdb.h"
6 
7 static bool parse_gdata_global(GDataGlobal *global, RzBuffer *buf, ut32 initial_seek) {
8  if (!rz_buf_read_le32(buf, &global->symtype) ||
9  !rz_buf_read_le32(buf, &global->offset)) {
10  return false;
11  }
12  if (!rz_buf_read_le16(buf, &global->segment)) {
13  return false;
14  }
15  if (global->leaf_type == 0x110E) {
17  ut16 len = strlen(global->name) + 1;
18  global->name_len = len;
20  } else {
21  if (!rz_buf_read8(buf, &global->name_len)) {
22  return false;
23  }
24  }
25  ut32 read_len = rz_buf_tell(buf) - initial_seek;
26  if (read_len % 4) {
27  ut16 remain = 4 - (read_len % 4);
28  rz_buf_seek(buf, remain, RZ_BUF_CUR);
29  }
30  return true;
31 }
32 
34  rz_return_val_if_fail(pdb && stream, false);
35  if (!pdb->s_gdata) {
37  }
38  RzPdbGDataStream *s = pdb->s_gdata;
39  if (!s) {
40  RZ_LOG_ERROR("Error allocating memory.\n");
41  return false;
42  }
43  RzBuffer *buf = stream->stream_data;
44  s->global_list = rz_list_new();
45  if (!s->global_list) {
46  return false;
47  }
48  ut16 len;
49  while (true) {
50  ut32 initial_seek = rz_buf_tell(buf);
51  if (!rz_buf_read_le16(buf, &len)) {
52  break;
53  }
54  if (len == 0 || len == UT16_MAX) {
55  break;
56  }
57  ut16 leaf_type;
58  if (!rz_buf_read_le16(buf, &leaf_type)) {
59  return false;
60  }
61  if (leaf_type == 0x110E || leaf_type == 0x1009) {
62  GDataGlobal *global = RZ_NEW0(GDataGlobal);
63  if (!global) {
64  goto skip;
65  }
66  global->leaf_type = leaf_type;
67  if (!parse_gdata_global(global, buf, initial_seek)) {
68  RZ_FREE(global);
69  return false;
70  }
71  rz_list_append(s->global_list, global);
72  continue;
73  }
74  skip:
75  rz_buf_seek(buf, len - sizeof(ut16), RZ_BUF_CUR);
76  }
77  return true;
78 }
79 
81  if (!stream) {
82  return;
83  }
84  GDataGlobal *global;
85  RzListIter *it;
86  rz_list_foreach (stream->global_list, it, global) {
87  RZ_FREE(global->name);
88  RZ_FREE(global);
89  }
90  rz_list_free(stream->global_list);
91  free(stream);
92 }
size_t len
Definition: 6502dis.c:15
#define RZ_IPI
Definition: analysis_wasm.c:11
uint16_t ut16
uint32_t ut32
RZ_IPI bool parse_gdata_stream(RzPdb *pdb, RzPdbMsfStream *stream)
Definition: gdata.c:33
static bool parse_gdata_global(GDataGlobal *global, RzBuffer *buf, ut32 initial_seek)
Definition: gdata.c:7
RZ_IPI void free_gdata_stream(RzPdbGDataStream *stream)
Definition: gdata.c:80
void skip(file *in, unsigned n)
Definition: gzappend.c:202
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf stream
Definition: ioapi.h:138
voidpf void * buf
Definition: ioapi.h:138
RZ_API RZ_OWN RzList * rz_list_new(void)
Returns a new initialized RzList pointer (free method is not initialized)
Definition: list.c:235
RZ_API RZ_BORROW RzListIter * rz_list_append(RZ_NONNULL RzList *list, void *data)
Appends at the end of the list a new element.
Definition: list.c:288
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
static RzSocket * s
Definition: rtr.c:28
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
RZ_API ut64 rz_buf_tell(RZ_NONNULL RzBuffer *b)
Return the current cursor position.
Definition: buf.c:1238
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
#define RZ_BUF_CUR
Definition: rz_buf.h:15
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
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
#define rz_buf_read_le16(b, result)
Read a big endian or little endian (ut16, ut32, ut64) at the specified offset in the buffer and shift...
Definition: rz_buf.h:266
#define rz_buf_read_le32(b, result)
Definition: rz_buf.h:267
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_FREE(x)
Definition: rz_types.h:369
#define UT16_MAX
ut16 leaf_type
Definition: gdata.h:10
ut32 offset
Definition: gdata.h:12
ut16 segment
Definition: gdata.h:13
char * name
Definition: gdata.h:14
ut8 name_len
Definition: gdata.h:15
ut32 symtype
Definition: gdata.h:11
RzPdbGDataStream * s_gdata
Definition: rz_pdb.h:248