diff --git a/.travis-ci.sh b/.travis-ci.sh new file mode 100755 index 00000000..d13d8485 --- /dev/null +++ b/.travis-ci.sh @@ -0,0 +1,30 @@ +OPAM_DEPENDS="ocplib-endian lwt async" + +case "$OCAML_VERSION,$OPAM_VERSION" in +3.12.1,1.0.0) ppa=avsm/ocaml312+opam10 ;; +3.12.1,1.1.0) ppa=avsm/ocaml312+opam11 ;; +4.00.1,1.0.0) ppa=avsm/ocaml40+opam10 ;; +4.00.1,1.1.0) ppa=avsm/ocaml40+opam11 ;; +4.01.0,1.0.0) ppa=avsm/ocaml41+opam10 ;; +4.01.0,1.1.0) ppa=avsm/ocaml41+opam11 ;; +*) echo Unknown $OCAML_VERSION,$OPAM_VERSION; exit 1 ;; +esac + +echo "yes" | sudo add-apt-repository ppa:$ppa +sudo apt-get update -qq +sudo apt-get install -qq ocaml ocaml-native-compilers camlp4-extra opam time + +export OPAMYES=1 +export OPAMVERBOSE=1 +echo OCaml version +ocaml -version +echo OPAM versions +opam --version +opam --git-version + +opam init +opam install ${OPAM_DEPENDS} + +eval `opam config env` +make +./test.sh diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..eb80cc01 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,9 @@ +language: c +script: bash -ex .travis-ci.sh +env: + - OCAML_VERSION=4.01.0 OPAM_VERSION=1.0.0 + - OCAML_VERSION=4.01.0 OPAM_VERSION=1.1.0 + - OCAML_VERSION=4.00.1 OPAM_VERSION=1.0.0 + - OCAML_VERSION=4.00.1 OPAM_VERSION=1.1.0 + - OCAML_VERSION=3.12.1 OPAM_VERSION=1.0.0 + - OCAML_VERSION=3.12.1 OPAM_VERSION=1.1.0 diff --git a/CHANGES b/CHANGES index d6c0f4ef..073bf113 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,10 @@ +0.7.2 (trunk): +* Improved ocamldoc for BE/LE modules. +* Add Travis-CI test scripts and fix `test.sh` script compilation. +* Check and raise error in case of negative offsets for blits (#4). +* Correctly preserve the sequence after a constant constructor is set during a `cenum` definition. +* Do not repeat the `sizeof_` binding for every get/set field (should be no externally observable change). + 0.7.1 (06-Mar-2013): * Add `Async_cstruct.Pipe` to map pipes of `Cstruct` buffers to strings or `Bigsubstring`. diff --git a/README.md b/README.md index 01b79ba9..e263f3c4 100644 --- a/README.md +++ b/README.md @@ -42,14 +42,91 @@ cstruct ipv4 { } 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 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 +``` You can also declare C-like enums: ``` -cenum foo64 { - ONE64; - TWO64; - THREE64 -} as uint64_t +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 ``` Please see the `lib_test/` directory for more in-depth examples. + +[![Build Status](https://travis-ci.org/avsm/ocaml-cstruct.png)](https://travis-ci.org/avsm/ocaml-cstruct) diff --git a/_oasis b/_oasis index 1191c6ff..0de70ba5 100644 --- a/_oasis +++ b/_oasis @@ -1,6 +1,6 @@ OASISFormat: 0.3 Name: cstruct -Version: 0.7.1 +Version: 0.7.2 Synopsis: Manipulate external buffers as C-like structs Authors: Anil Madhavapeddy, Richard Mortier, Thomas Gazagnaire, Pierre Chambart License: ISC diff --git a/lib/META b/lib/META index 639cfda8..62ed1fde 100644 --- a/lib/META +++ b/lib/META @@ -1,6 +1,6 @@ # OASIS_START -# DO NOT EDIT (digest: c671864304beccb6078416a289ce5ee8) -version = "0.7.1" +# DO NOT EDIT (digest: 930869fcefc42a7e26a3e025657bc63c) +version = "0.7.2" description = "Manipulate external buffers as C-like structs" requires = "bigarray ocplib-endian ocplib-endian.bigstring" archive(byte) = "cstruct.cma" @@ -9,7 +9,7 @@ archive(native) = "cstruct.cmxa" archive(native, plugin) = "cstruct.cmxs" exists_if = "cstruct.cma" package "unix" ( - version = "0.7.1" + version = "0.7.2" description = "Manipulate external buffers as C-like structs" requires = "cstruct unix" archive(byte) = "unix_cstruct.cma" @@ -20,7 +20,7 @@ package "unix" ( ) package "syntax" ( - version = "0.7.1" + version = "0.7.2" description = "Syntax extension for Cstruct" requires = "camlp4" archive(syntax, preprocessor) = "cstruct-syntax.cma" @@ -29,7 +29,7 @@ package "syntax" ( ) package "lwt" ( - version = "0.7.1" + version = "0.7.2" description = "Manipulate external buffers as C-like structs" requires = "cstruct lwt.unix" archive(byte) = "lwt_cstruct.cma" @@ -40,7 +40,7 @@ package "lwt" ( ) package "async" ( - version = "0.7.1" + version = "0.7.2" description = "Manipulate external buffers as C-like structs" requires = "cstruct async threads" archive(byte) = "async_cstruct.cma" diff --git a/lib/cstruct.ml b/lib/cstruct.ml index 700b071c..f93d8154 100644 --- a/lib/cstruct.ml +++ b/lib/cstruct.ml @@ -132,23 +132,23 @@ external unsafe_blit_string_to_bigstring : string -> int -> buffer -> int -> int external unsafe_blit_bigstring_to_string : buffer -> int -> string -> int -> int -> unit = "caml_blit_bigstring_to_string" "noalloc" let copy src srcoff len = - if src.len - srcoff < len then raise (Invalid_argument (invalid_bounds srcoff len)); + if len < 0 || srcoff < 0 || src.len - srcoff < len then raise (Invalid_argument (invalid_bounds srcoff len)); let s = String.create len in unsafe_blit_bigstring_to_string src.buffer (src.off+srcoff) s 0 len; s let blit src srcoff dst dstoff len = - if src.len - srcoff < len then raise (Invalid_argument (invalid_bounds srcoff len)); + if len < 0 || srcoff < 0 || src.len - srcoff < len then raise (Invalid_argument (invalid_bounds srcoff len)); if dst.len - dstoff < len then raise (Invalid_argument (invalid_bounds dstoff len)); unsafe_blit_bigstring_to_bigstring src.buffer (src.off+srcoff) dst.buffer (dst.off+dstoff) len let blit_from_string src srcoff dst dstoff len = - if String.length src - srcoff < len then raise (Invalid_argument (invalid_bounds srcoff len)); + if len < 0 || srcoff < 0 || dstoff < 0 || String.length src - srcoff < len then raise (Invalid_argument (invalid_bounds srcoff len)); if dst.len - dstoff < len then raise (Invalid_argument (invalid_bounds dstoff len)); unsafe_blit_string_to_bigstring src srcoff dst.buffer (dst.off+dstoff) len let blit_to_string src srcoff dst dstoff len = - if src.len - srcoff < len then raise (Invalid_argument (invalid_bounds srcoff len)); + if len < 0 || srcoff < 0 || dstoff < 0 || src.len - srcoff < len then raise (Invalid_argument (invalid_bounds srcoff len)); if String.length dst - dstoff < len then raise (Invalid_argument (invalid_bounds dstoff len)); unsafe_blit_bigstring_to_string src.buffer (src.off+srcoff) dst dstoff len diff --git a/lib_test/basic.ml b/lib_test/basic.ml index e0973a93..8f8aad95 100644 --- a/lib_test/basic.ml +++ b/lib_test/basic.ml @@ -30,27 +30,27 @@ cstruct bar { let _ = (* Test basic set/get functions *) - let be = Bigarray.(Array1.create char c_layout sizeof_foo) in + let be = Cstruct.of_bigarray (Bigarray.(Array1.create char c_layout sizeof_foo)) in for i = 0 to 255 do set_bar_a be i; assert(get_bar_a be = i) done; - let le = Bigarray.(Array1.create char c_layout sizeof_bar) in + let le = Cstruct.of_bigarray (Bigarray.(Array1.create char c_layout sizeof_bar)) in for i = 0 to 255 do set_foo_a le i; assert(get_foo_a le = i) done; - let be = Bigarray.(Array1.create char c_layout sizeof_foo) in + let be = Cstruct.of_bigarray (Bigarray.(Array1.create char c_layout sizeof_foo)) in for i = 0 to 65535 do set_bar_b be i; assert(get_bar_b be = i) done; - let le = Bigarray.(Array1.create char c_layout sizeof_bar) in + let le = Cstruct.of_bigarray(Bigarray.(Array1.create char c_layout sizeof_bar)) in for i = 0 to 65535 do set_foo_b le i; assert(get_foo_b le = i) done; - let be = Bigarray.(Array1.create char c_layout sizeof_foo) in + let be = Cstruct.of_bigarray (Bigarray.(Array1.create char c_layout sizeof_foo)) in let rec fn = function |i when i < 0l -> () |i -> @@ -59,7 +59,7 @@ let _ = fn (Int32.sub i 0x10l) in fn 0xffffffff_l; (* Get/set buffers and blits *) - let le = Bigarray.(Array1.create char c_layout sizeof_bar) in + let le = Cstruct.of_bigarray (Bigarray.(Array1.create char c_layout sizeof_bar)) in let rec fn = function |i when i < 0l -> () |i -> @@ -75,19 +75,15 @@ let _ = assert(copy_bar_d le = s1); Printf.printf "%s %s\n" (copy_foo_d be) (copy_bar_d le); (* Create sub-view and shift it back *) - let be = Bigarray.(Array1.create char c_layout sizeof_foo) in + let be = Cstruct.of_bigarray (Bigarray.(Array1.create char c_layout sizeof_foo)) in set_foo_a be 7; set_foo_b be 44; set_foo_c be 0xbeef_l; set_foo_d "abcdefgh" 0 be; - (* shifting the base array should fail *) - assert(not (Cstruct.shift_left be 1)); (* get a subview *) let be2 = Cstruct.shift be 3 in assert(Cstruct.BE.get_uint32 be2 0 = 0xbeef_l); - (* shift it back *) - assert(Cstruct.shift_left be2 3); - assert(Cstruct.BE.get_uint32 be2 3 = 0xbeef_l); - assert(not (Cstruct.shift_left be2 1)); - assert(get_foo_b be2 = 44); - assert(get_foo_a be2 = 7) + assert(Cstruct.BE.get_uint32 be 3 = 0xbeef_l); + assert(get_foo_b be = 44); + assert(get_foo_a be = 7) + diff --git a/lib_test/enum.ml b/lib_test/enum.ml index 720385b9..7b1f836c 100644 --- a/lib_test/enum.ml +++ b/lib_test/enum.ml @@ -22,10 +22,17 @@ cenum foo64 { cenum foo32 { ONE32; - TWO32 = 3; + TWO32 = 0xfffffffel; THREE32 } as uint32_t +cenum bar16 { + ONE = 1; + TWO; + FOUR = 4; + FIVE +} as uint16_t + cenum foo16 { ONE16; TWO16; @@ -47,9 +54,11 @@ let _ = ignore(foo32_to_int ONE32); ignore(foo16_to_int ONE16); ignore(foo8_to_int ONE8); - assert(foo32_to_int TWO32 = 3l); - assert(foo32_to_int THREE32 = 1l); - assert(int_to_foo32 3l = Some (TWO32)); - assert(int_to_foo32 1l = Some (THREE32)); + assert(bar16_to_int FOUR = 4); + assert(bar16_to_int FIVE = 5); + assert(foo32_to_int TWO32 = 0xfffffffel); + assert(foo32_to_int THREE32 = 0xffffffffl); + assert(int_to_foo32 0xfffffffel = Some (TWO32)); + assert(int_to_foo32 0xffffffffl = Some (THREE32)); assert(string_to_foo16 "ONE16" = Some ONE16); - print_endline (foo8_to_string ONE8) + assert(foo8_to_string ONE8 = "ONE8") diff --git a/lib_test/enum.mli b/lib_test/enum.mli index e14a73e6..f8f0bff1 100644 --- a/lib_test/enum.mli +++ b/lib_test/enum.mli @@ -26,6 +26,13 @@ cenum foo32 { THREE32 } as uint32_t +cenum bar16 { + ONE = 1; + TWO; + FOUR = 4; + FIVE +} as uint16_t + cenum foo16 { ONE16; TWO16; diff --git a/lib_test/pcap.ml b/lib_test/pcap.ml index 55aa91cb..00144b97 100644 --- a/lib_test/pcap.ml +++ b/lib_test/pcap.ml @@ -65,15 +65,8 @@ let num_packets = ref 0 let mac_to_string buf = let i n = Cstruct.get_uint8 buf n in - let i0 = i 0 in - let i1 = i 1 in - let i2 = i 2 in - let i3 = i 3 in - let i4 = i 4 in - let i5 = i 5 in - (* Printf.sprintf "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x" - (i 0) (i 1) (i 2) (i 3) (i 4) (i 5) *) - "" + Printf.sprintf "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x" + (i 0) (i 1) (i 2) (i 3) (i 4) (i 5) let printf fmt = Printf.kprintf (fun _ -> ()) fmt @@ -82,7 +75,7 @@ let print_packet p = let dst_mac = mac_to_string (get_ethernet_dst p) in let src_mac = mac_to_string (get_ethernet_src p) in let ethertype = get_ethernet_ethertype p in - (* printf "ether %s -> %s etype %x\n" src_mac dst_mac ethertype; *) + printf "ether %s -> %s etype %x\n" src_mac dst_mac ethertype; match ethertype with |0x0800 -> begin let ip = Cstruct.shift p sizeof_ethernet in @@ -90,7 +83,7 @@ let print_packet p = let hlen = (get_ipv4_hlen_version ip land 0xf) * 4 in let ttl = get_ipv4_ttl ip in let proto = get_ipv4_proto ip in - (* printf "ipv%d hlen %d ttl %d proto %d\n" version hlen ttl proto; *) + printf "ipv%d hlen %d ttl %d proto %d\n" version hlen ttl proto; match proto with |6 -> begin (* tcp *) let tcp = Cstruct.shift ip sizeof_ipv4 in @@ -111,10 +104,9 @@ let print_packet p = let seqnum = get_tcpv4_seqnum tcp in let acknum = get_tcpv4_acknum tcp in let window = get_tcpv4_window tcp in -(* printf "tcpv4 port %d->%d seq %lu ack %lu win %d off %d flags %s opt %d fin %b syn %b\n" + printf "tcpv4 port %d->%d seq %lu ack %lu win %d off %d flags %s opt %d fin %b syn %b payload_len=%d\n" src_port dst_port seqnum - acknum window off flags options fin syn; - printf "%S\n" (Cstruct.to_string payload) *) + acknum window off flags options fin syn (Cstruct.len payload); () end |_ -> printf "unknown ip proto %d\n" proto @@ -126,10 +118,8 @@ let rec print_pcap_packet (hdr,pkt) = let ts_usec = get_pcap_packet_ts_usec hdr in let incl_len = get_pcap_packet_incl_len hdr in let orig_len = get_pcap_packet_orig_len hdr in -(* printf "\n** %lu.%lu bytes %lu (of %lu)\n" - ts_sec ts_used incl_len orig_len -*) + ts_sec ts_usec incl_len orig_len; print_packet pkt let print_pcap_header buf = @@ -146,20 +136,19 @@ let print_pcap_header buf = let sigfis = get_pcap_header_sigfigs buf in let snaplen = get_pcap_header_snaplen buf in let header_network = get_pcap_header_network buf in -(* printf "pcap_header (len %d)\n" sizeof_pcap_header; + printf "pcap_header (len %d)\n" sizeof_pcap_header; printf "magic_number %lx (%s)\n%!" magic endian; printf "version %d %d\n" version_major version_minor; printf "timezone shift %lu\n" thiszone; printf "timestamp accuracy %lu\n" sigfis; printf "snaplen %lu\n" snaplen; printf "lltype %lx\n" header_network -*) - () let parse () = + printf "start parse\n%!"; let fd = Unix.(openfile "http.cap" [O_RDONLY] 0) in let t = Unix_cstruct.of_fd fd in - printf "total pcap file length %d\n" (Cstruct.len t); + printf "total pcap file length %d\n%!" (Cstruct.len t); let header, body = Cstruct.split t sizeof_pcap_header in print_pcap_header header; diff --git a/lib_test/pcap.mli b/lib_test/pcap.mli index 14b79098..219aed53 100644 --- a/lib_test/pcap.mli +++ b/lib_test/pcap.mli @@ -1 +1,13 @@ -(* blank *) +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 + diff --git a/myocamlbuild.ml b/myocamlbuild.ml index c212ccb8..22149065 100644 --- a/myocamlbuild.ml +++ b/myocamlbuild.ml @@ -1,7 +1,7 @@ (* OASIS_START *) -(* DO NOT EDIT (digest: 5c43404f9413ffbf5bbd77be7addc313) *) +(* DO NOT EDIT (digest: 905e7d91c4639240bc33fbb11fc75e22) *) module OASISGettext = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/oasis/OASISGettext.ml" *) +(* # 21 "src/oasis/OASISGettext.ml" *) let ns_ str = str @@ -24,7 +24,7 @@ module OASISGettext = struct end module OASISExpr = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/oasis/OASISExpr.ml" *) +(* # 21 "src/oasis/OASISExpr.ml" *) @@ -116,7 +116,7 @@ end # 117 "myocamlbuild.ml" module BaseEnvLight = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/base/BaseEnvLight.ml" *) +(* # 21 "src/base/BaseEnvLight.ml" *) module MapString = Map.Make(String) @@ -214,7 +214,7 @@ end # 215 "myocamlbuild.ml" module MyOCamlbuildFindlib = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/plugins/ocamlbuild/MyOCamlbuildFindlib.ml" *) +(* # 21 "src/plugins/ocamlbuild/MyOCamlbuildFindlib.ml" *) (** OCamlbuild extension, copied from * http://brion.inria.fr/gallium/index.php/Using_ocamlfind_with_ocamlbuild @@ -323,7 +323,7 @@ module MyOCamlbuildFindlib = struct end module MyOCamlbuildBase = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/plugins/ocamlbuild/MyOCamlbuildBase.ml" *) +(* # 21 "src/plugins/ocamlbuild/MyOCamlbuildBase.ml" *) (** Base functions for writing myocamlbuild.ml @author Sylvain Le Gall @@ -339,7 +339,7 @@ module MyOCamlbuildBase = struct type name = string type tag = string -(* # 56 "/home/vb/.opam/system/build/oasis.0.3.0/src/plugins/ocamlbuild/MyOCamlbuildBase.ml" *) +(* # 56 "src/plugins/ocamlbuild/MyOCamlbuildBase.ml" *) type t = { diff --git a/setup.ml b/setup.ml index 35d82ae6..00d3645f 100644 --- a/setup.ml +++ b/setup.ml @@ -1,14 +1,14 @@ (* setup.ml generated for the first time by OASIS v0.3.0 *) (* OASIS_START *) -(* DO NOT EDIT (digest: e952d0378df0d02709f53c1c1e6d81a5) *) +(* DO NOT EDIT (digest: adc3bb7c90dea385af98c221b71c34ac) *) (* Regenerated by OASIS v0.3.0 Visit http://oasis.forge.ocamlcore.org for more information and documentation about functions used in this file. *) module OASISGettext = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/oasis/OASISGettext.ml" *) +(* # 21 "src/oasis/OASISGettext.ml" *) let ns_ str = str @@ -31,7 +31,7 @@ module OASISGettext = struct end module OASISContext = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/oasis/OASISContext.ml" *) +(* # 21 "src/oasis/OASISContext.ml" *) open OASISGettext @@ -92,7 +92,7 @@ module OASISContext = struct end module OASISString = struct -(* # 1 "/home/vb/.opam/system/build/oasis.0.3.0/src/oasis/OASISString.ml" *) +(* # 1 "src/oasis/OASISString.ml" *) @@ -217,7 +217,7 @@ module OASISString = struct end module OASISUtils = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/oasis/OASISUtils.ml" *) +(* # 21 "src/oasis/OASISUtils.ml" *) open OASISGettext @@ -312,7 +312,7 @@ module OASISUtils = struct end module PropList = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/oasis/PropList.ml" *) +(* # 21 "src/oasis/PropList.ml" *) open OASISGettext @@ -352,7 +352,7 @@ module PropList = struct let clear t = Hashtbl.clear t -(* # 71 "/home/vb/.opam/system/build/oasis.0.3.0/src/oasis/PropList.ml" *) +(* # 71 "src/oasis/PropList.ml" *) end module Schema = @@ -593,7 +593,7 @@ module PropList = struct end module OASISMessage = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/oasis/OASISMessage.ml" *) +(* # 21 "src/oasis/OASISMessage.ml" *) open OASISGettext @@ -632,7 +632,7 @@ module OASISMessage = struct end module OASISVersion = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/oasis/OASISVersion.ml" *) +(* # 21 "src/oasis/OASISVersion.ml" *) open OASISGettext @@ -811,7 +811,7 @@ module OASISVersion = struct end module OASISLicense = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/oasis/OASISLicense.ml" *) +(* # 21 "src/oasis/OASISLicense.ml" *) (** License for _oasis fields @author Sylvain Le Gall @@ -851,7 +851,7 @@ module OASISLicense = struct end module OASISExpr = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/oasis/OASISExpr.ml" *) +(* # 21 "src/oasis/OASISExpr.ml" *) @@ -941,7 +941,7 @@ module OASISExpr = struct end module OASISTypes = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/oasis/OASISTypes.ml" *) +(* # 21 "src/oasis/OASISTypes.ml" *) @@ -1018,7 +1018,7 @@ module OASISTypes = struct type plugin_data = (all_plugin * plugin_data_purpose * (unit -> unit)) list -(* # 102 "/home/vb/.opam/system/build/oasis.0.3.0/src/oasis/OASISTypes.ml" *) +(* # 102 "src/oasis/OASISTypes.ml" *) type 'a conditional = 'a OASISExpr.choices @@ -1176,7 +1176,7 @@ module OASISTypes = struct end module OASISUnixPath = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/oasis/OASISUnixPath.ml" *) +(* # 21 "src/oasis/OASISUnixPath.ml" *) type unix_filename = string type unix_dirname = string @@ -1260,7 +1260,7 @@ module OASISUnixPath = struct end module OASISHostPath = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/oasis/OASISHostPath.ml" *) +(* # 21 "src/oasis/OASISHostPath.ml" *) open Filename @@ -1293,7 +1293,7 @@ module OASISHostPath = struct end module OASISSection = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/oasis/OASISSection.ml" *) +(* # 21 "src/oasis/OASISSection.ml" *) open OASISTypes @@ -1372,12 +1372,12 @@ module OASISSection = struct end module OASISBuildSection = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/oasis/OASISBuildSection.ml" *) +(* # 21 "src/oasis/OASISBuildSection.ml" *) end module OASISExecutable = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/oasis/OASISExecutable.ml" *) +(* # 21 "src/oasis/OASISExecutable.ml" *) open OASISTypes @@ -1408,7 +1408,7 @@ module OASISExecutable = struct end module OASISLibrary = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/oasis/OASISLibrary.ml" *) +(* # 21 "src/oasis/OASISLibrary.ml" *) open OASISTypes open OASISUtils @@ -1841,32 +1841,32 @@ module OASISLibrary = struct end module OASISFlag = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/oasis/OASISFlag.ml" *) +(* # 21 "src/oasis/OASISFlag.ml" *) end module OASISPackage = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/oasis/OASISPackage.ml" *) +(* # 21 "src/oasis/OASISPackage.ml" *) end module OASISSourceRepository = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/oasis/OASISSourceRepository.ml" *) +(* # 21 "src/oasis/OASISSourceRepository.ml" *) end module OASISTest = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/oasis/OASISTest.ml" *) +(* # 21 "src/oasis/OASISTest.ml" *) end module OASISDocument = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/oasis/OASISDocument.ml" *) +(* # 21 "src/oasis/OASISDocument.ml" *) end module OASISExec = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/oasis/OASISExec.ml" *) +(* # 21 "src/oasis/OASISExec.ml" *) open OASISGettext open OASISUtils @@ -1944,7 +1944,7 @@ module OASISExec = struct end module OASISFileUtil = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/oasis/OASISFileUtil.ml" *) +(* # 21 "src/oasis/OASISFileUtil.ml" *) open OASISGettext @@ -2141,7 +2141,7 @@ end # 2142 "setup.ml" module BaseEnvLight = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/base/BaseEnvLight.ml" *) +(* # 21 "src/base/BaseEnvLight.ml" *) module MapString = Map.Make(String) @@ -2239,7 +2239,7 @@ end # 2240 "setup.ml" module BaseContext = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/base/BaseContext.ml" *) +(* # 21 "src/base/BaseContext.ml" *) open OASISContext @@ -2250,7 +2250,7 @@ module BaseContext = struct end module BaseMessage = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/base/BaseMessage.ml" *) +(* # 21 "src/base/BaseMessage.ml" *) (** Message to user, overrid for Base @author Sylvain Le Gall @@ -2269,7 +2269,7 @@ module BaseMessage = struct end module BaseEnv = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/base/BaseEnv.ml" *) +(* # 21 "src/base/BaseEnv.ml" *) open OASISGettext open OASISUtils @@ -2729,7 +2729,7 @@ module BaseEnv = struct end module BaseArgExt = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/base/BaseArgExt.ml" *) +(* # 21 "src/base/BaseArgExt.ml" *) open OASISUtils open OASISGettext @@ -2757,7 +2757,7 @@ module BaseArgExt = struct end module BaseCheck = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/base/BaseCheck.ml" *) +(* # 21 "src/base/BaseCheck.ml" *) open BaseEnv open BaseMessage @@ -2883,7 +2883,7 @@ module BaseCheck = struct end module BaseOCamlcConfig = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/base/BaseOCamlcConfig.ml" *) +(* # 21 "src/base/BaseOCamlcConfig.ml" *) open BaseEnv @@ -2999,7 +2999,7 @@ module BaseOCamlcConfig = struct end module BaseStandardVar = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/base/BaseStandardVar.ml" *) +(* # 21 "src/base/BaseStandardVar.ml" *) open OASISGettext @@ -3363,7 +3363,7 @@ module BaseStandardVar = struct end module BaseFileAB = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/base/BaseFileAB.ml" *) +(* # 21 "src/base/BaseFileAB.ml" *) open BaseEnv open OASISGettext @@ -3411,7 +3411,7 @@ module BaseFileAB = struct end module BaseLog = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/base/BaseLog.ml" *) +(* # 21 "src/base/BaseLog.ml" *) open OASISUtils @@ -3530,7 +3530,7 @@ module BaseLog = struct end module BaseBuilt = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/base/BaseBuilt.ml" *) +(* # 21 "src/base/BaseBuilt.ml" *) open OASISTypes open OASISGettext @@ -3677,7 +3677,7 @@ module BaseBuilt = struct end module BaseCustom = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/base/BaseCustom.ml" *) +(* # 21 "src/base/BaseCustom.ml" *) open BaseEnv open BaseMessage @@ -3727,7 +3727,7 @@ module BaseCustom = struct end module BaseDynVar = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/base/BaseDynVar.ml" *) +(* # 21 "src/base/BaseDynVar.ml" *) open OASISTypes @@ -3774,7 +3774,7 @@ module BaseDynVar = struct end module BaseTest = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/base/BaseTest.ml" *) +(* # 21 "src/base/BaseTest.ml" *) open BaseEnv open BaseMessage @@ -3864,7 +3864,7 @@ module BaseTest = struct end module BaseDoc = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/base/BaseDoc.ml" *) +(* # 21 "src/base/BaseDoc.ml" *) open BaseEnv open BaseMessage @@ -3899,7 +3899,7 @@ module BaseDoc = struct end module BaseSetup = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/base/BaseSetup.ml" *) +(* # 21 "src/base/BaseSetup.ml" *) open BaseEnv open BaseMessage @@ -4479,7 +4479,7 @@ end # 4480 "setup.ml" module InternalConfigurePlugin = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/plugins/internal/InternalConfigurePlugin.ml" *) +(* # 21 "src/plugins/internal/InternalConfigurePlugin.ml" *) (** Configure using internal scheme @author Sylvain Le Gall @@ -4721,7 +4721,7 @@ module InternalConfigurePlugin = struct end module InternalInstallPlugin = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/plugins/internal/InternalInstallPlugin.ml" *) +(* # 21 "src/plugins/internal/InternalInstallPlugin.ml" *) (** Install using internal scheme @author Sylvain Le Gall @@ -5232,7 +5232,7 @@ end # 5233 "setup.ml" module OCamlbuildCommon = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/plugins/ocamlbuild/OCamlbuildCommon.ml" *) +(* # 21 "src/plugins/ocamlbuild/OCamlbuildCommon.ml" *) (** Functions common to OCamlbuild build and doc plugin *) @@ -5334,7 +5334,7 @@ module OCamlbuildCommon = struct end module OCamlbuildPlugin = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/plugins/ocamlbuild/OCamlbuildPlugin.ml" *) +(* # 21 "src/plugins/ocamlbuild/OCamlbuildPlugin.ml" *) (** Build using ocamlbuild @author Sylvain Le Gall @@ -5507,7 +5507,7 @@ module OCamlbuildPlugin = struct end module OCamlbuildDocPlugin = struct -(* # 21 "/home/vb/.opam/system/build/oasis.0.3.0/src/plugins/ocamlbuild/OCamlbuildDocPlugin.ml" *) +(* # 21 "src/plugins/ocamlbuild/OCamlbuildDocPlugin.ml" *) (* Create documentation using ocamlbuild .odocl files @author Sylvain Le Gall @@ -5578,7 +5578,7 @@ let setup_t = ocaml_version = None; findlib_version = None; name = "cstruct"; - version = "0.7.1"; + version = "0.7.2"; license = OASISLicense.DEP5License (OASISLicense.DEP5Unit @@ -5890,7 +5890,7 @@ let setup_t = }; oasis_fn = Some "_oasis"; oasis_version = "0.3.0"; - oasis_digest = Some "\132\223j4\229\203-2_\"\235@\155\\\168D"; + oasis_digest = Some "õ±<\127­XûÚÞ\139õ¥A?æd"; oasis_exec = None; oasis_setup_args = []; setup_update = false; diff --git a/syntax/pa_cstruct.ml b/syntax/pa_cstruct.ml index 3c835b63..62a171a6 100644 --- a/syntax/pa_cstruct.ml +++ b/syntax/pa_cstruct.ml @@ -1,5 +1,5 @@ (* - * Copyright (c) 2012 Anil Madhavapeddy + * Copyright (c) 2012-2013 Anil Madhavapeddy * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -198,11 +198,10 @@ let output_struct _loc s = let expr = List.fold_left (fun a f -> <:str_item< $a$ ; - $output_sizeof _loc s$ ; $output_get _loc s f$ ; $output_set _loc s f$ >> - ) <:str_item< >> s.fields + ) <:str_item< $output_sizeof _loc s$ >> s.fields in expr let output_struct_sig _loc s = @@ -210,25 +209,24 @@ let output_struct_sig _loc s = let expr = List.fold_left (fun a f -> <:sig_item< $a$ ; - $output_sizeof_sig _loc s$ ; $output_get_sig _loc s f$ ; $output_set_sig _loc s f$ ; >> - ) <:sig_item< >> s.fields + ) <:sig_item< $output_sizeof_sig _loc s$ >> s.fields in expr let output_enum _loc name fields width = let intfn,pattfn = match ty_of_string width with |None -> loc_err _loc ("enum: unknown width specifier " ^ width) - |Some UInt8|Some UInt16 -> - (fun i -> <:expr< $int:string_of_int i$ >>), - (fun i -> <:patt< $int:string_of_int i$ >>) + |Some UInt8 |Some UInt16 -> + (fun i -> <:expr< $int:Int64.to_string i$ >>), + (fun i -> <:patt< $int:Int64.to_string i$ >>) |Some UInt32 -> - (fun i -> <:expr< $int32:string_of_int i$ >>), - (fun i -> <:patt< $int32:string_of_int i$ >>) + (fun i -> <:expr< $int32:Printf.sprintf "0x%Lx" i$ >>), + (fun i -> <:patt< $int32:Printf.sprintf "0x%Lx" i$ >>) |Some UInt64 -> - (fun i -> <:expr< $int64:string_of_int i$ >>), - (fun i -> <:patt< $int64:string_of_int i$ >>) + (fun i -> <:expr< $int64:Printf.sprintf "0x%Lx" i$ >>), + (fun i -> <:patt< $int64:Printf.sprintf "0x%Lx" i$ >>) |Some (Buffer _) -> loc_err _loc "enum: array types not allowed" in let decls = tyOr_of_list (List.map (fun (f,_) -> @@ -296,7 +294,10 @@ EXTEND Gram constr_enum: [ [ f = UIDENT -> (f, None) - | f = UIDENT; "="; i = INT -> (f, Some (int_of_string i)) ] + | f = UIDENT; "="; i = INT64 -> (f, Some (Int64.of_string i)) + | f = UIDENT; "="; i = INT32 -> (f, Some (Int64.of_string i)) + | f = UIDENT; "="; i = NATIVEINT -> (f, Some (Int64.of_string i)) + | f = UIDENT; "="; i = INT -> (f, Some (Int64.of_string i)) ] ]; sig_item: [ @@ -306,11 +307,12 @@ EXTEND Gram ] | [ "cenum"; name = LIDENT; "{"; fields = LIST0 [ constr_enum ] SEP ";"; "}"; "as"; width = LIDENT -> - let n = ref (-1) in + let n = ref Int64.minus_one in + let incr_n () = n := Int64.succ !n in let fields = List.map (function - | (f, None) -> incr n; (f, !n) - | (f, Some i) -> (f, i) + | (f, None) -> incr_n (); (f, !n) + | (f, Some i) -> n := i; (f, i) ) fields in output_enum_sig _loc name fields width ] @@ -323,11 +325,12 @@ EXTEND Gram ] | [ "cenum"; name = LIDENT; "{"; fields = LIST0 [ constr_enum ] SEP ";"; "}"; "as"; width = LIDENT -> - let n = ref (-1) in + let n = ref Int64.minus_one in + let incr_n () = n := Int64.succ !n in let fields = List.map (function - | (f, None) -> incr n; (f, !n) - | (f, Some i) -> (f, i) + | (f, None) -> incr_n (); (f, !n) + | (f, Some i) -> n := i; (f, i) ) fields in output_enum _loc name fields width ] diff --git a/test.sh b/test.sh index 5472f3d8..84e5c018 100755 --- a/test.sh +++ b/test.sh @@ -1,6 +1,6 @@ #!/bin/sh -ex -endian=$(ocamlfind query ocplib-endian.bigstring -format "-I %d %a" -predicates native,archive) +endian=`ocamlfind query ocplib-endian.bigstring -format "-I %d %a" -predicates native,archive` test() { echo $1 @@ -8,7 +8,7 @@ mkdir -p _build/lib_test cp lib_test/$1.ml _build/lib_test/$1.ml camlp4orf -printer o _build/syntax/cstruct-syntax.cma lib_test/$1.ml > _build/lib_test/$1.gen.ml camlp4orf -printer o _build/syntax/cstruct-syntax.cma lib_test/$1.mli > _build/lib_test/$1.gen.mli -ocamlopt -pp 'camlp4orf -printer o _build/syntax/cstruct-syntax.cma' -I _build/lib -I _build/unix -i lib_test/$1.ml > _build/lib_test/$1.inferred.mli +ocamlopt -pp 'camlp4orf _build/syntax/cstruct-syntax.cma' -I _build/lib -I _build/unix -i lib_test/$1.ml > _build/lib_test/$1.inferred.mli cp _build/lib_test/$1.inferred.mli _build/lib_test/$1.mli rm -f _build/lib_test/$1.cmi cd _build/lib_test @@ -24,8 +24,8 @@ time ./$1.opt cd ../.. } -#test basic -#test enum +test basic +test enum mkdir -p _build/lib_test ln -nsf ../../lib_test/http.cap _build/lib_test/http.cap test pcap