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