AutoHotkey Community

It is currently May 26th, 2012, 4:30 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 356 posts ]  Go to page Previous  1 ... 7, 8, 9, 10, 11, 12, 13 ... 24  Next
Author Message
 Post subject:
PostPosted: March 7th, 2008, 2:26 am 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8775
Very kind of you. :D
Thank you Sir.

Code:
MCode( Code, "558becff4d108b4508740e8b4d0c8a1188104041ff4d1075f5c600005dc3" )
OldStr := "The Quick Brown Fox Jumps Over The Lazy Dog"
VarSetCapacity( NewStr,15+1 )
Result := DllCall( &Code, Str,NewStr, Str,OldStr, UInt,15+1 )

MsgBox, % NewStr "`n" Result "`n" Errorlevel


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")
}



Why do I get an errorlevel A12 ?

:)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 7th, 2008, 4:52 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
cdecl, and no return value.
Code:
DllCall(&Code, Str,NewStr, Str,OldStr, UInt,15+1, "CDECL")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 7th, 2008, 8:53 am 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8775
Thanks for the clarification :)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 9th, 2008, 1:01 am 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8775
@Laszlo:

Sir, I tried your CRC32 function, it is very nice, but looks much complicated for my requirement.
My current project needs to deal with checksum of files.
Is there some kind of checksum method where there is no need for lookup table ?
Maybe CRC16 ? Any kind of checksum computation that is very short and really fast! I will be happy if you can give me your own version too.

Please help.

:)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 9th, 2008, 1:58 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
CRC32 can be implemented without lookup tables. It is 2..4 times slower, but still pretty fast. If you want 32-bit checksums, it can be calculated with a very short function, much faster than CRC32. The drawback is that some small cahnges (like replacing i1 with j0 in program files) do not change the checksum, so there will be many collisions: different files with the same checksum. Which way do you want to go?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 9th, 2008, 2:18 am 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8775
Sir, this is my project:



Currently, I have made it too easy and so I want to complicate it..

Laszlo wrote:
CRC32 can be implemented without lookup tables. It is 2..4 times slower, but still pretty fast.


for 50 files @ 2MB each, how much time would it take for 50 Checksums ?

Quote:
there will be many collisions: different files with the same checksum. Which way do you want to go?


Sir, I want to do only one DllCall() to keep my code compact, lookup table or not.
I look forward to less collisions as I will also be using it on EXE files.

I keep my fingers crossed.

:)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 9th, 2008, 3:27 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
UNTESTED: Here is a very short CRC32 function (could be made slightly faster, but longer). It does not use tables. The C source code is:
Code:
unsigned int CRC32(unsigned char *data, int len, unsigned int result) {
    int i,j; unsigned char octet; // INIT: result = -1, CONT: ~last_result
   
    for (i=0; i<len; i++) {
        octet = *(data++);
        for (j=0; j<8; j++) {
            if ((octet >> 7) ^ (result >> 31))
                result = (result << 1) ^ 0x04c11db7;
            else
                result = (result << 1);
            octet <<= 1;
        }
    }
    return ~result;
}

A possible AHK wrapper:
Code:
CRC32(ByRef Buffer, Bytes=0, Start=-1) {
   Static f
   If f =
      MCode(f,"558bec8b450c85c07e3a8b5508535689450c8b4510578a0a6a08425e8bf80fb6d9c1e"
. "f1833fb03c0f7c780ffffff740535b71dc10402c94e75e2ff4d0c75d75f5e5beb038b4510f7d05dc3")
   If Bytes <= 0
      Bytes := StrLen(Buffer)
   Return DllCall(&f, "uint",&Buffer, "uint",Bytes, "int",Start, "cdecl uint")
}

It does not have the length field auto-attached to the input (no padding), but should be OK for home use. You can continue the CRC calculation for other parts of a large data set, if you set the Start parameter the bitwise complement of the result of the previous part. Test it with:
Code:
SetFormat Integer, HEX
a = ABCDEFGHIJKLMNOPQRSTUVWXYZ
MsgBox % CRC32(a)

CRC32(ByRef Buffer, Bytes=0, Start=-1) {
   Static f
   If f =
      MCode(f,"558bec8b450c85c07e3a8b5508535689450c8b4510578a0a6a08425e8bf80fb6d9c1e"
. "f1833fb03c0f7c780ffffff740535b71dc10402c94e75e2ff4d0c75d75f5e5beb038b4510f7d05dc3")
   If Bytes <= 0
      Bytes := StrLen(Buffer)
   Return DllCall(&f, "uint",&Buffer, "uint",Bytes, "int",Start, "cdecl uint")
}

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")
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 9th, 2008, 4:12 am 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8775
:D:D:D

