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