// File: /workspace/github_cpp_makefile_star2_O2/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 %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movq 0x210(%rdi), %r14
movl 0x7c(%rsi), %ebp
cmpl $0x5, %ebp
pushq $0x5
popq %r15
cmovgel %r15d, %ebp
movl %ebp, 0x2c(%r14)
movl 0x38(%rsi), %eax
movl %eax, 0x30(%r14)
andq $0x0, 0x30(%rsi)
xorl %eax, %eax
testl %ebp, %ebp
movl $0x0, %ecx
cmovgl %ebp, %ecx
shlq $0x2, %rcx
xorl %edx, %edx
cmpq %rax, %rcx
je 0x7a35
movq 0x40(%rsi,%rax,2), %rdi
movq %rdi, 0x58(%r14,%rax,2)
movl 0x68(%rsi,%rax), %edi
movl %edi, 0x80(%r14,%rax)
cmpl %edx, %edi
cmovgl %edi, %edx
addq $0x4, %rax
jmp 0x7a0f
movq 0x8(%rsi), %r12
movq %r12, 0x38(%r14)
movl 0x10(%rsi), %r13d
movl %r13d, 0x34(%r14)
andq $0x0, 0x8(%rsi)
movl %edx, %eax
leaq 0x4(,%rax,4), %rdi
callq 0x3040
movq %rax, 0xb0(%r14)
xorl %eax, %eax
testl %r13d, %r13d
movl $0x0, %ecx
cmovgl %r13d, %ecx
xorpd %xmm1, %xmm1
cmpq %rax, %rcx
je 0x7a89
movl (%r12,%rax,4), %edx
cvtsi2sd %rdx, %xmm0
addsd %xmm0, %xmm1
incq %rax
jmp 0x7a72
imull $0x3e8, %r13d, %eax     # imm = 0x3E8
cvtsi2sd %eax, %xmm0
divsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
xorl %ecx, %ecx
xorl %edx, %edx
movslq %r13d, %rsi
cmpq %rsi, %rcx
jge 0x7adc
movl (%r12,%rcx,4), %esi
cvtsi2sd %rsi, %xmm2
mulsd %xmm0, %xmm2
addsd %xmm1, %xmm2
cvttsd2si %xmm2, %rsi
movl %esi, %edi
cvtsi2sd %rdi, %xmm1
subsd %xmm1, %xmm2
movl %esi, (%r12,%rcx,4)
addl %esi, %edx
incq %rcx
movl 0x34(%r14), %r13d
movapd %xmm2, %xmm1
jmp 0x7aa0
subl %edx, %eax
jle 0x7ae5
addl %eax, -0x4(%r12,%rsi,4)
movl 0x208(%rbx), %eax
movl %eax, %ecx
shrl $0x1c, %ecx
andl $0x4, %ecx
incl %ecx
btl $0x8, %eax
cmovael %r15d, %ecx
cmpl %ebp, %ecx
cmovgel %ebp, %ecx
movl %ecx, 0x28(%r14)
orq $-0x1, 0xe0(%r14)
movl $0x87654321, 0xc4(%r14) # imm = 0x87654321
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x40(%r14)
callq 0x7b4c
movq %rbx, %rdi
callq 0x7be0
movq (%rbx), %rax
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *0x40(%rax)
nop