That was very fast .. Many thanks Sir.
I will try it and revert back.

:)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 9th, 2008, 7:57 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
[TESTED] Here is a CRC32 version, which does not invert the result, but lets you chose the polynomial (as a UInt value in parameter #4). Its standard value is 0x04C11DB7.
Code:
typedef long Int32;
typedef unsigned long UInt32;
typedef unsigned char UInt8;

// Not reversed: data bytes, result. Implied poly.bit32 = 1
UInt32 CRC32(UInt8 *data, Int32 len, UInt32 crc, UInt32 poly) {
    int i,j; unsigned char octet; // INIT: crc = -1, CONT: last_crc
   
    for (i=0; i<len; i++) {
        octet = *data++;
        for (j=0; j<8; j++) {
            if ((octet >> 7) ^ (crc >> 31)) // unsigned shift by wordlen-1: MS bit
                crc = (crc << 1) ^ poly;
            else
                crc = (crc << 1);
            octet <<= 1; // next bit to MS
        }
    }
    return crc;
}

Note that this function slightly differs from the IEEE standard CRC32: the result is not inverted, the data bytes and the final result are not reversed. This makes the code simpler, but the computed CRC different. It is equally good for checking if binary data are different.

The CRC calculation can be continued by giving the result of the last call the start value (parameter #3) for the next call. The very first call in a sequence normally sets it to -1, but other values are OK, too. The first parameter is the address of the data, either “UInt,&var”, “UInt,address” or “Str”,AHK_String”. Parameter #2 is the number of bytes to be processed.

You can use this function without a wrapper, too:
Code:
SetFormat Integer, HEX

MCode(CRC,"558bec8b450c85c07e388b5508535689450c8b4510578a0a6a08425e8bf80fb6d9c"
. "1ef1833fb03c0f7c780ffffff740333451402c94e75e4ff4d0c75d95f5e5b5dc38b45105dc3")

a = ABCDEFGHIJKLMNOPQRSTUVWXYZ ; binary buffer
MsgBox % DllCall(&CRC, uint,&a, uint,26, int,-1, uint,0x04C11DB7, "cdecl uint")

r1 :=    DllCall(&CRC, str,"ABCDEFGHIJKLM", uint,13, int,-1, uint,0x04C11DB7, "cdecl uint")
MsgBox % DllCall(&CRC, str,"NOPQRSTUVWXYZ", uint,13, int,r1, uint,0x04C11DB7, "cdecl uint")


You can compare the results (inverting it with MsgBox % ~DllCall…) to other implementations. Set CRC-32, but uncheck “reverse data bytes” and “reverse CRC result before Final XOR”, and use “direct” mode.

Edit: added missing ".." (thanks to SKAN)


Last edited by Laszlo on March 10th, 2008, 12:16 am, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 9th, 2008, 8:58 pm 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8775
Thanks again Sir.. also for giving me tested code. I am not good at testing this. I am taking your code for granted and implementing it in my function. I am happy it is 4 bytes short.

:)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 9th, 2008, 10:18 pm 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8775
What does this magic number denote ? : 0x04C11DB7 :roll:

.. and does not cdecl uint need to wrapped with quotes ?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 10th, 2008, 12:14 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
SKAN wrote:
What does this magic number denote ? : 0x04C11DB7?
It is the bit pattern of the coefficients of the standard IEEE CRC-32 binary irreducible polynomial (x**32 is implicit). You can use here any other polynomial of this type (Castagnoli: 0x1EDC6F41, Koopman: 0x741B8CD7), producing other CRC values.

SKAN wrote:
and does not cdecl uint need to wrapped with quotes ?
Yes, thanks. (The dll call works without it, but returns A16 in errorlevel.)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 10th, 2008, 12:47 am 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8775
Thanks for the clarification sir. :)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 14th, 2008, 3:25 pm 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8775
@Laszlo

Dear Sir, :)

I need mcode for

1) binary string copy ( using parameters similar to lstrcpyn and functionality of RtlMoveMemory )
2) binary string compare
3) any encryption function like tea or rc4 that gives ( returns result as pointer to binary )

I am thinking about writing an ahk code obfuscator.
I am not in a hurry ( will not be in town for 2 days ) but please do help when/if you have the time.

Regards, :)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 14th, 2008, 7:09 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Olfen already did the work for XTEA.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 356 posts ]  Go to page Previous  1 ... 7, 8, 9, 10, 11, 12, 13 ... 24  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], Yahoo [Bot] and 12 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