summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Buren <braewoods+rb@braewoods.net>2021-06-21 03:37:17 +0000
committerJames Buren <braewoods+rb@braewoods.net>2021-06-21 03:37:17 +0000
commitfc9208108052440ccde79b6cd33e3598e8fb7c26 (patch)
treecf3c54cd5ca8bfb75596dee27f117b55adc748b2
parent2ca5774cf9613b0f2c1d31d6054911b8cb36e131 (diff)
downloadrockbox-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
-rw-r--r--apps/plugin.c1
-rw-r--r--apps/plugin.h1
-rw-r--r--firmware/common/crc32.c30
-rw-r--r--firmware/include/crc32.h1
4 files changed, 33 insertions, 0 deletions
diff --git a/apps/plugin.c b/apps/plugin.c
index 32cecc320f..a3970a88a7 100644
--- a/apps/plugin.c
+++ b/apps/plugin.c
@@ -792,6 +792,7 @@ static const struct plugin_api rockbox_api = {
792#ifdef PLUGIN_USE_IRAM 792#ifdef PLUGIN_USE_IRAM
793 audio_hard_stop, 793 audio_hard_stop,
794#endif 794#endif
795 crc_32r,
795 796
796 /* new stuff at the end, sort into place next time 797 /* new stuff at the end, sort into place next time
797 the API gets incompatible */ 798 the API gets incompatible */
diff --git a/apps/plugin.h b/apps/plugin.h
index 394a4dbbfd..64ced00bfa 100644
--- a/apps/plugin.h
+++ b/apps/plugin.h
@@ -919,6 +919,7 @@ struct plugin_api {
919#ifdef PLUGIN_USE_IRAM 919#ifdef PLUGIN_USE_IRAM
920 void (*audio_hard_stop)(void); 920 void (*audio_hard_stop)(void);
921#endif 921#endif
922 uint32_t (*crc_32r)(const void *src, uint32_t len, uint32_t crc32);
922 923
923 /* new stuff at the end, sort into place next time 924 /* new stuff at the end, sort into place next time
924 the API gets incompatible */ 925 the API gets incompatible */
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 */
72uint32_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
26uint32_t crc_32(const void *src, uint32_t len, uint32_t crc32); 26uint32_t crc_32(const void *src, uint32_t len, uint32_t crc32);
27uint32_t crc_32r(const void *src, uint32_t len, uint32_t crc32);
27 28
28#endif 29#endif
29 30