1 | /* $Id: adler32.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Adler-32
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
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, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include <iprt/crc.h>
|
---|
42 | #include "internal/iprt.h"
|
---|
43 |
|
---|
44 | #include <iprt/asm.h>
|
---|
45 | #include <iprt/assert.h>
|
---|
46 |
|
---|
47 |
|
---|
48 | /*********************************************************************************************************************************
|
---|
49 | * Defined Constants And Macros *
|
---|
50 | *********************************************************************************************************************************/
|
---|
51 | #define RTCRC_ADLER_32_NUMBER 65521
|
---|
52 |
|
---|
53 |
|
---|
54 | RTDECL(uint32_t) RTCrcAdler32(void const *pv, size_t cb)
|
---|
55 | {
|
---|
56 | /* Don't want to do the unrolling twice. */
|
---|
57 | return RTCrcAdler32Process(RTCrcAdler32Start(), pv, cb);
|
---|
58 | }
|
---|
59 |
|
---|
60 |
|
---|
61 | RTDECL(uint32_t) RTCrcAdler32Start(void)
|
---|
62 | {
|
---|
63 | return 1;
|
---|
64 | }
|
---|
65 |
|
---|
66 |
|
---|
67 | RTDECL(uint32_t) RTCrcAdler32Process(uint32_t u32Crc, void const *pv, size_t cb)
|
---|
68 | {
|
---|
69 | uint8_t const *pbSrc = (uint8_t const *)pv;
|
---|
70 | uint32_t a = u32Crc & 0xffff;
|
---|
71 | uint32_t b = u32Crc >> 16;
|
---|
72 | if (cb < 64 /* randomly selected number */)
|
---|
73 | {
|
---|
74 | while (cb-- > 0)
|
---|
75 | {
|
---|
76 | a += *pbSrc++;
|
---|
77 | a %= RTCRC_ADLER_32_NUMBER;
|
---|
78 | b += a;
|
---|
79 | b %= RTCRC_ADLER_32_NUMBER;
|
---|
80 | }
|
---|
81 | }
|
---|
82 | else
|
---|
83 | {
|
---|
84 | switch (((uintptr_t)pbSrc & 0x3))
|
---|
85 | {
|
---|
86 | case 0:
|
---|
87 | break;
|
---|
88 |
|
---|
89 | case 1:
|
---|
90 | a += *pbSrc++;
|
---|
91 | a %= RTCRC_ADLER_32_NUMBER;
|
---|
92 | b += a;
|
---|
93 | b %= RTCRC_ADLER_32_NUMBER;
|
---|
94 | cb--;
|
---|
95 | RT_FALL_THRU();
|
---|
96 |
|
---|
97 | case 2:
|
---|
98 | a += *pbSrc++;
|
---|
99 | a %= RTCRC_ADLER_32_NUMBER;
|
---|
100 | b += a;
|
---|
101 | b %= RTCRC_ADLER_32_NUMBER;
|
---|
102 | cb--;
|
---|
103 | RT_FALL_THRU();
|
---|
104 |
|
---|
105 | case 3:
|
---|
106 | a += *pbSrc++;
|
---|
107 | a %= RTCRC_ADLER_32_NUMBER;
|
---|
108 | b += a;
|
---|
109 | b %= RTCRC_ADLER_32_NUMBER;
|
---|
110 | cb--;
|
---|
111 | break;
|
---|
112 | }
|
---|
113 |
|
---|
114 | while (cb >= 4)
|
---|
115 | {
|
---|
116 | uint32_t u32 = *(uint32_t const *)pbSrc;
|
---|
117 | pbSrc += 4;
|
---|
118 |
|
---|
119 | a += u32 & 0xff;
|
---|
120 | a %= RTCRC_ADLER_32_NUMBER;
|
---|
121 | b += a;
|
---|
122 | b %= RTCRC_ADLER_32_NUMBER;
|
---|
123 |
|
---|
124 | a += (u32 >> 8) & 0xff;
|
---|
125 | a %= RTCRC_ADLER_32_NUMBER;
|
---|
126 | b += a;
|
---|
127 | b %= RTCRC_ADLER_32_NUMBER;
|
---|
128 |
|
---|
129 | a += (u32 >> 16) & 0xff;
|
---|
130 | a %= RTCRC_ADLER_32_NUMBER;
|
---|
131 | b += a;
|
---|
132 | b %= RTCRC_ADLER_32_NUMBER;
|
---|
133 |
|
---|
134 | a += (u32 >> 24) & 0xff;
|
---|
135 | a %= RTCRC_ADLER_32_NUMBER;
|
---|
136 | b += a;
|
---|
137 | b %= RTCRC_ADLER_32_NUMBER;
|
---|
138 |
|
---|
139 | cb -= 4;
|
---|
140 | }
|
---|
141 |
|
---|
142 | switch (cb)
|
---|
143 | {
|
---|
144 | case 0:
|
---|
145 | break;
|
---|
146 |
|
---|
147 | case 3:
|
---|
148 | a += *pbSrc++;
|
---|
149 | a %= RTCRC_ADLER_32_NUMBER;
|
---|
150 | b += a;
|
---|
151 | b %= RTCRC_ADLER_32_NUMBER;
|
---|
152 | cb--;
|
---|
153 | RT_FALL_THRU();
|
---|
154 |
|
---|
155 | case 2:
|
---|
156 | a += *pbSrc++;
|
---|
157 | a %= RTCRC_ADLER_32_NUMBER;
|
---|
158 | b += a;
|
---|
159 | b %= RTCRC_ADLER_32_NUMBER;
|
---|
160 | cb--;
|
---|
161 | RT_FALL_THRU();
|
---|
162 |
|
---|
163 | case 1:
|
---|
164 | a += *pbSrc++;
|
---|
165 | a %= RTCRC_ADLER_32_NUMBER;
|
---|
166 | b += a;
|
---|
167 | b %= RTCRC_ADLER_32_NUMBER;
|
---|
168 | cb--;
|
---|
169 | break;
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | return a | (b << 16);
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 | RTDECL(uint32_t) RTCrcAdler32Finish(uint32_t u32Crc)
|
---|
178 | {
|
---|
179 | return u32Crc;
|
---|
180 | }
|
---|
181 |
|
---|