2016-02-18 07:11:59 -08:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* OCaml *)
|
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
|
|
|
(* Copyright 1997 Institut National de Recherche en Informatique et *)
|
|
|
|
(* en Automatique. *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. This file is distributed under the terms of *)
|
|
|
|
(* the GNU Lesser General Public License version 2.1, with the *)
|
|
|
|
(* special exception on linking described in the file LICENSE. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
1997-07-02 11:16:15 -07:00
|
|
|
|
|
|
|
type extern_flags =
|
|
|
|
No_sharing
|
|
|
|
| Closures
|
2013-04-18 04:58:59 -07:00
|
|
|
| Compat_32
|
2012-03-08 11:52:03 -08:00
|
|
|
(* note: this type definition is used in 'byterun/debugger.c' *)
|
1997-07-02 11:16:15 -07:00
|
|
|
|
|
|
|
external to_channel: out_channel -> 'a -> extern_flags list -> unit
|
2004-01-01 08:42:43 -08:00
|
|
|
= "caml_output_value"
|
2014-04-29 04:56:17 -07:00
|
|
|
external to_bytes: 'a -> extern_flags list -> bytes
|
|
|
|
= "caml_output_value_to_string"
|
1997-07-31 12:06:38 -07:00
|
|
|
external to_string: 'a -> extern_flags list -> string
|
2004-01-01 08:42:43 -08:00
|
|
|
= "caml_output_value_to_string"
|
1997-07-02 11:16:15 -07:00
|
|
|
external to_buffer_unsafe:
|
2014-04-29 04:56:17 -07:00
|
|
|
bytes -> int -> int -> 'a -> extern_flags list -> int
|
2004-01-01 08:42:43 -08:00
|
|
|
= "caml_output_value_to_buffer"
|
1997-07-02 11:16:15 -07:00
|
|
|
|
|
|
|
let to_buffer buff ofs len v flags =
|
2014-04-29 04:56:17 -07:00
|
|
|
if ofs < 0 || len < 0 || ofs > Bytes.length buff - len
|
1997-07-02 11:16:15 -07:00
|
|
|
then invalid_arg "Marshal.to_buffer: substring out of bounds"
|
|
|
|
else to_buffer_unsafe buff ofs len v flags
|
|
|
|
|
2014-08-22 06:45:02 -07:00
|
|
|
(* The functions below use byte sequences as input, never using any
|
|
|
|
mutation. It makes sense to use non-mutated [bytes] rather than
|
|
|
|
[string], because we really work with sequences of bytes, not
|
|
|
|
a text representation.
|
|
|
|
*)
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
external from_channel: in_channel -> 'a = "caml_input_value"
|
2014-04-29 04:56:17 -07:00
|
|
|
external from_bytes_unsafe: bytes -> int -> 'a
|
2004-01-01 08:42:43 -08:00
|
|
|
= "caml_input_value_from_string"
|
2014-04-29 04:56:17 -07:00
|
|
|
external data_size_unsafe: bytes -> int -> int = "caml_marshal_data_size"
|
1997-07-02 11:16:15 -07:00
|
|
|
|
|
|
|
let header_size = 20
|
|
|
|
let data_size buff ofs =
|
2014-04-29 04:56:17 -07:00
|
|
|
if ofs < 0 || ofs > Bytes.length buff - header_size
|
1997-07-02 11:16:15 -07:00
|
|
|
then invalid_arg "Marshal.data_size"
|
|
|
|
else data_size_unsafe buff ofs
|
1997-07-31 12:06:38 -07:00
|
|
|
let total_size buff ofs = header_size + data_size buff ofs
|
1997-07-02 11:16:15 -07:00
|
|
|
|
2014-04-29 04:56:17 -07:00
|
|
|
let from_bytes buff ofs =
|
|
|
|
if ofs < 0 || ofs > Bytes.length buff - header_size
|
|
|
|
then invalid_arg "Marshal.from_bytes"
|
1997-07-02 11:16:15 -07:00
|
|
|
else begin
|
|
|
|
let len = data_size_unsafe buff ofs in
|
2014-04-29 04:56:17 -07:00
|
|
|
if ofs > Bytes.length buff - (header_size + len)
|
|
|
|
then invalid_arg "Marshal.from_bytes"
|
|
|
|
else from_bytes_unsafe buff ofs
|
2005-10-25 11:34:07 -07:00
|
|
|
end
|
2014-04-29 04:56:17 -07:00
|
|
|
|
2014-08-22 06:45:02 -07:00
|
|
|
let from_string buff ofs =
|
|
|
|
(* Bytes.unsafe_of_string is safe here, as the produced byte
|
|
|
|
sequence is never mutated *)
|
|
|
|
from_bytes (Bytes.unsafe_of_string buff) ofs
|