// File: /workspace/github_cpp_makefile_star2_O3/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 %r15
pushq %r14
pushq %r12
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 0x90ab
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 0x907b
movl %edx, %eax
leaq 0x4(,%rax,4), %rdi
jmp 0x90b0
movl $0x4, %edi
movq 0x8(%rsi), %r15
movq %r15, 0x38(%r14)
movslq 0x10(%rsi), %r12
movl %r12d, 0x34(%r14)
movq $0x0, 0x8(%rsi)
callq 0x4040
movq %rax, 0xb0(%r14)
testq %r12, %r12
jle 0x9155
xorpd %xmm1, %xmm1
xorl %eax, %eax
movl (%r15,%rax,4), %ecx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
addsd %xmm0, %xmm1
incq %rax
cmpq %rax, %r12
jne 0x90df
imull $0x3e8, %r12d, %eax     # imm = 0x3E8
testl %r12d, %r12d
jle 0x915c
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd %xmm1, %xmm0
xorl %edx, %edx
xorpd %xmm1, %xmm1
xorl %ecx, %ecx
movl (%r15,%rdx,4), %esi
xorps %xmm2, %xmm2
cvtsi2sd %rsi, %xmm2
mulsd %xmm0, %xmm2
addsd %xmm1, %xmm2
cvttsd2si %xmm2, %rsi
movl %esi, %edi
xorps %xmm1, %xmm1
cvtsi2sd %rdi, %xmm1
subsd %xmm1, %xmm2
movl %esi, (%r15,%rdx,4)
addl %esi, %ecx
incq %rdx
movslq 0x34(%r14), %r12
movapd %xmm2, %xmm1
cmpq %r12, %rdx
jl 0x9116
jmp 0x915e
imull $0x3e8, %r12d, %eax     # imm = 0x3E8
xorl %ecx, %ecx
subl %ecx, %eax
jle 0x916a
movslq %r12d, %rcx
addl %eax, -0x4(%r15,%rcx,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 0x91d2
movq %rbx, %rdi
callq 0x9270
movq (%rbx), %rax
movq %rbx, %rdi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmpq *0x40(%rax)
nop