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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
{-# LANGUAGE CPP #-}
{-# LANGUAGE NoImplicitPrelude #-}
{- |
Module : System.Win32.Encoding
Copyright : 2012 shelarcy
License : BSD-style
Maintainer : shelarcy@gmail.com
Stability : Provisional
Portability : Non-portable (Win32 API)
Enocode/Decode mutibyte charactor using Win32 API.
-}
module GHC.IO.Windows.Encoding
( encodeMultiByte
, encodeMultiByteIO
, encodeMultiByteRawIO
, decodeMultiByte
, decodeMultiByteIO
, wideCharToMultiByte
, multiByteToWideChar
, withGhcInternalToUTF16
, withUTF16ToGhcInternal
) where
import Data.Word (Word8, Word16)
import Foreign.C.Types (CInt(..))
import Foreign.C.String (peekCAStringLen, peekCWStringLen,
withCWStringLen, withCAStringLen, )
import Foreign.Ptr (nullPtr, Ptr ())
import Foreign.Marshal.Array (allocaArray)
import Foreign.Marshal.Unsafe (unsafeLocalState)
import GHC.Windows
import GHC.IO.Encoding.CodePage (CodePage, getCurrentCodePage)
import GHC.IO
import GHC.Base
import GHC.Real
#include "windows_cconv.h"
-- | The "System.IO" output functions (e.g. `putStr`) don't
-- automatically convert to multibyte string on Windows, so this
-- function is provided to make the conversion from a Unicode string
-- in the given code page to a proper multibyte string. To get the
-- code page for the console, use `getCurrentCodePage`.
--
encodeMultiByte :: CodePage -> String -> String
encodeMultiByte cp = unsafeLocalState . encodeMultiByteIO cp
{-# INLINE encodeMultiByteIO' #-}
-- | String must not be zero length.
encodeMultiByteIO' :: CodePage -> String -> ((LPCSTR, CInt) -> IO a) -> IO a
encodeMultiByteIO' cp wstr transformer =
withCWStringLen wstr $ \(cwstr,len) -> do
mbchars' <- failIfZero "WideCharToMultiByte" $ wideCharToMultiByte
cp
0
cwstr
(fromIntegral len)
nullPtr 0
nullPtr nullPtr
-- mbchar' is the length of buffer required
allocaArray (fromIntegral mbchars') $ \mbstr -> do
mbchars <- failIfZero "WideCharToMultiByte" $ wideCharToMultiByte
cp
0
cwstr
(fromIntegral len)
mbstr mbchars'
nullPtr nullPtr
transformer (mbstr,fromIntegral mbchars)
-- converts [Char] to UTF-16
encodeMultiByteIO :: CodePage -> String -> IO String
encodeMultiByteIO _ "" = return ""
encodeMultiByteIO cp s = encodeMultiByteIO' cp s toString
where toString (st,l) = peekCAStringLen (st,fromIntegral l)
-- converts [Char] to UTF-16
encodeMultiByteRawIO :: CodePage -> String -> IO (LPCSTR, CInt)
encodeMultiByteRawIO _ "" = return (nullPtr, 0)
encodeMultiByteRawIO cp s = encodeMultiByteIO' cp s toSizedCString
where toSizedCString (st,l) = return (st, fromIntegral l)
foreign import WINDOWS_CCONV "WideCharToMultiByte"
wideCharToMultiByte
:: CodePage
-> DWORD -- dwFlags,
-> LPCWSTR -- lpWideCharStr
-> CInt -- cchWideChar
-> LPSTR -- lpMultiByteStr
-> CInt -- cbMultiByte
-> LPCSTR -- lpMultiByteStr
-> LPBOOL -- lpbFlags
-> IO CInt
-- | The `System.IO` input functions (e.g. `getLine`) don't
-- automatically convert to Unicode, so this function is provided to
-- make the conversion from a multibyte string in the given code page
-- to a proper Unicode string. To get the code page for the console,
-- use `getConsoleCP`.
stringToUnicode :: CodePage -> String -> IO String
stringToUnicode _cp "" = return ""
-- MultiByteToWideChar doesn't handle empty strings (#1929)
stringToUnicode cp mbstr =
withCAStringLen mbstr $ \(cstr,len) -> do
wchars <- failIfZero "MultiByteToWideChar" $ multiByteToWideChar
cp
0
cstr
(fromIntegral len)
nullPtr 0
-- wchars is the length of buffer required
allocaArray (fromIntegral wchars) $ \cwstr -> do
wchars' <- failIfZero "MultiByteToWideChar" $ multiByteToWideChar
cp
0
cstr
(fromIntegral len)
cwstr wchars
peekCWStringLen (cwstr,fromIntegral wchars') -- converts UTF-16 to [Char]
foreign import WINDOWS_CCONV unsafe "MultiByteToWideChar"
multiByteToWideChar
:: CodePage
-> DWORD -- dwFlags,
-> LPCSTR -- lpMultiByteStr
-> CInt -- cbMultiByte
-> LPWSTR -- lpWideCharStr
-> CInt -- cchWideChar
-> IO CInt
decodeMultiByte :: CodePage -> String -> String
decodeMultiByte cp = unsafeLocalState . decodeMultiByteIO cp
-- | Because of `stringToUnicode` is unclear name, we use `decodeMultiByteIO`
-- for alias of `stringToUnicode`.
decodeMultiByteIO :: CodePage -> String -> IO String
decodeMultiByteIO = stringToUnicode
{-# INLINE decodeMultiByteIO #-}
foreign import WINDOWS_CCONV unsafe "MultiByteToWideChar"
multiByteToWideChar'
:: CodePage
-> DWORD -- dwFlags,
-> Ptr Word8 -- lpMultiByteStr
-> CInt -- cbMultiByte
-> Ptr Word16 -- lpWideCharStr
-> CInt -- cchWideChar
-> IO CInt
-- TODO: GHC is internally UTF-32 which means we have re-encode for
-- Windows which is annoying. Switch to UTF-16 on IoNative
-- being default.
withGhcInternalToUTF16 :: Ptr Word8 -> Int -> ((Ptr Word16, CInt) -> IO a)
-> IO a
withGhcInternalToUTF16 ptr len fn
= do cp <- getCurrentCodePage
wchars <- failIfZero "withGhcInternalToUTF16" $
multiByteToWideChar' cp 0 ptr (fromIntegral len) nullPtr 0
-- wchars is the length of buffer required
allocaArray (fromIntegral wchars) $ \cwstr -> do
wchars' <- failIfZero "withGhcInternalToUTF16" $
multiByteToWideChar' cp 0 ptr (fromIntegral len) cwstr wchars
fn (cwstr, wchars')
foreign import WINDOWS_CCONV "WideCharToMultiByte"
wideCharToMultiByte'
:: CodePage
-> DWORD -- dwFlags,
-> Ptr Word16 -- lpWideCharStr
-> CInt -- cchWideChar
-> Ptr Word8 -- lpMultiByteStr
-> CInt -- cbMultiByte
-> LPCSTR -- lpMultiByteStr
-> LPBOOL -- lpbFlags
-> IO CInt
-- TODO: GHC is internally UTF-32 which means we have re-encode for
-- Windows which is annoying. Switch to UTF-16 on IoNative
-- being default.
-- | Decode a UTF16 buffer into the given buffer in the current code page.
-- The source UTF16 buffer is filled by the function given as argument.
withUTF16ToGhcInternal :: Ptr Word8 -- Buffer to store the encoded string in.
-> Int -- Length of the buffer
-- Function to fill source buffer.
-> ( CInt -- Size of available buffer in bytes
-> Ptr Word16 -- Temporary source buffer.
-> IO CInt -- Actual length of buffer content.
)
-> IO Int -- Returns number of bytes stored in buffer.
withUTF16ToGhcInternal ptr len fn
= do cp <- getCurrentCodePage
-- Annoyingly the IO system is very UTF-32 oriented and asks for bytes
-- as buffer reads. Problem is we don't know how many bytes we'll end up
-- having as UTF-32 MultiByte encoded UTF-16. So be conservative. We assume
-- that a single byte may expand to atmost 1 Word16. So assume that each
-- byte does and divide the requested number of bytes by two since each
-- Word16 encoded wchar may expand to only two Word8 sequences.
let reqBytes = fromIntegral (len `div` 2)
allocaArray reqBytes $ \w_ptr -> do
w_len <- fn (fromIntegral reqBytes) w_ptr
if w_len == 0
then return 0 else do
-- Get required length of encoding
mbchars' <- failIfZero "withUTF16ToGhcInternal" $
wideCharToMultiByte' cp 0 w_ptr
(fromIntegral w_len) nullPtr
0 nullPtr nullPtr
assert (mbchars' <= (fromIntegral len)) $ do
-- mbchar' is the length of buffer required
mbchars <- failIfZero "withUTF16ToGhcInternal" $
wideCharToMultiByte' cp 0 w_ptr
(fromIntegral w_len) ptr
mbchars' nullPtr nullPtr
return $ fromIntegral mbchars
|