AutoHotkey Community

It is currently May 27th, 2012, 10:21 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 356 posts ]  Go to page Previous  1 ... 20, 21, 22, 23, 24
Author Message
 Post subject: BinCompare
PostPosted: October 12th, 2011, 3:33 pm 
Offline

Joined: May 26th, 2009, 10:32 am
Posts: 20
Hi!
I would need a very fast BinaryCompare of 2 variables represented by the address returning the offset of the first difference:

BinCompare(Pointer1,Pointer2,MaxLen)

Regards and thanks in advance
Robert


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Re: BinCompare
PostPosted: December 3rd, 2011, 7:32 am 
Offline

Joined: June 7th, 2010, 3:40 pm
Posts: 553
RoAltmann wrote:
Hi!
I would need a very fast BinaryCompare of 2 variables represented by the address returning the offset of the first difference:

BinCompare(Pointer1,Pointer2,MaxLen)

Regards and thanks in advance
Robert


I'm not sure if you still wanted this. but here it is:

Code:
MCode(BinCompare, "535556578B7C242033F633D285FF7C2C8B44241C7F0485C074228B6C24148B4C24182BE98A1C293A19751B4183C60183D2003BD77CEE7F043BF072E85F5E83C8FF5D0BD05BC35F8BC65E5D5BC3")


Var1 := "This is a text."
Var2 := "This is a test."

Ptr := A_PtrSize ? "UPtr" : "UInt"

MaxLength := VarSetCapacity(Var1) < VarSetCapacity(Var2) ? VarSetCapacity(Var2) : VarSetCapacity(Var1)

DifferentPosition := DllCall(&BinCompare,Ptr,&Var1,Ptr,&Var2,"Int64",MaxLength,"cdecl int64")

MsgBox % DifferentPosition


