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