3 , ForeignFunctionInterface
4 , NondecreasingIndentation
7 -----------------------------------------------------------------------------
9 -- Module : GHC.IO.Encoding.Iconv
10 -- Copyright : (c) The University of Glasgow, 2008-2009
11 -- License : see libraries/base/LICENSE
13 -- Maintainer : libraries@haskell.org
14 -- Stability : internal
15 -- Portability : non-portable
17 -- This module provides text encoding/decoding using iconv
19 -----------------------------------------------------------------------------
22 module GHC.IO.Encoding.Iconv (
23 #if !defined(mingw32_HOST_OS)
27 utf16, utf16le, utf16be,
28 utf32, utf32le, utf32be,
34 #include "HsBaseConfig.h"
36 #if !defined(mingw32_HOST_OS)
38 import Foreign hiding (unsafePerformIO)
43 import GHC.IO.Encoding.Types
47 import System.IO.Unsafe (unsafePerformIO)
48 import System.Posix.Internals
53 iconv_trace :: String -> IO ()
55 | c_DEBUG_DUMP = puts s
56 | otherwise = return ()
58 puts :: String -> IO ()
59 puts s = do _ <- withCStringLen (s ++ "\n") $ \(p, len) ->
60 c_write 1 (castPtr p) (fromIntegral len)
63 -- -----------------------------------------------------------------------------
64 -- iconv encoders/decoders
66 {-# NOINLINE latin1 #-}
67 latin1 :: TextEncoding
68 latin1 = unsafePerformIO (mkTextEncoding "Latin1")
72 utf8 = unsafePerformIO (mkTextEncoding "UTF8")
74 {-# NOINLINE utf16 #-}
76 utf16 = unsafePerformIO (mkTextEncoding "UTF16")
78 {-# NOINLINE utf16le #-}
79 utf16le :: TextEncoding
80 utf16le = unsafePerformIO (mkTextEncoding "UTF16LE")
82 {-# NOINLINE utf16be #-}
83 utf16be :: TextEncoding
84 utf16be = unsafePerformIO (mkTextEncoding "UTF16BE")
86 {-# NOINLINE utf32 #-}
88 utf32 = unsafePerformIO (mkTextEncoding "UTF32")
90 {-# NOINLINE utf32le #-}
91 utf32le :: TextEncoding
92 utf32le = unsafePerformIO (mkTextEncoding "UTF32LE")
94 {-# NOINLINE utf32be #-}
95 utf32be :: TextEncoding
96 utf32be = unsafePerformIO (mkTextEncoding "UTF32BE")
98 {-# NOINLINE localeEncoding #-}
99 localeEncoding :: TextEncoding
100 localeEncoding = unsafePerformIO $ do
101 -- Use locale_charset() or nl_langinfo(CODESET) to get the encoding
102 -- if we have either of them.
103 cstr <- c_localeEncoding
104 r <- peekCString cstr
107 -- We hope iconv_t is a storable type. It should be, since it has at least the
108 -- value -1, which is a possible return value from iconv_open.
109 type IConv = CLong -- ToDo: (#type iconv_t)
111 foreign import ccall unsafe "hs_iconv_open"
112 hs_iconv_open :: CString -> CString -> IO IConv
114 foreign import ccall unsafe "hs_iconv_close"
115 hs_iconv_close :: IConv -> IO CInt
117 foreign import ccall unsafe "hs_iconv"
118 hs_iconv :: IConv -> Ptr CString -> Ptr CSize -> Ptr CString -> Ptr CSize
121 foreign import ccall unsafe "localeEncoding"
122 c_localeEncoding :: IO CString
124 haskellChar :: String
125 #ifdef WORDS_BIGENDIAN
126 haskellChar | charSize == 2 = "UTF-16BE"
127 | otherwise = "UTF-32BE"
129 haskellChar | charSize == 2 = "UTF-16LE"
130 | otherwise = "UTF-32LE"
134 char_shift | charSize == 2 = 1
137 mkTextEncoding :: String -> IO TextEncoding
138 mkTextEncoding charset = do
139 return (TextEncoding {
140 textEncodingName = charset,
141 mkTextDecoder = newIConv charset haskellChar iconvDecode,
142 mkTextEncoder = newIConv haskellChar charset iconvEncode})
144 newIConv :: String -> String
145 -> (IConv -> Buffer a -> Buffer b -> IO (Buffer a, Buffer b))
146 -> IO (BufferCodec a b ())
147 newIConv from to fn =
148 withCString from $ \ from_str ->
149 withCString to $ \ to_str -> do
150 iconvt <- throwErrnoIfMinus1 "mkTextEncoding" $ hs_iconv_open to_str from_str
151 let iclose = throwErrnoIfMinus1_ "Iconv.close" $ hs_iconv_close iconvt
155 -- iconv doesn't supply a way to save/restore the state
156 getState = return (),
157 setState = const $ return ()
160 iconvDecode :: IConv -> Buffer Word8 -> Buffer CharBufElem
161 -> IO (Buffer Word8, Buffer CharBufElem)
162 iconvDecode iconv_t ibuf obuf = iconvRecode iconv_t ibuf 0 obuf char_shift
164 iconvEncode :: IConv -> Buffer CharBufElem -> Buffer Word8
165 -> IO (Buffer CharBufElem, Buffer Word8)
166 iconvEncode iconv_t ibuf obuf = iconvRecode iconv_t ibuf char_shift obuf 0
168 iconvRecode :: IConv -> Buffer a -> Int -> Buffer b -> Int
169 -> IO (Buffer a, Buffer b)
171 input@Buffer{ bufRaw=iraw, bufL=ir, bufR=iw, bufSize=_ } iscale
172 output@Buffer{ bufRaw=oraw, bufL=_, bufR=ow, bufSize=os } oscale
174 iconv_trace ("haskelChar=" ++ show haskellChar)
175 iconv_trace ("iconvRecode before, input=" ++ show (summaryBuffer input))
176 iconv_trace ("iconvRecode before, output=" ++ show (summaryBuffer output))
177 withRawBuffer iraw $ \ piraw -> do
178 withRawBuffer oraw $ \ poraw -> do
179 with (piraw `plusPtr` (ir `shiftL` iscale)) $ \ p_inbuf -> do
180 with (poraw `plusPtr` (ow `shiftL` oscale)) $ \ p_outbuf -> do
181 with (fromIntegral ((iw-ir) `shiftL` iscale)) $ \ p_inleft -> do
182 with (fromIntegral ((os-ow) `shiftL` oscale)) $ \ p_outleft -> do
183 res <- hs_iconv iconv_t p_inbuf p_inleft p_outbuf p_outleft
184 new_inleft <- peek p_inleft
185 new_outleft <- peek p_outleft
187 new_inleft' = fromIntegral new_inleft `shiftR` iscale
188 new_outleft' = fromIntegral new_outleft `shiftR` oscale
190 | new_inleft == 0 = input { bufL = 0, bufR = 0 }
191 | otherwise = input { bufL = iw - new_inleft' }
192 new_output = output{ bufR = os - new_outleft' }
193 iconv_trace ("iconv res=" ++ show res)
194 iconv_trace ("iconvRecode after, input=" ++ show (summaryBuffer new_input))
195 iconv_trace ("iconvRecode after, output=" ++ show (summaryBuffer new_output))
197 then do -- all input translated
198 return (new_input, new_output)
202 e | e == eINVAL || e == e2BIG
203 || e == eILSEQ && new_inleft' /= (iw-ir) -> do
204 iconv_trace ("iconv ignoring error: " ++ show (errnoToIOError "iconv" e Nothing Nothing))
205 -- Output overflow is harmless
207 -- Similarly, we ignore EILSEQ unless we converted no
208 -- characters. Sometimes iconv reports EILSEQ for a
209 -- character in the input even when there is no room
210 -- in the output; in this case we might be about to
211 -- change the encoding anyway, so the following bytes
212 -- could very well be in a different encoding.
213 -- This also helps with pinpointing EILSEQ errors: we
214 -- don't report it until the rest of the characters in
215 -- the buffer have been drained.
216 return (new_input, new_output)
219 iconv_trace ("iconv returned error: " ++ show (errnoToIOError "iconv" e Nothing Nothing))
220 throwErrno "iconvRecoder"
221 -- illegal sequence, or some other error
223 #endif /* !mingw32_HOST_OS */