Debian Codegraph @

core/libs/dngwriter/extra/xmp_sdk/source/SafeStringAPIs.cpp

raw
1 // =================================================================================================
2 // Copyright 2020 Adobe
3 // All Rights Reserved.
4 // NOTICE: Adobe permits you to use, modify, and distribute this file in
5 // accordance with the terms of the Adobe license agreement accompanying
6 // it. If you have received this file from a source other than Adobe,
7 // then your use, modification, or distribution of it requires the prior
8 // written permission of Adobe.
9 // =================================================================================================
10
11 #if AdobePrivate
12 // =================================================================================================
13 // Change history
14 // ==============
15 //
16 // Writers:
17 // SKP Sunil Kishor Pathak
18 // AJ Abhishek Jindal
19 //
20 // mm-dd-yy who Description of changes, most recent on top
21 //
22 // 02-06-15 AJ Fixing warnings due to implicit typecasting
23 // 07-01-13 SKP First draft.
24 //
25 // =================================================================================================
26 #endif // AdobePrivate
27
28 #include <stddef.h> /* Include standard ANSI C stuff: size_t, NULL etc */
29 #include <string.h> /* memmove etc. defined here */
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <wchar.h>
33 #include <stdarg.h>
34
35 #include <assert.h>
36 #include <limits.h>
37 #include "SafeStringAPIs.h"
38
39 /*
40 Safe String Functions
41 */
42
43 CONDITIONAL_STATIC size_t strnlen_safe(const char *s, size_t maxSize)
44 {
45 if (s)
46 {
47 size_t n;
48 for (n = 0; n < maxSize && *s; n++, s++)
49 ;
50 return n;
51 }
52 else{
53 return 0;
54 }
55 }
56
57 CONDITIONAL_STATIC SafeInt32 strcpy_safe(char *dest, size_t size, const char *src)
58 {
59
60 if (dest && src && size > 0)
61 {
62 char* p = dest;
63 size_t available = size;
64 while((*p++ = *src++) != '\0' && --available > 0)
65 {
66 }
67 if(available == 0)
68 {
69 *(--p) = '\0';
70 //Buffer overflow. return ERROR_SAFE_BUFFER_OVERFLOW
71 return ERROR_SAFE_BUFFER_OVERFLOW;
72 }
73 else
74 return 0;
75 }
76 else{
77 if(dest)
78 *dest = '\0';
79 return ERROR_SAFE_INVALID_PARAM;
80 }
81
82 }
83
84 CONDITIONAL_STATIC SafeInt32 strncpy_safe(char *dest, size_t size, const char *src, size_t n)
85 {
86 if(n == 0 && dest == NULL && size == 0)
87 return 0;
88 if(dest && src && size > 0)
89 {
90 if(n == 0)
91 {
92 *dest = '\0';
93 return 0;
94 }
95 char* p = dest;
96 size_t available = size;
97 while((*p++ = *src++) != '\0' && --available > 0 && --n > 0)
98 {
99 }
100 if(n == 0)
101 *p = '\0';
102 if(available == 0)
103 {
104 *(--p) = '\0';
105 //Buffer overflow. Returns ERROR_SAFE_BUFFER_OVERFLOW
106 return ERROR_SAFE_BUFFER_OVERFLOW;
107 }
108
109 return 0;
110 }
111 else{
112 if(dest)
113 *dest = '\0';
114 return ERROR_SAFE_INVALID_PARAM;
115 }
116 }
117
118 CONDITIONAL_STATIC SafeInt32 strcat_safe(char *s1, size_t s1max, const char *s2)
119 {
120 if(s1 == NULL && s1max == 0)
121 return 0;
122 if(s1 && s2 && s1max > 0)
123 {
124 size_t available = s1max;
125 char* dest = s1;
126 while(available > 0 && *dest != '\0')
127 {
128 dest++;
129 available--;
130 }
131 if(available == 0)
132 {
133 *s1 = '\0';
134 //Destination not null terminated. Empty destination and return ERROR_SAFE_INVALID_PARAM
135 return ERROR_SAFE_INVALID_PARAM;
136 }
137 while((*dest++ = *s2++) != '\0' && --available > 0)
138 {
139 }
140 if(available == 0)
141 {
142 //Truncate at buffer overflow attempt. Return ERROR_SAFE_BUFFER_OVERFLOW.
143 *(--dest) = '\0';
144 return ERROR_SAFE_BUFFER_OVERFLOW;
145 }
146 return 0;
147 }
148 else{
149 if(s1)
150 *s1 = '\0';
151 return ERROR_SAFE_INVALID_PARAM;
152 }
153 }
154
155 CONDITIONAL_STATIC SafeInt32 strncat_safe(char *s1, size_t s1max, const char *s2, size_t n)
156 {
157 if(n == 0 && s1 == NULL && s1max == 0)
158 return 0;
159 if(s1 && s2 && s1max > 0)
160 {
161 char* dest = s1;
162 size_t available = s1max;
163
164 while(available > 0 && *dest != '\0')
165 {
166 dest++;
167 available--;
168 }
169 if(available == 0)
170 {
171 *s1 = '\0';
172 //Destination not null terminated. Empty destination and return ERROR_SAFE_INVALID_PARAM
173 return ERROR_SAFE_INVALID_PARAM;
174 }
175 while(n > 0 && (*dest++ = *s2++)!= '\0' && --available > 0)
176 {
177 n--;
178 }
179 if(n == 0)
180 {
181 *dest = '\0';
182 }
183 if(available == 0)
184 {
185 //Truncate. Return ERROR_SAFE_BUFFER_OVERFLOW
186 *(--dest) = '\0';
187 return ERROR_SAFE_BUFFER_OVERFLOW;
188 }
189 return 0;
190 }
191 else{
192 if(s1)
193 *s1 = '\0';
194 return ERROR_SAFE_INVALID_PARAM;
195 }
196 }
197
198 CONDITIONAL_STATIC char *strchr_safe(const char *s, size_t maxSize, char c)
199 {
200 if(s == NULL) return NULL;
201 for (; maxSize > 0 && *s != '\0'; s++, maxSize--)
202 {
203 if (*s == (const char)c)
204 {
205 return (char *)s;
206 }
207 }
208
209 if (maxSize > 0 && (const char)c == *s)
210 {
211 return (char *)s;
212 }
213
214 return NULL;
215 }
216
217 CONDITIONAL_STATIC char * strtok_safe(char* strToken, size_t maxSize, const char *strDelimit, char **context)
218 {
219 if(maxSize == 0)
220 {
221 return NULL;
222 }
223
224 if(strToken)
225 {
226 strToken[maxSize - 1] = '\0';
227 }
228
229 #if (defined(_WIN32) || defined(_WIN64)) && defined __STDC_WANT_SECURE_LIB__
230 return strtok_s(strToken, strDelimit, context);
231 #else
232 if( !strDelimit || !context || ( !( *context) && ( !strToken)))
233 {
234 return NULL;
235 }
236
237 if( !strToken)
238 {
239 // We got NULL input, so we get our last position from context.
240 strToken = *context;
241 }
242 /* pass all characters that are present in the delimiter string */
243 while(*strToken && strchr_safe(strDelimit, (size_t)-1, *strToken))
244 ++strToken;
245
246 if(*strToken){
247 /* This is where the next piece of string starts */
248 char *start = strToken;
249 /* Set the context pointer to the first byte after start */
250 *context = start + 1;
251 while(**context && !strchr_safe(strDelimit, (size_t)-1, **context))
252 ++*context;
253
254 if(**context){
255 //The end is not a null byte
256 **context = '\0'; /* Null terminate it */
257 ++*context; /* Advance the last pointer to beyond the null byte. */
258 }
259 return start;
260 }
261
262 // No remaining tokens. Set the context pointer to the original terminating null
263 // of strToken and return null.
264 *context = strToken;
265 return NULL;
266 #endif
267 }
268
269 CONDITIONAL_STATIC SafeInt32 vprintf_safe(const char* format, va_list argp)
270 {
271 #if (defined(_WIN32) || defined(_WIN64)) && defined __STDC_WANT_SECURE_LIB__
272 return vprintf_s(format, argp);
273 #else
274 if(format)
275 {
276 return vprintf(format, argp);
277 }
278 else
279 {
280 return ERROR_SAFE_INVALID_PARAM;
281 }
282 #endif
283 }
284
285 CONDITIONAL_STATIC SafeInt32 vsprintf_safe(char* buffer, size_t size, const char* format, va_list argp)
286 {
287 #if (defined(_WIN32) || defined(_WIN64)) && defined __STDC_WANT_SECURE_LIB__
288 return vsprintf_s(buffer, size, format, argp);
289 #else
290 if(buffer && format && size > 0)
291 {
292 SafeInt32 count = static_cast<SafeInt32>(size);
293 if(count > INT_MAX)
294 count = INT_MAX;
295 SafeInt32 numBytes = vsnprintf(buffer, count, format, argp);
296 if(numBytes >= count)
297 {
298 /* If number of bytes written is greater than count, the buffer is too small,
299 vsnprintf truncates the buffer and null terminates it.*/
300 return ERROR_SAFE_BUFFER_OVERFLOW;
301 }
302 return numBytes;
303 }
304 else
305 {
306 if(buffer)
307 *buffer = '\0';
308 return ERROR_SAFE_INVALID_PARAM;
309 }
310 #endif
311 }
312
313 CONDITIONAL_STATIC SafeInt32 vsnprintf_safe(char *buffer, size_t size, size_t count, const char* format, va_list argp)
314 {
315 #if (defined(_WIN32) || defined(_WIN64)) && defined __STDC_WANT_SECURE_LIB__
316 return vsnprintf_s(buffer, size, count, format, argp);
317 #else
318 if(count == 0 && buffer == NULL && size == 0)
319 return 0; //allowed
320 if(buffer && format && size > 0)
321 {
322 if(count == 0)
323 {
324 *buffer = '\0';
325 return 0;
326 }
327 SafeInt32 n = (SafeInt32)((count + 1) < size ? (count + 1) : size); //MIN(count+1, size);
328 SafeInt32 numBytes = vsnprintf(buffer, n, format, argp);
329 if(numBytes >= static_cast<SafeInt32>(size) && count >= size)
330 {
331 //Truncate here. Return ERROR_SAFE_BUFFER_OVERFLOW.
332 buffer[n-1] = '\0';
333 return ERROR_SAFE_BUFFER_OVERFLOW;
334 }
335 buffer[n-1] = '\0';
336 return numBytes;
337 }
338 else
339 {
340 if(buffer)
341 *buffer = '\0';
342 return ERROR_SAFE_INVALID_PARAM;
343 }
344 #endif
345 }
346
347 CONDITIONAL_STATIC SafeInt32 vwprintf_safe(const wchar_t *format, va_list argp)
348 {
349 #if (defined(_WIN32) || defined(_WIN64)) && defined __STDC_WANT_SECURE_LIB__
350 return vwprintf_s(format, argp);
351 #else
352 if(format)
353 {
354 return vwprintf(format, argp);
355 }
356 else
357 {
358 return ERROR_SAFE_INVALID_PARAM;
359 }
360 #endif
361 }
362
363 CONDITIONAL_STATIC SafeInt32 sprintf_safe(char *buffer, size_t size, const char *format, ...)
364 {
365 va_list argp;
366 va_start(argp, format);
367 SafeInt32 numBytes = vsprintf_safe(buffer, size, format, argp);
368 va_end(argp);
369 return numBytes;
370 }
371
372 CONDITIONAL_STATIC SafeInt32 printf_safe(const char *format, ...)
373 {
374 va_list argp;
375 va_start(argp, format);
376 SafeInt32 numBytes = vprintf_safe(format, argp);
377 va_end(argp);
378 return numBytes;
379 }
380
381 CONDITIONAL_STATIC SafeInt32 snprintf_safe(char* buffer, size_t size, size_t count, const char *format, ...)
382 {
383 va_list argp;
384 va_start(argp, format);
385 SafeInt32 numBytes = vsnprintf_safe(buffer, size, count, format, argp);
386 va_end(argp);
387 return numBytes;
388 }
389
390 CONDITIONAL_STATIC SafeInt32 wprintf_safe(const wchar_t *format, ...)
391 {
392 va_list argp;
393 va_start(argp, format);
394 SafeInt32 numBytes = vwprintf_safe(format, argp);
395 va_end(argp);
396 return numBytes;
397 }
398
399 #if (defined(_WIN32) || defined(_WIN64)) && defined __STDC_WANT_SECURE_LIB__
400
401 CONDITIONAL_STATIC SafeInt32 splitpath_safe(const char* path, char* drive, size_t driveSize, char* dir,
402 size_t dirSize, char* fname, size_t fnameSize, char* ext, size_t extSize)
403 {
404 return _splitpath_s(path, drive, driveSize, dir, dirSize, fname, fnameSize, ext, extSize);
405 }
406
407 CONDITIONAL_STATIC SafeInt32 makepath_safe(char* path, size_t size, const char* drive, const char* dir,
408 const char* fname, const char* ext)
409 {
410 return _makepath_s(path, size, drive, dir, fname, ext);
411 }
412
413 #endif
414
415 #if !(defined(_WIN32) || defined(_WIN64)) || !defined __STDC_WANT_SECURE_LIB__
416
417 static char *reverse_string_safe(char *str, size_t size)
418 {
419 size_t head = 0;
420 size_t tail = strnlen_safe(str, size) - 1;
421 while (head < tail)
422 {
423 char temp = str[head];
424 str[head] = str[tail];
425 str[tail] = temp;
426 head++;
427 tail--;
428 }
429 return str;
430 }
431
432
433 static SafeInt32 _xtox_safe(unsigned long val, char *buf, size_t size, int radix, int isNeg)
434 {
435 if(!buf)
436 {
437 return ERROR_SAFE_INVALID_PARAM;
438 }
439 if(radix < 2 || radix > 36)
440 {
441 return ERROR_SAFE_INVALID_PARAM;
442 }
443 size_t len;
444 unsigned long num;
445 if(isNeg)
446 num = (unsigned long)(-(long)val);
447 else
448 num = val;
449 for(len = 2; num; num/= radix) len++; /* quick log_base */
450
451 if(size <= 0 || size < len)
452 {
453 buf[0]='\0';
454 return ERROR_SAFE_BUFFER_OVERFLOW;
455 }
456 /* parameters are valid */
457
458 int strIndex = 0;
459 if(isNeg)
460 val = (unsigned long)(-(long)val);
461
462
463 do
464 {
465 unsigned curDigit = (unsigned) val % radix;
466 if(curDigit > 9)
467 buf[strIndex++] = curDigit + 'a' - 10;
468 else
469 buf[strIndex++] = curDigit + '0';
470
471 val /= radix;
472 }while(val);
473
474 if(isNeg)
475 buf[strIndex++] = '-';
476 buf[strIndex++] = 0;
477
478 /* Now reverse the string */
479 reverse_string_safe(buf, size);
480 return 0;
481 }
482
483 static SafeInt32 _x64tox_safe(SafeUns64 val, char *buf, size_t size, int radix, int isNeg)
484 {
485 if(!buf)
486 {
487 return ERROR_SAFE_INVALID_PARAM;
488 }
489 if(radix < 2 || radix > 36)
490 {
491 return ERROR_SAFE_INVALID_PARAM;
492 }
493 size_t len;
494 SafeUns64 num = 0;
495 if(isNeg)
496 num = (SafeUns64)(-(SafeInt64)val);
497 else
498 num = val;
499 for(len = 2; num; num/= radix) len++; /* quick log_base */
500
501 if(size <= 0 || size < len)
502 {
503 buf[0]='\0';
504 return ERROR_SAFE_BUFFER_OVERFLOW;
505 }
506 /* parameters are valid */
507
508 int strIndex = 0;
509 if(isNeg)
510 val = (SafeUns64)(-(SafeInt64)val);
511
512 do
513 {
514 unsigned curDigit = (unsigned) val % radix;
515 if(curDigit > 9)
516 buf[strIndex++] = curDigit + 'a' - 10;
517 else
518 buf[strIndex++] = curDigit + '0';
519
520 val /= radix;
521 }while(val);
522
523 if(isNeg)
524 buf[strIndex++] = '-';
525 buf[strIndex++] = 0;
526
527 /* Now reverse the string */
528 reverse_string_safe(buf, size);
529 return 0;
530 }
531 #endif
532
533
534 CONDITIONAL_STATIC SafeInt32 _itoa_safe(int val, char* buf, size_t size, int radix)
535 {
536 #if (defined(_WIN32) || defined(_WIN64)) && defined __STDC_WANT_SECURE_LIB__
537 return _itoa_s(val, buf, size, radix);
538 #else
539 if(radix == 10 && val < 0)
540 return _xtox_safe((unsigned long)val, buf, size, radix, 1);
541 else
542 return _xtox_safe((unsigned long)(unsigned int)val, buf, size, radix, 0);
543 #endif
544 }
545
546 CONDITIONAL_STATIC SafeInt32 _ltoa_safe(long val, char *buf, size_t size, int radix)
547 {
548 #if (defined(_WIN32) || defined(_WIN64)) && defined __STDC_WANT_SECURE_LIB__
549 return _ltoa_s(val, buf, size, radix);
550 #else
551 return _xtox_safe((unsigned long)val, buf, size, radix, (radix == 10 && val < 0));
552 #endif
553 }
554
555 CONDITIONAL_STATIC SafeInt32 _ultoa_safe(unsigned long val, char *buf, size_t size, int radix)
556 {
557 #if (defined(_WIN32) || defined(_WIN64)) && defined __STDC_WANT_SECURE_LIB__
558 return _ultoa_s(val, buf, size, radix);
559 #else
560 return _xtox_safe(val, buf, size, radix, 0);
561 #endif
562 }
563
564 CONDITIONAL_STATIC SafeInt32 _i64toa_safe(SafeInt64 val, char* buf, size_t size, int radix)
565 {
566 #if (defined(_WIN32) || defined(_WIN64)) && defined __STDC_WANT_SECURE_LIB__
567 return _i64toa_s(val, buf, size, radix);
568 #else
569 return _x64tox_safe((SafeUns64)val, buf, size, radix, (radix == 10 && val <0 ));
570 #endif
571 }
572
573 CONDITIONAL_STATIC SafeInt32 _ui64toa_safe(SafeUns64 val, char* buf, size_t size, int radix)
574 {
575 #if (defined(_WIN32) || defined(_WIN64)) && defined __STDC_WANT_SECURE_LIB__
576 return _ui64toa_s(val, buf, size, radix);
577 #else
578 return _x64tox_safe(val, buf, size, radix, 0);
579 #endif
580 }
581
582 #if (defined(_WIN32) || defined(_WIN64)) && defined __STDC_WANT_SECURE_LIB__
583 CONDITIONAL_STATIC SafeInt32 _itow_safe(int val, wchar_t *buf, size_t size, int radix)
584 {
585 return _itow_s(val, buf, size, radix);
586 }
587
588 CONDITIONAL_STATIC SafeInt32 _i64tow_safe(SafeInt64 val, wchar_t *buf, size_t size, int radix)
589 {
590 return _i64tow_s(val, buf, size, radix);
591 }
592
593 CONDITIONAL_STATIC SafeInt32 _ui64tow_safe(SafeUns64 val, wchar_t *buf, size_t size, int radix)
594 {
595 return _ui64tow_s(val, buf, size, radix);
596 }
597
598 CONDITIONAL_STATIC SafeInt32 _ltow_safe(long val, wchar_t *str, size_t size, int radix)
599 {
600 return _ltow_s(val, str, size, radix);
601 }
602
603 CONDITIONAL_STATIC SafeInt32 _ultow_safe(unsigned long val, wchar_t *str, size_t size, int radix)
604 {
605 return _ultow_s(val, str, size, radix);
606 }
607 #endif
608
609 CONDITIONAL_STATIC char * gets_safe(char* buffer, size_t size)
610 {
611 #if (defined(_WIN32) || defined(_WIN64)) && defined __STDC_WANT_SECURE_LIB__
612 return gets_s(buffer, size);
613 #else
614 if(buffer && size > 0)
615 {
616 char* pointer = buffer;
617 char ch;
618 size_t count = size;
619 ch = (char)getchar();
620 while(ch != (char)EOF && ch != '\n')
621 {
622 if(count > 0)
623 {
624 count--;
625 *pointer++ = ch;
626 }
627 ch = (char)getchar();
628 }
629 if(count == 0)
630 {
631 *buffer = '\0';
632 }
633 else
634 *pointer = '\0';
635 return buffer;
636 }
637 else
638 {
639 return NULL;
640 }
641 #endif
642 }