11(* * Low-level buffer manipulation *)
22
3- type t =
4- (int , Bigarray .int8_unsigned_elt , Bigarray .c_layout ) Bigarray.Array1 .t
5-
6- type cursor = {
7- buffer : t ;
8- mutable position : int ;
9- }
3+ type t = (int , Bigarray .int8_unsigned_elt , Bigarray .c_layout ) Bigarray.Array1 .t
4+ type cursor = { buffer : t ; mutable position : int }
105
116exception Invalid_format of string
7+
128let invalid_format msg = raise (Invalid_format msg)
139
1410let parse path =
15- let fd = Unix. openfile path [Unix. O_RDONLY ] 0 in
11+ let fd = Unix. openfile path [ Unix. O_RDONLY ] 0 in
1612 let len = Unix. lseek fd 0 Unix. SEEK_END in
1713 let t =
1814 Bigarray. array1_of_genarray
19- (Unix. map_file fd Bigarray. int8_unsigned
20- Bigarray. c_layout false [|len|]) in
15+ (Unix. map_file fd Bigarray. int8_unsigned Bigarray. c_layout false [| len |])
16+ in
2117 Unix. close fd;
2218 t
2319
2420let size = Bigarray.Array1. dim
25-
26- let cursor ?(at =0 ) buffer =
27- { buffer; position = at }
28-
29- let seek t position =
30- t.position < - position
21+ let cursor ?(at = 0 ) buffer = { buffer; position = at }
22+ let seek t position = t.position < - position
3123
3224let ensure t count msg =
3325 (* Ensure position does not overflow before checking for buffer overflow. *)
3426 let new_pos = t.position + count in
35- if (new_pos < 0 ) || (size t.buffer < new_pos) then
36- invalid_format msg
27+ if new_pos < 0 || size t.buffer < new_pos then invalid_format msg
3728
3829let advance t count = t.position < - t.position + count
39-
4030let at_end t = size t.buffer = t.position
4131
4232open Types
@@ -46,28 +36,28 @@ module Read = struct
4636 let u8 t : u8 =
4737 let result = t.buffer.{t.position} in
4838 advance t 1 ;
49- result
39+ Unsigned.UInt8. of_int result
5040
5141 let s8 t : s8 =
5242 let result = t.buffer.{t.position} in
5343 advance t 1 ;
54- if result > 0x7F
55- then result lor ((- 1 ) lsl 8 )
56- else result
44+ if result > 0x7F then Signed.Int8. of_int (result lor (- 1 lsl 8 ))
45+ else Signed.Int8. of_int result
5746
5847 let u16 t : u16 =
59- let result = t.buffer.{t.position} lor t.buffer.{t.position + 1 } lsl 8 in
48+ let result = t.buffer.{t.position} lor ( t.buffer.{t.position + 1 } lsl 8 ) in
6049 advance t 2 ;
61- result
50+ Unsigned.UInt16. of_int result
6251
6352 let u32 t : u32 =
64- let result = t.buffer.{t.position}
65- lor t.buffer.{t.position + 1 } lsl 8
66- lor t.buffer.{t.position + 2 } lsl 16
67- lor t.buffer.{t.position + 3 } lsl 24
53+ let result =
54+ t.buffer.{t.position}
55+ lor (t.buffer.{t.position + 1 } lsl 8 )
56+ lor (t.buffer.{t.position + 2 } lsl 16 )
57+ lor (t.buffer.{t.position + 3 } lsl 24 )
6858 in
6959 advance t 4 ;
70- result
60+ Unsigned.UInt32. of_int result
7161
7262 let u32be = u32
7363
@@ -79,38 +69,32 @@ module Read = struct
7969 result := logor ! result (shift_left n (i * 8 ))
8070 done ;
8171 advance t 8 ;
82- ! result
72+ Unsigned.UInt64. of_int64 ! result
8373
8474 let i64 t : i64 =
8575 (* u64 are wrapped in an i64 and are actually signed. *)
86- u64 t
76+ Unsigned.UInt64. to_int64 ( u64 t)
8777
8878 let uleb128 t : u128 =
8979 let rec aux t shift acc =
90- let x = u8 t in
80+ let x = u8 t |> Unsigned.UInt8. to_int in
9181 let acc = acc lor ((x land 0x7f ) lsl shift) in
92- if x land 0x80 = 0 then
93- acc
94- else
95- aux t (shift + 7 ) acc
82+ if x land 0x80 = 0 then acc else aux t (shift + 7 ) acc
9683 in
9784 aux t 0 0
9885
9986 let sleb128 t : s128 =
10087 let rec aux t shift acc =
101- let x = u8 t in
88+ let x = u8 t |> Unsigned.UInt8. to_int in
10289 let acc = acc lor ((x land 0x7f ) lsl shift) in
10390 if x land 0x80 = 0 then
104- if x land 0x40 = 0
105- then acc
106- else acc lor - (1 lsl (shift + 7 ))
107- else
108- aux t (shift + 7 ) acc
91+ if x land 0x40 = 0 then acc else acc lor - (1 lsl (shift + 7 ))
92+ else aux t (shift + 7 ) acc
10993 in
11094 aux t 0 0
11195
11296 let fixed_string t length =
113- let {buffer; position} = t in
97+ let { buffer; position } = t in
11498 let result = Bytes. create length in
11599 for i = 0 to length - 1 do
116100 Bytes. set result i (Char. unsafe_chr buffer.{position + i})
@@ -119,30 +103,27 @@ module Read = struct
119103 Bytes. unsafe_to_string result
120104
121105 let rec scan_0 (b : t ) ofs l i =
122- if i > = l then
123- None
124- else if b.{ofs + i} = 0 then
125- Some i
126- else
127- scan_0 b ofs l (i + 1 )
106+ if i > = l then None
107+ else if b.{ofs + i} = 0 then Some i
108+ else scan_0 b ofs l (i + 1 )
128109
129110 let zero_string t ?maxlen () =
130- let maxlen = match maxlen with
111+ let maxlen =
112+ match maxlen with
131113 | None -> size t.buffer - t.position
132114 | Some maxlen -> maxlen
133115 in
134116 match scan_0 t.buffer t.position maxlen 0 with
135117 | None -> None
136118 | Some length ->
137- let result = fixed_string t length in
138- advance t 1 ;
139- Some result
119+ let result = fixed_string t length in
120+ advance t 1 ;
121+ Some result
140122
141123 let buffer t length =
142124 let result = Bigarray.Array1. sub t.buffer t.position length in
143125 advance t length;
144126 result
145127end
146128
147- let sub t length =
148- cursor (Read. buffer t length)
129+ let sub t length = cursor (Read. buffer t length)
0 commit comments