File size: 3,603 Bytes
7e9dc27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
------------------------------------------------------------------
--
--  Author: Alexey Melnichuk <alexeymelnichuck@gmail.com>
--
--  Copyright (C) 2013-2016 Alexey Melnichuk <alexeymelnichuck@gmail.com>
--
--  Licensed according to the included 'LICENCE' document
--
--  This file is part of lua-path library.
--
------------------------------------------------------------------

local alien = require "alien"
local kernel32 = assert(alien.load("kernel32.dll"))
local MultiByteToWideChar_ = assert(kernel32.MultiByteToWideChar)
local WideCharToMultiByte_ = assert(kernel32.WideCharToMultiByte)
local GetLastError         = assert(kernel32.GetLastError)

local DWORD = "uint"
local WCHAR_SIZE = 2

-- int __stdcall MultiByteToWideChar(UINT cp, DWORD flag, const char* src, int srclen, wchar_t* dst, int dstlen);
MultiByteToWideChar_:types{abi="stdcall", ret = "int",
  "uint",   -- cp
  DWORD,    -- flag
  "string", -- src (const char*)
  "int",    -- srclen
  "string", -- dst (wchar_t*)
  "int"     -- dstlen
}

--int __stdcall WideCharToMultiByte(UINT cp, DWORD flag, const wchar_t* src, int srclen, char* dst, int dstlen, const char* defchar, int* used);
WideCharToMultiByte_:types{abi="stdcall", ret = "int", 
  "int",     -- cp
  DWORD,     -- flag
  "string",  -- src (const wchar_t*)
  "int",     -- srclen
  "string",  -- dst (char*)
  "int",     -- dstlen
  "pointer", -- defchar (char*)
  "pointer"  -- used(int*)
}

GetLastError:types{ret = DWORD, abi='stdcall'}

local function strnlen(data, n)
  if type(data) == 'string' then
    return #data
  end
  n = n or #data
  for i = 1, n do
    if data[i] == 0 then
      return i
    end
  end
  return n
end

local function wcsnlen(data, n)
  if type(data) == 'string' then
    return  math.ceil(#data/2)
  end
  n = n or #data
  for i = 1, (2 * n), 2 do
    if (data[i] == 0) and (data[i+1] == 0) then
      return math.floor( i / 2 )
    end
  end
  return n
end

local function MultiByteToWideChar(src, cp)
  local flag   = true
  local buflen = strnlen(src)
  local dst    = alien.buffer( WCHAR_SIZE * (buflen + 1) ) -- eos
  local ret = MultiByteToWideChar_(cp, 0, src, #src, dst, buflen)
  if ret < 0 then return nil, GetLastError() end
  if ret <= buflen then 
    dst[ret * WCHAR_SIZE    ] = 0
    dst[ret * WCHAR_SIZE + 1] = 0
    return dst, ret
  end
  dst    = alien.buffer(WCHAR_SIZE * 1)
  dst[0] = 0
  dst[1] = 0
  return dst,0
end

local function WideCharToMultiByte(src, cp)
  local srclen = wcsnlen(src)
  local buflen = (srclen + 1)
  while true do
    local dst = alien.buffer(buflen + 1) -- eof
    local ret = WideCharToMultiByte_(cp, 0, src, srclen, dst, buflen, nil, nil)
    if ret <= 0 then 
      local err = GetLastError()
      if err == 122 then -- buffer too small
        buflen = math.ceil(1.5 * buflen)
      else
        return nil, err
      end
    else
      if ret <= buflen then 
        return dst, ret
      end
    end
  end
  local dst = alien.buffer(1)
  dst[0] = 0
  return dst, 0
end

local function LUA_M2W(src, ...)
  if not src or #src == 0 then return src end
  local dst, dstlen = MultiByteToWideChar(src, ...)
  if not dst then return nil, dstlen end
  return dst:tostring(dstlen * WCHAR_SIZE)
end

local function LUA_W2M(src, ...)
  if not src or #src == 0 then return src end
  local dst, dstlen = WideCharToMultiByte(src, ...)
  if not dst then return nil, dstlen end
  return dst:tostring(dstlen)
end

local _M = {
  MultiByteToWideChar = MultiByteToWideChar;
  WideCharToMultiByte = WideCharToMultiByte;
  mbstowcs            = LUA_M2W;
  wcstombs            = LUA_W2M;
}

return _M