package cstruct

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

Install

Dune Dependency

Authors

Maintainers

Sources

cstruct-v6.0.1.tbz
sha256=4a67bb8f042753453c59eabf0e47865631253ba694091ce6062aac05d47a9bed
sha512=3eeeb6ae0fd3b625cf1d308498f0a1e6951d16566561f3362fdf74e7158d92d8f6c6d9fa968ff15f8c19a1886dce99d0ef17b44dbb37b97cc68c9b088fdc2248

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: 28 Jul 2021

README

Cstruct -- access C-like structures directly from OCaml

v6.0.1

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.03.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.

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. bigarray-compat
  2. dune >= "2.0.0"
  3. ocaml >= "4.03.0"

Dev Dependencies (3)

  1. ocaml with-test & < "4.08"
  2. crowbar with-test
  3. alcotest with-test

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

Conflicts (1)

  1. js_of_ocaml < "3.5.0"
OCaml

Innovation. Community. Security.