serialize.ml 3.95 KB
Newer Older
1
2
3
4
5
6
module Put = struct
  type t = { 
    buf : Buffer.t;
    mutable cur_byte : int; (* 0..255 *)
    mutable cur_bits : int; (* 0..7 *)
  }
7
8
9

  let pos t = t.cur_bits + 8 * Buffer.length t.buf

10
11
  type 'a f = t -> 'a -> unit

12
13
14
15
  type 'b property = (t * 'b) list ref

  let properties = ref []

16
17
18
19
20
21
22
23
24
  let get_property prop t = 
    match !prop with
      | (s,x) :: _ when t == s -> x
      | l ->
	  let x = List.assq t l in
	  prop := (t,x) :: (List.remove_assq t l);
	  x
	    (* Put in front of the list for quick access ... *)

25
26
27
28
29
30
31
  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

32
33
  let run f x =
    let t = { buf = Buffer.create 1024; cur_byte = 0; cur_bits = 0 } in
34
    List.iter (fun (f,_) -> f t) !properties;
35
    f t x;
36
    List.iter (fun (_,f) -> f t) !properties;
37
38
39
40
41
42
43
44
45
46
47
48
    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
    
49
50
  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))
51

52
(* TODO: handle negative ints better !! *)
53
  let rec int t i =
54
    assert (i >= 0);
55
    bits 4 t i;
56
57
58
    let i = i lsr 4 in
    if i <> 0 then (bool t true; int t i) else (bool t false)

59
60
61
  let substring t s pos len =
    int t len;
    for i = pos to pos + len - 1 do
62
      bits 8 t (Char.code (s.[i]))
63
64
    done

65
66
67
  let string t s =
    substring t s 0 (String.length s)

68
69
70
71
72
  let magic t s =
    for i = 0 to String.length s - 1 do
      bits 8 t (Char.code (s.[i]))
    done

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

77
78
79
80
81
82
  let array f t a =
    int t (Array.length a);
    for i = 0 to Array.length a - 1 do
      f t a.(i)
    done

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

85
86
87
88
89
end


module Get = struct
  type t = { buf : string; mutable idx : int; mutable idx_bits : int }
90
91
92

  let pos t = t.idx_bits + 8 * t.idx

93
94
  type 'a f = t -> 'a

95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  type 'b property = (t * 'b) list ref

  let properties = ref []

  let get_property prop t = 
    match !prop with
      | (s,x) :: _ when t == s -> x
      | l ->
	  let x = List.assq t l in
	  prop := (t,x) :: (List.remove_assq t l);
	  x
	    (* Put in front of the list for quick access ... *)

  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



117
  let run f s =
118
119
120
121
122
    let t = { buf = s; idx = 0; idx_bits = 0 } in
    List.iter (fun (f,_) -> f t) !properties;
    let res = f t in
    List.iter (fun (_,f) -> f t) !properties;
    res
123
124
125
126
127
128
129

  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
    
130
  let rec bits nb t =
131
132
    if nb = 0 then 0 
    else if bool t 
133
134
    then succ (bits (pred nb) t lsl 1)
    else bits (pred nb) t lsl 1
135
136

  let rec int t =
137
    let i = bits 4 t in
138
139
140
141
142
143
144
    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
145
      s.[i] <- Char.chr (bits 8 t)
146
147
148
    done;
    s

149
150
151
152
153
154
  let magic t s =
    for i = 0 to String.length s - 1 do
      let c = bits 8 t in
      if (Char.code (s.[i]) != c) then failwith "Invalid magic code."
    done

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

159
160
161
162
163
164
165
166
167
168
  let array f t =
    let n = int t in
    if n = 0 then [| |]
    else
      let a = Array.create n (f t) in
      for i = 1 to Array.length a - 1 do
	a.(i) <- f t
      done;
      a

169
170
171
172
173
  let pair f1 f2 t =
    let x = f1 t in
    let y = f2 t in
    (x,y)
end
174
175
176
177