2022-06-17 15:31:52 +02:00
|
|
|
#ifndef MBSEC_H
|
|
|
|
#define MBSEC_H
|
|
|
|
|
2022-11-14 14:59:32 +01:00
|
|
|
#ifdef __GNUC__
|
|
|
|
#define UNUSED __attribute__((__unused__))
|
|
|
|
#else
|
|
|
|
#define UNUSED
|
|
|
|
#endif
|
|
|
|
|
2022-06-17 15:31:52 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
#define UTFCONV_ERROR_INVALID_CONVERSION "Input contains invalid byte sequences."
|
|
|
|
|
2022-11-14 14:59:32 +01:00
|
|
|
static UNUSED LPWSTR utfconv_utf8towc(const char *str) {
|
2022-06-17 15:31:52 +02:00
|
|
|
LPWSTR output;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
// len includes \0
|
|
|
|
len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
|
|
|
|
if (len == 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
output = (LPWSTR) malloc(sizeof(WCHAR) * len);
|
|
|
|
if (output == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
len = MultiByteToWideChar(CP_UTF8, 0, str, -1, output, len);
|
|
|
|
if (len == 0) {
|
|
|
|
free(output);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2022-11-14 14:59:32 +01:00
|
|
|
static UNUSED char *utfconv_wctoutf8(LPCWSTR str) {
|
2022-06-17 15:31:52 +02:00
|
|
|
char *output;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
// len includes \0
|
|
|
|
len = WideCharToMultiByte(CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL);
|
|
|
|
if (len == 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
output = (char *) malloc(sizeof(char) * len);
|
|
|
|
if (output == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
len = WideCharToMultiByte(CP_UTF8, 0, str, -1, output, len, NULL, NULL);
|
|
|
|
if (len == 0) {
|
|
|
|
free(output);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|