| 1 |
/* |
| 2 |
compat: Some compatibility functions (basic memory and string stuff) |
| 3 |
|
| 4 |
The mpg123 code is determined to keep it's legacy. A legacy of old, old UNIX. |
| 5 |
So anything possibly somewhat advanced should be considered to be put here, with proper #ifdef;-) |
| 6 |
|
| 7 |
copyright 2007-2023 by the mpg123 project |
| 8 |
free software under the terms of the LGPL 2.1 |
| 9 |
see COPYING and AUTHORS files in distribution or http://mpg123.org |
| 10 |
initially written by Thomas Orgis, Windows Unicode stuff by JonY. |
| 11 |
*/ |
| 12 |
// Need POSIX 2008 for uselocale stuff. |
| 13 |
#define _POSIX_C_SOURCE 200809L |
| 14 |
#include "compat.h" |
| 15 |
|
| 16 |
/* Win32 is only supported with unicode now. These headers also cover |
| 17 |
module stuff. The WANT_WIN32_UNICODE macro is synonymous with |
| 18 |
"want windows-specific API, and only the unicode variants of which". */ |
| 19 |
#if defined (_WIN32) || defined (__CYGWIN__) |
| 20 |
#include <wchar.h> |
| 21 |
#include <windows.h> |
| 22 |
#include <winnls.h> |
| 23 |
#endif |
| 24 |
|
| 25 |
#if HAVE_LOCALE_H |
| 26 |
#include <locale.h> |
| 27 |
#endif |
| 28 |
|
| 29 |
#include "../common/debug.h" |
| 30 |
|
| 31 |
/* A safe realloc also for very old systems where realloc(NULL, size) returns NULL. */ |
| 32 |
void *INT123_safe_realloc(void *ptr, size_t size) |
| 33 |
{ |
| 34 |
if(ptr == NULL) return malloc(size); |
| 35 |
else return realloc(ptr, size); |
| 36 |
} |
| 37 |
|
| 38 |
// A more sensible variant of realloc: It deallocates the original memory if |
| 39 |
// realloc fails or if size zero was requested. |
| 40 |
void *INT123_safer_realloc(void *ptr, size_t size) |
| 41 |
{ |
| 42 |
void *nptr = size ? INT123_safe_realloc(ptr, size) : NULL; |
| 43 |
if(!nptr && ptr) |
| 44 |
free(ptr); |
| 45 |
return nptr; |
| 46 |
} |
| 47 |
|
| 48 |
const char *INT123_strerror(int errnum) |
| 49 |
{ |
| 50 |
#if defined(HAVE_STRERROR_L) && defined(HAVE_USELOCALE) |
| 51 |
locale_t curloc = uselocale((locale_t)0); |
| 52 |
if(curloc != LC_GLOBAL_LOCALE) |
| 53 |
return strerror_l(errnum, curloc); |
| 54 |
#endif |
| 55 |
// Also fall back to strerror() in case of no set locale. |
| 56 |
#if defined(HAVE_STRERROR) |
| 57 |
return strerror(errnum); |
| 58 |
#else |
| 59 |
extern int sys_nerr; |
| 60 |
extern char *sys_errlist[]; |
| 61 |
|
| 62 |
return (errnum < sys_nerr) ? sys_errlist[errnum] : ""; |
| 63 |
#endif |
| 64 |
} |
| 65 |
|
| 66 |
char* INT123_compat_strdup(const char *src) |
| 67 |
{ |
| 68 |
char *dest = NULL; |
| 69 |
if(src) |
| 70 |
{ |
| 71 |
size_t len; |
| 72 |
len = strlen(src)+1; |
| 73 |
if((dest = malloc(len))) |
| 74 |
memcpy(dest, src, len); |
| 75 |
} |
| 76 |
return dest; |
| 77 |
} |
| 78 |
|
| 79 |
/* Windows Unicode stuff */ |
| 80 |
/* Provided unconditionally, since WASAPI needs it to configure the audio device */ |
| 81 |
#if defined (_WIN32) || defined (__CYGWIN__) |
| 82 |
static |
| 83 |
int win32_wide_common(const wchar_t * const wptr, char **mbptr, size_t * buflen, UINT cp) |
| 84 |
{ |
| 85 |
size_t len; |
| 86 |
char *buf; |
| 87 |
int ret = 0; |
| 88 |
|
| 89 |
len = WideCharToMultiByte(cp, 0, wptr, -1, NULL, 0, NULL, NULL); /* Get utf-8 string length */ |
| 90 |
buf = calloc(len + 1, sizeof (char)); /* Can we assume sizeof char always = 1? */ |
| 91 |
|
| 92 |
if(!buf) len = 0; |
| 93 |
else { |
| 94 |
if (len != 0) ret = WideCharToMultiByte(cp, 0, wptr, -1, buf, len, NULL, NULL); /*Do actual conversion*/ |
| 95 |
buf[len] = '0'; /* Must terminate */ |
| 96 |
} |
| 97 |
*mbptr = buf; /* Set string pointer to allocated buffer */ |
| 98 |
if(buflen != NULL) *buflen = (len) * sizeof (char); /* Give length of allocated memory if needed. */ |
| 99 |
return ret; |
| 100 |
} |
| 101 |
|
| 102 |
int INT123_win32_wide_utf8(const wchar_t * const wptr, char **mbptr, size_t * buflen) |
| 103 |
{ |
| 104 |
return win32_wide_common(wptr, mbptr, buflen, CP_UTF8); |
| 105 |
} |
| 106 |
|
| 107 |
int INT123_win32_wide_utf7(const wchar_t * const wptr, char **mbptr, size_t * buflen) |
| 108 |
{ |
| 109 |
return win32_wide_common(wptr, mbptr, buflen, CP_UTF7); |
| 110 |
} |
| 111 |
|
| 112 |
int INT123_win32_utf8_wide(const char *const mbptr, wchar_t **wptr, size_t *buflen) |
| 113 |
{ |
| 114 |
size_t len; |
| 115 |
wchar_t *buf; |
| 116 |
int ret = 0; |
| 117 |
|
| 118 |
len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, mbptr, -1, NULL, 0); /* Get converted size */ |
| 119 |
buf = calloc(len + 1, sizeof (wchar_t)); /* Allocate memory accordingly */ |
| 120 |
|
| 121 |
if(!buf) len = 0; |
| 122 |
else { |
| 123 |
if (len != 0) ret = MultiByteToWideChar (CP_UTF8, MB_ERR_INVALID_CHARS, mbptr, -1, buf, len); /* Do conversion */ |
| 124 |
buf[len] = L'0'; /* Must terminate */ |
| 125 |
} |
| 126 |
*wptr = buf; /* Set string pointer to allocated buffer */ |
| 127 |
if (buflen != NULL) *buflen = len * sizeof (wchar_t); /* Give length of allocated memory if needed. */ |
| 128 |
return ret; /* Number of characters written */ |
| 129 |
} |
| 130 |
#endif |