summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/common/csf_common.h
blob: e8c2d1630b0d93af71a384a8a78aec8b74eb381b (plain)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef _CSF_COMMON_E58E5677_950A_424c_B6C2_CA180092E6A2_H
#define _CSF_COMMON_E58E5677_950A_424c_B6C2_CA180092E6A2_H

/*

This header file defines:

csf_countof
csf_sprintf
csf_vsprintf

*/

/*
  General security tip: Ensure that "format" is never a user-defined string.
  Format should ALWAYS be something that's built into your code, not user
  supplied. For example: never write:

  csf_sprintf(buffer, csf_countof(buffer), pUserSuppliedString);

  Instead write:

  csf_sprintf(buffer, csf_countof(buffer), "%s", pUserSuppliedString);

*/

#ifdef WIN32
#  if !defined(_countof)
#    if !defined(__cplusplus)
#      define _countof(_Array) (sizeof(_Array) / sizeof(_Array[0]))
#    else
extern "C++" {
template <typename _CountofType, size_t _SizeOfArray>
char (*_csf_countof_helper(_CountofType (&_Array)[_SizeOfArray]))[_SizeOfArray];
#      define _countof(_Array) sizeof(*_csf_countof_helper(_Array))
}
#    endif
#  endif
#else
#  define _countof(_Array) (sizeof(_Array) / sizeof(_Array[0]))
#endif
// csf_countof

#define csf_countof(anArray) _countof(anArray)

// csf_sprintf

#ifdef _WIN32
// Unlike snprintf, sprintf_s guarantees that the buffer will be null-terminated
// (unless the buffer size is zero).
#  define csf_sprintf(/* char* */ buffer,                                   \
                      /* size_t */ sizeOfBufferInCharsInclNullTerm,         \
                      /* const char * */ format, ...)                       \
    _snprintf_s(buffer, sizeOfBufferInCharsInclNullTerm, _TRUNCATE, format, \
                __VA_ARGS__)
#else
#  define csf_sprintf(/* char */ buffer,                                    \
                      /* size_t */ sizeOfBufferInCharsInclNullTerm,         \
                      /* const char * */ format, ...)                       \
    snprintf(buffer, sizeOfBufferInCharsInclNullTerm, format, __VA_ARGS__); \
    buffer[sizeOfBufferInCharsInclNullTerm - 1] = '\0'
#endif

// csf_vsprintf

#ifdef _WIN32
#  define csf_vsprintf(/* char* */ buffer,                                  \
                       /* size_t */ sizeOfBufferInCharsInclNullTerm,        \
                       /* const char * */ format, /* va_list */ vaList)     \
    vsnprintf_s(buffer, sizeOfBufferInCharsInclNullTerm, _TRUNCATE, format, \
                vaList);                                                    \
    buffer[sizeOfBufferInCharsInclNullTerm - 1] = '\0'
#else
#  define csf_vsprintf(/* char */ buffer,                               \
                       /* size_t */ sizeOfBufferInCharsInclNullTerm,    \
                       /* const char * */ format, /* va_list */ vaList) \
    vsprintf(buffer, format, vaList);                                   \
    buffer[sizeOfBufferInCharsInclNullTerm - 1] = '\0'
#endif

#endif