Firefox CRC calculation

This commit is contained in:
Davide Berardi 2024-02-19 17:09:27 +01:00
parent abeabc18e9
commit e32a5abea2
3 changed files with 22 additions and 7 deletions

View File

@ -1,4 +1,4 @@
CFLAGS+=-Wall -Werror -DVERSION='"v0.2.3"' CFLAGS+=-Wall -Werror -DVERSION='"v0.3.0"'
TARGET=snappy-fox TARGET=snappy-fox
.PHONY: all .PHONY: all

View File

@ -53,6 +53,8 @@ static uint8_t offset_dummy_byte = 0xff;
static uint32_t read_offset = 0; static uint32_t read_offset = 0;
/* Consider CRC Errors */ /* Consider CRC Errors */
static uint32_t consider_crc_errors = 0; static uint32_t consider_crc_errors = 0;
/* Use Firefox CRC32 implementation */
static uint32_t firefox_crc = 0;
/* CRC related functions */ /* CRC related functions */
static const uint32_t crc32c_table[] = { static const uint32_t crc32c_table[] = {
@ -124,13 +126,16 @@ static void crc32c(uint32_t *crc, const uint8_t *data, size_t len)
} }
static void crc32c_init(uint32_t *crc) { static void crc32c_init(uint32_t *crc) {
/* Initial value is 0 */ /* Initial value of CRC */
*crc = 0xffffffff; *crc = 0xffffffff;
} }
static void crc32c_fini(uint32_t *crc) { static void crc32c_fini(uint32_t *crc) {
/* Firefox uses unreversed CRCs */
if (!firefox_crc) {
/* Final step is to reverse the CRC Value */ /* Final step is to reverse the CRC Value */
*crc ^= 0xffffffff; *crc ^= 0xffffffff;
}
/* Mask the CRC */ /* Mask the CRC */
*crc = ((*crc >> 15) | (*crc << 17)) + 0xa282ead8; *crc = ((*crc >> 15) | (*crc << 17)) + 0xa282ead8;
} }
@ -628,8 +633,10 @@ static void usage(const char *progname) {
progname); progname);
fprintf(stderr, " files can be specified as - for stdin or stdout\n"); fprintf(stderr, " files can be specified as - for stdin or stdout\n");
fprintf(stderr, " Options:\n"); fprintf(stderr, " Options:\n");
fprintf(stderr, " -C --consider_crc_errors Consider CRC errors as fatal\n");
fprintf(stderr, " -E --ignore_offset_errors [substitution byte] Ignore any offset errors that occurs\n"); fprintf(stderr, " -E --ignore_offset_errors [substitution byte] Ignore any offset errors that occurs\n");
fprintf(stderr, " -O --read_offset [offset] Start reading file from offset\n"); fprintf(stderr, " -O --read_offset [offset] Start reading file from offset\n");
fprintf(stderr, " -f --firefox Use firefox's CRC algorithm\n");
fprintf(stderr, " -u --unframed Assume Unframed stream in input file\n"); fprintf(stderr, " -u --unframed Assume Unframed stream in input file\n");
fprintf(stderr, " -h --help This Help\n"); fprintf(stderr, " -h --help This Help\n");
fprintf(stderr, " -v --version Print Version and exit\n"); fprintf(stderr, " -v --version Print Version and exit\n");
@ -645,6 +652,7 @@ int main(int argc, char **argv) {
{"consider_crc_errors", no_argument, 0, 'C'}, {"consider_crc_errors", no_argument, 0, 'C'},
{"ignore_offset_errors", optional_argument, 0, 'E'}, {"ignore_offset_errors", optional_argument, 0, 'E'},
{"read_offset", required_argument, 0, 'O'}, {"read_offset", required_argument, 0, 'O'},
{"firefox", no_argument, 0, 'f'},
{"unframed", no_argument, 0, 'u'}, {"unframed", no_argument, 0, 'u'},
{"version", no_argument, 0, 'v'}, {"version", no_argument, 0, 'v'},
{"help", no_argument, 0, 'h'}, {"help", no_argument, 0, 'h'},
@ -652,7 +660,7 @@ int main(int argc, char **argv) {
}; };
while (c != -1) { while (c != -1) {
c = getopt_long(argc, argv, "CO:E::uhv", flags, &option_idx); c = getopt_long(argc, argv, "CO:E::fuhv", flags, &option_idx);
switch (c) { switch (c) {
case 'C': case 'C':
consider_crc_errors = 1; consider_crc_errors = 1;
@ -667,6 +675,9 @@ int main(int argc, char **argv) {
if (optarg != NULL) if (optarg != NULL)
read_offset = strtol(optarg, NULL, 0); read_offset = strtol(optarg, NULL, 0);
break; break;
case 'f':
firefox_crc = 1;
break;
case 'u': case 'u':
unframed_stream = 1; unframed_stream = 1;
break; break;

View File

@ -22,11 +22,15 @@ test001() {
./snappy-fox example/exampleimage.snappy example/exampleimage.jpg ./snappy-fox example/exampleimage.snappy example/exampleimage.jpg
file example/exampleimage.jpg | grep -q JPEG file example/exampleimage.jpg | grep -q JPEG
# The example image is CRC corrupted # The example image is CRC corrupted (it uses firefox CRCs)
echo "[Test 001 b] CRC Corruption" echo "[Test 001 b] CRC Corruption"
! ./snappy-fox --consider_crc_errors \ ! ./snappy-fox --consider_crc_errors \
example/exampleimage.snappy example/exampleimage.jpg example/exampleimage.snappy example/exampleimage.jpg
echo "[Test 001 b] Firefox CRC Test"
./snappy-fox --consider_crc_errors --firefox \
example/exampleimage.snappy example/exampleimage.jpg
# Test on the corrupted image # Test on the corrupted image
echo "[Test 001 c] Corrupted image" echo "[Test 001 c] Corrupted image"
! ./snappy-fox \ ! ./snappy-fox \