static mrb_irep* read_irep_record_1(mrb_state *mrb, const uint8_t *bin, size_t *len, uint8_t flags) { int i; const uint8_t *src = bin; ptrdiff_t diff; uint16_t tt, pool_data_len, snl; int plen; int ai = mrb_gc_arena_save(mrb); mrb_irep *irep = mrb_add_irep(mrb); /* skip record size */ src += sizeof(uint32_t); /* number of local variable */ irep->nlocals = bin_to_uint16(src); src += sizeof(uint16_t); /* number of register variable */ irep->nregs = bin_to_uint16(src); src += sizeof(uint16_t); /* number of child irep */ irep->rlen = (size_t)bin_to_uint16(src); src += sizeof(uint16_t); /* Binary Data Section */ /* ISEQ BLOCK */ irep->ilen = (uint16_t)bin_to_uint32(src); src += sizeof(uint32_t); src += skip_padding(src); if (irep->ilen > 0) { if (SIZE_ERROR_MUL(irep->ilen, sizeof(mrb_code))) { return NULL; } if ((flags & FLAG_SRC_MALLOC) == 0 && (flags & FLAG_BYTEORDER_NATIVE)) { irep->iseq = (mrb_code*)src; src += sizeof(mrb_code) * irep->ilen; irep->flags |= MRB_ISEQ_NO_FREE; } else { size_t data_len = sizeof(mrb_code) * irep->ilen; irep->iseq = (mrb_code *)mrb_malloc(mrb, data_len); memcpy(irep->iseq, src, data_len); src += data_len; } } /* POOL BLOCK */ plen = bin_to_uint32(src); /* number of pool */ src += sizeof(uint32_t); if (plen > 0) { if (SIZE_ERROR_MUL(plen, sizeof(mrb_value))) { return NULL; } irep->pool = (mrb_value*)mrb_malloc(mrb, sizeof(mrb_value) * plen); for (i = 0; i < plen; i++) { mrb_value s; tt = *src++; /* pool TT */ pool_data_len = bin_to_uint16(src); /* pool data length */ src += sizeof(uint16_t); if (flags & FLAG_SRC_MALLOC) { s = mrb_str_new(mrb, (char *)src, pool_data_len); } else { s = mrb_str_new_static(mrb, (char *)src, pool_data_len); } src += pool_data_len; switch (tt) { /* pool data */ case IREP_TT_FIXNUM: { mrb_value num = mrb_str_to_inum(mrb, s, 10, FALSE); #ifdef MRB_WITHOUT_FLOAT irep->pool[i] = num; #else irep->pool[i] = mrb_float_p(num)? mrb_float_pool(mrb, mrb_float(num)) : num; #endif } break; #ifndef MRB_WITHOUT_FLOAT case IREP_TT_FLOAT: irep->pool[i] = mrb_float_pool(mrb, mrb_str_to_dbl(mrb, s, FALSE)); break; #endif case IREP_TT_STRING: irep->pool[i] = mrb_str_pool(mrb, s); break; default: /* should not happen */ irep->pool[i] = mrb_nil_value(); break; } irep->plen++; mrb_gc_arena_restore(mrb, ai); } } /* SYMS BLOCK */ irep->slen = (uint16_t)bin_to_uint32(src); /* syms length */ src += sizeof(uint32_t); if (irep->slen > 0) { if (SIZE_ERROR_MUL(irep->slen, sizeof(mrb_sym))) { return NULL; } irep->syms = (mrb_sym *)mrb_malloc(mrb, sizeof(mrb_sym) * irep->slen); for (i = 0; i < irep->slen; i++) { snl = bin_to_uint16(src); /* symbol name length */ src += sizeof(uint16_t); if (snl == MRB_DUMP_NULL_SYM_LEN) { irep->syms[i] = 0; continue; } if (flags & FLAG_SRC_MALLOC) { irep->syms[i] = mrb_intern(mrb, (char *)src, snl); } else { irep->syms[i] = mrb_intern_static(mrb, (char *)src, snl); } src += snl + 1; mrb_gc_arena_restore(mrb, ai); } } irep->reps = (mrb_irep**)mrb_malloc(mrb, sizeof(mrb_irep*)*irep->rlen); diff = src - bin; mrb_assert_int_fit(ptrdiff_t, diff, size_t, SIZE_MAX); *len = (size_t)diff; return irep; }
static int read_rite_irep_record(mrb_state *mrb, const uint8_t *bin, uint32_t *len) { int ret; size_t i; const uint8_t *src = bin; uint16_t tt, pool_data_len, snl; size_t plen; int ai = mrb_gc_arena_save(mrb); mrb_irep *irep = mrb_add_irep(mrb); // skip record size src += sizeof(uint32_t); // number of local variable irep->nlocals = bin_to_uint16(src); src += sizeof(uint16_t); // number of register variable irep->nregs = bin_to_uint16(src); src += sizeof(uint16_t); // Binary Data Section // ISEQ BLOCK irep->ilen = bin_to_uint32(src); src += sizeof(uint32_t); if (irep->ilen > 0) { irep->iseq = (mrb_code *)mrb_malloc(mrb, sizeof(mrb_code) * irep->ilen); if (irep->iseq == 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 += sizeof(uint32_t); } } //POOL BLOCK plen = bin_to_uint32(src); /* number of pool */ src += sizeof(uint32_t); if (plen > 0) { irep->pool = (mrb_value *)mrb_malloc(mrb, sizeof(mrb_value) * plen); if (irep->pool == NULL) { ret = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } for (i = 0; i < plen; i++) { mrb_value s; tt = *src++; //pool TT pool_data_len = bin_to_uint16(src); //pool data length src += sizeof(uint16_t); s = mrb_str_new(mrb, (char *)src, pool_data_len); src += pool_data_len; switch (tt) { //pool data case MRB_TT_FIXNUM: irep->pool[i] = mrb_str_to_inum(mrb, s, 10, FALSE); break; case MRB_TT_FLOAT: irep->pool[i] = mrb_float_value(mrb_str_to_dbl(mrb, s, FALSE)); break; case MRB_TT_STRING: irep->pool[i] = s; break; default: irep->pool[i] = mrb_nil_value(); break; } irep->plen++; mrb_gc_arena_restore(mrb, ai); } } //SYMS BLOCK irep->slen = bin_to_uint32(src); //syms length src += sizeof(uint32_t); if (irep->slen > 0) { irep->syms = (mrb_sym *)mrb_malloc(mrb, sizeof(mrb_sym) * irep->slen); if (irep->syms == NULL) { ret = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } for (i = 0; i < irep->slen; i++) { snl = bin_to_uint16(src); //symbol name length src += sizeof(uint16_t); if (snl == MRB_DUMP_NULL_SYM_LEN) { irep->syms[i] = 0; continue; } irep->syms[i] = mrb_intern2(mrb, (char *)src, snl); src += snl + 1; mrb_gc_arena_restore(mrb, ai); } } *len = src - bin; ret = MRB_DUMP_OK; error_exit: return ret; }
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; }
static mrb_irep* read_irep_record_1(mrb_state *mrb, const uint8_t *bin, size_t *len, mrb_bool alloc) { size_t i; const uint8_t *src = bin; ptrdiff_t diff; uint16_t tt, pool_data_len, snl; size_t plen; int ai = mrb_gc_arena_save(mrb); mrb_irep *irep = mrb_add_irep(mrb); /* skip record size */ src += sizeof(uint32_t); /* number of local variable */ irep->nlocals = bin_to_uint16(src); src += sizeof(uint16_t); /* number of register variable */ irep->nregs = bin_to_uint16(src); src += sizeof(uint16_t); /* number of child irep */ irep->rlen = (size_t)bin_to_uint16(src); src += sizeof(uint16_t); /* Binary Data Section */ /* ISEQ BLOCK */ irep->ilen = (size_t)bin_to_uint32(src); src += sizeof(uint32_t); if (irep->ilen > 0) { if (SIZE_ERROR_MUL(sizeof(mrb_code), irep->ilen)) { return NULL; } irep->iseq = (mrb_code *)mrb_malloc(mrb, sizeof(mrb_code) * irep->ilen); for (i = 0; i < irep->ilen; i++) { irep->iseq[i] = (size_t)bin_to_uint32(src); /* iseq */ src += sizeof(uint32_t); } } /* POOL BLOCK */ plen = (size_t)bin_to_uint32(src); /* number of pool */ src += sizeof(uint32_t); if (plen > 0) { if (SIZE_ERROR_MUL(sizeof(mrb_value), plen)) { return NULL; } irep->pool = (mrb_value*)mrb_malloc(mrb, sizeof(mrb_value) * plen); for (i = 0; i < plen; i++) { mrb_value s; tt = *src++; /* pool TT */ pool_data_len = bin_to_uint16(src); /* pool data length */ src += sizeof(uint16_t); if (alloc) { s = mrb_str_new(mrb, (char *)src, pool_data_len); } else { s = mrb_str_new_static(mrb, (char *)src, pool_data_len); } src += pool_data_len; switch (tt) { /* pool data */ case IREP_TT_FIXNUM: irep->pool[i] = mrb_str_to_inum(mrb, s, 10, FALSE); break; case IREP_TT_FLOAT: irep->pool[i] = mrb_float_pool(mrb, mrb_str_to_dbl(mrb, s, FALSE)); break; case IREP_TT_STRING: irep->pool[i] = mrb_str_pool(mrb, s); break; default: /* should not happen */ irep->pool[i] = mrb_nil_value(); break; } irep->plen++; mrb_gc_arena_restore(mrb, ai); } } /* SYMS BLOCK */ irep->slen = (size_t)bin_to_uint32(src); /* syms length */ src += sizeof(uint32_t); if (irep->slen > 0) { if (SIZE_ERROR_MUL(sizeof(mrb_sym), irep->slen)) { return NULL; } irep->syms = (mrb_sym *)mrb_malloc(mrb, sizeof(mrb_sym) * irep->slen); for (i = 0; i < irep->slen; i++) { snl = bin_to_uint16(src); /* symbol name length */ src += sizeof(uint16_t); if (snl == MRB_DUMP_NULL_SYM_LEN) { irep->syms[i] = 0; continue; } if (alloc) { irep->syms[i] = mrb_intern(mrb, (char *)src, snl); } else { irep->syms[i] = mrb_intern_static(mrb, (char *)src, snl); } src += snl + 1; mrb_gc_arena_restore(mrb, ai); } } irep->reps = (mrb_irep**)mrb_malloc(mrb, sizeof(mrb_irep*)*irep->rlen); diff = src - bin; mrb_assert_int_fit(ptrdiff_t, diff, size_t, SIZE_MAX); *len = (size_t)diff; return irep; }
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); }