Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

retire ocplib-endian. Since OCaml 4.01 the primitives are part of Bigarray #177

Merged
merged 2 commits into from
Nov 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion cstruct.opam
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ build-test: ["jbuilder" "runtest" "-p" name]

depends: [
"jbuilder" {build & >="1.0+beta10"}
"ocplib-endian"
"sexplib"
"base-bytes"
"ounit" {test}
Expand Down
100 changes: 52 additions & 48 deletions lib/cstruct.ml
Original file line number Diff line number Diff line change
Expand Up @@ -223,74 +223,78 @@ let create len =

let set_uint8 t i c =
if i >= t.len || i < 0 then err_invalid_bounds "set_uint8" t i 1
else EndianBigstring.BigEndian.set_int8 t.buffer (t.off+i) c
else Bigarray.Array1.set t.buffer (t.off+i) (Char.unsafe_chr c)

let set_char t i c =
if i >= t.len || i < 0 then err_invalid_bounds "set_char" t i 1
else EndianBigstring.BigEndian.set_char t.buffer (t.off+i) c
else Bigarray.Array1.set t.buffer (t.off+i) c

let get_uint8 t i =
if i >= t.len || i < 0 then err_invalid_bounds "get_uint8" t i 1
else EndianBigstring.BigEndian.get_uint8 t.buffer (t.off+i)
else Char.code (Bigarray.Array1.get t.buffer (t.off+i))

let get_char t i =
if i >= t.len || i < 0 then err_invalid_bounds "get_char" t i 1
else EndianBigstring.BigEndian.get_char t.buffer (t.off+i)
else Bigarray.Array1.get t.buffer (t.off+i)

module BE = struct
include EndianBigstring.BigEndian

let set_uint16 t i c =
if (i+2) > t.len || i < 0 then err_invalid_bounds "BE.set_uint16" t i 2
else set_int16 t.buffer (t.off+i) c

let set_uint32 t i c =
if (i+4) > t.len || i < 0 then err_invalid_bounds "BE.set_uint32" t i 4
else set_int32 t.buffer (t.off+i) c

let set_uint64 t i c =
if (i+8) > t.len || i < 0 then err_invalid_bounds "BE.set_uint64" t i 8
else set_int64 t.buffer (t.off+i) c
external ba_set_int16 : buffer -> int -> uint16 -> unit = "caml_ba_uint8_set16"
external ba_set_int32 : buffer -> int -> uint32 -> unit = "caml_ba_uint8_set32"
external ba_set_int64 : buffer -> int -> uint64 -> unit = "caml_ba_uint8_set64"
external ba_get_int16 : buffer -> int -> uint16 = "caml_ba_uint8_get16"
external ba_get_int32 : buffer -> int -> uint32 = "caml_ba_uint8_get32"
external ba_get_int64 : buffer -> int -> uint64 = "caml_ba_uint8_get64"

let get_uint16 t i =
if (i+2) > t.len || i < 0 then err_invalid_bounds "BE.get_uint16" t i 2
else get_uint16 t.buffer (t.off+i)
external swap16 : int -> int = "%bswap16"
external swap32 : int32 -> int32 = "%bswap_int32"
external swap64 : int64 -> int64 = "%bswap_int64"

let get_uint32 t i =
if (i+4) > t.len || i < 0 then err_invalid_bounds "BE.get_uint32" t i 4
else get_int32 t.buffer (t.off+i)
let set_uint16 swap p t i c =
if (i+2) > t.len || i < 0 then err_invalid_bounds (p ^ ".set_uint16") t i 2
else ba_set_int16 t.buffer (t.off+i) (if swap then swap16 c else c)

let get_uint64 t i =
if (i+8) > t.len || i < 0 then err_invalid_bounds "BE.uint64" t i 8
else get_int64 t.buffer (t.off+i)
end

module LE = struct
include EndianBigstring.LittleEndian
let set_uint32 swap p t i c =
if (i+4) > t.len || i < 0 then err_invalid_bounds (p ^ ".set_uint32") t i 4
else ba_set_int32 t.buffer (t.off+i) (if swap then swap32 c else c)

let set_uint16 t i c =
if (i+2) > t.len || i < 0 then err_invalid_bounds "LE.set_uint16" t i 2
else set_int16 t.buffer (t.off+i) c
let set_uint64 swap p t i c =
if (i+8) > t.len || i < 0 then err_invalid_bounds (p ^ ".set_uint64") t i 8
else ba_set_int64 t.buffer (t.off+i) (if swap then swap64 c else c)

let set_uint32 t i c =
if (i+4) > t.len || i < 0 then err_invalid_bounds "LE.set_uint32" t i 4
else set_int32 t.buffer (t.off+i) c
let get_uint16 swap p t i =
if (i+2) > t.len || i < 0 then err_invalid_bounds (p ^ ".get_uint16") t i 2
else
let r = ba_get_int16 t.buffer (t.off+i) in
if swap then swap16 r else r

let set_uint64 t i c =
if (i+8) > t.len || i < 0 then err_invalid_bounds "LE.set_uint64" t i 8
else set_int64 t.buffer (t.off+i) c
let get_uint32 swap p t i =
if (i+4) > t.len || i < 0 then err_invalid_bounds (p ^ ".get_uint32") t i 4
else
let r = ba_get_int32 t.buffer (t.off+i) in
if swap then swap32 r else r

let get_uint16 t i =
if (i+2) > t.len || i < 0 then err_invalid_bounds "LE.get_uint16" t i 2
else get_uint16 t.buffer (t.off+i)
let get_uint64 swap p t i =
if (i+8) > t.len || i < 0 then err_invalid_bounds (p ^ "uint64") t i 8
else
let r = ba_get_int64 t.buffer (t.off+i) in
if swap then swap64 r else r

let get_uint32 t i =
if (i+4) > t.len || i < 0 then err_invalid_bounds "LE.get_uint32" t i 4
else get_int32 t.buffer (t.off+i)
module BE = struct
let set_uint16 t i c = set_uint16 (not Sys.big_endian) "BE" t i c
let set_uint32 t i c = set_uint32 (not Sys.big_endian) "BE" t i c
let set_uint64 t i c = set_uint64 (not Sys.big_endian) "BE" t i c
let get_uint16 t i = get_uint16 (not Sys.big_endian) "BE" t i
let get_uint32 t i = get_uint32 (not Sys.big_endian) "BE" t i
let get_uint64 t i = get_uint64 (not Sys.big_endian) "BE" t i
end

let get_uint64 t i =
if (i+8) > t.len || i < 0 then err_invalid_bounds "LE.get_uint64" t i 8
else get_int64 t.buffer (t.off+i)
module LE = struct
let set_uint16 t i c = set_uint16 Sys.big_endian "LE" t i c
let set_uint32 t i c = set_uint32 Sys.big_endian "LE" t i c
let set_uint64 t i c = set_uint64 Sys.big_endian "LE" t i c
let get_uint16 t i = get_uint16 Sys.big_endian "LE" t i
let get_uint32 t i = get_uint32 Sys.big_endian "LE" t i
let get_uint64 t i = get_uint64 Sys.big_endian "LE" t i
end

let len t =
Expand Down
2 changes: 1 addition & 1 deletion lib/jbuild
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
((name cstruct)
(public_name cstruct)
(modules (cstruct))
(libraries (sexplib ocplib-endian ocplib-endian.bigstring))
(libraries (sexplib))
(c_names (cstruct_stubs))
))
(jbuild_version 1)