Skip to content

Commit

Permalink
Merge branch 'dev41' into unicode
Browse files Browse the repository at this point in the history
  • Loading branch information
starg2 committed May 9, 2021
2 parents c327460 + 0c215c3 commit decbbd2
Show file tree
Hide file tree
Showing 130 changed files with 33,877 additions and 12,513 deletions.
2 changes: 0 additions & 2 deletions libmpg123/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ add_library(
src/libmpg123/getbits.h
src/libmpg123/getcpuflags.h
src/libmpg123/huffman.h
src/libmpg123/l12_integer_tables.h
src/libmpg123/l3_integer_tables.h
src/libmpg123/icy.h
src/libmpg123/icy2utf8.h
src/libmpg123/id3.h
Expand Down
42 changes: 0 additions & 42 deletions libmpg123/src/compat/compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,48 +155,6 @@ int compat_fclose(FILE *stream)
return fclose(stream);
}

/* Windows Unicode stuff */

#ifdef WANT_WIN32_UNICODE
int win32_wide_utf8(const wchar_t * const wptr, char **mbptr, size_t * buflen)
{
size_t len;
char *buf;
int ret = 0;

len = WideCharToMultiByte(CP_UTF8, 0, wptr, -1, NULL, 0, NULL, NULL); /* Get utf-8 string length */
buf = calloc(len + 1, sizeof (char)); /* Can we assume sizeof char always = 1? */

if(!buf) len = 0;
else {
if (len != 0) ret = WideCharToMultiByte(CP_UTF8, 0, wptr, -1, buf, len, NULL, NULL); /*Do actual conversion*/
buf[len] = '0'; /* Must terminate */
}
*mbptr = buf; /* Set string pointer to allocated buffer */
if(buflen != NULL) *buflen = (len) * sizeof (char); /* Give length of allocated memory if needed. */
return ret;
}

int win32_utf8_wide(const char *const mbptr, wchar_t **wptr, size_t *buflen)
{
size_t len;
wchar_t *buf;
int ret = 0;

len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, mbptr, -1, NULL, 0); /* Get converted size */
buf = calloc(len + 1, sizeof (wchar_t)); /* Allocate memory accordingly */

if(!buf) len = 0;
else {
if (len != 0) ret = MultiByteToWideChar (CP_UTF8, MB_ERR_INVALID_CHARS, mbptr, -1, buf, len); /* Do conversion */
buf[len] = L'0'; /* Must terminate */
}
*wptr = buf; /* Set string pointer to allocated buffer */
if (buflen != NULL) *buflen = len * sizeof (wchar_t); /* Give length of allocated memory if needed. */
return ret; /* Number of characters written */
}
#endif

#ifndef WINDOWS_UWP

/*
Expand Down
8 changes: 6 additions & 2 deletions libmpg123/src/compat/compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ typedef ptrdiff_t ssize_t;

/* A safe realloc also for very old systems where realloc(NULL, size) returns NULL. */
void *safe_realloc(void *ptr, size_t size);
// Also freeing ptr if result is NULL. You can do
// ptr = safer_realloc(ptr, size)
// Also, ptr = safer_realloc(ptr, 0) will do free(ptr); ptr=NULL;.
void *safer_realloc(void *ptr, size_t size);
#ifndef HAVE_STRERROR
const char *strerror(int errnum);
#endif
Expand Down Expand Up @@ -193,8 +197,8 @@ int win32_wide_utf8(const wchar_t * const wptr, char **mbptr, size_t * buflen);
* win32_mbc2uni
* Converts a null terminated UTF-8 string to a UCS-2 equivalent.
* Caller is supposed to free allocated buffer.
* @param[out] mbptr Pointer to multibyte string.
* @param[in] wptr Pointer to wide string.
* @param[in] mbptr Pointer to multibyte string.
* @param[out] wptr Pointer to wide string.
* @param[out] buflen Optional parameter for length of allocated buffer.
* @return status of WideCharToMultiByte conversion.
*
Expand Down
108 changes: 108 additions & 0 deletions libmpg123/src/compat/compat_dl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
compat_dl: dynamic loading of shared libs
This is a separate libcompat to avoid needlessly linking all mpg123 code
with libdl on Unix.
copyright 2007-2019 by the mpg123 project - free software under the terms of the LGPL 2.1
see COPYING and AUTHORS files in distribution or http://mpg123.org
initially written by Thomas Orgis, Windows Unicode stuff by JonY.
*/

#include "config.h"
/* This source file does need _POSIX_SOURCE to get some sigaction. */
#define _POSIX_SOURCE
#include "compat.h"

#ifdef _MSC_VER
#include <io.h>

#if(defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_APP))
#define WINDOWS_UWP
#endif

#endif
#ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif
#ifdef HAVE_DIRENT_H
# include <dirent.h>
#endif

/* Win32 is only supported with unicode now. These headers also cover
module stuff. The WANT_WIN32_UNICODE macro is synonymous with
"want windows-specific API, and only the unicode variants of which". */
#ifdef WANT_WIN32_UNICODE
#include <wchar.h>
#include <windows.h>
#include <winnls.h>
#include <shlwapi.h>
#endif

