package core_extended

  1. Overview
  2. Docs
Extra components that are not as closely vetted or as stable as Core

Install

Dune Dependency

Authors

Maintainers

Sources

v0.17.0.tar.gz
sha256=17de5f7cf59818d757bb0625c55f0afc84509122645b7782fb522ac98c3be446

doc/src/core_extended.immediate/iobuf_accessors.ml.html

Source file iobuf_accessors.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
open Core
include Iobuf_accessors_intf

module For_cinaps = struct
  let[@cold] fail name arg value lower_bound upper_bound =
    failwithf
      "%s: %s = %d out of bounds [%d,%d]"
      name
      arg
      value
      lower_bound
      upper_bound
      ()
  ;;

  let len_str = "argument len"
  let pos_str = "argument pos"
  let length_str = "length of immediate string"

  let get_pos ~pos_opt ~full_length ~name =
    match pos_opt with
    | None -> 0
    | Some pos ->
      let pos = pos + 0 in
      if pos >= 0 && pos <= full_length then pos else fail name pos_str pos 0 full_length
  ;;

  let get_len ~len_opt ~pos ~full_length ~name =
    let upper_bound = full_length - pos in
    match len_opt with
    | None -> upper_bound
    | Some len ->
      let len = len + 0 in
      if len >= 0 && len <= upper_bound then len else fail name len_str len 0 upper_bound
  ;;

  let check_length x ~length upper_bound ~name =
    let len = length x in
    if len <= upper_bound then () else fail name length_str len 0 upper_bound
  ;;

  let checked_read_with_pos_and_len ?pos:pos_opt ?len:len_opt buf f name =
    let full_length = Iobuf.length buf in
    let pos = get_pos ~pos_opt ~full_length ~name in
    let len = get_len ~len_opt ~pos ~full_length ~name in
    f ~pos ~len buf
  ;;

  let checked_read_with_len ?len:len_opt buf f name =
    let full_length = Iobuf.length buf in
    let len = get_len ~len_opt ~pos:0 ~full_length ~name in
    f ~len buf
  ;;

  let checked_write_with_pos_and_len x ~length ?pos:pos_opt ?len:len_opt buf f name =
    let full_length = Iobuf.length buf in
    let pos = get_pos ~pos_opt ~full_length ~name in
    let len = get_len ~len_opt ~pos ~full_length ~name in
    check_length x ~length len ~name;
    f x ~pos ~len buf
  ;;

  let checked_write_with_len x ~length ?len:len_opt buf f name =
    let full_length = Iobuf.length buf in
    let len = get_len ~len_opt ~pos:0 ~full_length ~name in
    check_length x ~length len ~name;
    f x ~len buf
  ;;

  let checked_write_with_pos x ~length ?pos:pos_opt buf f name =
    let full_length = Iobuf.length buf in
    let pos = get_pos ~pos_opt ~full_length ~name in
    check_length x ~length (full_length - pos) ~name;
    f x ~pos buf
  ;;

  let checked_write x ~length buf f name =
    let full_length = Iobuf.length buf in
    check_length x ~length full_length ~name;
    f x buf
  ;;

  let bigstring_read_padding_and_get_unpadded_length ~padding ~pos ~padded_length buf =
    let last = ref (padded_length - 1) in
    while !last >= 0 && phys_equal padding (Bigstring.unsafe_get buf (pos + !last)) do
      decr last
    done;
    1 + !last
  ;;

  let read_padding_and_get_unpadded_length ~padding ~pos ~padded_length buf =
    bigstring_read_padding_and_get_unpadded_length
      ~padding
      ~padded_length
      ~pos:(Iobuf.Expert.lo buf + pos)
      (Iobuf.Expert.buf buf)
  ;;

  let bigstring_write_padding ~padding ~pos ~unpadded_length ~padded_length buf =
    for i = unpadded_length to padded_length - 1 do
      Bigstring.unsafe_set buf (pos + i) padding
    done
  ;;

  let write_padding ~padding ~pos ~unpadded_length ~padded_length buf =
    bigstring_write_padding
      ~padding
      ~pos:(Iobuf.Expert.lo buf + pos)
      ~unpadded_length
      ~padded_length
      (Iobuf.Expert.buf buf)
  ;;
end
OCaml

Innovation. Community. Security.