]>
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, 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 | **/\r | |
15 | \r | |
16 | //\r | |
17 | // Include common header file for this module.\r | |
18 | //\r | |
19 | #include <BaseLibInternals.h>\r | |
20 | \r | |
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 | |
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 | |
131 | \r | |
132 | for (Sum = 0, Count = 0; Count < Length; Count++) {\r | |
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 | |
154 | @param Buffer Pointer to the buffer to carry out the checksum operation.\r | |
155 | @param Length The size, in bytes, of Buffer.\r | |
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 | |
191 | @param Buffer Pointer to the buffer to carry out the sum operation.\r | |
192 | @param Length The size, in bytes, of Buffer.\r | |
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 | |
206 | \r | |
207 | ASSERT (Buffer != NULL);\r | |
208 | ASSERT (((UINTN) Buffer & 0x3) == 0);\r | |
209 | ASSERT ((Length & 0x3) == 0);\r | |
210 | ASSERT (Length <= (MAX_ADDRESS - ((UINTN) Buffer) + 1));\r | |
211 | \r | |
212 | \r | |
213 | for (Sum = 0, Count = 0; Count < Length; Count++) {\r | |
214 | Sum = Sum + *(Buffer + Count);\r | |
215 | }\r | |
216 | \r | |
217 | return Sum;\r | |
218 | }\r | |
219 | \r | |
220 | \r | |
221 | /**\r | |
222 | Returns the two's complement checksum of all elements in a buffer of \r | |
223 | 32-bit values.\r | |
224 | \r | |
225 | This function first calculates the sum of the 32-bit values in the buffer \r | |
226 | specified by Buffer and Length. The carry bits in the result of addition \r | |
227 | are dropped. Then, the two's complement of the sum is returned. If Length \r | |
228 | is 0, then 0 is returned.\r | |
229 | \r | |
230 | If Buffer is NULL, then ASSERT().\r | |
231 | If Buffer is not aligned on a 32-bit boundary, then ASSERT().\r | |
232 | If Length is not aligned on a 32-bit boundary, then ASSERT().\r | |
233 | If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r | |
234 | \r | |
235 | @param Buffer Pointer to the buffer to carry out the checksum operation.\r | |
236 | @param Length The size, in bytes, of Buffer.\r | |
237 | \r | |
238 | @return Checksum The 2's complement checksum of Buffer.\r | |
239 | \r | |
240 | **/\r | |
241 | UINT32\r | |
242 | EFIAPI\r | |
243 | CalculateCheckSum32 (\r | |
244 | IN CONST UINT32 *Buffer,\r | |
245 | IN UINTN Length\r | |
246 | )\r | |
247 | {\r | |
248 | UINT32 CheckSum;\r | |
249 | \r | |
250 | CheckSum = CalculateSum32 (Buffer, Length);\r | |
251 | \r | |
252 | //\r | |
253 | // Return the checksum based on 2's complement.\r | |
254 | //\r | |
255 | return (UINT32) ((UINT32)(-1) - CheckSum + 1);\r | |
256 | }\r | |
257 | \r | |
258 | \r | |
259 | /**\r | |
260 | Returns the sum of all elements in a buffer of 64-bit values. During \r | |
261 | calculation, the carry bits are dropped.\r | |
262 | \r | |
263 | This function calculates the sum of the 64-bit values in the buffer \r | |
264 | specified by Buffer and Length. The carry bits in result of addition are dropped. \r | |
265 | The 64-bit result is returned. If Length is 0, then 0 is returned. \r | |
266 | \r | |
267 | If Buffer is NULL, then ASSERT().\r | |
268 | If Buffer is not aligned on a 64-bit boundary, then ASSERT().\r | |
269 | If Length is not aligned on a 64-bit boundary, then ASSERT().\r | |
270 | If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r | |
271 | \r | |
272 | @param Buffer Pointer to the buffer to carry out the sum operation.\r | |
273 | @param Length The size, in bytes, of Buffer.\r | |
274 | \r | |
275 | @return Sum The sum of Buffer with carry bits dropped during additions.\r | |
276 | \r | |
277 | **/\r | |
278 | UINT64\r | |
279 | EFIAPI\r | |
280 | CalculateSum64 (\r | |
281 | IN CONST UINT64 *Buffer,\r | |
282 | IN UINTN Length\r | |
283 | )\r | |
284 | {\r | |
285 | UINT64 Sum;\r | |
286 | UINTN Count;\r | |
287 | \r | |
288 | ASSERT (Buffer != NULL);\r | |
289 | ASSERT (((UINTN) Buffer & 0x7) == 0);\r | |
290 | ASSERT ((Length & 0x7) == 0);\r | |
291 | ASSERT (Length <= (MAX_ADDRESS - ((UINTN) Buffer) + 1));\r | |
292 | \r | |
293 | for (Sum = 0, Count = 0; Count < Length; Count++) {\r | |
294 | Sum = Sum + *(Buffer + Count);\r | |
295 | }\r | |
296 | \r | |
297 | return Sum;\r | |
298 | }\r | |
299 | \r | |
300 | \r | |
301 | /**\r | |
302 | Returns the two's complement checksum of all elements in a buffer of \r | |
303 | 64-bit values.\r | |
304 | \r | |
305 | This function first calculates the sum of the 64-bit values in the buffer \r | |
306 | specified by Buffer and Length. The carry bits in the result of addition \r | |
307 | are dropped. Then, the two's complement of the sum is returned. If Length \r | |
308 | is 0, then 0 is returned.\r | |
309 | \r | |
310 | If Buffer is NULL, then ASSERT().\r | |
311 | If Buffer is not aligned on a 64-bit boundary, then ASSERT().\r | |
312 | If Length is not aligned on a 64-bit boundary, then ASSERT().\r | |
313 | If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r | |
314 | \r | |
315 | @param Buffer Pointer to the buffer to carry out the checksum operation.\r | |
316 | @param Length The size, in bytes, of Buffer.\r | |
317 | \r | |
318 | @return Checksum The 2's complement checksum of Buffer.\r | |
319 | \r | |
320 | **/\r | |
321 | UINT64\r | |
322 | EFIAPI\r | |
323 | CalculateCheckSum64 (\r | |
324 | IN CONST UINT64 *Buffer,\r | |
325 | IN UINTN Length\r | |
326 | )\r | |
327 | {\r | |
328 | UINT64 CheckSum;\r | |
329 | \r | |
330 | CheckSum = CalculateSum64 (Buffer, Length);\r | |
331 | \r | |
332 | //\r | |
333 | // Return the checksum based on 2's complement.\r | |
334 | //\r | |
335 | return (UINT64) ((UINT64)(-1) - CheckSum + 1);\r | |
336 | }\r | |
337 | \r | |
338 | \r |