5 people like it.

Unmanaged memory search

Useful for pinvoke stuff where your native API requires that you have a unmanaged buffer containing the data you want to work on. Edit: Changed native calls, removed Marshal.Copy (there is a async corner case but I won't delve into it here) and replaced with pinning.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
open System    

module native = 
  open System.Runtime.InteropServices

  [<DllImport @"boyer_moore.dll">]
  extern nativeint boyerMoore(
    nativeint data, 
    nativeint search, 
    int       datalen, 
    int       searchlen)

  let vrfy n = if n <= 0n then failwith "null ptr" else n
  
  let inline (~~) (data : GCHandle) = data.AddrOfPinnedObject()
  let inline (!~) (ptr  : GCHandle) = ptr.Free()
  
  let pin (data : 'a array)       = GCHandle.Alloc(data,GCHandleType.Pinned)

  let search (data : byte array) (search : byte array) =
    let d,s = pin data, pin search

    let ret = ref 0n
    
    lock ret (fun () ->
                ret := boyerMoore(~~d,~~s,data.Length,search.Length)
                !~d; !~s
                )
    !ret

let f1 = IO.File.ReadAllBytes @"C:\users\dklein\desktop\librhash.dll"

let sbox = [|
   27uy; 0uy; 249uy; 100uy; 246uy; 205uy; 221uy; 254uy; 226uy; 241uy; 143uy;
   124uy; 20uy; 21uy; 215uy; 17uy; 211uy; 24uy; 140uy; 139uy; 30uy; 136uy;
   223uy; 221uy|]

match native.search f1 sbox with
  | x when x > 0n -> sprintf "[s-box detected] snefru hash function at 0x%x" x
  | _             -> ""
namespace System
namespace System.Runtime
namespace System.Runtime.InteropServices
Multiple items
type DllImportAttribute =
  inherit Attribute
  new : dllName:string -> DllImportAttribute
  val EntryPoint : string
  val CharSet : CharSet
  val SetLastError : bool
  val ExactSpelling : bool
  val PreserveSig : bool
  val CallingConvention : CallingConvention
  val BestFitMapping : bool
  val ThrowOnUnmappableChar : bool
  member Value : string

Full name: System.Runtime.InteropServices.DllImportAttribute

--------------------
DllImportAttribute(dllName: string) : unit
Multiple items
val nativeint : value:'T -> nativeint (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.nativeint

--------------------
type nativeint = IntPtr

Full name: Microsoft.FSharp.Core.nativeint
val boyerMoore : data:nativeint * search:nativeint * datalen:int * searchlen:int -> nativeint

Full name: Script.native.boyerMoore
val data : nativeint
val search : nativeint
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
val datalen : int
val searchlen : int
val vrfy : n:nativeint -> nativeint

Full name: Script.native.vrfy
val n : nativeint
val failwith : message:string -> 'T

Full name: Microsoft.FSharp.Core.Operators.failwith
val data : GCHandle
type GCHandle =
  struct
    member AddrOfPinnedObject : unit -> nativeint
    member Equals : o:obj -> bool
    member Free : unit -> unit
    member GetHashCode : unit -> int
    member IsAllocated : bool
    member Target : obj with get, set
    static member Alloc : value:obj -> GCHandle + 1 overload
    static member FromIntPtr : value:nativeint -> GCHandle
    static member ToIntPtr : value:GCHandle -> nativeint
  end

Full name: System.Runtime.InteropServices.GCHandle
GCHandle.AddrOfPinnedObject() : nativeint
val ptr : GCHandle
GCHandle.Free() : unit
val pin : data:'a array -> GCHandle

Full name: Script.native.pin
val data : 'a array
type 'T array = 'T []

Full name: Microsoft.FSharp.Core.array<_>
GCHandle.Alloc(value: obj) : GCHandle
GCHandle.Alloc(value: obj, type: GCHandleType) : GCHandle
type GCHandleType =
  | Weak = 0
  | WeakTrackResurrection = 1
  | Normal = 2
  | Pinned = 3

Full name: System.Runtime.InteropServices.GCHandleType
field GCHandleType.Pinned = 3
val search : data:byte array -> search:byte array -> nativeint

Full name: Script.native.search
val data : byte array
Multiple items
val byte : value:'T -> byte (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.byte

--------------------
type byte = Byte

Full name: Microsoft.FSharp.Core.byte
val search : byte array
val d : GCHandle
val s : GCHandle
val ret : nativeint ref
Multiple items
val ref : value:'T -> 'T ref

Full name: Microsoft.FSharp.Core.Operators.ref

--------------------
type 'T ref = Ref<'T>

Full name: Microsoft.FSharp.Core.ref<_>
val lock : lockObject:'Lock -> action:(unit -> 'T) -> 'T (requires reference type)

Full name: Microsoft.FSharp.Core.Operators.lock
property Array.Length: int
val f1 : byte []

Full name: Script.f1
namespace System.IO
type File =
  static member AppendAllLines : path:string * contents:IEnumerable<string> -> unit + 1 overload
  static member AppendAllText : path:string * contents:string -> unit + 1 overload
  static member AppendText : path:string -> StreamWriter
  static member Copy : sourceFileName:string * destFileName:string -> unit + 1 overload
  static member Create : path:string -> FileStream + 3 overloads
  static member CreateText : path:string -> StreamWriter
  static member Decrypt : path:string -> unit
  static member Delete : path:string -> unit
  static member Encrypt : path:string -> unit
  static member Exists : path:string -> bool
  ...

Full name: System.IO.File
IO.File.ReadAllBytes(path: string) : byte []
val sbox : byte []

Full name: Script.sbox
module native

from Script
val x : nativeint
val sprintf : format:Printf.StringFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf

More information

Link:http://fssnip.net/iB
Posted:10 years ago
Author:David Klein
Tags: interop , operators , native