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