]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseSafeIntLib/SafeIntLib.c
MdePkg: Apply uncrustify changes
[mirror_edk2.git] / MdePkg / Library / BaseSafeIntLib / SafeIntLib.c
CommitLineData
d7a09cb8
SB
1/** @file\r
2 This library provides helper functions to prevent integer overflow during\r
3 type conversion, addition, subtraction, and multiplication.\r
4\r
5 Copyright (c) 2017, Microsoft Corporation\r
6\r
7 All rights reserved.\r
9344f092 8 SPDX-License-Identifier: BSD-2-Clause-Patent\r
d7a09cb8
SB
9\r
10**/\r
11\r
12#include <Base.h>\r
13#include <Library/SafeIntLib.h>\r
dc9b2a57 14#include <Library/BaseLib.h>\r
d7a09cb8 15\r
d7a09cb8
SB
16//\r
17// Magnitude of MIN_INT64 as expressed by a UINT64 number.\r
18//\r
2f88bd3a 19#define MIN_INT64_MAGNITUDE (((UINT64)(- (MIN_INT64 + 1))) + 1)\r
d7a09cb8
SB
20\r
21//\r
22// Conversion functions\r
23//\r
24// There are three reasons for having conversion functions:\r
25//\r
26// 1. We are converting from a signed type to an unsigned type of the same\r
27// size, or vice-versa.\r
28//\r
29// 2. We are converting to a smaller type, and we could therefore possibly\r
30// overflow.\r
31//\r
32// 3. We are converting to a bigger type, and we are signed and the type we are\r
33// converting to is unsigned.\r
34//\r
35\r
36/**\r
37 INT8 -> UINT8 conversion\r
38\r
39 Converts the value specified by Operand to a value specified by Result type\r
40 and stores the converted value into the caller allocated output buffer\r
41 specified by Result. The caller must pass in a Result buffer that is at\r
42 least as large as the Result type.\r
43\r
44 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
45\r
46 If the conversion results in an overflow or an underflow condition, then\r
47 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
48\r
49 @param[in] Operand Operand to be converted to new type\r
50 @param[out] Result Pointer to the result of conversion\r
51\r
52 @retval RETURN_SUCCESS Successful conversion\r
53 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
54 @retval RETURN_INVALID_PARAMETER Result is NULL\r
55**/\r
56RETURN_STATUS\r
57EFIAPI\r
58SafeInt8ToUint8 (\r
59 IN INT8 Operand,\r
60 OUT UINT8 *Result\r
61 )\r
62{\r
63 RETURN_STATUS Status;\r
64\r
65 if (Result == NULL) {\r
66 return RETURN_INVALID_PARAMETER;\r
67 }\r
68\r
69 if (Operand >= 0) {\r
70 *Result = (UINT8)Operand;\r
2f88bd3a 71 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
72 } else {\r
73 *Result = UINT8_ERROR;\r
2f88bd3a 74 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
75 }\r
76\r
77 return Status;\r
78}\r
79\r
80/**\r
81 INT8 -> CHAR8 conversion\r
82\r
83 Converts the value specified by Operand to a value specified by Result type\r
84 and stores the converted value into the caller allocated output buffer\r
85 specified by Result. The caller must pass in a Result buffer that is at\r
86 least as large as the Result type.\r
87\r
88 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
89\r
90 If the conversion results in an overflow or an underflow condition, then\r
91 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
92\r
93 @param[in] Operand Operand to be converted to new type\r
94 @param[out] Result Pointer to the result of conversion\r
95\r
96 @retval RETURN_SUCCESS Successful conversion\r
97 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
98 @retval RETURN_INVALID_PARAMETER Result is NULL\r
99**/\r
100RETURN_STATUS\r
101EFIAPI\r
102SafeInt8ToChar8 (\r
103 IN INT8 Operand,\r
104 OUT CHAR8 *Result\r
105 )\r
106{\r
107 RETURN_STATUS Status;\r
108\r
109 if (Result == NULL) {\r
110 return RETURN_INVALID_PARAMETER;\r
111 }\r
112\r
113 if (Operand >= 0) {\r
114 *Result = (CHAR8)Operand;\r
2f88bd3a 115 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
116 } else {\r
117 *Result = CHAR8_ERROR;\r
2f88bd3a 118 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
119 }\r
120\r
121 return Status;\r
122}\r
123\r
124/**\r
125 INT8 -> UINT16 conversion\r
126\r
127 Converts the value specified by Operand to a value specified by Result type\r
128 and stores the converted value into the caller allocated output buffer\r
129 specified by Result. The caller must pass in a Result buffer that is at\r
130 least as large as the Result type.\r
131\r
132 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
133\r
134 If the conversion results in an overflow or an underflow condition, then\r
135 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
136\r
137 @param[in] Operand Operand to be converted to new type\r
138 @param[out] Result Pointer to the result of conversion\r
139\r
140 @retval RETURN_SUCCESS Successful conversion\r
141 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
142 @retval RETURN_INVALID_PARAMETER Result is NULL\r
143**/\r
144RETURN_STATUS\r
145EFIAPI\r
146SafeInt8ToUint16 (\r
147 IN INT8 Operand,\r
148 OUT UINT16 *Result\r
149 )\r
150{\r
151 RETURN_STATUS Status;\r
152\r
153 if (Result == NULL) {\r
154 return RETURN_INVALID_PARAMETER;\r
155 }\r
156\r
157 if (Operand >= 0) {\r
158 *Result = (UINT16)Operand;\r
2f88bd3a 159 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
160 } else {\r
161 *Result = UINT16_ERROR;\r
2f88bd3a 162 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
163 }\r
164\r
165 return Status;\r
166}\r
167\r
168/**\r
169 INT8 -> UINT32 conversion\r
170\r
171 Converts the value specified by Operand to a value specified by Result type\r
172 and stores the converted value into the caller allocated output buffer\r
173 specified by Result. The caller must pass in a Result buffer that is at\r
174 least as large as the Result type.\r
175\r
176 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
177\r
178 If the conversion results in an overflow or an underflow condition, then\r
179 Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
180\r
181 @param[in] Operand Operand to be converted to new type\r
182 @param[out] Result Pointer to the result of conversion\r
183\r
184 @retval RETURN_SUCCESS Successful conversion\r
185 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
186 @retval RETURN_INVALID_PARAMETER Result is NULL\r
187**/\r
188RETURN_STATUS\r
189EFIAPI\r
190SafeInt8ToUint32 (\r
191 IN INT8 Operand,\r
192 OUT UINT32 *Result\r
193 )\r
194{\r
195 RETURN_STATUS Status;\r
196\r
197 if (Result == NULL) {\r
198 return RETURN_INVALID_PARAMETER;\r
199 }\r
200\r
201 if (Operand >= 0) {\r
202 *Result = (UINT32)Operand;\r
2f88bd3a 203 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
204 } else {\r
205 *Result = UINT32_ERROR;\r
2f88bd3a 206 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
207 }\r
208\r
209 return Status;\r
210}\r
211\r
212/**\r
213 INT8 -> UINTN conversion\r
214\r
215 Converts the value specified by Operand to a value specified by Result type\r
216 and stores the converted value into the caller allocated output buffer\r
217 specified by Result. The caller must pass in a Result buffer that is at\r
218 least as large as the Result type.\r
219\r
220 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
221\r
222 If the conversion results in an overflow or an underflow condition, then\r
223 Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
224\r
225 @param[in] Operand Operand to be converted to new type\r
226 @param[out] Result Pointer to the result of conversion\r
227\r
228 @retval RETURN_SUCCESS Successful conversion\r
229 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
230 @retval RETURN_INVALID_PARAMETER Result is NULL\r
231**/\r
232RETURN_STATUS\r
233EFIAPI\r
234SafeInt8ToUintn (\r
235 IN INT8 Operand,\r
236 OUT UINTN *Result\r
237 )\r
238{\r
239 RETURN_STATUS Status;\r
240\r
241 if (Result == NULL) {\r
242 return RETURN_INVALID_PARAMETER;\r
243 }\r
244\r
245 if (Operand >= 0) {\r
246 *Result = (UINTN)Operand;\r
2f88bd3a 247 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
248 } else {\r
249 *Result = UINTN_ERROR;\r
2f88bd3a 250 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
251 }\r
252\r
253 return Status;\r
254}\r
255\r
256/**\r
257 INT8 -> UINT64 conversion\r
258\r
259 Converts the value specified by Operand to a value specified by Result type\r
260 and stores the converted value into the caller allocated output buffer\r
261 specified by Result. The caller must pass in a Result buffer that is at\r
262 least as large as the Result type.\r
263\r
264 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
265\r
266 If the conversion results in an overflow or an underflow condition, then\r
267 Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
268\r
269 @param[in] Operand Operand to be converted to new type\r
270 @param[out] Result Pointer to the result of conversion\r
271\r
272 @retval RETURN_SUCCESS Successful conversion\r
273 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
274 @retval RETURN_INVALID_PARAMETER Result is NULL\r
275**/\r
276RETURN_STATUS\r
277EFIAPI\r
278SafeInt8ToUint64 (\r
279 IN INT8 Operand,\r
280 OUT UINT64 *Result\r
281 )\r
282{\r
283 RETURN_STATUS Status;\r
284\r
285 if (Result == NULL) {\r
286 return RETURN_INVALID_PARAMETER;\r
287 }\r
288\r
289 if (Operand >= 0) {\r
290 *Result = (UINT64)Operand;\r
2f88bd3a 291 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
292 } else {\r
293 *Result = UINT64_ERROR;\r
2f88bd3a 294 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
295 }\r
296\r
297 return Status;\r
298}\r
299\r
300/**\r
301 UINT8 -> INT8 conversion\r
302\r
303 Converts the value specified by Operand to a value specified by Result type\r
304 and stores the converted value into the caller allocated output buffer\r
305 specified by Result. The caller must pass in a Result buffer that is at\r
306 least as large as the Result type.\r
307\r
308 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
309\r
310 If the conversion results in an overflow or an underflow condition, then\r
311 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
312\r
313 @param[in] Operand Operand to be converted to new type\r
314 @param[out] Result Pointer to the result of conversion\r
315\r
316 @retval RETURN_SUCCESS Successful conversion\r
317 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
318 @retval RETURN_INVALID_PARAMETER Result is NULL\r
319**/\r
320RETURN_STATUS\r
321EFIAPI\r
322SafeUint8ToInt8 (\r
323 IN UINT8 Operand,\r
324 OUT INT8 *Result\r
325 )\r
326{\r
327 RETURN_STATUS Status;\r
328\r
329 if (Result == NULL) {\r
330 return RETURN_INVALID_PARAMETER;\r
331 }\r
332\r
333 if (Operand <= MAX_INT8) {\r
334 *Result = (INT8)Operand;\r
2f88bd3a 335 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
336 } else {\r
337 *Result = INT8_ERROR;\r
2f88bd3a 338 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
339 }\r
340\r
341 return Status;\r
342}\r
343\r
344/**\r
345 UINT8 -> CHAR8 conversion\r
346\r
347 Converts the value specified by Operand to a value specified by Result type\r
348 and stores the converted value into the caller allocated output buffer\r
349 specified by Result. The caller must pass in a Result buffer that is at\r
350 least as large as the Result type.\r
351\r
352 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
353\r
354 If the conversion results in an overflow or an underflow condition, then\r
355 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
356\r
357 @param[in] Operand Operand to be converted to new type\r
358 @param[out] Result Pointer to the result of conversion\r
359\r
360 @retval RETURN_SUCCESS Successful conversion\r
361 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
362 @retval RETURN_INVALID_PARAMETER Result is NULL\r
363**/\r
364RETURN_STATUS\r
365EFIAPI\r
366SafeUint8ToChar8 (\r
367 IN UINT8 Operand,\r
368 OUT CHAR8 *Result\r
369 )\r
370{\r
371 RETURN_STATUS Status;\r
372\r
373 if (Result == NULL) {\r
374 return RETURN_INVALID_PARAMETER;\r
375 }\r
376\r
377 if (Operand <= MAX_INT8) {\r
378 *Result = (CHAR8)Operand;\r
2f88bd3a 379 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
380 } else {\r
381 *Result = CHAR8_ERROR;\r
2f88bd3a 382 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
383 }\r
384\r
385 return Status;\r
386}\r
387\r
388/**\r
389 INT16 -> INT8 conversion\r
390\r
391 Converts the value specified by Operand to a value specified by Result type\r
392 and stores the converted value into the caller allocated output buffer\r
393 specified by Result. The caller must pass in a Result buffer that is at\r
394 least as large as the Result type.\r
395\r
396 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
397\r
398 If the conversion results in an overflow or an underflow condition, then\r
399 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
400\r
401 @param[in] Operand Operand to be converted to new type\r
402 @param[out] Result Pointer to the result of conversion\r
403\r
404 @retval RETURN_SUCCESS Successful conversion\r
405 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
406 @retval RETURN_INVALID_PARAMETER Result is NULL\r
407**/\r
408RETURN_STATUS\r
409EFIAPI\r
410SafeInt16ToInt8 (\r
411 IN INT16 Operand,\r
412 OUT INT8 *Result\r
413 )\r
414{\r
415 RETURN_STATUS Status;\r
416\r
417 if (Result == NULL) {\r
418 return RETURN_INVALID_PARAMETER;\r
419 }\r
420\r
421 if ((Operand >= MIN_INT8) && (Operand <= MAX_INT8)) {\r
422 *Result = (INT8)Operand;\r
2f88bd3a 423 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
424 } else {\r
425 *Result = INT8_ERROR;\r
2f88bd3a 426 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
427 }\r
428\r
429 return Status;\r
430}\r
431\r
432/**\r
433 INT16 -> CHAR8 conversion\r
434\r
435 Converts the value specified by Operand to a value specified by Result type\r
436 and stores the converted value into the caller allocated output buffer\r
437 specified by Result. The caller must pass in a Result buffer that is at\r
438 least as large as the Result type.\r
439\r
440 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
441\r
442 If the conversion results in an overflow or an underflow condition, then\r
443 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
444\r
445 @param[in] Operand Operand to be converted to new type\r
446 @param[out] Result Pointer to the result of conversion\r
447\r
448 @retval RETURN_SUCCESS Successful conversion\r
449 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
450 @retval RETURN_INVALID_PARAMETER Result is NULL\r
451**/\r
452RETURN_STATUS\r
453EFIAPI\r
454SafeInt16ToChar8 (\r
455 IN INT16 Operand,\r
456 OUT CHAR8 *Result\r
457 )\r
458{\r
459 RETURN_STATUS Status;\r
460\r
461 if (Result == NULL) {\r
462 return RETURN_INVALID_PARAMETER;\r
463 }\r
464\r
465 if ((Operand >= 0) && (Operand <= MAX_INT8)) {\r
466 *Result = (CHAR8)Operand;\r
2f88bd3a 467 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
468 } else {\r
469 *Result = CHAR8_ERROR;\r
2f88bd3a 470 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
471 }\r
472\r
473 return Status;\r
474}\r
475\r
476/**\r
477 INT16 -> UINT8 conversion\r
478\r
479 Converts the value specified by Operand to a value specified by Result type\r
480 and stores the converted value into the caller allocated output buffer\r
481 specified by Result. The caller must pass in a Result buffer that is at\r
482 least as large as the Result type.\r
483\r
484 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
485\r
486 If the conversion results in an overflow or an underflow condition, then\r
487 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
488\r
489 @param[in] Operand Operand to be converted to new type\r
490 @param[out] Result Pointer to the result of conversion\r
491\r
492 @retval RETURN_SUCCESS Successful conversion\r
493 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
494 @retval RETURN_INVALID_PARAMETER Result is NULL\r
495**/\r
496RETURN_STATUS\r
497EFIAPI\r
498SafeInt16ToUint8 (\r
499 IN INT16 Operand,\r
500 OUT UINT8 *Result\r
501 )\r
502{\r
503 RETURN_STATUS Status;\r
504\r
505 if (Result == NULL) {\r
506 return RETURN_INVALID_PARAMETER;\r
507 }\r
508\r
509 if ((Operand >= 0) && (Operand <= MAX_UINT8)) {\r
510 *Result = (UINT8)Operand;\r
2f88bd3a 511 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
512 } else {\r
513 *Result = UINT8_ERROR;\r
2f88bd3a 514 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
515 }\r
516\r
517 return Status;\r
518}\r
519\r
520/**\r
521 INT16 -> UINT16 conversion\r
522\r
523 Converts the value specified by Operand to a value specified by Result type\r
524 and stores the converted value into the caller allocated output buffer\r
525 specified by Result. The caller must pass in a Result buffer that is at\r
526 least as large as the Result type.\r
527\r
528 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
529\r
530 If the conversion results in an overflow or an underflow condition, then\r
531 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
532\r
533 @param[in] Operand Operand to be converted to new type\r
534 @param[out] Result Pointer to the result of conversion\r
535\r
536 @retval RETURN_SUCCESS Successful conversion\r
537 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
538 @retval RETURN_INVALID_PARAMETER Result is NULL\r
539**/\r
540RETURN_STATUS\r
541EFIAPI\r
542SafeInt16ToUint16 (\r
543 IN INT16 Operand,\r
544 OUT UINT16 *Result\r
545 )\r
546{\r
547 RETURN_STATUS Status;\r
548\r
549 if (Result == NULL) {\r
550 return RETURN_INVALID_PARAMETER;\r
551 }\r
552\r
553 if (Operand >= 0) {\r
554 *Result = (UINT16)Operand;\r
2f88bd3a 555 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
556 } else {\r
557 *Result = UINT16_ERROR;\r
2f88bd3a 558 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
559 }\r
560\r
561 return Status;\r
562}\r
563\r
564/**\r
565 INT16 -> UINT32 conversion\r
566\r
567 Converts the value specified by Operand to a value specified by Result type\r
568 and stores the converted value into the caller allocated output buffer\r
569 specified by Result. The caller must pass in a Result buffer that is at\r
570 least as large as the Result type.\r
571\r
572 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
573\r
574 If the conversion results in an overflow or an underflow condition, then\r
575 Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
576\r
577 @param[in] Operand Operand to be converted to new type\r
578 @param[out] Result Pointer to the result of conversion\r
579\r
580 @retval RETURN_SUCCESS Successful conversion\r
581 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
582 @retval RETURN_INVALID_PARAMETER Result is NULL\r
583**/\r
584RETURN_STATUS\r
585EFIAPI\r
586SafeInt16ToUint32 (\r
587 IN INT16 Operand,\r
588 OUT UINT32 *Result\r
589 )\r
590{\r
591 RETURN_STATUS Status;\r
592\r
593 if (Result == NULL) {\r
594 return RETURN_INVALID_PARAMETER;\r
595 }\r
596\r
597 if (Operand >= 0) {\r
598 *Result = (UINT32)Operand;\r
2f88bd3a 599 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
600 } else {\r
601 *Result = UINT32_ERROR;\r
2f88bd3a 602 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
603 }\r
604\r
605 return Status;\r
606}\r
607\r
608/**\r
609 INT16 -> UINTN conversion\r
610\r
611 Converts the value specified by Operand to a value specified by Result type\r
612 and stores the converted value into the caller allocated output buffer\r
613 specified by Result. The caller must pass in a Result buffer that is at\r
614 least as large as the Result type.\r
615\r
616 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
617\r
618 If the conversion results in an overflow or an underflow condition, then\r
619 Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
620\r
621 @param[in] Operand Operand to be converted to new type\r
622 @param[out] Result Pointer to the result of conversion\r
623\r
624 @retval RETURN_SUCCESS Successful conversion\r
625 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
626 @retval RETURN_INVALID_PARAMETER Result is NULL\r
627**/\r
628RETURN_STATUS\r
629EFIAPI\r
630SafeInt16ToUintn (\r
631 IN INT16 Operand,\r
632 OUT UINTN *Result\r
633 )\r
634{\r
635 RETURN_STATUS Status;\r
636\r
637 if (Result == NULL) {\r
638 return RETURN_INVALID_PARAMETER;\r
639 }\r
640\r
641 if (Operand >= 0) {\r
642 *Result = (UINTN)Operand;\r
2f88bd3a 643 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
644 } else {\r
645 *Result = UINTN_ERROR;\r
2f88bd3a 646 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
647 }\r
648\r
649 return Status;\r
650}\r
651\r
652/**\r
653 INT16 -> UINT64 conversion\r
654\r
655 Converts the value specified by Operand to a value specified by Result type\r
656 and stores the converted value into the caller allocated output buffer\r
657 specified by Result. The caller must pass in a Result buffer that is at\r
658 least as large as the Result type.\r
659\r
660 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
661\r
662 If the conversion results in an overflow or an underflow condition, then\r
663 Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
664\r
665 @param[in] Operand Operand to be converted to new type\r
666 @param[out] Result Pointer to the result of conversion\r
667\r
668 @retval RETURN_SUCCESS Successful conversion\r
669 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
670 @retval RETURN_INVALID_PARAMETER Result is NULL\r
671**/\r
672RETURN_STATUS\r
673EFIAPI\r
674SafeInt16ToUint64 (\r
675 IN INT16 Operand,\r
676 OUT UINT64 *Result\r
677 )\r
678{\r
679 RETURN_STATUS Status;\r
680\r
681 if (Result == NULL) {\r
682 return RETURN_INVALID_PARAMETER;\r
683 }\r
684\r
685 if (Operand >= 0) {\r
686 *Result = (UINT64)Operand;\r
2f88bd3a 687 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
688 } else {\r
689 *Result = UINT64_ERROR;\r
2f88bd3a 690 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
691 }\r
692\r
693 return Status;\r
694}\r
695\r
696/**\r
697 UINT16 -> INT8 conversion\r
698\r
699 Converts the value specified by Operand to a value specified by Result type\r
700 and stores the converted value into the caller allocated output buffer\r
701 specified by Result. The caller must pass in a Result buffer that is at\r
702 least as large as the Result type.\r
703\r
704 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
705\r
706 If the conversion results in an overflow or an underflow condition, then\r
707 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
708\r
709 @param[in] Operand Operand to be converted to new type\r
710 @param[out] Result Pointer to the result of conversion\r
711\r
712 @retval RETURN_SUCCESS Successful conversion\r
713 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
714 @retval RETURN_INVALID_PARAMETER Result is NULL\r
715**/\r
716RETURN_STATUS\r
717EFIAPI\r
718SafeUint16ToInt8 (\r
719 IN UINT16 Operand,\r
720 OUT INT8 *Result\r
721 )\r
722{\r
723 RETURN_STATUS Status;\r
724\r
725 if (Result == NULL) {\r
726 return RETURN_INVALID_PARAMETER;\r
727 }\r
728\r
729 if (Operand <= MAX_INT8) {\r
730 *Result = (INT8)Operand;\r
2f88bd3a 731 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
732 } else {\r
733 *Result = INT8_ERROR;\r
2f88bd3a 734 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
735 }\r
736\r
737 return Status;\r
738}\r
739\r
740/**\r
741 UINT16 -> CHAR8 conversion\r
742\r
743 Converts the value specified by Operand to a value specified by Result type\r
744 and stores the converted value into the caller allocated output buffer\r
745 specified by Result. The caller must pass in a Result buffer that is at\r
746 least as large as the Result type.\r
747\r
748 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
749\r
750 If the conversion results in an overflow or an underflow condition, then\r
751 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
752\r
753 @param[in] Operand Operand to be converted to new type\r
754 @param[out] Result Pointer to the result of conversion\r
755\r
756 @retval RETURN_SUCCESS Successful conversion\r
757 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
758 @retval RETURN_INVALID_PARAMETER Result is NULL\r
759**/\r
760RETURN_STATUS\r
761EFIAPI\r
762SafeUint16ToChar8 (\r
763 IN UINT16 Operand,\r
764 OUT CHAR8 *Result\r
765 )\r
766{\r
767 RETURN_STATUS Status;\r
768\r
769 if (Result == NULL) {\r
770 return RETURN_INVALID_PARAMETER;\r
771 }\r
772\r
773 if (Operand <= MAX_INT8) {\r
774 *Result = (INT8)Operand;\r
2f88bd3a 775 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
776 } else {\r
777 *Result = CHAR8_ERROR;\r
2f88bd3a 778 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
779 }\r
780\r
781 return Status;\r
782}\r
783\r
784/**\r
785 UINT16 -> UINT8 conversion\r
786\r
787 Converts the value specified by Operand to a value specified by Result type\r
788 and stores the converted value into the caller allocated output buffer\r
789 specified by Result. The caller must pass in a Result buffer that is at\r
790 least as large as the Result type.\r
791\r
792 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
793\r
794 If the conversion results in an overflow or an underflow condition, then\r
795 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
796\r
797 @param[in] Operand Operand to be converted to new type\r
798 @param[out] Result Pointer to the result of conversion\r
799\r
800 @retval RETURN_SUCCESS Successful conversion\r
801 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
802 @retval RETURN_INVALID_PARAMETER Result is NULL\r
803**/\r
804RETURN_STATUS\r
805EFIAPI\r
806SafeUint16ToUint8 (\r
807 IN UINT16 Operand,\r
808 OUT UINT8 *Result\r
809 )\r
810{\r
811 RETURN_STATUS Status;\r
812\r
813 if (Result == NULL) {\r
814 return RETURN_INVALID_PARAMETER;\r
815 }\r
816\r
817 if (Operand <= MAX_UINT8) {\r
818 *Result = (UINT8)Operand;\r
2f88bd3a 819 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
820 } else {\r
821 *Result = UINT8_ERROR;\r
2f88bd3a 822 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
823 }\r
824\r
825 return Status;\r
826}\r
827\r
828/**\r
829 UINT16 -> INT16 conversion\r
830\r
831 Converts the value specified by Operand to a value specified by Result type\r
832 and stores the converted value into the caller allocated output buffer\r
833 specified by Result. The caller must pass in a Result buffer that is at\r
834 least as large as the Result type.\r
835\r
836 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
837\r
838 If the conversion results in an overflow or an underflow condition, then\r
839 Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
840\r
841 @param[in] Operand Operand to be converted to new type\r
842 @param[out] Result Pointer to the result of conversion\r
843\r
844 @retval RETURN_SUCCESS Successful conversion\r
845 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
846 @retval RETURN_INVALID_PARAMETER Result is NULL\r
847**/\r
848RETURN_STATUS\r
849EFIAPI\r
850SafeUint16ToInt16 (\r
851 IN UINT16 Operand,\r
852 OUT INT16 *Result\r
853 )\r
854{\r
855 RETURN_STATUS Status;\r
856\r
857 if (Result == NULL) {\r
858 return RETURN_INVALID_PARAMETER;\r
859 }\r
860\r
861 if (Operand <= MAX_INT16) {\r
862 *Result = (INT16)Operand;\r
2f88bd3a 863 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
864 } else {\r
865 *Result = INT16_ERROR;\r
2f88bd3a 866 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
867 }\r
868\r
869 return Status;\r
870}\r
871\r
872/**\r
873 INT32 -> INT8 conversion\r
874\r
875 Converts the value specified by Operand to a value specified by Result type\r
876 and stores the converted value into the caller allocated output buffer\r
877 specified by Result. The caller must pass in a Result buffer that is at\r
878 least as large as the Result type.\r
879\r
880 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
881\r
882 If the conversion results in an overflow or an underflow condition, then\r
883 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
884\r
885 @param[in] Operand Operand to be converted to new type\r
886 @param[out] Result Pointer to the result of conversion\r
887\r
888 @retval RETURN_SUCCESS Successful conversion\r
889 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
890 @retval RETURN_INVALID_PARAMETER Result is NULL\r
891**/\r
892RETURN_STATUS\r
893EFIAPI\r
894SafeInt32ToInt8 (\r
895 IN INT32 Operand,\r
896 OUT INT8 *Result\r
897 )\r
898{\r
899 RETURN_STATUS Status;\r
900\r
901 if (Result == NULL) {\r
902 return RETURN_INVALID_PARAMETER;\r
903 }\r
904\r
905 if ((Operand >= MIN_INT8) && (Operand <= MAX_INT8)) {\r
906 *Result = (INT8)Operand;\r
2f88bd3a 907 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
908 } else {\r
909 *Result = INT8_ERROR;\r
2f88bd3a 910 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
911 }\r
912\r
913 return Status;\r
914}\r
915\r
916/**\r
917 INT32 -> CHAR8 conversion\r
918\r
919 Converts the value specified by Operand to a value specified by Result type\r
920 and stores the converted value into the caller allocated output buffer\r
921 specified by Result. The caller must pass in a Result buffer that is at\r
922 least as large as the Result type.\r
923\r
924 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
925\r
926 If the conversion results in an overflow or an underflow condition, then\r
927 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
928\r
929 @param[in] Operand Operand to be converted to new type\r
930 @param[out] Result Pointer to the result of conversion\r
931\r
932 @retval RETURN_SUCCESS Successful conversion\r
933 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
934 @retval RETURN_INVALID_PARAMETER Result is NULL\r
935**/\r
936RETURN_STATUS\r
937EFIAPI\r
938SafeInt32ToChar8 (\r
939 IN INT32 Operand,\r
940 OUT CHAR8 *Result\r
941 )\r
942{\r
943 RETURN_STATUS Status;\r
944\r
945 if (Result == NULL) {\r
946 return RETURN_INVALID_PARAMETER;\r
947 }\r
948\r
949 if ((Operand >= 0) && (Operand <= MAX_INT8)) {\r
950 *Result = (CHAR8)Operand;\r
2f88bd3a 951 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
952 } else {\r
953 *Result = CHAR8_ERROR;\r
2f88bd3a 954 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
955 }\r
956\r
957 return Status;\r
958}\r
959\r
960/**\r
961 INT32 -> UINT8 conversion\r
962\r
963 Converts the value specified by Operand to a value specified by Result type\r
964 and stores the converted value into the caller allocated output buffer\r
965 specified by Result. The caller must pass in a Result buffer that is at\r
966 least as large as the Result type.\r
967\r
968 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
969\r
970 If the conversion results in an overflow or an underflow condition, then\r
971 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
972\r
973 @param[in] Operand Operand to be converted to new type\r
974 @param[out] Result Pointer to the result of conversion\r
975\r
976 @retval RETURN_SUCCESS Successful conversion\r
977 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
978 @retval RETURN_INVALID_PARAMETER Result is NULL\r
979**/\r
980RETURN_STATUS\r
981EFIAPI\r
982SafeInt32ToUint8 (\r
983 IN INT32 Operand,\r
984 OUT UINT8 *Result\r
985 )\r
986{\r
987 RETURN_STATUS Status;\r
988\r
989 if (Result == NULL) {\r
990 return RETURN_INVALID_PARAMETER;\r
991 }\r
992\r
993 if ((Operand >= 0) && (Operand <= MAX_UINT8)) {\r
994 *Result = (UINT8)Operand;\r
2f88bd3a 995 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
996 } else {\r
997 *Result = UINT8_ERROR;\r
2f88bd3a 998 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
999 }\r
1000\r
1001 return Status;\r
1002}\r
1003\r
1004/**\r
1005 INT32 -> INT16 conversion\r
1006\r
1007 Converts the value specified by Operand to a value specified by Result type\r
1008 and stores the converted value into the caller allocated output buffer\r
1009 specified by Result. The caller must pass in a Result buffer that is at\r
1010 least as large as the Result type.\r
1011\r
1012 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1013\r
1014 If the conversion results in an overflow or an underflow condition, then\r
1015 Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1016\r
1017 @param[in] Operand Operand to be converted to new type\r
1018 @param[out] Result Pointer to the result of conversion\r
1019\r
1020 @retval RETURN_SUCCESS Successful conversion\r
1021 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1022 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1023**/\r
1024RETURN_STATUS\r
1025EFIAPI\r
1026SafeInt32ToInt16 (\r
1027 IN INT32 Operand,\r
1028 OUT INT16 *Result\r
1029 )\r
1030{\r
1031 RETURN_STATUS Status;\r
1032\r
1033 if (Result == NULL) {\r
1034 return RETURN_INVALID_PARAMETER;\r
1035 }\r
1036\r
1037 if ((Operand >= MIN_INT16) && (Operand <= MAX_INT16)) {\r
1038 *Result = (INT16)Operand;\r
2f88bd3a 1039 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
1040 } else {\r
1041 *Result = INT16_ERROR;\r
2f88bd3a 1042 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
1043 }\r
1044\r
1045 return Status;\r
1046}\r
1047\r
1048/**\r
1049 INT32 -> UINT16 conversion\r
1050\r
1051 Converts the value specified by Operand to a value specified by Result type\r
1052 and stores the converted value into the caller allocated output buffer\r
1053 specified by Result. The caller must pass in a Result buffer that is at\r
1054 least as large as the Result type.\r
1055\r
1056 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1057\r
1058 If the conversion results in an overflow or an underflow condition, then\r
1059 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1060\r
1061 @param[in] Operand Operand to be converted to new type\r
1062 @param[out] Result Pointer to the result of conversion\r
1063\r
1064 @retval RETURN_SUCCESS Successful conversion\r
1065 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1066 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1067**/\r
1068RETURN_STATUS\r
1069EFIAPI\r
1070SafeInt32ToUint16 (\r
1071 IN INT32 Operand,\r
1072 OUT UINT16 *Result\r
1073 )\r
1074{\r
1075 RETURN_STATUS Status;\r
1076\r
1077 if (Result == NULL) {\r
1078 return RETURN_INVALID_PARAMETER;\r
1079 }\r
1080\r
1081 if ((Operand >= 0) && (Operand <= MAX_UINT16)) {\r
1082 *Result = (UINT16)Operand;\r
2f88bd3a 1083 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
1084 } else {\r
1085 *Result = UINT16_ERROR;\r
2f88bd3a 1086 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
1087 }\r
1088\r
1089 return Status;\r
1090}\r
1091\r
1092/**\r
1093 INT32 -> UINT32 conversion\r
1094\r
1095 Converts the value specified by Operand to a value specified by Result type\r
1096 and stores the converted value into the caller allocated output buffer\r
1097 specified by Result. The caller must pass in a Result buffer that is at\r
1098 least as large as the Result type.\r
1099\r
1100 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1101\r
1102 If the conversion results in an overflow or an underflow condition, then\r
1103 Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1104\r
1105 @param[in] Operand Operand to be converted to new type\r
1106 @param[out] Result Pointer to the result of conversion\r
1107\r
1108 @retval RETURN_SUCCESS Successful conversion\r
1109 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1110 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1111**/\r
1112RETURN_STATUS\r
1113EFIAPI\r
1114SafeInt32ToUint32 (\r
1115 IN INT32 Operand,\r
1116 OUT UINT32 *Result\r
1117 )\r
1118{\r
1119 RETURN_STATUS Status;\r
1120\r
1121 if (Result == NULL) {\r
1122 return RETURN_INVALID_PARAMETER;\r
1123 }\r
1124\r
1125 if (Operand >= 0) {\r
1126 *Result = (UINT32)Operand;\r
2f88bd3a 1127 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
1128 } else {\r
1129 *Result = UINT32_ERROR;\r
2f88bd3a 1130 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
1131 }\r
1132\r
1133 return Status;\r
1134}\r
1135\r
1136/**\r
1137 INT32 -> UINT64 conversion\r
1138\r
1139 Converts the value specified by Operand to a value specified by Result type\r
1140 and stores the converted value into the caller allocated output buffer\r
1141 specified by Result. The caller must pass in a Result buffer that is at\r
1142 least as large as the Result type.\r
1143\r
1144 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1145\r
1146 If the conversion results in an overflow or an underflow condition, then\r
1147 Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1148\r
1149 @param[in] Operand Operand to be converted to new type\r
1150 @param[out] Result Pointer to the result of conversion\r
1151\r
1152 @retval RETURN_SUCCESS Successful conversion\r
1153 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1154 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1155**/\r
1156RETURN_STATUS\r
1157EFIAPI\r
1158SafeInt32ToUint64 (\r
1159 IN INT32 Operand,\r
1160 OUT UINT64 *Result\r
1161 )\r
1162{\r
1163 RETURN_STATUS Status;\r
1164\r
1165 if (Result == NULL) {\r
1166 return RETURN_INVALID_PARAMETER;\r
1167 }\r
1168\r
1169 if (Operand >= 0) {\r
1170 *Result = (UINT64)Operand;\r
2f88bd3a 1171 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
1172 } else {\r
1173 *Result = UINT64_ERROR;\r
2f88bd3a 1174 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
1175 }\r
1176\r
1177 return Status;\r
1178}\r
1179\r
1180/**\r
1181 UINT32 -> INT8 conversion\r
1182\r
1183 Converts the value specified by Operand to a value specified by Result type\r
1184 and stores the converted value into the caller allocated output buffer\r
1185 specified by Result. The caller must pass in a Result buffer that is at\r
1186 least as large as the Result type.\r
1187\r
1188 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1189\r
1190 If the conversion results in an overflow or an underflow condition, then\r
1191 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1192\r
1193 @param[in] Operand Operand to be converted to new type\r
1194 @param[out] Result Pointer to the result of conversion\r
1195\r
1196 @retval RETURN_SUCCESS Successful conversion\r
1197 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1198 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1199**/\r
1200RETURN_STATUS\r
1201EFIAPI\r
1202SafeUint32ToInt8 (\r
1203 IN UINT32 Operand,\r
1204 OUT INT8 *Result\r
1205 )\r
1206{\r
1207 RETURN_STATUS Status;\r
1208\r
1209 if (Result == NULL) {\r
1210 return RETURN_INVALID_PARAMETER;\r
1211 }\r
1212\r
1213 if (Operand <= MAX_INT8) {\r
1214 *Result = (INT8)Operand;\r
2f88bd3a 1215 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
1216 } else {\r
1217 *Result = INT8_ERROR;\r
2f88bd3a 1218 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
1219 }\r
1220\r
1221 return Status;\r
1222}\r
1223\r
1224/**\r
1225 UINT32 -> CHAR8 conversion\r
1226\r
1227 Converts the value specified by Operand to a value specified by Result type\r
1228 and stores the converted value into the caller allocated output buffer\r
1229 specified by Result. The caller must pass in a Result buffer that is at\r
1230 least as large as the Result type.\r
1231\r
1232 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1233\r
1234 If the conversion results in an overflow or an underflow condition, then\r
1235 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1236\r
1237 @param[in] Operand Operand to be converted to new type\r
1238 @param[out] Result Pointer to the result of conversion\r
1239\r
1240 @retval RETURN_SUCCESS Successful conversion\r
1241 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1242 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1243**/\r
1244RETURN_STATUS\r
1245EFIAPI\r
1246SafeUint32ToChar8 (\r
1247 IN UINT32 Operand,\r
1248 OUT CHAR8 *Result\r
1249 )\r
1250{\r
1251 RETURN_STATUS Status;\r
1252\r
1253 if (Result == NULL) {\r
1254 return RETURN_INVALID_PARAMETER;\r
1255 }\r
1256\r
1257 if (Operand <= MAX_INT8) {\r
1258 *Result = (INT8)Operand;\r
2f88bd3a 1259 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
1260 } else {\r
1261 *Result = CHAR8_ERROR;\r
2f88bd3a 1262 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
1263 }\r
1264\r
1265 return Status;\r
1266}\r
1267\r
1268/**\r
1269 UINT32 -> UINT8 conversion\r
1270\r
1271 Converts the value specified by Operand to a value specified by Result type\r
1272 and stores the converted value into the caller allocated output buffer\r
1273 specified by Result. The caller must pass in a Result buffer that is at\r
1274 least as large as the Result type.\r
1275\r
1276 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1277\r
1278 If the conversion results in an overflow or an underflow condition, then\r
1279 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1280\r
1281 @param[in] Operand Operand to be converted to new type\r
1282 @param[out] Result Pointer to the result of conversion\r
1283\r
1284 @retval RETURN_SUCCESS Successful conversion\r
1285 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1286 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1287**/\r
1288RETURN_STATUS\r
1289EFIAPI\r
1290SafeUint32ToUint8 (\r
1291 IN UINT32 Operand,\r
1292 OUT UINT8 *Result\r
1293 )\r
1294{\r
1295 RETURN_STATUS Status;\r
1296\r
1297 if (Result == NULL) {\r
1298 return RETURN_INVALID_PARAMETER;\r
1299 }\r
1300\r
1301 if (Operand <= MAX_UINT8) {\r
1302 *Result = (UINT8)Operand;\r
2f88bd3a 1303 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
1304 } else {\r
1305 *Result = UINT8_ERROR;\r
2f88bd3a 1306 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
1307 }\r
1308\r
1309 return Status;\r
1310}\r
1311\r
1312/**\r
1313 UINT32 -> INT16 conversion\r
1314\r
1315 Converts the value specified by Operand to a value specified by Result type\r
1316 and stores the converted value into the caller allocated output buffer\r
1317 specified by Result. The caller must pass in a Result buffer that is at\r
1318 least as large as the Result type.\r
1319\r
1320 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1321\r
1322 If the conversion results in an overflow or an underflow condition, then\r
1323 Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1324\r
1325 @param[in] Operand Operand to be converted to new type\r
1326 @param[out] Result Pointer to the result of conversion\r
1327\r
1328 @retval RETURN_SUCCESS Successful conversion\r
1329 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1330 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1331**/\r
1332RETURN_STATUS\r
1333EFIAPI\r
1334SafeUint32ToInt16 (\r
1335 IN UINT32 Operand,\r
1336 OUT INT16 *Result\r
1337 )\r
1338{\r
1339 RETURN_STATUS Status;\r
1340\r
1341 if (Result == NULL) {\r
1342 return RETURN_INVALID_PARAMETER;\r
1343 }\r
1344\r
1345 if (Operand <= MAX_INT16) {\r
1346 *Result = (INT16)Operand;\r
2f88bd3a 1347 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
1348 } else {\r
1349 *Result = INT16_ERROR;\r
2f88bd3a 1350 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
1351 }\r
1352\r
1353 return Status;\r
1354}\r
1355\r
1356/**\r
1357 UINT32 -> UINT16 conversion\r
1358\r
1359 Converts the value specified by Operand to a value specified by Result type\r
1360 and stores the converted value into the caller allocated output buffer\r
1361 specified by Result. The caller must pass in a Result buffer that is at\r
1362 least as large as the Result type.\r
1363\r
1364 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1365\r
1366 If the conversion results in an overflow or an underflow condition, then\r
1367 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1368\r
1369 @param[in] Operand Operand to be converted to new type\r
1370 @param[out] Result Pointer to the result of conversion\r
1371\r
1372 @retval RETURN_SUCCESS Successful conversion\r
1373 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1374 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1375**/\r
1376RETURN_STATUS\r
1377EFIAPI\r
1378SafeUint32ToUint16 (\r
1379 IN UINT32 Operand,\r
1380 OUT UINT16 *Result\r
1381 )\r
1382{\r
1383 RETURN_STATUS Status;\r
1384\r
1385 if (Result == NULL) {\r
1386 return RETURN_INVALID_PARAMETER;\r
1387 }\r
1388\r
1389 if (Operand <= MAX_UINT16) {\r
1390 *Result = (UINT16)Operand;\r
2f88bd3a 1391 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
1392 } else {\r
1393 *Result = UINT16_ERROR;\r
2f88bd3a 1394 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
1395 }\r
1396\r
1397 return Status;\r
1398}\r
1399\r
1400/**\r
1401 UINT32 -> INT32 conversion\r
1402\r
1403 Converts the value specified by Operand to a value specified by Result type\r
1404 and stores the converted value into the caller allocated output buffer\r
1405 specified by Result. The caller must pass in a Result buffer that is at\r
1406 least as large as the Result type.\r
1407\r
1408 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1409\r
1410 If the conversion results in an overflow or an underflow condition, then\r
1411 Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1412\r
1413 @param[in] Operand Operand to be converted to new type\r
1414 @param[out] Result Pointer to the result of conversion\r
1415\r
1416 @retval RETURN_SUCCESS Successful conversion\r
1417 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1418 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1419**/\r
1420RETURN_STATUS\r
1421EFIAPI\r
1422SafeUint32ToInt32 (\r
1423 IN UINT32 Operand,\r
1424 OUT INT32 *Result\r
1425 )\r
1426{\r
1427 RETURN_STATUS Status;\r
1428\r
1429 if (Result == NULL) {\r
1430 return RETURN_INVALID_PARAMETER;\r
1431 }\r
1432\r
1433 if (Operand <= MAX_INT32) {\r
1434 *Result = (INT32)Operand;\r
2f88bd3a 1435 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
1436 } else {\r
1437 *Result = INT32_ERROR;\r
2f88bd3a 1438 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
1439 }\r
1440\r
1441 return Status;\r
1442}\r
1443\r
1444/**\r
1445 INTN -> INT8 conversion\r
1446\r
1447 Converts the value specified by Operand to a value specified by Result type\r
1448 and stores the converted value into the caller allocated output buffer\r
1449 specified by Result. The caller must pass in a Result buffer that is at\r
1450 least as large as the Result type.\r
1451\r
1452 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1453\r
1454 If the conversion results in an overflow or an underflow condition, then\r
1455 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1456\r
1457 @param[in] Operand Operand to be converted to new type\r
1458 @param[out] Result Pointer to the result of conversion\r
1459\r
1460 @retval RETURN_SUCCESS Successful conversion\r
1461 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1462 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1463**/\r
1464RETURN_STATUS\r
1465EFIAPI\r
1466SafeIntnToInt8 (\r
1467 IN INTN Operand,\r
1468 OUT INT8 *Result\r
1469 )\r
1470{\r
1471 RETURN_STATUS Status;\r
1472\r
1473 if (Result == NULL) {\r
1474 return RETURN_INVALID_PARAMETER;\r
1475 }\r
1476\r
1477 if ((Operand >= MIN_INT8) && (Operand <= MAX_INT8)) {\r
1478 *Result = (INT8)Operand;\r
2f88bd3a 1479 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
1480 } else {\r
1481 *Result = INT8_ERROR;\r
2f88bd3a 1482 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
1483 }\r
1484\r
1485 return Status;\r
1486}\r
1487\r
1488/**\r
1489 INTN -> CHAR8 conversion\r
1490\r
1491 Converts the value specified by Operand to a value specified by Result type\r
1492 and stores the converted value into the caller allocated output buffer\r
1493 specified by Result. The caller must pass in a Result buffer that is at\r
1494 least as large as the Result type.\r
1495\r
1496 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1497\r
1498 If the conversion results in an overflow or an underflow condition, then\r
1499 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1500\r
1501 @param[in] Operand Operand to be converted to new type\r
1502 @param[out] Result Pointer to the result of conversion\r
1503\r
1504 @retval RETURN_SUCCESS Successful conversion\r
1505 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1506 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1507**/\r
1508RETURN_STATUS\r
1509EFIAPI\r
1510SafeIntnToChar8 (\r
1511 IN INTN Operand,\r
1512 OUT CHAR8 *Result\r
1513 )\r
1514{\r
1515 RETURN_STATUS Status;\r
1516\r
1517 if (Result == NULL) {\r
1518 return RETURN_INVALID_PARAMETER;\r
1519 }\r
1520\r
1521 if ((Operand >= 0) && (Operand <= MAX_INT8)) {\r
1522 *Result = (CHAR8)Operand;\r
2f88bd3a 1523 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
1524 } else {\r
1525 *Result = CHAR8_ERROR;\r
2f88bd3a 1526 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
1527 }\r
1528\r
1529 return Status;\r
1530}\r
1531\r
1532/**\r
1533 INTN -> UINT8 conversion\r
1534\r
1535 Converts the value specified by Operand to a value specified by Result type\r
1536 and stores the converted value into the caller allocated output buffer\r
1537 specified by Result. The caller must pass in a Result buffer that is at\r
1538 least as large as the Result type.\r
1539\r
1540 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1541\r
1542 If the conversion results in an overflow or an underflow condition, then\r
1543 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1544\r
1545 @param[in] Operand Operand to be converted to new type\r
1546 @param[out] Result Pointer to the result of conversion\r
1547\r
1548 @retval RETURN_SUCCESS Successful conversion\r
1549 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1550 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1551**/\r
1552RETURN_STATUS\r
1553EFIAPI\r
1554SafeIntnToUint8 (\r
1555 IN INTN Operand,\r
1556 OUT UINT8 *Result\r
1557 )\r
1558{\r
1559 RETURN_STATUS Status;\r
1560\r
1561 if (Result == NULL) {\r
1562 return RETURN_INVALID_PARAMETER;\r
1563 }\r
1564\r
1565 if ((Operand >= 0) && (Operand <= MAX_UINT8)) {\r
1566 *Result = (UINT8)Operand;\r
2f88bd3a 1567 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
1568 } else {\r
1569 *Result = UINT8_ERROR;\r
2f88bd3a 1570 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
1571 }\r
1572\r
1573 return Status;\r
1574}\r
1575\r
1576/**\r
1577 INTN -> INT16 conversion\r
1578\r
1579 Converts the value specified by Operand to a value specified by Result type\r
1580 and stores the converted value into the caller allocated output buffer\r
1581 specified by Result. The caller must pass in a Result buffer that is at\r
1582 least as large as the Result type.\r
1583\r
1584 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1585\r
1586 If the conversion results in an overflow or an underflow condition, then\r
1587 Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1588\r
1589 @param[in] Operand Operand to be converted to new type\r
1590 @param[out] Result Pointer to the result of conversion\r
1591\r
1592 @retval RETURN_SUCCESS Successful conversion\r
1593 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1594 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1595**/\r
1596RETURN_STATUS\r
1597EFIAPI\r
1598SafeIntnToInt16 (\r
1599 IN INTN Operand,\r
1600 OUT INT16 *Result\r
1601 )\r
1602{\r
1603 RETURN_STATUS Status;\r
1604\r
1605 if (Result == NULL) {\r
1606 return RETURN_INVALID_PARAMETER;\r
1607 }\r
1608\r
1609 if ((Operand >= MIN_INT16) && (Operand <= MAX_INT16)) {\r
1610 *Result = (INT16)Operand;\r
2f88bd3a 1611 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
1612 } else {\r
1613 *Result = INT16_ERROR;\r
2f88bd3a 1614 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
1615 }\r
1616\r
1617 return Status;\r
1618}\r
1619\r
1620/**\r
1621 INTN -> UINT16 conversion\r
1622\r
1623 Converts the value specified by Operand to a value specified by Result type\r
1624 and stores the converted value into the caller allocated output buffer\r
1625 specified by Result. The caller must pass in a Result buffer that is at\r
1626 least as large as the Result type.\r
1627\r
1628 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1629\r
1630 If the conversion results in an overflow or an underflow condition, then\r
1631 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1632\r
1633 @param[in] Operand Operand to be converted to new type\r
1634 @param[out] Result Pointer to the result of conversion\r
1635\r
1636 @retval RETURN_SUCCESS Successful conversion\r
1637 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1638 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1639**/\r
1640RETURN_STATUS\r
1641EFIAPI\r
1642SafeIntnToUint16 (\r
1643 IN INTN Operand,\r
1644 OUT UINT16 *Result\r
1645 )\r
1646{\r
1647 RETURN_STATUS Status;\r
1648\r
1649 if (Result == NULL) {\r
1650 return RETURN_INVALID_PARAMETER;\r
1651 }\r
1652\r
1653 if ((Operand >= 0) && (Operand <= MAX_UINT16)) {\r
1654 *Result = (UINT16)Operand;\r
2f88bd3a 1655 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
1656 } else {\r
1657 *Result = UINT16_ERROR;\r
2f88bd3a 1658 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
1659 }\r
1660\r
1661 return Status;\r
1662}\r
1663\r
1664/**\r
1665 INTN -> UINTN conversion\r
1666\r
1667 Converts the value specified by Operand to a value specified by Result type\r
1668 and stores the converted value into the caller allocated output buffer\r
1669 specified by Result. The caller must pass in a Result buffer that is at\r
1670 least as large as the Result type.\r
1671\r
1672 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1673\r
1674 If the conversion results in an overflow or an underflow condition, then\r
1675 Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1676\r
1677 @param[in] Operand Operand to be converted to new type\r
1678 @param[out] Result Pointer to the result of conversion\r
1679\r
1680 @retval RETURN_SUCCESS Successful conversion\r
1681 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1682 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1683**/\r
1684RETURN_STATUS\r
1685EFIAPI\r
1686SafeIntnToUintn (\r
1687 IN INTN Operand,\r
1688 OUT UINTN *Result\r
1689 )\r
1690{\r
1691 RETURN_STATUS Status;\r
1692\r
1693 if (Result == NULL) {\r
1694 return RETURN_INVALID_PARAMETER;\r
1695 }\r
1696\r
1697 if (Operand >= 0) {\r
1698 *Result = (UINTN)Operand;\r
2f88bd3a 1699 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
1700 } else {\r
1701 *Result = UINTN_ERROR;\r
2f88bd3a 1702 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
1703 }\r
1704\r
1705 return Status;\r
1706}\r
1707\r
1708/**\r
1709 INTN -> UINT64 conversion\r
1710\r
1711 Converts the value specified by Operand to a value specified by Result type\r
1712 and stores the converted value into the caller allocated output buffer\r
1713 specified by Result. The caller must pass in a Result buffer that is at\r
1714 least as large as the Result type.\r
1715\r
1716 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1717\r
1718 If the conversion results in an overflow or an underflow condition, then\r
1719 Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1720\r
1721 @param[in] Operand Operand to be converted to new type\r
1722 @param[out] Result Pointer to the result of conversion\r
1723\r
1724 @retval RETURN_SUCCESS Successful conversion\r
1725 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1726 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1727**/\r
1728RETURN_STATUS\r
1729EFIAPI\r
1730SafeIntnToUint64 (\r
1731 IN INTN Operand,\r
1732 OUT UINT64 *Result\r
1733 )\r
1734{\r
1735 RETURN_STATUS Status;\r
1736\r
1737 if (Result == NULL) {\r
1738 return RETURN_INVALID_PARAMETER;\r
1739 }\r
1740\r
1741 if (Operand >= 0) {\r
1742 *Result = (UINT64)Operand;\r
2f88bd3a 1743 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
1744 } else {\r
1745 *Result = UINT64_ERROR;\r
2f88bd3a 1746 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
1747 }\r
1748\r
1749 return Status;\r
1750}\r
1751\r
1752/**\r
1753 UINTN -> INT8 conversion\r
1754\r
1755 Converts the value specified by Operand to a value specified by Result type\r
1756 and stores the converted value into the caller allocated output buffer\r
1757 specified by Result. The caller must pass in a Result buffer that is at\r
1758 least as large as the Result type.\r
1759\r
1760 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1761\r
1762 If the conversion results in an overflow or an underflow condition, then\r
1763 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1764\r
1765 @param[in] Operand Operand to be converted to new type\r
1766 @param[out] Result Pointer to the result of conversion\r
1767\r
1768 @retval RETURN_SUCCESS Successful conversion\r
1769 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1770 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1771**/\r
1772RETURN_STATUS\r
1773EFIAPI\r
1774SafeUintnToInt8 (\r
1775 IN UINTN Operand,\r
1776 OUT INT8 *Result\r
1777 )\r
1778{\r
1779 RETURN_STATUS Status;\r
1780\r
1781 if (Result == NULL) {\r
1782 return RETURN_INVALID_PARAMETER;\r
1783 }\r
1784\r
1785 if (Operand <= MAX_INT8) {\r
1786 *Result = (INT8)Operand;\r
2f88bd3a 1787 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
1788 } else {\r
1789 *Result = INT8_ERROR;\r
2f88bd3a 1790 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
1791 }\r
1792\r
1793 return Status;\r
1794}\r
1795\r
1796/**\r
1797 UINTN -> CHAR8 conversion\r
1798\r
1799 Converts the value specified by Operand to a value specified by Result type\r
1800 and stores the converted value into the caller allocated output buffer\r
1801 specified by Result. The caller must pass in a Result buffer that is at\r
1802 least as large as the Result type.\r
1803\r
1804 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1805\r
1806 If the conversion results in an overflow or an underflow condition, then\r
1807 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1808\r
1809 @param[in] Operand Operand to be converted to new type\r
1810 @param[out] Result Pointer to the result of conversion\r
1811\r
1812 @retval RETURN_SUCCESS Successful conversion\r
1813 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1814 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1815**/\r
1816RETURN_STATUS\r
1817EFIAPI\r
1818SafeUintnToChar8 (\r
1819 IN UINTN Operand,\r
1820 OUT CHAR8 *Result\r
1821 )\r
1822{\r
1823 RETURN_STATUS Status;\r
1824\r
1825 if (Result == NULL) {\r
1826 return RETURN_INVALID_PARAMETER;\r
1827 }\r
1828\r
1829 if (Operand <= MAX_INT8) {\r
1830 *Result = (INT8)Operand;\r
2f88bd3a 1831 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
1832 } else {\r
1833 *Result = CHAR8_ERROR;\r
2f88bd3a 1834 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
1835 }\r
1836\r
1837 return Status;\r
1838}\r
1839\r
1840/**\r
1841 UINTN -> UINT8 conversion\r
1842\r
1843 Converts the value specified by Operand to a value specified by Result type\r
1844 and stores the converted value into the caller allocated output buffer\r
1845 specified by Result. The caller must pass in a Result buffer that is at\r
1846 least as large as the Result type.\r
1847\r
1848 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1849\r
1850 If the conversion results in an overflow or an underflow condition, then\r
1851 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1852\r
1853 @param[in] Operand Operand to be converted to new type\r
1854 @param[out] Result Pointer to the result of conversion\r
1855\r
1856 @retval RETURN_SUCCESS Successful conversion\r
1857 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1858 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1859**/\r
1860RETURN_STATUS\r
1861EFIAPI\r
1862SafeUintnToUint8 (\r
1863 IN UINTN Operand,\r
1864 OUT UINT8 *Result\r
1865 )\r
1866{\r
1867 RETURN_STATUS Status;\r
1868\r
1869 if (Result == NULL) {\r
1870 return RETURN_INVALID_PARAMETER;\r
1871 }\r
1872\r
1873 if (Operand <= MAX_UINT8) {\r
1874 *Result = (UINT8)Operand;\r
2f88bd3a 1875 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
1876 } else {\r
1877 *Result = UINT8_ERROR;\r
2f88bd3a 1878 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
1879 }\r
1880\r
1881 return Status;\r
1882}\r
1883\r
1884/**\r
1885 UINTN -> INT16 conversion\r
1886\r
1887 Converts the value specified by Operand to a value specified by Result type\r
1888 and stores the converted value into the caller allocated output buffer\r
1889 specified by Result. The caller must pass in a Result buffer that is at\r
1890 least as large as the Result type.\r
1891\r
1892 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1893\r
1894 If the conversion results in an overflow or an underflow condition, then\r
1895 Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1896\r
1897 @param[in] Operand Operand to be converted to new type\r
1898 @param[out] Result Pointer to the result of conversion\r
1899\r
1900 @retval RETURN_SUCCESS Successful conversion\r
1901 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1902 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1903**/\r
1904RETURN_STATUS\r
1905EFIAPI\r
1906SafeUintnToInt16 (\r
1907 IN UINTN Operand,\r
1908 OUT INT16 *Result\r
1909 )\r
1910{\r
1911 RETURN_STATUS Status;\r
1912\r
1913 if (Result == NULL) {\r
1914 return RETURN_INVALID_PARAMETER;\r
1915 }\r
1916\r
1917 if (Operand <= MAX_INT16) {\r
1918 *Result = (INT16)Operand;\r
2f88bd3a 1919 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
1920 } else {\r
1921 *Result = INT16_ERROR;\r
2f88bd3a 1922 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
1923 }\r
1924\r
1925 return Status;\r
1926}\r
1927\r
1928/**\r
1929 UINTN -> UINT16 conversion\r
1930\r
1931 Converts the value specified by Operand to a value specified by Result type\r
1932 and stores the converted value into the caller allocated output buffer\r
1933 specified by Result. The caller must pass in a Result buffer that is at\r
1934 least as large as the Result type.\r
1935\r
1936 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1937\r
1938 If the conversion results in an overflow or an underflow condition, then\r
1939 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1940\r
1941 @param[in] Operand Operand to be converted to new type\r
1942 @param[out] Result Pointer to the result of conversion\r
1943\r
1944 @retval RETURN_SUCCESS Successful conversion\r
1945 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1946 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1947**/\r
1948RETURN_STATUS\r
1949EFIAPI\r
1950SafeUintnToUint16 (\r
1951 IN UINTN Operand,\r
1952 OUT UINT16 *Result\r
1953 )\r
1954{\r
1955 RETURN_STATUS Status;\r
1956\r
1957 if (Result == NULL) {\r
1958 return RETURN_INVALID_PARAMETER;\r
1959 }\r
1960\r
1961 if (Operand <= MAX_UINT16) {\r
1962 *Result = (UINT16)Operand;\r
2f88bd3a 1963 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
1964 } else {\r
1965 *Result = UINT16_ERROR;\r
2f88bd3a 1966 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
1967 }\r
1968\r
1969 return Status;\r
1970}\r
1971\r
1972/**\r
1973 UINTN -> INT32 conversion\r
1974\r
1975 Converts the value specified by Operand to a value specified by Result type\r
1976 and stores the converted value into the caller allocated output buffer\r
1977 specified by Result. The caller must pass in a Result buffer that is at\r
1978 least as large as the Result type.\r
1979\r
1980 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1981\r
1982 If the conversion results in an overflow or an underflow condition, then\r
1983 Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1984\r
1985 @param[in] Operand Operand to be converted to new type\r
1986 @param[out] Result Pointer to the result of conversion\r
1987\r
1988 @retval RETURN_SUCCESS Successful conversion\r
1989 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1990 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1991**/\r
1992RETURN_STATUS\r
1993EFIAPI\r
1994SafeUintnToInt32 (\r
1995 IN UINTN Operand,\r
1996 OUT INT32 *Result\r
1997 )\r
1998{\r
1999 RETURN_STATUS Status;\r
2000\r
2001 if (Result == NULL) {\r
2002 return RETURN_INVALID_PARAMETER;\r
2003 }\r
2004\r
2005 if (Operand <= MAX_INT32) {\r
2006 *Result = (INT32)Operand;\r
2f88bd3a 2007 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
2008 } else {\r
2009 *Result = INT32_ERROR;\r
2f88bd3a 2010 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
2011 }\r
2012\r
2013 return Status;\r
2014}\r
2015\r
2016/**\r
2017 UINTN -> INTN conversion\r
2018\r
2019 Converts the value specified by Operand to a value specified by Result type\r
2020 and stores the converted value into the caller allocated output buffer\r
2021 specified by Result. The caller must pass in a Result buffer that is at\r
2022 least as large as the Result type.\r
2023\r
2024 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2025\r
2026 If the conversion results in an overflow or an underflow condition, then\r
2027 Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2028\r
2029 @param[in] Operand Operand to be converted to new type\r
2030 @param[out] Result Pointer to the result of conversion\r
2031\r
2032 @retval RETURN_SUCCESS Successful conversion\r
2033 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2034 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2035**/\r
2036RETURN_STATUS\r
2037EFIAPI\r
2038SafeUintnToIntn (\r
2039 IN UINTN Operand,\r
2040 OUT INTN *Result\r
2041 )\r
2042{\r
2043 RETURN_STATUS Status;\r
2044\r
2045 if (Result == NULL) {\r
2046 return RETURN_INVALID_PARAMETER;\r
2047 }\r
2048\r
2049 if (Operand <= MAX_INTN) {\r
2050 *Result = (INTN)Operand;\r
2f88bd3a 2051 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
2052 } else {\r
2053 *Result = INTN_ERROR;\r
2f88bd3a 2054 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
2055 }\r
2056\r
2057 return Status;\r
2058}\r
2059\r
2060/**\r
2061 INT64 -> INT8 conversion\r
2062\r
2063 Converts the value specified by Operand to a value specified by Result type\r
2064 and stores the converted value into the caller allocated output buffer\r
2065 specified by Result. The caller must pass in a Result buffer that is at\r
2066 least as large as the Result type.\r
2067\r
2068 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2069\r
2070 If the conversion results in an overflow or an underflow condition, then\r
2071 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2072\r
2073 @param[in] Operand Operand to be converted to new type\r
2074 @param[out] Result Pointer to the result of conversion\r
2075\r
2076 @retval RETURN_SUCCESS Successful conversion\r
2077 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2078 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2079**/\r
2080RETURN_STATUS\r
2081EFIAPI\r
2082SafeInt64ToInt8 (\r
2083 IN INT64 Operand,\r
2084 OUT INT8 *Result\r
2085 )\r
2086{\r
2087 RETURN_STATUS Status;\r
2088\r
2089 if (Result == NULL) {\r
2090 return RETURN_INVALID_PARAMETER;\r
2091 }\r
2092\r
2093 if ((Operand >= MIN_INT8) && (Operand <= MAX_INT8)) {\r
2094 *Result = (INT8)Operand;\r
2f88bd3a 2095 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
2096 } else {\r
2097 *Result = INT8_ERROR;\r
2f88bd3a 2098 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
2099 }\r
2100\r
2101 return Status;\r
2102}\r
2103\r
2104/**\r
2105 INT64 -> CHAR8 conversion\r
2106\r
2107 Converts the value specified by Operand to a value specified by Result type\r
2108 and stores the converted value into the caller allocated output buffer\r
2109 specified by Result. The caller must pass in a Result buffer that is at\r
2110 least as large as the Result type.\r
2111\r
2112 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2113\r
2114 If the conversion results in an overflow or an underflow condition, then\r
2115 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2116\r
2117 @param[in] Operand Operand to be converted to new type\r
2118 @param[out] Result Pointer to the result of conversion\r
2119\r
2120 @retval RETURN_SUCCESS Successful conversion\r
2121 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2122 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2123**/\r
2124RETURN_STATUS\r
2125EFIAPI\r
2126SafeInt64ToChar8 (\r
2127 IN INT64 Operand,\r
2128 OUT CHAR8 *Result\r
2129 )\r
2130{\r
2131 RETURN_STATUS Status;\r
2132\r
2133 if (Result == NULL) {\r
2134 return RETURN_INVALID_PARAMETER;\r
2135 }\r
2136\r
2137 if ((Operand >= 0) && (Operand <= MAX_INT8)) {\r
2138 *Result = (CHAR8)Operand;\r
2f88bd3a 2139 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
2140 } else {\r
2141 *Result = CHAR8_ERROR;\r
2f88bd3a 2142 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
2143 }\r
2144\r
2145 return Status;\r
2146}\r
2147\r
2148/**\r
2149 INT64 -> UINT8 conversion\r
2150\r
2151 Converts the value specified by Operand to a value specified by Result type\r
2152 and stores the converted value into the caller allocated output buffer\r
2153 specified by Result. The caller must pass in a Result buffer that is at\r
2154 least as large as the Result type.\r
2155\r
2156 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2157\r
2158 If the conversion results in an overflow or an underflow condition, then\r
2159 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2160\r
2161 @param[in] Operand Operand to be converted to new type\r
2162 @param[out] Result Pointer to the result of conversion\r
2163\r
2164 @retval RETURN_SUCCESS Successful conversion\r
2165 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2166 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2167**/\r
2168RETURN_STATUS\r
2169EFIAPI\r
2170SafeInt64ToUint8 (\r
2171 IN INT64 Operand,\r
2172 OUT UINT8 *Result\r
2173 )\r
2174{\r
2175 RETURN_STATUS Status;\r
2176\r
2177 if (Result == NULL) {\r
2178 return RETURN_INVALID_PARAMETER;\r
2179 }\r
2180\r
2181 if ((Operand >= 0) && (Operand <= MAX_UINT8)) {\r
2182 *Result = (UINT8)Operand;\r
2f88bd3a 2183 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
2184 } else {\r
2185 *Result = UINT8_ERROR;\r
2f88bd3a 2186 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
2187 }\r
2188\r
2189 return Status;\r
2190}\r
2191\r
2192/**\r
2193 INT64 -> INT16 conversion\r
2194\r
2195 Converts the value specified by Operand to a value specified by Result type\r
2196 and stores the converted value into the caller allocated output buffer\r
2197 specified by Result. The caller must pass in a Result buffer that is at\r
2198 least as large as the Result type.\r
2199\r
2200 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2201\r
2202 If the conversion results in an overflow or an underflow condition, then\r
2203 Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2204\r
2205 @param[in] Operand Operand to be converted to new type\r
2206 @param[out] Result Pointer to the result of conversion\r
2207\r
2208 @retval RETURN_SUCCESS Successful conversion\r
2209 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2210 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2211**/\r
2212RETURN_STATUS\r
2213EFIAPI\r
2214SafeInt64ToInt16 (\r
2215 IN INT64 Operand,\r
2216 OUT INT16 *Result\r
2217 )\r
2218{\r
2219 RETURN_STATUS Status;\r
2220\r
2221 if (Result == NULL) {\r
2222 return RETURN_INVALID_PARAMETER;\r
2223 }\r
2224\r
2225 if ((Operand >= MIN_INT16) && (Operand <= MAX_INT16)) {\r
2226 *Result = (INT16)Operand;\r
2f88bd3a 2227 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
2228 } else {\r
2229 *Result = INT16_ERROR;\r
2f88bd3a 2230 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
2231 }\r
2232\r
2233 return Status;\r
2234}\r
2235\r
2236/**\r
2237 INT64 -> UINT16 conversion\r
2238\r
2239 Converts the value specified by Operand to a value specified by Result type\r
2240 and stores the converted value into the caller allocated output buffer\r
2241 specified by Result. The caller must pass in a Result buffer that is at\r
2242 least as large as the Result type.\r
2243\r
2244 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2245\r
2246 If the conversion results in an overflow or an underflow condition, then\r
2247 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2248\r
2249 @param[in] Operand Operand to be converted to new type\r
2250 @param[out] Result Pointer to the result of conversion\r
2251\r
2252 @retval RETURN_SUCCESS Successful conversion\r
2253 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2254 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2255**/\r
2256RETURN_STATUS\r
2257EFIAPI\r
2258SafeInt64ToUint16 (\r
2259 IN INT64 Operand,\r
2260 OUT UINT16 *Result\r
2261 )\r
2262{\r
2263 RETURN_STATUS Status;\r
2264\r
2265 if (Result == NULL) {\r
2266 return RETURN_INVALID_PARAMETER;\r
2267 }\r
2268\r
2269 if ((Operand >= 0) && (Operand <= MAX_UINT16)) {\r
2270 *Result = (UINT16)Operand;\r
2f88bd3a 2271 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
2272 } else {\r
2273 *Result = UINT16_ERROR;\r
2f88bd3a 2274 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
2275 }\r
2276\r
2277 return Status;\r
2278}\r
2279\r
2280/**\r
2281 INT64 -> INT32 conversion\r
2282\r
2283 Converts the value specified by Operand to a value specified by Result type\r
2284 and stores the converted value into the caller allocated output buffer\r
2285 specified by Result. The caller must pass in a Result buffer that is at\r
2286 least as large as the Result type.\r
2287\r
2288 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2289\r
2290 If the conversion results in an overflow or an underflow condition, then\r
2291 Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2292\r
2293 @param[in] Operand Operand to be converted to new type\r
2294 @param[out] Result Pointer to the result of conversion\r
2295\r
2296 @retval RETURN_SUCCESS Successful conversion\r
2297 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2298 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2299**/\r
2300RETURN_STATUS\r
2301EFIAPI\r
2302SafeInt64ToInt32 (\r
2303 IN INT64 Operand,\r
2304 OUT INT32 *Result\r
2305 )\r
2306{\r
2307 RETURN_STATUS Status;\r
2308\r
2309 if (Result == NULL) {\r
2310 return RETURN_INVALID_PARAMETER;\r
2311 }\r
2312\r
2313 if ((Operand >= MIN_INT32) && (Operand <= MAX_INT32)) {\r
2314 *Result = (INT32)Operand;\r
2f88bd3a 2315 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
2316 } else {\r
2317 *Result = INT32_ERROR;\r
2f88bd3a 2318 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
2319 }\r
2320\r
2321 return Status;\r
2322}\r
2323\r
2324/**\r
2325 INT64 -> UINT32 conversion\r
2326\r
2327 Converts the value specified by Operand to a value specified by Result type\r
2328 and stores the converted value into the caller allocated output buffer\r
2329 specified by Result. The caller must pass in a Result buffer that is at\r
2330 least as large as the Result type.\r
2331\r
2332 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2333\r
2334 If the conversion results in an overflow or an underflow condition, then\r
2335 Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2336\r
2337 @param[in] Operand Operand to be converted to new type\r
2338 @param[out] Result Pointer to the result of conversion\r
2339\r
2340 @retval RETURN_SUCCESS Successful conversion\r
2341 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2342 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2343**/\r
2344RETURN_STATUS\r
2345EFIAPI\r
2346SafeInt64ToUint32 (\r
2347 IN INT64 Operand,\r
2348 OUT UINT32 *Result\r
2349 )\r
2350{\r
2351 RETURN_STATUS Status;\r
2352\r
2353 if (Result == NULL) {\r
2354 return RETURN_INVALID_PARAMETER;\r
2355 }\r
2356\r
2357 if ((Operand >= 0) && (Operand <= MAX_UINT32)) {\r
2358 *Result = (UINT32)Operand;\r
2f88bd3a 2359 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
2360 } else {\r
2361 *Result = UINT32_ERROR;\r
2f88bd3a 2362 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
2363 }\r
2364\r
2365 return Status;\r
2366}\r
2367\r
2368/**\r
2369 INT64 -> UINT64 conversion\r
2370\r
2371 Converts the value specified by Operand to a value specified by Result type\r
2372 and stores the converted value into the caller allocated output buffer\r
2373 specified by Result. The caller must pass in a Result buffer that is at\r
2374 least as large as the Result type.\r
2375\r
2376 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2377\r
2378 If the conversion results in an overflow or an underflow condition, then\r
2379 Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2380\r
2381 @param[in] Operand Operand to be converted to new type\r
2382 @param[out] Result Pointer to the result of conversion\r
2383\r
2384 @retval RETURN_SUCCESS Successful conversion\r
2385 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2386 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2387**/\r
2388RETURN_STATUS\r
2389EFIAPI\r
2390SafeInt64ToUint64 (\r
2391 IN INT64 Operand,\r
2392 OUT UINT64 *Result\r
2393 )\r
2394{\r
2395 RETURN_STATUS Status;\r
2396\r
2397 if (Result == NULL) {\r
2398 return RETURN_INVALID_PARAMETER;\r
2399 }\r
2400\r
2401 if (Operand >= 0) {\r
2402 *Result = (UINT64)Operand;\r
2f88bd3a 2403 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
2404 } else {\r
2405 *Result = UINT64_ERROR;\r
2f88bd3a 2406 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
2407 }\r
2408\r
2409 return Status;\r
2410}\r
2411\r
2412/**\r
2413 UINT64 -> INT8 conversion\r
2414\r
2415 Converts the value specified by Operand to a value specified by Result type\r
2416 and stores the converted value into the caller allocated output buffer\r
2417 specified by Result. The caller must pass in a Result buffer that is at\r
2418 least as large as the Result type.\r
2419\r
2420 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2421\r
2422 If the conversion results in an overflow or an underflow condition, then\r
2423 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2424\r
2425 @param[in] Operand Operand to be converted to new type\r
2426 @param[out] Result Pointer to the result of conversion\r
2427\r
2428 @retval RETURN_SUCCESS Successful conversion\r
2429 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2430 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2431**/\r
2432RETURN_STATUS\r
2433EFIAPI\r
2434SafeUint64ToInt8 (\r
2435 IN UINT64 Operand,\r
2436 OUT INT8 *Result\r
2437 )\r
2438{\r
2439 RETURN_STATUS Status;\r
2440\r
2441 if (Result == NULL) {\r
2442 return RETURN_INVALID_PARAMETER;\r
2443 }\r
2444\r
2445 if (Operand <= MAX_INT8) {\r
2446 *Result = (INT8)Operand;\r
2f88bd3a 2447 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
2448 } else {\r
2449 *Result = INT8_ERROR;\r
2f88bd3a 2450 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
2451 }\r
2452\r
2453 return Status;\r
2454}\r
2455\r
2456/**\r
2457 UINT64 -> CHAR8 conversion\r
2458\r
2459 Converts the value specified by Operand to a value specified by Result type\r
2460 and stores the converted value into the caller allocated output buffer\r
2461 specified by Result. The caller must pass in a Result buffer that is at\r
2462 least as large as the Result type.\r
2463\r
2464 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2465\r
2466 If the conversion results in an overflow or an underflow condition, then\r
2467 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2468\r
2469 @param[in] Operand Operand to be converted to new type\r
2470 @param[out] Result Pointer to the result of conversion\r
2471\r
2472 @retval RETURN_SUCCESS Successful conversion\r
2473 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2474 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2475**/\r
2476RETURN_STATUS\r
2477EFIAPI\r
2478SafeUint64ToChar8 (\r
2479 IN UINT64 Operand,\r
2480 OUT CHAR8 *Result\r
2481 )\r
2482{\r
2483 RETURN_STATUS Status;\r
2484\r
2485 if (Result == NULL) {\r
2486 return RETURN_INVALID_PARAMETER;\r
2487 }\r
2488\r
2489 if (Operand <= MAX_INT8) {\r
2490 *Result = (INT8)Operand;\r
2f88bd3a 2491 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
2492 } else {\r
2493 *Result = CHAR8_ERROR;\r
2f88bd3a 2494 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
2495 }\r
2496\r
2497 return Status;\r
2498}\r
2499\r
2500/**\r
2501 UINT64 -> UINT8 conversion\r
2502\r
2503 Converts the value specified by Operand to a value specified by Result type\r
2504 and stores the converted value into the caller allocated output buffer\r
2505 specified by Result. The caller must pass in a Result buffer that is at\r
2506 least as large as the Result type.\r
2507\r
2508 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2509\r
2510 If the conversion results in an overflow or an underflow condition, then\r
2511 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2512\r
2513 @param[in] Operand Operand to be converted to new type\r
2514 @param[out] Result Pointer to the result of conversion\r
2515\r
2516 @retval RETURN_SUCCESS Successful conversion\r
2517 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2518 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2519**/\r
2520RETURN_STATUS\r
2521EFIAPI\r
2522SafeUint64ToUint8 (\r
2523 IN UINT64 Operand,\r
2524 OUT UINT8 *Result\r
2525 )\r
2526{\r
2527 RETURN_STATUS Status;\r
2528\r
2529 if (Result == NULL) {\r
2530 return RETURN_INVALID_PARAMETER;\r
2531 }\r
2532\r
2533 if (Operand <= MAX_UINT8) {\r
2534 *Result = (UINT8)Operand;\r
2f88bd3a 2535 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
2536 } else {\r
2537 *Result = UINT8_ERROR;\r
2f88bd3a 2538 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
2539 }\r
2540\r
2541 return Status;\r
2542}\r
2543\r
2544/**\r
2545 UINT64 -> INT16 conversion\r
2546\r
2547 Converts the value specified by Operand to a value specified by Result type\r
2548 and stores the converted value into the caller allocated output buffer\r
2549 specified by Result. The caller must pass in a Result buffer that is at\r
2550 least as large as the Result type.\r
2551\r
2552 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2553\r
2554 If the conversion results in an overflow or an underflow condition, then\r
2555 Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2556\r
2557 @param[in] Operand Operand to be converted to new type\r
2558 @param[out] Result Pointer to the result of conversion\r
2559\r
2560 @retval RETURN_SUCCESS Successful conversion\r
2561 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2562 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2563**/\r
2564RETURN_STATUS\r
2565EFIAPI\r
2566SafeUint64ToInt16 (\r
2567 IN UINT64 Operand,\r
2568 OUT INT16 *Result\r
2569 )\r
2570{\r
2571 RETURN_STATUS Status;\r
2572\r
2573 if (Result == NULL) {\r
2574 return RETURN_INVALID_PARAMETER;\r
2575 }\r
2576\r
2577 if (Operand <= MAX_INT16) {\r
2578 *Result = (INT16)Operand;\r
2f88bd3a 2579 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
2580 } else {\r
2581 *Result = INT16_ERROR;\r
2f88bd3a 2582 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
2583 }\r
2584\r
2585 return Status;\r
2586}\r
2587\r
2588/**\r
2589 UINT64 -> UINT16 conversion\r
2590\r
2591 Converts the value specified by Operand to a value specified by Result type\r
2592 and stores the converted value into the caller allocated output buffer\r
2593 specified by Result. The caller must pass in a Result buffer that is at\r
2594 least as large as the Result type.\r
2595\r
2596 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2597\r
2598 If the conversion results in an overflow or an underflow condition, then\r
2599 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2600\r
2601 @param[in] Operand Operand to be converted to new type\r
2602 @param[out] Result Pointer to the result of conversion\r
2603\r
2604 @retval RETURN_SUCCESS Successful conversion\r
2605 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2606 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2607**/\r
2608RETURN_STATUS\r
2609EFIAPI\r
2610SafeUint64ToUint16 (\r
2611 IN UINT64 Operand,\r
2612 OUT UINT16 *Result\r
2613 )\r
2614{\r
2615 RETURN_STATUS Status;\r
2616\r
2617 if (Result == NULL) {\r
2618 return RETURN_INVALID_PARAMETER;\r
2619 }\r
2620\r
2621 if (Operand <= MAX_UINT16) {\r
2622 *Result = (UINT16)Operand;\r
2f88bd3a 2623 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
2624 } else {\r
2625 *Result = UINT16_ERROR;\r
2f88bd3a 2626 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
2627 }\r
2628\r
2629 return Status;\r
2630}\r
2631\r
2632/**\r
2633 UINT64 -> INT32 conversion\r
2634\r
2635 Converts the value specified by Operand to a value specified by Result type\r
2636 and stores the converted value into the caller allocated output buffer\r
2637 specified by Result. The caller must pass in a Result buffer that is at\r
2638 least as large as the Result type.\r
2639\r
2640 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2641\r
2642 If the conversion results in an overflow or an underflow condition, then\r
2643 Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2644\r
2645 @param[in] Operand Operand to be converted to new type\r
2646 @param[out] Result Pointer to the result of conversion\r
2647\r
2648 @retval RETURN_SUCCESS Successful conversion\r
2649 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2650 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2651**/\r
2652RETURN_STATUS\r
2653EFIAPI\r
2654SafeUint64ToInt32 (\r
2655 IN UINT64 Operand,\r
2656 OUT INT32 *Result\r
2657 )\r
2658{\r
2659 RETURN_STATUS Status;\r
2660\r
2661 if (Result == NULL) {\r
2662 return RETURN_INVALID_PARAMETER;\r
2663 }\r
2664\r
2665 if (Operand <= MAX_INT32) {\r
2666 *Result = (INT32)Operand;\r
2f88bd3a 2667 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
2668 } else {\r
2669 *Result = INT32_ERROR;\r
2f88bd3a 2670 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
2671 }\r
2672\r
2673 return Status;\r
2674}\r
2675\r
2676/**\r
2677 UINT64 -> UINT32 conversion\r
2678\r
2679 Converts the value specified by Operand to a value specified by Result type\r
2680 and stores the converted value into the caller allocated output buffer\r
2681 specified by Result. The caller must pass in a Result buffer that is at\r
2682 least as large as the Result type.\r
2683\r
2684 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2685\r
2686 If the conversion results in an overflow or an underflow condition, then\r
2687 Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2688\r
2689 @param[in] Operand Operand to be converted to new type\r
2690 @param[out] Result Pointer to the result of conversion\r
2691\r
2692 @retval RETURN_SUCCESS Successful conversion\r
2693 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2694 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2695**/\r
2696RETURN_STATUS\r
2697EFIAPI\r
2698SafeUint64ToUint32 (\r
2699 IN UINT64 Operand,\r
2700 OUT UINT32 *Result\r
2701 )\r
2702{\r
2703 RETURN_STATUS Status;\r
2704\r
2705 if (Result == NULL) {\r
2706 return RETURN_INVALID_PARAMETER;\r
2707 }\r
2708\r
2709 if (Operand <= MAX_UINT32) {\r
2710 *Result = (UINT32)Operand;\r
2f88bd3a 2711 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
2712 } else {\r
2713 *Result = UINT32_ERROR;\r
2f88bd3a 2714 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
2715 }\r
2716\r
2717 return Status;\r
2718}\r
2719\r
2720/**\r
2721 UINT64 -> INTN conversion\r
2722\r
2723 Converts the value specified by Operand to a value specified by Result type\r
2724 and stores the converted value into the caller allocated output buffer\r
2725 specified by Result. The caller must pass in a Result buffer that is at\r
2726 least as large as the Result type.\r
2727\r
2728 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2729\r
2730 If the conversion results in an overflow or an underflow condition, then\r
2731 Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2732\r
2733 @param[in] Operand Operand to be converted to new type\r
2734 @param[out] Result Pointer to the result of conversion\r
2735\r
2736 @retval RETURN_SUCCESS Successful conversion\r
2737 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2738 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2739**/\r
2740RETURN_STATUS\r
2741EFIAPI\r
2742SafeUint64ToIntn (\r
2743 IN UINT64 Operand,\r
2744 OUT INTN *Result\r
2745 )\r
2746{\r
2747 RETURN_STATUS Status;\r
2748\r
2749 if (Result == NULL) {\r
2750 return RETURN_INVALID_PARAMETER;\r
2751 }\r
2752\r
2753 if (Operand <= MAX_INTN) {\r
2754 *Result = (INTN)Operand;\r
2f88bd3a 2755 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
2756 } else {\r
2757 *Result = INTN_ERROR;\r
2f88bd3a 2758 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
2759 }\r
2760\r
2761 return Status;\r
2762}\r
2763\r
2764/**\r
2765 UINT64 -> INT64 conversion\r
2766\r
2767 Converts the value specified by Operand to a value specified by Result type\r
2768 and stores the converted value into the caller allocated output buffer\r
2769 specified by Result. The caller must pass in a Result buffer that is at\r
2770 least as large as the Result type.\r
2771\r
2772 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2773\r
2774 If the conversion results in an overflow or an underflow condition, then\r
2775 Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2776\r
2777 @param[in] Operand Operand to be converted to new type\r
2778 @param[out] Result Pointer to the result of conversion\r
2779\r
2780 @retval RETURN_SUCCESS Successful conversion\r
2781 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2782 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2783**/\r
2784RETURN_STATUS\r
2785EFIAPI\r
2786SafeUint64ToInt64 (\r
2787 IN UINT64 Operand,\r
2788 OUT INT64 *Result\r
2789 )\r
2790{\r
2791 RETURN_STATUS Status;\r
2792\r
2793 if (Result == NULL) {\r
2794 return RETURN_INVALID_PARAMETER;\r
2795 }\r
2796\r
2797 if (Operand <= MAX_INT64) {\r
2798 *Result = (INT64)Operand;\r
2f88bd3a 2799 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
2800 } else {\r
2801 *Result = INT64_ERROR;\r
2f88bd3a 2802 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
2803 }\r
2804\r
2805 return Status;\r
2806}\r
2807\r
2808//\r
2809// Addition functions\r
2810//\r
2811\r
2812/**\r
2813 UINT8 addition\r
2814\r
2815 Performs the requested operation using the input parameters into a value\r
2816 specified by Result type and stores the converted value into the caller\r
2817 allocated output buffer specified by Result. The caller must pass in a\r
2818 Result buffer that is at least as large as the Result type.\r
2819\r
2820 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2821\r
2822 If the requested operation results in an overflow or an underflow condition,\r
2823 then Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2824\r
2825 @param[in] Augend A number to which addend will be added\r
2826 @param[in] Addend A number to be added to another\r
2827 @param[out] Result Pointer to the result of addition\r
2828\r
2829 @retval RETURN_SUCCESS Successful addition\r
2830 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2831 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2832**/\r
2833RETURN_STATUS\r
2834EFIAPI\r
2835SafeUint8Add (\r
2836 IN UINT8 Augend,\r
2837 IN UINT8 Addend,\r
2838 OUT UINT8 *Result\r
2839 )\r
2840{\r
2841 RETURN_STATUS Status;\r
2842\r
2843 if (Result == NULL) {\r
2844 return RETURN_INVALID_PARAMETER;\r
2845 }\r
2846\r
2847 if (((UINT8)(Augend + Addend)) >= Augend) {\r
2848 *Result = (UINT8)(Augend + Addend);\r
2f88bd3a 2849 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
2850 } else {\r
2851 *Result = UINT8_ERROR;\r
2f88bd3a 2852 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
2853 }\r
2854\r
2855 return Status;\r
2856}\r
2857\r
2858/**\r
2859 UINT16 addition\r
2860\r
2861 Performs the requested operation using the input parameters into a value\r
2862 specified by Result type and stores the converted value into the caller\r
2863 allocated output buffer specified by Result. The caller must pass in a\r
2864 Result buffer that is at least as large as the Result type.\r
2865\r
2866 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2867\r
2868 If the requested operation results in an overflow or an underflow condition,\r
2869 then Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2870\r
2871 @param[in] Augend A number to which addend will be added\r
2872 @param[in] Addend A number to be added to another\r
2873 @param[out] Result Pointer to the result of addition\r
2874\r
2875 @retval RETURN_SUCCESS Successful addition\r
2876 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2877 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2878**/\r
2879RETURN_STATUS\r
2880EFIAPI\r
2881SafeUint16Add (\r
2882 IN UINT16 Augend,\r
2883 IN UINT16 Addend,\r
2884 OUT UINT16 *Result\r
2885 )\r
2886{\r
2887 RETURN_STATUS Status;\r
2888\r
2889 if (Result == NULL) {\r
2890 return RETURN_INVALID_PARAMETER;\r
2891 }\r
2892\r
2893 if (((UINT16)(Augend + Addend)) >= Augend) {\r
2894 *Result = (UINT16)(Augend + Addend);\r
2f88bd3a 2895 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
2896 } else {\r
2897 *Result = UINT16_ERROR;\r
2f88bd3a 2898 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
2899 }\r
2900\r
2901 return Status;\r
2902}\r
2903\r
2904/**\r
2905 UINT32 addition\r
2906\r
2907 Performs the requested operation using the input parameters into a value\r
2908 specified by Result type and stores the converted value into the caller\r
2909 allocated output buffer specified by Result. The caller must pass in a\r
2910 Result buffer that is at least as large as the Result type.\r
2911\r
2912 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2913\r
2914 If the requested operation results in an overflow or an underflow condition,\r
2915 then Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2916\r
2917 @param[in] Augend A number to which addend will be added\r
2918 @param[in] Addend A number to be added to another\r
2919 @param[out] Result Pointer to the result of addition\r
2920\r
2921 @retval RETURN_SUCCESS Successful addition\r
2922 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2923 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2924**/\r
2925RETURN_STATUS\r
2926EFIAPI\r
2927SafeUint32Add (\r
2928 IN UINT32 Augend,\r
2929 IN UINT32 Addend,\r
2930 OUT UINT32 *Result\r
2931 )\r
2932{\r
2933 RETURN_STATUS Status;\r
2934\r
2935 if (Result == NULL) {\r
2936 return RETURN_INVALID_PARAMETER;\r
2937 }\r
2938\r
2939 if ((Augend + Addend) >= Augend) {\r
2940 *Result = (Augend + Addend);\r
2f88bd3a 2941 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
2942 } else {\r
2943 *Result = UINT32_ERROR;\r
2f88bd3a 2944 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
2945 }\r
2946\r
2947 return Status;\r
2948}\r
2949\r
2950/**\r
2951 UINT64 addition\r
2952\r
2953 Performs the requested operation using the input parameters into a value\r
2954 specified by Result type and stores the converted value into the caller\r
2955 allocated output buffer specified by Result. The caller must pass in a\r
2956 Result buffer that is at least as large as the Result type.\r
2957\r
2958 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2959\r
2960 If the requested operation results in an overflow or an underflow condition,\r
2961 then Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2962\r
2963 @param[in] Augend A number to which addend will be added\r
2964 @param[in] Addend A number to be added to another\r
2965 @param[out] Result Pointer to the result of addition\r
2966\r
2967 @retval RETURN_SUCCESS Successful addition\r
2968 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2969 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2970**/\r
2971RETURN_STATUS\r
2972EFIAPI\r
2973SafeUint64Add (\r
2974 IN UINT64 Augend,\r
2975 IN UINT64 Addend,\r
2976 OUT UINT64 *Result\r
2977 )\r
2978{\r
2979 RETURN_STATUS Status;\r
2980\r
2981 if (Result == NULL) {\r
2982 return RETURN_INVALID_PARAMETER;\r
2983 }\r
2984\r
2985 if ((Augend + Addend) >= Augend) {\r
2986 *Result = (Augend + Addend);\r
2f88bd3a 2987 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
2988 } else {\r
2989 *Result = UINT64_ERROR;\r
2f88bd3a 2990 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
2991 }\r
2992\r
2993 return Status;\r
2994}\r
2995\r
2996//\r
2997// Subtraction functions\r
2998//\r
2999\r
3000/**\r
3001 UINT8 subtraction\r
3002\r
3003 Performs the requested operation using the input parameters into a value\r
3004 specified by Result type and stores the converted value into the caller\r
3005 allocated output buffer specified by Result. The caller must pass in a\r
3006 Result buffer that is at least as large as the Result type.\r
3007\r
3008 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3009\r
3010 If the requested operation results in an overflow or an underflow condition,\r
3011 then Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3012\r
3013 @param[in] Minuend A number from which another is to be subtracted.\r
3014 @param[in] Subtrahend A number to be subtracted from another\r
3015 @param[out] Result Pointer to the result of subtraction\r
3016\r
3017 @retval RETURN_SUCCESS Successful subtraction\r
3018 @retval RETURN_BUFFER_TOO_SMALL Underflow\r
3019 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3020**/\r
3021RETURN_STATUS\r
3022EFIAPI\r
3023SafeUint8Sub (\r
3024 IN UINT8 Minuend,\r
3025 IN UINT8 Subtrahend,\r
3026 OUT UINT8 *Result\r
3027 )\r
3028{\r
3029 RETURN_STATUS Status;\r
3030\r
3031 if (Result == NULL) {\r
3032 return RETURN_INVALID_PARAMETER;\r
3033 }\r
3034\r
3035 if (Minuend >= Subtrahend) {\r
3036 *Result = (UINT8)(Minuend - Subtrahend);\r
2f88bd3a 3037 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
3038 } else {\r
3039 *Result = UINT8_ERROR;\r
2f88bd3a 3040 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
3041 }\r
3042\r
3043 return Status;\r
3044}\r
3045\r
3046/**\r
3047 UINT16 subtraction\r
3048\r
3049 Performs the requested operation using the input parameters into a value\r
3050 specified by Result type and stores the converted value into the caller\r
3051 allocated output buffer specified by Result. The caller must pass in a\r
3052 Result buffer that is at least as large as the Result type.\r
3053\r
3054 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3055\r
3056 If the requested operation results in an overflow or an underflow condition,\r
3057 then Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3058\r
3059 @param[in] Minuend A number from which another is to be subtracted.\r
3060 @param[in] Subtrahend A number to be subtracted from another\r
3061 @param[out] Result Pointer to the result of subtraction\r
3062\r
3063 @retval RETURN_SUCCESS Successful subtraction\r
3064 @retval RETURN_BUFFER_TOO_SMALL Underflow\r
3065 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3066**/\r
3067RETURN_STATUS\r
3068EFIAPI\r
3069SafeUint16Sub (\r
3070 IN UINT16 Minuend,\r
3071 IN UINT16 Subtrahend,\r
3072 OUT UINT16 *Result\r
3073 )\r
3074{\r
3075 RETURN_STATUS Status;\r
3076\r
3077 if (Result == NULL) {\r
3078 return RETURN_INVALID_PARAMETER;\r
3079 }\r
3080\r
3081 if (Minuend >= Subtrahend) {\r
3082 *Result = (UINT16)(Minuend - Subtrahend);\r
2f88bd3a 3083 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
3084 } else {\r
3085 *Result = UINT16_ERROR;\r
2f88bd3a 3086 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
3087 }\r
3088\r
3089 return Status;\r
3090}\r
3091\r
3092/**\r
3093 UINT32 subtraction\r
3094\r
3095 Performs the requested operation using the input parameters into a value\r
3096 specified by Result type and stores the converted value into the caller\r
3097 allocated output buffer specified by Result. The caller must pass in a\r
3098 Result buffer that is at least as large as the Result type.\r
3099\r
3100 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3101\r
3102 If the requested operation results in an overflow or an underflow condition,\r
3103 then Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3104\r
3105 @param[in] Minuend A number from which another is to be subtracted.\r
3106 @param[in] Subtrahend A number to be subtracted from another\r
3107 @param[out] Result Pointer to the result of subtraction\r
3108\r
3109 @retval RETURN_SUCCESS Successful subtraction\r
3110 @retval RETURN_BUFFER_TOO_SMALL Underflow\r
3111 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3112**/\r
3113RETURN_STATUS\r
3114EFIAPI\r
3115SafeUint32Sub (\r
3116 IN UINT32 Minuend,\r
3117 IN UINT32 Subtrahend,\r
3118 OUT UINT32 *Result\r
3119 )\r
3120{\r
3121 RETURN_STATUS Status;\r
3122\r
3123 if (Result == NULL) {\r
3124 return RETURN_INVALID_PARAMETER;\r
3125 }\r
3126\r
3127 if (Minuend >= Subtrahend) {\r
3128 *Result = (Minuend - Subtrahend);\r
2f88bd3a 3129 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
3130 } else {\r
3131 *Result = UINT32_ERROR;\r
2f88bd3a 3132 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
3133 }\r
3134\r
3135 return Status;\r
3136}\r
3137\r
3138/**\r
3139 UINT64 subtraction\r
3140\r
3141 Performs the requested operation using the input parameters into a value\r
3142 specified by Result type and stores the converted value into the caller\r
3143 allocated output buffer specified by Result. The caller must pass in a\r
3144 Result buffer that is at least as large as the Result type.\r
3145\r
3146 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3147\r
3148 If the requested operation results in an overflow or an underflow condition,\r
3149 then Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3150\r
3151 @param[in] Minuend A number from which another is to be subtracted.\r
3152 @param[in] Subtrahend A number to be subtracted from another\r
3153 @param[out] Result Pointer to the result of subtraction\r
3154\r
3155 @retval RETURN_SUCCESS Successful subtraction\r
3156 @retval RETURN_BUFFER_TOO_SMALL Underflow\r
3157 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3158**/\r
3159RETURN_STATUS\r
3160EFIAPI\r
3161SafeUint64Sub (\r
3162 IN UINT64 Minuend,\r
3163 IN UINT64 Subtrahend,\r
3164 OUT UINT64 *Result\r
3165 )\r
3166{\r
3167 RETURN_STATUS Status;\r
3168\r
3169 if (Result == NULL) {\r
3170 return RETURN_INVALID_PARAMETER;\r
3171 }\r
3172\r
3173 if (Minuend >= Subtrahend) {\r
3174 *Result = (Minuend - Subtrahend);\r
2f88bd3a 3175 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
3176 } else {\r
3177 *Result = UINT64_ERROR;\r
2f88bd3a 3178 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
3179 }\r
3180\r
3181 return Status;\r
3182}\r
3183\r
3184//\r
3185// Multiplication functions\r
3186//\r
3187\r
3188/**\r
3189 UINT8 multiplication\r
3190\r
3191 Performs the requested operation using the input parameters into a value\r
3192 specified by Result type and stores the converted value into the caller\r
3193 allocated output buffer specified by Result. The caller must pass in a\r
3194 Result buffer that is at least as large as the Result type.\r
3195\r
3196 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3197\r
3198 If the requested operation results in an overflow or an underflow condition,\r
3199 then Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3200\r
3201 @param[in] Multiplicand A number that is to be multiplied by another\r
3202 @param[in] Multiplier A number by which the multiplicand is to be multiplied\r
3203 @param[out] Result Pointer to the result of multiplication\r
3204\r
3205 @retval RETURN_SUCCESS Successful multiplication\r
3206 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
3207 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3208**/\r
3209RETURN_STATUS\r
3210EFIAPI\r
3211SafeUint8Mult (\r
3212 IN UINT8 Multiplicand,\r
3213 IN UINT8 Multiplier,\r
3214 OUT UINT8 *Result\r
3215 )\r
3216{\r
3217 UINT32 IntermediateResult;\r
3218\r
3219 IntermediateResult = ((UINT32)Multiplicand) *((UINT32)Multiplier);\r
3220\r
3221 return SafeUint32ToUint8 (IntermediateResult, Result);\r
3222}\r
3223\r
3224/**\r
3225 UINT16 multiplication\r
3226\r
3227 Performs the requested operation using the input parameters into a value\r
3228 specified by Result type and stores the converted value into the caller\r
3229 allocated output buffer specified by Result. The caller must pass in a\r
3230 Result buffer that is at least as large as the Result type.\r
3231\r
3232 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3233\r
3234 If the requested operation results in an overflow or an underflow condition,\r
3235 then Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3236\r
3237 @param[in] Multiplicand A number that is to be multiplied by another\r
3238 @param[in] Multiplier A number by which the multiplicand is to be multiplied\r
3239 @param[out] Result Pointer to the result of multiplication\r
3240\r
3241 @retval RETURN_SUCCESS Successful multiplication\r
3242 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
3243 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3244**/\r
3245RETURN_STATUS\r
3246EFIAPI\r
3247SafeUint16Mult (\r
3248 IN UINT16 Multiplicand,\r
3249 IN UINT16 Multiplier,\r
3250 OUT UINT16 *Result\r
3251 )\r
3252{\r
3253 UINT32 IntermediateResult;\r
3254\r
3255 IntermediateResult = ((UINT32)Multiplicand) *((UINT32)Multiplier);\r
3256\r
3257 return SafeUint32ToUint16 (IntermediateResult, Result);\r
3258}\r
3259\r
3260/**\r
3261 UINT32 multiplication\r
3262\r
3263 Performs the requested operation using the input parameters into a value\r
3264 specified by Result type and stores the converted value into the caller\r
3265 allocated output buffer specified by Result. The caller must pass in a\r
3266 Result buffer that is at least as large as the Result type.\r
3267\r
3268 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3269\r
3270 If the requested operation results in an overflow or an underflow condition,\r
3271 then Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3272\r
3273 @param[in] Multiplicand A number that is to be multiplied by another\r
3274 @param[in] Multiplier A number by which the multiplicand is to be multiplied\r
3275 @param[out] Result Pointer to the result of multiplication\r
3276\r
3277 @retval RETURN_SUCCESS Successful multiplication\r
3278 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
3279 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3280**/\r
3281RETURN_STATUS\r
3282EFIAPI\r
3283SafeUint32Mult (\r
3284 IN UINT32 Multiplicand,\r
3285 IN UINT32 Multiplier,\r
3286 OUT UINT32 *Result\r
3287 )\r
3288{\r
3289 UINT64 IntermediateResult;\r
3290\r
2f88bd3a 3291 IntermediateResult = ((UINT64)Multiplicand) *((UINT64)Multiplier);\r
d7a09cb8
SB
3292\r
3293 return SafeUint64ToUint32 (IntermediateResult, Result);\r
3294}\r
3295\r
3296/**\r
3297 UINT64 multiplication\r
3298\r
3299 Performs the requested operation using the input parameters into a value\r
3300 specified by Result type and stores the converted value into the caller\r
3301 allocated output buffer specified by Result. The caller must pass in a\r
3302 Result buffer that is at least as large as the Result type.\r
3303\r
3304 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3305\r
3306 If the requested operation results in an overflow or an underflow condition,\r
3307 then Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3308\r
3309 @param[in] Multiplicand A number that is to be multiplied by another\r
3310 @param[in] Multiplier A number by which the multiplicand is to be multiplied\r
3311 @param[out] Result Pointer to the result of multiplication\r
3312\r
3313 @retval RETURN_SUCCESS Successful multiplication\r
3314 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
3315 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3316**/\r
3317RETURN_STATUS\r
3318EFIAPI\r
3319SafeUint64Mult (\r
3320 IN UINT64 Multiplicand,\r
3321 IN UINT64 Multiplier,\r
3322 OUT UINT64 *Result\r
3323 )\r
3324{\r
3325 RETURN_STATUS Status;\r
3326 UINT32 DwordA;\r
3327 UINT32 DwordB;\r
3328 UINT32 DwordC;\r
3329 UINT32 DwordD;\r
3330 UINT64 ProductAD;\r
3331 UINT64 ProductBC;\r
3332 UINT64 ProductBD;\r
3333 UINT64 UnsignedResult;\r
3334\r
3335 if (Result == NULL) {\r
3336 return RETURN_INVALID_PARAMETER;\r
3337 }\r
3338\r
2f88bd3a
MK
3339 ProductAD = 0;\r
3340 ProductBC = 0;\r
3341 ProductBD = 0;\r
d7a09cb8 3342 UnsignedResult = 0;\r
2f88bd3a 3343 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
3344\r
3345 //\r
3346 // 64x64 into 128 is like 32.32 x 32.32.\r
3347 //\r
3348 // a.b * c.d = a*(c.d) + .b*(c.d) = a*c + a*.d + .b*c + .b*.d\r
3349 // back in non-decimal notation where A=a*2^32 and C=c*2^32:\r
3350 // A*C + A*d + b*C + b*d\r
3351 // So there are four components to add together.\r
3352 // result = (a*c*2^64) + (a*d*2^32) + (b*c*2^32) + (b*d)\r
3353 //\r
3354 // a * c must be 0 or there would be bits in the high 64-bits\r
3355 // a * d must be less than 2^32 or there would be bits in the high 64-bits\r
3356 // b * c must be less than 2^32 or there would be bits in the high 64-bits\r
3357 // then there must be no overflow of the resulting values summed up.\r
3358 //\r
dc9b2a57
DB
3359 DwordA = (UINT32)RShiftU64 (Multiplicand, 32);\r
3360 DwordC = (UINT32)RShiftU64 (Multiplier, 32);\r
d7a09cb8
SB
3361\r
3362 //\r
3363 // common case -- if high dwords are both zero, no chance for overflow\r
3364 //\r
3365 if ((DwordA == 0) && (DwordC == 0)) {\r
3366 DwordB = (UINT32)Multiplicand;\r
3367 DwordD = (UINT32)Multiplier;\r
3368\r
3369 *Result = (((UINT64)DwordB) *(UINT64)DwordD);\r
2f88bd3a 3370 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
3371 } else {\r
3372 //\r
3373 // a * c must be 0 or there would be bits set in the high 64-bits\r
3374 //\r
3375 if ((DwordA == 0) ||\r
2f88bd3a
MK
3376 (DwordC == 0))\r
3377 {\r
d7a09cb8
SB
3378 DwordD = (UINT32)Multiplier;\r
3379\r
3380 //\r
3381 // a * d must be less than 2^32 or there would be bits set in the high 64-bits\r
3382 //\r
02b7b861 3383 ProductAD = MultU64x64 ((UINT64)DwordA, (UINT64)DwordD);\r
d7a09cb8
SB
3384 if ((ProductAD & 0xffffffff00000000) == 0) {\r
3385 DwordB = (UINT32)Multiplicand;\r
3386\r
3387 //\r
3388 // b * c must be less than 2^32 or there would be bits set in the high 64-bits\r
3389 //\r
02b7b861 3390 ProductBC = MultU64x64 ((UINT64)DwordB, (UINT64)DwordC);\r
d7a09cb8
SB
3391 if ((ProductBC & 0xffffffff00000000) == 0) {\r
3392 //\r
3393 // now sum them all up checking for overflow.\r
3394 // shifting is safe because we already checked for overflow above\r
3395 //\r
dc9b2a57 3396 if (!RETURN_ERROR (SafeUint64Add (LShiftU64 (ProductBC, 32), LShiftU64 (ProductAD, 32), &UnsignedResult))) {\r
d7a09cb8
SB
3397 //\r
3398 // b * d\r
3399 //\r
02b7b861 3400 ProductBD = MultU64x64 ((UINT64)DwordB, (UINT64)DwordD);\r
d7a09cb8
SB
3401\r
3402 if (!RETURN_ERROR (SafeUint64Add (UnsignedResult, ProductBD, &UnsignedResult))) {\r
3403 *Result = UnsignedResult;\r
2f88bd3a 3404 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
3405 }\r
3406 }\r
3407 }\r
3408 }\r
3409 }\r
3410 }\r
3411\r
3412 if (RETURN_ERROR (Status)) {\r
3413 *Result = UINT64_ERROR;\r
3414 }\r
2f88bd3a 3415\r
d7a09cb8
SB
3416 return Status;\r
3417}\r
3418\r
3419//\r
3420// Signed operations\r
3421//\r
3422// Strongly consider using unsigned numbers.\r
3423//\r
3424// Signed numbers are often used where unsigned numbers should be used.\r
3425// For example file sizes and array indices should always be unsigned.\r
3426// Subtracting a larger positive signed number from a smaller positive\r
3427// signed number with SafeInt32Sub will succeed, producing a negative number,\r
3428// that then must not be used as an array index (but can occasionally be\r
3429// used as a pointer index.) Similarly for adding a larger magnitude\r
3430// negative number to a smaller magnitude positive number.\r
3431//\r
3432// This library does not protect you from such errors. It tells you if your\r
3433// integer operations overflowed, not if you are doing the right thing\r
3434// with your non-overflowed integers.\r
3435//\r
3436// Likewise you can overflow a buffer with a non-overflowed unsigned index.\r
3437//\r
3438\r
3439//\r
3440// Signed addition functions\r
3441//\r
3442\r
3443/**\r
3444 INT8 Addition\r
3445\r
3446 Performs the requested operation using the input parameters into a value\r
3447 specified by Result type and stores the converted value into the caller\r
3448 allocated output buffer specified by Result. The caller must pass in a\r
3449 Result buffer that is at least as large as the Result type.\r
3450\r
3451 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3452\r
3453 If the requested operation results in an overflow or an underflow condition,\r
3454 then Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3455\r
3456 @param[in] Augend A number to which addend will be added\r
3457 @param[in] Addend A number to be added to another\r
3458 @param[out] Result Pointer to the result of addition\r
3459\r
3460 @retval RETURN_SUCCESS Successful addition\r
3461 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
3462 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3463**/\r
3464RETURN_STATUS\r
3465EFIAPI\r
3466SafeInt8Add (\r
3467 IN INT8 Augend,\r
3468 IN INT8 Addend,\r
3469 OUT INT8 *Result\r
3470 )\r
3471{\r
3472 return SafeInt32ToInt8 (((INT32)Augend) + ((INT32)Addend), Result);\r
3473}\r
3474\r
3475/**\r
3476 CHAR8 Addition\r
3477\r
3478 Performs the requested operation using the input parameters into a value\r
3479 specified by Result type and stores the converted value into the caller\r
3480 allocated output buffer specified by Result. The caller must pass in a\r
3481 Result buffer that is at least as large as the Result type.\r
3482\r
3483 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3484\r
3485 If the requested operation results in an overflow or an underflow condition,\r
3486 then Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3487\r
3488 @param[in] Augend A number to which addend will be added\r
3489 @param[in] Addend A number to be added to another\r
3490 @param[out] Result Pointer to the result of addition\r
3491\r
3492 @retval RETURN_SUCCESS Successful addition\r
3493 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
3494 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3495**/\r
3496RETURN_STATUS\r
3497EFIAPI\r
3498SafeChar8Add (\r
3499 IN CHAR8 Augend,\r
3500 IN CHAR8 Addend,\r
3501 OUT CHAR8 *Result\r
3502 )\r
3503{\r
3504 INT32 Augend32;\r
3505 INT32 Addend32;\r
3506\r
3507 if (Result == NULL) {\r
3508 return RETURN_INVALID_PARAMETER;\r
3509 }\r
3510\r
3511 Augend32 = (INT32)Augend;\r
3512 Addend32 = (INT32)Addend;\r
2f88bd3a 3513 if ((Augend32 < 0) || (Augend32 > MAX_INT8)) {\r
d7a09cb8
SB
3514 *Result = CHAR8_ERROR;\r
3515 return RETURN_BUFFER_TOO_SMALL;\r
3516 }\r
2f88bd3a
MK
3517\r
3518 if ((Addend32 < 0) || (Addend32 > MAX_INT8)) {\r
d7a09cb8
SB
3519 *Result = CHAR8_ERROR;\r
3520 return RETURN_BUFFER_TOO_SMALL;\r
3521 }\r
3522\r
3523 return SafeInt32ToChar8 (Augend32 + Addend32, Result);\r
3524}\r
3525\r
3526/**\r
3527 INT16 Addition\r
3528\r
3529 Performs the requested operation using the input parameters into a value\r
3530 specified by Result type and stores the converted value into the caller\r
3531 allocated output buffer specified by Result. The caller must pass in a\r
3532 Result buffer that is at least as large as the Result type.\r
3533\r
3534 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3535\r
3536 If the requested operation results in an overflow or an underflow condition,\r
3537 then Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3538\r
3539 @param[in] Augend A number to which addend will be added\r
3540 @param[in] Addend A number to be added to another\r
3541 @param[out] Result Pointer to the result of addition\r
3542\r
3543 @retval RETURN_SUCCESS Successful addition\r
3544 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
3545 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3546**/\r
3547RETURN_STATUS\r
3548EFIAPI\r
3549SafeInt16Add (\r
3550 IN INT16 Augend,\r
3551 IN INT16 Addend,\r
3552 OUT INT16 *Result\r
3553 )\r
3554{\r
3555 return SafeInt32ToInt16 (((INT32)Augend) + ((INT32)Addend), Result);\r
3556}\r
3557\r
3558/**\r
3559 INT32 Addition\r
3560\r
3561 Performs the requested operation using the input parameters into a value\r
3562 specified by Result type and stores the converted value into the caller\r
3563 allocated output buffer specified by Result. The caller must pass in a\r
3564 Result buffer that is at least as large as the Result type.\r
3565\r
3566 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3567\r
3568 If the requested operation results in an overflow or an underflow condition,\r
3569 then Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3570\r
3571 @param[in] Augend A number to which addend will be added\r
3572 @param[in] Addend A number to be added to another\r
3573 @param[out] Result Pointer to the result of addition\r
3574\r
3575 @retval RETURN_SUCCESS Successful addition\r
3576 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
3577 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3578**/\r
3579RETURN_STATUS\r
3580EFIAPI\r
3581SafeInt32Add (\r
3582 IN INT32 Augend,\r
3583 IN INT32 Addend,\r
3584 OUT INT32 *Result\r
3585 )\r
3586{\r
3587 return SafeInt64ToInt32 (((INT64)Augend) + ((INT64)Addend), Result);\r
3588}\r
3589\r
3590/**\r
3591 INT64 Addition\r
3592\r
3593 Performs the requested operation using the input parameters into a value\r
3594 specified by Result type and stores the converted value into the caller\r
3595 allocated output buffer specified by Result. The caller must pass in a\r
3596 Result buffer that is at least as large as the Result type.\r
3597\r
3598 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3599\r
3600 If the requested operation results in an overflow or an underflow condition,\r
3601 then Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3602\r
3603 @param[in] Augend A number to which addend will be added\r
3604 @param[in] Addend A number to be added to another\r
3605 @param[out] Result Pointer to the result of addition\r
3606\r
3607 @retval RETURN_SUCCESS Successful addition\r
3608 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
3609 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3610**/\r
3611RETURN_STATUS\r
3612EFIAPI\r
3613SafeInt64Add (\r
3614 IN INT64 Augend,\r
3615 IN INT64 Addend,\r
3616 OUT INT64 *Result\r
3617 )\r
3618{\r
3619 RETURN_STATUS Status;\r
d7a09cb8
SB
3620\r
3621 if (Result == NULL) {\r
3622 return RETURN_INVALID_PARAMETER;\r
3623 }\r
3624\r
d7a09cb8 3625 //\r
41bfaffd
LE
3626 // * An Addend of zero can never cause underflow or overflow.\r
3627 //\r
3628 // * A positive Addend can only cause overflow. The overflow condition is\r
3629 //\r
3630 // (Augend + Addend) > MAX_INT64\r
3631 //\r
3632 // Subtracting Addend from both sides yields\r
3633 //\r
3634 // Augend > (MAX_INT64 - Addend)\r
3635 //\r
3636 // This condition can be coded directly in C because the RHS will neither\r
3637 // underflow nor overflow. That is due to the starting condition:\r
3638 //\r
3639 // 0 < Addend <= MAX_INT64\r
3640 //\r
3641 // Multiplying all three sides by (-1) yields\r
3642 //\r
3643 // 0 > (-Addend) >= (-MAX_INT64)\r
3644 //\r
3645 // Adding MAX_INT64 to all three sides yields\r
3646 //\r
3647 // MAX_INT64 > (MAX_INT64 - Addend) >= 0\r
3648 //\r
3649 // * A negative Addend can only cause underflow. The underflow condition is\r
3650 //\r
3651 // (Augend + Addend) < MIN_INT64\r
3652 //\r
3653 // Subtracting Addend from both sides yields\r
3654 //\r
3655 // Augend < (MIN_INT64 - Addend)\r
3656 //\r
3657 // This condition can be coded directly in C because the RHS will neither\r
3658 // underflow nor overflow. That is due to the starting condition:\r
3659 //\r
3660 // MIN_INT64 <= Addend < 0\r
3661 //\r
3662 // Multiplying all three sides by (-1) yields\r
3663 //\r
3664 // (-MIN_INT64) >= (-Addend) > 0\r
3665 //\r
3666 // Adding MIN_INT64 to all three sides yields\r
3667 //\r
3668 // 0 >= (MIN_INT64 - Addend) > MIN_INT64\r
d7a09cb8 3669 //\r
41bfaffd 3670 if (((Addend > 0) && (Augend > (MAX_INT64 - Addend))) ||\r
2f88bd3a
MK
3671 ((Addend < 0) && (Augend < (MIN_INT64 - Addend))))\r
3672 {\r
d7a09cb8 3673 *Result = INT64_ERROR;\r
2f88bd3a 3674 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8 3675 } else {\r
41bfaffd 3676 *Result = Augend + Addend;\r
2f88bd3a 3677 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
3678 }\r
3679\r
3680 return Status;\r
3681}\r
3682\r
3683//\r
3684// Signed subtraction functions\r
3685//\r
3686\r
3687/**\r
3688 INT8 Subtraction\r
3689\r
3690 Performs the requested operation using the input parameters into a value\r
3691 specified by Result type and stores the converted value into the caller\r
3692 allocated output buffer specified by Result. The caller must pass in a\r
3693 Result buffer that is at least as large as the Result type.\r
3694\r
3695 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3696\r
3697 If the requested operation results in an overflow or an underflow condition,\r
3698 then Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3699\r
3700 @param[in] Minuend A number from which another is to be subtracted.\r
3701 @param[in] Subtrahend A number to be subtracted from another\r
3702 @param[out] Result Pointer to the result of subtraction\r
3703\r
3704 @retval RETURN_SUCCESS Successful subtraction\r
3705 @retval RETURN_BUFFER_TOO_SMALL Underflow\r
3706 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3707**/\r
3708RETURN_STATUS\r
3709EFIAPI\r
3710SafeInt8Sub (\r
3711 IN INT8 Minuend,\r
3712 IN INT8 Subtrahend,\r
3713 OUT INT8 *Result\r
3714 )\r
3715{\r
3716 return SafeInt32ToInt8 (((INT32)Minuend) - ((INT32)Subtrahend), Result);\r
3717}\r
3718\r
3719/**\r
3720 CHAR8 Subtraction\r
3721\r
3722 Performs the requested operation using the input parameters into a value\r
3723 specified by Result type and stores the converted value into the caller\r
3724 allocated output buffer specified by Result. The caller must pass in a\r
3725 Result buffer that is at least as large as the Result type.\r
3726\r
3727 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3728\r
3729 If the requested operation results in an overflow or an underflow condition,\r
3730 then Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3731\r
3732 @param[in] Minuend A number from which another is to be subtracted.\r
3733 @param[in] Subtrahend A number to be subtracted from another\r
3734 @param[out] Result Pointer to the result of subtraction\r
3735\r
3736 @retval RETURN_SUCCESS Successful subtraction\r
3737 @retval RETURN_BUFFER_TOO_SMALL Underflow\r
3738 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3739**/\r
3740RETURN_STATUS\r
3741EFIAPI\r
3742SafeChar8Sub (\r
3743 IN CHAR8 Minuend,\r
3744 IN CHAR8 Subtrahend,\r
3745 OUT CHAR8 *Result\r
3746 )\r
3747{\r
3748 INT32 Minuend32;\r
3749 INT32 Subtrahend32;\r
3750\r
3751 if (Result == NULL) {\r
3752 return RETURN_INVALID_PARAMETER;\r
3753 }\r
3754\r
3755 Minuend32 = (INT32)Minuend;\r
3756 Subtrahend32 = (INT32)Subtrahend;\r
2f88bd3a 3757 if ((Minuend32 < 0) || (Minuend32 > MAX_INT8)) {\r
d7a09cb8
SB
3758 *Result = CHAR8_ERROR;\r
3759 return RETURN_BUFFER_TOO_SMALL;\r
3760 }\r
2f88bd3a
MK
3761\r
3762 if ((Subtrahend32 < 0) || (Subtrahend32 > MAX_INT8)) {\r
d7a09cb8
SB
3763 *Result = CHAR8_ERROR;\r
3764 return RETURN_BUFFER_TOO_SMALL;\r
3765 }\r
3766\r
3767 return SafeInt32ToChar8 (Minuend32 - Subtrahend32, Result);\r
3768}\r
3769\r
3770/**\r
3771 INT16 Subtraction\r
3772\r
3773 Performs the requested operation using the input parameters into a value\r
3774 specified by Result type and stores the converted value into the caller\r
3775 allocated output buffer specified by Result. The caller must pass in a\r
3776 Result buffer that is at least as large as the Result type.\r
3777\r
3778 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3779\r
3780 If the requested operation results in an overflow or an underflow condition,\r
3781 then Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3782\r
3783 @param[in] Minuend A number from which another is to be subtracted.\r
3784 @param[in] Subtrahend A number to be subtracted from another\r
3785 @param[out] Result Pointer to the result of subtraction\r
3786\r
3787 @retval RETURN_SUCCESS Successful subtraction\r
3788 @retval RETURN_BUFFER_TOO_SMALL Underflow\r
3789 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3790**/\r
3791RETURN_STATUS\r
3792EFIAPI\r
3793SafeInt16Sub (\r
3794 IN INT16 Minuend,\r
3795 IN INT16 Subtrahend,\r
3796 OUT INT16 *Result\r
3797 )\r
3798{\r
3799 return SafeInt32ToInt16 (((INT32)Minuend) - ((INT32)Subtrahend), Result);\r
3800}\r
3801\r
3802/**\r
3803 INT32 Subtraction\r
3804\r
3805 Performs the requested operation using the input parameters into a value\r
3806 specified by Result type and stores the converted value into the caller\r
3807 allocated output buffer specified by Result. The caller must pass in a\r
3808 Result buffer that is at least as large as the Result type.\r
3809\r
3810 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3811\r
3812 If the requested operation results in an overflow or an underflow condition,\r
3813 then Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3814\r
3815 @param[in] Minuend A number from which another is to be subtracted.\r
3816 @param[in] Subtrahend A number to be subtracted from another\r
3817 @param[out] Result Pointer to the result of subtraction\r
3818\r
3819 @retval RETURN_SUCCESS Successful subtraction\r
3820 @retval RETURN_BUFFER_TOO_SMALL Underflow\r
3821 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3822**/\r
3823RETURN_STATUS\r
3824EFIAPI\r
3825SafeInt32Sub (\r
3826 IN INT32 Minuend,\r
3827 IN INT32 Subtrahend,\r
3828 OUT INT32 *Result\r
3829 )\r
3830{\r
3831 return SafeInt64ToInt32 (((INT64)Minuend) - ((INT64)Subtrahend), Result);\r
3832}\r
3833\r
3834/**\r
3835 INT64 Subtraction\r
3836\r
3837 Performs the requested operation using the input parameters into a value\r
3838 specified by Result type and stores the converted value into the caller\r
3839 allocated output buffer specified by Result. The caller must pass in a\r
3840 Result buffer that is at least as large as the Result type.\r
3841\r
3842 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3843\r
3844 If the requested operation results in an overflow or an underflow condition,\r
3845 then Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3846\r
3847 @param[in] Minuend A number from which another is to be subtracted.\r
3848 @param[in] Subtrahend A number to be subtracted from another\r
3849 @param[out] Result Pointer to the result of subtraction\r
3850\r
3851 @retval RETURN_SUCCESS Successful subtraction\r
3852 @retval RETURN_BUFFER_TOO_SMALL Underflow\r
3853 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3854**/\r
3855RETURN_STATUS\r
3856EFIAPI\r
3857SafeInt64Sub (\r
3858 IN INT64 Minuend,\r
3859 IN INT64 Subtrahend,\r
3860 OUT INT64 *Result\r
3861 )\r
3862{\r
3863 RETURN_STATUS Status;\r
d7a09cb8
SB
3864\r
3865 if (Result == NULL) {\r
3866 return RETURN_INVALID_PARAMETER;\r
3867 }\r
3868\r
d7a09cb8 3869 //\r
54c7728a
LE
3870 // * A Subtrahend of zero can never cause underflow or overflow.\r
3871 //\r
3872 // * A positive Subtrahend can only cause underflow. The underflow condition\r
3873 // is:\r
3874 //\r
3875 // (Minuend - Subtrahend) < MIN_INT64\r
3876 //\r
3877 // Adding Subtrahend to both sides yields\r
3878 //\r
3879 // Minuend < (MIN_INT64 + Subtrahend)\r
3880 //\r
3881 // This condition can be coded directly in C because the RHS will neither\r
3882 // underflow nor overflow. That is due to the starting condition:\r
3883 //\r
3884 // 0 < Subtrahend <= MAX_INT64\r
3885 //\r
3886 // Adding MIN_INT64 to all three sides yields\r
3887 //\r
3888 // MIN_INT64 < (MIN_INT64 + Subtrahend) <= (MIN_INT64 + MAX_INT64) = -1\r
d7a09cb8 3889 //\r
54c7728a
LE
3890 // * A negative Subtrahend can only cause overflow. The overflow condition is\r
3891 //\r
3892 // (Minuend - Subtrahend) > MAX_INT64\r
3893 //\r
3894 // Adding Subtrahend to both sides yields\r
3895 //\r
3896 // Minuend > (MAX_INT64 + Subtrahend)\r
3897 //\r
3898 // This condition can be coded directly in C because the RHS will neither\r
3899 // underflow nor overflow. That is due to the starting condition:\r
3900 //\r
3901 // MIN_INT64 <= Subtrahend < 0\r
3902 //\r
3903 // Adding MAX_INT64 to all three sides yields\r
3904 //\r
3905 // -1 = (MAX_INT64 + MIN_INT64) <= (MAX_INT64 + Subtrahend) < MAX_INT64\r
3906 //\r
3907 if (((Subtrahend > 0) && (Minuend < (MIN_INT64 + Subtrahend))) ||\r
2f88bd3a
MK
3908 ((Subtrahend < 0) && (Minuend > (MAX_INT64 + Subtrahend))))\r
3909 {\r
d7a09cb8 3910 *Result = INT64_ERROR;\r
2f88bd3a 3911 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8 3912 } else {\r
54c7728a 3913 *Result = Minuend - Subtrahend;\r
2f88bd3a 3914 Status = RETURN_SUCCESS;\r
d7a09cb8
SB
3915 }\r
3916\r
3917 return Status;\r
3918}\r
3919\r
3920//\r
3921// Signed multiplication functions\r
3922//\r
3923\r
3924/**\r
3925 INT8 multiplication\r
3926\r
3927 Performs the requested operation using the input parameters into a value\r
3928 specified by Result type and stores the converted value into the caller\r
3929 allocated output buffer specified by Result. The caller must pass in a\r
3930 Result buffer that is at least as large as the Result type.\r
3931\r
3932 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3933\r
3934 If the requested operation results in an overflow or an underflow condition,\r
3935 then Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3936\r
3937 @param[in] Multiplicand A number that is to be multiplied by another\r
3938 @param[in] Multiplier A number by which the multiplicand is to be multiplied\r
3939 @param[out] Result Pointer to the result of multiplication\r
3940\r
3941 @retval RETURN_SUCCESS Successful multiplication\r
3942 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
3943 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3944**/\r
3945RETURN_STATUS\r
3946EFIAPI\r
3947SafeInt8Mult (\r
3948 IN INT8 Multiplicand,\r
3949 IN INT8 Multiplier,\r
3950 OUT INT8 *Result\r
3951 )\r
3952{\r
3953 return SafeInt32ToInt8 (((INT32)Multiplier) *((INT32)Multiplicand), Result);\r
3954}\r
3955\r
3956/**\r
3957 CHAR8 multiplication\r
3958\r
3959 Performs the requested operation using the input parameters into a value\r
3960 specified by Result type and stores the converted value into the caller\r
3961 allocated output buffer specified by Result. The caller must pass in a\r
3962 Result buffer that is at least as large as the Result type.\r
3963\r
3964 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3965\r
3966 If the requested operation results in an overflow or an underflow condition,\r
3967 then Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3968\r
3969 @param[in] Multiplicand A number that is to be multiplied by another\r
3970 @param[in] Multiplier A number by which the multiplicand is to be multiplied\r
3971 @param[out] Result Pointer to the result of multiplication\r
3972\r
3973 @retval RETURN_SUCCESS Successful multiplication\r
3974 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
3975 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3976**/\r
3977RETURN_STATUS\r
3978EFIAPI\r
3979SafeChar8Mult (\r
3980 IN CHAR8 Multiplicand,\r
3981 IN CHAR8 Multiplier,\r
3982 OUT CHAR8 *Result\r
3983 )\r
3984{\r
3985 INT32 Multiplicand32;\r
3986 INT32 Multiplier32;\r
3987\r
3988 if (Result == NULL) {\r
3989 return RETURN_INVALID_PARAMETER;\r
3990 }\r
3991\r
3992 Multiplicand32 = (INT32)Multiplicand;\r
3993 Multiplier32 = (INT32)Multiplier;\r
2f88bd3a 3994 if ((Multiplicand32 < 0) || (Multiplicand32 > MAX_INT8)) {\r
d7a09cb8
SB
3995 *Result = CHAR8_ERROR;\r
3996 return RETURN_BUFFER_TOO_SMALL;\r
3997 }\r
2f88bd3a
MK
3998\r
3999 if ((Multiplier32 < 0) || (Multiplier32 > MAX_INT8)) {\r
d7a09cb8
SB
4000 *Result = CHAR8_ERROR;\r
4001 return RETURN_BUFFER_TOO_SMALL;\r
4002 }\r
4003\r
4004 return SafeInt32ToChar8 (Multiplicand32 * Multiplier32, Result);\r
4005}\r
4006\r
4007/**\r
4008 INT16 multiplication\r
4009\r
4010 Performs the requested operation using the input parameters into a value\r
4011 specified by Result type and stores the converted value into the caller\r
4012 allocated output buffer specified by Result. The caller must pass in a\r
4013 Result buffer that is at least as large as the Result type.\r
4014\r
4015 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
4016\r
4017 If the requested operation results in an overflow or an underflow condition,\r
4018 then Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
4019\r
4020 @param[in] Multiplicand A number that is to be multiplied by another\r
4021 @param[in] Multiplier A number by which the multiplicand is to be multiplied\r
4022 @param[out] Result Pointer to the result of multiplication\r
4023\r
4024 @retval RETURN_SUCCESS Successful multiplication\r
4025 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
4026 @retval RETURN_INVALID_PARAMETER Result is NULL\r
4027**/\r
4028RETURN_STATUS\r
4029EFIAPI\r
4030SafeInt16Mult (\r
4031 IN INT16 Multiplicand,\r
4032 IN INT16 Multiplier,\r
4033 OUT INT16 *Result\r
4034 )\r
4035{\r
4036 return SafeInt32ToInt16 (((INT32)Multiplicand) *((INT32)Multiplier), Result);\r
4037}\r
4038\r
4039/**\r
4040 INT32 multiplication\r
4041\r
4042 Performs the requested operation using the input parameters into a value\r
4043 specified by Result type and stores the converted value into the caller\r
4044 allocated output buffer specified by Result. The caller must pass in a\r
4045 Result buffer that is at least as large as the Result type.\r
4046\r
4047 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
4048\r
4049 If the requested operation results in an overflow or an underflow condition,\r
4050 then Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
4051\r
4052 @param[in] Multiplicand A number that is to be multiplied by another\r
4053 @param[in] Multiplier A number by which the multiplicand is to be multiplied\r
4054 @param[out] Result Pointer to the result of multiplication\r
4055\r
4056 @retval RETURN_SUCCESS Successful multiplication\r
4057 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
4058 @retval RETURN_INVALID_PARAMETER Result is NULL\r
4059**/\r
4060RETURN_STATUS\r
4061EFIAPI\r
4062SafeInt32Mult (\r
4063 IN INT32 Multiplicand,\r
4064 IN INT32 Multiplier,\r
4065 OUT INT32 *Result\r
4066 )\r
4067{\r
dc9b2a57 4068 return SafeInt64ToInt32 (MultS64x64 (Multiplicand, Multiplier), Result);\r
d7a09cb8
SB
4069}\r
4070\r
4071/**\r
4072 INT64 multiplication\r
4073\r
4074 Performs the requested operation using the input parameters into a value\r
4075 specified by Result type and stores the converted value into the caller\r
4076 allocated output buffer specified by Result. The caller must pass in a\r
4077 Result buffer that is at least as large as the Result type.\r
4078\r
4079 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
4080\r
4081 If the requested operation results in an overflow or an underflow condition,\r
4082 then Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
4083\r
4084 @param[in] Multiplicand A number that is to be multiplied by another\r
4085 @param[in] Multiplier A number by which the multiplicand is to be multiplied\r
4086 @param[out] Result Pointer to the result of multiplication\r
4087\r
4088 @retval RETURN_SUCCESS Successful multiplication\r
4089 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
4090 @retval RETURN_INVALID_PARAMETER Result is NULL\r
4091**/\r
4092RETURN_STATUS\r
4093EFIAPI\r
4094SafeInt64Mult (\r
4095 IN INT64 Multiplicand,\r
4096 IN INT64 Multiplier,\r
4097 OUT INT64 *Result\r
4098 )\r
4099{\r
4100 RETURN_STATUS Status;\r
4101 UINT64 UnsignedMultiplicand;\r
4102 UINT64 UnsignedMultiplier;\r
4103 UINT64 UnsignedResult;\r
4104\r
4105 if (Result == NULL) {\r
4106 return RETURN_INVALID_PARAMETER;\r
4107 }\r
4108\r
4109 //\r
4110 // Split into sign and magnitude, do unsigned operation, apply sign.\r
4111 //\r
4112 if (Multiplicand < 0) {\r
4113 //\r
4114 // Avoid negating the most negative number.\r
4115 //\r
2f88bd3a 4116 UnsignedMultiplicand = ((UINT64)(-(Multiplicand + 1))) + 1;\r
d7a09cb8
SB
4117 } else {\r
4118 UnsignedMultiplicand = (UINT64)Multiplicand;\r
4119 }\r
4120\r
4121 if (Multiplier < 0) {\r
4122 //\r
4123 // Avoid negating the most negative number.\r
4124 //\r
2f88bd3a 4125 UnsignedMultiplier = ((UINT64)(-(Multiplier + 1))) + 1;\r
d7a09cb8
SB
4126 } else {\r
4127 UnsignedMultiplier = (UINT64)Multiplier;\r
4128 }\r
4129\r
4130 Status = SafeUint64Mult (UnsignedMultiplicand, UnsignedMultiplier, &UnsignedResult);\r
4131 if (!RETURN_ERROR (Status)) {\r
4132 if ((Multiplicand < 0) != (Multiplier < 0)) {\r
4133 if (UnsignedResult > MIN_INT64_MAGNITUDE) {\r
4134 *Result = INT64_ERROR;\r
2f88bd3a 4135 Status = RETURN_BUFFER_TOO_SMALL;\r
75505d16
LE
4136 } else if (UnsignedResult == MIN_INT64_MAGNITUDE) {\r
4137 *Result = MIN_INT64;\r
d7a09cb8 4138 } else {\r
2f88bd3a 4139 *Result = -((INT64)UnsignedResult);\r
d7a09cb8
SB
4140 }\r
4141 } else {\r
4142 if (UnsignedResult > MAX_INT64) {\r
4143 *Result = INT64_ERROR;\r
2f88bd3a 4144 Status = RETURN_BUFFER_TOO_SMALL;\r
d7a09cb8
SB
4145 } else {\r
4146 *Result = (INT64)UnsignedResult;\r
4147 }\r
4148 }\r
4149 } else {\r
4150 *Result = INT64_ERROR;\r
4151 }\r
2f88bd3a 4152\r
d7a09cb8
SB
4153 return Status;\r
4154}\r