Permalink
Browse files

Merge ../../mor1/ocaml-cstruct

Conflicts:
	unix/lib/cstruct.ml
	unix/lib/cstruct.mli
  • Loading branch information...
avsm committed Jun 30, 2012
2 parents 1f65118 + 4d8bab5 commit 1f0f097cf14e9f5de0876cfbe99f586f1ee5ac81
Showing with 65 additions and 1 deletion.
  1. +53 −1 unix/lib/cstruct.ml
  2. +12 −0 unix/lib/cstruct.mli
View
@@ -21,11 +21,64 @@ open Array1
type buf = (char, int8_unsigned_elt, c_layout) t
+type byte = char
+
+let byte (i:int) : byte = Char.chr i
+let byte_to_int (b:byte) = int_of_char b
+
+type bytes = string
+
+let bytes (s:string) : bytes = s
+
type uint8 = int
+
+let uint8 (i:int) : uint8 = min i 0xff
+
type uint16 = int
+
+let uint16 (i:int) : uint16 = min i 0xffff
+
type uint32 = int32
type uint64 = int64
+type ipv4 = int32
+
+let ipv4_to_string i =
+ let (&&&) x y = Int32.logand x y in
+ let (>>>) x y = Int32.shift_right_logical x y in
+ sprintf "%ld.%ld.%ld.%ld"
+ ((i &&& 0x0_ff000000_l) >>> 24) ((i &&& 0x0_00ff0000_l) >>> 16)
+ ((i &&& 0x0_0000ff00_l) >>> 8) ((i &&& 0x0_000000ff_l) )
+
+let bytes_to_ipv4 bs =
+ let (|||) x y = Int32.logor x y in
+ let (<<<) x y = Int32.shift_left x y in
+ let a = Int32.of_int (byte_to_int bs.[0]) in
+ let b = Int32.of_int (byte_to_int bs.[1]) in
+ let c = Int32.of_int (byte_to_int bs.[2]) in
+ let d = Int32.of_int (byte_to_int bs.[3]) in
+ (a <<< 24) ||| (b <<< 16) ||| (c <<< 8) ||| d
+
+type ipv6 = int64 * int64
+let ipv6_to_string (hi, lo) =
+ let (&&&&) x y = Int64.logand x y in
+ let (>>>>) x y = Int64.shift_right_logical x y in
+ sprintf "%Lx:%Lx:%Lx:%Lx:%Lx:%Lx:%Lx:%Lx"
+ ((hi >>>> 48) &&&& 0xffff_L) ((hi >>>> 32) &&&& 0xffff_L)
+ ((hi >>>> 16) &&&& 0xffff_L) ( hi &&&& 0xffff_L)
+ ((lo >>>> 48) &&&& 0xffff_L) ((lo >>>> 32) &&&& 0xffff_L)
+ ((lo >>>> 16) &&&& 0xffff_L) ( lo &&&& 0xffff_L)
+
+let bytes_to_ipv6 bs =
+ let (++++) x y = Int64.add x y in
+ let (<<<<) x y = Int64.shift_left x y in
+ let hihi = bytes_to_ipv4 (String.sub bs 0 4) in
+ let hilo = bytes_to_ipv4 (String.sub bs 4 4) in
+ let lohi = bytes_to_ipv4 (String.sub bs 8 4) in
+ let lolo = bytes_to_ipv4 (String.sub bs 12 4) in
+ ((Int64.of_int32 hihi) <<<< 48) ++++ (Int64.of_int32 hilo),
+ ((Int64.of_int32 lohi) <<<< 48) ++++ (Int64.of_int32 lolo)
+
let get_char s off =
get s off
@@ -192,4 +245,3 @@ let iter lenfn pfn buf =
let rec fold f next acc = match next () with
| None -> acc
| Some v -> fold f next (f acc v)
-
View
@@ -16,11 +16,23 @@
type buf = (char, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t
+type byte = char
+val byte : int -> byte
+val byte_to_int : byte -> int
+
+type bytes = string
+
type uint8 = int
type uint16 = int
type uint32 = int32
type uint64 = int64
+type ipv4 = int32
+type ipv6 = int64 * int64
+
+val ipv4_to_string : ipv4 -> string
+val ipv6_to_string : ipv6 -> string
+
val get_char : buf -> int -> char
val get_uint8 : buf -> int -> uint8
val set_char : buf -> int -> char -> unit

0 comments on commit 1f0f097

Please sign in to comment.