]>
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 | |
34 | @param Buffer Pointer to the buffer to carry out the sum operation.\r | |
35 | @param Length The size, in bytes, of Buffer .\r | |
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 | |
73 | \r | |
74 | @param Buffer Pointer to the buffer to carry out the checksum operation.\r | |
75 | @param Length The size, in bytes, of Buffer.\r | |
76 | \r | |
77 | @return Checksum The 2's complement checksum of Buffer.\r | |
78 | \r | |
79 | **/\r | |
80 | UINT8\r | |
81 | EFIAPI\r | |
82 | CalculateCheckSum8 (\r | |
83 | IN CONST UINT8 *Buffer,\r | |
84 | IN UINTN Length\r | |
85 | )\r | |
86 | {\r | |
87 | UINT8 CheckSum;\r | |
88 | \r | |
89 | CheckSum = CalculateSum8 (Buffer, Length);\r | |
90 | \r | |
91 | //\r | |
92 | // Return the checksum based on 2's complement.\r | |
93 | //\r | |
94 | return (UINT8) (0x100 - CheckSum);\r | |
95 | }\r | |
96 | \r | |
97 | /**\r | |
98 | Returns the sum of all elements in a buffer of 16-bit values. During \r | |
99 | calculation, the carry bits are dropped.\r | |
100 | \r | |
101 | This function calculates the sum of the 16-bit values in the buffer \r | |
102 | specified by Buffer and Length. The carry bits in result of addition are dropped. \r | |
103 | The 16-bit result is returned. If Length is 0, then 0 is returned. \r | |
104 | \r | |
105 | If Buffer is NULL, then ASSERT().\r | |
106 | If Buffer is not aligned on a 16-bit boundary, then ASSERT().\r | |
107 | If Length is not aligned on a 16-bit boundary, then ASSERT().\r | |
108 | If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r | |
109 | \r | |
110 | @param Buffer Pointer to the buffer to carry out the sum operation.\r | |
111 | @param Length The size, in bytes, of Buffer.\r | |
112 | \r | |
113 | @return Sum The sum of Buffer with carry bits dropped during additions.\r | |
114 | \r | |
115 | **/\r | |
116 | UINT16\r | |
117 | EFIAPI\r | |
118 | CalculateSum16 (\r | |
119 | IN CONST UINT16 *Buffer,\r | |
120 | IN UINTN Length\r | |
121 | )\r | |
122 | {\r | |
123 | UINT16 Sum;\r | |
124 | UINTN Count;\r | |
f9cea76b | 125 | UINTN Total;\r |
e1f414b6 | 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 | |
f9cea76b | 132 | Total = Length / sizeof (*Buffer);\r |
133 | for (Sum = 0, Count = 0; Count < Total; Count++) {\r | |
e1f414b6 | 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 | |
161 | UINT16\r | |
162 | EFIAPI\r | |
163 | CalculateCheckSum16 (\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 | |
198 | UINT32\r | |
199 | EFIAPI\r | |
200 | CalculateSum32 (\r | |
201 | IN CONST UINT32 *Buffer,\r | |
202 | IN UINTN Length\r | |
203 | )\r | |
204 | {\r | |
205 | UINT32 Sum;\r | |
206 | UINTN Count;\r | |
f9cea76b | 207 | UINTN Total;\r |
e1f414b6 | 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 | |
f9cea76b | 214 | Total = Length / sizeof (*Buffer);\r |
215 | for (Sum = 0, Count = 0; Count < Total; Count++) {\r | |
e1f414b6 | 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 | |
243 | UINT32\r | |
244 | EFIAPI\r | |
245 | CalculateCheckSum32 (\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 | |
280 | UINT64\r | |
281 | EFIAPI\r | |
282 | CalculateSum64 (\r | |
283 | IN CONST UINT64 *Buffer,\r | |
284 | IN UINTN Length\r | |
285 | )\r | |
286 | {\r | |
287 | UINT64 Sum;\r | |
288 | UINTN Count;\r | |
f9cea76b | 289 | UINTN Total;\r |
e1f414b6 | 290 | \r |
291 | ASSERT (Buffer != NULL);\r | |
292 | ASSERT (((UINTN) Buffer & 0x7) == 0);\r | |
293 | ASSERT ((Length & 0x7) == 0);\r | |
294 | ASSERT (Length <= (MAX_ADDRESS - ((UINTN) Buffer) + 1));\r | |
295 | \r | |
f9cea76b | 296 | Total = Length / sizeof (*Buffer);\r |
297 | for (Sum = 0, Count = 0; Count < Total; Count++) {\r | |
e1f414b6 | 298 | Sum = Sum + *(Buffer + Count);\r |
299 | }\r | |
300 | \r | |
301 | return Sum;\r | |
302 | }\r | |
303 | \r | |
304 | \r | |
305 | /**\r | |
306 | Returns the two's complement checksum of all elements in a buffer of \r | |
307 | 64-bit values.\r | |
308 | \r | |
309 | This function first calculates the sum of the 64-bit values in the buffer \r | |
310 | specified by Buffer and Length. The carry bits in the result of addition \r | |
311 | are dropped. Then, the two's complement of the sum is returned. If Length \r | |
312 | is 0, then 0 is returned.\r | |
313 | \r | |
314 | If Buffer is NULL, then ASSERT().\r | |
315 | If Buffer is not aligned on a 64-bit boundary, then ASSERT().\r | |
316 | If Length is not aligned on a 64-bit boundary, then ASSERT().\r | |
317 | If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r | |
318 | \r | |
319 | @param Buffer Pointer to the buffer to carry out the checksum operation.\r | |
320 | @param Length The size, in bytes, of Buffer.\r | |
321 | \r | |
322 | @return Checksum The 2's complement checksum of Buffer.\r | |
323 | \r | |
324 | **/\r | |
325 | UINT64\r | |
326 | EFIAPI\r | |
327 | CalculateCheckSum64 (\r | |
328 | IN CONST UINT64 *Buffer,\r | |
329 | IN UINTN Length\r | |
330 | )\r | |
331 | {\r | |
332 | UINT64 CheckSum;\r | |
333 | \r | |
334 | CheckSum = CalculateSum64 (Buffer, Length);\r | |
335 | \r | |
336 | //\r | |
337 | // Return the checksum based on 2's complement.\r | |
338 | //\r | |
339 | return (UINT64) ((UINT64)(-1) - CheckSum + 1);\r | |
340 | }\r | |
341 | \r | |
342 | \r |