Ticket #2 (new defect)
Opened 3 years ago
use dataenc instead of base64-string
| Reported by: | igloo | Owned by: | somebody |
|---|---|---|---|
| Priority: | major | Milestone: | |
| Component: | component1 | Version: | |
| Keywords: | Cc: |
Description
Magnus Therning <magnus@…> said:
I've attached a patch against version 0.4 of mime-string. It replaces the use of base64-string with the use of dataenc. Do with it what you like :-) If you like the idea, but not my code, then let me know how I can improve it.
diff -u --recursive mime-string-0.4/Codec/MIME/String/EncodedWord.hs mime-string-0.4_modified/Codec/MIME/String/EncodedWord.hs
--- mime-string-0.4/Codec/MIME/String/EncodedWord.hs 2008-12-05 18:42:47.000000000 +0000
+++ mime-string-0.4_modified/Codec/MIME/String/EncodedWord.hs 2009-03-05 00:12:41.000000000 +0000
@@ -1,8 +1,9 @@
module Codec.MIME.String.EncodedWord (base64_encode) where
-import Codec.Binary.Base64.String (encode)
+import Data.Char (ord)
+import Codec.Binary.Base64 (encode)
base64_encode :: String -> String -> String
-base64_encode charset xs = "=?" ++ charset ++ "?B?" ++ encode xs ++ "?="
-
+base64_encode charset xs = "=?" ++ charset ++ "?B?" ++ encode ws ++ "?="
+ where ws = map (fromIntegral . ord) xs
diff -u --recursive mime-string-0.4/Codec/MIME/String/Flatten.hs mime-string-0.4_modified/Codec/MIME/String/Flatten.hs
--- mime-string-0.4/Codec/MIME/String/Flatten.hs 2008-12-05 18:42:47.000000000 +0000
+++ mime-string-0.4_modified/Codec/MIME/String/Flatten.hs 2009-03-05 00:17:59.000000000 +0000
@@ -3,11 +3,12 @@
(flatten, Attachments, Attachment)
where
-import qualified Codec.Binary.Base64.String as Base64 (encode)
+import qualified Codec.Binary.Base64 as Base64 (encode)
import qualified Codec.MIME.String.QuotedPrintable as QP (encode)
import Codec.MIME.String.Headers
import Codec.MIME.String.Internal.Utils
import Codec.MIME.String.Types
+import Data.Char
{-
XXX For message IDs:
@@ -63,7 +64,8 @@
fn' -> fn')
++ "\"\n"
++ "\n"
- ++ Base64.encode a
+ ++ b64_encode a
+ b64_encode = Base64.encode . map (fromIntegral . ord)
msg = if single_part
then case maybeHtmlBody of
Nothing ->
diff -u --recursive mime-string-0.4/Codec/MIME/String/Headers.hs mime-string-0.4_modified/Codec/MIME/String/Headers.hs
--- mime-string-0.4/Codec/MIME/String/Headers.hs 2008-12-05 18:42:47.000000000 +0000
+++ mime-string-0.4_modified/Codec/MIME/String/Headers.hs 2009-03-05 00:15:22.000000000 +0000
@@ -30,7 +30,7 @@
(<$>), (<$), (<*>), (<*), (<|>), (<|), (<!>),
pEOI, pPred, pChar, pMany, pAtLeast, pMaybe, pOptDef, pString,
)
-import qualified Codec.Binary.Base64.String as Base64 (decode)
+import qualified Codec.Binary.Base64 as Base64 (decode)
import qualified Codec.Binary.EncodingQ.String as EncodingQ (decode)
import Codec.MIME.String.Internal.Utils
@@ -38,6 +38,7 @@
import qualified Data.ByteString.Lazy.Char8 as BS
import Data.Char
import Data.List
+import Data.Maybe
-----------------------
-- Utils
@@ -123,7 +124,9 @@
-- we'd want to fall back to showing it as a string anyway.
p_encoding :: Parser Char (String -> String)
p_encoding = EncodingQ.decode <$ (pChar 'Q' <|> pChar 'q')
- <|> Base64.decode <$ (pChar 'B' <|> pChar 'b')
+ <|> b64_decode <$ (pChar 'B' <|> pChar 'b')
+ where
+ b64_decode = map (chr . fromIntegral) . fromJust . Base64.decode
p_encoded_text :: Parser Char String
p_encoded_text = pMany (pPred (\c -> isAsciiPrint c && c /= '?' && c /= ' '))
diff -u --recursive mime-string-0.4/Codec/MIME/String/Parse.hs mime-string-0.4_modified/Codec/MIME/String/Parse.hs
--- mime-string-0.4/Codec/MIME/String/Parse.hs 2008-12-05 18:42:47.000000000 +0000
+++ mime-string-0.4_modified/Codec/MIME/String/Parse.hs 2009-03-05 00:19:20.000000000 +0000
@@ -1,7 +1,7 @@
module Codec.MIME.String.Parse where
-import qualified Codec.Binary.Base64.String as Base64 (decode)
+import qualified Codec.Binary.Base64 as Base64 (decode)
import Codec.MIME.String.ContentDisposition
(
ContentDisposition(ContentDisposition), get_content_disposition,
@@ -41,7 +41,8 @@
import Control.Monad.State (evalState, get, put)
import qualified Data.ByteString.Lazy.Char8 as BS
import Data.ByteString.Lazy.Char8 (ByteString)
-import Data.Maybe (fromMaybe)
+import Data.Maybe (fromMaybe, fromJust)
+import Data.Char (chr)
mkMessage :: MessageInfo -> ParseM MessageContent -> ParseM Message
mkMessage mi f_mc
@@ -173,9 +174,10 @@
get_content_transfer_encoding
cte@(ContentTransferEncoding content_transfer_encoding)
= fromMaybe (ContentTransferEncoding "7bit") m_ce
+ b64_decode = map (chr . fromIntegral) . fromJust . Base64.decode
m_decoded_body
= if content_transfer_encoding == "base64" then
- Just (Base64.decode body)
+ Just (b64_decode body)
else if content_transfer_encoding == "quoted-printable" then
Just (QuotedPrintable.decode $ my_lines body)
else if content_transfer_encoding `elem`
diff -u --recursive mime-string-0.4/mime-string.cabal mime-string-0.4_modified/mime-string.cabal
--- mime-string-0.4/mime-string.cabal 2008-12-05 18:42:47.000000000 +0000
+++ mime-string-0.4_modified/mime-string.cabal 2009-03-04 23:51:07.000000000 +0000
@@ -12,7 +12,7 @@
Implementation of the MIME RFCs 2045-2049.
Rather rough around the edges at the moment.
Category: Codec
-Build-Depends: base, mtl, network, iconv, base64-string, old-time, bytestring
+Build-Depends: base, mtl, network, iconv, dataenc, old-time, bytestring
Extensions: PatternGuards
Extra-source-files: "BSD3", "GPL-2"
Exposed-modules:
Note: See
TracTickets for help on using
tickets.
