Skip to content

Commit

Permalink
Merge pull request #98 from avsm/master
Browse files Browse the repository at this point in the history
Release 2.0.0
  • Loading branch information
avsm committed Apr 27, 2016
2 parents 3d2722c + 47806a4 commit daafda7
Show file tree
Hide file tree
Showing 30 changed files with 1,001 additions and 1,247 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ setup.log
*.swp
_build/
dist/
ppx/ppx_cstruct.ml
*.docdir
*.byte
*.native
Expand Down
14 changes: 8 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ install: wget https://raw.githubusercontent.com/ocaml/ocaml-travisci-skeleton/ma
script: bash -ex ./.travis-docker.sh
env:
global:
- PACKAGE="cstruct"
- DEPOPTS="camlp4 ppx_tools async lwt"
- PACKAGE="cstruct.9999"
- DEPOPTS="ppx_tools async lwt"
- REVDEPS="tls dns irmin tcpip"
matrix:
- DISTRO="debian-stable" OCAML_VERSION="4.01.0" DEPOPTS="camlp4 lwt"
- DISTRO="debian-testing" OCAML_VERSION="4.02.3"
- DISTRO="debian-stable" OCAML_VERSION="4.02.3"
- DISTRO="ubuntu-14.04" OCAML_VERSION="4.02.3"
- DISTRO="ubuntu-15.10" OCAML_VERSION="4.02.3"
- DISTRO="ubuntu-16.04" OCAML_VERSION="4.02.3"
- DISTRO="centos-7" OCAML_VERSION="4.02.3"
- DISTRO="centos-6" OCAML_VERSION="4.02.3"
- DISTRO="fedora-23" OCAML_VERSION="4.02.3"
- DISTRO="alpine-3.3" OCAML_VERSION="4.03.0"
- DISTRO="debian-testing" OCAML_VERSION="4.03.0"
- DISTRO="fedora-23" OCAML_VERSION="4.03.0"

