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 copy_s (p : byte array) =
    let ptr = Marshal.AllocHGlobal p.Length |> vrfy in
      Marshal.Copy(p,0,ptr,p.Length)
    ptr
  
  (* semantic return pointer to array *)  
  let inline (~~) (data : byte array)  = copy_s data 
  let inline (!~) (n : nativeint)      = Marshal.FreeHGlobal n

  (* cant get rid of this unless we free inside the native interface (bad idea)  *)
  let search data search =
    let pData,pSearch = ~~data, ~~search
    let result = boyerMoore(pData,pSearch,data.Length,search.Length)
    !~pData;!~pSearch
    result
    
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 copy_s : p:byte array -> nativeint

Full name: Script.native.copy_s
val p : 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
type 'T array = 'T []

Full name: Microsoft.FSharp.Core.array<_>
val ptr : nativeint
type Marshal =
  static val SystemDefaultCharSize : int
  static val SystemMaxDBCSCharSize : int
  static member AddRef : pUnk:nativeint -> int
  static member AllocCoTaskMem : cb:int -> nativeint
  static member AllocHGlobal : cb:nativeint -> nativeint + 1 overload
  static member AreComObjectsAvailableForCleanup : unit -> bool
  static member BindToMoniker : monikerName:string -> obj
  static member ChangeWrapperHandleStrength : otp:obj * fIsWeak:bool -> unit
  static member CleanupUnusedObjectsInCurrentContext : unit -> unit
  static member Copy : source:int[] * startIndex:int * destination:nativeint * length:int -> unit + 15 overloads
  ...

Full name: System.Runtime.InteropServices.Marshal
Marshal.AllocHGlobal(cb: int) : nativeint
Marshal.AllocHGlobal(cb: nativeint) : nativeint
property Array.Length: int
Marshal.Copy(source: nativeint, destination: nativeint [], startIndex: int, length: int) : unit
   (+0 other overloads)
Marshal.Copy(source: nativeint, destination: byte [], startIndex: int, length: int) : unit
   (+0 other overloads)
Marshal.Copy(source: nativeint, destination: float [], startIndex: int, length: int) : unit
   (+0 other overloads)
Marshal.Copy(source: nativeint, destination: float32 [], startIndex: int, length: int) : unit
   (+0 other overloads)
Marshal.Copy(source: nativeint, destination: int64 [], startIndex: int, length: int) : unit
   (+0 other overloads)
Marshal.Copy(source: nativeint, destination: int16 [], startIndex: int, length: int) : unit
   (+0 other overloads)
Marshal.Copy(source: nativeint, destination: char [], startIndex: int, length: int) : unit
   (+0 other overloads)
Marshal.Copy(source: nativeint, destination: int [], startIndex: int, length: int) : unit
   (+0 other overloads)
Marshal.Copy(source: nativeint [], startIndex: int, destination: nativeint, length: int) : unit
   (+0 other overloads)
Marshal.Copy(source: byte [], startIndex: int, destination: nativeint, length: int) : unit
   (+0 other overloads)
val data : byte array
Marshal.FreeHGlobal(hglobal: nativeint) : unit
val search : data:byte array -> search:byte array -> nativeint

Full name: Script.native.search
val search : byte array
val pData : nativeint
val pSearch : nativeint
val result : nativeint
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