0 | module Data.Compress.Utils.Bytes
2 | import Syntax.WithProof
6 | import Data.Fin.Extra
10 | import Data.Nat.Factor
11 | import Data.Nat.Order.Properties
13 | weakenN' : {m : Nat} -> (0 n : Nat) -> Fin m -> Fin (n + m)
14 | weakenN' n m' = rewrite plusCommutative n m in weakenN n m'
16 | fix_fin : (m : Nat) -> (n : Nat) -> (S m) = n -> S (S (S (S (S (S (S (S (mult m 8)))))))) = mult n 8
17 | fix_fin m n prf = rewrite sym prf in Refl
20 | to_le : (FiniteBits a, Cast a Bits8) => {n : _} -> {auto 0 no0 : NonZero n} -> {auto 0 prf : n * 8 = (bitSize {a})} -> a -> Vect n Bits8
21 | to_le x = let (S m) = n;
nmeq = Refl in map (go nmeq x) Fin.range
23 | go : {m : Nat} -> ((S m) = n) -> a -> Fin (S m) -> Bits8
24 | go nmeq b i = cast $
shiftR b (bitsToIndex $
coerce prf $
coerce (fix_fin m n nmeq) $
weakenN' 7 $
i * 8)
27 | from_le : (FiniteBits a, Cast Bits8 a) => {n : _} -> {auto 0 no0 : NonZero n} -> {auto 0 prf : n * 8 = (bitSize {a})} -> Vect n Bits8 -> a
28 | from_le p = let (S m) = n;
nmeq = Refl in foldl (.|.) zeroBits $
zipWith (go nmeq) p Fin.range
30 | go : {m : Nat} -> ((S m) = n) -> Bits8 -> Fin (S m) -> a
31 | go nmeq b i = shiftL (cast b) (bitsToIndex $
coerce prf $
coerce (fix_fin m n nmeq) $
weakenN' 7 $
i * 8)
34 | le_to_integer : Foldable t => t Bits8 -> Integer
35 | le_to_integer = go . toList
37 | go : List Bits8 -> Integer
38 | go v = foldr (.|.) 0 $
zipWith shiftL (cast {to=Integer} <$> v) ((*8) <$> [0..(length v)])
41 | integer_to_le : (n : Nat) -> Integer -> Vect n Bits8
42 | integer_to_le n v = (cast . shiftR v) <$> (((*8) . finToNat) <$> Fin.range)
45 | string_to_ascii : (x : String) -> List Bits8
46 | string_to_ascii = map (cast . ord) . unpack
49 | ascii_to_string : List Bits8 -> String
50 | ascii_to_string = pack . map cast