This is a Simple program that is used to change ASCII HEX to BIN HEX.. Sometimes i find code snipets online that are in ASCII HEX and want them into something i can use..

I added in support to check for Tabs,return valuse and spaces..

its also based on a old template that should be revamped for multi file support..


.386
.model flat,stdcall
option casemap:none

Main PROTO
INCLUDE \masm32\include\windows.inc
INCLUDE \masm32\include\user32.inc
INCLUDE \masm32\include\kernel32.inc
INCLUDE \masm32\include\comdlg32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\comdlg32.lib

.const
MAXSIZE equ 00FFFFh
.data
fintext db "Job Complete",0
fincap db "Job Finished",0
AppName db "Generic Decryptor",0
FilterString db "All Files",0,"*.*",0,0
buffer db MAXSIZE dup(0)
FILENAME db 250 dup(0)
Path db 250 dup(0)
Problem db "Not Successfull",0

.data?
hInstance HINSTANCE ?
hFileRead HANDLE ?
pMemory DWORD ?
hMemory DWORD ?
pMemory2 DWORD ?
hMemory2 DWORD ?
SizeWritten DWORD ?
ofn OPENFILENAME <>
FileSizeS LARGE_INTEGER <>
FileSizeU DWORD ?
PATHSIZE DWORD ?
NUMBEROFFILES DWORD ?
PASSES DWORD ?
FileNamePointer DWORD ?
PATHSIZEVAR DWORD ?
BLANK db 64 dup(?)
ENDOFFILENAME DWORD ?
FileSizeU2 dd ?
CorrectError dd ?

.CODE
Program:
INVOKE GetModuleHandle,NULL
mov hInstance,EAX
INVOKE Main
INVOKE ExitProcess,NULL
ret

Main proc
mov ofn.lStructSize,SIZEOF ofn
push hInstance
pop ofn.hInstance
mov ofn.lpstrFilter,OFFSET FilterString
mov ofn.lpstrFile,OFFSET buffer
mov ofn.nMaxFile,MAXSIZE
mov ofn.Flags,OFN_FILEMUSTEXIST or OFN_PATHMUSTEXIST or OFN_LONGNAMES or \
OFN_EXPLORER or OFN_HIDEREADONLY or OFN_ALLOWMULTISELECT
INVOKE GetOpenFileName,ADDR ofn
.IF eax!=TRUE
INVOKE MessageBox,NULL,ADDR Problem,ADDR AppName,MB_OK
ret
.ENDIF

mov eax,OFFSET buffer
mov ebp,OFFSET Path
mov ebx,1

move:
mov cl,BYTE PTR
mov BYTE PTR ,cl
.IF cl!=00
inc eax
inc ebp
inc ebx
jmp move
.ENDIF
mov PATHSIZE,EBX
mov PATHSIZEVAR,EBX
inc ebx
mov FileNamePointer,EBX
inc eax
xor ebx,EBX
xor ecx,ECX
COUNTFILES:
mov cl,BYTE PTR
.IF cl==00
inc ebx
inc eax
mov cl,BYTE PTR
.IF cl==00
mov NUMBEROFFILES,EBX
.IF EBX==01
xor EBP,EBP
mov eax,OFFSET Path
mov ebx,OFFSET FILENAME
looop:
.IF EBP>=250
mov eax,OFFSET FILENAME
EOFLOP:
mov cl,BYTE PTR
.IF cl==00
mov ENDOFFILENAME,eax
jmp SINGLE
.ENDIF
inc eax
jmp EOFLOP
.ENDIF
mov CL,BYTE PTR
mov BYTE PTR ,CL
INC EAX
INC EBX
INC EBP
jmp looop
.ENDIF
mov PASSES,0
jmp WORK
.ENDIF
.ENDIF
inc eax
jmp COUNTFILES
WORK:
mov eax,NUMBEROFFILES
.IF PASSES>=EAX
JMP DONE
.ENDIF
add PASSES,1
mov eax,OFFSET Path
mov ebx,OFFSET FILENAME
xor EBP,EBP
looop2:
.IF EBP>=250
JMP loop2done
.ENDIF
mov CL,BYTE PTR
mov BYTE PTR ,CL
INC EAX
INC EBX
INC EBP
jmp looop2
loop2done:
xor ebp,ebp
mov eax,OFFSET FILENAME
add eax,PATHSIZE
dec eax
mov BYTE PTR ,05ch
inc eax
mov ebx,OFFSET buffer
add ebx,PATHSIZEVAR
filenamesetup:
mov cl,BYTE PTR
mov BYTE PTR ,cl
.IF cl==00
inc ebp
add PATHSIZEVAR,ebp
mov ENDOFFILENAME,EAX
jmp SINGLE
.ENDIF
inc eax
inc ebx
inc ebp
jmp filenamesetup
SINGLE:
;INVOKE MessageBox,NULL,ADDR FILENAME,ADDR AppName,MB_OK
INVOKE CreateFile,ADDR FILENAME,GENERIC_READ or GENERIC_WRITE,NULL,NULL,\
OPEN_EXISTING,NULL,NULL
mov hFileRead,eax
INVOKE GetFileSize,hFileRead,addr FileSizeS
mov FileSizeU,eax
INVOKE GlobalAlloc,GMEM_MOVEABLE or GMEM_ZEROINIT,FileSizeU
mov hMemory,eax
INVOKE GlobalLock,hMemory
mov pMemory,eax

