serialize.ml 2.4 KB
Newer Older
1
2
3
4
5
6
7
8
module Put = struct
  type t = { 
    buf : Buffer.t;
    mutable cur_byte : int; (* 0..255 *)
    mutable cur_bits : int; (* 0..7 *)
  }
  type 'a f = t -> 'a -> unit

9
10
11
12
13
14
15
16
17
18
19
20
  type 'b property = (t * 'b) list ref

  let properties = ref []

  let get_property prop t = List.assq t !prop
  let mk_property init = 
    let prop = ref [] in
    properties := 
    ((fun t -> prop := (t, init t) :: !prop),
     (fun t -> prop := List.remove_assq t !prop)) :: !properties;
    prop

21
22
  let run f x =
    let t = { buf = Buffer.create 1024; cur_byte = 0; cur_bits = 0 } in
23
    List.iter (fun (f,_) -> f t) !properties;
24
    f t x;
25
    List.iter (fun (_,f) -> f t) !properties;
26
27
28
29
30
31
32
33
34
35
36
37
    if t.cur_bits > 0 then Buffer.add_char t.buf (Char.chr t.cur_byte); 
    Buffer.contents t.buf

  let bool t b =
    if b then t.cur_byte <- t.cur_byte lor (1 lsl t.cur_bits);
    if t.cur_bits = 7 then (
      Buffer.add_char t.buf (Char.chr t.cur_byte);
      t.cur_byte <- 0;
      t.cur_bits <- 0
    ) else
      t.cur_bits <- succ t.cur_bits
    
38
39
  let rec bits nb t i = (* TODO: opt *)
    if (nb > 0) then (bool t ((i land 1) <> 0); bits (pred nb) t (i lsr 1))
40
41

  let rec int t i =
42
    bits 4 t i;
43
44
45
46
47
48
49
    let i = i lsr 4 in
    if i <> 0 then (bool t true; int t i) else (bool t false)

  let string t s =
    let l = String.length s in 
    int t l;
    for i = 0 to l - 1 do
50
      bits 8 t (Char.code (s.[i]))
51
52
53
54
55
56
57
    done

  let rec list f t = function
    | [] -> bool t false
    | hd::tl -> bool t true; f t hd; list f t tl

  let pair f1 f2 t (x,y) = f1 t x; f2 t y
58

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
end


module Get = struct
  type t = { buf : string; mutable idx : int; mutable idx_bits : int }
  type 'a f = t -> 'a

  let run f s =
    f { buf = s; idx = 0; idx_bits = 0 }

  let bool t =
    let b = ((Char.code t.buf.[t.idx]) lsr t.idx_bits) land 1 <> 0 in
    if t.idx_bits = 7 then (t.idx_bits <- 0; t.idx <- succ t.idx)
    else t.idx_bits <- succ t.idx_bits;
    b
    
75
  let rec bits nb t =
76
77
    if nb = 0 then 0 
    else if bool t 
78
79
    then succ (bits (pred nb) t lsl 1)
    else bits (pred nb) t lsl 1
80
81

  let rec int t =
82
    let i = bits 4 t in
83
84
85
86
87
88
89
    if bool t then i + (int t) lsl 4 
    else i

  let string t =
    let l = int t in
    let s = String.create l in
    for i = 0 to l - 1 do
90
      s.[i] <- Char.chr (bits 8 t)
91
92
93
94
95
96
97
98
99
100
101
102
    done;
    s

  let rec list f t =
    if bool t then let hd = f t in hd::(list f t)
    else []

  let pair f1 f2 t =
    let x = f1 t in
    let y = f2 t in
    (x,y)
end