static int read_rite_section_irep(mrb_state *mrb, const uint8_t *bin) { int result; size_t sirep; size_t i; uint32_t len; uint16_t nirep; uint16_t n; const struct rite_section_irep_header *header; header = (const struct rite_section_irep_header*)bin; bin += sizeof(struct rite_section_irep_header); sirep = mrb->irep_len; nirep = bin_to_uint16(header->nirep); //Read Binary Data Section for (n = 0, i = sirep; n < nirep; n++, i++) { result = read_rite_irep_record(mrb, bin, &len); if (result != MRB_DUMP_OK) goto error_exit; bin += len; } result = sirep + bin_to_uint16(header->sirep); error_exit: if (result < MRB_DUMP_OK) { irep_free(sirep, mrb); } return result; }
static int32_t read_rite_section_irep_file(mrb_state *mrb, FILE *fp) { int32_t result; size_t sirep; size_t i; 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; buf = (uint8_t *)mrb_malloc(mrb, buf_size); //Read Binary Data Section for (n = 0, i = sirep; n < nirep; n++, i++) { if (fread(buf, record_header_size, 1, fp) == 0) { result = MRB_DUMP_READ_FAULT; goto error_exit; } buf_size = bin_to_uint32(&buf[0]); buf = (uint8_t *)mrb_realloc(mrb, buf, buf_size); 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 = sirep + bin_to_uint16(header.sirep); error_exit: mrb_free(mrb, buf); if (result < MRB_DUMP_OK) { irep_free(sirep, mrb); } return result; }
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; }