Exemplo n.º 1
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;
}
Exemplo n.º 2
0
Arquivo: load.c Projeto: Ancurio/mruby
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;
}