From 1ad8755a80242421ded72714dc1eeaf9ab4f8ca3 Mon Sep 17 00:00:00 2001 From: Henning Guenther Date: Wed, 12 Aug 2009 19:24:55 -0700 Subject: [PATCH] Fix chr calling on big numbers in UTF8 Ignore-this: 3a7650c4b75691b79b082db6bddd5b8b darcs-hash:20090813022455-a4fee-1989c74eb7f7a9daa87f902da138c45185ea520e --- Data/Encoding/UTF8.hs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/Data/Encoding/UTF8.hs b/Data/Encoding/UTF8.hs index 9cb3f27..facc454 100644 --- a/Data/Encoding/UTF8.hs +++ b/Data/Encoding/UTF8.hs @@ -69,11 +69,13 @@ instance Encoding UTF8 where v2 = w2 .&. 0x3F v3 = w3 .&. 0x3F v4 = w4 .&. 0x3F - return $ chr $ - ((fromIntegral v1) `shiftL` 18) + v = ((fromIntegral v1) `shiftL` 18) .|. ((fromIntegral v2) `shiftL` 12) .|. ((fromIntegral v3) `shiftL` 6) .|. (fromIntegral v4) + if v <= 0x10FFFF + then return $ chr v + else throwException (IllegalRepresentation [w1,w2,w3,w4]) | otherwise -> throwException (IllegalCharacter w1) decodeChar UTF8Strict = do w1 <- fetchWord8 @@ -109,13 +111,15 @@ instance Encoding UTF8 where v2 = w2 .&. 0x3F v3 = w3 .&. 0x3F v4 = w4 .&. 0x3F - if v1 == 0 && v2 < 0x10 - then throwException (IllegalRepresentation [w1,w2,w3,w4]) - else return $ chr $ - ((fromIntegral v1) `shiftL` 18) + v = ((fromIntegral v1) `shiftL` 18) .|. ((fromIntegral v2) `shiftL` 12) .|. ((fromIntegral v3) `shiftL` 6) .|. (fromIntegral v4) + if v1 == 0 && v2 < 0x10 + then throwException (IllegalRepresentation [w1,w2,w3,w4]) + else (if v <= 0x10FFFF + then return $ chr v + else throwException (IllegalRepresentation [w1,w2,w3,w4])) | otherwise -> throwException (IllegalCharacter w1) where invalidExtend wrd = wrd .&. 0xC0 /= 0x80