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