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