Skip to content

Commit 1d88206

Browse files
committed
first commit
0 parents  commit 1d88206

18 files changed

+1226
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.stack-work/
2+
*~

CHANGELOG.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Changelog for `tlvmmt`
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to the
7+
[Haskell Package Versioning Policy](https://pvp.haskell.org/).
8+
9+
## Unreleased
10+
11+
## 0.1.0.0 - YYYY-MM-DD

LICENSE

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Copyright (C) 2023 by SuperFashi <[email protected]>
2+
3+
Permission to use, copy, modify, and/or distribute this software for
4+
any purpose with or without fee is hereby granted.
5+
6+
THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL
7+
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
8+
OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE
9+
FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
10+
DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
11+
AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
12+
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# tlvmmt

Setup.hs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import Distribution.Simple
2+
main = defaultMain

app/Main.hs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module Main (main) where
2+
3+
import Control.Monad.Trans.Class (lift)
4+
import Data.Binary (get)
5+
import Data.Binary.Get (Decoder (..), runGetIncremental)
6+
import qualified Data.ByteString as B (null)
7+
import qualified Data.ByteString.Lazy as L (readFile)
8+
import qualified Data.ByteString.Lazy.Internal as L (ByteString (Chunk, Empty), chunk)
9+
import Data.Int (Int64)
10+
import Lib (TLVPacket)
11+
import ListT (ListT, cons)
12+
import qualified ListT (head)
13+
import Text.Show.Pretty (pPrint)
14+
15+
parseTLVPackets :: MonadFail m => Decoder TLVPacket -> (Int64, L.ByteString) -> ListT m TLVPacket
16+
parseTLVPackets (Fail _ loc e) (br, _) = lift $ fail $ "error parse at " ++ show (br + loc) ++ ": " ++ e
17+
parseTLVPackets (Done r _ p) (_, L.Empty) | B.null r = return p
18+
parseTLVPackets (Done r o p) (br, input) = cons p $ parseTLVPackets (runGetIncremental get) (o + br, L.chunk r input)
19+
parseTLVPackets (Partial k) (br, L.Empty) = parseTLVPackets (k Nothing) (br, L.Empty)
20+
parseTLVPackets (Partial k) (br, L.Chunk bs input) = parseTLVPackets (k (Just bs)) (br, input)
21+
22+
main :: IO ()
23+
main = do
24+
file <- L.readFile "F:\\29999.mmts"
25+
let packets = parseTLVPackets (runGetIncremental get) (0, file)
26+
len <- ListT.head packets
27+
pPrint len
28+
29+
-- list <- toList $ parseTLVPackets (runGetIncremental get) file
30+
-- print list
31+
32+
-- mapM_ print $ runGet (parseLimitPacket 2) input

package.yaml

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
name: tlvmmt
2+
version: 0.1.0.0
3+
github: "superfashi/tlvmmt"
4+
license: 0BSD
5+
author: "SuperFashi"
6+
maintainer: "[email protected]"
7+
copyright: "2023 SuperFashi"
8+
9+
extra-source-files:
10+
- README.md
11+
- CHANGELOG.md
12+
13+
# Metadata used when publishing your package
14+
# synopsis: Short description of your package
15+
# category: Web
16+
17+
# To avoid duplicated efforts in documentation and dealing with the
18+
# complications of embedding Haddock markup inside cabal files, it is
19+
# common to point users to the README.md file.
20+
description: Please see the README on GitHub at <https://github.com/superfashi/tlvmmt#readme>
21+
22+
dependencies:
23+
- base >= 4.15 && < 5
24+
- binary
25+
- bytestring
26+
27+
ghc-options:
28+
- -Wall
29+
- -Wcompat
30+
- -Widentities
31+
- -Wincomplete-record-updates
32+
- -Wincomplete-uni-patterns
33+
- -Wmissing-export-lists
34+
- -Wmissing-home-modules
35+
- -Wpartial-fields
36+
- -Wredundant-constraints
37+
38+
library:
39+
source-dirs: src
40+
dependencies:
41+
- extra
42+
43+
executables:
44+
tlvmmt-exe:
45+
main: Main.hs
46+
source-dirs: app
47+
ghc-options:
48+
- -threaded
49+
- -rtsopts
50+
- -with-rtsopts=-N
51+
dependencies:
52+
- tlvmmt
53+
- list-t
54+
- transformers
55+
- pretty-show
56+
57+
tests:
58+
tlvmmt-test:
59+
main: Spec.hs
60+
source-dirs: test
61+
ghc-options:
62+
- -threaded
63+
- -rtsopts
64+
- -with-rtsopts=-N
65+
dependencies:
66+
- tlvmmt

src/Common.hs

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
module Common where
2+
3+
import Data.Binary (Binary (..), Get, Word8)
4+
import Data.Binary.Get
5+
( Decoder (..),
6+
isEmpty,
7+
pushChunks,
8+
pushEndOfInput,
9+
runGetIncremental,
10+
)
11+
import qualified Data.ByteString as B
12+
import Data.ByteString.Lazy (ByteString)
13+
14+
data FragmentationIndicator
15+
= FragmentationIndicatorUndivided
16+
| FragmentationIndicatorDividedHead
17+
| FragmentationIndicatorDividedBody
18+
| FragmentationIndicatorDividedEnd
19+
deriving (Show, Enum)
20+
21+
data ISO639LanguageCode = ISO639LanguageCode Word8 Word8 Word8
22+
23+
instance Show ISO639LanguageCode where
24+
show (ISO639LanguageCode a b c) =
25+
[ toEnum $ fromIntegral a,
26+
toEnum $ fromIntegral b,
27+
toEnum $ fromIntegral c
28+
]
29+
30+
instance Binary ISO639LanguageCode where
31+
get = ISO639LanguageCode <$> get <*> get <*> get
32+
put (ISO639LanguageCode a b c) = put a >> put b >> put c
33+
34+
consumeAll :: Get a -> ByteString -> Get a
35+
consumeAll g bs = do
36+
case pushEndOfInput $ runGetIncremental g `pushChunks` bs of
37+
Fail _ loc err -> fail $ "error at " ++ show loc ++ ": " ++ err
38+
Partial _ -> fail "not enough bytes"
39+
-- Done _ _ a -> return a
40+
Done r _ a -> if B.null r then return a else fail "unconsumed input"
41+
42+
repeatRead :: Get a -> Get [a]
43+
repeatRead g = do
44+
end <- isEmpty
45+
if end
46+
then return []
47+
else do
48+
a <- g
49+
as <- repeatRead g
50+
return $ a : as
51+
52+
readN :: Binary a => Word8 -> Get [a]
53+
readN 0 = return []
54+
readN n = (:) <$> get <*> readN (n - 1)

0 commit comments

Comments
 (0)