-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmBytes.ml
More file actions
101 lines (81 loc) · 3.22 KB
/
Copy pathmBytes.ml
File metadata and controls
101 lines (81 loc) · 3.22 KB
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
(**************************************************************************)
(* *)
(* Copyright (c) 2014 - 2016. *)
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
(* *)
(* All rights reserved. No warranty, explicit or implicit, provided. *)
(* *)
(**************************************************************************)
open Bigarray
type t = (char, int8_unsigned_elt, c_layout) Array1.t
let create sz = Array1.create char c_layout sz
let length = Array1.dim
let sub = Array1.sub
let shift ba off = sub ba off (length ba - off)
let blit src srcoff dst dstoff len =
Array1.blit (sub src srcoff len) (sub dst dstoff len)
let copy ba =
let ba' = create (Array1.dim ba) in
Array1.blit ba ba';
ba'
(** Adapted from ocaml-cstruct. *)
external unsafe_blit_string_to_bigstring
: string -> int -> t -> int -> int -> unit
= "caml_blit_string_to_bigstring" [@@noalloc]
external unsafe_blit_bigstring_to_bytes
: t -> int -> bytes -> int -> int -> unit
= "caml_blit_bigstring_to_string" [@@noalloc]
(** HACK: force Cstruct at link which provides the previous primitives. *)
let dummy = Cstruct.byte_to_int
let invalid_bounds j l =
invalid_arg (Printf.sprintf "invalid bounds (index %d, length %d)" j l)
let blit_from_string src srcoff dst dstoff len =
if len < 0 || srcoff < 0 || dstoff < 0 || String.length src - srcoff < len then
raise (Invalid_argument (invalid_bounds srcoff len));
if length dst - dstoff < len then
raise (Invalid_argument (invalid_bounds dstoff len));
unsafe_blit_string_to_bigstring src srcoff dst dstoff len
let blit_to_bytes src srcoff dst dstoff len =
if len < 0 || srcoff < 0 || dstoff < 0 || length src - srcoff < len then
raise (Invalid_argument (invalid_bounds srcoff len));
if Bytes.length dst - dstoff < len then
raise (Invalid_argument (invalid_bounds dstoff len));
unsafe_blit_bigstring_to_bytes src srcoff dst dstoff len
let to_string buf =
let sz = length buf in
let s = Bytes.create sz in
unsafe_blit_bigstring_to_bytes buf 0 s 0 sz;
Bytes.unsafe_to_string s
let of_string buf =
let buflen = String.length buf in
let c = create buflen in
unsafe_blit_string_to_bigstring buf 0 c 0 buflen;
c
let substring src srcoff len =
if len < 0 || srcoff < 0 || length src - srcoff < len then
raise (Invalid_argument (invalid_bounds srcoff len));
let s = Bytes.create len in
unsafe_blit_bigstring_to_bytes src srcoff s 0 len;
Bytes.unsafe_to_string s
include EndianBigstring.BigEndian
let of_float f =
let buf = create 8 in
set_float buf 0 f;
buf
module LE = struct
include EndianBigstring.LittleEndian
end
let (=) = ((=) : t -> t -> bool)
let (<>) = ((<>) : t -> t -> bool)
let (<) = ((<) : t -> t -> bool)
let (<=) = ((<=) : t -> t -> bool)
let (>=) = ((>=) : t -> t -> bool)
let (>) = ((>) : t -> t -> bool)
let compare = Pervasives.compare
let concat b1 b2 =
let l1 = length b1 in
let l2 = length b2 in
let b = create (l1 + l2) in
blit b1 0 b 0 l1 ;
blit b2 0 b l1 l2 ;
b