mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Remove CRC16 from dumped mruby binary.
`calc_crc_16_ccitt()` consumes a lot of clock cycles in programs like `mrbtest` which loads a lot of dumped binary. Error detection for flaky channels should be done in the higher level. Note: `mruby/c` should be updated to support this change.
This commit is contained in:
@@ -39,10 +39,9 @@ MRB_API mrb_irep *mrb_read_irep_buf(mrb_state*, const void*, size_t);
|
||||
#define MRB_DUMP_GENERAL_FAILURE (-1)
|
||||
#define MRB_DUMP_WRITE_FAULT (-2)
|
||||
#define MRB_DUMP_READ_FAULT (-3)
|
||||
#define MRB_DUMP_CRC_ERROR (-4)
|
||||
#define MRB_DUMP_INVALID_FILE_HEADER (-5)
|
||||
#define MRB_DUMP_INVALID_IREP (-6)
|
||||
#define MRB_DUMP_INVALID_ARGUMENT (-7)
|
||||
#define MRB_DUMP_INVALID_FILE_HEADER (-4)
|
||||
#define MRB_DUMP_INVALID_IREP (-5)
|
||||
#define MRB_DUMP_INVALID_ARGUMENT (-6)
|
||||
|
||||
/* null symbol length */
|
||||
#define MRB_DUMP_NULL_SYM_LEN 0xFFFF
|
||||
@@ -73,7 +72,6 @@ struct rite_binary_header {
|
||||
uint8_t binary_ident[4]; /* Binary Identifier */
|
||||
uint8_t major_version[2]; /* Binary Format Major Version */
|
||||
uint8_t minor_version[2]; /* Binary Format Minor Version */
|
||||
uint8_t binary_crc[2]; /* Binary CRC */
|
||||
uint8_t binary_size[4]; /* Binary Size */
|
||||
uint8_t compiler_name[4]; /* Compiler name */
|
||||
uint8_t compiler_version[4];
|
||||
@@ -157,8 +155,4 @@ bin_to_uint8(const uint8_t *bin)
|
||||
|
||||
MRB_END_DECL
|
||||
|
||||
/** @internal crc.c */
|
||||
uint16_t
|
||||
calc_crc_16_ccitt(const uint8_t *src, size_t nbytes, uint16_t crc);
|
||||
|
||||
#endif /* MRUBY_DUMP_H */
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
** crc.c - calculate CRC
|
||||
**
|
||||
** See Copyright Notice in mruby.h
|
||||
*/
|
||||
|
||||
#include <limits.h>
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
/* Calculate CRC (CRC-16-CCITT)
|
||||
**
|
||||
** 0000_0000_0000_0000_0000_0000_0000_0000
|
||||
** ^|------- CRC -------|- work --|
|
||||
** carry
|
||||
*/
|
||||
#define CRC_16_CCITT 0x11021ul /* x^16+x^12+x^5+1 */
|
||||
#define CRC_XOR_PATTERN (CRC_16_CCITT << 8)
|
||||
#define CRC_CARRY_BIT (0x01000000)
|
||||
|
||||
uint16_t
|
||||
calc_crc_16_ccitt(const uint8_t *src, size_t nbytes, uint16_t crc)
|
||||
{
|
||||
size_t ibyte;
|
||||
uint32_t ibit;
|
||||
uint32_t crcwk = crc << 8;
|
||||
|
||||
for (ibyte = 0; ibyte < nbytes; ibyte++) {
|
||||
crcwk |= *src++;
|
||||
for (ibit = 0; ibit < CHAR_BIT; ibit++) {
|
||||
crcwk <<= 1;
|
||||
if (crcwk & CRC_CARRY_BIT) {
|
||||
crcwk ^= CRC_XOR_PATTERN;
|
||||
}
|
||||
}
|
||||
}
|
||||
return (uint16_t)(crcwk >> 8);
|
||||
}
|
||||
|
||||
@@ -747,8 +747,6 @@ static int
|
||||
write_rite_binary_header(mrb_state *mrb, size_t binary_size, uint8_t *bin, uint8_t flags)
|
||||
{
|
||||
struct rite_binary_header *header = (struct rite_binary_header *)bin;
|
||||
uint16_t crc;
|
||||
uint32_t offset;
|
||||
|
||||
memcpy(header->binary_ident, RITE_BINARY_IDENT, sizeof(header->binary_ident));
|
||||
memcpy(header->major_version, RITE_BINARY_MAJOR_VER, sizeof(header->major_version));
|
||||
@@ -758,10 +756,6 @@ write_rite_binary_header(mrb_state *mrb, size_t binary_size, uint8_t *bin, uint8
|
||||
mrb_assert(binary_size <= UINT32_MAX);
|
||||
uint32_to_bin((uint32_t)binary_size, header->binary_size);
|
||||
|
||||
offset = (uint32_t)((&(header->binary_crc[0]) - bin) + sizeof(uint16_t));
|
||||
crc = calc_crc_16_ccitt(bin + offset, binary_size - offset, 0);
|
||||
uint16_to_bin(crc, header->binary_crc);
|
||||
|
||||
return MRB_DUMP_OK;
|
||||
}
|
||||
|
||||
|
||||
+3
-20
@@ -37,13 +37,6 @@
|
||||
return irep; \
|
||||
}
|
||||
|
||||
static size_t
|
||||
offset_crc_body(void)
|
||||
{
|
||||
struct rite_binary_header header;
|
||||
return ((uint8_t *)header.binary_crc - (uint8_t *)&header) + sizeof(header.binary_crc);
|
||||
}
|
||||
|
||||
#ifndef MRB_NO_FLOAT
|
||||
static double
|
||||
str_to_double(mrb_state *mrb, const char *p)
|
||||
@@ -519,7 +512,7 @@ lv_exit:
|
||||
}
|
||||
|
||||
static int
|
||||
read_binary_header(const uint8_t *bin, size_t bufsize, size_t *bin_size, uint16_t *crc, uint8_t *flags)
|
||||
read_binary_header(const uint8_t *bin, size_t bufsize, size_t *bin_size, uint8_t *flags)
|
||||
{
|
||||
const struct rite_binary_header *header = (const struct rite_binary_header *)bin;
|
||||
|
||||
@@ -540,9 +533,6 @@ read_binary_header(const uint8_t *bin, size_t bufsize, size_t *bin_size, uint16_
|
||||
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) {
|
||||
@@ -559,24 +549,17 @@ read_irep(mrb_state *mrb, const uint8_t *bin, size_t bufsize, uint8_t flags)
|
||||
struct RProc *proc = NULL;
|
||||
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, bufsize, &bin_size, &crc, &flags);
|
||||
result = read_binary_header(bin, bufsize, &bin_size, &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;
|
||||
@@ -705,7 +688,7 @@ mrb_proc_read_irep_file(mrb_state *mrb, FILE *fp)
|
||||
if (fread(buf, header_size, 1, fp) == 0) {
|
||||
goto irep_exit;
|
||||
}
|
||||
result = read_binary_header(buf, (size_t)-1, &buf_size, NULL, &flags);
|
||||
result = read_binary_header(buf, (size_t)-1, &buf_size, &flags);
|
||||
if (result != MRB_DUMP_OK || buf_size <= header_size) {
|
||||
goto irep_exit;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user