Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into cap
Browse files Browse the repository at this point in the history
  • Loading branch information
dinosaure committed Sep 15, 2020
2 parents 3350d41 + 1342786 commit be3efd0
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 10 deletions.
4 changes: 3 additions & 1 deletion lib/cstruct.mli
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,9 @@ val stop_pos : t -> int
(** [stop_pos cs] is [cs]'s stop position in the base {!Cstruct.buffer}. *)

val length : t -> int
(** [length cs] is the number of bytes in [cs]. *)
(** Returns the length of the current cstruct view. Note that this
length is potentially smaller than the actual size of the underlying
buffer, as the [sub] or [set_len] functions can construct a smaller view. *)

val head : ?rev:bool -> t -> char option
(** [head cs] is [Some (get cs h)] with [h = 0] if [rev = false] (default) or [h
Expand Down
20 changes: 17 additions & 3 deletions lib/cstruct_cap.ml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,23 @@ external wo : 'a wr t -> wo t = "%identity"
let of_string = Cstruct.of_string ?allocator:None
let of_bytes = Cstruct.of_bytes ?allocator:None

let to_string ?(off= 0) ?len t =
let len = match len with
| Some len -> len
| None -> Cstruct.length t - off in
Cstruct.copy t off len

let to_bytes ?(off= 0) ?len t =
let len = match len with
| Some len -> len
| None -> Cstruct.length t - off in
(* XXX(dinosaure): this is safe when [copy] allocates itself [bytes]
and uses [Bytes.unsafe_to_string]. *)
Bytes.unsafe_of_string (Cstruct.copy t off len)

let pp ppf t = Cstruct.hexdump_pp ppf t

let length = Cstruct.len
let length = Cstruct.length

let blit src ~src_off dst ~dst_off ~len =
Cstruct.blit src src_off dst dst_off len
Expand All @@ -61,9 +75,9 @@ let unsafe_to_bigarray = Cstruct.to_bigarray
let concat vss =
let res = create_unsafe (Cstruct.sum_lengths ~caller:"Cstruct.Cap.concat" vss) in
let go off v =
let len = Cstruct.len v in
let len = Cstruct.length v in
Cstruct.blit v 0 res off len ;
off + len in
let len = List.fold_left go 0 vss in
assert (len = Cstruct.len res) ;
assert (len = Cstruct.length res) ;
res
2 changes: 1 addition & 1 deletion lib/cstruct_sexp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ let t_of_sexp = function
| sexp -> Conv.of_sexp_error "Cstruct.t_of_sexp: atom needed" sexp

let sexp_of_t t =
let n = Cstruct.len t in
let n = Cstruct.length t in
let str = Bytes.create n in
Cstruct.blit_to_bytes t 0 str 0 n ;
(* The following call is safe, since str is not visible elsewhere. *)
Expand Down
4 changes: 2 additions & 2 deletions lib_test/tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ let concat_samples () =

let concat_random ~n () =
let rec explode cs =
let n = Cstruct.len cs in
let n = Cstruct.length cs in
if n = 0 then [] else
let k = Random.int (n + 1) in
Cstruct.sub cs 0 k :: explode (Cstruct.shift cs k) in
Expand Down Expand Up @@ -110,7 +110,7 @@ let check_alignment alignment () =
let buf = Cstruct.create (expected * alignment) in
(* How many aligned offsets are there in this buffer? *)
let actual = ref 0 in
for i = 0 to Cstruct.len buf - 1 do
for i = 0 to Cstruct.length buf - 1 do
if Cstruct.(check_alignment (shift buf i) alignment) then incr actual
done;
Alcotest.(check int) "alignement" expected !actual
Expand Down
2 changes: 1 addition & 1 deletion lwt/lwt_cstruct.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ let complete op t =
let rec loop t =
op t >>= fun n ->
let t = Cstruct.shift t n in
if Cstruct.len t = 0
if Cstruct.length t = 0
then return ()
else if n = 0
then fail End_of_file
Expand Down
4 changes: 2 additions & 2 deletions ppx_test/pcap.ml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ let print_packet p =
let window = get_tcpv4_window tcp in
printf "tcpv4 port %d->%d seq %lu ack %lu win %d off %d flags %s opt %d fin %b syn %b payload_len=%d\n"
src_port dst_port seqnum
acknum window off flags options fin syn (Cstruct.len payload);
acknum window off flags options fin syn (Cstruct.length payload);
()
end
|_ -> printf "unknown ip proto %d\n" proto
Expand Down Expand Up @@ -150,7 +150,7 @@ let parse () =
printf "start parse\n%!";
let fd = Unix.(openfile "http.cap" [O_RDONLY] 0) in
let t = Unix_cstruct.of_fd fd in
printf "total pcap file length %d\n%!" (Cstruct.len t);
printf "total pcap file length %d\n%!" (Cstruct.length t);

let header, body = Cstruct.split t sizeof_pcap_header in
print_pcap_header header;
Expand Down

0 comments on commit be3efd0

Please sign in to comment.