]>
Commit | Line | Data |
---|---|---|
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 | |
e1f414b6 | 14 | **/\r |
15 | \r | |
16 | //\r | |
17 | // Include common header file for this module.\r | |
18 | //\r | |
f734a10a A |
19 | #include <BaseLibInternals.h>\r |
20 | \r | |
e1f414b6 | 21 | \r |
22 | /**\r | |
23 | Calculate the sum of all elements in a buffer in unit of UINT8. \r | |
24 | During calculation, the carry bits are dropped.\r | |
25 | \r | |
26 | This function calculates the sum of all elements in a buffer \r | |
27 | in unit of UINT8. The carry bits in result of addition are dropped. \r | |
28 | The result is returned as UINT8. If Length is Zero, then Zero is \r | |
29 | returned.\r | |
30 | \r | |
31 | If Buffer is NULL, then ASSERT().\r | |
32 | If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r | |
33 | \r | |
1106ffe1 | 34 | @param Buffer Pointer to the buffer to carry out the sum operation.\r |
35 | @param Length The size, in bytes, of Buffer .\r | |
e1f414b6 | 36 | \r |
37 | @return Sum The sum of Buffer with carry bits dropped during additions.\r | |
38 | \r | |
39 | **/\r | |
40 | UINT8\r | |
41 | EFIAPI\r | |
42 | CalculateSum8 (\r | |
43 | IN CONST UINT8 *Buffer,\r | |
44 | IN UINTN Length\r | |
45 | )\r | |
46 | {\r | |
47 | UINT8 Sum;\r | |
48 | UINTN Count;\r | |
49 | \r | |
50 | ASSERT (Buffer != NULL);\r | |
51 | ASSERT (Length <= (MAX_ADDRESS - ((UINTN) Buffer) + 1));\r | |
52 | \r | |
53 | for (Sum = 0, Count = 0; Count < Length; Count++) {\r | |
54 | Sum = (UINT8) (Sum + *(Buffer + Count));\r | |
55 | }\r | |
56 | \r | |
57 | return Sum;\r | |
58 | }\r | |
59 | \r | |
60 | \r | |
61 | /**\r | |
62 | Returns the two's complement checksum of all elements in a buffer \r | |
63 | of 8-bit values.\r | |
64 | \r | |
65 | This function first calculates the sum of the 8-bit values in the \r | |
66 | buffer specified by Buffer and Length. The carry bits in the result \r | |
67 | of addition are dropped. Then, the two's complement of the sum is \r | |
68 | returned. If Length is 0, then 0 is returned.\r | |
69 | \r | |
70 | If Buffer is NULL, then ASSERT().\r | |
71 | If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r | |
72 | \r | |
1106ffe1 | 73 | @param Buffer Pointer to the buffer to carry out the checksum operation.\r |
74 | @param Length The size, in bytes, of Buffer.\r | |
e1f414b6 | 75 | \r |
76 | @return Checksum The 2's complement checksum of Buffer.\r | |
77 | \r | |
78 | **/\r | |
79 | UINT8\r | |
80 | EFIAPI\r | |
81 | CalculateCheckSum8 (\r | |
82 | IN CONST UINT8 *Buffer,\r | |
83 | IN UINTN Length\r | |
84 | )\r | |
85 | {\r | |
86 | UINT8 CheckSum;\r | |
87 | \r | |
88 | CheckSum = CalculateSum8 (Buffer, Length);\r | |
89 | \r | |
90 | //\r | |
91 | // Return the checksum based on 2's complement.\r | |
92 | //\r | |
93 | return (UINT8) (0x100 - CheckSum);\r | |
94 | }\r | |
95 | \r | |
96 | /**\r | |
97 | Returns the sum of all elements in a buffer of 16-bit values. During \r | |
98 | calculation, the carry bits are dropped.\r | |
99 | \r | |
100 | This function calculates the sum of the 16-bit values in the buffer \r | |
101 | specified by Buffer and Length. The carry bits in result of addition are dropped. \r | |
102 | The 16-bit result is returned. If Length is 0, then 0 is returned. \r | |
103 | \r | |
104 | If Buffer is NULL, then ASSERT().\r | |
105 | If Buffer is not aligned on a 16-bit boundary, then ASSERT().\r | |
106 | If Length is not aligned on a 16-bit boundary, then ASSERT().\r | |
107 | If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r | |
108 | \r | |
1106ffe1 | 109 | @param Buffer Pointer to the buffer to carry out the sum operation.\r |
110 | @param Length The size, in bytes, of Buffer.\r | |
e1f414b6 | 111 | \r |
112 | @return Sum The sum of Buffer with carry bits dropped during additions.\r | |
113 | \r | |
114 | **/\r | |
115 | UINT16\r | |
116 | EFIAPI\r | |
117 | CalculateSum16 (\r | |
118 | IN CONST UINT16 *Buffer,\r | |
119 | IN UINTN Length\r | |
120 | )\r | |
121 | {\r | |
122 | UINT16 Sum;\r | |
123 | UINTN Count;\r | |
f9cea76b | 124 | UINTN Total;\r |
e1f414b6 | 125 | \r |
126 | ASSERT (Buffer != NULL);\r | |
127 | ASSERT (((UINTN) Buffer & 0x1) == 0);\r | |
128 | ASSERT ((Length & 0x1) == 0);\r | |
129 | ASSERT (Length <= (MAX_ADDRESS - ((UINTN) Buffer) + 1));\r | |
130 | \r | |
f9cea76b | 131 | Total = Length / sizeof (*Buffer);\r |
132 | for (Sum = 0, Count = 0; Count < Total; Count++) {\r | |
e1f414b6 | 133 | Sum = (UINT16) (Sum + *(Buffer + Count));\r |
134 | }\r | |
135 | \r | |
136 | return Sum;\r | |
137 | }\r | |
138 | \r | |
139 | \r | |
140 | /**\r | |
141 | Returns the two's complement checksum of all elements in a buffer of \r | |
142 | 16-bit values.\r | |
143 | \r | |
144 | This function first calculates the sum of the 16-bit values in the buffer \r | |
145 | specified by Buffer and Length. The carry bits in the result of addition \r | |
146 | are dropped. Then, the two's complement of the sum is returned. If Length \r | |
147 | is 0, then 0 is returned.\r | |
148 | \r | |
149 | If Buffer is NULL, then ASSERT().\r | |
150 | If Buffer is not aligned on a 16-bit boundary, then ASSERT().\r | |
151 | If Length is not aligned on a 16-bit boundary, then ASSERT().\r | |
152 | If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r | |
153 | \r | |
1106ffe1 | 154 | @param Buffer Pointer to the buffer to carry out the checksum operation.\r |
155 | @param Length The size, in bytes, of Buffer.\r | |
e1f414b6 | 156 | \r |
157 | @return Checksum The 2's complement checksum of Buffer.\r | |
158 | \r | |
159 | **/\r | |
160 | UINT16\r | |
161 | EFIAPI\r | |
162 | CalculateCheckSum16 (\r | |
163 | IN CONST UINT16 *Buffer,\r | |
164 | IN UINTN Length\r | |
165 | )\r | |
166 | {\r | |
167 | UINT16 CheckSum;\r | |
168 | \r | |
169 | CheckSum = CalculateSum16 (Buffer, Length);\r | |
170 | \r | |
171 | //\r | |
172 | // Return the checksum based on 2's complement.\r | |
173 | //\r | |
174 | return (UINT16) (0x10000 - CheckSum);\r | |
175 | }\r | |
176 | \r | |
177 | \r | |
178 | /**\r | |
179 | Returns the sum of all elements in a buffer of 32-bit values. During \r | |
180 | calculation, the carry bits are dropped.\r | |
181 | \r | |
182 | This function calculates the sum of the 32-bit values in the buffer \r | |
183 | specified by Buffer and Length. The carry bits in result of addition are dropped. \r | |
184 | The 32-bit result is returned. If Length is 0, then 0 is returned. \r | |
185 | \r | |
186 | If Buffer is NULL, then ASSERT().\r | |
187 | If Buffer is not aligned on a 32-bit boundary, then ASSERT().\r | |
188 | If Length is not aligned on a 32-bit boundary, then ASSERT().\r | |
189 | If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r | |
190 | \r | |
1106ffe1 | 191 | @param Buffer Pointer to the buffer to carry out the sum operation.\r |
192 | @param Length The size, in bytes, of Buffer.\r | |
e1f414b6 | 193 | \r |
194 | @return Sum The sum of Buffer with carry bits dropped during additions.\r | |
195 | \r | |
196 | **/\r | |
197 | UINT32\r | |
198 | EFIAPI\r | |
199 | CalculateSum32 (\r | |
200 | IN CONST UINT32 *Buffer,\r | |
201 | IN UINTN Length\r | |
202 | )\r | |
203 | {\r | |
204 | UINT32 Sum;\r | |
205 | UINTN Count;\r | |
f9cea76b | 206 | UINTN Total;\r |
e1f414b6 | 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 | |
f9cea76b | 213 | Total = Length / sizeof (*Buffer);\r |
214 | for (Sum = 0, Count = 0; Count < Total; Count++) {\r | |
e1f414b6 | 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 | |
1106ffe1 | 236 | @param Buffer Pointer to the buffer to carry out the checksum operation.\r |
237 | @param Length The size, in bytes, of Buffer.\r | |
e1f414b6 | 238 | \r |
239 | @return Checksum The 2's complement checksum of Buffer.\r | |
240 | \r | |
241 | **/\r | |
242 | UINT32\r | |
243 | EFIAPI\r | |
244 | CalculateCheckSum32 (\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 | |
1106ffe1 | 273 | @param Buffer Pointer to the buffer to carry out the sum operation.\r |
274 | @param Length The size, in bytes, of Buffer.\r | |
e1f414b6 | 275 | \r |
276 | @return Sum The sum of Buffer with carry bits dropped during additions.\r | |
277 | \r | |
278 | **/\r | |
279 | UINT64\r | |
280 | EFIAPI\r | |
281 | CalculateSum64 (\r | |
282 | IN CONST UINT64 *Buffer,\r | |
283 | IN UINTN Length\r | |
284 | )\r | |
285 | {\r | |
286 | UINT64 Sum;\r | |
287 | UINTN Count;\r | |
f9cea76b | 288 | UINTN Total;\r |
e1f414b6 | 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 | |
f9cea76b | 295 | Total = Length / sizeof (*Buffer);\r |
296 | for (Sum = 0, Count = 0; Count < Total; Count++) {\r | |
e1f414b6 | 297 | Sum = Sum + *(Buffer + Count);\r |
298 | }\r | |
299 | \r | |
300 | return Sum;\r | |
301 | }\r | |
302 | \r | |
303 | \r | |
304 | /**\r | |
305 | Returns the two's complement checksum of all elements in a buffer of \r | |
306 | 64-bit values.\r | |
307 | \r | |
308 | This function first calculates the sum of the 64-bit values in the buffer \r | |
309 | specified by Buffer and Length. The carry bits in the result of addition \r | |
310 | are dropped. Then, the two's complement of the sum is returned. If Length \r | |
311 | is 0, then 0 is returned.\r | |
312 | \r | |
313 | If Buffer is NULL, then ASSERT().\r | |
314 | If Buffer is not aligned on a 64-bit boundary, then ASSERT().\r | |
315 | If Length is not aligned on a 64-bit boundary, then ASSERT().\r | |
316 | If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r | |
317 | \r | |
1106ffe1 | 318 | @param Buffer Pointer to the buffer to carry out the checksum operation.\r |
319 | @param Length The size, in bytes, of Buffer.\r | |
e1f414b6 | 320 | \r |
321 | @return Checksum The 2's complement checksum of Buffer.\r | |
322 | \r | |
323 | **/\r | |
324 | UINT64\r | |
325 | EFIAPI\r | |
326 | CalculateCheckSum64 (\r | |
327 | IN CONST UINT64 *Buffer,\r | |
328 | IN UINTN Length\r | |
329 | )\r | |
330 | {\r | |
331 | UINT64 CheckSum;\r | |
332 | \r | |
333 | CheckSum = CalculateSum64 (Buffer, Length);\r | |
334 | \r | |
335 | //\r | |
336 | // Return the checksum based on 2's complement.\r | |
337 | //\r | |
338 | return (UINT64) ((UINT64)(-1) - CheckSum + 1);\r | |
339 | }\r | |
340 | \r | |
341 | \r |