-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathLittleEndian.hs
83 lines (71 loc) · 2.73 KB
/
LittleEndian.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
--
-- Safe.hs --- Checked binary packing/unpacking.
--
-- Copyright (C) 2015, Galois, Inc.
-- All Rights Reserved.
--
module Ivory.Serialize.Safe.LittleEndian where
import Ivory.Language
import Ivory.Serialize.Atoms
import Ivory.Serialize.PackRep
packInto :: (Packable a, ANat len)
=> Ref s1 ('Array len ('Stored Uint8)) -- ^ buf
-> Uint32 -- ^ offset
-> ConstRef s2 a -- ^ value
-> Ivory ('Effects r b ('Scope s3)) ()
packInto = packInto' packRep
packInto' :: ANat len
=> PackRep a -- ^ encoding
-> Ref s1 ('Array len ('Stored Uint8)) -- ^ buf
-> Uint32 -- ^ offset
-> ConstRef s2 a -- ^ value
-> Ivory ('Effects r b ('Scope s3)) ()
packInto' rep buf offs val = do
assert $ offs + fromIntegral (packSize rep) <=? arrayLen buf
packSetLE rep (toCArray buf) offs val
packVInto :: (Packable ('Stored a), ANat len, IvoryInit a)
=> Ref s1 ('Array len ('Stored Uint8)) -- ^ buf
-> Uint32 -- ^ offset
-> a -- ^ value
-> Ivory ('Effects r b ('Scope s2)) ()
packVInto = packVInto' packRep
packVInto' :: (ANat len, IvoryInit a)
=> PackRep ('Stored a) -- ^ encoding
-> Ref s1 ('Array len ('Stored Uint8)) -- ^ buf
-> Uint32 -- ^ offset
-> a -- ^ value
-> Ivory ('Effects r b ('Scope s2)) ()
packVInto' rep buf offs val = do
tmp <- local $ ival val
packInto' rep buf offs (constRef tmp)
unpackFrom :: (Packable a, ANat len)
=> ConstRef s1 ('Array len ('Stored Uint8)) -- ^ buf
-> Uint32 -- ^ offset
-> Ref s2 a -- ^ value
-> Ivory ('Effects r b ('Scope s3)) ()
unpackFrom = unpackFrom' packRep
unpackFrom' :: ANat len
=> PackRep a -- ^ encoding
-> ConstRef s1 ('Array len ('Stored Uint8)) -- ^ buf
-> Uint32 -- ^ offset
-> Ref s2 a -- ^ value
-> Ivory ('Effects r b ('Scope s3)) ()
unpackFrom' rep buf offs val = do
assert $ offs + fromIntegral (packSize rep) <=? arrayLen buf
packGetLE rep (toCArray buf) offs val
unpackVFrom :: (Packable ('Stored a), ANat len, IvoryStore a, IvoryZeroVal a)
=> ConstRef s1 ('Array len ('Stored Uint8)) -- ^ buf
-> Uint32 -- ^ offset
-> Ivory ('Effects r b ('Scope s2)) a
unpackVFrom = unpackVFrom' packRep
unpackVFrom' :: (Packable ('Stored a), ANat len, IvoryStore a, IvoryZeroVal a)
=> PackRep ('Stored a)
-> ConstRef s1 ('Array len ('Stored Uint8)) -- ^ buf
-> Uint32 -- ^ offset
-> Ivory ('Effects r b ('Scope s2)) a
unpackVFrom' rep buf offs = do
tmp <- local izero
unpackFrom' rep buf offs tmp
deref tmp