// File: OpenFodder[P]SOS_Unpacker/capsimg/CAPSImg/CapsImage.cpp
// Function: CCapsImage::ConvertDumpInfo(CapsWH*)
void CCapsImage::ConvertDumpInfo(PCAPSWH wh)
{
	PDISKTRACKINFO pti = di.pdt;

	// use the number of revolutions that could be stored by pti at most or the number of revolutions sampled if less
	int maxrev = std::min(CAPS_MTRS, wh->trkcnt);
	pti->rawtrackcnt = maxrev;

	// save the total size of all tracks decoded; don't have to recalculate later
	pti->rawlen = wh->rawlen;

	// prevent deallocating the buffer with the track data
	wh->rawbuf = NULL;

	// longest track found
	int maxtracksize = 0;

	// copy the track buffer start positions and sizes for the decompressed stream
	for (int rev = 0; rev < maxrev; rev++) {
		pti->trackdata[rev] = wh->trkbuf[rev];
		pti->tracksize[rev] = wh->trklen[rev];

		// find the longest track
		if (pti->tracksize[rev] > maxtracksize)
			maxtracksize = pti->tracksize[rev];
	}

	// save the original decoded timing data length
	pti->rawtimebuf = wh->timbuf;
	pti->rawtimecnt = wh->timlen;

	// prevent deallocating the buffer with the timing data
	wh->timbuf = NULL;

	// allocate timing buffer the same size as the longest data track; +1 for alternate density
	pti->timebuf = new UDWORD[maxtracksize + 1];

	// calculate the sum of the timing samples
	double timesum = 0;
	for (int i = 0; i < pti->rawtimecnt; i++) {
		timesum += pti->rawtimebuf[i];
	}

	// normalize all timing values; 1000 is the default value
	// compensate for precision loss to integer values by using a remainder
	int targetsum = pti->rawtimecnt * 1000;
	int actsum = 0;
	double tconv = (double)targetsum / timesum;
	double rem = 0;
	for (int i = 0; i < pti->rawtimecnt; i++) {
		double vr = pti->rawtimebuf[i] * tconv + rem;
		UDWORD vi = (UDWORD)vr;
		rem = vr - vi;
		pti->rawtimebuf[i] = vi;
		actsum += vi;
	}

	// If there is still a difference between the targeted normalized values and the actual values
	// add the remainder to the last timing.
	// tdiff can't be more than the target since it represents the remaining sum of precision losses
	int tdiff = targetsum - actsum;
	if (tdiff > 0)
		pti->rawtimebuf[pti->rawtimecnt - 1] += tdiff;

	// generate 1 revolution of data as default, 5 for analyser
	int trackcnt = (di.flag & DI_LOCK_ANA) ? 5 : 1;

	// 5 revolutions if track update is not used
	if (!(di.flag & DI_LOCK_UPDATEFD))
		trackcnt = 5;

	// the number of revolutions can't be more than the number of track revolutions decoded
	if (trackcnt > maxrev)
		trackcnt = maxrev;

	// write track info
	pti->trackcnt = trackcnt;
	pti->overlap = -1;
	pti->overlapbit = -1;

	// reset random seed
	pti->wseed = 0x87654321;

	// request raw track updates/streaming
	pti->rawdump = 1;
	pti->rawupdate = 1;

	// initialize lookup tables used by the weak bit detector
	InitFirstBitTables();

	// identify and record weak bit areas
	FindWeakBits();

	// first image update to initialize streaming
	UpdateDump();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x78(%rbp)
movq 0x210(%rax), %rax
movq %rax, -0x18(%rbp)
movl $0x5, -0x20(%rbp)
movq -0x10(%rbp), %rsi
addq $0x7c, %rsi
leaq -0x20(%rbp), %rdi
callq 0xe0c0
movl (%rax), %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x2c(%rax)
movq -0x10(%rbp), %rax
movl 0x38(%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x30(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x30(%rax)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0xd985
movq -0x10(%rbp), %rax
movslq -0x28(%rbp), %rcx
movq 0x40(%rax,%rcx,8), %rdx
movq -0x18(%rbp), %rax
movslq -0x28(%rbp), %rcx
movq %rdx, 0x58(%rax,%rcx,8)
movq -0x10(%rbp), %rax
movslq -0x28(%rbp), %rcx
movl 0x68(%rax,%rcx,4), %edx
movq -0x18(%rbp), %rax
movslq -0x28(%rbp), %rcx
movl %edx, 0x80(%rax,%rcx,4)
movq -0x18(%rbp), %rax
movslq -0x28(%rbp), %rcx
movl 0x80(%rax,%rcx,4), %eax
cmpl -0x24(%rbp), %eax
jle 0xd978
movq -0x18(%rbp), %rax
movslq -0x28(%rbp), %rcx
movl 0x80(%rax,%rcx,4), %eax
movl %eax, -0x24(%rbp)
jmp 0xd97a
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0xd915
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x10(%rbp), %rax
movl 0x10(%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x34(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x8(%rax)
movl -0x24(%rbp), %eax
addl $0x1, %eax
cltq
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x4040
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0xb0(%rax)
xorps %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x34(%rcx), %eax
jge 0xda2a
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
movslq -0x34(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cvtsi2sd %rax, %xmm0
addsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xd9f5
movq -0x18(%rbp), %rax
imull $0x3e8, 0x34(%rax), %eax # imm = 0x3E8
movl %eax, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
cvtsi2sdl -0x38(%rbp), %xmm0
divsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x34(%rcx), %eax
jge 0xdad9
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
movslq -0x54(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cvtsi2sd %rax, %xmm0
movsd -0x48(%rbp), %xmm2
movsd -0x50(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x60(%rbp)
cvttsd2si -0x60(%rbp), %rax
movl %eax, -0x64(%rbp)
movsd -0x60(%rbp), %xmm0
movl -0x64(%rbp), %eax
cvtsi2sd %rax, %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp)
movl -0x64(%rbp), %edx
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
movslq -0x54(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x64(%rbp), %eax
addl -0x3c(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0xda5d
movl -0x38(%rbp), %eax
subl -0x3c(%rbp), %eax
movl %eax, -0x68(%rbp)
cmpl $0x0, -0x68(%rbp)
jle 0xdb06
movl -0x68(%rbp), %edx
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x18(%rbp), %rcx
movl 0x34(%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
addl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
movq -0x78(%rbp), %rax
movl 0x208(%rax), %ecx
movl %ecx, %esi
andq $0x40000000, %rsi       # imm = 0x40000000
movl $0x1, %ecx
movl $0x5, %edx
cmpq $0x0, %rsi
cmovnel %edx, %ecx
movl %ecx, -0x6c(%rbp)
movl 0x208(%rax), %eax
andq $0x100, %rax            # imm = 0x100
cmpq $0x0, %rax
jne 0xdb46
movl $0x5, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jle 0xdb54
movl -0x1c(%rbp), %eax
movl %eax, -0x6c(%rbp)
movq -0x78(%rbp), %rdi
movl -0x6c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x28(%rax)
movq -0x18(%rbp), %rax
movl $0xffffffff, 0xe0(%rax) # imm = 0xFFFFFFFF
movq -0x18(%rbp), %rax
movl $0xffffffff, 0xe4(%rax) # imm = 0xFFFFFFFF
movq -0x18(%rbp), %rax
movl $0x87654321, 0xc4(%rax) # imm = 0x87654321
movq -0x18(%rbp), %rax
movl $0x1, 0x44(%rax)
movq -0x18(%rbp), %rax
movl $0x1, 0x40(%rax)
callq 0xdbd0
movq -0x78(%rbp), %rdi
callq 0xdcf0
movq -0x78(%rbp), %rdi
movq (%rdi), %rax
callq *0x40(%rax)
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)