package cstruct
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=4f0d2d7d6b7048c99e5d9d62e62ed3fe957bdede56b385567962075c969f1d8b
sha512=24620eb8dd37b7508a6adbad19fe21b44739a1bd641827b4fdd74e642991647fb814cabdf44195199bdb5208cac182580c55dce511bf22ad595d56a140cc0584
Description
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.
README
Cstruct - access C-like structures directly from OCaml
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.
Installation
This repository provides several packages that can be installed via the opam package manager:
cstruct
: the core Cstruct librarycstruct-sexp
: serialisers into s-expression format of Cstructscstruct-unix
: provide Unix variations of the read/write functions using file descriptorscstruct-async
: provide Async Pipe and Bigstring supportcstruct-lwt
: provide Lwt variants of read/write functionsppx_cstruct
: a PPX syntax extension (see below)
The libraries depend on OCaml version 4.08.0 and later, since it provides a ppx extension point. The old camlp4 syntax extension is nolonger available; the last cstruct release which contained it was v1.9.0.
Local development
You can build the library via dune, using make
or dune build
directly. Since everything is built via dune, you can also place this repository within a wider dune workspace in order to make local modifications across repositories.
Documentation
A documentation of the last version of cstruct
is available here.
Usage
PPX
The PPX processor is used by passing the OCaml source code through the ppx_cstruct
binary. An example pcap description is:
[%%cstruct
type pcap_header = {
magic_number: uint32_t; (* magic number *)
version_major: uint16_t; (* major version number *)
version_minor: uint16_t; (* minor version number *)
thiszone: uint32_t; (* GMT to local correction *)
sigfigs: uint32_t; (* accuracy of timestamps *)
snaplen: uint32_t; (* max length of captured packets, in octets *)
network: uint32_t; (* data link type *)
} [@@little_endian]]
[%%cstruct
type pcap_packet = {
ts_sec: uint32_t; (* timestamp seconds *)
ts_usec: uint32_t; (* timestamp microseconds *)
incl_len: uint32_t; (* number of octets of packet saved in file *)
orig_len: uint32_t; (* actual length of packet *)
} [@@little_endian]]
[%%cstruct
type ethernet = {
dst: uint8_t [@len 6];
src: uint8_t [@len 6];
ethertype: uint16_t;
} [@@big_endian]]
[%%cstruct
type ipv4 = {
hlen_version: uint8_t;
tos: uint8_t;
len: uint16_t;
id: uint16_t;
off: uint16_t;
ttl: uint8_t;
proto: uint8_t;
csum: uint16_t;
src: uint8_t [@len 4];
dst: uint8_t [@len 4];
} [@@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
type foo32 =
| ONE32
| TWO32 [@id 0xfffffffel]
| THREE32
[@@uint32_t]
]
[%%cenum
type bar16 =
| ONE [@id 1]
| TWO
| FOUR [@id 4]
| FIVE
[@@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
val compare_foo32 : foo32 -> foo32 -> int
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
val compare_bar16 : bar16 -> bar16 -> int
Comparisons will be done relatively to the constructor ids.
You can also add a (sexp)
decorator to output s-expression convertors for use with the sexplib
library.
[%%cenum
type foo64 =
| ONE64
| TWO64
| THREE64
[@@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.
If you do use the sexp decorator, then you will also need to add sexplib
to the dependency list for your package (both in the dune
file and the opam
file).
Please see the ppx_test/
directory for more in-depth examples.
-
albatross
>= "1.3.0" & < "2.2.0"
-
angstrom
>= "0.2.0" & < "0.6.0"
-
arakoon
>= "1.8.6" & < "1.8.12"
-
arp
>= "3.0.0"
-
asn1-combinators
= "0.2.6"
-
async_unix
>= "v0.17.0"
-
awa
!= "0.0.3"
- awa-lwt
- awa-mirage
- bip32
-
builder
< "0.4.0"
-
builder-web
< "0.2.0"
-
capnp-rpc-net
>= "1.2.1"
-
carton
>= "0.4.3"
- carton-git
- carton-lwt
-
certify
>= "0.2" & < "0.3.3"
- chamelon
- channel
- charrua
- charrua-client
- charrua-client-lwt
-
charrua-client-mirage
< "0.12.0"
- charrua-server
-
charrua-unix
= "0.6"
-
cohttp
>= "0.9.7" & < "0.10.0"
-
cohttp-mirage
>= "6.0.0~alpha0"
-
colombe
< "0.2.0"
-
conduit
>= "0.6.0" & < "0.15.2"
-
conduit-async
= "3.0.0"
-
conduit-lwt
= "3.0.0"
-
conduit-mirage
!= "3.0.0"
-
conex
< "0.10.0"
- conex-mirage-crypto
-
conex-nocrypto
>= "0.11.0"
-
cowabloga
>= "0.0.5"
- crc
-
crunch
>= "2.0.0" & < "3.0.0"
-
cstruct-async
>= "3.4.0" & < "4.0.0" | = "6.1.0"
-
cstruct-lwt
= "6.1.0"
-
cstruct-sexp
= "6.1.0"
-
cstruct-unix
>= "3.1.0" & < "3.2.0" | >= "3.4.0" & < "4.0.0" | = "6.1.0"
- current-albatross-deployer
-
current_git
>= "0.6"
-
current_github
>= "0.4"
-
current_web
>= "0.4"
- datakit
-
datakit-ci
>= "0.12.4"
- datakit-client
- datakit-client-9p
-
datakit-server
< "0.12.0"
-
depyt
>= "0.3.0"
- dirsp-proscript
-
dns
>= "6.0.0" & < "9.0.0"
-
dns-cli
>= "6.0.0" & < "9.0.0"
-
dns-client
< "7.0.0"
-
dns-forward
>= "0.9.0"
-
dns-mirage
>= "6.0.0"
-
dns-server
>= "6.0.0"
-
dns-stub
>= "6.0.0"
-
dns-tsig
>= "6.0.0" & < "9.0.0"
- dnssd
-
dnssec
< "9.0.0"
- dream
- eio
-
eqaf
>= "0.8" & < "0.10"
- eqaf-cstruct
-
ethernet
>= "2.2.1"
-
fat-filesystem
= "0.13.0" | >= "0.15.0"
-
frenetic
< "2.0.0" | >= "3.2.0" & < "5.0.0" | >= "5.0.5"
-
geojsone
>= "0.2.0"
-
git
>= "2.0.0" & < "3.4.0" | >= "3.9.1"
- git-cohttp
- git-cohttp-mirage
- git-cohttp-unix
-
git-mirage
>= "3.0.0"
-
git-unix
>= "3.0.0"
-
github-hooks
>= "0.2.0"
-
gluten-mirage
< "0.5.2"
- gpt
- h2-mirage
-
hacl-star
>= "0.7.0"
-
hex
>= "1.4.0"
-
hkdf
< "2.0.0"
-
http-multipart-formdata
>= "3.1.0"
- httpun-mirage
- httpun-ws-mirage
- hvsock
- io-page
- io-page-unix
- io-page-xen
-
ipaddr-cstruct
>= "5.2.0"
-
irmin
>= "0.9.0" & < "1.0.0" | >= "1.1.0" & < "1.3.2"
-
irmin-git
>= "2.3.0"
-
irmin-indexeddb
>= "0.3"
-
jose
!= "0.6.0"
-
key-parsers
>= "0.5.0" & < "0.9.2" | >= "1.2.1"
- launchd
- learn-ocaml-client
-
ledgerwallet
>= "0.2.1"
-
letsencrypt
>= "0.4.0" & < "1.0.0"
-
letsencrypt-app
>= "0.4.0" & < "1.0.0"
- lt-code
-
macaddr-cstruct
>= "5.2.0"
-
mbr-format
= "1.0.0"
-
memtrace_viewer
< "v0.15.0"
- metrics-mirage
-
mimic
>= "0.0.4"
-
mirage
>= "0.7.2" & < "0.9.0" | >= "0.10.0" & < "2.4.0"
-
mirage-block
>= "2.0.0"
- mirage-block-ccm
-
mirage-block-combinators
>= "3.0.0"
-
mirage-block-ramdisk
>= "0.3" & < "0.5"
-
mirage-block-solo5
>= "0.6.2"
-
mirage-block-unix
= "2.0.0" | = "2.7.0" | >= "2.11.1" & < "2.12.0" | >= "2.13.0"
-
mirage-block-xen
>= "1.4.0" & < "1.5.2" | >= "1.6.0" & < "2.0.0" | >= "2.1.1"
- mirage-btrees
-
mirage-channel
= "4.0.0" | >= "4.1.0"
- mirage-channel-lwt
-
mirage-clock-unix
< "1.0.0"
-
mirage-clock-xen
< "1.0.0"
-
mirage-conduit
< "2.0.0" | >= "2.3.1"
-
mirage-console
>= "3.0.0" & < "4.0.0"
- mirage-console-lwt
-
mirage-console-solo5
>= "0.2.0"
-
mirage-console-unix
>= "2.2.1" & < "2.3.3" | >= "3.0.0" & < "3.0.2" | >= "5.0.0"
-
mirage-console-xen
>= "5.0.0"
-
mirage-console-xen-backend
>= "5.0.0"
-
mirage-crypto
>= "0.8.1" & < "0.8.5" | >= "0.10.4" & < "1.0.0"
-
mirage-crypto-ec
< "1.0.0"
- mirage-crypto-entropy
-
mirage-crypto-pk
< "1.0.0"
-
mirage-crypto-rng
< "1.0.0"
-
mirage-crypto-rng-eio
< "1.0.0"
-
mirage-crypto-rng-mirage
< "1.0.0"
-
mirage-dns
!= "2.6.0" & < "2.7.0"
-
mirage-entropy
< "0.5.1"
-
mirage-entropy-xen
< "0.3.0"
-
mirage-flow
< "1.2.0" | >= "2.0.0"
-
mirage-flow-combinators
!= "2.0.1"
- mirage-flow-rawlink
-
mirage-flow-unix
>= "1.4.0" & != "2.0.1"
-
mirage-fs
>= "0.4.0" & < "1.0.0" | >= "3.0.0"
- mirage-fs-lwt
-
mirage-fs-unix
< "1.1.0" | >= "1.4.0" & < "1.5.0"
- mirage-kv-lwt
- mirage-kv-unix
- mirage-nat
-
mirage-net
= "0.5.2" | >= "3.0.0"
-
mirage-net-fd
< "0.2.1"
- mirage-net-lwt
- mirage-net-macosx
- mirage-net-solo5
-
mirage-net-unix
< "2.1.0" | >= "2.2.1" & < "2.4.1" | >= "2.6.0"
-
mirage-net-xen
!= "1.4.2" & != "1.7.0"
-
mirage-profile
>= "0.8.2"
-
mirage-protocols
>= "6.0.0" & < "8.0.0"
- mirage-protocols-lwt
-
mirage-qubes
!= "0.2" & < "0.5" | >= "0.7.0" & < "0.9.0" | >= "0.9.2"
-
mirage-qubes-ipv4
< "0.9.4"
-
mirage-random
< "4.0.0"
- mirage-random-stdlib
- mirage-random-test
-
mirage-solo5
< "0.9.4"
- mirage-stack-lwt
- mirage-tc
- mirage-tcpip-unix
- mirage-tcpip-xen
-
mirage-types-lwt
< "3.7.1"
-
mirage-unix
< "0.9.4" | >= "2.5.0" & < "3.0.8"
-
mirage-vnetif
>= "0.6.0"
- mirage-vnetif-stack
-
mirage-www
< "0.4.0" | >= "1.1.0"
-
mirage-xen
< "2.0.0" | >= "2.6.0"
- monorobot
-
mrt-format
>= "0.3.1"
-
mstruct
< "1.4.0"
-
nbd
>= "4.0.3"
-
netchannel
!= "2.0.0"
-
nocrypto
< "0.4.0"
- noise
- oneffs
-
openflow
< "0.2.0"
-
otr
= "0.3.1" | >= "0.3.5" & < "1.0.0"
-
ox
< "1.1.1"
-
paf
>= "0.0.5"
-
pbkdf
= "1.2.0"
-
pcap-format
>= "0.5.2"
- pf-qubes
- plebeia
-
plist-xml
>= "0.4.0"
-
ppx_cstruct
= "6.1.0"
-
protocol-9p
>= "2.0.2"
-
protocol-9p-tool
>= "2.0.2"
-
protocol-9p-unix
= "0.11.3" | >= "2.0.2"
-
qcow
>= "0.11.0"
- qcow-tool
-
randomconv
< "0.2.0"
-
rawlink
>= "0.6" & != "1.0"
-
reparse
>= "3.0.0"
- reparse-lwt
- reparse-lwt-unix
-
rfc6287
>= "1.0.2" & < "1.0.4"
-
salsa20
!= "1.1.0"
-
salsa20-core
= "1.1.0"
-
scrypt-kdf
= "1.0.0" | >= "1.2.0"
-
secp256k1-internal
>= "0.3.1"
-
sendmail
>= "0.5.0"
- shared-block-ring
-
shared-memory-ring
= "3.0.1" | >= "3.1.1"
- shared-memory-ring-lwt
-
sihl
>= "3.0.0"
- slack
- solo5-elftool
-
ssh-agent
!= "0.3.0"
- swapfs
-
tar
>= "2.0.0" & < "3.0.0"
- tar-mirage
-
tar-unix
!= "1.0.0" & < "3.0.0"
-
tcpip
>= "3.3.0" & < "4.1.0" | >= "7.1.0"
- tezos-lmdb
-
tls
>= "0.12.5" & < "0.13.1" | >= "0.14.0" & < "1.0.0"
-
u2f
>= "0.1.2"
- uecc
- uring
-
vchan
>= "3.0.0" & < "5.0.0" | >= "6.0.1"
- vchan-unix
- vchan-xen
-
vhd-format
>= "0.12.0" & < "0.12.2"
-
vhd-format-lwt
>= "0.12.1" & < "0.12.3"
-
vhd-tool
< "0.12.0"
-
vmnet
>= "1.1.0"
-
wayland
>= "1.0"
-
webauthn
< "0.2.0"
-
x509
>= "0.14.1" & < "1.0.0"
- xe
-
xen-api-client
>= "0.9.6" & < "0.9.14"
-
xen-block-driver
>= "0.2.5"
- xen-gnt
-
xenstore
>= "2.1.0" & < "2.3.0"
Conflicts (1)
-
js_of_ocaml
< "3.5.0"