Пример #1
0
static int
write_rite_binary_header(mrb_state *mrb, size_t binary_size, uint8_t *bin, uint8_t flags)
{
  struct rite_binary_header *header = (struct rite_binary_header *)bin;
  uint16_t crc;
  uint32_t offset;

  switch (flags & DUMP_ENDIAN_NAT) {
  endian_big:
  case DUMP_ENDIAN_BIG:
    memcpy(header->binary_ident, RITE_BINARY_IDENT, sizeof(header->binary_ident));
    break;
  endian_little:
  case DUMP_ENDIAN_LIL:
    memcpy(header->binary_ident, RITE_BINARY_IDENT_LIL, sizeof(header->binary_ident));
    break;

  case DUMP_ENDIAN_NAT:
    if (bigendian_p()) goto endian_big;
    goto endian_little;
    break;
  }

  memcpy(header->binary_version, RITE_BINARY_FORMAT_VER, sizeof(header->binary_version));
  memcpy(header->compiler_name, RITE_COMPILER_NAME, sizeof(header->compiler_name));
  memcpy(header->compiler_version, RITE_COMPILER_VERSION, sizeof(header->compiler_version));
  mrb_assert(binary_size <= UINT32_MAX);
  uint32_to_bin((uint32_t)binary_size, header->binary_size);

  offset = (&(header->binary_crc[0]) - bin) + sizeof(uint16_t);
  crc = calc_crc_16_ccitt(bin + offset, binary_size - offset, 0);
  uint16_to_bin(crc, header->binary_crc);

  return MRB_DUMP_OK;
}
Пример #2
0
static int
dump_rite_header(mrb_state *mrb, int top, FILE* fp, uint32_t rbds)
{
  rite_binary_header binary_header;
  rite_file_header file_header;
  uint16_t crc;
  int type;

  if (fseek(fp, 0, SEEK_SET) != 0)
    return MRB_DUMP_GENERAL_FAILURE;

  /* calc crc */
  memcpy( &binary_header, def_rite_binary_header, sizeof(binary_header));

  type = DUMP_TYPE_BIN;
  uint32_dump(rbds, (char*)&binary_header.rbds, type);
  uint16_dump((uint16_t)mrb->irep_len, (char*)&binary_header.nirep, type);
  uint16_dump((uint16_t)top, (char*)&binary_header.sirep, type);

  crc = calc_crc_16_ccitt((unsigned char*)&binary_header, sizeof(binary_header));

  /* dump rbc header */
  memcpy( &file_header, def_rite_file_header, sizeof(file_header));

  type = DUMP_TYPE_HEX;
  uint32_dump(rbds, (char*)&file_header.rbds, type);
  uint16_dump((uint16_t)mrb->irep_len, (char*)&file_header.nirep, type);
  uint16_dump((uint16_t)top, (char*)&file_header.sirep, type);
  uint16_dump(crc, (char*)&file_header.hcrc, type);

  if (fwrite(&file_header, sizeof(file_header), 1, fp) != 1)
    return MRB_DUMP_WRITE_FAULT;

  return MRB_DUMP_OK;
}
Пример #3
0
static int
calc_crc_section(mrb_state *mrb, mrb_irep *irep, uint16_t *crc, int section)
{
  char *buf, *buf_top;
  uint32_t buf_size;
  int type = DUMP_TYPE_BIN;

  switch (section) {
  case DUMP_IREP_HEADER: buf_size = get_irep_header_size(mrb, irep, type); break;
  case DUMP_ISEQ_BLOCK:  buf_size = get_iseq_block_size(mrb, irep, type); break;
  case DUMP_POOL_BLOCK:  buf_size = get_pool_block_size(mrb, irep, type); break;
  case DUMP_SYMS_BLOCK:  buf_size = get_syms_block_size(mrb, irep, type); break;
  default: return MRB_DUMP_GENERAL_FAILURE;
  }

  if ((buf = mrb_malloc(mrb, buf_size)) == 0)
    return MRB_DUMP_GENERAL_FAILURE;

  buf_top = buf;
  memset(buf, 0, buf_size);

  switch (section) {
  case DUMP_IREP_HEADER: buf += write_irep_header(mrb, irep, buf, type); break;
  case DUMP_ISEQ_BLOCK: buf += write_iseq_block(mrb, irep, buf, type); break;
  case DUMP_POOL_BLOCK: buf += write_pool_block(mrb, irep, buf, type); break;
  case DUMP_SYMS_BLOCK: buf += write_syms_block(mrb, irep, buf, type); break;
  default: break;
  }

  *crc = calc_crc_16_ccitt((unsigned char*)buf_top, (int)(buf - buf_top));

  mrb_free(mrb, buf_top);

  return MRB_DUMP_OK;
}
Пример #4
0
static mrb_irep*
read_irep(mrb_state *mrb, const uint8_t *bin, uint8_t flags)
{
  int result;
  mrb_irep *irep = NULL;
  const struct rite_section_header *section_header;
  uint16_t crc;
  size_t bin_size = 0;
  size_t n;

  if ((mrb == NULL) || (bin == NULL)) {
    return NULL;
  }

  result = read_binary_header(bin, &bin_size, &crc, &flags);
  if (result != MRB_DUMP_OK) {
    return NULL;
  }

  n = offset_crc_body();
  if (crc != calc_crc_16_ccitt(bin + n, bin_size - n, 0)) {
    return NULL;
  }

  bin += sizeof(struct rite_binary_header);
  do {
    section_header = (const struct rite_section_header *)bin;
    if (memcmp(section_header->section_ident, RITE_SECTION_IREP_IDENT, sizeof(section_header->section_ident)) == 0) {
      irep = read_section_irep(mrb, bin, flags);
      if (!irep) return NULL;
    }
    else if (memcmp(section_header->section_ident, RITE_SECTION_LINENO_IDENT, sizeof(section_header->section_ident)) == 0) {
      if (!irep) return NULL;   /* corrupted data */
      result = read_section_lineno(mrb, bin, irep);
      if (result < MRB_DUMP_OK) {
        return NULL;
      }
    }
    else if (memcmp(section_header->section_ident, RITE_SECTION_DEBUG_IDENT, sizeof(section_header->section_ident)) == 0) {
      if (!irep) return NULL;   /* corrupted data */
      result = read_section_debug(mrb, bin, irep, flags);
      if (result < MRB_DUMP_OK) {
        return NULL;
      }
    }
    else if (memcmp(section_header->section_ident, RITE_SECTION_LV_IDENT, sizeof(section_header->section_ident)) == 0) {
      if (!irep) return NULL;
      result = read_section_lv(mrb, bin, irep, flags);
      if (result < MRB_DUMP_OK) {
        return NULL;
      }
    }
    bin += bin_to_uint32(section_header->section_size);
  } while (memcmp(section_header->section_ident, RITE_BINARY_EOF, sizeof(section_header->section_ident)) != 0);

  return irep;
}
Пример #5
0
static uint16_t
calc_rite_header_crc(mrb_state *mrb, int top, rite_binary_header *binary_header, uint32_t rbds, int type)
{
  memcpy( binary_header, def_rite_binary_header, sizeof(*binary_header));

  uint32_dump(rbds, (char*)binary_header->rbds, type);
  uint16_dump((uint16_t)mrb->irep_len, (char*)binary_header->nirep, type);
  uint16_dump((uint16_t)top, (char*)binary_header->sirep, type);

  return calc_crc_16_ccitt((unsigned char*)binary_header, sizeof(*binary_header));
}
Пример #6
0
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;
}
Пример #7
0
static int
calc_crc_section(mrb_state *mrb, mrb_irep *irep, uint16_t *crc, int section)
{
  char *buf, *buf_top;
  uint32_t buf_size;
  int type = DUMP_TYPE_BIN;
  int result;

  switch (section) {
  case DUMP_IREP_HEADER: buf_size = get_irep_header_size(mrb, type); break;
  case DUMP_ISEQ_BLOCK:  buf_size = get_iseq_block_size(mrb, irep, type); break;
  case DUMP_POOL_BLOCK:  buf_size = get_pool_block_size(mrb, irep, type); break;
  case DUMP_SYMS_BLOCK:  buf_size = get_syms_block_size(mrb, irep, type); break;
  default: return MRB_DUMP_GENERAL_FAILURE;
  }

  buf = (char *)mrb_calloc(mrb, 1, buf_size);
  if (buf == NULL)
    return MRB_DUMP_GENERAL_FAILURE;

  buf_top = buf;

  switch (section) {
  case DUMP_IREP_HEADER:
    result = write_irep_header(mrb, irep, buf, type);
    break;
  case DUMP_ISEQ_BLOCK:
    result = write_iseq_block(mrb, irep, buf, type);
    break;
  case DUMP_POOL_BLOCK:
    result = write_pool_block(mrb, irep, buf, type);
    break;
  case DUMP_SYMS_BLOCK:
    result = write_syms_block(mrb, irep, buf, type);
    break;
  default:
    result = MRB_DUMP_GENERAL_FAILURE;
    break; /* Already checked above. */
  }
  if (result < 0) {
    goto error_exit;
  }
  buf += result;

  *crc = calc_crc_16_ccitt((unsigned char*)buf_top, (int)(buf - buf_top));

  mrb_free(mrb, buf_top);

  result = MRB_DUMP_OK;
 error_exit:
  return result;
}
Пример #8
0
static int
write_rite_binary_header(mrb_state *mrb, size_t binary_size, uint8_t *bin)
{
  struct rite_binary_header *header = (struct rite_binary_header *)bin;
  uint16_t crc;
  size_t offset;

  memcpy(header->binary_identify, RITE_BINARY_IDENTIFIER, sizeof(header->binary_identify));
  memcpy(header->binary_version, RITE_BINARY_FORMAT_VER, sizeof(header->binary_version));
  memcpy(header->compiler_name, RITE_COMPILER_NAME, sizeof(header->compiler_name));
  memcpy(header->compiler_version, RITE_COMPILER_VERSION, sizeof(header->compiler_version));
  uint32_to_bin(binary_size, header->binary_size);

  offset = (&(header->binary_crc[0]) - bin) + sizeof(uint16_t);
  crc = calc_crc_16_ccitt(bin + offset, binary_size - offset, 0);
  uint16_to_bin(crc, header->binary_crc);

  return MRB_DUMP_OK;
}
Пример #9
0
static int
read_rite_header(mrb_state *mrb, unsigned char *bin, rite_binary_header*  bin_header)
{
  uint16_t crc;

  *bin_header = *(rite_binary_header *)bin;
  bin += sizeof(rite_binary_header);
  if (memcmp(bin_header->rbfi, RITE_FILE_IDENFIFIER, sizeof(bin_header->rbfi)) != 0) {
    return MRB_DUMP_INVALID_FILE_HEADER;    //File identifier error
  }
  if (memcmp(bin_header->risv, RITE_VM_VER, sizeof(bin_header->risv)) != 0) {
    return MRB_DUMP_INVALID_FILE_HEADER;    //Instruction set version check
  }

  crc = calc_crc_16_ccitt((unsigned char*)bin_header, sizeof(*bin_header));   //Calculate CRC
  if (crc != bin_to_uint16(bin)) {
    return MRB_DUMP_INVALID_FILE_HEADER;    //CRC error
  }

  return bin_to_uint16(bin_header->nirep);
}
Пример #10
0
static int
write_rite_header(mrb_state *mrb, int top, char* bin, uint32_t rbds)
{
  rite_binary_header *binary_header;
  uint16_t crc;
  int type = DUMP_TYPE_BIN;

  binary_header = (rite_binary_header*)bin;

  memcpy( binary_header, def_rite_binary_header, sizeof(*binary_header));

  uint32_dump(rbds, (char*)binary_header->rbds, type);
  uint16_dump((uint16_t)mrb->irep_len, (char*)binary_header->nirep, type);
  uint16_dump((uint16_t)top, (char*)binary_header->sirep, type);

  crc = calc_crc_16_ccitt((unsigned char*)binary_header, sizeof(*binary_header));
  bin += sizeof(*binary_header);
  uint16_dump(crc, bin, type);

  return MRB_DUMP_OK;
}
Пример #11
0
Файл: load.c Проект: kano4/mruby
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(&section_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;
}
Пример #12
0
mrb_irep*
mrb_read_irep_file(mrb_state *mrb, FILE* fp)
{
  mrb_irep *irep = NULL;
  int result;
  uint8_t *buf;
  uint16_t crc, crcwk = 0;
  size_t section_size = 0;
  size_t nbytes;
  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 NULL;
  }

  /* You don't need use SIZE_ERROR as buf_size is enough small. */
  buf = (uint8_t*)mrb_malloc(mrb, buf_size);
  if (fread(buf, buf_size, 1, fp) == 0) {
    mrb_free(mrb, buf);
    return NULL;
  }
  result = read_binary_header(buf, NULL, &crc);
  mrb_free(mrb, buf);
  if (result != MRB_DUMP_OK) {
    return NULL;
  }

  /* 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 = (uint8_t*)mrb_malloc_simple(mrb, block_size);
    if (buf) break;
  }
  if (!buf) {
    return NULL;
  }
  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 NULL;
  }
  if (crcwk != crc) {
    return NULL;
  }
  fseek(fp, fpos + section_size, SEEK_SET);

  /* read sections */
  do {
    fpos = ftell(fp);
    if (fread(&section_header, sizeof(struct rite_section_header), 1, fp) == 0) {
      return NULL;
    }
    section_size = (size_t)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);
      irep = read_section_irep_file(mrb, fp);
      if (!irep) return NULL;
    }
    else if (memcmp(section_header.section_identify, RITE_SECTION_LINENO_IDENTIFIER, sizeof(section_header.section_identify)) == 0) {
      if (!irep) return NULL;   /* corrupted data */
      fseek(fp, fpos, SEEK_SET);
      result = read_section_lineno_file(mrb, fp, irep);
      if (result < MRB_DUMP_OK) return NULL;
    }
    else if (memcmp(section_header.section_identify, RITE_SECTION_DEBUG_IDENTIFIER, sizeof(section_header.section_identify)) == 0) {
      if (!irep) return NULL;   /* corrupted data */
      else {
        uint8_t* const bin = (uint8_t*)mrb_malloc(mrb, section_size);

        fseek(fp, fpos, SEEK_SET);
        if (fread((char*)bin, section_size, 1, fp) != 1) {
          mrb_free(mrb, bin);
          return NULL;
        }
        result = read_section_debug(mrb, bin, irep, TRUE);
        mrb_free(mrb, bin);
      }
      if (result < MRB_DUMP_OK) return NULL;
    }
    else if (memcmp(section_header.section_identify, RITE_SECTION_LV_IDENTIFIER, sizeof(section_header.section_identify)) == 0) {
      if (!irep) return NULL;
      else {
        uint8_t* const bin = (uint8_t*)mrb_malloc(mrb, section_size);

        fseek(fp, fpos, SEEK_SET);
        if (fread((char*)bin, section_size, 1, fp) != 1) {
          mrb_free(mrb, bin);
          return NULL;
        }
        result = read_section_lv(mrb, bin, irep, TRUE);
        mrb_free(mrb, bin);
      }
      if (result < MRB_DUMP_OK) return NULL;
    }

    fseek(fp, fpos + section_size, SEEK_SET);
  } while (memcmp(section_header.section_identify, RITE_BINARY_EOF, sizeof(section_header.section_identify)) != 0);

  return irep;
}
Пример #13
0
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(&section_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;
}
Пример #14
0
static int
read_rite_irep_record(mrb_state *mrb, unsigned char *src, uint32_t* len)
{
  int i, ret = MRB_DUMP_OK;
  char *buf;
  unsigned char *recordStart, *pStart;
  uint16_t crc, tt, pdl, snl, offset, bufsize=MRB_DUMP_DEFAULT_STR_LEN;
  mrb_int fix_num;
  mrb_float f;
  int plen;
  int ai = mrb_gc_arena_save(mrb);
  mrb_irep *irep = mrb_add_irep(mrb);

  recordStart = src;
  buf = (char *)mrb_malloc(mrb, bufsize);
  if (buf == NULL) {
    ret = MRB_DUMP_INVALID_IREP;
    goto error_exit;
  }

  //Header Section
  pStart = src;
  if (*src != RITE_IREP_IDENFIFIER)
    return MRB_DUMP_INVALID_IREP;
  src += (sizeof(unsigned char) * 2);
  irep->nlocals = bin_to_uint16(src);       //number of local variable
  src += MRB_DUMP_SIZE_OF_SHORT;
  irep->nregs = bin_to_uint16(src);         //number of register variable
  src += MRB_DUMP_SIZE_OF_SHORT;
  offset = bin_to_uint16(src);              //offset of isec block
  src += MRB_DUMP_SIZE_OF_SHORT;
  crc = calc_crc_16_ccitt(pStart, src - pStart);     //Calculate CRC
  if (crc != bin_to_uint16(src))             //header CRC
    return MRB_DUMP_INVALID_IREP;
  src += offset;

  //Binary Data Section
  //ISEQ BLOCK
  pStart = src;
  irep->ilen = bin_to_uint32(src);          //iseq length
  src += MRB_DUMP_SIZE_OF_LONG;
  if (irep->ilen > 0) {
    if ((irep->iseq = (mrb_code *)mrb_malloc(mrb, sizeof(mrb_code) * irep->ilen)) == NULL) {
      ret = MRB_DUMP_GENERAL_FAILURE;
      goto error_exit;
    }
    for (i=0; i<irep->ilen; i++) {
      irep->iseq[i] = bin_to_uint32(src);     //iseq
      src += MRB_DUMP_SIZE_OF_LONG;
    }
  }
  crc = calc_crc_16_ccitt((unsigned char*)pStart, src - pStart);     //Calculate CRC
  if (crc != bin_to_uint16(src)) {          //iseq CRC
    ret = MRB_DUMP_INVALID_IREP;
    goto error_exit;
  }
  src += MRB_DUMP_SIZE_OF_SHORT;

  //POOL BLOCK
  pStart = src;
  plen = bin_to_uint32(src);          //pool length
  src += MRB_DUMP_SIZE_OF_LONG;
  if (plen > 0) {
    irep->pool = (mrb_value *)mrb_malloc(mrb, sizeof(mrb_value) * plen);
    if (irep->pool == NULL) {
      ret = MRB_DUMP_INVALID_IREP;
      goto error_exit;
    }

    for (i=0; i<plen; i++) {
      tt = *src;                              //pool TT
      src += sizeof(unsigned char);
      pdl = bin_to_uint16(src);               //pool data length
      src += MRB_DUMP_SIZE_OF_SHORT;
      if (pdl > bufsize - 1) {
        mrb_free(mrb, buf);
        bufsize = pdl + 1;
        if ((buf = (char *)mrb_malloc(mrb, bufsize)) == NULL) {
          ret = MRB_DUMP_GENERAL_FAILURE;
          goto error_exit;
        }
      }
      memcpy(buf, src, pdl);
      src += pdl;
      buf[pdl] = '\0';

      switch (tt) {                           //pool data
      case MRB_TT_FIXNUM:
        fix_num = str_to_mrb_int(buf);
        irep->pool[i] = mrb_fixnum_value(fix_num);
        break;

      case MRB_TT_FLOAT:
        f = str_to_mrb_float(buf);
        irep->pool[i] = mrb_float_value(f);
        break;

      case MRB_TT_STRING:
        irep->pool[i] = mrb_str_new(mrb, buf, pdl);
        break;

#ifdef ENABLE_REGEXP
      case MRB_TT_REGEX:
        str = mrb_str_new(mrb, buf, pdl);
        irep->pool[i] = mrb_reg_quote(mrb, str);
        break;
#endif

      default:
        irep->pool[i] = mrb_nil_value();
        break;
      }
      irep->plen++;
      mrb_gc_arena_restore(mrb, ai);
    }
  }
  crc = calc_crc_16_ccitt((unsigned char*)pStart, src - pStart);     //Calculate CRC
  if (crc != bin_to_uint16(src)) {          //pool CRC
    ret = MRB_DUMP_INVALID_IREP;
    goto error_exit;
  }
  src += MRB_DUMP_SIZE_OF_SHORT;

  //SYMS BLOCK
  pStart = src;
  irep->slen = bin_to_uint32(src);          //syms length
  src += MRB_DUMP_SIZE_OF_LONG;
  if (irep->slen > 0) {
    if ((irep->syms = (mrb_sym *)mrb_malloc(mrb, sizeof(mrb_sym) * irep->slen)) == NULL) {
      ret = MRB_DUMP_INVALID_IREP;
      goto error_exit;
    }

    for (i = 0; i < irep->slen; i++) {
      static const mrb_sym mrb_sym_zero = { 0 };
      *irep->syms = mrb_sym_zero;
    }
    for (i=0; i<irep->slen; i++) {
      snl = bin_to_uint16(src);               //symbol name length
      src += MRB_DUMP_SIZE_OF_SHORT;

      if (snl == MRB_DUMP_NULL_SYM_LEN) {
        irep->syms[i] = 0;
        continue;
      }

      if (snl > bufsize - 1) {
        mrb_free(mrb, buf);
        bufsize = snl + 1;
        if ((buf = (char *)mrb_malloc(mrb, bufsize)) == NULL) {
          ret = MRB_DUMP_GENERAL_FAILURE;
          goto error_exit;
        }
      }
      memcpy(buf, src, snl);                  //symbol name
      src += snl;
      buf[snl] = '\0';
      irep->syms[i] = mrb_intern2(mrb, buf, snl);
    }
  }
  crc = calc_crc_16_ccitt((unsigned char*)pStart, src - pStart);     //Calculate CRC
  if (crc != bin_to_uint16(src)) {           //syms CRC
    ret = MRB_DUMP_INVALID_IREP;
    goto error_exit;
  }
  src += MRB_DUMP_SIZE_OF_SHORT;

  *len = src - recordStart;
error_exit:
  if (buf)
    mrb_free(mrb, buf);

  return ret;
}