Skip to content

Instantly share code, notes, and snippets.

@heri3x
Created May 9, 2019 07:49
Show Gist options
  • Save heri3x/bcf2bc3162b622bdac9b2f26b85e4050 to your computer and use it in GitHub Desktop.
Save heri3x/bcf2bc3162b622bdac9b2f26b85e4050 to your computer and use it in GitHub Desktop.
NSIS Unicode Plugin for x86-unicode
@echo Building "unicode.dll"
@CALL "C:\Program Files\Microsoft Visual Studio\2017\Community\Common7\Tools\VsDevCmd.bat"
cl /O1 unicode.c /DUNICODE /D_UNICODE /Gs40960 /GS- /LD /link kernel32.lib user32.lib /NODEFAULTLIB /ENTRY:DllMain
@pause
#ifndef _EXDLL_H_
#define _EXDLL_H_
#include <windows.h>
#if defined(__GNUC__)
#define UNUSED __attribute__((unused))
#else
#define UNUSED
#endif
// only include this file from one place in your DLL.
// (it is all static, if you use it in two places it will fail)
#define EXDLL_INIT() { \
g_stringsize=string_size; \
g_stacktop=stacktop; \
g_variables=variables; }
// For page showing plug-ins
#define WM_NOTIFY_OUTER_NEXT (WM_USER+0x8)
#define WM_NOTIFY_CUSTOM_READY (WM_USER+0xd)
#define NOTIFY_BYE_BYE 'x'
typedef struct _stack_t {
struct _stack_t *next;
#ifdef UNICODE
WCHAR text[1]; // this should be the length of g_stringsize when allocating
#else
char text[1];
#endif
} stack_t;
static unsigned int g_stringsize;
static stack_t **g_stacktop;
static char *g_variables;
static int __stdcall popstringW(LPTSTR str);
static void __stdcall pushstringW(LPCTSTR str);
static char * __stdcall getuservariable(const int varnum) UNUSED;
static void __stdcall setuservariable(const int varnum, const char *var) UNUSED;
enum
{
INST_0, // $0
INST_1, // $1
INST_2, // $2
INST_3, // $3
INST_4, // $4
INST_5, // $5
INST_6, // $6
INST_7, // $7
INST_8, // $8
INST_9, // $9
INST_R0, // $R0
INST_R1, // $R1
INST_R2, // $R2
INST_R3, // $R3
INST_R4, // $R4
INST_R5, // $R5
INST_R6, // $R6
INST_R7, // $R7
INST_R8, // $R8
INST_R9, // $R9
INST_CMDLINE, // $CMDLINE
INST_INSTDIR, // $INSTDIR
INST_OUTDIR, // $OUTDIR
INST_EXEDIR, // $EXEDIR
INST_LANG, // $LANGUAGE
__INST_LAST
};
typedef struct {
int autoclose;
int all_user_var;
int exec_error;
int abort;
int exec_reboot;
int reboot_called;
int XXX_cur_insttype; // deprecated
int XXX_insttype_changed; // deprecated
int silent;
int instdir_error;
int rtl;
int errlvl;
int alter_reg_view;
int status_update;
} exec_flags_type;
typedef struct {
exec_flags_type *exec_flags;
int (__stdcall *ExecuteCodeSegment)(int, HWND);
void (__stdcall *validate_filename)(char *);
} extra_parameters;
static int __stdcall popstringW(LPTSTR str)
{
stack_t *th;
if (!g_stacktop || !*g_stacktop) return 1;
th=(*g_stacktop);
if (str) lstrcpyW(str,th->text);
*g_stacktop = th->next;
GlobalFree((HGLOBAL)th);
return 0;
}
static void __stdcall pushstringW(LPCTSTR str)
{
stack_t *th;
if (!g_stacktop) return;
th=(stack_t*)GlobalAlloc(GPTR,(sizeof(stack_t)+(g_stringsize)*sizeof(*str)));
lstrcpynW(th->text,str,g_stringsize);
th->next=*g_stacktop;
*g_stacktop=th;
}
static char * __stdcall getuservariable(const int varnum)
{
if (varnum < 0 || varnum >= __INST_LAST) return NULL;
return g_variables+varnum*g_stringsize;
}
static void __stdcall setuservariable(const int varnum, const char *var)
{
if (var != NULL && varnum >= 0 && varnum < __INST_LAST)
lstrcpyA(g_variables + varnum*g_stringsize, var);
}
#endif//_EXDLL_H_
/*
NSIS Unicode Plugin for x86-unicode
How to build with Visual Studio 2017:
> VsDevCmd.bat
> cl /O1 unicode.c /DUNICODE /D_UNICODE /Gs40960 /GS- /LD /link kernel32.lib user32.lib /NODEFAULTLIB /ENTRY:DllMain
*/
/*****************************************************************
* NSIS plugin for Unicode files conversion v1.1 *
* *
* 2005 Shengalts Aleksander aka Instructor ([email protected]) *
* 2009 Axel Mock ([email protected] ) *
*****************************************************************/
#include <windows.h>
#include "exdll.h"
int __UnicodeType(TCHAR *chInFile);
void __declspec(dllexport) __FileUnicode2UTF8(TCHAR *chInFile, TCHAR *chOutFile, TCHAR *chUnicodeType);
void __declspec(dllexport) __FileUnicode2Ansi(TCHAR *chInFile, TCHAR *chOutFile, TCHAR *chUnicodeType);
void __declspec(dllexport) __FileAnsi2Unicode(TCHAR *chInFile, TCHAR *chOutFile, TCHAR *chUnicodeType);
void __declspec(dllexport) UnicodeType(HWND hwndParent, int string_size,
char *variables, stack_t **stacktop,
extra_parameters *extra)
{
EXDLL_INIT();
{
TCHAR chInFile[1024];
TCHAR error[2]=L"0";
popstringW(chInFile);
if (chInFile[0] == 0)
{
TCHAR error[2]=L"1";
pushstringW(error);
return;
}
__UnicodeType(chInFile);
}
}
void __declspec(dllexport) FileUnicode2Ansi(HWND hwndParent, int string_size,
char *variables, stack_t **stacktop,
extra_parameters *extra)
{
EXDLL_INIT();
{
TCHAR chInFile[1024];
TCHAR chOutFile[1024];
TCHAR chUnicodeType[1024];
TCHAR error[2]=L"0";
//Get parameters
popstringW(chInFile);
if (chInFile[0] == 0)
{
error[0]=L'1';
goto exit;
}
popstringW(chOutFile);
if (chOutFile[0] == 0)
{
error[0]=L'1';
goto exit;
}
popstringW(chUnicodeType);
if (chUnicodeType[0] == 0)
{
error[0]=L'1';
goto exit;
}
if ((lstrcmpiW(chUnicodeType, L"AUTO") != 0) &&
(lstrcmpiW(chUnicodeType, L"UTF-8") != 0) &&
(lstrcmpiW(chUnicodeType, L"UTF-16LE") != 0) &&
(lstrcmpiW(chUnicodeType, L"UTF-16BE") != 0))
{
error[0]='2';
goto exit;
}
__FileUnicode2Ansi (chInFile, chOutFile, chUnicodeType);
return;
//Exit
exit:
pushstringW(error);
}
}
void __declspec(dllexport) __FileUnicode2Ansi(TCHAR *chInFile, TCHAR *chOutFile, TCHAR *chUnicodeType)
{
HANDLE hFileRead=0;
HANDLE hFileWrite=0;
TCHAR error[2]=L"0";
int nUnicodeType=0;
int nMultiByteLen=0;
DWORD dwNumberOfBytesRead=0;
DWORD dwNumberOfBytesWritten=0;
DWORD dwBytesToRead=0;
void *pReadBuffer=NULL;
char *pBufferMultiByte=NULL;
WCHAR *pBufferWideCharLEw=NULL;
char *pBufferWideCharLEa=NULL;
//Get unicode file type
if (lstrcmpiW(chUnicodeType, L"AUTO") == 0)
{
nUnicodeType=__UnicodeType(chInFile);
if (nUnicodeType == -1)
return;
else if (nUnicodeType == 0)
{
popstringW(NULL);
error[0]='4';
goto exit;
}
else if ((nUnicodeType == 44) || (nUnicodeType == 46))
{
popstringW(NULL);
error[0]='5';
goto exit;
}
}
//ReadFile
hFileRead=CreateFile(chInFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hFileRead == INVALID_HANDLE_VALUE)
{
error[0]=L'6';
goto exit;
}
dwBytesToRead=GetFileSize(hFileRead, NULL);
pReadBuffer=GlobalAlloc(GPTR, dwBytesToRead+1);
ReadFile(hFileRead, pReadBuffer, dwBytesToRead, &dwNumberOfBytesRead, NULL);
CloseHandle(hFileRead);
//ConvertFile
if ((nUnicodeType == 13) || (lstrcmpiW(chUnicodeType, L"UTF-8") == 0))
{
//UTF-8 -> UTF-16LE
pBufferWideCharLEw=(WCHAR*)GlobalAlloc(GPTR, dwBytesToRead*2);
MultiByteToWideChar(CP_UTF8, 0, pReadBuffer, -1, pBufferWideCharLEw, dwBytesToRead);
//UTF-16LE -> ANSI
pBufferMultiByte=(char*)GlobalAlloc(GPTR, dwBytesToRead);
nMultiByteLen=WideCharToMultiByte(CP_ACP, 0, pBufferWideCharLEw, -1, pBufferMultiByte, dwBytesToRead, 0, 0);
--nMultiByteLen;
if (lstrcmpiW(chUnicodeType, L"UTF-8") != 0)
{
++pBufferMultiByte;
--nMultiByteLen;
}
}
else if ((nUnicodeType == 22) || (lstrcmpiW(chUnicodeType, L"UTF-16LE") == 0))
{
//UTF-16LE -> ANSI
pBufferMultiByte=(char*)GlobalAlloc(GPTR, dwBytesToRead/2);
nMultiByteLen=WideCharToMultiByte(CP_ACP, WC_DEFAULTCHAR, pReadBuffer, dwBytesToRead/2, pBufferMultiByte, dwBytesToRead, 0, 0);
if (lstrcmpiW(chUnicodeType, L"UTF-16LE") != 0)
{
++pBufferMultiByte;
--nMultiByteLen;
}
}
else if ((nUnicodeType == 23) || (lstrcmpiW(chUnicodeType, L"UTF-16BE") == 0))
{
DWORD dwCount=0;
DWORD dwCount2=0;
char *pBufferWideCharLEa2=NULL;
//UTF-16BE -> UTF-16LE
pBufferWideCharLEa=(char*)GlobalAlloc(GPTR, dwBytesToRead);
pBufferWideCharLEa2=(char*)pReadBuffer;
for (dwCount=0, dwCount2=1; dwCount != dwBytesToRead; dwCount+=2, dwCount2+=2)
{
pBufferWideCharLEa[dwCount]=pBufferWideCharLEa2[dwCount2];
pBufferWideCharLEa[dwCount2]=pBufferWideCharLEa2[dwCount];
}
pBufferWideCharLEw=(WCHAR*)pBufferWideCharLEa;
//UTF-16LE -> ANSI
pBufferMultiByte=(char*)GlobalAlloc(GPTR, dwBytesToRead/2);
nMultiByteLen=WideCharToMultiByte(CP_ACP, 0, pBufferWideCharLEw, dwBytesToRead/2, pBufferMultiByte, dwBytesToRead, 0, 0);
if (lstrcmpiW(chUnicodeType, L"UTF-16BE") != 0)
{
++pBufferMultiByte;
--nMultiByteLen;
}
}
//WriteFile
hFileWrite=CreateFile(chOutFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (hFileWrite == INVALID_HANDLE_VALUE)
{
error[0]=L'7';
goto exit;
}
WriteFile(hFileWrite, pBufferMultiByte, nMultiByteLen, &dwNumberOfBytesWritten, NULL);
CloseHandle(hFileWrite);
//Exit
exit:
if (pReadBuffer)
GlobalFree(pReadBuffer);
if (pBufferMultiByte)
GlobalFree(pBufferMultiByte);
if (pBufferWideCharLEw)
GlobalFree(pBufferWideCharLEw);
pushstringW(error);
}
void __declspec(dllexport) FileAnsi2Unicode(HWND hwndParent, int string_size,
char *variables, stack_t **stacktop,
extra_parameters *extra)
{
EXDLL_INIT();
{
TCHAR chInFile[1024];
TCHAR chOutFile[1024];
TCHAR chUnicodeType[1024];
TCHAR error[2]=L"0";
//Get parameters
popstringW(chInFile);
if (chInFile[0] == 0)
{
error[0]=L'1';
goto exit;
}
popstringW(chOutFile);
if (chOutFile[0] == 0)
{
error[0]=L'1';
goto exit;
}
popstringW(chUnicodeType);
if (chUnicodeType[0] == 0)
{
error[0]=L'1';
goto exit;
}
__FileAnsi2Unicode (chInFile, chOutFile, chUnicodeType);
return;
//Exit
exit:
pushstringW(error);
}
}
void __declspec(dllexport) __FileAnsi2Unicode(TCHAR *chInFile, TCHAR *chOutFile, TCHAR *chUnicodeType)
{
HANDLE hFileRead=0;
HANDLE hFileWrite=0;
TCHAR error[2]=L"0";
int nUnicodeType=0;
int nMultiByteLen=0;
int nBufferLen=0;
DWORD dwNumberOfBytesRead=0;
DWORD dwNumberOfBytesWritten=0;
DWORD dwBytesToRead=0;
DWORD dwWideCharByteLen=0;
void *pReadBuffer=NULL;
char *pBufferMultiByte=NULL;
WCHAR *pBufferWideCharLEw=NULL;
void *pBufferWrite=NULL;
char *pBufferWideCharBEa=NULL;
//Get unicode file type
nUnicodeType=__UnicodeType(chInFile);
if (nUnicodeType == -1)
return;
else if (nUnicodeType != 0)
{
popstringW(NULL);
error[0]=L'3';
goto exit;
}
//ReadFile
hFileRead=CreateFile(chInFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hFileRead == INVALID_HANDLE_VALUE)
{
error[0]=L'6';
goto exit;
}
dwBytesToRead=GetFileSize(hFileRead, NULL);
pReadBuffer=GlobalAlloc(GPTR, dwBytesToRead+1);
ReadFile(hFileRead, pReadBuffer, dwBytesToRead, &dwNumberOfBytesRead, NULL);
CloseHandle(hFileRead);
//ConvertFile
if (lstrcmpiW(chUnicodeType, L"UTF-8") == 0)
{
//ANSI -> UTF-16LE
pBufferWideCharLEw=(WCHAR*)GlobalAlloc(GPTR, dwBytesToRead*2);
MultiByteToWideChar(CP_ACP, 0, pReadBuffer, dwBytesToRead, pBufferWideCharLEw, dwBytesToRead);
//Add BOM
pBufferMultiByte=(char*)GlobalAlloc(GPTR, dwBytesToRead*2);
pBufferMultiByte[0]=(char)0xEF;
pBufferMultiByte[1]=(char)0xBB;
pBufferMultiByte[2]=(char)0xBF;
pBufferMultiByte+=3;
//UTF-16LE -> UTF-8
nBufferLen=WideCharToMultiByte(CP_UTF8, 0, pBufferWideCharLEw, dwBytesToRead, pBufferMultiByte, dwBytesToRead*2, 0, 0);
pBufferWrite=pBufferMultiByte - 3;
nBufferLen+=3;
}
else if (lstrcmpiW(chUnicodeType, L"UTF-16LE") == 0)
{
//Add BOM
dwWideCharByteLen=dwBytesToRead*2 + 2;
pBufferWideCharLEw=(WCHAR*)GlobalAlloc(GPTR, dwWideCharByteLen);
pBufferWideCharLEw[0]=0xFEFF;
++pBufferWideCharLEw;
//ANSI -> UTF-16LE
MultiByteToWideChar(CP_ACP, 0, pReadBuffer, dwBytesToRead, pBufferWideCharLEw, dwBytesToRead);
pBufferWrite=--pBufferWideCharLEw;
nBufferLen=dwWideCharByteLen;
}
else if (lstrcmpiW(chUnicodeType, L"UTF-16BE") == 0)
{
DWORD dwCount=0;
DWORD dwCount2=0;
char *pBufferWideCharBEa2=NULL;
//Add BOM
dwWideCharByteLen=dwBytesToRead*2 + 2;
pBufferWideCharLEw=(WCHAR*)GlobalAlloc(GPTR, dwWideCharByteLen);
pBufferWideCharLEw[0]=0xFEFF;
++pBufferWideCharLEw;
//ANSI -> UTF-16LE
MultiByteToWideChar(CP_ACP, 0, pReadBuffer, dwBytesToRead, pBufferWideCharLEw, dwBytesToRead);
--pBufferWideCharLEw;
//UTF-16LE -> UTF-16BE
pBufferWideCharBEa=(char*)GlobalAlloc(GPTR, dwWideCharByteLen + 2);
pBufferWideCharBEa2=(char*)pBufferWideCharLEw;
for (dwCount=0, dwCount2=1; dwCount != dwWideCharByteLen; dwCount+=2, dwCount2+=2)
{
pBufferWideCharBEa[dwCount]=pBufferWideCharBEa2[dwCount2];
pBufferWideCharBEa[dwCount2]=pBufferWideCharBEa2[dwCount];
}
nBufferLen=dwWideCharByteLen;
pBufferWrite=pBufferWideCharBEa;
}
else if ((lstrcmpiW(chUnicodeType, L"UTF-32LE") == 0) || (lstrcmpiW(chUnicodeType, L"UTF-32BE") == 0))
{
error[0]=L'5';
goto exit;
}
else
{
error[0]=L'2';
goto exit;
}
//WriteFile
hFileWrite=CreateFile(chOutFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (hFileWrite == INVALID_HANDLE_VALUE)
{
error[0]=L'7';
goto exit;
}
WriteFile(hFileWrite, pBufferWrite, nBufferLen, &dwNumberOfBytesWritten, NULL);
CloseHandle(hFileWrite);
//Exit
exit:
if (pReadBuffer)
GlobalFree(pReadBuffer);
if (pBufferMultiByte)
GlobalFree(pBufferMultiByte);
if (pBufferWideCharLEw)
GlobalFree(pBufferWideCharLEw);
if (pBufferWideCharBEa)
GlobalFree(pBufferWideCharBEa);
pushstringW(error);
}
void __declspec(dllexport) FileUnicode2UTF8(HWND hwndParent, int string_size,
char *variables, stack_t **stacktop,
extra_parameters *extra)
{
EXDLL_INIT();
{
TCHAR chInFile[1024];
TCHAR chOutFile[1024];
TCHAR chUnicodeType[1024];
TCHAR error[2]=L"0";
//Get parameters
popstringW(chInFile);
if (chInFile[0] == 0)
{
error[0]=L'1';
goto exit;
}
popstringW(chOutFile);
if (chOutFile[0] == 0)
{
error[0]=L'1';
goto exit;
}
popstringW(chUnicodeType);
if (chUnicodeType[0] == 0)
{
error[0]=L'1';
goto exit;
}
if ((lstrcmpiW(chUnicodeType, L"AUTO") != 0) &&
(lstrcmpiW(chUnicodeType, L"UTF-8") != 0) &&
(lstrcmpiW(chUnicodeType, L"UTF-16LE") != 0) &&
(lstrcmpiW(chUnicodeType, L"UTF-16BE") != 0))
{
error[0]=L'2';
goto exit;
}
__FileUnicode2UTF8 (chInFile, chOutFile, chUnicodeType);
return;
//Exit
exit:
pushstringW(error);
}
}
void __declspec(dllexport) __FileUnicode2UTF8(TCHAR *chInFile, TCHAR *chOutFile, TCHAR *chUnicodeType)
{
HANDLE hFileRead=0;
HANDLE hFileWrite=0;
TCHAR error[2]=L"0";
int nUnicodeType=0;
int nMultiByteLen=0;
int nBufferLen=0;
DWORD dwNumberOfBytesRead=0;
DWORD dwNumberOfBytesWritten=0;
DWORD dwBytesToRead=0;
void *pReadBuffer=NULL;
char *pBufferMultiByte=NULL;
WCHAR *pBufferWideCharLEw=NULL;
char *pBufferWideCharLEa=NULL;
//Get unicode file type
if (lstrcmpiW(chUnicodeType, L"AUTO") == 0)
{
nUnicodeType=__UnicodeType(chInFile);
if (nUnicodeType == -1)
return;
else if ((nUnicodeType == 44) || (nUnicodeType == 46))
{
popstringW(NULL);
error[0]=L'5';
goto exit;
}
else if (nUnicodeType == 0)
{
popstringW(NULL);
error[0]=L'4';
goto exit;
}
}
//ReadFile
hFileRead=CreateFile(chInFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hFileRead == INVALID_HANDLE_VALUE)
{
error[0]=L'6';
goto exit;
}
dwBytesToRead=GetFileSize(hFileRead, NULL);
pReadBuffer=GlobalAlloc(GPTR, dwBytesToRead+1);
ReadFile(hFileRead, pReadBuffer, dwBytesToRead, &dwNumberOfBytesRead, NULL);
CloseHandle(hFileRead);
//ConvertFile
if ((nUnicodeType == 13) || (lstrcmpiW(chUnicodeType, L"UTF-8") == 0))
{
DWORD dwCount;
nMultiByteLen= dwBytesToRead;
pBufferMultiByte=(char*)GlobalAlloc(GPTR, dwBytesToRead);
for (dwCount=0; dwCount < dwBytesToRead; dwCount++) {
pBufferMultiByte[dwCount] = ((char *)pReadBuffer)[dwCount];
}
}
else if ((nUnicodeType == 22) || (lstrcmpiW(chUnicodeType, L"UTF-16LE") == 0))
{
nBufferLen=dwBytesToRead;
nMultiByteLen = 0;
while (nMultiByteLen == 0) {
pBufferMultiByte=(char*)GlobalAlloc(GPTR, nBufferLen);
//UTF-16LE -> UTF-8
nMultiByteLen=WideCharToMultiByte(CP_UTF8, 0, pReadBuffer, dwBytesToRead/2, pBufferMultiByte, dwBytesToRead, NULL, NULL);
if ((nMultiByteLen == 0) && (GetLastError () == ERROR_INSUFFICIENT_BUFFER) ) {
if (pBufferMultiByte) {
GlobalFree(pBufferMultiByte);
pBufferMultiByte = NULL;
}
nBufferLen = 2 * nBufferLen;
}
}
}
else if ((nUnicodeType == 23) || (lstrcmpiW(chUnicodeType, L"UTF-16BE") == 0))
{
DWORD dwCount=0;
DWORD dwCount2=0;
char *pBufferWideCharLEa2=NULL;
//UTF-16BE -> UTF-16LE
pBufferWideCharLEa=(char*)GlobalAlloc(GPTR, dwBytesToRead);
pBufferWideCharLEa2=(char*)pReadBuffer;
for (dwCount=0, dwCount2=1; dwCount != dwBytesToRead; dwCount+=2, dwCount2+=2)
{
pBufferWideCharLEa[dwCount]=pBufferWideCharLEa2[dwCount2];
pBufferWideCharLEa[dwCount2]=pBufferWideCharLEa2[dwCount];
}
pBufferWideCharLEw=(WCHAR*)pBufferWideCharLEa;
nBufferLen=dwBytesToRead;
pBufferMultiByte=(char*)GlobalAlloc(GPTR, nBufferLen);
//UTF-16LE -> UTF-8
nMultiByteLen=WideCharToMultiByte(CP_UTF8, 0, pBufferWideCharLEw, dwBytesToRead/2, pBufferMultiByte, dwBytesToRead, 0, 0);
}
//WriteFile
hFileWrite=CreateFile(chOutFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (hFileWrite == INVALID_HANDLE_VALUE)
{
error[0]=L'7';
goto exit;
}
WriteFile(hFileWrite, pBufferMultiByte, nMultiByteLen, &dwNumberOfBytesWritten, NULL);
CloseHandle(hFileWrite);
//Exit
exit:
if (pReadBuffer)
GlobalFree(pReadBuffer);
if (pBufferMultiByte)
GlobalFree(pBufferMultiByte);
if (pBufferWideCharLEw)
GlobalFree(pBufferWideCharLEw);
pushstringW(error);
}
int __UnicodeType(TCHAR *chInFile)
{
HANDLE hFileRead=0;
BYTE bom[32];
DWORD dwNumberOfBytesRead=0;
hFileRead=CreateFile(chInFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hFileRead == INVALID_HANDLE_VALUE)
{
pushstringW(L"6");
return -1;
}
ReadFile(hFileRead, &bom, 4, &dwNumberOfBytesRead, NULL);
CloseHandle(hFileRead);
if (bom[0] == 0xEF && bom[1] == 0xBB && bom[2] == 0xBF)
{
pushstringW(L"UTF-8"); // Variable Width (Web)
return 13;
}
else if (bom[0] == 0xFF && bom[1] == 0xFE && bom[2] == 0 && bom[3] == 0)
{
pushstringW(L"UTF-32LE|UCS-4LE"); // 32-bit Little Endian
return 44;
}
else if (bom[0] == 0 && bom[1] == 0 && bom[2] == 0xFE&& bom[3] == 0xFF)
{
pushstringW(L"UTF-32BE|UCS-4BE"); // 32-bit Big Endian
return 46;
}
else if (bom[0] == 0xFF && bom[1] == 0xFE)
{
pushstringW(L"UTF-16LE|UCS-2LE"); // Little Endian (Default for Windows)
return 22;
}
else if (bom[0] == 0xFE && bom[1] == 0xFF)
{
pushstringW(L"UTF-16BE|UCS-2BE"); // Big Endian (Default for Linux)
return 23;
}
else
{
pushstringW(L"NONE"); // None Unicode
return 0;
}
}
BOOL __stdcall DllMain(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
{
return TRUE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment