summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Buren <braewoods+rb@braewoods.net>2021-07-30 23:11:49 +0000
committerJames Buren <braewoods+rb@braewoods.net>2021-07-30 23:11:49 +0000
commitf32fc84ef6c0b95eec85b4436623a04d0b0bd6cb (patch)
treea5d6ca620ac6ce07b93b74f386ce21492b56595b
parentee05b8574a84fa7a46d6f0453b60a611c1bcc813 (diff)
downloadrockbox-f32fc84ef6c0b95eec85b4436623a04d0b0bd6cb.tar.gz
rockbox-f32fc84ef6c0b95eec85b4436623a04d0b0bd6cb.zip
adler32: import adapted implementation from tinf/zlib
This adds an adapted version of the adler32 algorithm from tinf/zlib which will be necessary to support ZLIB deflate streams in the future. Change-Id: Ie60e15acb288acf56a2d44e3d3e912e1b3eb2216
-rw-r--r--firmware/SOURCES1
-rw-r--r--firmware/common/adler32.c75
-rw-r--r--firmware/include/adler32.h29
3 files changed, 105 insertions, 0 deletions
diff --git a/firmware/SOURCES b/firmware/SOURCES
index fc194fe640..71a11429cd 100644
--- a/firmware/SOURCES
+++ b/firmware/SOURCES
@@ -269,6 +269,7 @@ common/timefuncs.c
269common/unicode.c 269common/unicode.c
270common/vuprintf.c 270common/vuprintf.c
271common/zip.c 271common/zip.c
272common/adler32.c
272 273
273/* Display */ 274/* Display */
274scroll_engine.c 275scroll_engine.c
diff --git a/firmware/common/adler32.c b/firmware/common/adler32.c
new file mode 100644
index 0000000000..8f5f85b453
--- /dev/null
+++ b/firmware/common/adler32.c
@@ -0,0 +1,75 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2021 James Buren (adaptations from tinf/zlib)
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#include "adler32.h"
23#include "system.h"
24
25/* adler_32 (derived from tinf adler32 which was taken from zlib)
26 * Adler-32 algorithm taken from the zlib source, which is
27 * Copyright (C) 1995-1998 Jean-loup Gailly and Mark Adler
28 */
29uint32_t adler_32(const void *src, uint32_t len, uint32_t adler32)
30{
31 const unsigned char *buf = (const unsigned char *)src;
32 uint32_t s1 = (adler32 & 0xffff);
33 uint32_t s2 = (adler32 >> 16);
34
35 enum {
36 A32_BASE = 65521,
37 A32_NMAX = 5552,
38 };
39
40 while (len > 0) {
41 uint32_t k = MIN(len, A32_NMAX);
42 uint32_t i;
43
44 for (i = k / 16; i; --i, buf += 16) {
45 s2 += s1 += buf[0];
46 s2 += s1 += buf[1];
47 s2 += s1 += buf[2];
48 s2 += s1 += buf[3];
49 s2 += s1 += buf[4];
50 s2 += s1 += buf[5];
51 s2 += s1 += buf[6];
52 s2 += s1 += buf[7];
53 s2 += s1 += buf[8];
54 s2 += s1 += buf[9];
55 s2 += s1 += buf[10];
56 s2 += s1 += buf[11];
57 s2 += s1 += buf[12];
58 s2 += s1 += buf[13];
59 s2 += s1 += buf[14];
60 s2 += s1 += buf[15];
61 }
62
63 for (i = k % 16; i; --i) {
64 s1 += *buf++;
65 s2 += s1;
66 }
67
68 s1 %= A32_BASE;
69 s2 %= A32_BASE;
70
71 len -= k;
72 }
73
74 return (s1 | (s2 << 16));
75}
diff --git a/firmware/include/adler32.h b/firmware/include/adler32.h
new file mode 100644
index 0000000000..cd5302e869
--- /dev/null
+++ b/firmware/include/adler32.h
@@ -0,0 +1,29 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2021 James Buren (adaptations from tinf/zlib)
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#include <stdint.h>
23
24#ifndef _ADLER32_H
25#define _ADLER32_H
26
27uint32_t adler_32(const void *src, uint32_t len, uint32_t adler32);
28
29#endif