Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Machine code functions: Bit Wizardry


  • Please log in to reply
144 replies to this topic
SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Very kind of you. :D
Thank you Sir.

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 ?

:)

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
cdecl, and no return value.
DllCall(&Code, Str,NewStr, Str,OldStr, UInt,15+1, "CDECL")


SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Thanks for the clarification :)

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
@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.

:)

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
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?

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Sir, this is my project:

how does one create an image file library? posted by SKAN
Currently, I have made it too easy and so I want to complicate it..

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 ?

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.

:)

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
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:
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:
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:
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")
}


SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
:D:D:D

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

:)

guest3456
  • Members
  • 1704 posts
  • Last active: Nov 19 2015 11:58 AM
  • Joined: 10 Mar 2011
i thought i remembered seeing a very basic memcmp function and C code posted by Laszlo in this thread, but i cannot seem to find it. does anyone have it?

Cerro_Azul
  • Members
  • 4 posts
  • Last active: May 29 2015 10:58 PM
  • Joined: 24 Mar 2015

Hola me pueden dar un ejemplo practico quiero convertir este c√≥digo sacado de Cheat Engine y quiero usarlo en AutoHotkey sirve para aumentar las municiones de 1 en 1

 

[ENABLE]
//activar Aunmentar municion de uno en uno
alloc(newmem,2048)
label(returnhere)
label(originalcode)
label(exit)
 
newmem:
//place your code here
 
originalcode:
inc [ecx]
mov edx,[ebp+08]
 
exit:
jmp returnhere
 
00DA8599:
jmp newmem
returnhere:
 
 
 
 
[DISABLE]
//desactivar Aunmentar municion de uno en uno
dealloc(newmem)
00DA8599:
mov [ecx],eax
mov edx,[ebp+08]
//Alt: db 89 01 8B 55 08