Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
1.0.0 (trunk):
* Remove IPv4/IPv6 types (now moved to `ocaml-ipaddr`).
* Improved ocamldoc for the interface.
* More conservative bounds checking in the length manipulation functions.
* Build C stubs with `-Wall`.

0.8.1 (2013-11-06):
* Trailing semicolons are allowed in cstruct field definitions.
* Buffer elements can be any primitive integer, not just `uint8`.
Expand Down
4 changes: 2 additions & 2 deletions _oasis
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
OASISFormat: 0.3
Name: cstruct
Version: 0.8.1
Version: 1.0.0
Synopsis: Manipulate external buffers as C-like structs
Authors: Anil Madhavapeddy, Richard Mortier, Thomas Gazagnaire, Pierre Chambart
License: ISC
Expand All @@ -24,7 +24,7 @@ Library cstruct
Modules: Cstruct
BuildDepends: bigarray, ocplib-endian, ocplib-endian.bigstring
CSources: cstruct_stubs.c
CCOpt: -O3
CCOpt: -O3 -Wall

Library async_cstruct
Build$: flag(async)
Expand Down
12 changes: 6 additions & 6 deletions lib/META
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# OASIS_START
# DO NOT EDIT (digest: 54e2ff3b4376263634c6e7c306cb1309)
version = "0.8.1"
# DO NOT EDIT (digest: 46bbe16c53f695df2a41384800ca1361)
version = "1.0.0"
description = "Manipulate external buffers as C-like structs"
requires = "bigarray ocplib-endian ocplib-endian.bigstring"
archive(byte) = "cstruct.cma"
Expand All @@ -9,7 +9,7 @@ archive(native) = "cstruct.cmxa"
archive(native, plugin) = "cstruct.cmxs"
exists_if = "cstruct.cma"
package "unix" (
version = "0.8.1"
version = "1.0.0"
description = "Manipulate external buffers as C-like structs"
requires = "cstruct unix"
archive(byte) = "unix_cstruct.cma"
Expand All @@ -20,7 +20,7 @@ package "unix" (
)

package "syntax" (
version = "0.8.1"
version = "1.0.0"
description = "Syntax extension for Cstruct"
requires = "camlp4"
archive(syntax, preprocessor) = "cstruct-syntax.cma"
Expand All @@ -29,7 +29,7 @@ package "syntax" (
)

package "lwt" (
version = "0.8.1"
version = "1.0.0"
description = "Manipulate external buffers as C-like structs"
requires = "cstruct lwt.unix"
archive(byte) = "lwt_cstruct.cma"
Expand All @@ -40,7 +40,7 @@ package "lwt" (
)

package "async" (
version = "0.8.1"
version = "1.0.0"
description = "Manipulate external buffers as C-like structs"
requires = "cstruct async threads"
archive(byte) = "async_cstruct.cma"
Expand Down
58 changes: 16 additions & 42 deletions lib/cstruct.ml
Original file line number Diff line number Diff line change
Expand Up @@ -58,44 +58,6 @@ let uint16 (i:int) : uint16 = min i 0xffff
type uint32 = int32
type uint64 = int64

type ipv4 = int32

let ipv4_to_string i =
let (&&&) x y = Int32.logand x y in
let (>>>) x y = Int32.shift_right_logical x y in
sprintf "%ld.%ld.%ld.%ld"
((i &&& 0x0_ff000000_l) >>> 24) ((i &&& 0x0_00ff0000_l) >>> 16)
((i &&& 0x0_0000ff00_l) >>> 8) ((i &&& 0x0_000000ff_l) )

let bytes_to_ipv4 bs =
let (|||) x y = Int32.logor x y in
let (<<<) x y = Int32.shift_left x y in
let a = Int32.of_int (byte_to_int bs.[0]) in
let b = Int32.of_int (byte_to_int bs.[1]) in
let c = Int32.of_int (byte_to_int bs.[2]) in
let d = Int32.of_int (byte_to_int bs.[3]) in
(a <<< 24) ||| (b <<< 16) ||| (c <<< 8) ||| d

type ipv6 = int64 * int64
let ipv6_to_string (hi, lo) =
let (&&&&) x y = Int64.logand x y in
let (>>>>) x y = Int64.shift_right_logical x y in
sprintf "%Lx:%Lx:%Lx:%Lx:%Lx:%Lx:%Lx:%Lx"
((hi >>>> 48) &&&& 0xffff_L) ((hi >>>> 32) &&&& 0xffff_L)
((hi >>>> 16) &&&& 0xffff_L) ( hi &&&& 0xffff_L)
((lo >>>> 48) &&&& 0xffff_L) ((lo >>>> 32) &&&& 0xffff_L)
((lo >>>> 16) &&&& 0xffff_L) ( lo &&&& 0xffff_L)

let bytes_to_ipv6 bs =
let (++++) x y = Int64.add x y in
let (<<<<) x y = Int64.shift_left x y in
let hihi = bytes_to_ipv4 (String.sub bs 0 4) in
let hilo = bytes_to_ipv4 (String.sub bs 4 4) in
let lohi = bytes_to_ipv4 (String.sub bs 8 4) in
let lolo = bytes_to_ipv4 (String.sub bs 12 4) in
((Int64.of_int32 hihi) <<<< 48) ++++ (Int64.of_int32 hilo),
((Int64.of_int32 lohi) <<<< 48) ++++ (Int64.of_int32 lolo)

let debug t =
let max_len = Bigarray.Array1.dim t.buffer in
let str = Printf.sprintf "t=[%d,%d](%d)" t.off t.len max_len in
Expand All @@ -106,16 +68,28 @@ let debug t =
str

let sub t off len =
{ t with off = t.off + off; len }
let off = t.off + off in
if not (check_bounds t (off+len)) then
raise (Invalid_argument "Cstruct.sub");
{ t with off; len }

let shift t off =
{ t with off = t.off + off; len = t.len - off }
let off = t.off + off in
let len = t.len - off in
if not (check_bounds t (off+len)) then
raise (Invalid_argument "Cstruct.shift");
{ t with off; len }

let set_len t len =
{ t with len = len }
if not (check_bounds t (t.off+len)) then
raise (Invalid_argument "Cstruct.set_len");
{ t with len }

let add_len t len =
{ t with len = t.len + len }
let len = t.len + len in
if not (check_bounds t (t.off+len)) then
raise (Invalid_argument "Cstruct.add_len");
{ t with len }

let invalid_arg fmt =
let b = Buffer.create 20 in (* for thread safety. *)
Expand Down
Loading