@@ -42,14 +42,91 @@ cstruct ipv4 {
4242} as big_endian
4343```
4444
45+ This auto-generates generates functions of the form below in the ` ml ` file:
46+
47+ ```
48+ let sizeof_pcap_packet = 16
49+ let get_pcap_packet_ts_sec v = Cstruct.LE.get_uint32 v 0
50+ let set_pcap_packet_ts_sec v x = Cstruct.LE.set_uint32 v 0 x
51+ let get_pcap_packet_ts_usec v = Cstruct.LE.get_uint32 v 4
52+ let set_pcap_packet_ts_usec v x = Cstruct.LE.set_uint32 v 4 x
53+ let get_pcap_packet_incl_len v = Cstruct.LE.get_uint32 v 8
54+ let set_pcap_packet_incl_len v x = Cstruct.LE.set_uint32 v 8 x
55+ let get_pcap_packet_orig_len v = Cstruct.LE.get_uint32 v 12
56+ let set_pcap_packet_orig_len v x = Cstruct.LE.set_uint32 v 12 x
57+
58+ let sizeof_ethernet = 14
59+ let get_ethernet_dst src = Cstruct.sub src 0 6
60+ let copy_ethernet_dst src = Cstruct.copy src 0 6
61+ let set_ethernet_dst src srcoff dst =
62+ Cstruct.blit_from_string src srcoff dst 0 6
63+ let blit_ethernet_dst src srcoff dst = Cstruct.blit src srcoff dst 0 6
64+ let get_ethernet_src src = Cstruct.sub src 6 6
65+ let copy_ethernet_src src = Cstruct.copy src 6 6
66+ let set_ethernet_src src srcoff dst =
67+ Cstruct.blit_from_string src srcoff dst 6 6
68+ let blit_ethernet_src src srcoff dst = Cstruct.blit src srcoff dst 6 6
69+ let get_ethernet_ethertype v = Cstruct.BE.get_uint16 v 12
70+ let set_ethernet_ethertype v x = Cstruct.BE.set_uint16 v 12 x
71+ ```
72+
73+ The ` mli ` file will have signatures of this form:
74+
75+ ```
76+ val sizeof_pcap_packet : int
77+ val get_pcap_packet_ts_sec : Cstruct.t -> Cstruct.uint32
78+ val set_pcap_packet_ts_sec : Cstruct.t -> Cstruct.uint32 -> unit
79+ val get_pcap_packet_ts_usec : Cstruct.t -> Cstruct.uint32
80+ val set_pcap_packet_ts_usec : Cstruct.t -> Cstruct.uint32 -> unit
81+ val get_pcap_packet_incl_len : Cstruct.t -> Cstruct.uint32
82+ val set_pcap_packet_incl_len : Cstruct.t -> Cstruct.uint32 -> unit
83+ val get_pcap_packet_orig_len : Cstruct.t -> Cstruct.uint32
84+ val set_pcap_packet_orig_len : Cstruct.t -> Cstruct.uint32 -> unit
85+
86+ val sizeof_ethernet : int
87+ val get_ethernet_dst : Cstruct.t -> Cstruct.t
88+ val copy_ethernet_dst : Cstruct.t -> string
89+ val set_ethernet_dst : string -> int -> Cstruct.t -> unit
90+ val blit_ethernet_dst : Cstruct.t -> int -> Cstruct.t -> unit
91+ val get_ethernet_src : Cstruct.t -> Cstruct.t
92+ val copy_ethernet_src : Cstruct.t -> string
93+ val set_ethernet_src : string -> int -> Cstruct.t -> unit
94+ val blit_ethernet_src : Cstruct.t -> int -> Cstruct.t -> unit
95+ val get_ethernet_ethertype : Cstruct.t -> Cstruct.uint16
96+ val set_ethernet_ethertype : Cstruct.t -> Cstruct.uint16 -> unit
97+ ```
4598You can also declare C-like enums:
4699
47100```
48- cenum foo64 {
49- ONE64;
50- TWO64;
51- THREE64
52- } as uint64_t
101+ cenum foo32 {
102+ ONE32;
103+ TWO32 = 0xfffffffel;
104+ THREE32
105+ } as uint32_t
106+
107+ cenum bar16 {
108+ ONE = 1;
109+ TWO;
110+ FOUR = 4;
111+ FIVE
112+ } as uint16_t
113+ ```
114+
115+ This generates signatures of the form:
116+
117+ ```
118+ type foo32 = | ONE32 | TWO32 | THREE32
119+ val int_to_foo32 : int32 -> foo32 option
120+ val foo32_to_int : foo32 -> int32
121+ val foo32_to_string : foo32 -> string
122+ val string_to_foo32 : string -> foo32 option
123+ type bar16 = | ONE | TWO | FOUR | FIVE
124+ val int_to_bar16 : int -> bar16 option
125+ val bar16_to_int : bar16 -> int
126+ val bar16_to_string : bar16 -> string
127+ val string_to_bar16 : string -> bar16 option
53128```
54129
55130Please see the ` lib_test/ ` directory for more in-depth examples.
131+
132+ [ ![ Build Status] ( https://travis-ci.org/avsm/ocaml-cstruct.png )] ( https://travis-ci.org/avsm/ocaml-cstruct )
0 commit comments