6 changes: 6 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
2.0.0 (2016-04-26):
* Remove camlp4 extension as it is no longer maintained (#95).
* Add support for OCaml 4.03 in the PPX extension (#96).
* Minimum supported OCaml version for the library is now 4.02.3.
* Fix parsing of high int32 `@@enum` values.

1.9.0 (2016-02-19):
* Add support for a ppx-based extension that uses the extension point
support in OCaml 4.02 and higher to generate Cstruct and Cenum
Expand Down
4 changes: 0 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ configure:

# OASIS_STOP

test: setup.data build test.sh
$(SETUP) -test $(TESTFLAGS)
./test.sh

JS_DIR ?= $(shell ocamlfind query cstruct)

.PHONY: js-install js-uninstall
Expand Down
160 changes: 5 additions & 155 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ Cstruct is a library and syntax extension to make it easier to access C-like
structures directly from OCaml. It supports both reading and writing to these
structures, and they are accessed via the `Bigarray` module.

There are two supported syntax extensions: the
This library depends on OCaml version 4.02.0 and later, since it provides a
[ppx](http://whitequark.org/blog/2014/04/16/a-guide-to-extension-points-in-ocaml/)
extension point supported from OCaml 4.02.0 onwards, and the older
[camlp4](http://caml.inria.fr/pub/docs/manual-camlp4/manual002.html) extension.
extension point. The old
[camlp4](http://caml.inria.fr/pub/docs/manual-camlp4/manual002.html)
syntax extension is nolonger available; the last version which contained it
was v1.9.0.

## PPX

Expand Down Expand Up @@ -169,156 +171,4 @@ The representation of the Sexp is the string representation of the enum.

Please see the `ppx_test/` directory for more in-depth examples.

## Camlp4

The Camlp4 subpackage is activated by using the `cstruct.syntax` ocamlfind
package. An example pcap description is:

```
cstruct pcap_header {
uint32_t magic_number; (* magic number *)
uint16_t version_major; (* major version number *)
uint16_t version_minor; (* minor version number *)
uint32_t thiszone; (* GMT to local correction *)
uint32_t sigfigs; (* accuracy of timestamps *)
uint32_t snaplen; (* max length of captured packets, in octets *)
uint32_t network (* data link type *)
} as little_endian
cstruct pcap_packet {
uint32_t ts_sec; (* timestamp seconds *)
uint32_t ts_usec; (* timestamp microseconds *)
uint32_t incl_len; (* number of octets of packet saved in file *)
uint32_t orig_len (* actual length of packet *)
} as little_endian
cstruct ethernet {
uint8_t dst[6];
uint8_t src[6];
uint16_t ethertype
} as big_endian
cstruct ipv4 {
uint8_t hlen_version;
uint8_t tos;
uint16_t len;
uint16_t id;
uint16_t off;
uint8_t ttl;
uint8_t proto;
uint16_t csum;
uint8_t src[4];
uint8_t dst[4]
} as big_endian
```

This auto-generates generates functions of the form below in the `ml` file:

```
let sizeof_pcap_packet = 16
let get_pcap_packet_ts_sec v = Cstruct.LE.get_uint32 v 0
let set_pcap_packet_ts_sec v x = Cstruct.LE.set_uint32 v 0 x
let get_pcap_packet_ts_usec v = Cstruct.LE.get_uint32 v 4
let set_pcap_packet_ts_usec v x = Cstruct.LE.set_uint32 v 4 x
let get_pcap_packet_incl_len v = Cstruct.LE.get_uint32 v 8
let set_pcap_packet_incl_len v x = Cstruct.LE.set_uint32 v 8 x
let get_pcap_packet_orig_len v = Cstruct.LE.get_uint32 v 12
let set_pcap_packet_orig_len v x = Cstruct.LE.set_uint32 v 12 x
let sizeof_ethernet = 14
let get_ethernet_dst src = Cstruct.sub src 0 6
let copy_ethernet_dst src = Cstruct.copy src 0 6
let set_ethernet_dst src srcoff dst =
Cstruct.blit_from_string src srcoff dst 0 6
let blit_ethernet_dst src srcoff dst = Cstruct.blit src srcoff dst 0 6
let get_ethernet_src src = Cstruct.sub src 6 6
let copy_ethernet_src src = Cstruct.copy src 6 6
let set_ethernet_src src srcoff dst =
Cstruct.blit_from_string src srcoff dst 6 6
let blit_ethernet_src src srcoff dst = Cstruct.blit src srcoff dst 6 6
let get_ethernet_ethertype v = Cstruct.BE.get_uint16 v 12
let set_ethernet_ethertype v x = Cstruct.BE.set_uint16 v 12 x
```

The `mli` file will have signatures of this form:

```
val sizeof_pcap_packet : int
val get_pcap_packet_ts_sec : Cstruct.t -> Cstruct.uint32
val set_pcap_packet_ts_sec : Cstruct.t -> Cstruct.uint32 -> unit
val get_pcap_packet_ts_usec : Cstruct.t -> Cstruct.uint32
val set_pcap_packet_ts_usec : Cstruct.t -> Cstruct.uint32 -> unit
val get_pcap_packet_incl_len : Cstruct.t -> Cstruct.uint32
val set_pcap_packet_incl_len : Cstruct.t -> Cstruct.uint32 -> unit
val get_pcap_packet_orig_len : Cstruct.t -> Cstruct.uint32
val set_pcap_packet_orig_len : Cstruct.t -> Cstruct.uint32 -> unit
val hexdump_pcap_packet_to_buffer : Buffer.t -> pcap_packet -> unit
val hexdump_pcap_packet : Cstruct.t -> unit
val sizeof_ethernet : int
val get_ethernet_dst : Cstruct.t -> Cstruct.t
val copy_ethernet_dst : Cstruct.t -> string
val set_ethernet_dst : string -> int -> Cstruct.t -> unit
val blit_ethernet_dst : Cstruct.t -> int -> Cstruct.t -> unit
val get_ethernet_src : Cstruct.t -> Cstruct.t
val copy_ethernet_src : Cstruct.t -> string
val set_ethernet_src : string -> int -> Cstruct.t -> unit
val blit_ethernet_src : Cstruct.t -> int -> Cstruct.t -> unit
val get_ethernet_ethertype : Cstruct.t -> Cstruct.uint16
val set_ethernet_ethertype : Cstruct.t -> Cstruct.uint16 -> unit
val hexdump_ethernet_to_buffer : Buffer.t -> Cstruct.t -> unit
val hexdump_ethernet : Cstruct.t -> unit
```

The `hexdump` functions above are convenient pretty-printing functions
to help you debug, and aren't intended to be high performance.

You can also declare C-like enums:

```
cenum foo32 {
ONE32;
TWO32 = 0xfffffffel;
THREE32
} as uint32_t
cenum bar16 {
ONE = 1;
TWO;
FOUR = 4;
FIVE
} as uint16_t
```

This generates signatures of the form:

```
type foo32 = | ONE32 | TWO32 | THREE32
val int_to_foo32 : int32 -> foo32 option
val foo32_to_int : foo32 -> int32
val foo32_to_string : foo32 -> string
val string_to_foo32 : string -> foo32 option
type bar16 = | ONE | TWO | FOUR | FIVE
val int_to_bar16 : int -> bar16 option
val bar16_to_int : bar16 -> int
val bar16_to_string : bar16 -> string
val string_to_bar16 : string -> bar16 option
```

You can also add a `(sexp)` decorator to output s-expression convertors
for use with the `sexplib` library.

```
cenum foo64 {
ONE64;
TWO64;
THREE64
} as uint64_t(sexp)
```

And `sexp_of_foo64` and `foo64_of_sexp` functions will also be available.
The representation of the Sexp is the string representation of the enum.

Please see the `lib_test/` directory for more in-depth examples.

[![Build Status](https://travis-ci.org/mirage/ocaml-cstruct.png)](https://travis-ci.org/mirage/ocaml-cstruct)
65 changes: 47 additions & 18 deletions _oasis
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
OASISFormat: 0.4
Name: cstruct
Version: 1.9.0
Version: 2.0.0
Synopsis: Manipulate external buffers as C-like structs
Authors: Anil Madhavapeddy, Richard Mortier, Thomas Gazagnaire,
Pierre Chambart, David Kaloper, Jeremy Yallop, Hannes Mehnert
License: ISC
Plugins: META (0.4), DevFiles (0.4)
BuildTools: ocamlbuild
PostConfCommand: sh select_ppx_version.sh

Flag lwt
Description: build the Lwt library
Expand All @@ -20,10 +21,6 @@ Flag unix
Description: build the UNIX library
Default: true

Flag camlp4
Description: build the camlp4 syntax extension
Default: true

Flag ppx
Description: build the ppx syntax extension
Default: true
Expand Down Expand Up @@ -59,19 +56,6 @@ Library unix_cstruct
Findlibparent: cstruct
Modules: Unix_cstruct

Library "cstruct-syntax"
Build$: flag(camlp4)
FindlibName: syntax
FindlibParent: cstruct
BuildTools: ocamlbuild
Path: syntax
BuildDepends: camlp4.lib, camlp4.quotations.r, camlp4.extend, bigarray
Modules: Pa_cstruct
XMETAEnable: true
XMETAType: syntax
XMETADescription: Syntax extension for Cstruct
XMETARequires: camlp4

Executable ppx_cstruct
Build$: flag(ppx)
Install: true
Expand All @@ -95,6 +79,51 @@ Test test_bounds
Run: true
WorkingDirectory: _build/lib/

Executable test_ppx_basic
Build$: flag(tests) && flag(ppx)
Install: false
Path: ppx_test
Custom: true
CompiledObject: best
BuildTools: ppx_cstruct
BuildDepends: cstruct, unix, oUnit, cstruct.unix
MainIs: basic.ml

Executable test_ppx_enum
Build$: flag(tests) && flag(ppx)
Install: false
Path: ppx_test
Custom: true
CompiledObject: best
BuildTools: ppx_cstruct
BuildDepends: cstruct, unix, oUnit, cstruct.unix
MainIs: enum.ml

Executable test_ppx_pcap
Build$: flag(tests) && flag(ppx)
Install: false
Path: ppx_test
Custom: true
CompiledObject: best
BuildTools: ppx_cstruct
BuildDepends: cstruct, unix, oUnit, cstruct.unix
MainIs: pcap.ml

Test test_ppx_pcap
Command: $test_ppx_pcap
Run$: flag(ppx)
WorkingDirectory: ppx_test

Test test_ppx_basic
Command: $test_ppx_basic
Run$: flag(ppx)
WorkingDirectory: ppx_test

Test test_ppx_enum
Command: $test_ppx_enum
Run$: flag(ppx)
WorkingDirectory: ppx_test

Executable misc_tests
Build$: flag(tests)
Install: false
Expand Down
Loading

0 comments on commit daafda7

Please sign in to comment.