package gapi-ocaml

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file netstring_tstring.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
(* $Id$ *)

open Netsys_types

type _ tstring_kind =
  | String_kind : string tstring_kind
  | Bytes_kind : Bytes.t tstring_kind
  | Memory_kind : memory tstring_kind

type 't tstring_ops =
    { kind : 't tstring_kind option;
      length : 't -> int;
      get : 't -> int -> char;
      unsafe_get : 't -> int -> char;
      unsafe_get3 : 't -> int -> int; (* get 3 chars packed into one int *)
      copy : 't -> 't;
      string : 't -> string;
      bytes : 't -> Bytes.t;
      sub : 't -> int -> int -> 't;
      substring : 't -> int -> int -> string;
      subbytes : 't -> int -> int -> Bytes.t;
      subpoly : 'u . 'u tstring_kind -> 't -> int -> int -> 'u;
      blit_to_bytes : 't -> int -> Bytes.t -> int -> int -> unit;
      blit_to_memory : 't -> int -> memory -> int -> int -> unit;
      index_from : 't -> int -> char -> int;
      index_from3 : 't -> int -> int -> char -> char -> char -> int;
      rindex_from : 't -> int -> char -> int;
      rindex_from3 : 't -> int -> int -> char -> char -> char -> int;
    }

type tstring_ops_box =
  | Tstring_ops_box : 't tstring_kind * 't tstring_ops -> tstring_ops_box

type tstring_box =
  | Tstring_box : 't tstring_kind * 't tstring_ops * 't -> tstring_box

