Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
secure_crt.h
Go to the documentation of this file.
1/* Copyright (C) 2015 Wildfire Games.
2 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
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.
21 */
22
23/*
24 * partial implementation of VC8's secure CRT functions
25 */
26
27#ifndef INCLUDED_SECURE_CRT
28#define INCLUDED_SECURE_CRT
29
30#include <stdarg.h>
31
32#include "lib/status.h"
33
34namespace ERR
35{
37}
38
39// if the platform lacks a secure CRT implementation, we'll provide one.
40#if MSC_VERSION
41# define EMULATE_SECURE_CRT 0
42#else
43# define EMULATE_SECURE_CRT 1
44#endif
45
46
47#if EMULATE_SECURE_CRT
48
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
57extern size_t strnlen(const char* str, size_t max_len);
58#endif
59extern size_t wcsnlen(const wchar_t* str, size_t max_len);
60#endif
61
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.
67//
68// note: padding with zeroes is not called for by NG1031.
69extern int strncpy_s(char* dst, size_t max_dst_chars, const char* src, size_t max_src_chars);
70extern int wcsncpy_s(wchar_t* dst, size_t max_dst_chars, const wchar_t* src, size_t max_src_chars);
71
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.
76//
77// note: implemented as tncpy_s(dst, max_dst_chars, src, SIZE_MAX)
78extern int strcpy_s(char* dst, size_t max_dst_chars, const char* src);
79extern int wcscpy_s(wchar_t* dst, size_t max_dst_chars, const wchar_t* src);
80
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.
86extern int strncat_s(char* dst, size_t max_dst_chars, const char* src, size_t max_src_chars);
87extern int wcsncat_s(wchar_t* dst, size_t max_dst_chars, const wchar_t* src, size_t max_src_chars);
88
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.
93//
94// note: implemented as tncat_s(dst, max_dst_chars, src, SIZE_MAX)
95extern int strcat_s(char* dst, size_t max_dst_chars, const char* src);
96extern int wcscat_s(wchar_t* dst, size_t max_dst_chars, const wchar_t* src);
97
98extern int vsprintf_s(char* dst, size_t max_dst_chars, const char* fmt, va_list ap) VPRINTF_ARGS(3);
99extern int vswprintf_s(wchar_t* dst, size_t max_dst_chars, const wchar_t* fmt, va_list ap) VWPRINTF_ARGS(3);
100
101extern int sprintf_s(char* buf, size_t max_chars, const char* fmt, ...) PRINTF_ARGS(3);
102extern int swprintf_s(wchar_t* buf, size_t max_chars, const wchar_t* fmt, ...) WPRINTF_ARGS(3);
103
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
120
121#endif // #if EMULATE_SECURE_CRT
122#endif // #ifndef INCLUDED_SECURE_CRT
#define WPRINTF_ARGS(fmtpos)
Definition: code_annotation.h:259
#define PRINTF_ARGS(fmtpos)
Definition: code_annotation.h:257
#define VWPRINTF_ARGS(fmtpos)
Definition: code_annotation.h:260
#define VPRINTF_ARGS(fmtpos)
Definition: code_annotation.h:258
Definition: debug.h:395
const Status STRING_NOT_TERMINATED
Definition: secure_crt.h:36
size_t strnlen(const char *str, size_t max_len)
int swprintf_s(wchar_t *buf, size_t max_chars, const wchar_t *fmt,...) WPRINTF_ARGS(3)
int strncpy_s(char *dst, size_t max_dst_chars, const char *src, size_t max_src_chars)
int wcsncat_s(wchar_t *dst, size_t max_dst_chars, const wchar_t *src, size_t max_src_chars)
size_t wcsnlen(const wchar_t *str, size_t max_len)
int wcscpy_s(wchar_t *dst, size_t max_dst_chars, const wchar_t *src)
int wcscat_s(wchar_t *dst, size_t max_dst_chars, const wchar_t *src)
int vswprintf_s(wchar_t *dst, size_t max_dst_chars, const wchar_t *fmt, va_list ap) VWPRINTF_ARGS(3)
int wcsncpy_s(wchar_t *dst, size_t max_dst_chars, const wchar_t *src, size_t max_src_chars)
int strcpy_s(char *dst, size_t max_dst_chars, const char *src)
int strcat_s(char *dst, size_t max_dst_chars, const char *src)
int sprintf_s(char *buf, size_t max_chars, const char *fmt,...) PRINTF_ARGS(3)
int vsprintf_s(char *dst, size_t max_dst_chars, const char *fmt, va_list ap) VPRINTF_ARGS(3)
int strncat_s(char *dst, size_t max_dst_chars, const char *src, size_t max_src_chars)
i64 Status
Error handling system.
Definition: status.h:173