 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Fri Mar 07, 2008 1:26 am Post subject: |
|
|
Very kind of you.
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 ?
 |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4710 Location: Boulder, CO
|
Posted: Fri Mar 07, 2008 3:52 am Post subject: |
|
|
cdecl, and no return value.
| Code: | | DllCall(&Code, Str,NewStr, Str,OldStr, UInt,15+1, "CDECL") |
|
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Fri Mar 07, 2008 7:53 am Post subject: |
|
|
Thanks for the clarification  |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Sun Mar 09, 2008 12:01 am Post subject: |
|
|
@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.
 |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4710 Location: Boulder, CO
|
Posted: Sun Mar 09, 2008 12:58 am Post subject: |
|
|
| 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? |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Sun Mar 09, 2008 1:18 am Post subject: |
|
|
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.
 |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4710 Location: Boulder, CO
|
Posted: Sun Mar 09, 2008 2:27 am Post subject: |
|
|
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")
} |
|
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Sun Mar 09, 2008 3:12 am Post subject: |
|
|
 
That was very fast .. Many thanks Sir.
I will try it and revert back.
 |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4710 Location: Boulder, CO
|
Posted: Sun Mar 09, 2008 6:57 pm Post subject: |
|
|
[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 Sun Mar 09, 2008 11:16 pm; edited 1 time in total |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Sun Mar 09, 2008 7:58 pm Post subject: |
|
|
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.
 |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Sun Mar 09, 2008 9:18 pm Post subject: |
|
|
What does this magic number denote ? : 0x04C11DB7
.. and does not cdecl uint need to wrapped with quotes ? |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4710 Location: Boulder, CO
|
Posted: Sun Mar 09, 2008 11:14 pm Post subject: |
|
|
| 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.) |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Sun Mar 09, 2008 11:47 pm Post subject: |
|
|
Thanks for the clarification sir.  |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Fri Mar 14, 2008 2:25 pm Post subject: |
|
|
@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,  |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4710 Location: Boulder, CO
|
Posted: Fri Mar 14, 2008 6:09 pm Post subject: |
|
|
| Olfen already did the work for XTEA. |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|