michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef _CSF_COMMON_E58E5677_950A_424c_B6C2_CA180092E6A2_H michael@0: #define _CSF_COMMON_E58E5677_950A_424c_B6C2_CA180092E6A2_H michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: /* michael@0: michael@0: This header file defines: michael@0: michael@0: csf_countof michael@0: csf_sprintf michael@0: csf_vsprintf michael@0: michael@0: */ michael@0: michael@0: /* michael@0: General security tip: Ensure that "format" is never a user-defined string. Format should ALWAYS be something that's built into your code, not michael@0: user supplied. For example: never write: michael@0: michael@0: csf_sprintf(buffer, csf_countof(buffer), pUserSuppliedString); michael@0: michael@0: Instead write: michael@0: michael@0: csf_sprintf(buffer, csf_countof(buffer), "%s", pUserSuppliedString); michael@0: michael@0: */ michael@0: michael@0: #ifdef WIN32 michael@0: #if !defined(_countof) michael@0: #if !defined(__cplusplus) michael@0: #define _countof(_Array) (sizeof(_Array) / sizeof(_Array[0])) michael@0: #else michael@0: extern "C++" michael@0: { michael@0: template michael@0: char (*_csf_countof_helper(_CountofType (&_Array)[_SizeOfArray]))[_SizeOfArray]; michael@0: #define _countof(_Array) sizeof(*_csf_countof_helper(_Array)) michael@0: } michael@0: #endif michael@0: #endif michael@0: #else michael@0: #define _countof(_Array) (sizeof(_Array) / sizeof(_Array[0])) michael@0: #endif michael@0: //csf_countof michael@0: michael@0: #define csf_countof(anArray) _countof(anArray) michael@0: michael@0: //csf_sprintf michael@0: michael@0: #ifdef _WIN32 michael@0: //Unlike snprintf, sprintf_s guarantees that the buffer will be null-terminated (unless the buffer size is zero). michael@0: #define csf_sprintf(/* char* */ buffer, /* size_t */ sizeOfBufferInCharsInclNullTerm, /* const char * */ format, ...)\ michael@0: _snprintf_s (buffer, sizeOfBufferInCharsInclNullTerm, _TRUNCATE, format, __VA_ARGS__) michael@0: #else michael@0: #define csf_sprintf(/* char */ buffer, /* size_t */ sizeOfBufferInCharsInclNullTerm, /* const char * */ format, ...)\ michael@0: snprintf (buffer, sizeOfBufferInCharsInclNullTerm, format, __VA_ARGS__);\ michael@0: buffer[sizeOfBufferInCharsInclNullTerm-1] = '\0' michael@0: #endif michael@0: michael@0: //csf_vsprintf michael@0: michael@0: #ifdef _WIN32 michael@0: #define csf_vsprintf(/* char* */ buffer, /* size_t */ sizeOfBufferInCharsInclNullTerm, /* const char * */ format, /* va_list */ vaList)\ michael@0: vsnprintf_s (buffer, sizeOfBufferInCharsInclNullTerm, _TRUNCATE, format, vaList);\ michael@0: buffer[sizeOfBufferInCharsInclNullTerm-1] = '\0' michael@0: #else michael@0: #define csf_vsprintf(/* char */ buffer, /* size_t */ sizeOfBufferInCharsInclNullTerm, /* const char * */ format, /* va_list */ vaList)\ michael@0: vsprintf (buffer, format, vaList);\ michael@0: buffer[sizeOfBufferInCharsInclNullTerm-1] = '\0' michael@0: #endif michael@0: michael@0: #endif