]> git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Foundation/Library/EdkIIGlueLib/Library/BaseLib/CheckSum.c
Update the copyright notice format
[mirror_edk2.git] / EdkCompatibilityPkg / Foundation / Library / EdkIIGlueLib / Library / BaseLib / CheckSum.c
1 /**
2 Utility functions to generate checksum based on 2's complement
3 algorithm.
4
5 Copyright (c) 2007, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 Module Name: CheckSum.c
15
16 **/
17
18 #include "BaseLibInternals.h"
19
20 /**
21 Calculate the sum of all elements in a buffer in unit of UINT8.
22 During calculation, the carry bits are dropped.
23
24 This function calculates the sum of all elements in a buffer
25 in unit of UINT8. The carry bits in result of addition are dropped.
26 The result is returned as UINT8. If Length is Zero, then Zero is
27 returned.
28
29 If Buffer is NULL, then ASSERT().
30 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
31
32 @param Buffer Pointer to the buffer to carry out the sum operation.
33 @param Length The size, in bytes, of Buffer .
34
35 @return Sum The sum of Buffer with carry bits dropped during additions.
36
37 **/
38 UINT8
39 EFIAPI
40 CalculateSum8 (
41 IN CONST UINT8 *Buffer,
42 IN UINTN Length
43 )
44 {
45 UINT8 Sum;
46 UINTN Count;
47
48 ASSERT (Buffer != NULL);
49 ASSERT (Length <= (MAX_ADDRESS - ((UINTN) Buffer) + 1));
50
51 for (Sum = 0, Count = 0; Count < Length; Count++) {
52 Sum = (UINT8) (Sum + *(Buffer + Count));
53 }
54
55 return Sum;
56 }
57
58
59 /**
60 Returns the two's complement checksum of all elements in a buffer
61 of 8-bit values.
62
63 This function first calculates the sum of the 8-bit values in the
64 buffer specified by Buffer and Length. The carry bits in the result
65 of addition are dropped. Then, the two's complement of the sum is
66 returned. If Length is 0, then 0 is returned.
67
68 If Buffer is NULL, then ASSERT().
69 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
70
71
72 @param Buffer Pointer to the buffer to carry out the checksum operation.
73 @param Length The size, in bytes, of Buffer.
74
75 @return Checksum The 2's complement checksum of Buffer.
76
77 **/
78 UINT8
79 EFIAPI
80 CalculateCheckSum8 (
81 IN CONST UINT8 *Buffer,
82 IN UINTN Length
83 )
84 {
85 UINT8 CheckSum;
86
87 CheckSum = CalculateSum8 (Buffer, Length);
88
89 //
90 // Return the checksum based on 2's complement.
91 //
92 return (UINT8) (0x100 - CheckSum);
93 }
94
95 /**
96 Returns the sum of all elements in a buffer of 16-bit values. During
97 calculation, the carry bits are dropped.
98
99 This function calculates the sum of the 16-bit values in the buffer
100 specified by Buffer and Length. The carry bits in result of addition are dropped.
101 The 16-bit result is returned. If Length is 0, then 0 is returned.
102
103 If Buffer is NULL, then ASSERT().
104 If Buffer is not aligned on a 16-bit boundary, then ASSERT().
105 If Length is not aligned on a 16-bit boundary, then ASSERT().
106 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
107
108 @param Buffer Pointer to the buffer to carry out the sum operation.
109 @param Length The size, in bytes, of Buffer.
110
111 @return Sum The sum of Buffer with carry bits dropped during additions.
112
113 **/
114 UINT16
115 EFIAPI
116 CalculateSum16 (
117 IN CONST UINT16 *Buffer,
118 IN UINTN Length
119 )
120 {
121 UINT16 Sum;
122 UINTN Count;
123
124 ASSERT (Buffer != NULL);
125 ASSERT (((UINTN) Buffer & 0x1) == 0);
126 ASSERT ((Length & 0x1) == 0);
127 ASSERT (Length <= (MAX_ADDRESS - ((UINTN) Buffer) + 1));
128
129
130 for (Sum = 0, Count = 0; Count < Length; Count++) {
131 Sum = (UINT16) (Sum + *(Buffer + Count));
132 }
133
134 return Sum;
135 }
136
137
138 /**
139 Returns the two's complement checksum of all elements in a buffer of
140 16-bit values.
141
142 This function first calculates the sum of the 16-bit values in the buffer
143 specified by Buffer and Length. The carry bits in the result of addition
144 are dropped. Then, the two's complement of the sum is returned. If Length
145 is 0, then 0 is returned.
146
147 If Buffer is NULL, then ASSERT().
148 If Buffer is not aligned on a 16-bit boundary, then ASSERT().
149 If Length is not aligned on a 16-bit boundary, then ASSERT().
150 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
151
152 @param Buffer Pointer to the buffer to carry out the checksum operation.
153 @param Length The size, in bytes, of Buffer.
154
155 @return Checksum The 2's complement checksum of Buffer.
156
157 **/
158 UINT16
159 EFIAPI
160 CalculateCheckSum16 (
161 IN CONST UINT16 *Buffer,
162 IN UINTN Length
163 )
164 {
165 UINT16 CheckSum;
166
167 CheckSum = CalculateSum16 (Buffer, Length);
168
169 //
170 // Return the checksum based on 2's complement.
171 //
172 return (UINT16) (0x10000 - CheckSum);
173 }
174
175
176 /**
177 Returns the sum of all elements in a buffer of 32-bit values. During
178 calculation, the carry bits are dropped.
179
180 This function calculates the sum of the 32-bit values in the buffer
181 specified by Buffer and Length. The carry bits in result of addition are dropped.
182 The 32-bit result is returned. If Length is 0, then 0 is returned.
183
184 If Buffer is NULL, then ASSERT().
185 If Buffer is not aligned on a 32-bit boundary, then ASSERT().
186 If Length is not aligned on a 32-bit boundary, then ASSERT().
187 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
188
189 @param Buffer Pointer to the buffer to carry out the sum operation.
190 @param Length The size, in bytes, of Buffer.
191
192 @return Sum The sum of Buffer with carry bits dropped during additions.
193
194 **/
195 UINT32
196 EFIAPI
197 CalculateSum32 (
198 IN CONST UINT32 *Buffer,
199 IN UINTN Length
200 )
201 {
202 UINT32 Sum;
203 UINTN Count;
204
205 ASSERT (Buffer != NULL);
206 ASSERT (((UINTN) Buffer & 0x3) == 0);
207 ASSERT ((Length & 0x3) == 0);
208 ASSERT (Length <= (MAX_ADDRESS - ((UINTN) Buffer) + 1));
209
210
211 for (Sum = 0, Count = 0; Count < Length; Count++) {
212 Sum = Sum + *(Buffer + Count);
213 }
214
215 return Sum;
216 }
217
218
219 /**
220 Returns the two's complement checksum of all elements in a buffer of
221 32-bit values.
222
223 This function first calculates the sum of the 32-bit values in the buffer
224 specified by Buffer and Length. The carry bits in the result of addition
225 are dropped. Then, the two's complement of the sum is returned. If Length
226 is 0, then 0 is returned.
227
228 If Buffer is NULL, then ASSERT().
229 If Buffer is not aligned on a 32-bit boundary, then ASSERT().
230 If Length is not aligned on a 32-bit boundary, then ASSERT().
231 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
232
233 @param Buffer Pointer to the buffer to carry out the checksum operation.
234 @param Length The size, in bytes, of Buffer.
235
236 @return Checksum The 2's complement checksum of Buffer.
237
238 **/
239 UINT32
240 EFIAPI
241 CalculateCheckSum32 (
242 IN CONST UINT32 *Buffer,
243 IN UINTN Length
244 )
245 {
246 UINT32 CheckSum;
247
248 CheckSum = CalculateSum32 (Buffer, Length);
249
250 //
251 // Return the checksum based on 2's complement.
252 //
253 return (UINT32) ((UINT32)(-1) - CheckSum + 1);
254 }
255
256
257 /**
258 Returns the sum of all elements in a buffer of 64-bit values. During
259 calculation, the carry bits are dropped.
260
261 This function calculates the sum of the 64-bit values in the buffer
262 specified by Buffer and Length. The carry bits in result of addition are dropped.
263 The 64-bit result is returned. If Length is 0, then 0 is returned.
264
265 If Buffer is NULL, then ASSERT().
266 If Buffer is not aligned on a 64-bit boundary, then ASSERT().
267 If Length is not aligned on a 64-bit boundary, then ASSERT().
268 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
269
270 @param Buffer Pointer to the buffer to carry out the sum operation.
271 @param Length The size, in bytes, of Buffer.
272
273 @return Sum The sum of Buffer with carry bits dropped during additions.
274
275 **/
276 UINT64
277 EFIAPI
278 CalculateSum64 (
279 IN CONST UINT64 *Buffer,
280 IN UINTN Length
281 )
282 {
283 UINT64 Sum;
284 UINTN Count;
285
286 ASSERT (Buffer != NULL);
287 ASSERT (((UINTN) Buffer & 0x7) == 0);
288 ASSERT ((Length & 0x7) == 0);
289 ASSERT (Length <= (MAX_ADDRESS - ((UINTN) Buffer) + 1));
290
291 for (Sum = 0, Count = 0; Count < Length; Count++) {
292 Sum = Sum + *(Buffer + Count);
293 }
294
295 return Sum;
296 }
297
298
299 /**
300 Returns the two's complement checksum of all elements in a buffer of
301 64-bit values.
302
303 This function first calculates the sum of the 64-bit values in the buffer
304 specified by Buffer and Length. The carry bits in the result of addition
305 are dropped. Then, the two's complement of the sum is returned. If Length
306 is 0, then 0 is returned.
307
308 If Buffer is NULL, then ASSERT().
309 If Buffer is not aligned on a 64-bit boundary, then ASSERT().
310 If Length is not aligned on a 64-bit boundary, then ASSERT().
311 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
312
313 @param Buffer Pointer to the buffer to carry out the checksum operation.
314 @param Length The size, in bytes, of Buffer.
315
316 @return Checksum The 2's complement checksum of Buffer.
317
318 **/
319 UINT64
320 EFIAPI
321 CalculateCheckSum64 (
322 IN CONST UINT64 *Buffer,
323 IN UINTN Length
324 )
325 {
326 UINT64 CheckSum;
327
328 CheckSum = CalculateSum64 (Buffer, Length);
329
330 //
331 // Return the checksum based on 2's complement.
332 //
333 return (UINT64) ((UINT64)(-1) - CheckSum + 1);
334 }
335
336