-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtables.hs
34 lines (29 loc) · 915 Bytes
/
tables.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as LB
import qualified Data.ByteString.Char8 as CB
import Data.Binary.Get
import System.Environment
import Control.Monad
tables = do
skip 4
n <- getWord16be
skip 6
replicateM (fromIntegral $ n) tableEntry
tableEntry = do
_tag <- getBytes 4
let tag = CB.unpack _tag
skip 4 -- checksum
offset <- getWord32be
len <- getWord32be
return (tag, offset, len)
tableData (tag, offset, len) = do
skip $ (fromIntegral $ offset)
cont <- getByteString $ fromIntegral len
return (tag, cont)
main = do
(fileName:name:_) <- getArgs
f <- LB.readFile $ fileName
let entries = runGet tables f
if (name == "tables")
then putStrLn $ show $ map (\(t, _, _) -> t) entries
else B.putStr $ head $ map (\e -> snd $ runGet (tableData e) f) $ filter (\(t, _, _) -> t == name) entries