-
Notifications
You must be signed in to change notification settings - Fork 2
/
ntintsafe.h
8615 lines (7433 loc) · 156 KB
/
ntintsafe.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************
* *
* ntintsafe.h -- This module defines helper functions to prevent *
* integer overflow bugs for drivers. A similar *
* file, intsafe.h, is available for applications. *
* *
* Copyright (c) Microsoft Corp. All rights reserved. *
* *
******************************************************************/
#ifndef _NTINTSAFE_H_INCLUDED_
#define _NTINTSAFE_H_INCLUDED_
#include <winapifamily.h>
#if (_MSC_VER > 1000)
#pragma once
#endif
#pragma region Application Family or Games Family
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM | WINAPI_PARTITION_GAMES)
#include <specstrings.h> // for _In_, etc.
#if _MSC_VER >= 1200
#pragma warning(push)
#pragma warning(disable:4668) // #if not_defined treated as #if 0
#endif
#if !defined(_W64)
#if !defined(__midl) && (defined(_X86_) || defined(_M_IX86) || defined(_ARM_) || defined(_M_ARM)) && (_MSC_VER >= 1300)
#define _W64 __w64
#else
#define _W64
#endif
#endif
//
// typedefs
//
typedef char CHAR;
typedef signed char INT8;
typedef unsigned char UCHAR;
typedef unsigned char UINT8;
typedef unsigned char BYTE;
typedef short SHORT;
typedef signed short INT16;
typedef unsigned short USHORT;
typedef unsigned short UINT16;
typedef unsigned short WORD;
typedef int INT;
typedef signed int INT32;
typedef unsigned int UINT;
typedef unsigned int UINT32;
typedef long LONG;
typedef unsigned long ULONG;
typedef unsigned long DWORD;
typedef __int64 LONGLONG;
typedef __int64 LONG64;
typedef signed __int64 RtlINT64;
typedef unsigned __int64 ULONGLONG;
typedef unsigned __int64 DWORDLONG;
typedef unsigned __int64 ULONG64;
typedef unsigned __int64 DWORD64;
typedef unsigned __int64 UINT64;
#if (__midl > 501)
typedef [public] __int3264 INT_PTR;
typedef [public] unsigned __int3264 UINT_PTR;
typedef [public] __int3264 LONG_PTR;
typedef [public] unsigned __int3264 ULONG_PTR;
#else
#ifdef _WIN64
typedef __int64 INT_PTR;
typedef unsigned __int64 UINT_PTR;
typedef __int64 LONG_PTR;
typedef unsigned __int64 ULONG_PTR;
#else
typedef _W64 int INT_PTR;
typedef _W64 unsigned int UINT_PTR;
typedef _W64 long LONG_PTR;
typedef _W64 unsigned long ULONG_PTR;
#endif // WIN64
#endif // (__midl > 501)
#ifdef _WIN64
typedef __int64 ptrdiff_t;
typedef unsigned __int64 size_t;
#else
typedef _W64 int ptrdiff_t;
typedef _W64 unsigned int size_t;
#endif
typedef ULONG_PTR DWORD_PTR;
typedef LONG_PTR SSIZE_T;
typedef ULONG_PTR SIZE_T;
#undef _USE_INTRINSIC_MULTIPLY128
#if !defined(_M_CEE) && ((defined(_AMD64_) && !defined(_ARM64EC_)) || (defined(_IA64_) && (_MSC_VER >= 1400)))
#define _USE_INTRINSIC_MULTIPLY128
#endif
#if defined(_USE_INTRINSIC_MULTIPLY128)
#ifdef __cplusplus
extern "C" {
#endif
#if !defined(_ARM64EC_)
#define UnsignedMultiply128 _umul128
#else
#define _umul128 Multiply128
#endif // defined(_ARM64EC_)
ULONG64
UnsignedMultiply128(
_In_ ULONGLONG ullMultiplicand,
_In_ ULONGLONG ullMultiplier,
_Out_ _Deref_out_range_(==, ullMultiplicand * ullMultiplier) ULONGLONG* pullResultHigh);
#if !defined(_ARM64EC_)
#pragma intrinsic(_umul128)
#endif
#ifdef __cplusplus
}
#endif
#endif // _USE_INTRINSIC_MULTIPLY128
typedef _Return_type_success_(return >= 0) long NTSTATUS;
#define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)
#define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
#ifndef SORTPP_PASS
// compiletime asserts (failure results in error C2118: negative subscript)
#define C_ASSERT(e) typedef char __C_ASSERT__[(e)?1:-1]
#else
#define C_ASSERT(e)
#endif
//
// UInt32x32To64 macro
//
#ifndef UInt32x32To64
#if defined(MIDL_PASS) || defined(RC_INVOKED) || defined(_M_CEE_PURE) \
|| defined(_68K_) || defined(_MPPC_) \
|| defined(_M_IA64) || defined(_M_AMD64) || defined(_M_ARM) || defined(_M_ARM64) \
|| defined(_M_HYBRID_X86_ARM64)
#define UInt32x32To64(a, b) (((unsigned __int64)((unsigned int)(a))) * ((unsigned __int64)((unsigned int)(b))))
#elif defined(_M_IX86)
#define UInt32x32To64(a, b) ((unsigned __int64)(((unsigned __int64)((unsigned int)(a))) * ((unsigned int)(b))))
#else
#error Must define a target architecture.
#endif
#endif
//
// Min/Max type values
//
#define INT8_MIN (-127i8 - 1)
#define SHORT_MIN (-32768)
#define INT16_MIN (-32767i16 - 1)
#ifndef INT_MIN
#define INT_MIN (-2147483647 - 1)
#endif
#define INT32_MIN (-2147483647i32 - 1)
#ifndef LONG_MIN
#define LONG_MIN (-2147483647L - 1)
#endif
#define LONGLONG_MIN (-9223372036854775807i64 - 1)
#define LONG64_MIN (-9223372036854775807i64 - 1)
#define INT64_MIN (-9223372036854775807i64 - 1)
#define INT128_MIN (-170141183460469231731687303715884105727i128 - 1)
#ifdef _WIN64
#define INT_PTR_MIN (-9223372036854775807i64 - 1)
#define LONG_PTR_MIN (-9223372036854775807i64 - 1)
#define PTRDIFF_T_MIN (-9223372036854775807i64 - 1)
#define SSIZE_T_MIN (-9223372036854775807i64 - 1)
#else
#define INT_PTR_MIN (-2147483647 - 1)
#define LONG_PTR_MIN (-2147483647L - 1)
#define PTRDIFF_T_MIN (-2147483647 - 1)
#define SSIZE_T_MIN (-2147483647L - 1)
#endif
#define INT8_MAX 127i8
#define UINT8_MAX 0xffui8
#define BYTE_MAX 0xff
#define SHORT_MAX 32767
#define INT16_MAX 32767i16
#define USHORT_MAX 0xffff
#define UINT16_MAX 0xffffui16
#define WORD_MAX 0xffff
#ifndef INT_MAX
#define INT_MAX 2147483647
#endif
#define INT32_MAX 2147483647i32
#ifndef UINT_MAX
#define UINT_MAX 0xffffffff
#endif
#define UINT32_MAX 0xffffffffui32
#ifndef LONG_MAX
#define LONG_MAX 2147483647L
#endif
#ifndef ULONG_MAX
#define ULONG_MAX 0xffffffffUL
#endif
#define DWORD_MAX 0xffffffffUL
#define LONGLONG_MAX 9223372036854775807i64
#define LONG64_MAX 9223372036854775807i64
#define INT64_MAX 9223372036854775807i64
#define ULONGLONG_MAX 0xffffffffffffffffui64
#define DWORDLONG_MAX 0xffffffffffffffffui64
#define ULONG64_MAX 0xffffffffffffffffui64
#define DWORD64_MAX 0xffffffffffffffffui64
#define UINT64_MAX 0xffffffffffffffffui64
#define INT128_MAX 170141183460469231731687303715884105727i128
#define UINT128_MAX 0xffffffffffffffffffffffffffffffffui128
#undef SIZE_T_MAX
#ifdef _WIN64
#define INT_PTR_MAX 9223372036854775807i64
#define UINT_PTR_MAX 0xffffffffffffffffui64
#define LONG_PTR_MAX 9223372036854775807i64
#define ULONG_PTR_MAX 0xffffffffffffffffui64
#define DWORD_PTR_MAX 0xffffffffffffffffui64
#define PTRDIFF_T_MAX 9223372036854775807i64
#define SIZE_T_MAX 0xffffffffffffffffui64
#define SSIZE_T_MAX 9223372036854775807i64
#define _SIZE_T_MAX 0xffffffffffffffffui64
#else
#define INT_PTR_MAX 2147483647
#define UINT_PTR_MAX 0xffffffff
#define LONG_PTR_MAX 2147483647L
#define ULONG_PTR_MAX 0xffffffffUL
#define DWORD_PTR_MAX 0xffffffffUL
#define PTRDIFF_T_MAX 2147483647
#define SIZE_T_MAX 0xffffffff
#define SSIZE_T_MAX 2147483647L
#define _SIZE_T_MAX 0xffffffffUL
#endif
//
// It is common for -1 to be used as an error value
//
#define INT8_ERROR (-1i8)
#define UINT8_ERROR 0xffui8
#define BYTE_ERROR 0xff
#define SHORT_ERROR (-1)
#define INT16_ERROR (-1i16)
#define USHORT_ERROR 0xffff
#define UINT16_ERROR 0xffffui16
#define WORD_ERROR 0xffff
#define INT_ERROR (-1)
#define INT32_ERROR (-1i32)
#define UINT_ERROR 0xffffffff
#define UINT32_ERROR 0xffffffffui32
#define LONG_ERROR (-1L)
#define ULONG_ERROR 0xffffffffUL
#define DWORD_ERROR 0xffffffffUL
#define LONGLONG_ERROR (-1i64)
#define LONG64_ERROR (-1i64)
#define INT64_ERROR (-1i64)
#define ULONGLONG_ERROR 0xffffffffffffffffui64
#define DWORDLONG_ERROR 0xffffffffffffffffui64
#define ULONG64_ERROR 0xffffffffffffffffui64
#define UINT64_ERROR 0xffffffffffffffffui64
#ifdef _WIN64
#define INT_PTR_ERROR (-1i64)
#define UINT_PTR_ERROR 0xffffffffffffffffui64
#define LONG_PTR_ERROR (-1i64)
#define ULONG_PTR_ERROR 0xffffffffffffffffui64
#define DWORD_PTR_ERROR 0xffffffffffffffffui64
#define PTRDIFF_T_ERROR (-1i64)
#define SIZE_T_ERROR 0xffffffffffffffffui64
#define SSIZE_T_ERROR (-1i64)
#define _SIZE_T_ERROR 0xffffffffffffffffui64
#else
#define INT_PTR_ERROR (-1)
#define UINT_PTR_ERROR 0xffffffff
#define LONG_PTR_ERROR (-1L)
#define ULONG_PTR_ERROR 0xffffffffUL
#define DWORD_PTR_ERROR 0xffffffffUL
#define PTRDIFF_T_ERROR (-1)
#define SIZE_T_ERROR 0xffffffff
#define SSIZE_T_ERROR (-1L)
#define _SIZE_T_ERROR 0xffffffffUL
#endif
//
// We make some assumptions about the sizes of various types. Let's be
// explicit about those assumptions and check them.
//
C_ASSERT(sizeof(USHORT) == 2);
C_ASSERT(sizeof(INT) == 4);
C_ASSERT(sizeof(UINT) == 4);
C_ASSERT(sizeof(LONG) == 4);
C_ASSERT(sizeof(ULONG) == 4);
C_ASSERT(sizeof(UINT_PTR) == sizeof(ULONG_PTR));
//=============================================================================
// Conversion functions
//
// There are three reasons for having conversion functions:
//
// 1. We are converting from a signed type to an unsigned type of the same
// size, or vice-versa.
//
// Since we default to only having unsigned math functions,
// (see ENABLE_INTSAFE_SIGNED_FUNCTIONS below) we prefer people to convert
// to unsigned, do the math, and then convert back to signed.
//
// 2. We are converting to a smaller type, and we could therefore possibly
// overflow.
//
// 3. We are converting to a bigger type, and we are signed and the type we are
// converting to is unsigned.
//
//=============================================================================
//
// INT8 -> UCHAR conversion
//
_Must_inspect_result_
__inline
NTSTATUS
RtlInt8ToUChar(
_In_ INT8 i8Operand,
_Out_ _Deref_out_range_(==, i8Operand) UCHAR* pch)
{
NTSTATUS status;
if (i8Operand >= 0)
{
*pch = (UCHAR)i8Operand;
status = STATUS_SUCCESS;
}
else
{
*pch = '\0';
status = STATUS_INTEGER_OVERFLOW;
}
return status;
}
//
// INT8 -> UINT8 conversion
//
_Must_inspect_result_
__inline
NTSTATUS
RtlInt8ToUInt8(
_In_ INT8 i8Operand,
_Out_ _Deref_out_range_(==, i8Operand) UINT8* pu8Result)
{
NTSTATUS status;
if (i8Operand >= 0)
{
*pu8Result = (UINT8)i8Operand;
status = STATUS_SUCCESS;
}
else
{
*pu8Result = UINT8_ERROR;
status = STATUS_INTEGER_OVERFLOW;
}
return status;
}
//
// INT8 -> BYTE conversion
//
#define RtlInt8ToByte RtlInt8ToUInt8
//
// INT8 -> USHORT conversion
//
_Must_inspect_result_
__inline
NTSTATUS
RtlInt8ToUShort(
_In_ INT8 i8Operand,
_Out_ _Deref_out_range_(==, i8Operand) USHORT* pusResult)
{
NTSTATUS status;
if (i8Operand >= 0)
{
*pusResult = (USHORT)i8Operand;
status = STATUS_SUCCESS;
}
else
{
*pusResult = USHORT_ERROR;
status = STATUS_INTEGER_OVERFLOW;
}
return status;
}
//
// INT8 -> UINT16 conversion
//
#define RtlInt8ToUInt16 RtlInt8ToUShort
//
// INT8 -> WORD conversion
//
#define RtlInt8ToWord RtlInt8ToUShort
//
// INT8 -> UINT conversion
//
_Must_inspect_result_
__inline
NTSTATUS
RtlInt8ToUInt(
_In_ INT8 i8Operand,
_Out_ _Deref_out_range_(==, i8Operand) UINT* puResult)
{
NTSTATUS status;
if (i8Operand >= 0)
{
*puResult = (UINT)i8Operand;
status = STATUS_SUCCESS;
}
else
{
*puResult = UINT_ERROR;
status = STATUS_INTEGER_OVERFLOW;
}
return status;
}
//
// INT8 -> UINT32 conversion
//
#define RtlInt8ToUInt32 RtlInt8ToUInt
//
// INT8 -> UINT_PTR conversion
//
_Must_inspect_result_
__inline
NTSTATUS
RtlInt8ToUIntPtr(
_In_ INT8 i8Operand,
_Out_ _Deref_out_range_(==, i8Operand) UINT_PTR* puResult)
{
NTSTATUS status;
if (i8Operand >= 0)
{
*puResult = (UINT_PTR)i8Operand;
status = STATUS_SUCCESS;
}
else
{
*puResult = UINT_PTR_ERROR;
status = STATUS_INTEGER_OVERFLOW;
}
return status;
}
//
// INT8 -> ULONG conversion
//
_Must_inspect_result_
__inline
NTSTATUS
RtlInt8ToULong(
_In_ INT8 i8Operand,
_Out_ _Deref_out_range_(==, i8Operand) ULONG* pulResult)
{
NTSTATUS status;
if (i8Operand >= 0)
{
*pulResult = (ULONG)i8Operand;
status = STATUS_SUCCESS;
}
else
{
*pulResult = ULONG_ERROR;
status = STATUS_INTEGER_OVERFLOW;
}
return status;
}
//
// INT8 -> ULONG_PTR conversion
//
_Must_inspect_result_
__inline
NTSTATUS
RtlInt8ToULongPtr(
_In_ INT8 i8Operand,
_Out_ _Deref_out_range_(==, i8Operand) ULONG_PTR* pulResult)
{
NTSTATUS status;
if (i8Operand >= 0)
{
*pulResult = (ULONG_PTR)i8Operand;
status = STATUS_SUCCESS;
}
else
{
*pulResult = ULONG_PTR_ERROR;
status = STATUS_INTEGER_OVERFLOW;
}
return status;
}
//
// INT8 -> DWORD conversion
//
#define RtlInt8ToDWord RtlInt8ToULong
//
// INT8 -> DWORD_PTR conversion
//
#define RtlInt8ToDWordPtr RtlInt8ToULongPtr
//
// INT8 -> ULONGLONG conversion
//
_Must_inspect_result_
__inline
NTSTATUS
RtlInt8ToULongLong(
_In_ INT8 i8Operand,
_Out_ _Deref_out_range_(==, i8Operand) ULONGLONG* pullResult)
{
NTSTATUS status;
if (i8Operand >= 0)
{
*pullResult = (ULONGLONG)i8Operand;
status = STATUS_SUCCESS;
}
else
{
*pullResult = ULONGLONG_ERROR;
status = STATUS_INTEGER_OVERFLOW;
}
return status;
}
//
// INT8 -> DWORDLONG conversion
//
#define RtlInt8ToDWordLong RtlInt8ToULongLong
//
// INT8 -> ULONG64 conversion
//
#define RtlInt8ToULong64 RtlInt8ToULongLong
//
// INT8 -> DWORD64 conversion
//
#define RtlInt8ToDWord64 RtlInt8ToULongLong
//
// INT8 -> UINT64 conversion
//
#define RtlInt8ToUInt64 RtlInt8ToULongLong
//
// INT8 -> size_t conversion
//
#define RtlInt8ToSizeT RtlInt8ToUIntPtr
//
// INT8 -> SIZE_T conversion
//
#define RtlInt8ToSIZET RtlInt8ToULongPtr
//
// UINT8 -> INT8 conversion
//
_Must_inspect_result_
__inline
NTSTATUS
RtlUInt8ToInt8(
_In_ UINT8 u8Operand,
_Out_ _Deref_out_range_(==, u8Operand) INT8* pi8Result)
{
NTSTATUS status;
if (u8Operand <= INT8_MAX)
{
*pi8Result = (INT8)u8Operand;
status = STATUS_SUCCESS;
}
else
{
*pi8Result = INT8_ERROR;
status = STATUS_INTEGER_OVERFLOW;
}
return status;
}
//
// UINT8 -> CHAR conversion
//
__forceinline
NTSTATUS
RtlUInt8ToChar(
_In_ UINT8 u8Operand,
_Out_ _Deref_out_range_(==, u8Operand) CHAR* pch)
{
#ifdef _CHAR_UNSIGNED
*pch = (CHAR)u8Operand;
return STATUS_SUCCESS;
#else
return RtlUInt8ToInt8(u8Operand, (INT8*)pch);
#endif
}
//
// BYTE -> INT8 conversion
//
_Must_inspect_result_
__inline
NTSTATUS
RtlByteToInt8(
_In_ BYTE bOperand,
_Out_ _Deref_out_range_(==, bOperand) INT8* pi8Result)
{
NTSTATUS status;
if (bOperand <= INT8_MAX)
{
*pi8Result = (INT8)bOperand;
status = STATUS_SUCCESS;
}
else
{
*pi8Result = INT8_ERROR;
status = STATUS_INTEGER_OVERFLOW;
}
return status;
}
//
// BYTE -> CHAR conversion
//
__forceinline
NTSTATUS
RtlByteToChar(
_In_ BYTE bOperand,
_Out_ _Deref_out_range_(==, bOperand) CHAR* pch)
{
#ifdef _CHAR_UNSIGNED
*pch = (CHAR)bOperand;
return STATUS_SUCCESS;
#else
return RtlByteToInt8(bOperand, (INT8*)pch);
#endif
}
//
// SHORT -> INT8 conversion
//
_Must_inspect_result_
__inline
NTSTATUS
RtlShortToInt8(
_In_ SHORT sOperand,
_Out_ _Deref_out_range_(==, sOperand) INT8* pi8Result)
{
NTSTATUS status;
if ((sOperand >= INT8_MIN) && (sOperand <= INT8_MAX))
{
*pi8Result = (INT8)sOperand;
status = STATUS_SUCCESS;
}
else
{
*pi8Result = INT8_ERROR;
status = STATUS_INTEGER_OVERFLOW;
}
return status;
}
//
// SHORT -> UCHAR conversion
//
_Must_inspect_result_
__inline
NTSTATUS
RtlShortToUChar(
_In_ SHORT sOperand,
_Out_ _Deref_out_range_(==, sOperand) UCHAR* pch)
{
NTSTATUS status;
if ((sOperand >= 0) && (sOperand <= 255))
{
*pch = (UCHAR)sOperand;
status = STATUS_SUCCESS;
}
else
{
*pch = '\0';
status = STATUS_INTEGER_OVERFLOW;
}
return status;
}
//
// SHORT -> CHAR conversion
//
__forceinline
NTSTATUS
RtlShortToChar(
_In_ SHORT sOperand,
_Out_ _Deref_out_range_(==, sOperand) CHAR* pch)
{
#ifdef _CHAR_UNSIGNED
return RtlShortToUChar(sOperand, (UCHAR*)pch);
#else
return RtlShortToInt8(sOperand, (INT8*)pch);
#endif // _CHAR_UNSIGNED
}
//
// SHORT -> UINT8 conversion
//
_Must_inspect_result_
__inline
NTSTATUS
RtlShortToUInt8(
_In_ SHORT sOperand,
_Out_ _Deref_out_range_(==, sOperand) UINT8* pui8Result)
{
NTSTATUS status;
if ((sOperand >= 0) && (sOperand <= UINT8_MAX))
{
*pui8Result = (UINT8)sOperand;
status = STATUS_SUCCESS;
}
else
{
*pui8Result = UINT8_ERROR;
status = STATUS_INTEGER_OVERFLOW;
}
return status;
}
//
// SHORT -> BYTE conversion
//
#define RtlShortToByte RtlShortToUInt8
//
// SHORT -> USHORT conversion
//
_Must_inspect_result_
__inline
NTSTATUS
RtlShortToUShort(
_In_ SHORT sOperand,
_Out_ _Deref_out_range_(==, sOperand) USHORT* pusResult)
{
NTSTATUS status;
if (sOperand >= 0)
{
*pusResult = (USHORT)sOperand;
status = STATUS_SUCCESS;
}
else
{
*pusResult = USHORT_ERROR;
status = STATUS_INTEGER_OVERFLOW;
}
return status;
}
//
// SHORT -> UINT16 conversion
//
#define RtlShortToUInt16 RtlShortToUShort
//
// SHORT -> WORD conversion
//
#define RtlShortToWord RtlShortToUShort
//
// SHORT -> UINT conversion
//
_Must_inspect_result_
__inline
NTSTATUS
RtlShortToUInt(
_In_ SHORT sOperand,
_Out_ _Deref_out_range_(==, sOperand) UINT* puResult)
{
NTSTATUS status;
if (sOperand >= 0)
{
*puResult = (UINT)sOperand;
status = STATUS_SUCCESS;
}
else
{
*puResult = UINT_ERROR;
status = STATUS_INTEGER_OVERFLOW;
}
return status;
}
//
// SHORT -> UINT32 conversion
//
#define RtlShortToUInt32 RtlShortToUInt
//
// SHORT -> UINT_PTR conversion
//
_Must_inspect_result_
__inline
NTSTATUS
RtlShortToUIntPtr(
_In_ SHORT sOperand,
_Out_ _Deref_out_range_(==, sOperand) UINT_PTR* puResult)
{
NTSTATUS status;
if (sOperand >= 0)
{
*puResult = (UINT_PTR)sOperand;
status = STATUS_SUCCESS;
}
else
{
*puResult = UINT_PTR_ERROR;
status = STATUS_INTEGER_OVERFLOW;
}
return status;
}
//
// SHORT -> ULONG conversion
//
_Must_inspect_result_
__inline
NTSTATUS
RtlShortToULong(
_In_ SHORT sOperand,
_Out_ _Deref_out_range_(==, sOperand) ULONG* pulResult)
{
NTSTATUS status;
if (sOperand >= 0)
{
*pulResult = (ULONG)sOperand;
status = STATUS_SUCCESS;
}
else
{
*pulResult = ULONG_ERROR;
status = STATUS_INTEGER_OVERFLOW;
}
return status;
}
//
// SHORT -> ULONG_PTR conversion
//
_Must_inspect_result_
__inline
NTSTATUS
RtlShortToULongPtr(
_In_ SHORT sOperand,
_Out_ _Deref_out_range_(==, sOperand) ULONG_PTR* pulResult)
{
NTSTATUS status;
if (sOperand >= 0)
{
*pulResult = (ULONG_PTR)sOperand;
status = STATUS_SUCCESS;
}
else
{
*pulResult = ULONG_PTR_ERROR;
status = STATUS_INTEGER_OVERFLOW;
}
return status;
}
//
// SHORT -> DWORD conversion
//
#define RtlShortToDWord RtlShortToULong
//
// SHORT -> DWORD_PTR conversion
//
_Must_inspect_result_
__inline
NTSTATUS
RtlShortToDWordPtr(
_In_ SHORT sOperand,
_Out_ _Deref_out_range_(==, sOperand) DWORD_PTR* pdwResult)
{
NTSTATUS status;
if (sOperand >= 0)
{
*pdwResult = (DWORD_PTR)sOperand;
status = STATUS_SUCCESS;
}
else
{
*pdwResult = DWORD_PTR_ERROR;
status = STATUS_INTEGER_OVERFLOW;
}
return status;
}
//
// SHORT -> ULONGLONG conversion
//
_Must_inspect_result_
__inline
NTSTATUS
RtlShortToULongLong(
_In_ SHORT sOperand,
_Out_ _Deref_out_range_(==, sOperand) ULONGLONG* pullResult)
{
NTSTATUS status;
if (sOperand >= 0)
{
*pullResult = (ULONGLONG)sOperand;
status = STATUS_SUCCESS;
}
else
{
*pullResult = ULONGLONG_ERROR;
status = STATUS_INTEGER_OVERFLOW;
}
return status;
}
//
// SHORT -> DWORDLONG conversion
//
#define RtlShortToDWordLong RtlShortToULongLong
//
// SHORT -> ULONG64 conversion
//
#define RtlShortToULong64 RtlShortToULongLong
//