1 /* Copyright (C) 2015 Wildfire Games.
3 * Permission is hereby granted, free of charge, to any person obtaining
4 * a copy of this software and associated documentation files (the
5 * "Software"), to deal in the Software without restriction, including
6 * without limitation the rights to use, copy, modify, merge, publish,
7 * distribute, sublicense, and/or sell copies of the Software, and to
8 * permit persons to whom the Software is furnished to do so, subject to
9 * the following conditions:
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * partial implementation of VC8's secure CRT functions
27 #ifndef INCLUDED_SECURE_CRT
28 #define INCLUDED_SECURE_CRT
32 #include "lib/status.h"
36 const Status STRING_NOT_TERMINATED
= -100600;
39 // if the platform lacks a secure CRT implementation, we'll provide one.
41 # define EMULATE_SECURE_CRT 0
43 # define EMULATE_SECURE_CRT 1
47 #if EMULATE_SECURE_CRT
49 // (conflicts with glibc definitions)
50 #if !OS_UNIX || OS_MACOSX || OS_OPENBSD
51 // return length [in characters] of a string, not including the trailing
52 // null character. to protect against access violations, only the
53 // first <max_len> characters are examined; if the null character is
54 // not encountered by then, <max_len> is returned.
55 // strnlen is available on OpenBSD and MacOS
56 #if !OS_OPENBSD && !OS_MACOSX
57 extern size_t strnlen(const char* str
, size_t max_len
);
59 extern size_t wcsnlen(const wchar_t* str
, size_t max_len
);
62 // copy at most <max_src_chars> (not including trailing null) from
63 // <src> into <dst>, which must not overlap.
64 // if thereby <max_dst_chars> (including null) would be exceeded,
65 // <dst> is set to the empty string and ERANGE returned; otherwise,
66 // 0 is returned to indicate success and that <dst> is null-terminated.
68 // note: padding with zeroes is not called for by NG1031.
69 extern int strncpy_s(char* dst
, size_t max_dst_chars
, const char* src
, size_t max_src_chars
);
70 extern int wcsncpy_s(wchar_t* dst
, size_t max_dst_chars
, const wchar_t* src
, size_t max_src_chars
);
72 // copy <src> (including trailing null) into <dst>, which must not overlap.
73 // if thereby <max_dst_chars> (including null) would be exceeded,
74 // <dst> is set to the empty string and ERANGE returned; otherwise,
75 // 0 is returned to indicate success and that <dst> is null-terminated.
77 // note: implemented as tncpy_s(dst, max_dst_chars, src, SIZE_MAX)
78 extern int strcpy_s(char* dst
, size_t max_dst_chars
, const char* src
);
79 extern int wcscpy_s(wchar_t* dst
, size_t max_dst_chars
, const wchar_t* src
);
81 // append at most <max_src_chars> (not including trailing null) from
82 // <src> to <dst>, which must not overlap.
83 // if thereby <max_dst_chars> (including null) would be exceeded,
84 // <dst> is set to the empty string and ERANGE returned; otherwise,
85 // 0 is returned to indicate success and that <dst> is null-terminated.
86 extern int strncat_s(char* dst
, size_t max_dst_chars
, const char* src
, size_t max_src_chars
);
87 extern int wcsncat_s(wchar_t* dst
, size_t max_dst_chars
, const wchar_t* src
, size_t max_src_chars
);
89 // append <src> to <dst>, which must not overlap.
90 // if thereby <max_dst_chars> (including null) would be exceeded,
91 // <dst> is set to the empty string and ERANGE returned; otherwise,
92 // 0 is returned to indicate success and that <dst> is null-terminated.
94 // note: implemented as tncat_s(dst, max_dst_chars, src, SIZE_MAX)
95 extern int strcat_s(char* dst
, size_t max_dst_chars
, const char* src
);
96 extern int wcscat_s(wchar_t* dst
, size_t max_dst_chars
, const wchar_t* src
);
98 extern int vsprintf_s(char* dst
, size_t max_dst_chars
, const char* fmt
, va_list ap
) VPRINTF_ARGS(3);
99 extern int vswprintf_s(wchar_t* dst
, size_t max_dst_chars
, const wchar_t* fmt
, va_list ap
) VWPRINTF_ARGS(3);
101 extern int sprintf_s(char* buf
, size_t max_chars
, const char* fmt
, ...) PRINTF_ARGS(3);
102 extern int swprintf_s(wchar_t* buf
, size_t max_chars
, const wchar_t* fmt
, ...) WPRINTF_ARGS(3);
104 // we'd like to avoid deprecation warnings caused by scanf. selective
105 // 'undeprecation' isn't possible, replacing all stdio declarations with
106 // our own deprecation scheme is a lot of work, suppressing all deprecation
107 // warnings would cause important other warnings to be missed, and avoiding
108 // scanf outright isn't convenient.
109 // the remaining alternative is using scanf_s where available and otherwise
110 // defining it to scanf. note that scanf_s has a different API:
111 // any %s or %c or %[ format specifier's buffer must be followed by a
112 // size parameter. callers must either avoid these, or provide two codepaths
113 // (use scanf #if EMULATE_SECURE_CRT, otherwise scanf_s).
114 #define scanf_s scanf
115 #define wscanf_s wscanf
116 #define fscanf_s fscanf
117 #define fwscanf_s fwscanf
118 #define sscanf_s sscanf
119 #define swscanf_s swscanf
121 #endif // #if EMULATE_SECURE_CRT
122 #endif // #ifndef INCLUDED_SECURE_CRT