xor eax,eax

mov eax,FileSizeU
mov ecx,02h
div ecx
add eax,edx

mov FileSizeU2,eax
INVOKE GlobalAlloc,GMEM_MOVEABLE or GMEM_ZEROINIT,FileSizeU2
mov hMemory2,eax
INVOKE GlobalLock,hMemory2
mov pMemory2,eax
INVOKE ReadFile,hFileRead,pMemory,FileSizeU,addr SizeWritten,NULL
INVOKE CloseHandle,hFileRead
mov edi,pMemory
mov esi,pMemory2
mov ebx,00h
mov ecx,00h

Loop_1:
cmp byte ptr ,20h
je Num_3_1
cmp byte ptr ,09h
je Num_3_1
cmp word ptr ,0a0dh
je Num_3
mov al, byte ptr
cmp al,41h ;A
je A_h
cmp al,42h ;B
je B_h
cmp al,43h ;C
je C_h
cmp al,44h ;D
je D_h
cmp al,45h ;E
je E_h
cmp al,46h ;F
je F_h

sub al,30h
jmp Num_1

A_h:
mov al,0Ah
jmp Num_1
B_h:
mov al,0Bh
jmp Num_1
C_h:
mov al,0Ch
jmp Num_1
D_h:
mov al,0Dh
jmp Num_1
E_h:
mov al,0Eh
jmp Num_1
F_h:
mov al,0Fh
jmp Num_1

Num_1:
shl al,04h
mov cl,al
inc edi
inc ebx

Loop_2:
cmp byte ptr ,20h
je Num_4_1
cmp byte ptr ,09h
je Num_4_1
cmp word ptr ,0a0dh
je Num_4
mov al, byte ptr
cmp al,41h ;A
je A2_h
cmp al,42h ;B
je B2_h
cmp al,43h ;C
je C2_h
cmp al,44h ;D
je D2_h
cmp al,45h ;E
je E2_h
cmp al,46h ;F
je F2_h

sub al,30h
jmp Num_2

A2_h:
mov al,0Ah
jmp Num_2
B2_h:
mov al,0Bh
jmp Num_2
C2_h:
mov al,0Ch
jmp Num_2
D2_h:
mov al,0Dh
jmp Num_2
E2_h:
mov al,0Eh
jmp Num_2
F2_h:
mov al,0Fh
jmp Num_2

Num_2:
add cl,al
mov byte ptr ,cl
inc edi
inc esi
inc ebx
cmp ebx,FileSizeU
jne Loop_1
jmp nextfile

Num_3:
dec FileSizeU2
add edi,02h
add ebx,02h
cmp ebx,FileSizeU
jne Loop_1
jmp nextfile

Num_3_1:
inc CorrectError
inc edi
inc ebx
cmp ebx,FileSizeU
jne Loop_1
jmp nextfile

Num_4:
dec FileSizeU2
add edi,02h
add ebx,02h
cmp ebx,FileSizeU
jne Loop_2
jmp nextfile

Num_4_1:
inc CorrectError
inc edi
inc ebx
cmp ebx,FileSizeU
jne Loop_2
jmp nextfile


nextfile:
xor eax,eax
xor edx,edx
xor ecx,ecx

mov eax,CorrectError
mov ecx,02h
div ecx
add eax,edx
sub FileSizeU2,eax
INVOKE CreateFile,ADDR FILENAME,GENERIC_READ or GENERIC_WRITE,NULL,NULL,\
CREATE_ALWAYS,NULL,NULL
exit:
mov hFileRead,eax
INVOKE WriteFile,hFileRead,pMemory2,FileSizeU2,addr SizeWritten,NULL
INVOKE CloseHandle,hFileRead
INVOKE GlobalUnlock,pMemory
INVOKE GlobalFree,hMemory
INVOKE GlobalUnlock,pMemory2
INVOKE GlobalFree,hMemory2
.IF NUMBEROFFILES==01
JMP DONE
.ENDIF
JMP WORK
DONE:
invoke MessageBoxA,NULL,addr fintext,addr fincap,MB_OK
INVOKE PostQuitMessage,NULL
invoke ExitProcess,NULL
Main endp
END Program
Posted on 2003-09-06 20:48:44 by devilsclaw
Have a look at the following:

http://www.asmcommunity.net/board/index.php?topic=4362&highlight=Hex+string+mmx

Oh yes a good snipplet to test for range is the following:


lea ecx, [eax-LOWERLIMIT]
cmp ecx, UPPERLIMIT-LOWERLIMIT
jna _in_range ; ja _notinrange
Posted on 2003-09-06 22:21:42 by roticv