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