Exemplo n.º 1
0
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;
}
Exemplo n.º 2
0
int
mrb_read_irep(mrb_state *mrb, char *bin)
{
    int ret = MRB_DUMP_OK, i, n, nirep, sirep;
    uint32_t len;
    unsigned char *src;
    rite_binary_header  bin_header;

    if ((mrb == NULL) || (bin == NULL)) {
        return MRB_DUMP_INVALID_ARGUMENT;
    }
    src = (unsigned char*)bin;
    sirep = mrb->irep_len;

    //Read File Header Section
    if ((nirep = read_rite_header(mrb, src, &bin_header)) < 0)
        return nirep;

    mrb_add_irep(mrb, sirep + nirep);

    for (n=0,i=sirep; n<nirep; n++,i++) {
        if ((mrb->irep[i] = mrb_malloc(mrb, sizeof(mrb_irep))) == NULL) {
            ret = MRB_DUMP_GENERAL_FAILURE;
            goto error_exit;
        }
        memset(mrb->irep[i], 0, sizeof(mrb_irep));
    }
    src += sizeof(bin_header) + MRB_DUMP_SIZE_OF_SHORT;  //header + crc

    //Read Binary Data Section
    for (n=0,i=sirep; n<nirep; n++,i++) {
        src += MRB_DUMP_SIZE_OF_LONG;                      //record ren
        if ((ret = read_rite_irep_record(mrb, src, mrb->irep[i], &len)) != MRB_DUMP_OK)
            goto error_exit;
        mrb->irep[i]->idx = i;
        src += len;
    }
    if (0 != bin_to_uint32(src)) {              //dummy record len
        ret = MRB_DUMP_GENERAL_FAILURE;
    }

    mrb->irep_len += nirep;

error_exit:
    if (ret != MRB_DUMP_OK) {
        for (n=0,i=sirep; n<nirep; n++,i++) {
            if (mrb->irep[i]) {
                if (mrb->irep[i]->iseq)
                    mrb_free(mrb, mrb->irep[i]->iseq);

                if (mrb->irep[i]->pool)
                    mrb_free(mrb, mrb->irep[i]->pool);

                if (mrb->irep[i]->syms)
                    mrb_free(mrb, mrb->irep[i]->syms);

                mrb_free(mrb, mrb->irep[i]);
            }
        }
        return ret;
    }
    return sirep + hex_to_uint8(bin_header.sirep);
}