package cstruct

  1. Overview
  2. Docs
Access C-like structures directly from OCaml

Install

Dune Dependency

Authors

Maintainers

Sources

cstruct-6.1.1.tbz
sha256=1b74f9870f6a7ee6008924590716dd533a728a3ca10bb18da9fea8be467f518d
sha512=6fdd4517436c501ff0f39088eb0c3ea18e93370e020fa4d10dbf7aad29f1ee9cef881d52499f02c738e8aa0d78b568622a793c68f50ef51f282ee03160171317

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.

Tags

org:mirage org:ocamllabs

Published: 25 Jul 2022

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 library

  • cstruct-sexp: serialisers into s-expression format of Cstructs

  • cstruct-unix: provide Unix variations of the read/write functions using file descriptors

  • cstruct-async: provide Async Pipe and Bigstring support

  • cstruct-lwt: provide Lwt variants of read/write functions

  • ppx_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.

Dependencies (3)

  1. fmt >= "0.8.9"
  2. dune >= "2.0.0"
  3. ocaml >= "4.08.0"

Dev Dependencies (2)

  1. crowbar with-test
  2. alcotest with-test

  1. albatross >= "1.3.0"
  2. angstrom >= "0.2.0" & < "0.6.0"
  3. arakoon >= "1.8.6" & < "1.8.12"
  4. arp >= "3.0.0"
  5. asn1-combinators >= "0.2.6"
  6. awa != "0.0.3"
  7. awa-lwt
  8. awa-mirage
  9. bip32
  10. builder
  11. builder-web
  12. capnp-rpc-net >= "1.2.1"
  13. carton >= "0.4.3"
  14. carton-git
  15. carton-lwt
  16. certify >= "0.2" & < "0.3.3"
  17. chamelon
  18. channel
  19. charrua
  20. charrua-client
  21. charrua-client-lwt
  22. charrua-client-mirage < "0.12.0"
  23. charrua-server
  24. charrua-unix = "0.6"
  25. cohttp >= "0.9.7" & < "0.10.0"
  26. cohttp-mirage >= "6.0.0~alpha0"
  27. colombe < "0.2.0"
  28. conduit >= "0.6.0" & < "0.15.2"
  29. conduit-async = "3.0.0"
  30. conduit-lwt = "3.0.0"
  31. conduit-mirage != "3.0.0"
  32. conex < "0.10.0"
  33. conex-mirage-crypto
  34. conex-nocrypto >= "0.11.0"
  35. cowabloga >= "0.0.5"
  36. crc
  37. crunch >= "2.0.0" & < "3.0.0"
  38. cstruct-async >= "3.4.0" & < "4.0.0" | = "6.1.1"
  39. cstruct-lwt = "6.1.1"
  40. cstruct-sexp = "6.1.1"
  41. cstruct-unix >= "3.1.0" & < "3.2.0" | >= "3.4.0" & < "4.0.0" | = "6.1.1"
  42. current-albatross-deployer
  43. current_git >= "0.6"
  44. current_github >= "0.4"
  45. current_web >= "0.4"
  46. datakit
  47. datakit-ci >= "0.12.4"
  48. datakit-client
  49. datakit-client-9p
  50. datakit-server < "0.12.0"
  51. depyt >= "0.3.0"
  52. dirsp-proscript
  53. dns >= "6.0.0"
  54. dns-cli >= "6.0.0"
  55. dns-client < "7.0.0"
  56. dns-forward >= "0.9.0"
  57. dns-mirage >= "6.0.0"
  58. dns-server >= "6.0.0"
  59. dns-stub >= "6.0.0"
  60. dns-tsig >= "6.0.0"
  61. dnssd
  62. dnssec
  63. dream
  64. eio
  65. eqaf >= "0.8"
  66. ethernet >= "2.2.1"
  67. fat-filesystem = "0.13.0" | >= "0.15.0"
  68. frenetic < "2.0.0" | >= "3.2.0" & < "5.0.0" | >= "5.0.5"
  69. geojsone >= "0.2.0"
  70. git >= "2.0.0" & < "3.4.0" | >= "3.9.1"
  71. git-cohttp
  72. git-cohttp-mirage
  73. git-cohttp-unix
  74. git-mirage >= "3.0.0"
  75. git-unix >= "3.0.0"
  76. github-hooks >= "0.2.0"
  77. gluten-mirage
  78. gpt
  79. h2-mirage
  80. hacl-star >= "0.7.0"
  81. hex >= "1.4.0"
  82. hkdf
  83. http-multipart-formdata >= "3.1.0"
  84. hvsock
  85. io-page
  86. io-page-unix
  87. io-page-xen
  88. ipaddr-cstruct >= "5.2.0"
  89. irmin >= "0.9.0" & < "1.0.0" | >= "1.1.0" & < "1.3.2"
  90. irmin-git >= "2.3.0"
  91. irmin-indexeddb >= "0.3"
  92. jose != "0.6.0"
  93. key-parsers >= "0.5.0" & < "0.9.2" | >= "1.2.1"
  94. launchd
  95. learn-ocaml-client
  96. ledgerwallet >= "0.2.1"
  97. letsencrypt >= "0.4.0"
  98. letsencrypt-app >= "0.4.0"
  99. lt-code
  100. macaddr-cstruct >= "5.2.0"
  101. mbr-format = "1.0.0"
  102. memtrace_viewer < "v0.15.0"
  103. metrics-mirage
  104. mimic >= "0.0.4"
  105. mirage >= "0.7.2" & < "0.9.0" | >= "0.10.0" & < "2.4.0"
  106. mirage-block >= "2.0.0"
  107. mirage-block-ccm
  108. mirage-block-combinators >= "3.0.0"
  109. mirage-block-ramdisk >= "0.3" & < "0.5"
  110. mirage-block-solo5 >= "0.6.2"
  111. mirage-block-unix = "2.0.0" | = "2.7.0" | >= "2.11.1" & < "2.12.0" | >= "2.13.0"
  112. mirage-block-xen >= "1.4.0" & < "1.5.2" | >= "1.6.0" & < "2.0.0" | >= "2.1.1"
  113. mirage-btrees
  114. mirage-channel = "4.0.0" | >= "4.1.0"
  115. mirage-channel-lwt
  116. mirage-clock-unix < "1.0.0"
  117. mirage-clock-xen < "1.0.0"
  118. mirage-conduit < "2.0.0" | >= "2.3.1"
  119. mirage-console >= "3.0.0" & < "4.0.0"
  120. mirage-console-lwt
  121. mirage-console-solo5 >= "0.2.0"
  122. mirage-console-unix >= "2.2.1" & < "2.3.3" | >= "3.0.0" & < "3.0.2" | >= "5.0.0"
  123. mirage-console-xen >= "5.0.0"
  124. mirage-console-xen-backend >= "5.0.0"
  125. mirage-crypto >= "0.8.1" & < "0.8.5" | >= "0.10.4"
  126. mirage-crypto-ec
  127. mirage-crypto-entropy
  128. mirage-crypto-pk
  129. mirage-crypto-rng
  130. mirage-crypto-rng-eio
  131. mirage-crypto-rng-mirage
  132. mirage-dns != "2.6.0" & < "2.7.0"
  133. mirage-entropy < "0.5.1"
  134. mirage-entropy-xen < "0.3.0"
  135. mirage-flow < "1.2.0" | >= "2.0.0"
  136. mirage-flow-combinators != "2.0.1"
  137. mirage-flow-rawlink
  138. mirage-flow-unix >= "1.4.0" & != "2.0.1"
  139. mirage-fs >= "0.4.0" & < "1.0.0" | >= "3.0.0"
  140. mirage-fs-lwt
  141. mirage-fs-unix < "1.1.0" | >= "1.4.0" & < "1.5.0"
  142. mirage-kv-lwt
  143. mirage-kv-unix
  144. mirage-nat
  145. mirage-net = "0.5.2" | >= "3.0.0"
  146. mirage-net-fd < "0.2.1"
  147. mirage-net-lwt
  148. mirage-net-macosx
  149. mirage-net-solo5
  150. mirage-net-unix < "2.1.0" | >= "2.2.1" & < "2.4.1" | >= "2.6.0"
  151. mirage-net-xen != "1.4.2" & != "1.7.0"
  152. mirage-profile >= "0.8.2"
  153. mirage-protocols >= "6.0.0" & < "8.0.0"
  154. mirage-protocols-lwt
  155. mirage-qubes != "0.2" & < "0.5" | >= "0.7.0" & < "0.9.0" | >= "0.9.2"
  156. mirage-qubes-ipv4
  157. mirage-random
  158. mirage-random-stdlib
  159. mirage-random-test
  160. mirage-solo5
  161. mirage-stack-lwt
  162. mirage-tc
  163. mirage-tcpip-unix
  164. mirage-tcpip-xen
  165. mirage-types-lwt < "3.7.1"
  166. mirage-unix < "0.9.4" | >= "2.5.0" & < "3.0.8"
  167. mirage-vnetif >= "0.6.0"
  168. mirage-vnetif-stack
  169. mirage-www < "0.4.0" | >= "1.1.0"
  170. mirage-xen < "2.0.0" | >= "2.6.0"
  171. monorobot
  172. mrt-format >= "0.3.1"
  173. mstruct < "1.4.0"
  174. multihash
  175. nbd >= "4.0.3"
  176. netchannel != "2.0.0"
  177. nocrypto < "0.4.0"
  178. noise
  179. oneffs
  180. openflow < "0.2.0"
  181. otr = "0.3.1" | >= "0.3.5"
  182. ox < "1.1.1"
  183. paf >= "0.0.5"
  184. pbkdf >= "1.2.0"
  185. pcap-format >= "0.5.2"
  186. pf-qubes
  187. plebeia
  188. plist-xml >= "0.4.0"
  189. ppx_cstruct = "6.1.1"
  190. protocol-9p >= "2.0.2"
  191. protocol-9p-tool >= "2.0.2"
  192. protocol-9p-unix = "0.11.3" | >= "2.0.2"
  193. qcow >= "0.11.0"
  194. qcow-tool
  195. randomconv
  196. rawlink >= "0.6" & != "1.0"
  197. reparse >= "3.0.0"
  198. reparse-lwt
  199. reparse-lwt-unix
  200. rfc6287 >= "1.0.2" & < "1.0.4"
  201. salsa20 != "1.1.0"
  202. salsa20-core >= "1.1.0"
  203. scrypt-kdf = "1.0.0" | >= "1.2.0"
  204. secp256k1-internal >= "0.3.1"
  205. sendmail >= "0.5.0"
  206. shared-block-ring
  207. shared-memory-ring = "3.0.1" | >= "3.1.1"
  208. shared-memory-ring-lwt
  209. sihl >= "3.0.0"
  210. solo5-elftool
  211. ssh-agent != "0.3.0"
  212. tar >= "2.0.0"
  213. tar-mirage
  214. tar-unix != "1.0.0"
  215. tcpip >= "3.3.0" & < "4.1.0" | >= "7.1.0"
  216. tezos-lmdb
  217. tls >= "0.12.5" & < "0.13.1" | >= "0.14.0"
  218. u2f >= "0.1.2"
  219. uecc
  220. uring
  221. vchan >= "3.0.0" & < "5.0.0" | >= "6.0.1"
  222. vchan-unix
  223. vchan-xen
  224. vhd-format >= "0.12.0" & < "0.12.2"
  225. vhd-format-lwt >= "0.12.1" & < "0.12.3"
  226. vhd-tool < "0.12.0"
  227. vmnet >= "1.1.0"
  228. wayland >= "1.0"
  229. webauthn
  230. x509 >= "0.14.1"
  231. xe
  232. xen-api-client >= "0.9.6" & < "0.9.14"
  233. xen-block-driver >= "0.2.5"
  234. xen-gnt
  235. xenstore >= "2.1.0"

Conflicts (1)

  1. js_of_ocaml < "3.5.0"