@@ -1,4 +1,5 @@
11{-# LANGUAGE OverloadedStrings, ScopedTypeVariables #-}
2+{-# OPTIONS_GHC -fno-warn-type-defaults #-}
23{-
34 Copyright (C) 2011-2014 John MacFarlane <jgm@berkeley.edu>
45
@@ -38,8 +39,11 @@ import Control.Monad
3839import Data.Bits
3940import Data.Binary
4041import Data.Binary.Get
41−import Text.Pandoc.Shared (safeRead)
42+import Text.Pandoc.Shared (safeRead, hush)
4243import qualified Data.Map as M
44+import Text.Pandoc.Compat.Except
45+import Control.Monad.Trans
46+import Data.Maybe (fromMaybe)
4347
4448-- quick and dirty functions to get image sizes
4549-- algorithms borrowed from wwwis.pl
@@ -64,7 +68,7 @@ imageType img = case B.take 4 img of
6468 "%!PS"
6569 | (B.take 4 $ B.drop 1 $ B.dropWhile (/=' ') img) == "EPSF"
6670 -> return Eps
67− _ -> fail "Unknown image type"
71+ _ -> (hush . Left) "Unknown image type"
6872
6973imageSize :: ByteString -> Maybe ImageSize
7074imageSize img = do
@@ -114,7 +118,7 @@ pngSize img = do
114118 ([w1,w2,w3,w4,h1,h2,h3,h4] :: [Integer]) -> return
115119 ((shift w1 24) + (shift w2 16) + (shift w3 8) + w4,
116120 (shift h1 24) + (shift h2 16) + (shift h3 8) + h4)
117− _ -> fail "PNG parse error"
121+ _ -> (hush . Left) "PNG parse error"
118122 let (dpix, dpiy) = findpHYs rest''
119123 return $ ImageSize { pxX = x, pxY = y, dpiX = dpix, dpiY = dpiy }
120124
@@ -143,7 +147,7 @@ gifSize img = do
143147 dpiX = 72,
144148 dpiY = 72
145149 }
146− _ -> fail "GIF parse error"
150+ _ -> (hush . Left) "GIF parse error"
147151
148152jpegSize :: ByteString -> Maybe ImageSize
149153jpegSize img = do
@@ -174,36 +178,37 @@ findJfifSize bs = do
174178 Just (c,bs'') | c >= '\xc0' && c <= '\xc3' -> do
175179 case map fromIntegral $ unpack $ B.take 4 $ B.drop 3 bs'' of
176180 [h1,h2,w1,w2] -> return (shift w1 8 + w2, shift h1 8 + h2)
177− _ -> fail "JPEG parse error"
181+ _ -> (hush . Left) "JPEG parse error"
178182 Just (_,bs'') -> do
179183 case map fromIntegral $ unpack $ B.take 2 bs'' of
180184 [c1,c2] -> do
181185 let len = shift c1 8 + c2
182186 -- skip variables
183187 findJfifSize $ B.drop len bs''
184− _ -> fail "JPEG parse error"
185− Nothing -> fail "Did not find length record"
188+ _ -> (hush . Left) "JPEG parse error"
189+ Nothing -> (hush . Left) "Did not find length record"
186190
187191exifSize :: ByteString -> Maybe ImageSize
188−exifSize bs = runGet (Just <$> exifHeader bl) bl
192+exifSize bs = hush . runGet header $ bl
189193 where bl = BL.fromChunks [bs]
194+ header = runExceptT $ exifHeader bl
190195-- NOTE: It would be nicer to do
191196-- runGet ((Just <$> exifHeader) <|> return Nothing)
192197-- which would prevent pandoc from raising an error when an exif header can't
193198-- be parsed. But we only get an Alternative instance for Get in binary 0.6,
194199-- and binary 0.5 ships with ghc 7.6.
195200
196−exifHeader :: BL.ByteString -> Get ImageSize
201+exifHeader :: BL.ByteString -> ExceptT String Get ImageSize
197202exifHeader hdr = do
198− _app1DataSize <- getWord16be
199− exifHdr <- getWord32be
200− unless (exifHdr == 0x45786966) $ fail "Did not find exif header"
201− zeros <- getWord16be
202− unless (zeros == 0) $ fail "Expected zeros after exif header"
203+ _app1DataSize <- lift getWord16be
204+ exifHdr <- lift getWord32be
205+ unless (exifHdr == 0x45786966) $ throwError "Did not find exif header"
206+ zeros <- lift getWord16be
207+ unless (zeros == 0) $ throwError "Expected zeros after exif header"
203208 -- beginning of tiff header -- we read whole thing to use
204209 -- in getting data from offsets:
205210 let tiffHeader = BL.drop 8 hdr
206− byteAlign <- getWord16be
211+ byteAlign <- lift getWord16be
207212 let bigEndian = byteAlign == 0x4d4d
208213 let (getWord16, getWord32, getWord64) =
209214 if bigEndian
@@ -213,17 +218,17 @@ exifHeader hdr = do
213218 num <- getWord32
214219 den <- getWord32
215220 return $ fromIntegral num / fromIntegral den
216− tagmark <- getWord16
217− unless (tagmark == 0x002a) $ fail "Failed alignment sanity check"
218− ifdOffset <- getWord32
219− skip (fromIntegral ifdOffset - 8) -- skip to IDF
220− numentries <- getWord16
221− let ifdEntry = do
222− tag <- getWord16 >>= \t ->
223− maybe (return UnknownTagType) return
224− (M.lookup t tagTypeTable)
225− dataFormat <- getWord16
226− numComponents <- getWord32
221+ tagmark <- lift getWord16
222+ unless (tagmark == 0x002a) $ throwError "Failed alignment sanity check"
223+ ifdOffset <- lift getWord32
224+ lift $ skip (fromIntegral ifdOffset - 8) -- skip to IDF
225+ numentries <- lift getWord16
226+ let ifdEntry :: ExceptT String Get (TagType, DataFormat)
227+ ifdEntry = do
228+ tag <- fromMaybe UnknownTagType . flip M.lookup tagTypeTable
229+ <$> lift getWord16
230+ dataFormat <- lift getWord16
231+ numComponents <- lift getWord32
227232 (fmt, bytesPerComponent) <-
228233 case dataFormat of
229234 1 -> return (UnsignedByte . runGet getWord8, 1)
@@ -238,9 +243,10 @@ exifHeader hdr = do
238243 10 -> return (SignedRational . runGet getRational, 8)
239244 11 -> return (SingleFloat . runGet getWord32 {- TODO -}, 4)
240245 12 -> return (DoubleFloat . runGet getWord64 {- TODO -}, 8)
241− _ -> fail $ "Unknown data format " ++ show dataFormat
246+ _ -> throwError $ "Unknown data format " ++ show dataFormat
242247 let totalBytes = fromIntegral $ numComponents * bytesPerComponent
243− payload <- if totalBytes <= 4 -- data is right here
248+ payload <- lift $
249+ if totalBytes <= 4 -- data is right here
244250 then fmt <$>
245251 (getLazyByteString (fromIntegral totalBytes) <*
246252 skip (4 - totalBytes))
@@ -252,9 +258,9 @@ exifHeader hdr = do
252258 entries <- sequence $ replicate (fromIntegral numentries) ifdEntry
253259 subentries <- case lookup ExifOffset entries of
254260 Just (UnsignedLong offset) -> do
255− pos <- bytesRead
256− skip (fromIntegral offset - (fromIntegral pos - 8))
257− numsubentries <- getWord16
261+ pos <- lift bytesRead
262+ lift $ skip (fromIntegral offset - (fromIntegral pos - 8))
263+ numsubentries <- lift getWord16
258264 sequence $
259265 replicate (fromIntegral numsubentries) ifdEntry
260266 _ -> return []