ReactOS 0.4.16-dev-1020-gf135cab
corecrt_internal_string_templates.h
Go to the documentation of this file.
1//
2// corecrt_internal_string_templates.h
3//
4// Copyright (c) Microsoft Corporation. All rights reserved.
5//
6// This internal header defines template implementations of several secure
7// string functions that have identical implementations for both narrow and
8// wide character strings.
9//
10#pragma once
11
13
14
15
16// _strcat_s() and _wcscat_s()
17template <typename Character>
18_Success_(return == 0)
19static errno_t __cdecl common_tcscat_s(
20 _Inout_updates_z_(size_in_elements) Character* const destination,
22 _In_z_ Character const* const source
24{
27
28 Character* destination_it = destination;
30 while (available > 0 && *destination_it != 0)
31 {
32 ++destination_it;
33 --available;
34 }
35
36 if (available == 0)
37 {
38 _RESET_STRING(destination, size_in_elements);
40 }
41
42 Character const* source_it = source;
43 while ((*destination_it++ = *source_it++) != 0 && --available > 0)
44 {
45 }
46
47 if (available == 0)
48 {
49 _RESET_STRING(destination, size_in_elements);
51 }
54}
55
56
57
58// _strcpy_s() and _wcscpy_s()
59template <typename Character>
60_Success_(return == 0)
61static errno_t __cdecl common_tcscpy_s(
62 _Out_writes_z_(size_in_elements) Character* const destination,
64 _In_z_ Character const* const source
65 ) throw()
66{
69
70 Character* destination_it = destination;
71 Character const* source_it = source;
72
74 while ((*destination_it++ = *source_it++) != 0 && --available > 0)
75 {
76 }
77
78 if (available == 0)
79 {
80 _RESET_STRING(destination, size_in_elements);
82 }
85}
86
87
88
89// _strncat_s() and _wcsncat_s()
90template <typename Character>
91_Success_(return == 0)
92static errno_t __cdecl common_tcsncat_s(
93 _Inout_updates_z_(size_in_elements) Character* const destination,
96 _In_ size_t const count
97 ) throw()
98{
99 if (count == 0 && destination == nullptr && size_in_elements == 0)
100 {
101 // This case is allowed; nothing to do:
103 }
104
106 if (count != 0)
107 {
109 }
110
111 Character* destination_it = destination;
112
114 size_t remaining = count;
115 while (available > 0 && *destination_it != 0)
116 {
117 ++destination_it;
118 --available;
119 }
120
121 if (available == 0)
122 {
123 _RESET_STRING(destination, size_in_elements);
125 }
126
127 Character const* source_it = source;
128 if (count == _TRUNCATE)
129 {
130 while ((*destination_it++ = *source_it++) != 0 && --available > 0)
131 {
132 }
133 }
134 else
135 {
136 while (remaining > 0 && (*destination_it++ = *source_it++) != 0 && --available > 0)
137 {
138 remaining--;
139 }
140
141 if (remaining == 0)
142 {
143 *destination_it = 0;
144 }
145 }
146
147 if (available == 0)
148 {
149 if (count == _TRUNCATE)
150 {
151 destination[size_in_elements - 1] = 0;
153 }
154 _RESET_STRING(destination, size_in_elements);
156 }
159}
160
161
162
163// _strncpy_s() and _wcsncpy_s()
164template <typename Character>
165_Success_(return == 0)
166static errno_t __cdecl common_tcsncpy_s(
167 _Out_writes_z_(size_in_elements) Character* const destination,
170 _In_ size_t const count
171 ) throw()
172{
173 if (count == 0 && destination == nullptr && size_in_elements == 0)
174 {
175 // this case is allowed; nothing to do:
177 }
178
180 if (count == 0)
181 {
182 // Notice that the source string pointer can be nullptr in this case:
183 _RESET_STRING(destination, size_in_elements);
185 }
187
188 Character* destination_it = destination;
189 Character const* source_it = source;
190
192 size_t remaining = count;
193 if (count == _TRUNCATE)
194 {
195 while ((*destination_it++ = *source_it++) != 0 && --available > 0)
196 {
197 }
198 }
199 else
200 {
201 while ((*destination_it++ = *source_it++) != 0 && --available > 0 && --remaining > 0)
202 {
203 }
204 if (remaining == 0)
205 {
206 *destination_it = 0;
207 }
208 }
209
210 if (available == 0)
211 {
212 if (count == _TRUNCATE)
213 {
214 destination[size_in_elements - 1] = 0;
216 }
217 _RESET_STRING(destination, size_in_elements);
219 }
222}
223
224
225
226// _strnset_s() and _wcsnset_s()
227template <typename Character>
228_Success_(return == 0)
229static errno_t __cdecl common_tcsnset_s(
230 _Inout_updates_z_(size_in_elements) Character* const destination,
232 _In_ Character const value,
233 _In_ size_t const count
234 ) throw()
235{
236 if (count == 0 && destination == nullptr && size_in_elements == 0)
237 {
238 // This case is allowed; nothing to do:
240 }
242
243 Character* destination_it = destination;
244
246 size_t remaining = count;
247 while (*destination_it != 0 && remaining > 0 && --available > 0)
248 {
249 *destination_it++ = value;
250 --remaining;
251 }
252
253 if (remaining == 0)
254 {
255 // Ensure the string is null-terminated:
256 while (*destination_it != 0 && --available > 0)
257 {
258 ++destination_it;
259 }
260 }
261
262 if (available == 0)
263 {
264 _RESET_STRING(destination, size_in_elements);
266 }
269}
270
271
272
273// _strset_s() and _wcsset_s()
274template <typename Character>
275_Success_(return == 0)
276static errno_t __cdecl common_tcsset_s(
277 _Inout_updates_z_(size_in_elements) Character* const destination,
279 _In_ Character const value
280 ) throw()
281{
283
284 Character* destination_it = destination;
285
287 while (*destination_it != 0 && --available > 0)
288 {
289 *destination_it++ = value;
290 }
291
292 if (available == 0)
293 {
294 _RESET_STRING(destination, size_in_elements);
296 }
299}
#define __cdecl
Definition: accygwin.h:79
#define _RETURN_DEST_NOT_NULL_TERMINATED(_String, _Size)
#define _FILL_STRING
#define _RETURN_BUFFER_TOO_SMALL(_String, _Size)
#define _RESET_STRING(_String, _Size)
#define _RETURN_TRUNCATE
#define _RETURN_NO_ERROR
#define _VALIDATE_POINTER_RESET_STRING(_Pointer, _String, _Size)
#define _VALIDATE_STRING(_String, _Size)
size_t const size_in_elements
_In_ size_t const _In_ Character const value
size_t const _In_z_ Character const *const source throw()
static WCHAR available[MAX_STRING_RESOURCE_LEN]
Definition: object.c:2336
GLuint GLuint GLsizei count
Definition: gl.h:1545
#define _Out_writes_z_(s)
Definition: no_sal2.h:180
#define _Success_(c)
Definition: no_sal2.h:84
#define _In_z_
Definition: no_sal2.h:164
#define _Inout_updates_z_(s)
Definition: no_sal2.h:186
#define _In_reads_or_z_(s)
Definition: no_sal2.h:174
#define _In_
Definition: no_sal2.h:158
int errno_t
Definition: corecrt.h:615
#define _TRUNCATE
Definition: corecrt.h:278
Definition: pdh_main.c:96
#define const
Definition: zconf.h:233