Skip to content

Commit

Permalink
* replace generic but used only in a specific way 'to_int' with
Browse files Browse the repository at this point in the history
'pkt_len_unsafe'
* comment 'junk_pkt'
  • Loading branch information
ulugbekna committed Nov 27, 2020
1 parent de07146 commit dab0824
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
35 changes: 16 additions & 19 deletions src/not-so-smart/decoder.ml
Original file line number Diff line number Diff line change
Expand Up @@ -124,34 +124,31 @@ let at_least_one_line decoder =
done;
!pos < decoder.max && !chr = '\n' && !has_cr

let digit = function
| 'a' .. 'f' as chr -> 10 + Char.code chr - Char.code 'a'
| 'A' .. 'F' as chr -> 10 + Char.code chr - Char.code 'A'
| '0' .. '9' as chr -> Char.code chr - Char.code '0'
| _ -> invalid_arg "invalid digit"

let to_int ~base ~off ~len buf =
let code = ref 0 in
for i = 0 to len - 1 do
let v = digit (Bytes.get buf (off + i)) in
assert (v < base);
code := (base * !code) + v
done;
!code
(** reads off 4 bytes from [decoder.buffer] starting at [decoder.pos] and interprets read
bytes as hex and converts to int.
Why unsafe:
@raise Invalid_argument if there are no 4 bytes to read, i.e.,
[decoder.max - decoder.pos < 4] *)
let pkt_len_unsafe (decoder : decoder) =
let hex = Bytes.of_string "0x0000" in
Bytes.blit decoder.buffer decoder.pos hex 2 4;
Bytes.to_string hex |> int_of_string

(* no header *)

let at_least_one_pkt decoder =
let len = decoder.max - decoder.pos in
if len >= 4 then
let pkt_len = to_int ~base:16 ~off:decoder.pos ~len:4 decoder.buffer in
len - pkt_len >= 0
let pkt_len = pkt_len_unsafe decoder in
len >= pkt_len
else false

(* no header *)

let get_pkt_len decoder =
let len = decoder.max - decoder.pos in
if len >= 4 then
let pkt_len = to_int ~base:16 ~off:decoder.pos ~len:4 decoder.buffer in
let pkt_len = pkt_len_unsafe decoder in
Some pkt_len
else None

Expand Down Expand Up @@ -258,12 +255,12 @@ let prompt :
go decoder.max

let peek_pkt decoder =
let len = to_int ~base:16 ~off:decoder.pos ~len:4 decoder.buffer in
let len = pkt_len_unsafe decoder in
if len >= 4 then decoder.buffer, decoder.pos + 4, len - 4
else decoder.buffer, decoder.pos + 4, 0

let junk_pkt decoder =
let len = to_int ~base:16 ~off:decoder.pos ~len:4 decoder.buffer in
let len = pkt_len_unsafe decoder in
if len < 4 then decoder.pos <- decoder.pos + 4
else decoder.pos <- decoder.pos + len

Expand Down
5 changes: 5 additions & 0 deletions src/not-so-smart/decoder.mli
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,9 @@ val prompt :
val peek_while_eol : decoder -> bytes * int * int
val peek_while_eol_or_space : decoder -> bytes * int * int
val peek_pkt : decoder -> bytes * int * int

val junk_pkt : decoder -> unit
(** skip [max(4, pkt_len)], where [pkt_len] is the length of the pkt line starting at
[decoder.pos] encoded according to pkt line encoding (as hex in the first 4 bytes);
@raise Invalid_argument if there aren't 4 bytes representing the length *)

0 comments on commit dab0824

Please sign in to comment.