// File: OpenFodder[P]SOS_Unpacker/capsimg/CAPSImg/CapsImageStd.cpp
// Function: CCapsImageStd::InitGapStream(ImageStreamInfo*)
int CCapsImageStd::InitGapStream(PIMAGESTREAMINFO pstr)
{
	// sample looping on zero data count is allowed
	pstr->allowloop=1;

	PIMAGEBLOCKINFO pi=di.block+pstr->actblock;

	// always set data size mode to bits for a gap stream
	pstr->sizemodebit=1;

	// generate gap stream from gap value; 1 byte size, 8 bit pattern, gap value
	int align=(pstr->strtype == isitGap0) ? 0 : 1;
	int gflag=align ? CAPS_BF_GP1 : CAPS_BF_GP0;
	int es=1;
	int gc=0;
	pstr->gapstr[gc++]=cpgapData | es<<CAPS_SIZE_S;
	pstr->gapstr[gc++]=8;
	pstr->gapstr[gc++]=(uint8_t)pi->gapvalue;
	pstr->gapstr[gc++]=cpgapEnd;

	// use the generated gap stream if no gap streams are available at all
	if (!(pi->flag & (CAPS_BF_GP0 | CAPS_BF_GP1))) {
		pstr->strstart=0;
		pstr->strend=0;
		pstr->strbase=pstr->gapstr;
		pstr->strsize=gc;
		return imgeOk;
	}

	// disable gap stream if not available
	if (!(pi->flag & gflag)) {
		pstr->strstart=0;
		pstr->strend=0;
		pstr->strbase=NULL;
		pstr->strsize=0;
		return imgeOk;
	}

	// stream start from block descriptor
	pstr->strstart=pi->gapoffset;

	// check buffer overrun for file limit
	if (pstr->strstart >= (uint32_t)di.pdt->datasize)
		return imgeShort;

	// find the next gap stream stored
	int bls;
	for (bls=pstr->actblock+1; bls < di.blockcount; bls++) {
		if (di.block[bls].flag & (CAPS_BF_GP0 | CAPS_BF_GP1))
			break;
	}

	// stream ends at the beginning of the data streams if this is the last gap stream stored
	// if another gap streams follows, this one ends where the next one begins
	if (bls == di.blockcount)
		pstr->strend=di.block[0].dataoffset;
	else
		pstr->strend=di.block[bls].gapoffset;

	// check buffer overrun for stream limit
	if (pstr->strstart >= pstr->strend)
		return imgeShort;

	// set stream pointer and maximum size
	pstr->strbase=di.data+pstr->strstart;
	pstr->strsize=pstr->strend-pstr->strstart;

	// resize stream; find the end of stream#0; if stream#0 is not present gap stream starts with stream#1
	int semode=align && (pi->flag & CAPS_BF_GP0);
	int res=FindGapStreamEnd(pstr, semode);

	return res;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x18(%rbp), %rcx
movl $0x1, 0x44(%rcx)
movq 0x1f8(%rax), %rax
movq -0x18(%rbp), %rcx
movslq 0x4(%rcx), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movl $0x1, 0x10(%rax)
movq -0x18(%rbp), %rax
movl (%rax), %edx
movl $0x1, %eax
xorl %ecx, %ecx
cmpl $0x1, %edx
cmovel %ecx, %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %edx
movl $0x1, %eax
movl $0x2, %ecx
cmpl $0x0, %edx
cmovneq %rcx, %rax
movl %eax, -0x28(%rbp)
movl $0x1, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x2c(%rbp), %eax
shll $0x5, %eax
orl $0x2, %eax
movb %al, %dl
movq -0x18(%rbp), %rax
movl -0x30(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x30(%rbp)
movslq %ecx, %rcx
movb %dl, 0x30(%rax,%rcx)
movq -0x18(%rbp), %rax
movl -0x30(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x30(%rbp)
movslq %ecx, %rcx
movb $0x8, 0x30(%rax,%rcx)
movq -0x20(%rbp), %rax
movl 0x18(%rax), %eax
movb %al, %dl
movq -0x18(%rbp), %rax
movl -0x30(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x30(%rbp)
movslq %ecx, %rcx
movb %dl, 0x30(%rax,%rcx)
movq -0x18(%rbp), %rax
movl -0x30(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x30(%rbp)
movslq %ecx, %rcx
movb $0x0, 0x30(%rax,%rcx)
movq -0x20(%rbp), %rax
movl 0x14(%rax), %eax
andq $0x3, %rax
cmpq $0x0, %rax
jne 0x113be
movq -0x18(%rbp), %rax
movl $0x0, 0x14(%rax)
movq -0x18(%rbp), %rax
movl $0x0, 0x18(%rax)
movq -0x18(%rbp), %rcx
addq $0x30, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x28(%rax)
movl -0x30(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x20(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x1155a
movq -0x20(%rbp), %rax
movl 0x14(%rax), %eax
andl -0x28(%rbp), %eax
cmpl $0x0, %eax
jne 0x11406
movq -0x18(%rbp), %rax
movl $0x0, 0x14(%rax)
movq -0x18(%rbp), %rax
movl $0x0, 0x18(%rax)
movq -0x18(%rbp), %rax
movq $0x0, 0x28(%rax)
movq -0x18(%rbp), %rax
movl $0x0, 0x20(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x1155a
movq -0x48(%rbp), %rcx
movq -0x20(%rbp), %rax
movl 0x8(%rax), %edx
movq -0x18(%rbp), %rax
movl %edx, 0x14(%rax)
movq -0x18(%rbp), %rax
movl 0x14(%rax), %eax
movq 0x210(%rcx), %rcx
cmpl 0x24(%rcx), %eax
jb 0x11437
movl $0x7, -0x4(%rbp)
jmp 0x1155a
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movq -0x48(%rbp), %rcx
movl -0x34(%rbp), %eax
cmpl 0x204(%rcx), %eax
jge 0x11485
movq -0x48(%rbp), %rax
movq 0x1f8(%rax), %rax
movslq -0x34(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x14(%rax), %eax
andq $0x3, %rax
cmpq $0x0, %rax
je 0x11478
jmp 0x11485
jmp 0x1147a
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x11444
movq -0x48(%rbp), %rcx
movl -0x34(%rbp), %eax
cmpl 0x204(%rcx), %eax
jne 0x114ab
movq -0x48(%rbp), %rax
movq 0x1f8(%rax), %rax
movl 0x1c(%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x18(%rax)
jmp 0x114cb
movq -0x48(%rbp), %rax
movq 0x1f8(%rax), %rax
movslq -0x34(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x18(%rbp), %rax
movl 0x14(%rax), %eax
movq -0x18(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jb 0x114e4
movl $0x7, -0x4(%rbp)
jmp 0x1155a
movq -0x48(%rbp), %rax
movq 0x1e8(%rax), %rcx
movq -0x18(%rbp), %rax
movl 0x14(%rax), %eax
addq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x18(%rbp), %rax
movl 0x18(%rax), %ecx
movq -0x18(%rbp), %rax
subl 0x14(%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x20(%rax)
xorl %eax, %eax
cmpl $0x0, -0x24(%rbp)
movb %al, -0x49(%rbp)
je 0x11536
movq -0x20(%rbp), %rax
movl 0x14(%rax), %eax
andq $0x1, %rax
cmpq $0x0, %rax
setne %al
movb %al, -0x49(%rbp)
movq -0x48(%rbp), %rdi
movb -0x49(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x38(%rbp)
movq -0x18(%rbp), %rsi
movl -0x38(%rbp), %edx
callq 0x11700
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)