static mrb_irep* read_irep_record_file(mrb_state *mrb, FILE *fp) { uint8_t header[1 + 4]; const size_t record_header_size = sizeof(header); size_t buf_size, i; size_t len; mrb_irep *irep = NULL; void *ptr; uint8_t *buf; if (fread(header, record_header_size, 1, fp) == 0) { return NULL; } buf_size = (size_t)bin_to_uint32(&header[0]); if (SIZE_ERROR(buf_size)) { return NULL; } ptr = mrb_malloc(mrb, buf_size); buf = (uint8_t *)ptr; memcpy(buf, header, record_header_size); if (fread(&buf[record_header_size], buf_size - record_header_size, 1, fp) == 0) { return NULL; } irep = read_irep_record_1(mrb, buf, &len, TRUE); mrb_free(mrb, ptr); if (!irep) return NULL; for (i=0; i<irep->rlen; i++) { irep->reps[i] = read_irep_record_file(mrb, fp); if (!irep->reps[i]) return NULL; } return irep; }
static int read_lineno_record_file(mrb_state *mrb, FILE *fp, mrb_irep *irep) { uint8_t header[4]; const size_t record_header_size = sizeof(header); int result; size_t i, buf_size; size_t len; void *ptr; uint8_t *buf; if (fread(header, record_header_size, 1, fp) == 0) { return MRB_DUMP_READ_FAULT; } buf_size = (size_t)bin_to_uint32(&header[0]); if (SIZE_ERROR(buf_size)) { return MRB_DUMP_GENERAL_FAILURE; } ptr = mrb_malloc(mrb, buf_size); buf = (uint8_t *)ptr; if (fread(&buf[record_header_size], buf_size - record_header_size, 1, fp) == 0) { return MRB_DUMP_READ_FAULT; } result = read_lineno_record_1(mrb, buf, irep, &len); mrb_free(mrb, ptr); if (result != MRB_DUMP_OK) return result; for (i = 0; i < irep->rlen; i++) { result = read_lineno_record_file(mrb, fp, irep->reps[i]); if (result != MRB_DUMP_OK) break; } return result; }
static int read_rite_lineno_record(mrb_state *mrb, const uint8_t *bin, size_t irepno, uint32_t *len) { int ret; size_t i, fname_len, niseq; char *fname; uint16_t *lines; ret = MRB_DUMP_OK; *len = 0; bin += sizeof(uint32_t); // record size *len += sizeof(uint32_t); fname_len = bin_to_uint16(bin); bin += sizeof(uint16_t); *len += sizeof(uint16_t); if (SIZE_ERROR(fname_len + 1)) { ret = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } fname = (char *)mrb_malloc(mrb, fname_len + 1); if (fname == NULL) { ret = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } memcpy(fname, bin, fname_len); fname[fname_len] = '\0'; bin += fname_len; *len += fname_len; niseq = bin_to_uint32(bin); bin += sizeof(uint32_t); // niseq *len += sizeof(uint32_t); if (SIZE_ERROR_MUL(niseq, sizeof(uint16_t))) { ret = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } lines = (uint16_t *)mrb_malloc(mrb, niseq * sizeof(uint16_t)); if (lines == NULL) { ret = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } for (i = 0; i < niseq; i++) { lines[i] = bin_to_uint16(bin); bin += sizeof(uint16_t); // niseq *len += sizeof(uint16_t); } mrb->irep[irepno]->filename = fname; mrb->irep[irepno]->lines = lines; error_exit: return ret; }
static int read_lineno_record_1(mrb_state *mrb, const uint8_t *bin, mrb_irep *irep, uint32_t *len) { int ret; size_t i, fname_len, niseq; char *fname; uint16_t *lines; ret = MRB_DUMP_OK; *len = 0; bin += sizeof(uint32_t); /* record size */ *len += sizeof(uint32_t); fname_len = bin_to_uint16(bin); bin += sizeof(uint16_t); *len += sizeof(uint16_t); if (SIZE_ERROR(fname_len + 1)) { return MRB_DUMP_GENERAL_FAILURE; } fname = (char *)mrb_malloc(mrb, fname_len + 1); if (fname == NULL) { return MRB_DUMP_GENERAL_FAILURE; } memcpy(fname, bin, fname_len); fname[fname_len] = '\0'; bin += fname_len; *len += fname_len; niseq = bin_to_uint32(bin); bin += sizeof(uint32_t); /* niseq */ *len += sizeof(uint32_t); if (SIZE_ERROR_MUL(niseq, sizeof(uint16_t))) { return MRB_DUMP_GENERAL_FAILURE; } lines = (uint16_t *)mrb_malloc(mrb, niseq * sizeof(uint16_t)); if (lines == NULL) { return MRB_DUMP_GENERAL_FAILURE; } for (i = 0; i < niseq; i++) { lines[i] = bin_to_uint16(bin); bin += sizeof(uint16_t); /* niseq */ *len += sizeof(uint16_t); } irep->filename = fname; irep->lines = lines; return ret; }
static int32_t read_rite_section_irep_file(mrb_state *mrb, FILE *fp) { int32_t result; size_t sirep; uint16_t nirep; uint16_t n; uint32_t len, buf_size; uint8_t *buf = NULL; const size_t record_header_size = 1 + 4; struct rite_section_irep_header header; if (fread(&header, sizeof(struct rite_section_irep_header), 1, fp) == 0) { return MRB_DUMP_READ_FAULT; } sirep = mrb->irep_len; nirep = bin_to_uint16(header.nirep); buf_size = record_header_size; /* You don't need use SIZE_ERROR as buf_size is enough small. */ buf = (uint8_t *)mrb_malloc(mrb, buf_size); if (!buf) { result = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } //Read Binary Data Section for (n = 0; n < nirep; n++) { void *ptr; if (fread(buf, record_header_size, 1, fp) == 0) { result = MRB_DUMP_READ_FAULT; goto error_exit; } buf_size = bin_to_uint32(&buf[0]); if (SIZE_ERROR(buf_size)) { result = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } ptr = mrb_realloc(mrb, buf, buf_size); if (!ptr) { result = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } buf = (uint8_t *)ptr; if (fread(&buf[record_header_size], buf_size - record_header_size, 1, fp) == 0) { result = MRB_DUMP_READ_FAULT; goto error_exit; } result = read_rite_irep_record(mrb, buf, &len); if (result != MRB_DUMP_OK) goto error_exit; } result = nirep; error_exit: if (buf) { mrb_free(mrb, buf); } if (result < MRB_DUMP_OK) { irep_free(sirep, mrb); } return result; }