{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
module System.FileArchive.GZip (
Header(..), Section, GZipError(..),
Footer(..),
decompress,
hDecompress,
read_sections,
read_header,
read_section
)
where
import Control.Monad.Except (MonadError(..))
import Data.Bits ((.&.))
import Data.Bits.Utils (fromBytes)
import Data.Char (ord)
import Data.Compression.Inflate (inflate_string_remainder)
import Data.Hash.CRC32.GZip (update_crc)
import Data.Word (Word32)
import System.IO (Handle, hGetContents, hPutStr)
data GZipError = CRCError
| NotGZIPFile
| UnknownMethod
| UnknownError String
deriving (GZipError -> GZipError -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: GZipError -> GZipError -> Bool
$c/= :: GZipError -> GZipError -> Bool
== :: GZipError -> GZipError -> Bool
$c== :: GZipError -> GZipError -> Bool
Eq, Int -> GZipError -> ShowS
[GZipError] -> ShowS
GZipError -> [Char]
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
showList :: [GZipError] -> ShowS
$cshowList :: [GZipError] -> ShowS
show :: GZipError -> [Char]
$cshow :: GZipError -> [Char]
showsPrec :: Int -> GZipError -> ShowS
$cshowsPrec :: Int -> GZipError -> ShowS
Show)
magic :: String
magic :: [Char]
magic = [Char]
"\x1f\x8b"
fFHCRC, fFEXTRA, fFNAME, fFCOMMENT :: Int
fFHCRC :: Int
fFHCRC = Int
2
= Int
4
fFNAME :: Int
fFNAME = Int
8
= Int
16
data = {
Header -> Int
method :: Int,
Header -> Int
flags :: Int,
:: Maybe String,
Header -> Maybe [Char]
filename :: Maybe String,
:: Maybe String,
Header -> Word32
mtime :: Word32,
Header -> Int
xfl :: Int,
Header -> Int
os :: Int
} deriving (Header -> Header -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Header -> Header -> Bool
$c/= :: Header -> Header -> Bool
== :: Header -> Header -> Bool
$c== :: Header -> Header -> Bool
Eq, Int -> Header -> ShowS
[Header] -> ShowS
Header -> [Char]
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
showList :: [Header] -> ShowS
$cshowList :: [Header] -> ShowS
show :: Header -> [Char]
$cshow :: Header -> [Char]
showsPrec :: Int -> Header -> ShowS
$cshowsPrec :: Int -> Header -> ShowS
Show)
data = {
Footer -> Word32
size :: Word32,
Footer -> Word32
crc32 :: Word32,
Footer -> Bool
crc32valid :: Bool
}
type Section = (Header, String, Footer)
split1 :: String -> (Char, String)
split1 :: [Char] -> (Char, [Char])
split1 [Char]
s = (forall a. [a] -> a
head [Char]
s, forall a. [a] -> [a]
tail [Char]
s)
hDecompress :: Handle
-> Handle
-> IO (Maybe GZipError)
hDecompress :: Handle -> Handle -> IO (Maybe GZipError)
hDecompress Handle
infd Handle
outfd =
do [Char]
inc <- Handle -> IO [Char]
hGetContents Handle
infd
let ([Char]
outstr, Maybe GZipError
err) = [Char] -> ([Char], Maybe GZipError)
decompress [Char]
inc
Handle -> [Char] -> IO ()
hPutStr Handle
outfd [Char]
outstr
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe GZipError
err
decompress :: String -> (String, Maybe GZipError)
decompress :: [Char] -> ([Char], Maybe GZipError)
decompress [Char]
s =
let procs :: [Section] -> (String, Bool)
procs :: [Section] -> ([Char], Bool)
procs [] = ([], Bool
True)
procs ((Header
_, [Char]
content, Footer
foot):[Section]
xs) =
let ([Char]
nexth, Bool
nextb) = [Section] -> ([Char], Bool)
procs [Section]
xs in
([Char]
content forall a. [a] -> [a] -> [a]
++ [Char]
nexth, (Footer -> Bool
crc32valid Footer
foot) Bool -> Bool -> Bool
&& Bool
nextb)
in case [Char] -> Either GZipError [Section]
read_sections [Char]
s of
Left GZipError
x -> ([Char]
"", forall a. a -> Maybe a
Just GZipError
x)
Right [Section]
x -> let ([Char]
decomp, Bool
iscrcok) = [Section] -> ([Char], Bool)
procs [Section]
x
in ([Char]
decomp, if Bool
iscrcok then forall a. Maybe a
Nothing else forall a. a -> Maybe a
Just GZipError
CRCError)
read_sections :: String -> Either GZipError [Section]
read_sections :: [Char] -> Either GZipError [Section]
read_sections [] = forall a b. b -> Either a b
Right []
read_sections [Char]
s =
do (Section, [Char])
x <- [Char] -> Either GZipError (Section, [Char])
read_section [Char]
s
case (Section, [Char])
x of
(Section
sect, [Char]
remain) ->
do [Section]
next <- [Char] -> Either GZipError [Section]
read_sections [Char]
remain
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Section
sect forall a. a -> [a] -> [a]
: [Section]
next
parseword :: String -> Word32
parseword :: [Char] -> Word32
parseword [Char]
s = forall a. (Bits a, Num a) => [a] -> a
fromBytes forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Int
ord) forall a b. (a -> b) -> a -> b
$ forall a. [a] -> [a]
reverse [Char]
s
read_section :: String -> Either GZipError (Section, String)
read_section :: [Char] -> Either GZipError (Section, [Char])
read_section [Char]
s =
do (Header, [Char])
x <- [Char] -> Either GZipError (Header, [Char])
read_header [Char]
s
let headerrem :: [Char]
headerrem = forall a b. (a, b) -> b
snd (Header, [Char])
x
let ([Char]
decompressed, Word32
crc, [Char]
remainder) = [Char] -> ([Char], Word32, [Char])
read_data [Char]
headerrem
let ([Char]
crc32str, [Char]
rm) = forall a. Int -> [a] -> ([a], [a])
splitAt Int
4 [Char]
remainder
let ([Char]
sizestr, [Char]
rem2) = forall a. Int -> [a] -> ([a], [a])
splitAt Int
4 [Char]
rm
let filecrc32 :: Word32
filecrc32 = [Char] -> Word32
parseword [Char]
crc32str
let filesize :: Word32
filesize = [Char] -> Word32
parseword [Char]
sizestr
forall (m :: * -> *) a. Monad m => a -> m a
return ((forall a b. (a, b) -> a
fst (Header, [Char])
x, [Char]
decompressed,
Footer {size :: Word32
size = Word32
filesize, crc32 :: Word32
crc32 = Word32
filecrc32,
crc32valid :: Bool
crc32valid = Word32
filecrc32 forall a. Eq a => a -> a -> Bool
== Word32
crc})
,[Char]
rem2)
read_data :: String -> (String, Word32, String)
read_data :: [Char] -> ([Char], Word32, [Char])
read_data [Char]
x =
let ([Char]
decompressed1, [Char]
remainder) = [Char] -> ([Char], [Char])
inflate_string_remainder [Char]
x
([Char]
decompressed, Word32
crc32) = [Char] -> Word32 -> ([Char], Word32)
read_data_internal [Char]
decompressed1 Word32
0
in
([Char]
decompressed, Word32
crc32, [Char]
remainder)
where
read_data_internal :: [Char] -> Word32 -> ([Char], Word32)
read_data_internal [] Word32
ck = ([], Word32
ck)
read_data_internal (Char
y:[Char]
ys) Word32
ck =
let newcrc :: Word32
newcrc = Word32 -> Char -> Word32
update_crc Word32
ck Char
y
n :: ([Char], Word32)
n = Word32
newcrc seq :: forall a b. a -> b -> b
`seq` [Char] -> Word32 -> ([Char], Word32)
read_data_internal [Char]
ys Word32
newcrc
in
(Char
y forall a. a -> [a] -> [a]
: forall a b. (a, b) -> a
fst ([Char], Word32)
n, forall a b. (a, b) -> b
snd ([Char], Word32)
n)
read_header :: String -> Either GZipError (Header, String)
[Char]
s =
let ok :: Either a [Char]
ok = forall a b. b -> Either a b
Right [Char]
"ok" in
do let ([Char]
mag, [Char]
rem1) = forall a. Int -> [a] -> ([a], [a])
splitAt Int
2 [Char]
s
[Char]
_ <- if [Char]
mag forall a. Eq a => a -> a -> Bool
/= [Char]
magic
then forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError GZipError
NotGZIPFile
else forall {a}. Either a [Char]
ok
let (Char
method, [Char]
rem2) = [Char] -> (Char, [Char])
split1 [Char]
rem1
[Char]
_ <- if (Char -> Int
ord(Char
method) forall a. Eq a => a -> a -> Bool
/= Int
8)
then forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError GZipError
UnknownMethod
else forall {a}. Either a [Char]
ok
let (Char
flag_S, [Char]
rem3) = [Char] -> (Char, [Char])
split1 [Char]
rem2
let flag :: Int
flag = Char -> Int
ord Char
flag_S
let ([Char]
mtimea, [Char]
rem3a) = forall a. Int -> [a] -> ([a], [a])
splitAt Int
4 [Char]
rem3
let mtime :: Word32
mtime = [Char] -> Word32
parseword [Char]
mtimea
let (Char
xfla, [Char]
rem3b) = [Char] -> (Char, [Char])
split1 [Char]
rem3a
let xfl :: Int
xfl = Char -> Int
ord Char
xfla
let (Char
osa, [Char]
_) = [Char] -> (Char, [Char])
split1 [Char]
rem3b
let os :: Int
os = Char -> Int
ord Char
osa
let rem4 :: [Char]
rem4 = forall a. Int -> [a] -> [a]
drop Int
6 [Char]
rem3
let (Maybe [Char]
extra, [Char]
rem5) =
if (Int
flag forall a. Bits a => a -> a -> a
.&. Int
fFEXTRA forall a. Eq a => a -> a -> Bool
/= Int
0)
then let (Char
xlen_S, [Char]
_) = [Char] -> (Char, [Char])
split1 [Char]
rem4
(Char
xlen2_S, [Char]
rem4b) = [Char] -> (Char, [Char])
split1 [Char]
rem4
xlen :: Int
xlen = (Char -> Int
ord Char
xlen_S) forall a. Num a => a -> a -> a
+ Int
256 forall a. Num a => a -> a -> a
* (Char -> Int
ord Char
xlen2_S)
([Char]
ex, [Char]
rrem) = forall a. Int -> [a] -> ([a], [a])
splitAt Int
xlen [Char]
rem4b
in (forall a. a -> Maybe a
Just [Char]
ex, [Char]
rrem)
else (forall a. Maybe a
Nothing, [Char]
rem4)
let (Maybe [Char]
filename, [Char]
rem6) =
if (Int
flag forall a. Bits a => a -> a -> a
.&. Int
fFNAME forall a. Eq a => a -> a -> Bool
/= Int
0)
then let fn :: [Char]
fn = forall a. (a -> Bool) -> [a] -> [a]
takeWhile (forall a. Eq a => a -> a -> Bool
/= Char
'\x00') [Char]
rem5
in (forall a. a -> Maybe a
Just [Char]
fn, forall a. Int -> [a] -> [a]
drop ((forall (t :: * -> *) a. Foldable t => t a -> Int
length [Char]
fn) forall a. Num a => a -> a -> a
+ Int
1) [Char]
rem5)
else (forall a. Maybe a
Nothing, [Char]
rem5)
let (Maybe [Char]
comment, [Char]
rem7) =
if (Int
flag forall a. Bits a => a -> a -> a
.&. Int
fFCOMMENT forall a. Eq a => a -> a -> Bool
/= Int
0)
then let cm :: [Char]
cm = forall a. (a -> Bool) -> [a] -> [a]
takeWhile (forall a. Eq a => a -> a -> Bool
/= Char
'\x00') [Char]
rem6
in (forall a. a -> Maybe a
Just [Char]
cm, forall a. Int -> [a] -> [a]
drop ((forall (t :: * -> *) a. Foldable t => t a -> Int
length [Char]
cm) forall a. Num a => a -> a -> a
+ Int
1) [Char]
rem6)
else (forall a. Maybe a
Nothing, [Char]
rem6)
[Char]
rem8 <- if (Int
flag forall a. Bits a => a -> a -> a
.&. Int
fFHCRC forall a. Eq a => a -> a -> Bool
/= Int
0)
then forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. Int -> [a] -> [a]
drop Int
2 [Char]
rem7
else forall (m :: * -> *) a. Monad m => a -> m a
return [Char]
rem7
forall (m :: * -> *) a. Monad m => a -> m a
return (Header {method :: Int
method = Char -> Int
ord Char
method,
flags :: Int
flags = Int
flag,
extra :: Maybe [Char]
extra = Maybe [Char]
extra,
filename :: Maybe [Char]
filename = Maybe [Char]
filename,
comment :: Maybe [Char]
comment = Maybe [Char]
comment,
mtime :: Word32
mtime = Word32
mtime,
xfl :: Int
xfl = Int
xfl,
os :: Int
os = Int
os}, [Char]
rem8)