11(* * Basic utilities for postcard deserialization. *)
22
3- type postcard_state = { ic : in_channel }
3+ (* * We use our own buffer rather than moving the [in_channel], which is more
4+ performant. *)
5+ type postcard_state = {
6+ ic : in_channel ;
7+ buf : Bytes .t ;
8+ mutable buf_pos : int ; (* * The next byte to read in [buf]. *)
9+ mutable buf_len : int ; (* * The number of bytes read into [buf]. *)
10+ mutable buf_ofs : int ; (* * The position in the input of [buf]'s start. *)
11+ }
12+
13+ let state_of_channel (ic : in_channel ) : postcard_state =
14+ {
15+ ic;
16+ buf = Bytes. create 65536 ;
17+ buf_pos = 0 ;
18+ buf_len = 0 ;
19+ buf_ofs = pos_in ic;
20+ }
21+
22+ (* * The position of the next byte to read in the input. *)
23+ let [@ inline] pos (st : postcard_state ) : int = st.buf_ofs + st.buf_pos
24+
25+ (* * Moves to the given position of the input. *)
26+ let [@ inline] seek (st : postcard_state ) (pos : int ) : unit =
27+ seek_in st.ic pos;
28+ st.buf_pos < - 0 ;
29+ st.buf_len < - 0 ;
30+ st.buf_ofs < - pos
31+
32+ (* * Reads more input into the buffer; [false] if the end of the input is
33+ reached. *)
34+ let [@ inline] refill (st : postcard_state ) : bool =
35+ st.buf_ofs < - st.buf_ofs + st.buf_len;
36+ st.buf_pos < - 0 ;
37+ st.buf_len < - input st.ic st.buf 0 (Bytes. length st.buf);
38+ st.buf_len > 0
39+
440type input_format_hint = Json | Postcard | Empty
541
642(* * Attempts detecting the format of a string, which corresponds to the first N
@@ -30,12 +66,12 @@ let format_hint_of_file (file : string) : (input_format_hint, string) result =
3066(* * Same as [format_hint_of_file], but operates on an already-opened in_channel
3167 and restores the position after reading the sample. *)
3268let format_hint_of_postcard (st : postcard_state ) : input_format_hint =
33- let pos = pos_in st.ic in
34- seek_in st.ic 0 ;
69+ let pos = pos st in
70+ seek st 0 ;
3571 let sample_len = min 64 (in_channel_length st.ic) in
3672 let sample = really_input_string st.ic sample_len in
3773 let hint = format_hint_of_string sample in
38- seek_in st.ic pos;
74+ seek st pos;
3975 hint
4076
4177let ( let* ) o f =
@@ -45,71 +81,85 @@ let ( let* ) o f =
4581
4682let with_state_of_file (file : string )
4783 (f : postcard_state -> ('a, string) result ) : ('a, string) result =
48- try In_channel. with_open_bin file (fun ic -> f { ic } )
84+ try In_channel. with_open_bin file (fun ic -> f (state_of_channel ic) )
4985 with Sys_error e -> Error e
5086
5187let combine_error_msgs (st : postcard_state ) (msg : string )
5288 (res : ('a, string) result ) : ('a, string) result =
5389 match res with
5490 | Ok x -> Ok x
5591 | Error e ->
56- let rem = in_channel_length st.ic - pos_in st.ic in
92+ let pos = pos st in
93+ let rem = in_channel_length st.ic - pos in
5794 let n = if rem < 8 then rem else 8 in
58- let pos = pos_in st.ic in
95+ seek st pos;
5996 let preview =
60- List. init n (fun i -> try input_byte st.ic with End_of_file -> 0 )
97+ List. init n (fun _ -> try input_byte st.ic with End_of_file -> 0 )
6198 |> List. map (fun b -> Printf. sprintf " %02x" b)
6299 |> String. concat " "
63100 in
101+ seek st pos;
64102 Error
65103 (msg ^ " failed at byte " ^ string_of_int pos ^ " (next: " ^ preview
66104 ^ " ): " ^ e)
67105
68106let take_u8 (st : postcard_state ) : (int, string) result =
69- try Ok (input_byte st.ic)
70- with End_of_file ->
71- Error " unexpected end of postcard input, expected 1 more byte"
107+ if st.buf_pos < st.buf_len || refill st then (
108+ let b = Bytes. unsafe_get st.buf st.buf_pos in
109+ st.buf_pos < - st.buf_pos + 1 ;
110+ Ok (Char. code b))
111+ else Error " unexpected end of postcard input, expected 1 more byte"
72112
73113let take_bytes (st : postcard_state ) (len : int ) : (bytes, string) result =
74- try
75- let bytes = Bytes. create len in
76- really_input st.ic bytes 0 len;
77- Ok bytes
78- with End_of_file ->
79- Error
80- (" unexpected end of postcard input, expected " ^ string_of_int len
81- ^ " more bytes" )
82-
83- let read_varint_z ~(max_bytes : int ) ~(max_last : int ) (st : postcard_state ) :
84- (Z. t , string ) result =
85- let rec loop (i : int ) (shift : int ) (acc : Z.t ) : (Z.t, string) result =
114+ let bytes = Bytes. create len in
115+ let rec loop ofs =
116+ if ofs > = len then Ok bytes
117+ else if st.buf_pos < st.buf_len || refill st then (
118+ let n = min (len - ofs) (st.buf_len - st.buf_pos) in
119+ Bytes. blit st.buf st.buf_pos bytes ofs n;
120+ st.buf_pos < - st.buf_pos + n;
121+ loop (ofs + n))
122+ else
123+ Error
124+ (" unexpected end of postcard input, expected " ^ string_of_int len
125+ ^ " more bytes" )
126+ in
127+ loop 0
128+
129+ (* * Reads a varint of at most [max_bytes] bytes, whose last byte is at most
130+ [max_last], as an OCaml int of at most [bits] bits (taken as unsigned). *)
131+ let read_varint ~(max_bytes : int ) ~(max_last : int ) ~(bits : int )
132+ (st : postcard_state ) : (int, string) result =
133+ let rec loop (i : int ) (shift : int ) (acc : int ) : (int, string) result =
86134 if i > = max_bytes then Error " invalid postcard varint"
87135 else
88136 let * raw = take_u8 st in
89137 let payload = raw land 0x7F in
90- let acc = Z. logor acc (Z. shift_left (Z. of_int payload) shift) in
91- let has_more = raw land 0x80 <> 0 in
92- if not has_more then
93- if i = max_bytes - 1 && raw > max_last then
94- Error " invalid postcard varint terminal byte"
95- else Ok acc
96- else loop (i + 1 ) (shift + 7 ) acc
138+ if payload <> 0 && (shift > = bits || payload lsr (bits - shift) <> 0 ) then
139+ Error " integer out of OCaml int range"
140+ else
141+ let acc = acc lor (payload lsl shift) in
142+ let has_more = raw land 0x80 <> 0 in
143+ if not has_more then
144+ if i = max_bytes - 1 && raw > max_last then
145+ Error " invalid postcard varint terminal byte"
146+ else Ok acc
147+ else loop (i + 1 ) (shift + 7 ) acc
97148 in
98- loop 0 0 Z. zero
149+ loop 0 0 0
99150
100- let z_to_int ( z : Z.t ) : (int, string) result =
101- if Z. geq z Z. zero && Z. leq z ( Z. of_int max_int) then Ok ( Z. to_int z)
102- else Error " integer out of OCaml int range "
151+ (* * Unsigned integers, of at most 62 bits. *)
152+ let read_unsigned ~ max_bytes ~ max_last st =
153+ read_varint ~max_bytes ~max_last ~bits: 62 st
103154
104- let z_to_i ( z : Z.t ) : (Z.t, string) result =
105- let one = Z. one in
106- if Z. equal ( Z. logand z one) Z. zero then Ok ( Z. shift_right z 1 )
107- else Ok Z. (neg (add (shift_right z 1 ) one ))
155+ (* * Zigzag-encoded signed integers, of at most 63 bits. *)
156+ let read_signed ~ max_bytes ~ max_last st =
157+ let * z = read_varint ~max_bytes ~max_last ~bits: 63 st in
158+ Ok ((z lsr 1 ) lxor - (z land 1 ))
108159
109160let usize_of_postcard (_ctx : 'ctx ) (st : postcard_state ) : (int , string ) result
110161 =
111- let * z = read_varint_z ~max_bytes: 10 ~max_last: 1 st in
112- z_to_int z
162+ read_unsigned ~max_bytes: 10 ~max_last: 1 st
113163
114164let bool_of_postcard (_ctx : 'ctx ) (st : postcard_state ) : (bool , string ) result
115165 =
@@ -120,41 +170,29 @@ let bool_of_postcard (_ctx : 'ctx) (st : postcard_state) : (bool, string) result
120170 | _ -> Error " invalid postcard bool"
121171
122172let int_of_postcard (_ctx : 'ctx ) (st : postcard_state ) : (int, string) result =
123- let * z = read_varint_z ~max_bytes: 5 ~max_last: 15 st in
124- z_to_int z
173+ read_unsigned ~max_bytes: 5 ~max_last: 15 st
125174
126175let u16_of_postcard (_ctx : 'ctx ) (st : postcard_state ) : (int, string) result =
127- let * z = read_varint_z ~max_bytes: 3 ~max_last: 3 st in
128- z_to_int z
176+ read_unsigned ~max_bytes: 3 ~max_last: 3 st
129177
130178let u32_of_postcard (_ctx : 'ctx ) (st : postcard_state ) : (int, string) result =
131- let * z = read_varint_z ~max_bytes: 5 ~max_last: 15 st in
132- z_to_int z
179+ read_unsigned ~max_bytes: 5 ~max_last: 15 st
133180
134181let u64_of_postcard (_ctx : 'ctx ) (st : postcard_state ) : (int, string) result =
135- let * z = read_varint_z ~max_bytes: 10 ~max_last: 1 st in
136- z_to_int z
182+ read_unsigned ~max_bytes: 10 ~max_last: 1 st
137183
138184let i16_of_postcard (_ctx : 'ctx ) (st : postcard_state ) : (int, string) result =
139- let * z = read_varint_z ~max_bytes: 3 ~max_last: 3 st in
140- let * s = z_to_i z in
141- z_to_int s
185+ read_signed ~max_bytes: 3 ~max_last: 3 st
142186
143187let i32_of_postcard (_ctx : 'ctx ) (st : postcard_state ) : (int, string) result =
144- let * z = read_varint_z ~max_bytes: 5 ~max_last: 15 st in
145- let * s = z_to_i z in
146- z_to_int s
188+ read_signed ~max_bytes: 5 ~max_last: 15 st
147189
148190let i64_of_postcard (_ctx : 'ctx ) (st : postcard_state ) : (int, string) result =
149- let * z = read_varint_z ~max_bytes: 10 ~max_last: 1 st in
150- let * s = z_to_i z in
151- z_to_int s
191+ read_signed ~max_bytes: 10 ~max_last: 1 st
152192
153193let isize_of_postcard (_ctx : 'ctx ) (st : postcard_state ) : (int , string ) result
154194 =
155- let * z = read_varint_z ~max_bytes: 10 ~max_last: 1 st in
156- let * s = z_to_i z in
157- z_to_int s
195+ read_signed ~max_bytes: 10 ~max_last: 1 st
158196
159197let u8_of_postcard (_ctx : 'ctx ) (st : postcard_state ) : (int, string) result =
160198 take_u8 st
@@ -235,20 +273,18 @@ let list_of_postcard
235273 (a_of_postcard : 'ctx -> postcard_state -> ('a, string) result ) (ctx : 'ctx )
236274 (st : postcard_state ) : ('a list, string) result =
237275 let * len = usize_of_postcard ctx st in
238- if len > 1_000_000 then
239- Error
240- (" suspicious postcard sequence length " ^ string_of_int len
241- ^ " (varint read at byte "
242- ^ string_of_int (pos_in st.ic)
243- ^ " )" )
244- else
245- let rec loop i acc =
246- if i < = 0 then Ok (List. rev acc)
247- else
248- let * x = a_of_postcard ctx st in
249- loop (i - 1 ) (x :: acc)
250- in
251- loop len []
276+ (* this is a little scary, but we never access the array unless we have
277+ computed all indices, so it's fine. going through an array is more memory
278+ efficient than a list, which we would have to reverse. *)
279+ let arr = Array. make len (Obj. magic 0 ) in
280+ let rec loop i =
281+ if i == len then Ok (Array. to_list arr)
282+ else
283+ let * x = a_of_postcard ctx st in
284+ Array. set arr i x;
285+ loop (i + 1 )
286+ in
287+ loop 0
252288
253289let option_of_postcard
254290 (a_of_postcard : 'ctx -> postcard_state -> ('a, string) result ) (ctx : 'ctx )
@@ -301,7 +337,7 @@ let key_value_pair_of_postcard
301337
302338let ensure_eof (st : postcard_state ) : (unit, string) result =
303339 let len = in_channel_length st.ic in
304- let pos = pos_in st.ic in
340+ let pos = pos st in
305341 if pos > = len then Ok ()
306342 else
307343 Format. kasprintf Result. error
0 commit comments