static ut32 get_ut32(RBuffer *buffer, bool *error) { ut32 ret = 0; int size = r_buf_read (buffer, (ut8*)&ret, sizeof (ret)); if (size < sizeof (ret)) *error = true; return ret; }
static int read_int_var(char *var_name, int *var, R_PDB *pdb) { int bytes_read = r_buf_read(pdb->buf, (unsigned char *)var, 4); if (bytes_read != 4) { eprintf ("error while reading from file [%s]", var_name); return 0; } return bytes_read; }
static ut8 *get_bytes(RBuffer *buffer, ut32 size) { ut8 *ret = R_NEWS0 (ut8, size + 1); if (!ret) return NULL; if (r_buf_read (buffer, ret, size) < size) { free (ret); return NULL; } return ret; }
int init_pdb_parser(R_PDB *pdb, const char *filename) { char *signature = NULL; int bytes_read = 0; if (!pdb) { eprintf ("struct R_PDB is not correct\n"); goto error; } if (!pdb->cb_printf) pdb->cb_printf = (PrintfCallback)printf; pdb->buf = r_buf_file(filename); // pdb->fp = r_sandbox_fopen (filename, "rb"); // if (!pdb->fp) { // eprintf ("file %s can not be open\n", filename); // goto error; // } signature = (char *)calloc (1, PDB7_SIGNATURE_LEN); if (!signature) { eprintf ("memory allocation error\n"); goto error; } bytes_read = r_buf_read(pdb->buf, (unsigned char *)signature, PDB7_SIGNATURE_LEN); if (bytes_read != PDB7_SIGNATURE_LEN) { eprintf ("file reading error\n"); goto error; } r_buf_seek(pdb->buf, 0, 0); if (!memcmp (signature, PDB7_SIGNATURE, PDB7_SIGNATURE_LEN)) { pdb->pdb_parse = pdb7_parse; } else { goto error; } R_FREE (signature); pdb->pdb_streams = r_list_new (); pdb->stream_map = 0; pdb->finish_pdb_parse = finish_pdb_parse; pdb->print_types = print_types; pdb->print_gvars = print_gvars; // printf("init_pdb_parser() finish with success\n"); return 1; error: R_FREE (signature); return 0; }
static bool check_bytes_buf(RBuffer* rbuf) { ut8 buf[4] = {0}; return rbuf && r_buf_read (rbuf, buf, 4) == 4 && !memcmp (buf, R_BIN_WASM_MAGIC_BYTES, 4); }
static bool pdb7_parse(R_PDB *pdb) { char signature[PDB7_SIGNATURE_LEN + 1]; int num_root_index_pages = 0; int *root_index_pages = 0; void *root_page_data = 0; int *root_page_list = 0; int num_root_pages = 0; int num_file_pages = 0; int alloc_tbl_ptr = 0; int bytes_read = 0; int page_size = 0; int root_size = 0; int reserved = 0; void *p_tmp; int i = 0; bytes_read = r_buf_read(pdb->buf, (unsigned char *)signature, PDB7_SIGNATURE_LEN); if (bytes_read != PDB7_SIGNATURE_LEN) { eprintf ("error while reading PDB7_SIGNATURE\n"); goto error; } if (!read_int_var ("page_size", &page_size, pdb)) { goto error; } if (!read_int_var ("alloc_tbl_ptr", &alloc_tbl_ptr, pdb)) { goto error; } if (!read_int_var ("num_file_pages", &num_file_pages, pdb)) { goto error; } if (!read_int_var ("root_size", &root_size, pdb)) { goto error; } if (!read_int_var("reserved", &reserved, pdb)) { goto error; } // FIXME: why they is not equal ???? // if (memcmp(signature, PDB7_SIGNATURE, PDB7_SIGNATURE_LEN) != 0) { // printf("Invalid signature for PDB7 format\n"); // //goto error; // } num_root_pages = count_pages (root_size, page_size); num_root_index_pages = count_pages ((num_root_pages * 4), page_size); root_index_pages = (int *)calloc (sizeof (int), R_MAX (num_root_index_pages, 1)); if (!root_index_pages) { eprintf("error memory allocation\n"); goto error; } // bytes_read = fread(root_index_pages, 4, num_root_index_pages, pdb->fp); bytes_read = r_buf_read(pdb->buf, (unsigned char *)root_index_pages, 4 * num_root_index_pages); //fread(root_index_pages, 4, num_root_index_pages, pdb->fp); if (bytes_read != 4 * num_root_index_pages) { eprintf ("error while reading root_index_pages\n"); goto error; } root_page_data = (int *)calloc (page_size, num_root_index_pages); if (!root_page_data) { eprintf ("error memory allocation of root_page_data\n"); goto error; } p_tmp = root_page_data; for (i = 0; i < num_root_index_pages; i++) { r_buf_seek(pdb->buf, root_index_pages[i] * page_size, 0); r_buf_read(pdb->buf, p_tmp, page_size); p_tmp = (char *)p_tmp + page_size; } root_page_list = (int *)calloc (sizeof(int), num_root_pages); if (!root_page_list) { eprintf ("error: memory allocation of root page\n"); goto error; } p_tmp = root_page_data; for (i = 0; i < num_root_pages; i++) { root_page_list[i] = *((int *)p_tmp); p_tmp = (int *)p_tmp + 1; } pdb->pdb_streams2 = 0; if (!init_pdb7_root_stream (pdb, root_page_list, num_root_pages, ePDB_STREAM_ROOT, root_size, page_size)) { eprintf ("root stream has not initialized\n"); goto error; } if (!pdb_read_root (pdb)) { eprintf ("pdb root has not initialized\n"); goto error; } R_FREE (root_page_list); R_FREE (root_page_data); R_FREE (root_index_pages); return true; error: R_FREE (root_page_list); R_FREE (root_page_data); R_FREE (root_index_pages); return false; }