]>
Commit | Line | Data |
---|---|---|
e1f414b6 | 1 | /** @file\r |
2 | Utility functions to generate checksum based on 2's complement\r | |
3 | algorithm.\r | |
4 | \r | |
24dcb5e5 | 5 | Copyright (c) 2007 - 2008, Intel Corporation<BR>\r |
e1f414b6 | 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 | |
e1f414b6 | 14 | **/\r |
15 | \r | |
47fc17d8 | 16 | #include "BaseLibInternals.h"\r |
f734a10a | 17 | \r |
e1f414b6 | 18 | /**\r |
9aa049d9 | 19 | Returns the sum of all elements in a buffer in unit of UINT8.\r |
e1f414b6 | 20 | During calculation, the carry bits are dropped.\r |
21 | \r | |
9aa049d9 | 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 | |
e1f414b6 | 25 | returned.\r |
9aa049d9 | 26 | \r |
e1f414b6 | 27 | If Buffer is NULL, then ASSERT().\r |
9aa049d9 | 28 | If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r |
e1f414b6 | 29 | \r |
1106ffe1 | 30 | @param Buffer Pointer to the buffer to carry out the sum operation.\r |
9aa049d9 | 31 | @param Length The size, in bytes, of Buffer.\r |
e1f414b6 | 32 | \r |
9aa049d9 | 33 | @return Sum The sum of Buffer with carry bits dropped during additions.\r |
e1f414b6 | 34 | \r |
35 | **/\r | |
36 | UINT8\r | |
37 | EFIAPI\r | |
38 | CalculateSum8 (\r | |
2fc60b70 | 39 | IN CONST UINT8 *Buffer,\r |
40 | IN UINTN Length\r | |
e1f414b6 | 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 = (UINT8) (Sum + *(Buffer + Count));\r | |
51 | }\r | |
52 | \r | |
53 | return Sum;\r | |
54 | }\r | |
55 | \r | |
56 | \r | |
57 | /**\r | |
9aa049d9 | 58 | Returns the two's complement checksum of all elements in a buffer\r |
e1f414b6 | 59 | of 8-bit values.\r |
60 | \r | |
9aa049d9 | 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 | |
e1f414b6 | 64 | returned. If Length is 0, then 0 is returned.\r |
9aa049d9 | 65 | \r |
e1f414b6 | 66 | If Buffer is NULL, then ASSERT().\r |
67 | If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r | |
68 | \r | |
1106ffe1 | 69 | @param Buffer Pointer to the buffer to carry out the checksum operation.\r |
70 | @param Length The size, in bytes, of Buffer.\r | |
e1f414b6 | 71 | \r |
9aa049d9 | 72 | @return Checksum The 2's complement checksum of Buffer.\r |
e1f414b6 | 73 | \r |
74 | **/\r | |
75 | UINT8\r | |
76 | EFIAPI\r | |
77 | CalculateCheckSum8 (\r | |
2fc60b70 | 78 | IN CONST UINT8 *Buffer,\r |
79 | IN UINTN Length\r | |
e1f414b6 | 80 | )\r |
81 | {\r | |
82 | UINT8 CheckSum;\r | |
83 | \r | |
84 | CheckSum = CalculateSum8 (Buffer, Length);\r | |
85 | \r | |
86 | //\r | |
87 | // Return the checksum based on 2's complement.\r | |
88 | //\r | |
89 | return (UINT8) (0x100 - CheckSum);\r | |
90 | }\r | |
91 | \r | |
92 | /**\r | |
9aa049d9 | 93 | Returns the sum of all elements in a buffer of 16-bit values. During\r |
e1f414b6 | 94 | calculation, the carry bits are dropped.\r |
95 | \r | |
9aa049d9 | 96 | This function calculates the sum of the 16-bit values in the buffer\r |
97 | specified by Buffer and Length. The carry bits in result of addition are dropped.\r | |
98 | The 16-bit result is returned. If Length is 0, then 0 is returned.\r | |
99 | \r | |
e1f414b6 | 100 | If Buffer is NULL, then ASSERT().\r |
101 | If Buffer is not aligned on a 16-bit boundary, then ASSERT().\r | |
102 | If Length is not aligned on a 16-bit boundary, then ASSERT().\r | |
103 | If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r | |
104 | \r | |
1106ffe1 | 105 | @param Buffer Pointer to the buffer to carry out the sum operation.\r |
106 | @param Length The size, in bytes, of Buffer.\r | |
e1f414b6 | 107 | \r |
9aa049d9 | 108 | @return Sum The sum of Buffer with carry bits dropped during additions.\r |
e1f414b6 | 109 | \r |
110 | **/\r | |
111 | UINT16\r | |
112 | EFIAPI\r | |
113 | CalculateSum16 (\r | |
2fc60b70 | 114 | IN CONST UINT16 *Buffer,\r |
115 | IN UINTN Length\r | |
e1f414b6 | 116 | )\r |
117 | {\r | |
118 | UINT16 Sum;\r | |
119 | UINTN Count;\r | |
f9cea76b | 120 | UINTN Total;\r |
e1f414b6 | 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 | |
f9cea76b | 127 | Total = Length / sizeof (*Buffer);\r |
128 | for (Sum = 0, Count = 0; Count < Total; Count++) {\r | |
e1f414b6 | 129 | Sum = (UINT16) (Sum + *(Buffer + Count));\r |
130 | }\r | |
131 | \r | |
132 | return Sum;\r | |
133 | }\r | |
134 | \r | |
135 | \r | |
136 | /**\r | |
9aa049d9 | 137 | Returns the two's complement checksum of all elements in a buffer of\r |
e1f414b6 | 138 | 16-bit values.\r |
139 | \r | |
9aa049d9 | 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 | |
e1f414b6 | 143 | is 0, then 0 is returned.\r |
9aa049d9 | 144 | \r |
e1f414b6 | 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 | |
9aa049d9 | 148 | If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r |
e1f414b6 | 149 | \r |
1106ffe1 | 150 | @param Buffer Pointer to the buffer to carry out the checksum operation.\r |
151 | @param Length The size, in bytes, of Buffer.\r | |
e1f414b6 | 152 | \r |
9aa049d9 | 153 | @return Checksum The 2's complement checksum of Buffer.\r |
e1f414b6 | 154 | \r |
155 | **/\r | |
156 | UINT16\r | |
157 | EFIAPI\r | |
158 | CalculateCheckSum16 (\r | |
2fc60b70 | 159 | IN CONST UINT16 *Buffer,\r |
160 | IN UINTN Length\r | |
e1f414b6 | 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 | |
9aa049d9 | 175 | Returns the sum of all elements in a buffer of 32-bit values. During\r |
e1f414b6 | 176 | calculation, the carry bits are dropped.\r |
177 | \r | |
9aa049d9 | 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 | |
e1f414b6 | 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 | |
1106ffe1 | 187 | @param Buffer Pointer to the buffer to carry out the sum operation.\r |
188 | @param Length The size, in bytes, of Buffer.\r | |
e1f414b6 | 189 | \r |
9aa049d9 | 190 | @return Sum The sum of Buffer with carry bits dropped during additions.\r |
e1f414b6 | 191 | \r |
192 | **/\r | |
193 | UINT32\r | |
194 | EFIAPI\r | |
195 | CalculateSum32 (\r | |
2fc60b70 | 196 | IN CONST UINT32 *Buffer,\r |
197 | IN UINTN Length\r | |
e1f414b6 | 198 | )\r |
199 | {\r | |
200 | UINT32 Sum;\r | |
201 | UINTN Count;\r | |
f9cea76b | 202 | UINTN Total;\r |
e1f414b6 | 203 | \r |
204 | ASSERT (Buffer != NULL);\r | |
205 | ASSERT (((UINTN) Buffer & 0x3) == 0);\r | |
206 | ASSERT ((Length & 0x3) == 0);\r | |
207 | ASSERT (Length <= (MAX_ADDRESS - ((UINTN) Buffer) + 1));\r | |
208 | \r | |
f9cea76b | 209 | Total = Length / sizeof (*Buffer);\r |
210 | for (Sum = 0, Count = 0; Count < Total; Count++) {\r | |
e1f414b6 | 211 | Sum = Sum + *(Buffer + Count);\r |
212 | }\r | |
213 | \r | |
214 | return Sum;\r | |
215 | }\r | |
216 | \r | |
217 | \r | |
218 | /**\r | |
9aa049d9 | 219 | Returns the two's complement checksum of all elements in a buffer of\r |
e1f414b6 | 220 | 32-bit values.\r |
221 | \r | |
9aa049d9 | 222 | This function first calculates the sum of the 32-bit values in the buffer\r |
223 | specified by Buffer and Length. The carry bits in the result of addition\r | |
224 | are dropped. Then, the two's complement of the sum is returned. If Length\r | |
e1f414b6 | 225 | is 0, then 0 is returned.\r |
9aa049d9 | 226 | \r |
e1f414b6 | 227 | If Buffer is NULL, then ASSERT().\r |
228 | If Buffer is not aligned on a 32-bit boundary, then ASSERT().\r | |
229 | If Length is not aligned on a 32-bit boundary, then ASSERT().\r | |
9aa049d9 | 230 | If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r |
e1f414b6 | 231 | \r |
1106ffe1 | 232 | @param Buffer Pointer to the buffer to carry out the checksum operation.\r |
233 | @param Length The size, in bytes, of Buffer.\r | |
e1f414b6 | 234 | \r |
9aa049d9 | 235 | @return Checksum The 2's complement checksum of Buffer.\r |
e1f414b6 | 236 | \r |
237 | **/\r | |
238 | UINT32\r | |
239 | EFIAPI\r | |
240 | CalculateCheckSum32 (\r | |
2fc60b70 | 241 | IN CONST UINT32 *Buffer,\r |
242 | IN UINTN Length\r | |
e1f414b6 | 243 | )\r |
244 | {\r | |
245 | UINT32 CheckSum;\r | |
246 | \r | |
247 | CheckSum = CalculateSum32 (Buffer, Length);\r | |
248 | \r | |
249 | //\r | |
250 | // Return the checksum based on 2's complement.\r | |
251 | //\r | |
252 | return (UINT32) ((UINT32)(-1) - CheckSum + 1);\r | |
253 | }\r | |
254 | \r | |
255 | \r | |
256 | /**\r | |
9aa049d9 | 257 | Returns the sum of all elements in a buffer of 64-bit values. During\r |
e1f414b6 | 258 | calculation, the carry bits are dropped.\r |
259 | \r | |
9aa049d9 | 260 | This function calculates the sum of the 64-bit values in the buffer\r |
261 | specified by Buffer and Length. The carry bits in result of addition are dropped.\r | |
262 | The 64-bit result is returned. If Length is 0, then 0 is returned.\r | |
263 | \r | |
e1f414b6 | 264 | If Buffer is NULL, then ASSERT().\r |
265 | If Buffer is not aligned on a 64-bit boundary, then ASSERT().\r | |
266 | If Length is not aligned on a 64-bit boundary, then ASSERT().\r | |
267 | If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r | |
268 | \r | |
1106ffe1 | 269 | @param Buffer Pointer to the buffer to carry out the sum operation.\r |
270 | @param Length The size, in bytes, of Buffer.\r | |
e1f414b6 | 271 | \r |
9aa049d9 | 272 | @return Sum The sum of Buffer with carry bits dropped during additions.\r |
e1f414b6 | 273 | \r |
274 | **/\r | |
275 | UINT64\r | |
276 | EFIAPI\r | |
277 | CalculateSum64 (\r | |
2fc60b70 | 278 | IN CONST UINT64 *Buffer,\r |
279 | IN UINTN Length\r | |
e1f414b6 | 280 | )\r |
281 | {\r | |
282 | UINT64 Sum;\r | |
283 | UINTN Count;\r | |
f9cea76b | 284 | UINTN Total;\r |
e1f414b6 | 285 | \r |
286 | ASSERT (Buffer != NULL);\r | |
287 | ASSERT (((UINTN) Buffer & 0x7) == 0);\r | |
288 | ASSERT ((Length & 0x7) == 0);\r | |
289 | ASSERT (Length <= (MAX_ADDRESS - ((UINTN) Buffer) + 1));\r | |
290 | \r | |
f9cea76b | 291 | Total = Length / sizeof (*Buffer);\r |
292 | for (Sum = 0, Count = 0; Count < Total; Count++) {\r | |
e1f414b6 | 293 | Sum = Sum + *(Buffer + Count);\r |
294 | }\r | |
295 | \r | |
296 | return Sum;\r | |
297 | }\r | |
298 | \r | |
299 | \r | |
300 | /**\r | |
9aa049d9 | 301 | Returns the two's complement checksum of all elements in a buffer of\r |
e1f414b6 | 302 | 64-bit values.\r |
303 | \r | |
9aa049d9 | 304 | This function first calculates the sum of the 64-bit values in the buffer\r |
305 | specified by Buffer and Length. The carry bits in the result of addition\r | |
306 | are dropped. Then, the two's complement of the sum is returned. If Length\r | |
e1f414b6 | 307 | is 0, then 0 is returned.\r |
9aa049d9 | 308 | \r |
e1f414b6 | 309 | If Buffer is NULL, then ASSERT().\r |
310 | If Buffer is not aligned on a 64-bit boundary, then ASSERT().\r | |
311 | If Length is not aligned on a 64-bit boundary, then ASSERT().\r | |
9aa049d9 | 312 | If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r |
e1f414b6 | 313 | \r |
1106ffe1 | 314 | @param Buffer Pointer to the buffer to carry out the checksum operation.\r |
315 | @param Length The size, in bytes, of Buffer.\r | |
e1f414b6 | 316 | \r |
9aa049d9 | 317 | @return Checksum The 2's complement checksum of Buffer.\r |
e1f414b6 | 318 | \r |
319 | **/\r | |
320 | UINT64\r | |
321 | EFIAPI\r | |
322 | CalculateCheckSum64 (\r | |
2fc60b70 | 323 | IN CONST UINT64 *Buffer,\r |
324 | IN UINTN Length\r | |
e1f414b6 | 325 | )\r |
326 | {\r | |
327 | UINT64 CheckSum;\r | |
328 | \r | |
329 | CheckSum = CalculateSum64 (Buffer, Length);\r | |
330 | \r | |
331 | //\r | |
332 | // Return the checksum based on 2's complement.\r | |
333 | //\r | |
334 | return (UINT64) ((UINT64)(-1) - CheckSum + 1);\r | |
335 | }\r | |
336 | \r | |
337 | \r |