package binsec

  1. Overview
  2. Docs

doc/src/binsec.base/size.ml.html

Source file size.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
(**************************************************************************)
(*  This file is part of BINSEC.                                          *)
(*                                                                        *)
(*  Copyright (C) 2016-2026                                               *)
(*    CEA (Commissariat à l'énergie atomique et aux énergies              *)
(*         alternatives)                                                  *)
(*                                                                        *)
(*  you can redistribute it and/or modify it under the terms of the GNU   *)
(*  Lesser General Public License as published by the Free Software       *)
(*  Foundation, version 2.1.                                              *)
(*                                                                        *)
(*  It is distributed in the hope that it will be useful,                 *)
(*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *)
(*  GNU Lesser General Public License for more details.                   *)
(*                                                                        *)
(*  See the GNU Lesser General Public License version 2.1                 *)
(*  for more details (enclosed in the file licenses/LGPLv2.1).            *)
(*                                                                        *)
(**************************************************************************)

module type Size = sig
  type t = private Natural.t

  val create : int -> t
  val of_string : string -> t
  val of_int32 : int32 -> t
  val to_int : t -> int
  val eq : t -> t -> bool
  val pp : Format.formatter -> t -> unit
  val pp_hex : Format.formatter -> t -> unit
  val add : t -> t -> t
  val sub : t -> t -> t
  val div : t -> t -> t
  val mul : t -> t -> t
  val pred : t -> t
  val is_zero : t -> bool
end

module CommonSize = struct
  include Natural

  let pp_hex ppf t = Format.fprintf ppf "%x" (to_int t)
  let of_string s = int_of_string s |> create
  let of_int32 n = Int32.to_int n |> create
end

module Bit = struct
  include CommonSize

  let bits1 = create 1
  let bits2 = create 2
  let bits4 = create 4
  let bits8 = create 8
  let bits16 = create 16
  let bits32 = create 32
  let bits64 = create 64
  let bits80 = create 80
  let bits128 = create 128
  let bits256 = create 256
  let bits512 = create 512
end

module Byte = struct
  include CommonSize

  let bytesize = Natural.create 8
  let to_bitsize n = mul n bytesize

  let of_bitsize n =
    assert (to_int n mod (bytesize :> int) = 0);
    div n bytesize

  let unsafe_of_bits n = of_bitsize (create n)
  let one = create 1
  let two = create 2
  let four = create 4
  let eight = create 8
  let fifteen = create 15
  let sixteen = create 16
  let thirtytwo = create 32
end