Merge pull request #22 from yomimono/master

update to ipaddr 4.0.0 & decompress 0.9.0
This commit is contained in:
Hannes Mehnert 2019-07-27 12:46:01 +02:00 committed by GitHub
commit 28959a66bf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 14 deletions

View file

@ -9,7 +9,7 @@ depends: [
"ocaml" {>= "4.05.0"}
"dune" {build}
"lwt" {>= "3.0.0"}
"ipaddr" {>= "2.9.0"}
"ipaddr" {>= "4.0.0"}
"hex"
"cstruct"
"logs"
@ -24,7 +24,7 @@ depends: [
"nocrypto"
"asn1-combinators" {>= "0.2.0"}
"duration"
"decompress" {>= "0.8.1"}
"decompress" {>= "0.9.0"}
"checkseum"
]

View file

@ -24,8 +24,8 @@ let projections_of asn =
(decode_strict c, Asn.encode c)
let ipv4 =
let f cs = Ipaddr.V4.of_bytes_exn (Cstruct.to_string cs)
and g ip = Cstruct.of_string (Ipaddr.V4.to_bytes ip)
let f cs = Ipaddr.V4.of_octets_exn (Cstruct.to_string cs)
and g ip = Cstruct.of_string (Ipaddr.V4.to_octets ip)
in
Asn.S.map f g Asn.S.octet_string

View file

@ -1,7 +1,8 @@
(* copied n 2018-03-18 from github.com:mirage/decompress.git (bin/easy.ml)
(MIT licensed) at fa1551b19165503fc77da6da99411fa59b6a7f6a by Hannes Mehnert
*)
(* edited to reflect later API (v0.9.0) changes on 2019-07-16 *)
module Stdlib_buffer = Buffer
open Decompress
(* Keep in your mind, this is an easy example of Decompress but not efficient.
@ -22,7 +23,7 @@ let compress ?(level = 4) data =
(* We need to allocate an output buffer, is like you can. it's depends your
capabilities of your writing. *)
let pos = ref 0 in
let res = Buffer.create (String.length data) in
let res = Stdlib_buffer.create (String.length data) in
(* The buffer is not a good idea. In fact, we can have a memory problem with
that (like if the output is too big). You need to keep in your mind that
is insecure to let a buffer to grow automatically (an attacker can use
@ -66,13 +67,13 @@ let compress ?(level = 4) data =
pos := !pos + n ;
n )
(fun output_buffer len ->
Buffer.add_subbytes res output_buffer 0 len ;
Stdlib_buffer.add_subbytes res output_buffer 0 len ;
0xFFFF )
(Zlib_deflate.default ~witness:B.bytes level)
(Zlib_deflate.default ~witness:Buffer.bytes level)
(* We can specify the level of the compression, see the documentation to know
what we use for each level. The default is 4. *)
|> function
| Ok _ -> Buffer.contents res
| Ok _ -> Stdlib_buffer.contents res
| Error e ->
Logs.err (fun m -> m "error %a while compressing" Zlib_deflate.pp_error e) ;
invalid_arg "cannot compress"
@ -83,7 +84,7 @@ let uncompress data =
your reading. *)
let output_buffer = Bytes.create 0xFFFF in
(* Same as [compress]. *)
let window = Window.create ~witness:B.bytes in
let window = Window.create ~crc:Window.adler32 ~witness:Buffer.bytes in
(* We allocate a window. We let the user to do that to reuse the window if
it's needed. In fact, the window is a big buffer ([size = (1 << 15)]) and
allocate this buffer costs.
@ -91,7 +92,7 @@ let uncompress data =
So in this case, we decompress only one time but if you want to decompress
some flows, you can reuse this window after a [Window.reset]. *)
let pos = ref 0 in
let res = Buffer.create (String.length data) in
let res = Stdlib_buffer.create (String.length data) in
Zlib_inflate.bytes input_buffer output_buffer
(* Same logic as [compress]. *)
(fun input_buffer ->
@ -100,11 +101,11 @@ let uncompress data =
pos := !pos + n ;
n )
(fun output_buffer len ->
Buffer.add_subbytes res output_buffer 0 len ;
Stdlib_buffer.add_subbytes res output_buffer 0 len ;
0xFFFF )
(Zlib_inflate.default ~witness:B.bytes window)
(Zlib_inflate.default ~witness:Buffer.bytes window)
|> function
| Ok _ -> Ok (Buffer.contents res)
| Ok _ -> Ok (Stdlib_buffer.contents res)
| Error exn ->
Logs.err (fun m -> m "error %a while uncompressing" Zlib_inflate.pp_error exn) ;
Error ()