type tstring_polybox =
  | Tstring_polybox : 't tstring_ops * 't -> tstring_polybox
  (* Warning: you cannot match on the type 't here *)

let str_subpoly : type u . u tstring_kind -> string -> int -> int -> u =
  function
  | String_kind -> String.sub
  | Bytes_kind -> (fun s pos len ->
                     let b = Bytes.create len in
                     Bytes.blit_string s pos b 0 len;
                     b
                  )
  | Memory_kind -> (fun s pos len ->
                      let m =
                        Bigarray.Array1.create Bigarray.char Bigarray.c_layout
                                               len in
                      Netsys_mem.blit_string_to_memory s pos m 0 len;
                      m
                   )


let str_index_from3 s p n c1 c2 c3 =
  (* FIXME: implement in C *)
  let sn = String.length s in
  if n < 0 || p < 0 || p > sn-n then
    invalid_arg "index_from3";
  let lim = p+n in
  let p = ref p in
  while !p < lim &&
          (let c = String.unsafe_get s !p in c <> c1 && c <> c2 && c <> c3)
  do
    incr p
  done;
  if !p >= lim then raise Not_found;
  !p

let str_rindex_from3 s p n c1 c2 c3 =
  (* FIXME: implement in C *)
  let sn = String.length s in
  if n < 0 || p < -1 || p >= sn || n-1 > p then
    invalid_arg "rindex_from";
  let lim = p-n+1 in
  let p = ref p in
  while !p >= lim && 
           (let c = String.unsafe_get s !p in c <> c1 && c <> c2 && c <> c3)
  do
    decr p
  done;
  if !p < lim then raise Not_found;
  !p

let string_ops =
  { kind = Some String_kind;
    length = String.length;
    get = String.get;
    unsafe_get = String.unsafe_get;
    unsafe_get3 = (fun s k ->
                     let c0 = Char.code (String.unsafe_get s k) in
                     let c1 = Char.code (String.unsafe_get s (k+1)) in
                     let c2 = Char.code (String.unsafe_get s (k+2)) in
                     (c0 lsl 16) lor (c1 lsl 8) lor c2
                  );
    copy = STRING_COPY;   (* ... for the time being ... *)
    string = (fun s -> s);
    bytes = Bytes.of_string;
    sub = String.sub;
    substring = String.sub;
    subbytes = (fun s p l ->
                  let b = Bytes.create l in
                  Bytes.blit_string s p b 0 l;
                  b
               );
    subpoly = str_subpoly;
    blit_to_bytes = Bytes.blit_string;
    blit_to_memory = Netsys_mem.blit_string_to_memory;
    index_from = String.index_from;
    index_from3 = str_index_from3;
    rindex_from = String.rindex_from;
    rindex_from3 = str_rindex_from3;
  }

let bytes_index_from3 s p n c1 c2 c3 =
  str_index_from3 (Bytes.unsafe_to_string s) p n c1 c2 c3

let bytes_rindex_from3 s p n c1 c2 c3 =
  str_rindex_from3 (Bytes.unsafe_to_string s) p n c1 c2 c3

let bytes_subpoly : type u . u tstring_kind -> Bytes.t -> int -> int -> u =
  function
  | String_kind -> Bytes.sub_string
  | Bytes_kind -> Bytes.sub
  | Memory_kind -> (fun s pos len ->
                      let m =
                        Bigarray.Array1.create Bigarray.char Bigarray.c_layout
                                               len in
                      Netsys_mem.blit_bytes_to_memory s pos m 0 len;
                      m
                   )

let bytes_ops =
  { kind = Some Bytes_kind;
    length = Bytes.length;
    get = Bytes.get;
    unsafe_get = Bytes.unsafe_get;
    unsafe_get3 = (fun s k ->
                     let c0 = Char.code (Bytes.unsafe_get s k) in
                     let c1 = Char.code (Bytes.unsafe_get s (k+1)) in
                     let c2 = Char.code (Bytes.unsafe_get s (k+2)) in
                     (c0 lsl 16) lor (c1 lsl 8) lor c2
                  );
    copy = Bytes.copy;
    string = Bytes.to_string;
    bytes = (fun s -> s);
    sub = Bytes.sub;
    substring = Bytes.sub_string;
    subbytes = Bytes.sub;
    subpoly = bytes_subpoly;
    blit_to_bytes = Bytes.blit;
    blit_to_memory = Netsys_mem.blit_bytes_to_memory;
    index_from = Bytes.index_from;
    index_from3 = bytes_index_from3;
    rindex_from = Bytes.rindex_from;
    rindex_from3 = bytes_rindex_from3;
  }

let mem_index_from3 m p n c1 c2 c3 =
  (* FIXME: implement in C *)
  let sn = Bigarray.Array1.dim m in
  if n < 0 || p < 0 || p > sn-n then
    invalid_arg "index_from";
  let lim = p+n in
  let p = ref p in
  while
    !p < lim &&
      (let c = Bigarray.Array1.unsafe_get m !p in c <> c1 && c <> c2 && c <> c3)
  do
    incr p
  done;
  if !p >= lim then raise Not_found;
  !p

let mem_index_from m p c =
  let sn = Bigarray.Array1.dim m in
  mem_index_from3 m p (sn-p) c c c

let mem_rindex_from3 m p n c1 c2 c3 =
  (* FIXME: implement in C *)
  let sn = Bigarray.Array1.dim m in
  if n < 0 || p < -1 || p >= sn || n-1 > p then
    invalid_arg "rindex_from";
  let lim = p-n+1 in
  let p = ref p in
  while
    !p >= lim &&
      (let c = Bigarray.Array1.unsafe_get m !p in c <> c1 && c <> c2 && c <> c3)
  do
    decr p
  done;
  if !p < lim then raise Not_found;
  !p

let mem_rindex_from m p c =
  mem_rindex_from3 m p (p+1) c c c

let mem_sub m1 p len = 
  let m2 =
    Bigarray.Array1.create Bigarray.char Bigarray.c_layout len in
  Bigarray.Array1.blit
    (Bigarray.Array1.sub m1 p len)
    m2;
  m2

let mem_subpoly : type u . u tstring_kind -> memory -> int -> int -> u =
  function
  | String_kind -> (fun m pos len ->
                     Netsys_mem.string_of_memory
                       (Bigarray.Array1.sub m pos len)
                   )
  | Bytes_kind -> (fun m pos len ->
                     Netsys_mem.bytes_of_memory
                       (Bigarray.Array1.sub m pos len)
                  )
  | Memory_kind -> mem_sub

let memory_ops =
  { kind = Some Memory_kind;
    length = (Bigarray.Array1.dim : memory -> int);
    get = Bigarray.Array1.get;
    unsafe_get = Bigarray.Array1.unsafe_get;
    unsafe_get3 = (fun s k ->
                     let c0 = Char.code (Bigarray.Array1.unsafe_get s k) in
                     let c1 = Char.code (Bigarray.Array1.unsafe_get s (k+1)) in
                     let c2 = Char.code (Bigarray.Array1.unsafe_get s (k+2)) in
                     (c0 lsl 16) lor (c1 lsl 8) lor c2
                  );
    copy = (fun m1 ->
              let n = Bigarray.Array1.dim m1 in
              mem_sub m1 0 n
           );
    string = Netsys_mem.string_of_memory;
    bytes = Netsys_mem.bytes_of_memory;
    sub = mem_sub;
    substring = (fun m p l ->
                   let m1 = Bigarray.Array1.sub m p l in
                   Netsys_mem.string_of_memory m1
                );
    subbytes = (fun m p l ->
                  let m1 = Bigarray.Array1.sub m p l in
                  Netsys_mem.bytes_of_memory m1
               );
    subpoly = mem_subpoly;
    blit_to_bytes = Netsys_mem.blit_memory_to_bytes;
    blit_to_memory = (fun m1 p1 m2 p2 l ->
                        let sub1 = Bigarray.Array1.sub m1 p1 l in
                        let sub2 = Bigarray.Array1.sub m2 p2 l in
                        Bigarray.Array1.blit sub1 sub2
                     );
    index_from = mem_index_from;
    index_from3 = mem_index_from3;
    rindex_from = mem_rindex_from;
    rindex_from3 = mem_rindex_from3;
  }

let ops_of_tstring =
  function
  | `String _ ->
      Tstring_ops_box(String_kind, string_ops)
  | `Bytes _ ->
      Tstring_ops_box(Bytes_kind, bytes_ops)
  | `Memory _ ->
      Tstring_ops_box(Memory_kind, memory_ops)


type 'a with_fun =
    { with_fun : 's . 's tstring_ops -> 's -> 'a }

let with_tstring : 'a with_fun -> tstring -> 'a =
  fun f ->
    function
    | `String s ->
        f.with_fun string_ops s
    | `Bytes s ->
        f.with_fun bytes_ops s
    | `Memory s ->
        f.with_fun memory_ops s


let length_tstring ts =
  with_tstring
    { with_fun = (fun ops s -> ops.length s) }
    ts

let polymorph_string_transformation
  : type s t . (string->string) -> s tstring_ops -> t tstring_kind -> s -> t =
  (fun f ops out_kind s ->
     let s' = f (ops.string s) in
     match out_kind with
       | String_kind ->
           s'
       | Bytes_kind ->
           Bytes.of_string s'
       | Memory_kind ->
           Netsys_mem.memory_of_string s'
  )


let tstring_of_tbuffer =
  function
  | `Bytes s -> `Bytes s
  | `String s -> `Bytes s
  | `Memory m -> `Memory m
OCaml

Innovation. Community. Security.