// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r14
pushq %rbx
movq %rdi, %rbx
movq 0x210(%rdi), %r14
movl 0x7c(%rsi), %eax
cmpl $0x5, %eax
movl $0x5, %ebp
cmovll %eax, %ebp
movl %ebp, 0x2c(%r14)
movl 0x38(%rsi), %ecx
movl %ecx, 0x30(%r14)
movq $0x0, 0x30(%rsi)
testl %eax, %eax
jle 0x9313
movl %ebp, %eax
shlq $0x2, %rax
xorl %ecx, %ecx
xorl %edx, %edx
movq 0x40(%rsi,%rcx,2), %rdi
movq %rdi, 0x58(%r14,%rcx,2)
movl 0x68(%rsi,%rcx), %edi
movl %edi, 0x80(%r14,%rcx)
cmpl %edx, %edi
cmovgl %edi, %edx
addq $0x4, %rcx
cmpq %rcx, %rax
jne 0x92e3
movl %edx, %eax
leaq 0x4(,%rax,4), %rdi
jmp 0x9318
movl $0x4, %edi
movq 0x8(%rsi), %rax
movq %rax, 0x38(%r14)
movl 0x10(%rsi), %eax
movl %eax, 0x34(%r14)
movq $0x0, 0x8(%rsi)
callq 0x4040
movq %rax, 0xb0(%r14)
movslq 0x34(%r14), %rax
testq %rax, %rax
jle 0x9367
movq 0x38(%r14), %rcx
xorpd %xmm1, %xmm1
xorl %edx, %edx
movl (%rcx,%rdx,4), %esi
xorps %xmm0, %xmm0
cvtsi2sd %rsi, %xmm0
addsd %xmm0, %xmm1
incq %rdx
cmpq %rdx, %rax
jne 0x934e
jmp 0x936b
xorpd %xmm1, %xmm1
imull $0x3e8, %eax, %eax      # imm = 0x3E8
movl 0x34(%r14), %edi
testl %edi, %edi
jle 0x93ce
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd %xmm1, %xmm0
movq 0x38(%r14), %rdx
xorl %esi, %esi
xorpd %xmm1, %xmm1
xorl %ecx, %ecx
movl (%rdx,%rsi,4), %edi
xorps %xmm2, %xmm2
cvtsi2sd %rdi, %xmm2
mulsd %xmm0, %xmm2
addsd %xmm1, %xmm2
cvttsd2si %xmm2, %rdi
movl %edi, %r8d
xorps %xmm1, %xmm1
cvtsi2sd %r8, %xmm1
subsd %xmm1, %xmm2
movl %edi, (%rdx,%rsi,4)
addl %edi, %ecx
incq %rsi
movslq 0x34(%r14), %rdi
movapd %xmm2, %xmm1
cmpq %rdi, %rsi
jl 0x9390
jmp 0x93d0
xorl %ecx, %ecx
subl %ecx, %eax
jle 0x93df
movq 0x38(%r14), %rcx
movslq %edi, %rdx
addl %eax, -0x4(%rcx,%rdx,4)
movl 0x208(%rbx), %eax
movl %eax, %ecx
shrl $0x1c, %ecx
andl $0x4, %ecx
incl %ecx
btl $0x8, %eax
movl $0x5, %eax
cmovbl %ecx, %eax
cmpl %ebp, %eax
cmovgel %ebp, %eax
movl %eax, 0x28(%r14)
movq $-0x1, 0xe0(%r14)
movl $0x87654321, 0xc4(%r14) # imm = 0x87654321
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x40(%r14)
callq 0x9442
movq %rbx, %rdi
callq 0x94de
movq (%rbx), %rax
movq %rbx, %rdi
popq %rbx
popq %r14
popq %rbp
jmpq *0x40(%rax)