-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathsnprintf_support.c
304 lines (263 loc) · 9.5 KB
/
snprintf_support.c
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
/* SPDX-License-Identifier: MIT */
/*
* Copyright (c) 2014-2022 Intel Corp
*/
#include "safeclib_private.h"
#include "safe_str_constraint.h"
#include "safe_str_lib.h"
#include "snprintf_s.h"
#define FMT_CHAR 'c'
#define FMT_WCHAR 'C'
#define FMT_SHORT 'h'
#define FMT_INT 'd'
#define FMT_LONG 'l'
#define FMT_STRING 's'
#define FMT_WSTRING 'S'
#define FMT_DOUBLE 'g'
#define FMT_LDOUBLE 'G'
#define FMT_VOID 'p'
#define FMT_PCHAR '1'
#define FMT_PSHORT '2'
#define FMT_PINT '3'
#define FMT_PLONG '4'
#define MAX_FORMAT_ELEMENTS 16
#define CHK_FORMAT(X,Y) (((X)==(Y))?1:0)
unsigned int
parse_format(const char *format, char pformatList[], unsigned int maxFormats)
{
unsigned int numFormats = 0;
unsigned int index = 0;
unsigned int start = 0;
char lmod = 0;
while (index < RSIZE_MAX_STR && format[index] != '\0' && numFormats < maxFormats)
{
if (format[index] == '%') {
start = index; // remember where the format string started
// Check for flags
switch( format[++index]) {
case '\0': continue; // skip - end of format string
case '%' : continue; // skip - actually a percent character
case '#' : // convert to alternate form
case '0' : // zero pad
case '-' : // left adjust
case ' ' : // pad with spaces
case '+' : // force a sign be used
index++; // skip the flag character
break;
}
// check for and skip the optional field width
while ( format[index] >= '0' && format[index] <= '9') {
index++;
}
// Check for an skip the optional precision
if ( format[index] == '.') {
index++; // skip the period
while ( format[index] >= '0' && format[index] <= '9') {
index++;
}
}
// Check for and skip the optional length modifiers
lmod = ' ';
switch( format[index]) {
case 'h' : if ( format[++index] == 'h') {
++index; //also recognize the 'hh' modifier
lmod = 'H'; // for char
} else {
lmod = 'h'; // for short
}
break;
case 'l' : if ( format[++index] == 'l') {
++index; //also recognize the 'll' modifier
lmod = 'd'; // for long long
} else {
lmod = 'l'; // for long
}
break;
case 'L' : lmod = 'L'; break;
case 'j' :
case 'z' :
case 't' : index++;
break;
}
// Recognize and record the actual modifier
switch( format[index]) {
case 'c' :
if ( lmod == 'l') {
pformatList[numFormats] = FMT_WCHAR; // store the format character
} else {
pformatList[numFormats] = FMT_CHAR;
}
numFormats++;
index++; // skip the format character
break;
case 'd' : case 'i' : // signed
case 'o' : case 'u' : // unsigned
case 'x' : case 'X' : // unsigned
if ( lmod == 'H') {
pformatList[numFormats] = FMT_CHAR; // store the format character
} else if ( lmod == 'l') {
pformatList[numFormats] = FMT_LONG; // store the format character
} else if ( lmod == 'h') {
pformatList[numFormats] = FMT_SHORT; // store the format character
} else{
pformatList[numFormats] = FMT_INT;
}
numFormats++;
index++; // skip the format character
break;
case 'e' : case 'E' :
case 'f' : case 'F' :
case 'g' : case 'G' :
case 'a' : case 'A' :
if ( lmod == 'L') {
pformatList[numFormats] = FMT_LDOUBLE; // store the format character
} else{
pformatList[numFormats] = FMT_DOUBLE;
}
numFormats++;
index++; // skip the format character
break;
case 's' :
if ( lmod == 'l' || lmod == 'L') {
pformatList[numFormats] = FMT_WSTRING; // store the format character
} else {
pformatList[numFormats] = FMT_STRING;
}
numFormats++;
index++; // skip the format character
break;
case 'p' :
pformatList[numFormats] = FMT_VOID;
numFormats++;
index++; // skip the format character
break;
case 'n' :
if ( lmod == 'H') {
pformatList[numFormats] = FMT_PCHAR; // store the format character
} else if ( lmod == 'l') {
pformatList[numFormats] = FMT_PLONG; // store the format character
} else if ( lmod == 'h') {
pformatList[numFormats] = FMT_PSHORT; // store the format character
} else{
pformatList[numFormats] = FMT_PINT;
}
numFormats++;
index++; // skip the format character
break;
case 'm' :
// Does not represent an argument in the call stack
index++; // skip the format character
continue;
default:
printf("failed to recognize format string [");
for (;start<index; start++) { printf("%c", format[start]); }
puts("]");
break;
}
} else {
index++; // move past this character
}
}
return numFormats;
}
unsigned int
check_integer_format(const char format)
{
unsigned int retValue = 0; // default failure
switch( format) {
case FMT_CHAR :
case FMT_SHORT :
case FMT_INT :
retValue = 1;
break;
}
return retValue;
}
inline int snprintf_s_i(char *dest, rsize_t dmax, const char *format, int a)
{
char pformatList[MAX_FORMAT_ELEMENTS];
unsigned int index = 0;
// Determine the number of format options in the format string
unsigned int nfo = parse_format(format, &pformatList[0], MAX_FORMAT_ELEMENTS);
// Check that there are not too many format options
if ( nfo != 1 ) {
dest[0] = '\0';
return SNPRFNEGATE(ESBADFMT);
}
// Check that the format is for an integer type
if ( check_integer_format(pformatList[index]) == 0) {
dest[0] = '\0';
return SNPRFNEGATE(ESFMTTYP);
}
index++;
return snprintf(dest, dmax, format, a);
}
inline int snprintf_s_l(char *dest, rsize_t dmax, const char *format, long a)
{
char pformatList[MAX_FORMAT_ELEMENTS];
unsigned int index = 0;
// Determine the number of format options in the format string
unsigned int nfo = parse_format(format, &pformatList[0], MAX_FORMAT_ELEMENTS);
// Check that there are not too many format options
if ( nfo != 1 ) {
dest[0] = '\0';
return SNPRFNEGATE(ESBADFMT);
}
// Check that the format is for an long type
if ( CHK_FORMAT(FMT_LONG, pformatList[index]) == 0) {
dest[0] = '\0';
return SNPRFNEGATE(ESFMTTYP);
}
index++;
return snprintf(dest, dmax, format, a);
}
inline int snprintf_s_si(char *dest, rsize_t dmax, const char *format, char *s, int a)
{
char pformatList[MAX_FORMAT_ELEMENTS];
unsigned int index = 0;
// Determine the number of format options in the format string
unsigned int nfo = parse_format(format, &pformatList[0], MAX_FORMAT_ELEMENTS);
// Check that there are not too many format options
if ( nfo != 2 ) {
dest[0] = '\0';
return SNPRFNEGATE(ESBADFMT);
}
// Check first format is of string type
if ( CHK_FORMAT(FMT_STRING, pformatList[index]) == 0) {
dest[0] = '\0';
return SNPRFNEGATE(ESFMTTYP);
}
index++;
// Check that the format is for an integer type
if ( check_integer_format(pformatList[index]) == 0) {
dest[0] = '\0';
return SNPRFNEGATE(ESFMTTYP);
}
index++;
return snprintf(dest, dmax, format, s, a);
}
inline int snprintf_s_sl(char *dest, rsize_t dmax, const char *format, char *s, long a)
{
char pformatList[MAX_FORMAT_ELEMENTS];
unsigned int index = 0;
// Determine the number of format options in the format string
unsigned int nfo = parse_format(format, &pformatList[0], MAX_FORMAT_ELEMENTS);
// Check that there are not too many format options
if ( nfo != 2 ) {
dest[0] = '\0';
return SNPRFNEGATE(ESBADFMT);
}
// Check first format is of string type
if ( CHK_FORMAT(FMT_STRING, pformatList[index]) == 0) {
dest[0] = '\0';
return SNPRFNEGATE(ESFMTTYP);
}
index++;
// Check that the format is for an integer type
if ( CHK_FORMAT(FMT_LONG, pformatList[index]) == 0) {
dest[0] = '\0';
return SNPRFNEGATE(ESFMTTYP);
}
index++;
return snprintf(dest, dmax, format, s, a);
}