#ifdef USE_MODULES
# ifdef HAVE_DLFCN_H
# include <dlfcn.h>
# endif
#endif

#include "debug.h"

#include "wpathconv.h"

#ifdef USE_MODULES
/*
This is what I expected the platform-specific dance for dynamic module
support to be. Little did I know about the peculiarities of (long)
paths and directory/file search on Windows.
LoadLibrary throws GUI error boxes, use SetThreadErrorMode to suppress.
It needs to be done on per-thread basis to avoid race conditions
clobbering each other when setting/restoring across different threads.
*/

void *compat_dlopen(const char *path)
{
void *handle = NULL;
#ifdef WANT_WIN32_UNICODE
wchar_t *wpath;
wpath = u2wlongpath(path);
if(wpath) {
DWORD emode = GetThreadErrorMode();
int mode_ok = SetThreadErrorMode(emode | SEM_FAILCRITICALERRORS, NULL);
handle = LoadLibraryW(wpath);
if(mode_ok) {
SetThreadErrorMode(emode, NULL);
}
}
free(wpath);
#else
handle = dlopen(path, RTLD_NOW);
#endif
return handle;
}

void *compat_dlsym(void *handle, const char *name)
{
void *sym = NULL;
if(!handle)
return NULL;
#ifdef WANT_WIN32_UNICODE
sym = GetProcAddress(handle, name);
#else
sym = dlsym(handle, name);
#endif
return sym;
}

void compat_dlclose(void *handle)
{
if(!handle)
return;
#ifdef WANT_WIN32_UNICODE
FreeLibrary(handle);
#else
dlclose(handle);
#endif
}

#endif /* USE_MODULES */
62 changes: 62 additions & 0 deletions libmpg123/src/compat/compat_str.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
*/

#include "compat.h"

/* Win32 is only supported with unicode now. These headers also cover
module stuff. The WANT_WIN32_UNICODE macro is synonymous with
"want windows-specific API, and only the unicode variants of which". */
#ifdef WANT_WIN32_UNICODE
#include <wchar.h>
#include <windows.h>
#include <winnls.h>
#endif

#include "debug.h"

/* A safe realloc also for very old systems where realloc(NULL, size) returns NULL. */
Expand All @@ -19,6 +29,16 @@ void *safe_realloc(void *ptr, size_t size)
else return realloc(ptr, size);
}

// A more sensible variant of realloc: It deallocates the original memory if
// realloc fails or if size zero was requested.
void *safer_realloc(void *ptr, size_t size)
{
void *nptr = size ? safe_realloc(ptr, size) : NULL;
if(!nptr && ptr)
free(ptr);
return nptr;
}

#ifndef HAVE_STRERROR
const char *strerror(int errnum)
{
Expand All @@ -41,3 +61,45 @@ char* compat_strdup(const char *src)
}
return dest;
}

/* Windows Unicode stuff */

#ifdef WANT_WIN32_UNICODE
int win32_wide_utf8(const wchar_t * const wptr, char **mbptr, size_t * buflen)
{
size_t len;
char *buf;
int ret = 0;

len = WideCharToMultiByte(CP_UTF8, 0, wptr, -1, NULL, 0, NULL, NULL); /* Get utf-8 string length */
buf = calloc(len + 1, sizeof (char)); /* Can we assume sizeof char always = 1? */

if(!buf) len = 0;
else {
if (len != 0) ret = WideCharToMultiByte(CP_UTF8, 0, wptr, -1, buf, len, NULL, NULL); /*Do actual conversion*/
buf[len] = '0'; /* Must terminate */
}
*mbptr = buf; /* Set string pointer to allocated buffer */
if(buflen != NULL) *buflen = (len) * sizeof (char); /* Give length of allocated memory if needed. */
return ret;
}

int win32_utf8_wide(const char *const mbptr, wchar_t **wptr, size_t *buflen)
{
size_t len;
wchar_t *buf;
int ret = 0;

len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, mbptr, -1, NULL, 0); /* Get converted size */
buf = calloc(len + 1, sizeof (wchar_t)); /* Allocate memory accordingly */

if(!buf) len = 0;
else {
if (len != 0) ret = MultiByteToWideChar (CP_UTF8, MB_ERR_INVALID_CHARS, mbptr, -1, buf, len); /* Do conversion */
buf[len] = L'0'; /* Must terminate */
}
*wptr = buf; /* Set string pointer to allocated buffer */
if (buflen != NULL) *buflen = len * sizeof (wchar_t); /* Give length of allocated memory if needed. */
return ret; /* Number of characters written */
}
#endif
1 change: 0 additions & 1 deletion libmpg123/src/intsym.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@
#define dectype INT123_dectype
#define defdec INT123_defdec
#define decclass INT123_decclass
#define check_decoders INT123_check_decoders
#define read_frame_init INT123_read_frame_init
#define frame_bitrate INT123_frame_bitrate
#define frame_freq INT123_frame_freq
Expand Down
Loading

0 comments on commit decbbd2

Please sign in to comment.