int32_t mrb_read_irep(mrb_state *mrb, const uint8_t *bin) { int result; int32_t total_nirep = 0; const struct rite_section_header *section_header; uint16_t crc; size_t bin_size = 0; size_t n; size_t sirep; if ((mrb == NULL) || (bin == NULL)) { return MRB_DUMP_INVALID_ARGUMENT; } result = read_rite_binary_header(bin, &bin_size, &crc); if (result != MRB_DUMP_OK) { return result; } n = offset_crc_body(); if (crc != calc_crc_16_ccitt(bin + n, bin_size - n, 0)) { return MRB_DUMP_INVALID_FILE_HEADER; } bin += sizeof(struct rite_binary_header); sirep = mrb->irep_len; do { section_header = (const struct rite_section_header *)bin; if (memcmp(section_header->section_identify, RITE_SECTION_IREP_IDENTIFIER, sizeof(section_header->section_identify)) == 0) { result = read_rite_section_irep(mrb, bin); if (result < MRB_DUMP_OK) { return result; } total_nirep += result; } else if (memcmp(section_header->section_identify, RITE_SECTION_LINENO_IDENTIFIER, sizeof(section_header->section_identify)) == 0) { result = read_rite_section_lineno(mrb, bin, sirep); if (result < MRB_DUMP_OK) { return result; } } else if (memcmp(section_header->section_identify, RITE_SECTION_DEBUG_IDENTIFIER, sizeof(section_header->section_identify)) == 0) { result = read_rite_section_debug(mrb, bin, sirep); if (result < MRB_DUMP_OK) { return result; } } bin += bin_to_uint32(section_header->section_size); } while (memcmp(section_header->section_identify, RITE_BINARY_EOF, sizeof(section_header->section_identify)) != 0); return sirep; }
int32_t mrb_read_irep_file(mrb_state *mrb, FILE* fp) { int result; int32_t total_nirep = 0; uint8_t *buf; uint16_t crc, crcwk = 0; uint32_t section_size = 0; size_t nbytes; size_t sirep; struct rite_section_header section_header; long fpos; const size_t block_size = 1 << 14; const size_t buf_size = sizeof(struct rite_binary_header); if ((mrb == NULL) || (fp == NULL)) { return MRB_DUMP_INVALID_ARGUMENT; } buf = mrb_malloc(mrb, buf_size); if (fread(buf, buf_size, 1, fp) == 0) { mrb_free(mrb, buf); return MRB_DUMP_READ_FAULT; } result = read_rite_binary_header(buf, NULL, &crc); mrb_free(mrb, buf); if (result != MRB_DUMP_OK) { return result; } /* verify CRC */ fpos = ftell(fp); buf = mrb_malloc(mrb, block_size); fseek(fp, offset_crc_body(), SEEK_SET); while ((nbytes = fread(buf, 1, block_size, fp)) > 0) { crcwk = calc_crc_16_ccitt(buf, nbytes, crcwk); } mrb_free(mrb, buf); if (nbytes == 0 && ferror(fp)) { return MRB_DUMP_READ_FAULT; } if (crcwk != crc) { return MRB_DUMP_INVALID_FILE_HEADER; } fseek(fp, fpos + section_size, SEEK_SET); sirep = mrb->irep_len; // read sections do { fpos = ftell(fp); if (fread(§ion_header, sizeof(struct rite_section_header), 1, fp) == 0) { return MRB_DUMP_READ_FAULT; } section_size = bin_to_uint32(section_header.section_size); if (memcmp(section_header.section_identify, RITE_SECTION_IREP_IDENTIFIER, sizeof(section_header.section_identify)) == 0) { fseek(fp, fpos, SEEK_SET); result = read_rite_section_irep_file(mrb, fp); if (result < MRB_DUMP_OK) { return result; } total_nirep += result; } else if (memcmp(section_header.section_identify, RITE_SECTION_LIENO_IDENTIFIER, sizeof(section_header.section_identify)) == 0) { fseek(fp, fpos, SEEK_SET); result = read_rite_section_lineno_file(mrb, fp, sirep); if (result < MRB_DUMP_OK) { return result; } } fseek(fp, fpos + section_size, SEEK_SET); } while (memcmp(section_header.section_identify, RITE_BINARY_EOF, sizeof(section_header.section_identify)) != 0); return total_nirep; }
int32_t mrb_read_irep_file(mrb_state *mrb, FILE* fp) { int result; int32_t total_nirep = 0; uint8_t *buf; uint16_t crc, crcwk = 0; uint32_t section_size = 0; size_t nbytes; size_t sirep; struct rite_section_header section_header; long fpos; size_t block_size = 1 << 14; const uint8_t block_fallback_count = 4; int i; const size_t buf_size = sizeof(struct rite_binary_header); if ((mrb == NULL) || (fp == NULL)) { return MRB_DUMP_INVALID_ARGUMENT; } /* You don't need use SIZE_ERROR as buf_size is enough small. */ buf = mrb_malloc(mrb, buf_size); if (!buf) { return MRB_DUMP_GENERAL_FAILURE; } if (fread(buf, buf_size, 1, fp) == 0) { mrb_free(mrb, buf); return MRB_DUMP_READ_FAULT; } result = read_rite_binary_header(buf, NULL, &crc); mrb_free(mrb, buf); if (result != MRB_DUMP_OK) { return result; } /* verify CRC */ fpos = ftell(fp); /* You don't need use SIZE_ERROR as block_size is enough small. */ for (i = 0; i < block_fallback_count; i++,block_size >>= 1){ buf = mrb_malloc_simple(mrb, block_size); if (buf) break; } if (!buf) { return MRB_DUMP_GENERAL_FAILURE; } fseek(fp, offset_crc_body(), SEEK_SET); while ((nbytes = fread(buf, 1, block_size, fp)) > 0) { crcwk = calc_crc_16_ccitt(buf, nbytes, crcwk); } mrb_free(mrb, buf); if (nbytes == 0 && ferror(fp)) { return MRB_DUMP_READ_FAULT; } if (crcwk != crc) { return MRB_DUMP_INVALID_FILE_HEADER; } fseek(fp, fpos + section_size, SEEK_SET); sirep = mrb->irep_len; // read sections do { fpos = ftell(fp); if (fread(§ion_header, sizeof(struct rite_section_header), 1, fp) == 0) { return MRB_DUMP_READ_FAULT; } section_size = bin_to_uint32(section_header.section_size); if (memcmp(section_header.section_identify, RITE_SECTION_IREP_IDENTIFIER, sizeof(section_header.section_identify)) == 0) { fseek(fp, fpos, SEEK_SET); result = read_rite_section_irep_file(mrb, fp); if (result < MRB_DUMP_OK) { return result; } total_nirep += result; } else if (memcmp(section_header.section_identify, RITE_SECTION_LINENO_IDENTIFIER, sizeof(section_header.section_identify)) == 0) { fseek(fp, fpos, SEEK_SET); result = read_rite_section_lineno_file(mrb, fp, sirep); if (result < MRB_DUMP_OK) { return result; } } else if (memcmp(section_header.section_identify, RITE_SECTION_DEBUG_IDENTIFIER, sizeof(section_header.section_identify)) == 0) { uint8_t* const bin = mrb_malloc(mrb, section_size); fseek(fp, fpos, SEEK_SET); if(fread((char*)bin, section_size, 1, fp) != 1) { mrb_free(mrb, bin); return MRB_DUMP_READ_FAULT; } result = read_rite_section_debug(mrb, bin, sirep); mrb_free(mrb, bin); if (result < MRB_DUMP_OK) { return result; } } fseek(fp, fpos + section_size, SEEK_SET); } while (memcmp(section_header.section_identify, RITE_BINARY_EOF, sizeof(section_header.section_identify)) != 0); return sirep; }