Unverified Commit 85a2dfc8 authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto Committed by GitHub

Merge pull request #4403 from dearblue/read-irep-from-buf

Read irep from buffers
parents 38f8edbd 8f6f36f6
......@@ -31,6 +31,7 @@ MRB_API mrb_value mrb_load_irep_file(mrb_state*,FILE*);
MRB_API mrb_value mrb_load_irep_file_cxt(mrb_state*, FILE*, mrbc_context*);
#endif
MRB_API mrb_irep *mrb_read_irep(mrb_state*, const uint8_t*);
MRB_API mrb_irep *mrb_read_irep_buf(mrb_state*, const void*, size_t);
/* dump/load error code
*
......
......@@ -52,9 +52,21 @@ MRB_API mrb_irep *mrb_add_irep(mrb_state *mrb);
/* @param [const uint8_t*] irep code, expected as a literal */
MRB_API mrb_value mrb_load_irep(mrb_state*, const uint8_t*);
/*
* @param [const void*] irep code
* @param [size_t] size of irep buffer. If -1 is given, it is considered unrestricted.
*/
MRB_API mrb_value mrb_load_irep_buf(mrb_state*, const void*, size_t);
/* @param [const uint8_t*] irep code, expected as a literal */
MRB_API mrb_value mrb_load_irep_cxt(mrb_state*, const uint8_t*, mrbc_context*);
/*
* @param [const void*] irep code
* @param [size_t] size of irep buffer. If -1 is given, it is considered unrestricted.
*/
MRB_API mrb_value mrb_load_irep_buf_cxt(mrb_state*, const void*, size_t, mrbc_context*);
void mrb_irep_free(mrb_state*, struct mrb_irep*);
void mrb_irep_incref(mrb_state*, struct mrb_irep*);
void mrb_irep_decref(mrb_state*, struct mrb_irep*);
......
......@@ -459,10 +459,14 @@ lv_exit:
}
static int
read_binary_header(const uint8_t *bin, size_t *bin_size, uint16_t *crc, uint8_t *flags)
read_binary_header(const uint8_t *bin, size_t bufsize, size_t *bin_size, uint16_t *crc, uint8_t *flags)
{
const struct rite_binary_header *header = (const struct rite_binary_header *)bin;
if (bufsize < sizeof(struct rite_binary_header)) {
return MRB_DUMP_READ_FAULT;
}
if (memcmp(header->binary_ident, RITE_BINARY_IDENT, sizeof(header->binary_ident)) == 0) {
if (bigendian_p())
*flags |= FLAG_BYTEORDER_NATIVE;
......@@ -479,16 +483,24 @@ read_binary_header(const uint8_t *bin, size_t *bin_size, uint16_t *crc, uint8_t
return MRB_DUMP_INVALID_FILE_HEADER;
}
if (memcmp(header->binary_version, RITE_BINARY_FORMAT_VER, sizeof(header->binary_version)) != 0) {
return MRB_DUMP_INVALID_FILE_HEADER;
}
if (crc) {
*crc = bin_to_uint16(header->binary_crc);
}
*bin_size = (size_t)bin_to_uint32(header->binary_size);
if (bufsize < *bin_size) {
return MRB_DUMP_READ_FAULT;
}
return MRB_DUMP_OK;
}
static mrb_irep*
read_irep(mrb_state *mrb, const uint8_t *bin, uint8_t flags)
read_irep(mrb_state *mrb, const uint8_t *bin, size_t bufsize, uint8_t flags)
{
int result;
mrb_irep *irep = NULL;
......@@ -501,7 +513,7 @@ read_irep(mrb_state *mrb, const uint8_t *bin, uint8_t flags)
return NULL;
}
result = read_binary_header(bin, &bin_size, &crc, &flags);
result = read_binary_header(bin, bufsize, &bin_size, &crc, &flags);
if (result != MRB_DUMP_OK) {
return NULL;
}
......@@ -547,7 +559,13 @@ mrb_read_irep(mrb_state *mrb, const uint8_t *bin)
uint8_t flags = FLAG_SRC_STATIC;
#endif
return read_irep(mrb, bin, flags);
return read_irep(mrb, bin, (size_t)-1, flags);
}
MRB_API mrb_irep*
mrb_read_irep_buf(mrb_state *mrb, const void *buf, size_t bufsize)
{
return read_irep(mrb, (const uint8_t *)buf, bufsize, FLAG_SRC_MALLOC);
}
void mrb_exc_set(mrb_state *mrb, mrb_value exc);
......@@ -583,12 +601,24 @@ mrb_load_irep_cxt(mrb_state *mrb, const uint8_t *bin, mrbc_context *c)
return load_irep(mrb, mrb_read_irep(mrb, bin), c);
}
MRB_API mrb_value
mrb_load_irep_buf_cxt(mrb_state *mrb, const void *buf, size_t bufsize, mrbc_context *c)
{
return load_irep(mrb, mrb_read_irep_buf(mrb, buf, bufsize), c);
}
MRB_API mrb_value
mrb_load_irep(mrb_state *mrb, const uint8_t *bin)
{
return mrb_load_irep_cxt(mrb, bin, NULL);
}
MRB_API mrb_value
mrb_load_irep_buf(mrb_state *mrb, const void *buf, size_t bufsize)
{
return mrb_load_irep_buf_cxt(mrb, buf, bufsize, NULL);
}
#ifndef MRB_DISABLE_STDIO
mrb_irep*
......@@ -609,7 +639,7 @@ mrb_read_irep_file(mrb_state *mrb, FILE* fp)
if (fread(buf, header_size, 1, fp) == 0) {
goto irep_exit;
}
result = read_binary_header(buf, &buf_size, NULL, &flags);
result = read_binary_header(buf, (size_t)-1, &buf_size, NULL, &flags);
if (result != MRB_DUMP_OK || buf_size <= header_size) {
goto irep_exit;
}
......@@ -618,7 +648,7 @@ mrb_read_irep_file(mrb_state *mrb, FILE* fp)
if (fread(buf+header_size, buf_size-header_size, 1, fp) == 0) {
goto irep_exit;
}
irep = read_irep(mrb, buf, FLAG_SRC_MALLOC);
irep = read_irep(mrb, buf, (size_t)-1, FLAG_SRC_MALLOC);
irep_exit:
mrb_free(mrb, buf);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment