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