From 8e753ba58844f0188306fd0a468fab899bd4308d Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Thu, 1 Jan 1970 00:00:00 +0000 Subject: [PATCH] Added get_int32/int64/C_int/C_long --- lib/virt_mem_mmap.ml | 24 ++++++++++++++++++++++++ lib/virt_mem_mmap.mli | 12 ++++++++++++ 2 files changed, 36 insertions(+) diff --git a/lib/virt_mem_mmap.ml b/lib/virt_mem_mmap.ml index f6edee8..87741e4 100644 --- a/lib/virt_mem_mmap.ml +++ b/lib/virt_mem_mmap.ml @@ -252,6 +252,30 @@ let get_bytes t addr len = with Invalid_argument _ -> invalid_arg "get_bytes" +let get_int32 t addr = + let e = get_endian t in + let str = get_bytes t addr 4 in + let bs = Bitmatch.bitstring_of_string str in + bitmatch bs with + | { addr : 32 : endian (e) } -> addr + | { _ } -> invalid_arg "follow_pointer" + +let get_int64 t addr = + let e = get_endian t in + let str = get_bytes t addr 8 in + let bs = Bitmatch.bitstring_of_string str in + bitmatch bs with + | { addr : 64 : endian (e) } -> addr + | { _ } -> invalid_arg "follow_pointer" + +let get_C_int = get_int32 + +let get_C_long t addr = + let ws = get_wordsize t in + match ws with + | W32 -> Int64.of_int32 (get_int32 t addr) + | W64 -> get_int64 t addr + let get_string t addr = let chars = ref [] in try diff --git a/lib/virt_mem_mmap.mli b/lib/virt_mem_mmap.mli index 68c7b2f..de052a2 100644 --- a/lib/virt_mem_mmap.mli +++ b/lib/virt_mem_mmap.mli @@ -87,6 +87,18 @@ val get_bytes : ('a, 'b) t -> addr -> int -> string This may raise [Invalid_argument "get_bytes"] if the address range is not fully mapped. *) +val get_int32 : ('a, [`Endian]) t -> addr -> int32 +(** Return the 32-bit int at [addr]. *) + +val get_int64 : ('a, [`Endian]) t -> addr -> int64 +(** Return the 64-bit int at [addr]. *) + +val get_C_int : ([`Wordsize], [`Endian]) t -> addr -> int32 +(** Return the C 32-bit int at [addr]. *) + +val get_C_long : ([`Wordsize], [`Endian]) t -> addr -> int64 +(** Return the C 32 or 64-bit long at [addr]. *) + val get_string : ('a, 'b) t -> addr -> string (** Return the sequence of bytes starting at [addr] up to (but not including) the first ASCII NUL character. In other words, this -- 1.8.3.1