Debian Codegraph @

src/read.ml

raw symbols SCIP
1 (* Read_ml: reading values from the binary protocol using (mostly) OCaml. *)
2
3 (* Note: the code is this file is carefully written to avoid unnecessary allocations. When
4 touching this code, be sure to run the benchmarks to check for regressions. *)
5
6 open Bigarray
7 open Common
8 include Read_intf.Definitions
9
10 external unsafe_get : buf -> int -> char = "%caml_ba_unsafe_ref_1"
11 external unsafe_get8 : buf -> int -> int = "%caml_ba_unsafe_ref_1"
12
13 let unsafe_get8_signed buf pos =
14 let c = unsafe_get8 buf pos in
15 if c >= 128 then c - 256 else c
16 ;;
17
18 (*$ open Bin_prot_cinaps $*)
19
20 let arch_sixtyfour = Sys.word_size = 64
21 let arch_big_endian = Sys.big_endian
22 let max_int_int32 = if arch_sixtyfour then Int32.max_int else Int32.of_int max_int
23 let min_int_int32 = if arch_sixtyfour then Int32.min_int else Int32.of_int min_int
24 let max_int_int64 = Int64.of_int max_int
25 let min_int_int64 = Int64.of_int min_int
26
27 let[@inline always] safe_int_of_int32 pos x =
28 if arch_sixtyfour
29 then Int32.to_int x
30 else if x >= min_int_int32 && x <= max_int_int32
31 then Int32.to_int x
32 else raise_read_error ReadError.Int_overflow pos
33 ;;
34
35 let[@inline always] safe_int_of_int64 pos x =
36 if x >= min_int_int64 && x <= max_int_int64
37 then Int64.to_int x
38 else raise_read_error ReadError.Int_overflow pos
39 ;;
40
41 let safe_nativeint_of_int64 =
42 if arch_sixtyfour
43 then fun _pos x -> Int64.to_nativeint x
44 else
45 fun [@inline always] pos x ->
46 if x >= Int64.of_nativeint Nativeint.min_int
47 && x <= Int64.of_nativeint Nativeint.max_int
48 then Int64.to_nativeint x
49 else raise_read_error ReadError.Int_overflow pos
50 ;;
51
52 external unsafe_get16 : buf -> int -> int = "%caml_bigstring_get16u"
53 external unsafe_get32 : buf -> int -> int32 = "%caml_bigstring_get32u"
54 external unsafe_get64 : buf -> int -> int64 = "%caml_bigstring_get64u"
55 external bswap16 : int -> int = "%bswap16"
56 external bswap32 : int32 -> int32 = "%bswap_int32"
57 external bswap64 : int64 -> int64 = "%bswap_int64"
58
59 let unsafe_get16be_unsigned =
60 if arch_big_endian then unsafe_get16 else fun buf pos -> unsafe_get16 buf pos |> bswap16
61 ;;
62
63 let unsafe_get32be =
64 if arch_big_endian then unsafe_get32 else fun buf pos -> unsafe_get32 buf pos |> bswap32
65 ;;
66
67 let unsafe_get64be =
68 if arch_big_endian then unsafe_get64 else fun buf pos -> unsafe_get64 buf pos |> bswap64
69 ;;
70
71 let unsafe_get16le_unsigned =
72 if arch_big_endian then fun buf pos -> unsafe_get16 buf pos |> bswap16 else unsafe_get16
73 ;;
74
75 let unsafe_get32le =
76 if arch_big_endian then fun buf pos -> unsafe_get32 buf pos |> bswap32 else unsafe_get32
77 ;;
78
79 let unsafe_get64le =
80 if arch_big_endian then fun buf pos -> unsafe_get64 buf pos |> bswap64 else unsafe_get64
81 ;;
82
83 let unsafe_get16le_signed buf pos =
84 let x = unsafe_get16le_unsigned buf pos in
85 if x > 32767 then x - 65536 else x
86 ;;
87
88 let bin_read_unit buf ~pos_ref =
89 let pos = safe_get_pos buf pos_ref in
90 assert_pos pos;
91 if unsafe_get buf pos = '\000'
92 then pos_ref := pos + 1
93 else raise_read_error ReadError.Unit_code pos
94 ;;
95
96 let bin_read_bool buf ~pos_ref =
97 let pos = safe_get_pos buf pos_ref in
98 assert_pos pos;
99 match unsafe_get buf pos with
100 | '\000' ->
101 pos_ref := pos + 1;
102 false
103 | '\001' ->
104 pos_ref := pos + 1;
105 true
106 | _ -> raise_read_error ReadError.Bool_code pos
107 ;;
108
109 let safe_bin_read_neg_int8 buf ~pos_ref ~pos =
110 let next = pos + 1 in
111 check_next buf next;
112 let n = unsafe_get8_signed buf pos in
113 if n >= 0 then raise_read_error ReadError.Neg_int8 !pos_ref;
114 pos_ref := next;
115 n
116 ;;
117
118 let safe_bin_read_int16 buf ~pos_ref ~pos =
119 let next = pos + 2 in
120 check_next buf next;
121 pos_ref := next;
122 (* Can be above next line (no errors possible with 16bit).
123 This should improve the generated code. *)
124 unsafe_get16le_signed buf pos
125 ;;
126
127 let safe_bin_read_int32 buf ~pos_ref ~pos =
128 let next = pos + 4 in
129 check_next buf next;
130 pos_ref := next;
131 (* No error possible either. *)
132 unsafe_get32le buf pos
133 ;;
134
135 let safe_bin_read_int64 buf ~pos_ref ~pos =
136 let next = pos + 8 in
137 check_next buf next;
138 pos_ref := next;
139 (* No error possible either. *)
140 unsafe_get64le buf pos
141 ;;
142
143 let safe_bin_read_int32_as_int buf ~pos_ref ~pos =
144 let next = pos + 4 in
145 check_next buf next;
146 let n = unsafe_get32le buf pos in
147 let n = safe_int_of_int32 !pos_ref n in
148 pos_ref := next;
149 n
150 ;;
151
152 let safe_bin_read_int64_as_int buf ~pos_ref ~pos =
153 let next = pos + 8 in
154 check_next buf next;
155 let n = unsafe_get64le buf pos in
156 let n = safe_int_of_int64 !pos_ref n in
157 pos_ref := next;
158 n
159 ;;
160
161 let safe_bin_read_int32_as_int64 buf ~pos_ref ~pos =
162 let next = pos + 4 in
163 check_next buf next;
164 pos_ref := next;
165 let n = unsafe_get32le buf pos in
166 Int64.of_int32 n
167 ;;
168
169 let safe_bin_read_int32_as_nativeint buf ~pos_ref ~pos =
170 let next = pos + 4 in
171 check_next buf next;
172 pos_ref := next;
173 let n = unsafe_get32le buf pos in
174 Nativeint.of_int32 n
175 ;;
176
177 let safe_bin_read_int64_as_nativeint buf ~pos_ref ~pos =
178 let next = pos + 8 in
179 check_next buf next;
180 let n = unsafe_get64le buf pos in
181 let n = safe_nativeint_of_int64 pos n in
182 pos_ref := next;
183 n
184 ;;
185
186 let safe_bin_read_nat0_16 buf ~pos_ref ~pos =
187 let next = pos + 2 in
188 check_next buf next;
189 pos_ref := next;
190 Nat0.unsafe_of_int (unsafe_get16le_unsigned buf pos)
191 ;;
192
193 let safe_bin_read_nat0_32 =
194 if arch_sixtyfour
195 then (
196 let mask_32bit = Int64.to_int 0xffff_ffffL in
197 fun buf ~pos_ref ~pos ->
198 let next = pos + 4 in
199 check_next buf next;
200 pos_ref := next;
201 let n = Int32.to_int (unsafe_get32le buf pos) in
202 if n >= 0
203 then Nat0.unsafe_of_int n
204 else
205 (* Erase the upper bits that were set to 1 during the int32 -> int conversion. *)
206 Nat0.unsafe_of_int (n land mask_32bit))
207 else
208 fun buf ~pos_ref ~pos ->
209 let next = pos + 4 in
210 check_next buf next;
211 let n = unsafe_get32le buf pos in
212 if n >= 0l && n <= max_int_int32
213 then (
214 let n = Nat0.unsafe_of_int (Int32.to_int n) in
215 pos_ref := next;
216 n)
217 else raise_read_error ReadError.Nat0_overflow !pos_ref
218 ;;
219
220 let safe_bin_read_nat0_64 buf ~pos_ref ~pos =
221 let next = pos + 8 in
222 check_next buf next;
223 let n = unsafe_get64le buf pos in
224 if n >= 0L && n <= max_int_int64
225 then (
226 let n = Nat0.unsafe_of_int (Int64.to_int n) in
227 pos_ref := next;
228 n)
229 else raise_read_error ReadError.Nat0_overflow !pos_ref
230 ;;
231
232 let bin_read_nat0 buf ~pos_ref =
233 let pos = safe_get_pos buf pos_ref in
234 assert_pos pos;
235 match unsafe_get buf pos with
236 | '\x00'..'\x7f' as ch ->
237 pos_ref := pos + 1;
238 Nat0.unsafe_of_int (Char.code ch)
239 | (*$ Code.char INT16 *)'\xfe'(*$*) ->
240 safe_bin_read_nat0_16 buf ~pos_ref ~pos:(pos + 1)
241 | (*$ Code.char INT32 *)'\xfd'(*$*) ->
242 safe_bin_read_nat0_32 buf ~pos_ref ~pos:(pos + 1)
243 | (*$ Code.char INT64 *)'\xfc'(*$*) ->
244 if arch_sixtyfour then
245 safe_bin_read_nat0_64 buf ~pos_ref ~pos:(pos + 1)
246 else
247 raise_read_error ReadError.Nat0_overflow pos
248 | _ ->
249 raise_read_error ReadError.Nat0_code pos
250 [@@ocamlformat "disable"]
251
252 let bin_read_bytes buf ~pos_ref =
253 let start_pos = !pos_ref in
254 let len = (bin_read_nat0 buf ~pos_ref :> int) in
255 if len > Sys.max_string_length then raise_read_error ReadError.String_too_long start_pos;
256 let pos = !pos_ref in
257 let next = pos + len in
258 check_next buf next;
259 pos_ref := next;
260 let str = Bytes.create len in
261 unsafe_blit_buf_bytes ~src_pos:pos buf ~dst_pos:0 str ~len;
262 str
263 ;;
264
265 let bin_read_string buf ~pos_ref =
266 let str = bin_read_bytes buf ~pos_ref in
267 Bytes.unsafe_to_string str
268 ;;
269
270 let bin_read_char buf ~pos_ref =
271 let pos = safe_get_pos buf pos_ref in
272 assert_pos pos;
273 pos_ref := pos + 1;
274 unsafe_get buf pos
275 ;;
276
277 let bin_read_int buf ~pos_ref =
278 let pos = safe_get_pos buf pos_ref in
279 assert_pos pos;
280 match unsafe_get buf pos with
281 | '\x00'..'\x7f' as ch ->
282 pos_ref := pos + 1;
283 Char.code ch
284 | (*$ Code.char NEG_INT8 *)'\xff'(*$*) ->
285 safe_bin_read_neg_int8 buf ~pos_ref ~pos:(pos + 1)
286 | (*$ Code.char INT16 *)'\xfe'(*$*) ->
287 safe_bin_read_int16 buf ~pos_ref ~pos:(pos + 1)
288 | (*$ Code.char INT32 *)'\xfd'(*$*) ->
289 safe_bin_read_int32_as_int buf ~pos_ref ~pos:(pos + 1)
290 | (*$ Code.char INT64 *)'\xfc'(*$*) ->
291 if arch_sixtyfour then
292 safe_bin_read_int64_as_int buf ~pos_ref ~pos:(pos + 1)
293 else
294 raise_read_error ReadError.Int_overflow pos
295 | _ ->
296 raise_read_error ReadError.Int_code pos
297 [@@ocamlformat "disable"]
298
299 let bin_read_float buf ~pos_ref =
300 let pos = safe_get_pos buf pos_ref in
301 assert_pos pos;
302 let next = pos + 8 in
303 check_next buf next;
304 pos_ref := next;
305 (* No error possible either. *)
306 Int64.float_of_bits (unsafe_get64le buf pos)
307 ;;
308
309 let bin_read_int32 buf ~pos_ref =
310 let pos = safe_get_pos buf pos_ref in
311 assert_pos pos;
312 match unsafe_get buf pos with
313 | '\x00'..'\x7f' as ch ->
314 pos_ref := pos + 1;
315 Int32.of_int (Char.code ch)
316 | (*$ Code.char NEG_INT8 *)'\xff'(*$*) ->
317 Int32.of_int (safe_bin_read_neg_int8 buf ~pos_ref ~pos:(pos + 1))
318 | (*$ Code.char INT16 *)'\xfe'(*$*) ->
319 Int32.of_int (safe_bin_read_int16 buf ~pos_ref ~pos:(pos + 1))
320 | (*$ Code.char INT32 *)'\xfd'(*$*) ->
321 safe_bin_read_int32 buf ~pos_ref ~pos:(pos + 1)
322 | _ ->
323 raise_read_error ReadError.Int32_code pos
324 [@@ocamlformat "disable"]
325
326 let bin_read_int64 buf ~pos_ref =
327 let pos = safe_get_pos buf pos_ref in
328 assert_pos pos;
329 match unsafe_get buf pos with
330 | '\x00'..'\x7f' as ch ->
331 pos_ref := pos + 1;
332 Int64.of_int (Char.code ch)
333 | (*$ Code.char NEG_INT8 *)'\xff'(*$*) ->
334 Int64.of_int (safe_bin_read_neg_int8 buf ~pos_ref ~pos:(pos + 1))
335 | (*$ Code.char INT16 *)'\xfe'(*$*) ->
336 Int64.of_int (safe_bin_read_int16 buf ~pos_ref ~pos:(pos + 1))
337 | (*$ Code.char INT32 *)'\xfd'(*$*) ->
338 safe_bin_read_int32_as_int64 buf ~pos_ref ~pos:(pos + 1)
339 | (*$ Code.char INT64 *)'\xfc'(*$*) ->
340 safe_bin_read_int64 buf ~pos_ref ~pos:(pos + 1)
341 | _ ->
342 raise_read_error ReadError.Int64_code pos
343 [@@ocamlformat "disable"]
344
345 let bin_read_nativeint buf ~pos_ref =
346 let pos = safe_get_pos buf pos_ref in
347 assert_pos pos;
348 match unsafe_get buf pos with
349 | '\x00'..'\x7f' as ch ->
350 pos_ref := pos + 1;
351 Nativeint.of_int (Char.code ch)
352 | (*$ Code.char NEG_INT8 *)'\xff'(*$*) ->
353 Nativeint.of_int (safe_bin_read_neg_int8 buf ~pos_ref ~pos:(pos + 1))
354 | (*$ Code.char INT16 *)'\xfe'(*$*) ->
355 Nativeint.of_int (safe_bin_read_int16 buf ~pos_ref ~pos:(pos + 1))
356 | (*$ Code.char INT32 *)'\xfd'(*$*) ->
357 safe_bin_read_int32_as_nativeint buf ~pos_ref ~pos:(pos + 1)
358 | (*$ Code.char INT64 *)'\xfc'(*$*) when arch_sixtyfour ->
359 safe_bin_read_int64_as_nativeint buf ~pos_ref ~pos:(pos + 1)
360 | _ ->
361 raise_read_error ReadError.Nativeint_code pos
362 [@@ocamlformat "disable"]
363
364 let bin_read_ref bin_read_el buf ~pos_ref =
365 let el = bin_read_el buf ~pos_ref in
366 ref el
367 ;;
368
369 let bin_read_lazy bin_read_el buf ~pos_ref =
370 let el = bin_read_el buf ~pos_ref in
371 Lazy.from_val el
372 ;;
373
374 let bin_read_option bin_read_el buf ~pos_ref =
375 let pos = safe_get_pos buf pos_ref in
376 assert_pos pos;
377 match unsafe_get buf pos with
378 | '\000' ->
379 pos_ref := pos + 1;
380 None
381 | '\001' ->
382 pos_ref := pos + 1;
383 let el = bin_read_el buf ~pos_ref in
384 Some el
385 | _ -> raise_read_error ReadError.Option_code pos
386 ;;
387
388 let bin_read_pair bin_read_a bin_read_b buf ~pos_ref =
389 let a = bin_read_a buf ~pos_ref in
390 let b = bin_read_b buf ~pos_ref in
391 a, b
392 ;;
393
394 let bin_read_triple bin_read_a bin_read_b bin_read_c buf ~pos_ref =
395 let a = bin_read_a buf ~pos_ref in
396 let b = bin_read_b buf ~pos_ref in
397 let c = bin_read_c buf ~pos_ref in
398 a, b, c
399 ;;
400
401 let bin_read_n_rev_list bin_read_el buf ~pos_ref len =
402 let rec loop n acc =
403 if n = 0 then acc else loop (n - 1) (bin_read_el buf ~pos_ref :: acc)
404 in
405 loop len []
406 ;;
407
408 let bin_read_list_with_max_len ~max_len bin_read_el buf ~pos_ref =
409 let len = (bin_read_nat0 buf ~pos_ref :> int) in
410 if len > max_len then raise_read_error (List_too_long { len; max_len }) !pos_ref;
411 let rev_lst = bin_read_n_rev_list bin_read_el buf ~pos_ref len in
412 List.rev rev_lst
413 ;;
414
415 let bin_read_list bin_read_el buf ~pos_ref =
416 bin_read_list_with_max_len ~max_len:max_int bin_read_el buf ~pos_ref
417 ;;
418
419 let dummy_float_buf = create_buf 8
420 let () = ignore (Write.bin_write_float dummy_float_buf ~pos:0 3.1)
421
422 let max_float_array_length =
423 if arch_sixtyfour then Sys.max_array_length else Sys.max_array_length / 2
424 ;;
425
426 let[@inline always] bin_read_float_array_gen ~create ~blit buf ~pos_ref =
427 let pos = !pos_ref in
428 let len = (bin_read_nat0 buf ~pos_ref :> int) in
429 if len > max_float_array_length then raise_read_error ReadError.Array_too_long pos;
430 let size = len * 8 in
431 let pos = !pos_ref in
432 let next = pos + size in
433 check_next buf next;
434 let arr = create len in
435 blit ~src_pos:pos buf ~dst_pos:0 arr ~len;
436 pos_ref := next;
437 arr
438 ;;
439
440 let bin_read_floatarray buf ~pos_ref =
441 bin_read_float_array_gen
442 ~create:Float.Array.create
443 ~blit:unsafe_blit_buf_floatarray
444 buf
445 ~pos_ref
446 ;;
447
448 let bin_read_float_array buf ~pos_ref =
449 bin_read_float_array_gen
450 ~create:Array.create_float
451 ~blit:unsafe_blit_buf_float_array
452 buf
453 ~pos_ref
454 ;;
455
456 let bin_read_array (type a) bin_read_el buf ~pos_ref =
457 if (Obj.magic (bin_read_el : a reader) : float reader) == bin_read_float
458 then (Obj.magic (bin_read_float_array buf ~pos_ref : float array) : a array)
459 else (
460 let start_pos = !pos_ref in
461 let len = (bin_read_nat0 buf ~pos_ref :> int) in
462 if len = 0
463 then [||]
464 else (
465 if arch_sixtyfour
466 then (
467 if len > Sys.max_array_length
468 then raise_read_error ReadError.Array_too_long start_pos)
469 else if len > Sys.max_array_length / 2
470 then (
471 let maybe_float =
472 try
473 let el = bin_read_el dummy_float_buf ~pos_ref:(ref 0) in
474 Some el
475 with
476 | _ -> None
477 in
478 match maybe_float with
479 | None ->
480 if len > Sys.max_array_length
481 then raise_read_error ReadError.Array_too_long start_pos
482 | Some el ->
483 if Obj.tag (Obj.repr el) = Obj.double_tag || len > Sys.max_array_length
484 then raise_read_error ReadError.Array_too_long start_pos);
485 let first = bin_read_el buf ~pos_ref in
486 let res = Array.make len first in
487 for i = 1 to len - 1 do
488 let el = bin_read_el buf ~pos_ref in
489 Array.unsafe_set res i el
490 done;
491 res))
492 ;;
493
494 let bin_read_hashtbl bin_read_key bin_read_val buf ~pos_ref =
495 let len = (bin_read_nat0 buf ~pos_ref :> int) in
496 let htbl = Hashtbl.create len in
497 let read_kv_pair = bin_read_pair bin_read_key bin_read_val in
498 let els = bin_read_n_rev_list read_kv_pair buf ~pos_ref len in
499 copy_htbl_list htbl els
500 ;;
501
502 external buf_of_vec32 : vec32 -> buf = "%identity"
503 external buf_of_vec64 : vec64 -> buf = "%identity"
504 external buf_of_mat32 : mat32 -> buf = "%identity"
505 external buf_of_mat64 : mat64 -> buf = "%identity"
506
507 let bin_read_float32_vec buf ~pos_ref =
508 let len = (bin_read_nat0 buf ~pos_ref :> int) in
509 let size = len * 4 in
510 let pos = !pos_ref in
511 let next = pos + size in
512 check_next buf next;
513 let vec = Array1.create float32 fortran_layout len in
514 unsafe_blit_buf ~src:buf ~src_pos:pos ~dst:(buf_of_vec32 vec) ~dst_pos:0 ~len:size;
515 pos_ref := next;
516 vec
517 ;;
518
519 let bin_read_float64_vec buf ~pos_ref =
520 let len = (bin_read_nat0 buf ~pos_ref :> int) in
521 let size = len * 8 in
522 let pos = !pos_ref in
523 let next = pos + size in
524 check_next buf next;
525 let vec = Array1.create float64 fortran_layout len in
526 unsafe_blit_buf ~src:buf ~src_pos:pos ~dst:(buf_of_vec64 vec) ~dst_pos:0 ~len:size;
527 pos_ref := next;
528 vec
529 ;;
530
531 let bin_read_vec = bin_read_float64_vec
532
533 let bin_read_float32_mat buf ~pos_ref =
534 let len1 = (bin_read_nat0 buf ~pos_ref :> int) in
535 let len2 = (bin_read_nat0 buf ~pos_ref :> int) in
536 let size = len1 * len2 * 4 in
537 let pos = !pos_ref in
538 let next = pos + size in
539 check_next buf next;
540 let mat = Array2.create float32 fortran_layout len1 len2 in
541 unsafe_blit_buf ~src:buf ~src_pos:pos ~dst:(buf_of_mat32 mat) ~dst_pos:0 ~len:size;
542 pos_ref := next;
543 mat
544 ;;
545
546 let bin_read_float64_mat buf ~pos_ref =
547 let len1 = (bin_read_nat0 buf ~pos_ref :> int) in
548 let len2 = (bin_read_nat0 buf ~pos_ref :> int) in
549 let size = len1 * len2 * 8 in
550 let pos = !pos_ref in
551 let next = pos + size in
552 check_next buf next;
553 let mat = Array2.create float64 fortran_layout len1 len2 in
554 unsafe_blit_buf ~src:buf ~src_pos:pos ~dst:(buf_of_mat64 mat) ~dst_pos:0 ~len:size;
555 pos_ref := next;
556 mat
557 ;;
558
559 let bin_read_mat = bin_read_float64_mat
560
561 let bin_read_bigstring buf ~pos_ref =
562 let len = (bin_read_nat0 buf ~pos_ref :> int) in
563 let pos = !pos_ref in
564 let next = pos + len in
565 check_next buf next;
566 let str = create_buf len in
567 unsafe_blit_buf ~src:buf ~src_pos:pos ~dst:str ~dst_pos:0 ~len;
568 pos_ref := next;
569 str
570 ;;
571
572 let bin_read_variant_int buf ~pos_ref =
573 let pos = !pos_ref in
574 assert_pos pos;
575 let next = pos + 4 in
576 check_next buf next;
577 let n = unsafe_get32le buf pos in
578 (* [n] must contain an integer already encoded, i.e. [n = 2 * k + 1]. *)
579 if Int32.logand n 1l = 0l
580 then raise (Read_error (ReadError.Variant_tag, pos))
581 else (
582 (* We shift it by one bit to the right se we get back [2 * k + 1] in the end. *)
583 pos_ref := next;
584 Int32.to_int (Int32.shift_right n 1))
585 ;;
586
587 let bin_read_int_8bit buf ~pos_ref =
588 let pos = safe_get_pos buf pos_ref in
589 assert_pos pos;
590 pos_ref := pos + 1;
591 unsafe_get8 buf pos
592 ;;
593
594 let bin_read_int_16bit buf ~pos_ref =
595 let pos = !pos_ref in
596 assert_pos pos;
597 let next = pos + 2 in
598 check_next buf next;
599 pos_ref := next;
600 unsafe_get16le_unsigned buf pos
601 ;;
602
603 let bin_read_int_32bit buf ~pos_ref =
604 let pos = !pos_ref in
605 assert_pos pos;
606 let next = pos + 4 in
607 check_next buf next;
608 pos_ref := next;
609 let n = unsafe_get32le buf pos in
610 safe_int_of_int32 pos n
611 ;;
612
613 let bin_read_int_64bit buf ~pos_ref =
614 let pos = !pos_ref in
615 assert_pos pos;
616 let next = pos + 8 in
617 check_next buf next;
618 pos_ref := next;
619 let n = unsafe_get64le buf pos in
620 safe_int_of_int64 pos n
621 ;;
622
623 let bin_read_int64_bits buf ~pos_ref =
624 let pos = !pos_ref in
625 assert_pos pos;
626 let next = pos + 8 in
627 check_next buf next;
628 pos_ref := next;
629 unsafe_get64le buf pos
630 ;;
631
632 let bin_read_network16_int buf ~pos_ref =
633 let pos = !pos_ref in
634 assert_pos pos;
635 let next = pos + 2 in
636 check_next buf next;
637 pos_ref := next;
638 unsafe_get16be_unsigned buf pos
639 ;;
640
641 let bin_read_network32_int buf ~pos_ref =
642 let pos = !pos_ref in
643 assert_pos pos;
644 let next = pos + 4 in
645 check_next buf next;
646 pos_ref := next;
647 let n = unsafe_get32be buf pos in
648 safe_int_of_int32 pos n
649 ;;
650
651 let bin_read_network32_int32 buf ~pos_ref =
652 let pos = !pos_ref in
653 assert_pos pos;
654 let next = pos + 4 in
655 check_next buf next;
656 pos_ref := next;
657 unsafe_get32be buf pos
658 ;;
659
660 let bin_read_network64_int buf ~pos_ref =
661 let pos = !pos_ref in
662 assert_pos pos;
663 let next = pos + 8 in
664 check_next buf next;
665 pos_ref := next;
666 let n = unsafe_get64be buf pos in
667 safe_int_of_int64 pos n
668 ;;
669
670 let bin_read_network64_int64 buf ~pos_ref =
671 let pos = !pos_ref in
672 assert_pos pos;
673 let next = pos + 8 in
674 check_next buf next;
675 pos_ref := next;
676 unsafe_get64be buf pos
677 ;;
678
679 [%%if ocaml_version < (4, 07, 0)]
680
681 external unsafe_bytes_set32 : bytes -> int -> int32 -> unit = "%caml_string_set32u"
682 external unsafe_bytes_set64 : bytes -> int -> int64 -> unit = "%caml_string_set64u"
683
684 [%%else]
685
686 external unsafe_bytes_set32 : bytes -> int -> int32 -> unit = "%caml_bytes_set32u"
687 external unsafe_bytes_set64 : bytes -> int -> int64 -> unit = "%caml_bytes_set64u"
688
689 [%%endif]
690
691 let bin_read_md5 buf ~pos_ref =
692 let pos = !pos_ref in
693 assert_pos pos;
694 let next = pos + 16 in
695 check_next buf next;
696 pos_ref := next;
697 let res = Bytes.create 16 in
698 if arch_sixtyfour
699 then (
700 let a = unsafe_get64 buf pos in
701 let b = unsafe_get64 buf (pos + 8) in
702 unsafe_bytes_set64 res 0 a;
703 unsafe_bytes_set64 res 8 b)
704 else (
705 let a = unsafe_get32 buf pos in
706 let b = unsafe_get32 buf (pos + 4) in
707 let c = unsafe_get32 buf (pos + 8) in
708 let d = unsafe_get32 buf (pos + 12) in
709 unsafe_bytes_set32 res 0 a;
710 unsafe_bytes_set32 res 4 b;
711 unsafe_bytes_set32 res 8 c;
712 unsafe_bytes_set32 res 12 d);
713 Md5_lib.unsafe_of_binary (Bytes.unsafe_to_string res)
714 ;;