]>
Commit | Line | Data |
---|---|---|
1 | /** @file\r | |
2 | Utility functions to generate checksum based on 2's complement\r | |
3 | algorithm.\r | |
4 | \r | |
5 | Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>\r | |
6 | 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 | **/\r | |
15 | \r | |
16 | #include "BaseLibInternals.h"\r | |
17 | \r | |
18 | /**\r | |
19 | Returns 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 The 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 | |
36 | UINT8\r | |
37 | EFIAPI\r | |
38 | CalculateSum8 (\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 = (UINT8) (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 | @param Buffer The pointer to the buffer to carry out the checksum operation.\r | |
70 | @param Length The size, in bytes, of Buffer.\r | |
71 | \r | |
72 | @return Checksum The 2's complement checksum of Buffer.\r | |
73 | \r | |
74 | **/\r | |
75 | UINT8\r | |
76 | EFIAPI\r | |
77 | CalculateCheckSum8 (\r | |
78 | IN CONST UINT8 *Buffer,\r | |
79 | IN UINTN Length\r | |
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 | |
93 | Returns the sum of all elements in a buffer of 16-bit values. During\r | |
94 | calculation, the carry bits are dropped.\r | |
95 | \r | |
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 | |
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 | |
105 | @param Buffer The pointer to the buffer to carry out the sum operation.\r | |
106 | @param Length The size, in bytes, of Buffer.\r | |
107 | \r | |
108 | @return Sum The sum of Buffer with carry bits dropped during additions.\r | |
109 | \r | |
110 | **/\r | |
111 | UINT16\r | |
112 | EFIAPI\r | |
113 | CalculateSum16 (\r | |
114 | IN CONST UINT16 *Buffer,\r | |
115 | IN UINTN Length\r | |
116 | )\r | |
117 | {\r | |
118 | UINT16 Sum;\r | |
119 | UINTN Count;\r | |
120 | UINTN Total;\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 | Total = Length / sizeof (*Buffer);\r | |
128 | for (Sum = 0, Count = 0; Count < Total; Count++) {\r | |
129 | Sum = (UINT16) (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 The 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 | |
156 | UINT16\r | |
157 | EFIAPI\r | |
158 | CalculateCheckSum16 (\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 The 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 | |
193 | UINT32\r | |
194 | EFIAPI\r | |
195 | CalculateSum32 (\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 | UINTN Total;\r | |
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 | |
209 | Total = Length / sizeof (*Buffer);\r | |
210 | for (Sum = 0, Count = 0; Count < Total; Count++) {\r | |
211 | Sum = Sum + *(Buffer + Count);\r | |
212 | }\r | |
213 | \r | |
214 | return Sum;\r | |
215 | }\r | |
216 | \r | |
217 | \r | |
218 | /**\r | |
219 | Returns the two's complement checksum of all elements in a buffer of\r | |
220 | 32-bit values.\r | |
221 | \r | |
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 | |
225 | is 0, then 0 is returned.\r | |
226 | \r | |
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 | |
230 | If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r | |
231 | \r | |
232 | @param Buffer The pointer to the buffer to carry out the checksum operation.\r | |
233 | @param Length The size, in bytes, of Buffer.\r | |
234 | \r | |
235 | @return Checksum The 2's complement checksum of Buffer.\r | |
236 | \r | |
237 | **/\r | |
238 | UINT32\r | |
239 | EFIAPI\r | |
240 | CalculateCheckSum32 (\r | |
241 | IN CONST UINT32 *Buffer,\r | |
242 | IN UINTN Length\r | |
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 | |
257 | Returns the sum of all elements in a buffer of 64-bit values. During\r | |
258 | calculation, the carry bits are dropped.\r | |
259 | \r | |
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 | |
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 | |
269 | @param Buffer The pointer to the buffer to carry out the sum operation.\r | |
270 | @param Length The size, in bytes, of Buffer.\r | |
271 | \r | |
272 | @return Sum The sum of Buffer with carry bits dropped during additions.\r | |
273 | \r | |
274 | **/\r | |
275 | UINT64\r | |
276 | EFIAPI\r | |
277 | CalculateSum64 (\r | |
278 | IN CONST UINT64 *Buffer,\r | |
279 | IN UINTN Length\r | |
280 | )\r | |
281 | {\r | |
282 | UINT64 Sum;\r | |
283 | UINTN Count;\r | |
284 | UINTN Total;\r | |
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 | |
291 | Total = Length / sizeof (*Buffer);\r | |
292 | for (Sum = 0, Count = 0; Count < Total; Count++) {\r | |
293 | Sum = Sum + *(Buffer + Count);\r | |
294 | }\r | |
295 | \r | |
296 | return Sum;\r | |
297 | }\r | |
298 | \r | |
299 | \r | |
300 | /**\r | |
301 | Returns the two's complement checksum of all elements in a buffer of\r | |
302 | 64-bit values.\r | |
303 | \r | |
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 | |
307 | is 0, then 0 is returned.\r | |
308 | \r | |
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 | |
312 | If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r | |
313 | \r | |
314 | @param Buffer The pointer to the buffer to carry out the checksum operation.\r | |
315 | @param Length The size, in bytes, of Buffer.\r | |
316 | \r | |
317 | @return Checksum The 2's complement checksum of Buffer.\r | |
318 | \r | |
319 | **/\r | |
320 | UINT64\r | |
321 | EFIAPI\r | |
322 | CalculateCheckSum64 (\r | |
323 | IN CONST UINT64 *Buffer,\r | |
324 | IN UINTN Length\r | |
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 |