| 1 |
/* |
| 2 |
* SRT - Secure, Reliable, Transport |
| 3 |
* Copyright (c) 2018 Haivision Systems Inc. |
| 4 |
* |
| 5 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 6 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 |
* |
| 9 |
*/ |
| 10 |
|
| 11 |
// Implementation file for srt_compat.h |
| 12 |
/***************************************************************************** |
| 13 |
written by |
| 14 |
Haivision Systems Inc. |
| 15 |
*****************************************************************************/ |
| 16 |
|
| 17 |
// Prevents from misconfiguration through preprocessor. |
| 18 |
|
| 19 |
#include "platform_sys.h" |
| 20 |
#include <srt_compat.h> |
| 21 |
|
| 22 |
#include <string.h> |
| 23 |
#include <stdio.h> |
| 24 |
#include <errno.h> |
| 25 |
#if defined(__unix__) && !defined(BSD) && !defined(SUNOS) |
| 26 |
#include <features.h> |
| 27 |
#endif |
| 28 |
|
| 29 |
#if defined(_WIN32) |
| 30 |
#define WIN32_LEAN_AND_MEAN |
| 31 |
#include <windows.h> |
| 32 |
#endif |
| 33 |
|
| 34 |
|
| 35 |
static const char* SysStrError_Fallback(int errnum, char* buf, size_t buflen) |
| 36 |
{ |
| 37 |
#if defined(_MSC_VER) && _MSC_VER < 1900 |
| 38 |
_snprintf(buf, buflen - 1, "ERROR CODE %d", errnum); |
| 39 |
buf[buflen - 1] = '\0'; |
| 40 |
#else |
| 41 |
snprintf(buf, buflen, "ERROR CODE %d", errnum); |
| 42 |
#endif |
| 43 |
return buf; |
| 44 |
} |
| 45 |
|
| 46 |
// This function is a portable and thread-safe version of `strerror`. |
| 47 |
// It requires a user-supplied buffer to store the message. The returned |
| 48 |
// value is always equal to the given buffer pointer. If the system |
| 49 |
// error message is longer than the given buflen, it will be trimmed. |
| 50 |
// When the error code is incorrect for the given error message function, |
| 51 |
// a fallback message will be returned, either as returned by the underlying |
| 52 |
// function, or crafted by this function as a response to error in an |
| 53 |
// underlying function. |
| 54 |
extern const char * SysStrError(int errnum, char * buf, size_t buflen) |
| 55 |
{ |
| 56 |
if (buf == NULL || buflen < 4) // Required to put ??? into it as a fallback |
| 57 |
{ |
| 58 |
errno = EFAULT; |
| 59 |
return buf; |
| 60 |
} |
| 61 |
|
| 62 |
buf[0] = '\0'; |
| 63 |
|
| 64 |
#if defined(_WIN32) |
| 65 |
const char* lpMsgBuf; |
| 66 |
|
| 67 |
// Note: Intentionally the "fixed char size" types are used despite using |
| 68 |
// character size dependent FormatMessage (instead of FormatMessageA) so that |
| 69 |
// your compilation fails when you use wide characters. |
| 70 |
// The problem is that when TCHAR != char, then the buffer written this way |
| 71 |
// would have to be converted to ASCII, not just copied by strncpy. |
| 72 |
FormatMessageA(0 |
| 73 |
| FORMAT_MESSAGE_ALLOCATE_BUFFER |
| 74 |
| FORMAT_MESSAGE_FROM_SYSTEM |
| 75 |
| FORMAT_MESSAGE_IGNORE_INSERTS, |
| 76 |
NULL, // no lpSource |
| 77 |
errnum, // dwMessageId (as controlled by FORMAT_MESSAGE_FROM_SYSTEM) |
| 78 |
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 79 |
// This below parameter normally should contain a pointer to an allocated buffer, |
| 80 |
// and this way it's LPTSTR. But when FORMAT_MESSAGE_ALLOCATE_BUFFER, then it is |
| 81 |
// expected to be a the value of LPTSTR* type, converted to LPTSTR, that designates |
| 82 |
// a pointer to a variable of type LPTSTR, to which the newly allocated buffer is |
| 83 |
// assigned. This buffer should be freed afterwards using LocalFree(). |
| 84 |
(LPSTR)&lpMsgBuf, |
| 85 |
0, NULL); |
| 86 |
|
| 87 |
if (lpMsgBuf) |
| 88 |
{ |
| 89 |
#ifdef _MSC_VER |
| 90 |
strncpy_s(buf, buflen, lpMsgBuf, _TRUNCATE); |
| 91 |
#else |
| 92 |
strncpy(buf, lpMsgBuf, buflen-1); |
| 93 |
buf[buflen-1] = 0; |
| 94 |
#endif |
| 95 |
LocalFree((HLOCAL)lpMsgBuf); |
| 96 |
} |
| 97 |
else |
| 98 |
{ |
| 99 |
SysStrError_Fallback(errnum, buf, buflen); |
| 100 |
} |
| 101 |
|
| 102 |
return buf; |
| 103 |
|
| 104 |
#elif (!defined(__GNU_LIBRARY__) && !defined(__GLIBC__) ) \ |
| 105 |
|| (( (_POSIX_C_SOURCE >= 200112L) || (_XOPEN_SOURCE >= 600)) && ! _GNU_SOURCE ) |
| 106 |
// POSIX/XSI-compliant version. |
| 107 |
// Overall general POSIX version: returns status. |
| 108 |
// 0 for success, otherwise it's: |
| 109 |
// - possibly -1 and the error code is in ::errno |
| 110 |
// - possibly the error code itself |
| 111 |
// The details of the error are not interesting; simply |
| 112 |
// craft a fallback message in this case. |
| 113 |
if (strerror_r(errnum, buf, buflen) != 0) |
| 114 |
{ |
| 115 |
return SysStrError_Fallback(errnum, buf, buflen); |
| 116 |
} |
| 117 |
return buf; |
| 118 |
#else |
| 119 |
// GLIBC is non-standard under these conditions. |
| 120 |
// GNU version: returns the pointer to the message. |
| 121 |
// This is either equal to the local buffer (buf) |
| 122 |
// or some system-wide (constant) storage. To maintain |
| 123 |
// stability of the API, this overall function shall |
| 124 |
// always return the local buffer and the message in |
| 125 |
// this buffer - so these cases should be distinguished |
| 126 |
// and the internal storage copied to the buffer. |
| 127 |
|
| 128 |
char * gnu_buffer = strerror_r(errnum, buf, buflen); |
| 129 |
if (!gnu_buffer) |
| 130 |
{ |
| 131 |
// This should never happen, so just a paranoid check |
| 132 |
return SysStrError_Fallback(errnum, buf, buflen); |
| 133 |
} |
| 134 |
|
| 135 |
// If they are the same, the message is already copied |
| 136 |
// (and it's usually a "fallback message" for an error case). |
| 137 |
if (gnu_buffer != buf) |
| 138 |
{ |
| 139 |
strncpy(buf, gnu_buffer, buflen-1); |
| 140 |
buf[buflen-1] = 0; // guarantee what strncpy doesn't |
| 141 |
} |
| 142 |
|
| 143 |
return buf; |
| 144 |
#endif |
| 145 |
} |