I`m trying to use qsort, but it is crashing, because the internal pointers of the comparefunction are not being correctly achieved.
; equates usedof the structure DFMScript
[DFMScript.pLabelNameDis 0
DFMScript.pSigLabelDis 4
DFMScript.pExtentionDis 8
DFMScript.pSignatureDataLenDis 12
DFMScript.pSignatureDataDis 16
DFMScript.pSigSizeDis 20]
[Size_of_DFMScript 24]
[ScriptData2: D$ 0] ; <----- This is a pointer to the actual file data. It is zero only before precomputing the scrypt...but, below, the pointer is already retrieved.
c_call 'msvcrt.qsort' D$ScriptData2, 9, Size_of_DFMScript, CompareFunc
Proc CompareFunc:
Arguments @Arg1, @Arg2
Uses esi, edi, edx, esi
mov ecx D@Arg1
mov edx D@Arg2
mov esi ecx | mov ebx D$esi+DFMScript.pSignatureDataDis | add ebx D$ScriptData2 | move edi D$esi+DFMScript.pSigSizeDis
call BinaryDecode ebx, edi, DecodedData1
mov esi edx | mov ebx D$esi+DFMScript.pSignatureDataDis | add ebx D$ScriptData2 | mov ecx D$esi+DFMScript.pSigSizeDis
call BinaryDecode ebx, ecx, DecodedData2
If ecx > edi;D@DataLen2
mov ecx edi;D@DataLen2
End_If
c_call 'msvcrt._mbsnbcmp' DecodedData1, DecodedData2, ecx
; To sort an array in decreasing order, reverse the sense of
; greater than and less than in the comparison function :
;
neg eax
L0:
EndSTD
Proc BinaryDecode:
Arguments @Input, @InputSize, @Output
Uses esi, edi, ecx, ebx, edx
; EncodeSignature
mov esi D@Input
mov edi D@Output
mov ebx D@InputSize
;..While B$esi <> 0
..While ebx <> 0
.If B$esi = SIG_TYPE_BYTE
inc esi
movsb
dec ebx
.Else_If B$esi = SIG_TYPE_WORD
inc esi
movsw
sub ebx 2
.Else_If B$esi = SIG_TYPE_DWORD
inc esi
movsd
sub ebx 4
.Else_If B$esi = SIG_TYPE_REPEAT_BYTES
;mov eax eax
inc esi
movzx ecx B$esi | inc esi | mov edx ecx
movzx eax B$esi | inc esi
rep stosb
sub ebx edx
.Else_If B$esi = SIG_TYPE_BYPASS_BYTES
inc esi
movzx ecx B$esi | inc esi | mov edx ecx
mov al 0FF
rep stosb
sub ebx edx
.Else
; error when decoding
mov eax eax
.End_If
..End_While
EndP
I have no idea why it is crashing.
The structure is 24 bytes long, and the file contains 9 structures. So it should suppose to work, but....it isn´t :(
I have no idea what is happenning. The debugger shows me that on the 1st loop of the call back CompareFunc, Arg1 and Arg2 points inside the strcture array....but, on the 2nd loop Arg1 points outside the structure array :(:(:(
Btw: ENDSTD macro is a simple ret instruction. It is to simulate a stdcall function return