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*); ...@@ -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*); MRB_API mrb_value mrb_load_irep_file_cxt(mrb_state*, FILE*, mrbc_context*);
#endif #endif
MRB_API mrb_irep *mrb_read_irep(mrb_state*, const uint8_t*); 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 /* dump/load error code
* *
......
...@@ -52,9 +52,21 @@ MRB_API mrb_irep *mrb_add_irep(mrb_state *mrb); ...@@ -52,9 +52,21 @@ MRB_API mrb_irep *mrb_add_irep(mrb_state *mrb);
/* @param [const uint8_t*] irep code, expected as a literal */ /* @param [const uint8_t*] irep code, expected as a literal */
MRB_API mrb_value mrb_load_irep(mrb_state*, const uint8_t*); 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 */ /* @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*); 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_free(mrb_state*, struct mrb_irep*);
void mrb_irep_incref(mrb_state*, struct mrb_irep*); void mrb_irep_incref(mrb_state*, struct mrb_irep*);
void mrb_irep_decref(mrb_state*, struct mrb_irep*); void mrb_irep_decref(mrb_state*, struct mrb_irep*);
......
...@@ -459,10 +459,14 @@ lv_exit: ...@@ -459,10 +459,14 @@ lv_exit:
} }
static int 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; 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 (memcmp(header->binary_ident, RITE_BINARY_IDENT, sizeof(header->binary_ident)) == 0) {
if (bigendian_p()) if (bigendian_p())
*flags |= FLAG_BYTEORDER_NATIVE; *flags |= FLAG_BYTEORDER_NATIVE;
...@@ -479,16 +483,24 @@ read_binary_header(const uint8_t *bin, size_t *bin_size, uint16_t *crc, uint8_t ...@@ -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; 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) { if (crc) {
*crc = bin_to_uint16(header->binary_crc); *crc = bin_to_uint16(header->binary_crc);
} }
*bin_size = (size_t)bin_to_uint32(header->binary_size); *bin_size = (size_t)bin_to_uint32(header->binary_size);
if (bufsize < *bin_size) {
return MRB_DUMP_READ_FAULT;
}
return MRB_DUMP_OK; return MRB_DUMP_OK;
} }
static mrb_irep* 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; int result;
mrb_irep *irep = NULL; mrb_irep *irep = NULL;
...@@ -501,7 +513,7 @@ read_irep(mrb_state *mrb, const uint8_t *bin, uint8_t flags) ...@@ -501,7 +513,7 @@ read_irep(mrb_state *mrb, const uint8_t *bin, uint8_t flags)
return NULL; 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) { if (result != MRB_DUMP_OK) {
return NULL; return NULL;
} }
...@@ -547,7 +559,13 @@ mrb_read_irep(mrb_state *mrb, const uint8_t *bin) ...@@ -547,7 +559,13 @@ mrb_read_irep(mrb_state *mrb, const uint8_t *bin)
uint8_t flags = FLAG_SRC_STATIC; uint8_t flags = FLAG_SRC_STATIC;
#endif #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); 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) ...@@ -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); 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_API mrb_value
mrb_load_irep(mrb_state *mrb, const uint8_t *bin) mrb_load_irep(mrb_state *mrb, const uint8_t *bin)
{ {
return mrb_load_irep_cxt(mrb, bin, NULL); 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 #ifndef MRB_DISABLE_STDIO
mrb_irep* mrb_irep*
...@@ -609,7 +639,7 @@ mrb_read_irep_file(mrb_state *mrb, FILE* fp) ...@@ -609,7 +639,7 @@ mrb_read_irep_file(mrb_state *mrb, FILE* fp)
if (fread(buf, header_size, 1, fp) == 0) { if (fread(buf, header_size, 1, fp) == 0) {
goto irep_exit; 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) { if (result != MRB_DUMP_OK || buf_size <= header_size) {
goto irep_exit; goto irep_exit;
} }
...@@ -618,7 +648,7 @@ mrb_read_irep_file(mrb_state *mrb, FILE* fp) ...@@ -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) { if (fread(buf+header_size, buf_size-header_size, 1, fp) == 0) {
goto irep_exit; goto irep_exit;
} }
irep = read_irep(mrb, buf, FLAG_SRC_MALLOC); irep = read_irep(mrb, buf, (size_t)-1, FLAG_SRC_MALLOC);
irep_exit: irep_exit:
mrb_free(mrb, buf); 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