-
Notifications
You must be signed in to change notification settings - Fork 2
/
ntstrsafe.h
13072 lines (10347 loc) · 402 KB
/
ntstrsafe.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
/******************************************************************
* *
* ntstrsafe.h -- This module defines safer C library string *
* routine replacements for drivers. These are *
* meant to make C a bit more safe in reference *
* to security and robustness. A similar file, *
* strsafe.h, is available for applications. *
* *
* Copyright (c) Microsoft Corp. All rights reserved. *
* *
******************************************************************/
#ifndef _NTSTRSAFE_H_INCLUDED_
#define _NTSTRSAFE_H_INCLUDED_
#if (_MSC_VER > 1000)
#pragma once
#endif
#include <stdio.h> // for _vsnprintf, _vsnwprintf, getc, getwc
#include <string.h> // for memset
#include <stdarg.h> // for va_start, etc.
#include <specstrings.h> // for _In_, etc.
#include <winapifamily.h> // for WINAPI_FAMILY_PARTITION()
#ifndef NTSTRSAFE_NO_UNICODE_STRING_FUNCTIONS
#include <ntdef.h> // for UNICODE_STRING, etc.
#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
#if defined(_M_MRX000) || defined(_M_ALPHA) || defined(_M_PPC) || defined(_M_IA64) || defined(_M_AMD64) || defined(_M_ARM) || defined(_M_ARM64)
#define ALIGNMENT_MACHINE
#define UNALIGNED __unaligned
#if defined(_WIN64)
#define UNALIGNED64 __unaligned
#else
#define UNALIGNED64
#endif
#else
#undef ALIGNMENT_MACHINE
#define UNALIGNED
#define UNALIGNED64
#endif
// typedefs
#ifdef _WIN64
typedef unsigned __int64 size_t;
#else
typedef _W64 unsigned int size_t;
#endif
#ifndef _NTSTATUS_DEFINED
#define _NTSTATUS_DEFINED
typedef _Return_type_success_(return >= 0) long NTSTATUS;
#endif
typedef unsigned long DWORD;
#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
#ifdef __cplusplus
#define EXTERN_C extern "C"
#else
#define EXTERN_C extern
#endif
// use the new secure crt functions if available
#ifndef NTSTRSAFE_USE_SECURE_CRT
#if defined(__GOT_SECURE_LIB__) && (__GOT_SECURE_LIB__ >= 200402L)
#define NTSTRSAFE_USE_SECURE_CRT 0
#else
#define NTSTRSAFE_USE_SECURE_CRT 0
#endif
#endif // !NTSTRSAFE_USE_SECURE_CRT
#ifdef _M_CEE_PURE
#define NTSTRSAFEDDI __inline NTSTATUS __clrcall
#else
#define NTSTRSAFEDDI __inline NTSTATUS __stdcall
#endif
#if defined(NTSTRSAFE_LIB_IMPL) || defined(NTSTRSAFE_LIB)
#define NTSTRSAFEWORKERDDI EXTERN_C NTSTATUS __stdcall
#else
#define NTSTRSAFEWORKERDDI static NTSTRSAFEDDI
#endif
// The following steps are *REQUIRED* if ntstrsafe.h is used for drivers on:
// Windows 2000
// Windows Millennium Edition
// Windows 98 Second Edition
// Windows 98
//
// 1. #define NTSTRSAFE_LIB before including the ntstrsafe.h header file.
// 2. Add ntstrsafe.lib to the TARGET_LIBS line in SOURCES
//
// Drivers running on XP and later can skip these steps to create a smaller
// driver by running the functions inline.
#if defined(NTSTRSAFE_LIB)
#pragma comment(lib, "ntstrsafe.lib")
#endif
#pragma warning(push)
#pragma warning(disable: 28210) // Because not all PREFast versions like _Always_ equally.
// The user can request no "Cb" or no "Cch" functions, but not both
#if defined(NTSTRSAFE_NO_CB_FUNCTIONS) && defined(NTSTRSAFE_NO_CCH_FUNCTIONS)
#error cannot specify both NTSTRSAFE_NO_CB_FUNCTIONS and NTSTRSAFE_NO_CCH_FUNCTIONS !!
#endif
// The user may override NTSTRSAFE_MAX_CCH, but it must always be less than INT_MAX
#ifndef NTSTRSAFE_MAX_CCH
#define NTSTRSAFE_MAX_CCH 2147483647 // max buffer size, in characters, that we support (same as INT_MAX)
#endif
C_ASSERT(NTSTRSAFE_MAX_CCH <= 2147483647);
C_ASSERT(NTSTRSAFE_MAX_CCH > 1);
#define NTSTRSAFE_MAX_LENGTH (NTSTRSAFE_MAX_CCH - 1) // max buffer length, in characters, that we support
// The user may override NTSTRSAFE_UNICODE_STRING_MAX_CCH, but it must always be less than (USHORT_MAX / sizeof(wchar_t))
#ifndef NTSTRSAFE_UNICODE_STRING_MAX_CCH
#define NTSTRSAFE_UNICODE_STRING_MAX_CCH (0xffff / sizeof(wchar_t)) // max buffer size, in characters, for a UNICODE_STRING
#endif
C_ASSERT(NTSTRSAFE_UNICODE_STRING_MAX_CCH <= (0xffff / sizeof(wchar_t)));
C_ASSERT(NTSTRSAFE_UNICODE_STRING_MAX_CCH > 1);
// Flags for controlling the Ex functions
//
// STRSAFE_FILL_BYTE(0xFF) 0x000000FF // bottom byte specifies fill pattern
#define STRSAFE_IGNORE_NULLS 0x00000100 // treat null string pointers as TEXT("") -- don't fault on NULL buffers
#define STRSAFE_FILL_BEHIND_NULL 0x00000200 // on success, fill in extra space behind the null terminator with fill pattern
#define STRSAFE_FILL_ON_FAILURE 0x00000400 // on failure, overwrite pszDest with fill pattern and null terminate it
#define STRSAFE_NULL_ON_FAILURE 0x00000800 // on failure, set *pszDest = TEXT('\0')
#define STRSAFE_NO_TRUNCATION 0x00001000 // instead of returning a truncated result, copy/append nothing to pszDest and null terminate it
// Flags for controlling UNICODE_STRING Ex functions
//
// STRSAFE_FILL_BYTE(0xFF) 0x000000FF // bottom byte specifies fill pattern
// STRSAFE_IGNORE_NULLS 0x00000100 // don't fault on NULL UNICODE_STRING pointers, and treat null pszSrc as L""
#define STRSAFE_FILL_BEHIND 0x00000200 // on success, fill in extra space at the end of the UNICODE_STRING Buffer with fill pattern
// STRSAFE_FILL_ON_FAILURE 0x00000400 // on failure, fill the UNICODE_STRING Buffer with fill pattern and set the Length to 0
#define STRSAFE_ZERO_LENGTH_ON_FAILURE 0x00000800 // on failure, set the UNICODE_STRING Length to 0
// STRSAFE_NO_TRUNCATION 0x00001000 // instead of returning a truncated result, copy/append nothing to UNICODE_STRING Buffer
#define STRSAFE_VALID_FLAGS (0x000000FF | STRSAFE_IGNORE_NULLS | STRSAFE_FILL_BEHIND_NULL | STRSAFE_FILL_ON_FAILURE | STRSAFE_NULL_ON_FAILURE | STRSAFE_NO_TRUNCATION)
#define STRSAFE_UNICODE_STRING_VALID_FLAGS (0x000000FF | STRSAFE_IGNORE_NULLS | STRSAFE_FILL_BEHIND | STRSAFE_FILL_ON_FAILURE | STRSAFE_ZERO_LENGTH_ON_FAILURE | STRSAFE_NO_TRUNCATION)
// helper macro to set the fill character and specify buffer filling
#define STRSAFE_FILL_BYTE(x) ((DWORD)((x & 0x000000FF) | STRSAFE_FILL_BEHIND_NULL))
#define STRSAFE_FAILURE_BYTE(x) ((DWORD)((x & 0x000000FF) | STRSAFE_FILL_ON_FAILURE))
#define STRSAFE_GET_FILL_PATTERN(dwFlags) ((int)(dwFlags & 0x000000FF))
// Deprecated, use the non STRSAFE_ prefixed types instead (e.g. LPSTR or PSTR) as they are the same as these.
typedef _Null_terminated_ char* NTSTRSAFE_PSTR;
typedef _Null_terminated_ const char* NTSTRSAFE_PCSTR;
typedef _Null_terminated_ wchar_t* NTSTRSAFE_PWSTR;
typedef _Null_terminated_ const wchar_t* NTSTRSAFE_PCWSTR;
typedef _Null_terminated_ const wchar_t UNALIGNED* NTSTRSAFE_PCUWSTR;
// Deprecated, use the base types instead.
// RtlStrings where the string is NOT guaranteed to be null terminated (does not have _Null_terminated_).
typedef const char* STRSAFE_PCNZCH;
typedef const wchar_t* STRSAFE_PCNZWCH;
typedef const wchar_t UNALIGNED* STRSAFE_PCUNZWCH;
// prototypes for the worker functions
NTSTRSAFEWORKERDDI
RtlStringLengthWorkerA(
_In_reads_or_z_(cchMax) STRSAFE_PCNZCH psz,
_In_ _In_range_(<=, NTSTRSAFE_MAX_CCH) size_t cchMax,
_Out_opt_ _Deref_out_range_(<, cchMax) _Deref_out_range_(<=, _String_length_(psz)) size_t* pcchLength);
NTSTRSAFEWORKERDDI
RtlStringLengthWorkerW(
_In_reads_or_z_(cchMax) STRSAFE_PCNZWCH psz,
_In_ _In_range_(<=, NTSTRSAFE_MAX_CCH) size_t cchMax,
_Out_opt_ _Deref_out_range_(<, cchMax) _Deref_out_range_(<=, _String_length_(psz)) size_t* pcchLength);
#ifdef ALIGNMENT_MACHINE
NTSTRSAFEWORKERDDI
RtlUnalignedStringLengthWorkerW(
_In_reads_or_z_(cchMax) STRSAFE_PCUNZWCH psz,
_In_ _In_range_(<=, NTSTRSAFE_MAX_CCH) size_t cchMax,
_Out_opt_ _Deref_out_range_(<, cchMax) size_t* pcchLength);
#endif // ALIGNMENT_MACHINE
_When_(_Old_(*ppszSrc) != NULL, _Unchanged_(*ppszSrc))
_When_(_Old_(*ppszSrc) == NULL, _At_(*ppszSrc, _Post_z_))
NTSTRSAFEWORKERDDI
RtlStringExValidateSrcA(
_Inout_ _Deref_post_notnull_ STRSAFE_PCNZCH* ppszSrc,
_Inout_opt_
_Deref_out_range_(<, cchMax)
_Deref_out_range_(<=, _Old_(*pcchToRead)) size_t* pcchToRead,
_In_ const size_t cchMax,
_In_ DWORD dwFlags);
_When_(_Old_(*ppszSrc) != NULL, _Unchanged_(*ppszSrc))
_When_(_Old_(*ppszSrc) == NULL, _At_(*ppszSrc, _Post_z_))
NTSTRSAFEWORKERDDI
RtlStringExValidateSrcW(
_Inout_ _Deref_post_notnull_ STRSAFE_PCNZWCH* ppszSrc,
_Inout_opt_
_Deref_out_range_(<, cchMax)
_Deref_out_range_(<=, _Old_(*pcchToRead)) size_t* pcchToRead,
_In_ const size_t cchMax,
_In_ DWORD dwFlags);
_Post_satisfies_(cchDest > 0 && cchDest <= cchMax)
NTSTRSAFEWORKERDDI
RtlStringValidateDestA(
_In_reads_opt_(cchDest) STRSAFE_PCNZCH pszDest,
_In_ size_t cchDest,
_In_ const size_t cchMax);
_Post_satisfies_(cchDest > 0 && cchDest <= cchMax)
NTSTRSAFEWORKERDDI
RtlStringValidateDestAndLengthA(
_In_reads_opt_(cchDest) NTSTRSAFE_PCSTR pszDest,
_In_ size_t cchDest,
_Out_ _Deref_out_range_(0, cchDest - 1) size_t* pcchDestLength,
_In_ const size_t cchMax);
_Post_satisfies_(cchDest > 0 && cchDest <= cchMax)
NTSTRSAFEWORKERDDI
RtlStringValidateDestW(
_In_reads_opt_(cchDest) STRSAFE_PCNZWCH pszDest,
_In_ size_t cchDest,
_In_ const size_t cchMax);
_Post_satisfies_(cchDest > 0 && cchDest <= cchMax)
NTSTRSAFEWORKERDDI
RtlStringValidateDestAndLengthW(
_In_reads_opt_(cchDest) NTSTRSAFE_PCWSTR pszDest,
_In_ size_t cchDest,
_Out_ _Deref_out_range_(0, cchDest - 1) size_t* pcchDestLength,
_In_ const size_t cchMax);
NTSTRSAFEWORKERDDI
RtlStringExValidateDestA(
_In_reads_opt_(cchDest) STRSAFE_PCNZCH pszDest,
_In_ size_t cchDest,
_In_ const size_t cchMax,
_In_ DWORD dwFlags);
NTSTRSAFEWORKERDDI
RtlStringExValidateDestAndLengthA(
_In_reads_opt_(cchDest) NTSTRSAFE_PCSTR pszDest,
_In_ size_t cchDest,
_Out_ _Deref_out_range_(0, (cchDest>0?cchDest-1:0)) size_t* pcchDestLength,
_In_ const size_t cchMax,
_In_ DWORD dwFlags);
NTSTRSAFEWORKERDDI
RtlStringExValidateDestW(
_In_reads_opt_(cchDest) STRSAFE_PCNZWCH pszDest,
_In_ size_t cchDest,
_In_ const size_t cchMax,
_In_ DWORD dwFlags);
NTSTRSAFEWORKERDDI
RtlStringExValidateDestAndLengthW(
_In_reads_opt_(cchDest) NTSTRSAFE_PCWSTR pszDest,
_In_ size_t cchDest,
_Out_ _Deref_out_range_(0, (cchDest>0?cchDest-1:0)) size_t* pcchDestLength,
_In_ const size_t cchMax,
_In_ DWORD dwFlags);
NTSTRSAFEWORKERDDI
RtlStringCopyWorkerA(
_Out_writes_(cchDest) _Always_(_Post_z_) NTSTRSAFE_PSTR pszDest,
_In_ _In_range_(1, NTSTRSAFE_MAX_CCH) size_t cchDest,
_Always_(_Out_opt_ _Deref_out_range_(<=, (cchToCopy < cchDest) ? cchToCopy : (cchDest - 1))) size_t* pcchNewDestLength,
_In_reads_or_z_(cchToCopy) STRSAFE_PCNZCH pszSrc,
_In_ _In_range_(<, NTSTRSAFE_MAX_CCH) size_t cchToCopy);
NTSTRSAFEWORKERDDI
RtlStringCopyWorkerW(
_Out_writes_(cchDest) _Always_(_Post_z_) NTSTRSAFE_PWSTR pszDest,
_In_ _In_range_(1, NTSTRSAFE_MAX_CCH) size_t cchDest,
_Always_(_Out_opt_ _Deref_out_range_(<=, (cchToCopy < cchDest) ? cchToCopy : (cchDest - 1))) size_t* pcchNewDestLength,
_In_reads_or_z_(cchToCopy) STRSAFE_PCNZWCH pszSrc,
_In_ _In_range_(<, NTSTRSAFE_MAX_CCH) size_t cchToCopy);
NTSTRSAFEWORKERDDI
RtlStringVPrintfWorkerA(
_Out_writes_(cchDest) _Always_(_Post_z_) NTSTRSAFE_PSTR pszDest,
_In_ _In_range_(1, NTSTRSAFE_MAX_CCH) size_t cchDest,
_Always_(_Out_opt_ _Deref_out_range_(<=, cchDest - 1)) size_t* pcchNewDestLength,
_In_ _Printf_format_string_ NTSTRSAFE_PCSTR pszFormat,
_In_ va_list argList);
NTSTRSAFEWORKERDDI
RtlStringVPrintfWorkerW(
_Out_writes_(cchDest) _Always_(_Post_z_) NTSTRSAFE_PWSTR pszDest,
_In_ _In_range_(1, NTSTRSAFE_MAX_CCH) size_t cchDest,
_Always_(_Out_opt_ _Deref_out_range_(<=, cchDest - 1)) size_t* pcchNewDestLength,
_In_ _Printf_format_string_ NTSTRSAFE_PCWSTR pszFormat,
_In_ va_list argList);
NTSTRSAFEWORKERDDI
RtlStringExHandleFillBehindNullA(
_Inout_updates_bytes_(cbRemaining) NTSTRSAFE_PSTR pszDestEnd,
_In_ size_t cbRemaining,
_In_ DWORD dwFlags);
NTSTRSAFEWORKERDDI
RtlStringExHandleFillBehindNullW(
_Inout_updates_bytes_(cbRemaining) NTSTRSAFE_PWSTR pszDestEnd,
_In_ size_t cbRemaining,
_In_ DWORD dwFlags);
_Success_(1) // always succeeds, no exit tests needed
NTSTRSAFEWORKERDDI
RtlStringExHandleOtherFlagsA(
_Out_writes_bytes_(cbDest) NTSTRSAFE_PSTR pszDest,
_In_ _In_range_(1, NTSTRSAFE_MAX_CCH * sizeof(char)) size_t cbDest,
_In_ _In_range_(0, cbDest>sizeof(char)?(cbDest / sizeof(char)) - 1:0) size_t cchOriginalDestLength,
_Outptr_result_buffer_(*pcchRemaining) NTSTRSAFE_PSTR* ppszDestEnd,
_Out_ _Deref_out_range_(0, cbDest / sizeof(char)) size_t* pcchRemaining,
_In_ DWORD dwFlags);
_Success_(1) // always succeeds, no exit tests needed
NTSTRSAFEWORKERDDI
RtlStringExHandleOtherFlagsW(
_Out_writes_bytes_(cbDest) NTSTRSAFE_PWSTR pszDest,
_In_ _In_range_(1, NTSTRSAFE_MAX_CCH * sizeof(wchar_t)) size_t cbDest,
_In_ _In_range_(0, cbDest>sizeof(wchar_t)?(cbDest / sizeof(wchar_t)) - 1:0) size_t cchOriginalDestLength,
_Outptr_result_buffer_(*pcchRemaining) NTSTRSAFE_PWSTR* ppszDestEnd,
_Out_ _Deref_out_range_(0, cbDest / sizeof(wchar_t)) size_t* pcchRemaining,
_In_ DWORD dwFlags);
#ifndef NTSTRSAFE_NO_UNICODE_STRING_FUNCTIONS
_At_(DestinationString->Buffer, _Post_equal_to_(pszSrc))
_At_(DestinationString->Length, _Post_equal_to_(_String_length_(pszSrc) * sizeof(WCHAR)))
_At_(DestinationString->MaximumLength, _Post_equal_to_((_String_length_(pszSrc)+1) * sizeof(WCHAR)))
NTSTRSAFEWORKERDDI
RtlUnicodeStringInitWorker(
_Out_ PUNICODE_STRING DestinationString,
_In_opt_ NTSTRSAFE_PCWSTR pszSrc,
_In_ const size_t cchMax,
_In_ DWORD dwFlags);
NTSTRSAFEWORKERDDI
RtlUnicodeStringValidateWorker(
_In_opt_ PCUNICODE_STRING SourceString,
_In_ const size_t cchMax,
_In_ DWORD dwFlags);
_Post_satisfies_(*pcchSrcLength*sizeof(wchar_t) == SourceString->MaximumLength)
NTSTRSAFEWORKERDDI
RtlUnicodeStringValidateSrcWorker(
_In_ PCUNICODE_STRING SourceString,
_Outptr_result_buffer_(*pcchSrcLength) wchar_t** ppszSrc,
_Out_ size_t* pcchSrcLength,
_In_ const size_t cchMax,
_In_ DWORD dwFlags);
_Post_satisfies_(*pcchDest*sizeof(wchar_t) == DestinationString->MaximumLength)
NTSTRSAFEWORKERDDI
RtlUnicodeStringValidateDestWorker(
_In_ PCUNICODE_STRING DestinationString,
_Outptr_result_buffer_(*pcchDest) wchar_t** ppszDest,
_Out_ size_t* pcchDest,
_Out_opt_ size_t* pcchDestLength,
_In_ const size_t cchMax,
_In_ DWORD dwFlags);
NTSTRSAFEWORKERDDI
RtlStringCopyWideCharArrayWorker(
_Out_writes_(cchDest) NTSTRSAFE_PWSTR pszDest,
_In_ size_t cchDest,
_Out_opt_ size_t* pcchNewDestLength,
_In_reads_(cchSrcLength) const wchar_t* pszSrc,
_In_ size_t cchSrcLength);
NTSTRSAFEWORKERDDI
RtlWideCharArrayCopyStringWorker(
_Out_writes_to_(cchDest, *pcchNewDestLength) wchar_t* pszDest,
_In_ size_t cchDest,
_Out_ size_t* pcchNewDestLength,
_In_ NTSTRSAFE_PCWSTR pszSrc,
_In_ size_t cchToCopy);
NTSTRSAFEWORKERDDI
RtlWideCharArrayCopyWorker(
_Out_writes_to_(cchDest, *pcchNewDestLength) wchar_t* pszDest,
_In_ size_t cchDest,
_Out_ size_t* pcchNewDestLength,
_In_reads_(cchSrcLength) const wchar_t* pszSrc,
_In_ size_t cchSrcLength);
NTSTRSAFEWORKERDDI
RtlWideCharArrayVPrintfWorker(
_Out_writes_to_(cchDest, *pcchNewDestLength) wchar_t* pszDest,
_In_ size_t cchDest,
_Out_ size_t* pcchNewDestLength,
_In_ _Printf_format_string_ NTSTRSAFE_PCWSTR pszFormat,
_In_ va_list argList);
NTSTRSAFEWORKERDDI
RtlUnicodeStringExHandleFill(
_Out_writes_(cchRemaining) wchar_t* pszDestEnd,
_In_ size_t cchRemaining,
_In_ DWORD dwFlags);
NTSTRSAFEWORKERDDI
RtlUnicodeStringExHandleOtherFlags(
_Inout_updates_(cchDest) wchar_t* pszDest,
_In_ size_t cchDest,
_In_ size_t cchOriginalDestLength,
_Out_ size_t* pcchNewDestLength,
_Outptr_result_buffer_(*pcchRemaining) wchar_t** ppszDestEnd,
_Out_ size_t* pcchRemaining,
_In_ DWORD dwFlags);
#endif // !NTSTRSAFE_NO_UNICODE_STRING_FUNCTIONS
// To allow this to stand alone.
#define __WARNING_CYCLOMATIC_COMPLEXITY 28734
#define __WARNING_USING_UNINIT_VAR 6001
#define __WARNING_RETURN_UNINIT_VAR 6101
#define __WARNING_DEREF_NULL_PTR 6011
#define __WARNING_MISSING_ZERO_TERMINATION2 6054
#define __WARNING_INVALID_PARAM_VALUE_1 6387
#define __WARNING_INCORRECT_ANNOTATION 26007
#define __WARNING_POTENTIAL_BUFFER_OVERFLOW_HIGH_PRIORITY 26015
#define __WARNING_PRECONDITION_NULLTERMINATION_VIOLATION 26035
#define __WARNING_POSTCONDITION_NULLTERMINATION_VIOLATION 26036
#define __WARNING_HIGH_PRIORITY_OVERFLOW_POSTCONDITION 26045
#define __WARNING_RANGE_POSTCONDITION_VIOLATION 26061
#define __WARNING_POTENTIAL_RANGE_POSTCONDITION_VIOLATION 26071
#define __WARNING_INVALID_PARAM_VALUE_3 28183
#define __WARNING_RETURNING_BAD_RESULT 28196
#define __WARNING_BANNED_API_USAGE 28719
#define __WARNING_POST_EXPECTED 28210
#pragma warning(push)
#if _MSC_VER <= 1400
#pragma warning(disable: 4616) // turn off warning out of range so prefast pragmas won't show
// show up in build.wrn/build.err
#endif
#pragma warning(disable : 4996) // 'function': was declared deprecated
#pragma warning(disable : 4995) // name was marked as #pragma deprecated
#pragma warning(disable : 4793) // vararg causes native code generation
#pragma warning(disable : __WARNING_CYCLOMATIC_COMPLEXITY)
#ifndef NTSTRSAFE_LIB_IMPL
#ifndef NTSTRSAFE_NO_CCH_FUNCTIONS
/*++
NTSTATUS
RtlStringCchCopy(
_Out_writes_(cchDest) _Always_(_Post_z_) LPTSTR pszDest,
_In_ size_t cchDest,
_In_ LPCTSTR pszSrc
);
Routine Description:
This routine is a safer version of the C built-in function 'strcpy'.
The size of the destination buffer (in characters) is a parameter and
this function will not write past the end of this buffer and it will
ALWAYS null terminate the destination buffer (unless it is zero length).
This routine is not a replacement for strncpy. That function will pad the
destination string with extra null termination characters if the count is
greater than the length of the source string, and it will fail to null
terminate the destination string if the source string length is greater
than or equal to the count. You can not blindly use this instead of strncpy:
it is common for code to use it to "patch" strings and you would introduce
errors if the code started null terminating in the middle of the string.
This function returns an NTSTATUS value, and not a pointer. It returns
STATUS_SUCCESS if the string was copied without truncation and null terminated,
otherwise it will return a failure code. In failure cases as much of
pszSrc will be copied to pszDest as possible, and pszDest will be null
terminated.
Arguments:
pszDest - destination string
cchDest - size of destination buffer in characters.
length must be = (_tcslen(src) + 1) to hold all of the
source including the null terminator
pszSrc - source string which must be null terminated
Notes:
Behavior is undefined if source and destination strings overlap.
pszDest and pszSrc should not be NULL. See RtlStringCchCopyEx if you require
the handling of NULL values.
Return Value:
STATUS_SUCCESS - if there was source data and it was all copied and the
resultant dest string was null terminated
failure - you can use the macro NTSTATUS_CODE() to get a win32
error code for all hresult failure cases
STATUS_BUFFER_OVERFLOW /
NTSTATUS_CODE(status) == ERROR_INSUFFICIENT_BUFFER
- this return value is an indication that the copy
operation failed due to insufficient space. When this
error occurs, the destination buffer is modified to
contain a truncated version of the ideal result and is
null terminated. This is useful for situations where
truncation is ok
It is strongly recommended to use the NT_SUCCESS() macro to test the
return value of this function.
--*/
NTSTRSAFEDDI
RtlStringCchCopyA(
_Out_writes_(cchDest) _Always_(_Post_z_) NTSTRSAFE_PSTR pszDest,
_In_ size_t cchDest,
_In_ NTSTRSAFE_PCSTR pszSrc)
{
NTSTATUS status;
status = RtlStringValidateDestA(pszDest, cchDest, NTSTRSAFE_MAX_CCH);
if (NT_SUCCESS(status))
{
status = RtlStringCopyWorkerA(pszDest,
cchDest,
NULL,
pszSrc,
NTSTRSAFE_MAX_LENGTH);
}
else if (cchDest > 0)
{
*pszDest = '\0';
}
return status;
}
NTSTRSAFEDDI
RtlStringCchCopyW(
_Out_writes_(cchDest) _Always_(_Post_z_) NTSTRSAFE_PWSTR pszDest,
_In_ size_t cchDest,
_In_ NTSTRSAFE_PCWSTR pszSrc)
{
NTSTATUS status;
status = RtlStringValidateDestW(pszDest, cchDest, NTSTRSAFE_MAX_CCH);
if (NT_SUCCESS(status))
{
status = RtlStringCopyWorkerW(pszDest,
cchDest,
NULL,
pszSrc,
NTSTRSAFE_MAX_LENGTH);
}
else if (cchDest > 0)
{
*pszDest = L'\0';
}
return status;
}
#endif // !NTSTRSAFE_NO_CCH_FUNCTIONS
#ifndef NTSTRSAFE_NO_CB_FUNCTIONS
/*++
NTSTATUS
RtlStringCbCopy(
_Out_writes_bytes_(cbDest) _Always_(_Post_z_) LPTSTR pszDest,
_In_ size_t cbDest,
_In_ LPCTSTR pszSrc
);
Routine Description:
This routine is a safer version of the C built-in function 'strcpy'.
The size of the destination buffer (in bytes) is a parameter and this
function will not write past the end of this buffer and it will ALWAYS
null terminate the destination buffer (unless it is zero length).
This routine is not a replacement for strncpy. That function will pad the
destination string with extra null termination characters if the count is
greater than the length of the source string, and it will fail to null
terminate the destination string if the source string length is greater
than or equal to the count. You can not blindly use this instead of strncpy:
it is common for code to use it to "patch" strings and you would introduce
errors if the code started null terminating in the middle of the string.
This function returns an NTSTATUS value, and not a pointer. It returns
STATUS_SUCCESS if the string was copied without truncation and null terminated,
otherwise it will return a failure code. In failure cases as much of pszSrc
will be copied to pszDest as possible, and pszDest will be null terminated.
Arguments:
pszDest - destination string
cbDest - size of destination buffer in bytes.
length must be = ((_tcslen(src) + 1) * sizeof(TCHAR)) to
hold all of the source including the null terminator
pszSrc - source string which must be null terminated
Notes:
Behavior is undefined if source and destination strings overlap.
pszDest and pszSrc should not be NULL. See RtlStringCbCopyEx if you require
the handling of NULL values.
Return Value:
STATUS_SUCCESS - if there was source data and it was all copied and the
resultant dest string was null terminated
failure - you can use the macro NTSTATUS_CODE() to get a win32
error code for all hresult failure cases
STATUS_BUFFER_OVERFLOW /
NTSTATUS_CODE(status) == ERROR_INSUFFICIENT_BUFFER
- this return value is an indication that the copy
operation failed due to insufficient space. When this
error occurs, the destination buffer is modified to
contain a truncated version of the ideal result and is
null terminated. This is useful for situations where
truncation is ok
It is strongly recommended to use the NT_SUCCESS() macro to test the
return value of this function.
--*/
NTSTRSAFEDDI
RtlStringCbCopyA(
_Out_writes_bytes_(cbDest) _Always_(_Post_z_) NTSTRSAFE_PSTR pszDest,
_In_ size_t cbDest,
_In_ NTSTRSAFE_PCSTR pszSrc)
{
NTSTATUS status;
size_t cchDest = cbDest / sizeof(char);
status = RtlStringValidateDestA(pszDest, cchDest, NTSTRSAFE_MAX_CCH);
if (NT_SUCCESS(status))
{
status = RtlStringCopyWorkerA(pszDest,
cchDest,
NULL,
pszSrc,
NTSTRSAFE_MAX_LENGTH);
}
else if (cchDest > 0)
{
*pszDest = '\0';
}
return status;
}
#pragma warning(push)
#pragma warning(disable : __WARNING_POSTCONDITION_NULLTERMINATION_VIOLATION)
NTSTRSAFEDDI
RtlStringCbCopyW(
_Out_writes_bytes_(cbDest) _Always_(_Post_z_) NTSTRSAFE_PWSTR pszDest,
_In_ size_t cbDest,
_In_ NTSTRSAFE_PCWSTR pszSrc)
{
NTSTATUS status;
size_t cchDest = cbDest / sizeof(wchar_t);
status = RtlStringValidateDestW(pszDest, cchDest, NTSTRSAFE_MAX_CCH);
if (NT_SUCCESS(status))
{
status = RtlStringCopyWorkerW(pszDest,
cchDest,
NULL,
pszSrc,
NTSTRSAFE_MAX_LENGTH);
}
else if (cchDest > 0)
{
*pszDest = L'\0';
}
return status;
}
#pragma warning(pop)
#endif // !NTSTRSAFE_NO_CB_FUNCTIONS
#ifndef NTSTRSAFE_NO_CCH_FUNCTIONS
/*++
NTSTATUS
RtlStringCchCopyEx(
_Out_writes_(cchDest) _Always_(_Post_z_) LPTSTR pszDest OPTIONAL,
_In_ size_t cchDest,
_In_ LPCTSTR pszSrc OPTIONAL,
_Outptr_opt_result_buffer_(*pcchRemaining) LPTSTR* ppszDestEnd OPTIONAL,
_Out_opt_ size_t* pcchRemaining OPTIONAL,
_In_ DWORD dwFlags
);
Routine Description:
This routine is a safer version of the C built-in function 'strcpy' with
some additional parameters. In addition to functionality provided by
RtlStringCchCopy, this routine also returns a pointer to the end of the
destination string and the number of characters left in the destination string
including the null terminator. The flags parameter allows additional controls.
Arguments:
pszDest - destination string
cchDest - size of destination buffer in characters.
length must be = (_tcslen(pszSrc) + 1) to hold all of
the source including the null terminator
pszSrc - source string which must be null terminated
ppszDestEnd - if ppszDestEnd is non-null, the function will return a
pointer to the end of the destination string. If the
function copied any data, the result will point to the
null termination character
pcchRemaining - if pcchRemaining is non-null, the function will return the
number of characters left in the destination string,
including the null terminator
dwFlags - controls some details of the string copy:
STRSAFE_FILL_BEHIND_NULL
if the function succeeds, the low byte of dwFlags will be
used to fill the uninitialize part of destination buffer
behind the null terminator
STRSAFE_IGNORE_NULLS
treat NULL string pointers like empty strings (TEXT("")).
this flag is useful for emulating functions like lstrcpy
STRSAFE_FILL_ON_FAILURE
if the function fails, the low byte of dwFlags will be
used to fill all of the destination buffer, and it will
be null terminated. This will overwrite any truncated
string returned when the failure is
STATUS_BUFFER_OVERFLOW
STRSAFE_NO_TRUNCATION /
STRSAFE_NULL_ON_FAILURE
if the function fails, the destination buffer will be set
to the empty string. This will overwrite any truncated string
returned when the failure is STATUS_BUFFER_OVERFLOW.
Notes:
Behavior is undefined if source and destination strings overlap.
pszDest and pszSrc should not be NULL unless the STRSAFE_IGNORE_NULLS flag
is specified. If STRSAFE_IGNORE_NULLS is passed, both pszDest and pszSrc
may be NULL. An error may still be returned even though NULLS are ignored
due to insufficient space.
Return Value:
STATUS_SUCCESS - if there was source data and it was all copied and the
resultant dest string was null terminated
failure - you can use the macro NTSTATUS_CODE() to get a win32
error code for all hresult failure cases
STATUS_BUFFER_OVERFLOW /
NTSTATUS_CODE(status) == ERROR_INSUFFICIENT_BUFFER
- this return value is an indication that the copy
operation failed due to insufficient space. When this
error occurs, the destination buffer is modified to
contain a truncated version of the ideal result and is
null terminated. This is useful for situations where
truncation is ok.
It is strongly recommended to use the NT_SUCCESS() macro to test the
return value of this function
--*/
NTSTRSAFEDDI
RtlStringCchCopyExA(
_Out_writes_(cchDest) _Always_(_Post_z_) NTSTRSAFE_PSTR pszDest,
_In_ size_t cchDest,
_In_ NTSTRSAFE_PCSTR pszSrc,
_Outptr_opt_result_buffer_(*pcchRemaining) NTSTRSAFE_PSTR* ppszDestEnd,
_Out_opt_ size_t* pcchRemaining,
_In_ DWORD dwFlags)
{
NTSTATUS status;
status = RtlStringExValidateDestA(pszDest, cchDest, NTSTRSAFE_MAX_CCH, dwFlags);
if (NT_SUCCESS(status))
{
NTSTRSAFE_PSTR pszDestEnd = pszDest;
size_t cchRemaining = cchDest;
status = RtlStringExValidateSrcA(&pszSrc, NULL, NTSTRSAFE_MAX_CCH, dwFlags);
if (NT_SUCCESS(status))
{
if (dwFlags & (~STRSAFE_VALID_FLAGS))
{
status = STATUS_INVALID_PARAMETER;
if (cchDest != 0)
{
*pszDest = '\0';
}
}
else if (cchDest == 0)
{
// only fail if there was actually src data to copy
if (*pszSrc != '\0')
{
if (pszDest == NULL)
{
status = STATUS_INVALID_PARAMETER;
}
else
{
status = STATUS_BUFFER_OVERFLOW;
}
}
}
else
{
size_t cchCopied = 0;
status = RtlStringCopyWorkerA(pszDest,
cchDest,
&cchCopied,
pszSrc,
NTSTRSAFE_MAX_LENGTH);
pszDestEnd = pszDest + cchCopied;
cchRemaining = cchDest - cchCopied;
if (NT_SUCCESS(status) &&
(dwFlags & STRSAFE_FILL_BEHIND_NULL) &&
(cchRemaining > 1))
{
size_t cbRemaining;
// safe to multiply cchRemaining * sizeof(char) since cchRemaining < NTSTRSAFE_MAX_CCH and sizeof(char) is 1
cbRemaining = cchRemaining * sizeof(char);
// handle the STRSAFE_FILL_BEHIND_NULL flag
RtlStringExHandleFillBehindNullA(pszDestEnd, cbRemaining, dwFlags);
}
}
}
else
{
if (cchDest != 0)
{
*pszDest = '\0';
}
}
if (!NT_SUCCESS(status) &&
(dwFlags & (STRSAFE_NO_TRUNCATION | STRSAFE_FILL_ON_FAILURE | STRSAFE_NULL_ON_FAILURE)) &&
(cchDest != 0))
{
size_t cbDest;
// safe to multiply cchDest * sizeof(char) since cchDest < NTSTRSAFE_MAX_CCH and sizeof(char) is 1
cbDest = cchDest * sizeof(char);
// handle the STRSAFE_FILL_ON_FAILURE, STRSAFE_NULL_ON_FAILURE, and STRSAFE_NO_TRUNCATION flags
RtlStringExHandleOtherFlagsA(pszDest,
cbDest,
0,
&pszDestEnd,
&cchRemaining,
dwFlags);
}
if (NT_SUCCESS(status) || (status == STATUS_BUFFER_OVERFLOW))
{
if (ppszDestEnd)
{
*ppszDestEnd = pszDestEnd;
}
if (pcchRemaining)
{
*pcchRemaining = cchRemaining;
}
}
}
else if (cchDest > 0)
{
*pszDest = '\0';
}
return status;
}
NTSTRSAFEDDI
RtlStringCchCopyExW(
_Out_writes_(cchDest) _Always_(_Post_z_) NTSTRSAFE_PWSTR pszDest,
_In_ size_t cchDest,
_In_ NTSTRSAFE_PCWSTR pszSrc,
_Outptr_opt_result_buffer_(*pcchRemaining) NTSTRSAFE_PWSTR* ppszDestEnd,
_Out_opt_ size_t* pcchRemaining,
_In_ DWORD dwFlags)
{
NTSTATUS status;
status = RtlStringExValidateDestW(pszDest, cchDest, NTSTRSAFE_MAX_CCH, dwFlags);
if (NT_SUCCESS(status))
{