1 | /* $Id: adler32.cpp 21914 2009-07-31 12:09:58Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Adler-32
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #include <iprt/crc.h>
|
---|
36 | #include "internal/iprt.h"
|
---|
37 |
|
---|
38 | #include <iprt/asm.h>
|
---|
39 | #include <iprt/assert.h>
|
---|
40 |
|
---|
41 |
|
---|
42 | /*******************************************************************************
|
---|
43 | * Defined Constants And Macros *
|
---|
44 | *******************************************************************************/
|
---|
45 | #define RTCRC_ADLER_32_NUMBER 65521
|
---|
46 |
|
---|
47 |
|
---|
48 | RTDECL(uint32_t) RTCrcAdler32(void const *pv, size_t cb)
|
---|
49 | {
|
---|
50 | /* Don't want to do the unrolling twice. */
|
---|
51 | return RTCrcAdler32Process(RTCrcAdler32Start(), pv, cb);
|
---|
52 | }
|
---|
53 |
|
---|
54 |
|
---|
55 | RTDECL(uint32_t) RTCrcAdler32Start(void)
|
---|
56 | {
|
---|
57 | return 1;
|
---|
58 | }
|
---|
59 |
|
---|
60 |
|
---|
61 | RTDECL(uint32_t) RTCrcAdler32Process(uint32_t u32Crc, void const *pv, size_t cb)
|
---|
62 | {
|
---|
63 | uint8_t const *pbSrc = (uint8_t const *)pv;
|
---|
64 | uint32_t a = u32Crc & 0xffff;
|
---|
65 | uint32_t b = u32Crc >> 16;
|
---|
66 | if (cb < 64 /* randomly selected number */)
|
---|
67 | {
|
---|
68 | while (cb-- > 0)
|
---|
69 | {
|
---|
70 | a += *pbSrc++;
|
---|
71 | a %= RTCRC_ADLER_32_NUMBER;
|
---|
72 | b += a;
|
---|
73 | b %= RTCRC_ADLER_32_NUMBER;
|
---|
74 | }
|
---|
75 | }
|
---|
76 | else
|
---|
77 | {
|
---|
78 | switch (((uintptr_t)pbSrc & 0x3))
|
---|
79 | {
|
---|
80 | case 0:
|
---|
81 | break;
|
---|
82 |
|
---|
83 | case 1:
|
---|
84 | a += *pbSrc++;
|
---|
85 | a %= RTCRC_ADLER_32_NUMBER;
|
---|
86 | b += a;
|
---|
87 | b %= RTCRC_ADLER_32_NUMBER;
|
---|
88 | cb--;
|
---|
89 | /* fall thru */
|
---|
90 |
|
---|
91 | case 2:
|
---|
92 | a += *pbSrc++;
|
---|
93 | a %= RTCRC_ADLER_32_NUMBER;
|
---|
94 | b += a;
|
---|
95 | b %= RTCRC_ADLER_32_NUMBER;
|
---|
96 | cb--;
|
---|
97 | /* fall thru */
|
---|
98 |
|
---|
99 | case 3:
|
---|
100 | a += *pbSrc++;
|
---|
101 | a %= RTCRC_ADLER_32_NUMBER;
|
---|
102 | b += a;
|
---|
103 | b %= RTCRC_ADLER_32_NUMBER;
|
---|
104 | cb--;
|
---|
105 | break;
|
---|
106 | }
|
---|
107 |
|
---|
108 | while (cb >= 4)
|
---|
109 | {
|
---|
110 | uint32_t u32 = *(uint32_t const *)pbSrc;
|
---|
111 | pbSrc += 4;
|
---|
112 |
|
---|
113 | a += u32 & 0xff;
|
---|
114 | a %= RTCRC_ADLER_32_NUMBER;
|
---|
115 | b += a;
|
---|
116 | b %= RTCRC_ADLER_32_NUMBER;
|
---|
117 |
|
---|
118 | a += (u32 >> 8) & 0xff;
|
---|
119 | a %= RTCRC_ADLER_32_NUMBER;
|
---|
120 | b += a;
|
---|
121 | b %= RTCRC_ADLER_32_NUMBER;
|
---|
122 |
|
---|
123 | a += (u32 >> 16) & 0xff;
|
---|
124 | a %= RTCRC_ADLER_32_NUMBER;
|
---|
125 | b += a;
|
---|
126 | b %= RTCRC_ADLER_32_NUMBER;
|
---|
127 |
|
---|
128 | a += (u32 >> 24) & 0xff;
|
---|
129 | a %= RTCRC_ADLER_32_NUMBER;
|
---|
130 | b += a;
|
---|
131 | b %= RTCRC_ADLER_32_NUMBER;
|
---|
132 |
|
---|
133 | cb -= 4;
|
---|
134 | }
|
---|
135 |
|
---|
136 | switch (cb)
|
---|
137 | {
|
---|
138 | case 0:
|
---|
139 | break;
|
---|
140 |
|
---|
141 | case 3:
|
---|
142 | a += *pbSrc++;
|
---|
143 | a %= RTCRC_ADLER_32_NUMBER;
|
---|
144 | b += a;
|
---|
145 | b %= RTCRC_ADLER_32_NUMBER;
|
---|
146 | cb--;
|
---|
147 | /* fall thru */
|
---|
148 |
|
---|
149 | case 2:
|
---|
150 | a += *pbSrc++;
|
---|
151 | a %= RTCRC_ADLER_32_NUMBER;
|
---|
152 | b += a;
|
---|
153 | b %= RTCRC_ADLER_32_NUMBER;
|
---|
154 | cb--;
|
---|
155 | /* fall thru */
|
---|
156 |
|
---|
157 | case 1:
|
---|
158 | a += *pbSrc++;
|
---|
159 | a %= RTCRC_ADLER_32_NUMBER;
|
---|
160 | b += a;
|
---|
161 | b %= RTCRC_ADLER_32_NUMBER;
|
---|
162 | cb--;
|
---|
163 | break;
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | return a | (b << 16);
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 | RTDECL(uint32_t) RTCrcAdler32Finish(uint32_t u32Crc)
|
---|
172 | {
|
---|
173 | return u32Crc;
|
---|
174 | }
|
---|
175 |
|
---|