MCode(ByRef code, hex) { ; allocate memory and write Machine Code there
   VarSetCapacity(code,StrLen(hex)//2)
   Loop % StrLen(hex)//2
      NumPut("0x" . SubStr(hex,2*A_Index-1,2), code, A_Index-1, "Char")
}


C function:
Code:
long long int BinCompare(unsigned char *_Pointer1, unsigned char *_Pointer2, long long int _MaxLength){
   long long int LoopPosition = 0;
   
   while (LoopPosition < _MaxLength){
      if (*_Pointer1 != *_Pointer2)
         return LoopPosition;
      
      _Pointer1 ++;
      _Pointer2 ++;
      LoopPosition ++;
   }
   
   return -1;
}


If they are the same it will return -1 else it returns the offset as you wanted.

_________________
(Statistics script) M&K Counter 2.0
My FAST ini Lib
Minecraft NBT Lib


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 14th, 2012, 2:59 pm 
Offline

Joined: February 8th, 2009, 5:52 pm
Posts: 47
To the machine code champions!

So I have the below code which I would like to execute from AHK (works as a limited user, no need for admin), just for the coolness factor.

But it is not a function as such, it is in fact a complete (tiny) program that displays a visual effect. I've tried to launch it using Mcode but no success..

But if I save this as an EXE (converting to bin first) and
run it, it works fine.

Any help appreciated.

Code:
B81300CD1000C68EDA00C68EE26800A007D6BAC803EE420DC30F50C1E80AEEF6E0C1E806EE58EE4075ED0F3136668904DBE33666FF0436DB048802DE32D9EBD8C9D9FED9C1D9FED8C0D9EAD8CBD9FE648A258A05C1E804240F6428252805D510AAE2ECBFA07DBB4101FD648A14AC0000D01864001064D018E2F0F7DBFC78EBC1EB07D9C275E31E6640666BC003721866C702030000BFD80ADF02D9FAD8C9D9CAF572F3DEE2DEC278210FA01F7302D8C2D9C1D8C8D9C1D8C8DEC1D9EC7302D8CDDEF1DCCADEC97302D8C4C60237D9C0DE0ADF1B691B40017018C60242D9C1DE0ADF1B031BB2FF2A11C0EA050011F7DB00111FE292E460480F8535FFB003CD10C3


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 14th, 2012, 5:41 pm 
Offline

Joined: June 7th, 2010, 3:40 pm
Posts: 553
I don't believe it is possible to run executable files through a dllcall. Dllcall expects a function to be executed when it runs the code - not an executable.

The best you will be able to get is to save that as a exe in the temp folder and run it on command.

_________________
(Statistics script) M&K Counter 2.0
My FAST ini Lib
Minecraft NBT Lib


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Bit Wizardry and AHK_L?
PostPosted: May 2nd, 2012, 4:30 pm 
Offline

Joined: May 26th, 2009, 10:32 am
Posts: 20
Hi wizards!

Got a lot of functions from you working fine with AHK Standard.
Bit now I'm using AHK_L and the functions do not work anymore.

The example:

Code:
ScriptAHKType     := "AHK_L Unicode32" ; Info for automatic compiler to use AHKL Unicode 32 bit as default

HEX := "3000000004FC00000000028000FC008008020080000000000800444445373331615F5F5F060000076B061500020000002C38B86B3004020000000080FFFB0080FECADEFAFEAFFECA0000000074FB008000100000310412007800008003020080FECADEFAFEAFFECA00000000000000000010000019BA45610000000000000000"
VarSetCapacity(BIN,0x2000,0x41)
_Hex2Bin(&BIN,HEX)
R := _Bin2Hex(&BIN,StrLen(HEX)//2)
DoInform(HEX "`n`n" R)
ExitApp

DoInform(Msg = "")
{
  MsgBox,262208,Information, %Msg%
}

_Hex2Bin(ptr, hex) { ; Hex2Bin(fun,"8B4C24") = MCode(fun,"8B4C24")
   Static fun
   If (fun = "") {
      h:="568b74240c8a164684d2743b578b7c240c538ac2c0e806b109f6e98ac802cac0e104880f8"
       . "a164684d2741a8ac2c0e806b309f6eb80e20f02c20ac188078a16474684d275cd5b5f5ec3"
      l := StrLen(h)//2
      VarSetCapacity(fun,l)
      Loop, %l%
         NumPut("0x" . SubStr(h,2*A_Index-1,2), fun, A_Index-1, "Char")
    }
  dllcall(&fun, "Uint",ptr, "Str",hex, "cdecl")
}

_Bin2Hex(addr,len) {
   Static fun
   If (fun = "") {
      h:="8B54240C85D2568B7424087E3A53578B7C24148A07478AC8C0E90480F9090F97C3F6DB80E30702"
       . "D980C330240F881E463C090F97C1F6D980E10702C880C130880E464A75CE5F5BC606005EC3"
      l := StrLen(h)//2
      VarSetCapacity(fun,l)
      Loop, %l%
         NumPut("0x" . SubStr(h,2*A_Index-1,2), fun, A_Index-1, "Char")
   }
   VarSetCapacity(hex,2*len+1)
   dllcall(&fun, "uint",&hex, "uint",addr, "uint",len, "cdecl")
   VarSetCapacity(hex,-1) ; update StrLen
   Return hex
}

_BinByteSum(ptr,len)
{
   Static fun
   If (fun = "") {
      h :="33c03944240876108b4c24040fbe1103c241ff4c240875f4c3"
      l := StrLen(h)//2
      VarSetCapacity(fun,l)
      Loop, %l%
         NumPut("0x" . SubStr(h,2*A_Index-1,2), fun, A_Index-1, "Char")
   }

   PtrType := A_PtrSize ? "UPtr" : "UInt"
   R := dllcall(&fun,PtrType,ptr,Uint,len)
   return R
}

_BinCompare(Ptr1,Ptr2,Len)
{
   Static fun
   If (fun = "") {
      h :="535556578B7C242033F633D285FF7C2C8B44241C7F0485C074228B6C24148B4C24182BE98A1C293A19751B4183C60183D2003BD77CEE7F043BF072E85F5E83C8FF5D0BD05BC35F8BC65E5D5BC3"
      l := StrLen(h)//2
      VarSetCapacity(fun,l)
      Loop, %l%
         NumPut("0x" . SubStr(h,2*A_Index-1,2), fun, A_Index-1, "Char")
   }

   PtrType := A_PtrSize ? "UPtr" : "UInt"
   R := DllCall(&fun,PtrType,Ptr1,PtrType,Ptr2,"Int64",Len,"cdecl int64")
   return R
}


Result on AHK:
Quote:
---------------------------
Information
---------------------------
3000000004FC00000000028000FC008008020080000000000800444445373331615F5F5F060000076B061500020000002C38B86B3004020000000080FFFB0080FECADEFAFEAFFECA0000000074FB008000100000310412007800008003020080FECADEFAFEAFFECA00000000000000000010000019BA45610000000000000000

3000000004FC00000000028000FC008008020080000000000800444445373331615F5F5F060000076B061500020000002C38B86B3004020000000080FFFB0080FECADEFAFEAFFECA0000000074FB008000100000310412007800008003020080FECADEFAFEAFFECA00000000000000000010000019BA45610000000000000000
---------------------------
OK
---------------------------


Result on AHK_L:
Quote:
---------------------------
Information
---------------------------
3000000004FC00000000028000FC008008020080000000000800444445373331615F5F5F060000076B061500020000002C38B86B3004020000000080FFFB0080FECADEFAFEAFFECA0000000074FB008000100000310412007800008003020080FECADEFAFEAFFECA00000000000000000010000019BA45610000000000000000

〳ㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴ
---------------------------
OK
---------------------------



Could anybody help me?


Report this post
Top
 Profile  
Reply with quote  
PostPosted: May 2nd, 2012, 6:43 pm 
Offline

Joined: March 27th, 2008, 2:14 pm
Posts: 700
If you're using unicode ahk, _Bin2Hex is broken.

Use something like this instead: (untested)
Code:
_Bin2Hex(addr, len) {
    loop %len%
        ret .= SubStr("00" NumGet(addr+0, A_Index-1, "UChar"), -1)
    return ret
}


Report this post
Top
 Profile  
Reply with quote  
PostPosted: May 2nd, 2012, 6:57 pm 
Offline

Joined: May 26th, 2009, 10:32 am
Posts: 20
I used _Bin2Hex because it much faster than a simple ahk solution.


Report this post
Top
 Profile  
Reply with quote  
PostPosted: May 2nd, 2012, 7:14 pm 
Offline

Joined: March 27th, 2008, 2:14 pm
Posts: 700
If you were a sane person you would translate your machine-code function (I'm assuming that's what it is) from hex to binary ONLY ONCE, at the START of your script, and therefore there's no flipping difference unless your machine code function is in the megabytes.

However if your hex data is not a machine code function, but some dynamically read/written data, then you can use this to translate from ASCII (which _Bin2Hex assumes) to UTF-16 (which is used by AHK_L Unicode)

StrGet()
Code:
msgbox % StrGet(addr, len, "")


Report this post
Top
 Profile  
Reply with quote  
PostPosted: May 2nd, 2012, 7:53 pm 
Offline

Joined: May 26th, 2009, 10:32 am
Posts: 20
Im Using this functions to read/write/find/compare data from and to Intelhex and Motorola S19-Files.
For manipulation an for streaming and jumping within the data and driven by pointers within the data, the data is needed in binary.
So I want to convert HexStrings from ASCII files to binary-Arrays up to 8MByte and after binary changes write them back to ASCII files.

Is there anybody knowing how to change Bin2Hex,Hex2Bin,BinCompare to work with AHK_L Unicode?

Regards
Robert


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Re:
PostPosted: May 7th, 2012, 12:05 pm 
Offline

Joined: May 26th, 2009, 10:32 am
Posts: 20
TheGood wrote:
Here is a version of Bin2Hex which works on AHK_L x86/x64 ANSI/Unicode and vanilla AHK:

Code:
;By Laszlo
;http://www.autohotkey.com/forum/viewtopic.php?p=135522#135522
Bin2Hex(addr,len) {
    Static fun, ptr
    If (fun = "") {
        If A_IsUnicode
            If (A_PtrSize = 8)
                h=4533c94c8bd14585c07e63458bd86690440fb60248ffc2418bc9410fb6c0c0e8043c090fb6c00f97c14180e00f66f7d96683e1076603c8410fb6c06683c1304180f8096641890a418bc90f97c166f7d94983c2046683e1076603c86683c13049ffcb6641894afe75a76645890ac366448909c3
            Else h=558B6C241085ED7E5F568B74240C578B7C24148A078AC8C0E90447BA090000003AD11BD2F7DA66F7DA0FB6C96683E2076603D16683C230668916240FB2093AD01BC9F7D966F7D96683E1070FB6D06603CA6683C13066894E0283C6044D75B433C05F6689065E5DC38B54240833C966890A5DC3
        Else h=558B6C241085ED7E45568B74240C578B7C24148A078AC8C0E9044780F9090F97C2F6DA80E20702D1240F80C2303C090F97C1F6D980E10702C880C1308816884E0183C6024D75CC5FC606005E5DC38B542408C602005DC3
        VarSetCapacity(fun, StrLen(h) // 2)
        Loop % StrLen(h) // 2
            NumPut("0x" . SubStr(h, 2 * A_Index - 1, 2), fun, A_Index - 1, "Char")
        ptr := A_PtrSize ? "Ptr" : "UInt"
        DllCall("VirtualProtect", ptr, &fun, ptr, VarSetCapacity(fun), "UInt", 0x40, "UInt*", 0)
    }
    VarSetCapacity(hex, A_IsUnicode ? 4 * len + 2 : 2 * len + 1)
    DllCall(&fun, ptr, &hex, ptr, addr, "UInt", len, "CDecl")
    VarSetCapacity(hex, -1) ; update StrLen
    Return hex
}

It is based on Laszlo's fastest implementation and is optimized for speed.


Great! Could you do this for all other functions like bin2hex, findbin, bincompare, binbytesum, ..., too?


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Re:
PostPosted: May 11th, 2012, 4:14 am 
TheGood wrote:
Yeah, you're right. I did a bit more testing today (this time with MulDiv), and it seems like _MulDiv is the fastest in mainstream AHK, while "MulDiv" is fastest in AHK_L by a slight margin.

Code:
_MulDiv := DllCall("GetProcAddress", "uint", DllCall("GetModuleHandle", "str", "kernel32"), A_IsUnicode ? "astr" : "str", "MulDiv")
MCode(MulDiv, "FF742410FF742410FF742410FF542410C3")

AHK:
                           1        2        3  average
DllCall("MulDiv") = 5551.670 7609.345 7494.034 6885.016 ms
DllCall(_MulDiv)  = 1992.712 1683.229 1991.623 1889.188 ms
DllCall(&MulDiv)  = 5125.352 5154.847 5146.098 5142.099 ms

AHK_L
                           1        2        3  average
DllCall("MulDiv") = 1532.916 1595.839 1506.173 1544.976 ms
DllCall(_MulDiv)  = 1559.126 1659.080 1563.747 1593.984 ms
DllCall(&MulDiv)  = 2899.750 3738.775 3769.903 3469.476 ms


&MulDiv and _MulDiv is direct address. Why is &MulDiv much slower than _MulDiv in AHK_L?


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 356 posts ]  Go to page Previous  1 ... 20, 21, 22, 23, 24

All times are UTC [ DST ]


Who is online

Users browsing this forum: XX0 and 26 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group