diff options
author | James Buren <braewoods+rb@braewoods.net> | 2021-06-21 03:37:17 +0000 |
---|---|---|
committer | James Buren <braewoods+rb@braewoods.net> | 2021-06-21 03:37:17 +0000 |
commit | fc9208108052440ccde79b6cd33e3598e8fb7c26 (patch) | |
tree | cf3c54cd5ca8bfb75596dee27f117b55adc748b2 /firmware | |
parent | 2ca5774cf9613b0f2c1d31d6054911b8cb36e131 (diff) | |
download | rockbox-fc9208108052440ccde79b6cd33e3598e8fb7c26.tar.gz rockbox-fc9208108052440ccde79b6cd33e3598e8fb7c26.zip |
rockbox: add a crc32 reverse polynomial function
This uses the reverse of the polynomial used by the current crc_32
function. The code for this was derived from the implementation used
by tinf. This version is space optimized and should be a good way to
reduce code duplication in other parts of rockbox that use the same
crc32 algorithm. This is mainly of use in areas where DEFLATE is in
use.
Change-Id: I918da5b4ea4dc441c0e7e6b5007abcc2da463bcb
Diffstat (limited to 'firmware')
-rw-r--r-- | firmware/common/crc32.c | 30 | ||||
-rw-r--r-- | firmware/include/crc32.h | 1 |
2 files changed, 31 insertions, 0 deletions
diff --git a/firmware/common/crc32.c b/firmware/common/crc32.c index c8c70e415c..1a9bc4faea 100644 --- a/firmware/common/crc32.c +++ b/firmware/common/crc32.c | |||
@@ -61,3 +61,33 @@ uint32_t crc_32(const void *src, uint32_t len, uint32_t crc32) | |||
61 | return crc32; | 61 | return crc32; |
62 | } | 62 | } |
63 | 63 | ||
64 | |||
65 | /* crc_32r (derived from tinf crc32 which was taken from zlib) | ||
66 | * CRC32 algorithm taken from the zlib source, which is | ||
67 | * Copyright (C) 1995-1998 Jean-loup Gailly and Mark Adler | ||
68 | */ | ||
69 | |||
70 | /* Tool function to calculate a CRC32 (reversed polynomial) across some buffer */ | ||
71 | /* third argument is either the starting value or value from last piece */ | ||
72 | uint32_t crc_32r(const void *src, uint32_t len, uint32_t crc32) | ||
73 | { | ||
74 | const unsigned char* buf = src; | ||
75 | |||
76 | /* reversed polynomial from other crc32 function -- 0xEDB88320 */ | ||
77 | static const unsigned crc32_lookup[16] = | ||
78 | { | ||
79 | 0x00000000, 0x1DB71064, 0x3B6E20C8, 0x26D930AC, | ||
80 | 0x76DC4190, 0x6B6B51F4, 0x4DB26158, 0x5005713C, | ||
81 | 0xEDB88320, 0xF00F9344, 0xD6D6A3E8, 0xCB61B38C, | ||
82 | 0x9B64C2B0, 0x86D3D2D4, 0xA00AE278, 0xBDBDF21C | ||
83 | }; | ||
84 | |||
85 | for(uint32_t i = 0; i < len; i++) | ||
86 | { | ||
87 | crc32 ^= buf[i]; | ||
88 | crc32 = crc32_lookup[crc32 & 0x0F] ^ (crc32 >> 4); | ||
89 | crc32 = crc32_lookup[crc32 & 0x0F] ^ (crc32 >> 4); | ||
90 | } | ||
91 | |||
92 | return crc32; | ||
93 | } | ||
diff --git a/firmware/include/crc32.h b/firmware/include/crc32.h index 8e1f868988..adf7e2bc6e 100644 --- a/firmware/include/crc32.h +++ b/firmware/include/crc32.h | |||
@@ -24,6 +24,7 @@ | |||
24 | #define _CRC32_H | 24 | #define _CRC32_H |
25 | 25 | ||
26 | uint32_t crc_32(const void *src, uint32_t len, uint32_t crc32); | 26 | uint32_t crc_32(const void *src, uint32_t len, uint32_t crc32); |
27 | uint32_t crc_32r(const void *src, uint32_t len, uint32_t crc32); | ||
27 | 28 | ||
28 | #endif | 29 | #endif |
29 | 30 | ||