package charrua-core

  1. Overview
  2. Docs

Source file util.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
(*
 * Copyright (c) 2015 Christiano F. Haesbaert <haesbaert@haesbaert.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *)

let find_map f t =
  let rec loop = function
    | [] -> None
    | x :: l ->
      match f x with
      | None -> loop l
      | Some _ as r -> r
  in
  loop t

let filter_map f l = List.rev @@
  List.fold_left (fun a v -> match f v with Some v' -> v'::a | None -> a) [] l

let finalize f g =
  try
    let x = f () in
    g ();
    x
  with exn ->
    g ();
    raise exn

let string_extend_if_le s m =
  let n = String.length s in
  if n > m then
    invalid_arg ("string is too damn big: " ^ (string_of_int n));
  s ^ String.make (m - n) (Char.chr 0)

let string_nul b =
  let len = String.length b in
  let rec loop i =
    if i = len then
      true
    else if (String.get b i) <> (Char.chr 0) then
      false
    else
      loop (succ i)
  in
  loop 0

let cstruct_copy_normalized f buf =
  let b = f buf in
  if string_nul b then "" else b

let some_or_default x d = match x with Some x -> x | None -> d
let some_or_f x f = match x with Some x -> x | None -> f ()
let some_or_invalid x s = some_or_f x (fun () -> invalid_arg s)
let some_or_fail x s = some_or_f x (fun () -> failwith s)
let find_some f = try Some (f ()) with Not_found -> None
let true_if_some x = match x with Some _ -> true | None -> false

let cons v tl = v :: tl
let cons_if p v tl = if p then v :: tl else tl
let cons_if_some v tl = match v with Some v -> v :: tl | None -> tl
let cons_if_some_f v fnr tl = match v with Some x -> fnr x :: tl | None -> tl

let guard p e = if p then Result.Ok () else Result.Error e

let addr_in_range addr range =
  let low_ip, high_ip = range in
  let low_32 = Ipaddr.V4.to_int32 low_ip in
  let high_32 = Ipaddr.V4.to_int32 high_ip in
  let addr_32 = Ipaddr.V4.to_int32 addr in
  addr_32 >= low_32 && addr_32 <= high_32
OCaml

Innovation. Community. Security.