]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/CheckSum.c
Import some basic libraries instances for Mde Packages.
[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 Module Name: CheckSum.c
15
16 **/
17
18 //
19 // Include common header file for this module.
20 //
21 #include "CommonHeader.h"
22
23 /**
24 Calculate the sum of all elements in a buffer in unit of UINT8.
25 During calculation, the carry bits are dropped.
26
27 This function calculates the sum of all elements in a buffer
28 in unit of UINT8. The carry bits in result of addition are dropped.
29 The result is returned as UINT8. If Length is Zero, then Zero is
30 returned.
31
32 If Buffer is NULL, then ASSERT().
33 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
34
35 @param Buffer Pointer to the buffer to carry out the sum operation.
36 @param Length The size, in bytes, of Buffer .
37
38 @return Sum The sum of Buffer with carry bits dropped during additions.
39
40 **/
41 UINT8
42 EFIAPI
43 CalculateSum8 (
44 IN CONST UINT8 *Buffer,
45 IN UINTN Length
46 )
47 {
48 UINT8 Sum;
49 UINTN Count;
50
51 ASSERT (Buffer != NULL);
52 ASSERT (Length <= (MAX_ADDRESS - ((UINTN) Buffer) + 1));
53
54 for (Sum = 0, Count = 0; Count < Length; Count++) {
55 Sum = (UINT8) (Sum + *(Buffer + Count));
56 }
57
58 return Sum;
59 }
60
61
62 /**
63 Returns the two's complement checksum of all elements in a buffer
64 of 8-bit values.
65
66 This function first calculates the sum of the 8-bit values in the
67 buffer specified by Buffer and Length. The carry bits in the result
68 of addition are dropped. Then, the two's complement of the sum is
69 returned. If Length is 0, then 0 is returned.
70
71 If Buffer is NULL, then ASSERT().
72 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
73
74
75 @param Buffer Pointer to the buffer to carry out the checksum operation.
76 @param Length The size, in bytes, of Buffer.
77
78 @return Checksum The 2's complement checksum of Buffer.
79
80 **/
81 UINT8
82 EFIAPI
83 CalculateCheckSum8 (
84 IN CONST UINT8 *Buffer,
85 IN UINTN Length
86 )
87 {
88 UINT8 CheckSum;
89
90 CheckSum = CalculateSum8 (Buffer, Length);
91
92 //
93 // Return the checksum based on 2's complement.
94 //
95 return (UINT8) (0x100 - CheckSum);
96 }
97
98 /**
99 Returns the sum of all elements in a buffer of 16-bit values. During
100 calculation, the carry bits are dropped.
101
102 This function calculates the sum of the 16-bit values in the buffer
103 specified by Buffer and Length. The carry bits in result of addition are dropped.
104 The 16-bit result is returned. If Length is 0, then 0 is returned.
105
106 If Buffer is NULL, then ASSERT().
107 If Buffer is not aligned on a 16-bit boundary, then ASSERT().
108 If Length is not aligned on a 16-bit boundary, then ASSERT().
109 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
110
111 @param Buffer Pointer to the buffer to carry out the sum operation.
112 @param Length The size, in bytes, of Buffer.
113
114 @return Sum The sum of Buffer with carry bits dropped during additions.
115
116 **/
117 UINT16
118 EFIAPI
119 CalculateSum16 (
120 IN CONST UINT16 *Buffer,
121 IN UINTN Length
122 )
123 {
124 UINT16 Sum;
125 UINTN Count;
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
133 for (Sum = 0, Count = 0; Count < Length; 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
208 ASSERT (Buffer != NULL);
209 ASSERT (((UINTN) Buffer & 0x3) == 0);
210 ASSERT ((Length & 0x3) == 0);
211 ASSERT (Length <= (MAX_ADDRESS - ((UINTN) Buffer) + 1));
212
213
214 for (Sum = 0, Count = 0; Count < Length; Count++) {
215 Sum = Sum + *(Buffer + Count);
216 }
217
218 return Sum;
219 }
220
221
222 /**
223 Returns the two's complement checksum of all elements in a buffer of
224 32-bit values.
225
226 This function first calculates the sum of the 32-bit values in the buffer
227 specified by Buffer and Length. The carry bits in the result of addition
228 are dropped. Then, the two's complement of the sum is returned. If Length
229 is 0, then 0 is returned.
230
231 If Buffer is NULL, then ASSERT().
232 If Buffer is not aligned on a 32-bit boundary, then ASSERT().
233 If Length is not aligned on a 32-bit boundary, then ASSERT().
234 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
235
236 @param Buffer Pointer to the buffer to carry out the checksum operation.
237 @param Length The size, in bytes, of Buffer.
238
239 @return Checksum The 2's complement checksum of Buffer.
240
241 **/
242 UINT32
243 EFIAPI
244 CalculateCheckSum32 (
245 IN CONST UINT32 *Buffer,
246 IN UINTN Length
247 )
248 {
249 UINT32 CheckSum;
250
251 CheckSum = CalculateSum32 (Buffer, Length);
252
253 //
254 // Return the checksum based on 2's complement.
255 //
256 return (UINT32) ((UINT32)(-1) - CheckSum + 1);
257 }
258
259
260 /**
261 Returns the sum of all elements in a buffer of 64-bit values. During
262 calculation, the carry bits are dropped.
263
264 This function calculates the sum of the 64-bit values in the buffer
265 specified by Buffer and Length. The carry bits in result of addition are dropped.
266 The 64-bit result is returned. If Length is 0, then 0 is returned.
267
268 If Buffer is NULL, then ASSERT().
269 If Buffer is not aligned on a 64-bit boundary, then ASSERT().
270 If Length is not aligned on a 64-bit boundary, then ASSERT().
271 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
272
273 @param Buffer Pointer to the buffer to carry out the sum operation.
274 @param Length The size, in bytes, of Buffer.
275
276 @return Sum The sum of Buffer with carry bits dropped during additions.
277
278 **/
279 UINT64
280 EFIAPI
281 CalculateSum64 (
282 IN CONST UINT64 *Buffer,
283 IN UINTN Length
284 )
285 {
286 UINT64 Sum;
287 UINTN Count;
288
289 ASSERT (Buffer != NULL);
290 ASSERT (((UINTN) Buffer & 0x7) == 0);
291 ASSERT ((Length & 0x7) == 0);
292 ASSERT (Length <= (MAX_ADDRESS - ((UINTN) Buffer) + 1));
293
294 for (Sum = 0, Count = 0; Count < Length; Count++) {
295 Sum = Sum + *(Buffer + Count);
296 }
297
298 return Sum;
299 }
300
301
302 /**
303 Returns the two's complement checksum of all elements in a buffer of
304 64-bit values.
305
306 This function first calculates the sum of the 64-bit values in the buffer
307 specified by Buffer and Length. The carry bits in the result of addition
308 are dropped. Then, the two's complement of the sum is returned. If Length
309 is 0, then 0 is returned.
310
311 If Buffer is NULL, then ASSERT().
312 If Buffer is not aligned on a 64-bit boundary, then ASSERT().
313 If Length is not aligned on a 64-bit boundary, then ASSERT().
314 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
315
316 @param Buffer Pointer to the buffer to carry out the checksum operation.
317 @param Length The size, in bytes, of Buffer.
318
319 @return Checksum The 2's complement checksum of Buffer.
320
321 **/
322 UINT64
323 EFIAPI
324 CalculateCheckSum64 (
325 IN CONST UINT64 *Buffer,
326 IN UINTN Length
327 )
328 {
329 UINT64 CheckSum;
330
331 CheckSum = CalculateSum64 (Buffer, Length);
332
333 //
334 // Return the checksum based on 2's complement.
335 //
336 return (UINT64) ((UINT64)(-1) - CheckSum + 1);
337 }
338
339