97579aac1b 2024-01-23 1: local ffi = require("ffi")
9973312896 2024-01-23 2: local d_4 = {}
97579aac1b 2024-01-23 3: ffi.cdef([[
97579aac1b 2024-01-23 4: typedef unsigned int UINT;
97579aac1b 2024-01-23 5: typedef unsigned long DWORD;
97579aac1b 2024-01-23 6: typedef wchar_t WCHAR;
97579aac1b 2024-01-23 7: typedef WCHAR* LPWSTR;
97579aac1b 2024-01-23 8: typedef wchar_t* LPCWCH;
97579aac1b 2024-01-23 9: typedef char CHAR;
97579aac1b 2024-01-23 10: typedef CHAR* LPSTR;
97579aac1b 2024-01-23 11: typedef const char* LPCCH;
97579aac1b 2024-01-23 12: typedef int BOOL;
97579aac1b 2024-01-23 13: typedef BOOL* LPBOOL;
97579aac1b 2024-01-23 14: int MultiByteToWideChar(UINT CodePage, DWORD dwFlags, LPCCH lpMultiByteStr, int cbMultiByte, LPWSTR lpWideCharStr, int cchWideChar);
97579aac1b 2024-01-23 15: int WideCharToMultiByte(UINT CodePage, DWORD dwFlags, LPCWCH lpWideCharStr, int cchWideChar, LPSTR lpMultiByteStr, int cchMultiByte, LPCCH lpDefaultChar, LPBOOL pfUsedDefaultChar);
97579aac1b 2024-01-23 16: ]])
9973312896 2024-01-23 17: d_4.CP_ACP = 0
9973312896 2024-01-23 18: d_4.CP_UTF8 = 65001
9973312896 2024-01-23 19: function d_4.m2w(input, o1)
9973312896 2024-01-23 20: local wlen = ffi.C.MultiByteToWideChar(o1 or d_4.CP_ACP, 0, input, #input, nil, 0)
97579aac1b 2024-01-23 21: local wstr = ffi.new("wchar_t[?]", wlen + 1)
9973312896 2024-01-23 22: ffi.C.MultiByteToWideChar(o1 or d_4.CP_ACP, 0, input, #input, wstr, wlen)
97579aac1b 2024-01-23 23: return wstr, wlen
97579aac1b 2024-01-23 24: end
9973312896 2024-01-23 25: function d_4.w2m(wstr, wlen, o1)
9973312896 2024-01-23 26: local len = ffi.C.WideCharToMultiByte(o1 or d_4.CP_ACP, 0, wstr, wlen or -1, nil, 0, nil, nil)
97579aac1b 2024-01-23 27: local str = ffi.new("char[?]", len)
9973312896 2024-01-23 28: ffi.C.WideCharToMultiByte(o1 or d_4.CP_ACP, 0, wstr, wlen or -1, str, len, nil, nil)
97579aac1b 2024-01-23 29: return ffi.string(str, len)
97579aac1b 2024-01-23 30: end
9973312896 2024-01-23 31: function d_4.u8_to_a(input)
9973312896 2024-01-23 32: return d_4.w2m(d_4.m2w(input, d_4.CP_UTF8))
9973312896 2024-01-23 33: end
9973312896 2024-01-23 34: function d_4.a_to_u8(input)
9973312896 2024-01-23 35: local k1, k2 = d_4.m2w(input)
9973312896 2024-01-23 36: return d_4.w2m(k1, k2, d_4.CP_UTF8)
9973312896 2024-01-23 37: end
9973312896 2024-01-23 38: return d_4