// File: /workspace/github_c_makefile_star2_O3/inflex[P]ripMIME/ripOLE/ole.c
// Function: OLE_get_block
int OLE_get_block( struct OLE_object *ole, int block_index, unsigned char *block_buffer )
{
	if (block_buffer == NULL)
	{
		LOGGER_log("%s:%d:OLE_get_block:ERROR: Block buffer is NULL",FL);
		return -1;
	}

	if (ole->f != NULL)
	{
		int read_count = 0;
		int fseek_result = 0;
		size_t offset = 0;
		unsigned char *bb = NULL;

		bb = malloc(sizeof(unsigned char) *ole->header.sector_size);
		if (bb == NULL)
		{
			LOGGER_log("%s:%d:OLE_get_block:ERROR: Cannot allocate %d bytes for OLE block",FL, ole->header.sector_size);
			return -1;
		}

		DOLE LOGGER_log("%s:%d:OLE_get_block:DEBUG: BlockIndex=%d, Buffer=0x%x",FL, block_index, block_buffer);

		//20051211-2343:PLD: offset = (block_index +1) << ole->header.sector_shift;
		offset = OLE_sectorpos(ole, block_index);

		DOLE LOGGER_log("%s:%d:OLE_get_block:DEBUG: Read offset in file = 0x%x size to read= 0x%x",FL,offset,ole->header.sector_size);

		fseek_result = fseek(ole->f, offset, SEEK_SET);
		if (fseek_result != 0)
		{
			if (bb != NULL) { free(bb); bb = NULL; }
			LOGGER_log("%s:%d:OLE_get_block:ERROR: Seek failure (block=%d:%d)",FL, block_index,offset, strerror(errno));
			return OLEER_GET_BLOCK_SEEK;
		}

		//read_count = fread(block_buffer, sizeof(unsigned char), ole->header.sector_size, ole->f);
		read_count = fread(bb, sizeof(unsigned char), ole->header.sector_size, ole->f);
		DOLE LOGGER_log("%s:%d:OLE_get_block:DEBUG: Read %d byte of data",FL,read_count);
		if (read_count != (int)ole->header.sector_size)
		{
			if (bb != NULL){ free(bb); bb = NULL; }
			VOLE LOGGER_log("%s:%d:Mismatch in bytes read. Requested %d, got %d\n", FL, ole->header.sector_size, read_count);
			return OLEER_GET_BLOCK_READ;
		}

		DOLE LOGGER_log("%s:%d:OLE_get_block:DEBUG: Copying over memory read from file",FL);

		memcpy(block_buffer, bb, ole->header.sector_size);

		DOLE LOGGER_log("%s:%d:OLE_get_block:DEBUG: memory block copied to block_buffer",FL);

		/* We're now done with BB, dispose of it */
		if (bb) { free(bb); bb = NULL; }

		DOLE LOGGER_log("%s:%d:OLE_get_block:DEBUG: Disposed of temporary bb block",FL);

	} else {
		LOGGER_log("%s:%d:OLE_get_block:ERROR: OLE file is closed\n",FL);
		return -1;
	}

	DOLE LOGGER_log("%s:%d:OLE_get_block:DEBUG: Done",FL);

	return OLE_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
testq %rdx, %rdx
je 0xe944
movq %rdi, %rbx
cmpq $0x0, 0x20(%rdi)
je 0xe959
movq %rdx, %r15
movl %esi, %ebp
movl 0x268(%rbx), %r12d
movq %r12, %rdi
callq 0x22a0
testq %rax, %rax
je 0xe989
movq %rax, %r14
cmpl $0x0, 0x444(%rbx)
je 0xe9a8
leaq 0xa5c6(%rip), %rdi      # 0x18eb1
leaq 0xa547(%rip), %rsi      # 0x18e39
movl $0x158, %edx            # imm = 0x158
movl %ebp, %ecx
movq %r15, %r8
xorl %eax, %eax
callq 0xc84f
movl 0x268(%rbx), %r8d
movl %r8d, %eax
imull %ebp, %eax
addl $0x200, %eax            # imm = 0x200
movslq %eax, %r12
cmpl $0x0, 0x444(%rbx)
je 0xe9b6
leaq 0xa5bb(%rip), %rdi      # 0x18ee7
leaq 0xa506(%rip), %rsi      # 0x18e39
movl $0x15d, %edx            # imm = 0x15D
movq %r12, %rcx
xorl %eax, %eax
callq 0xc84f
jmp 0xe9b6
leaq 0xa4f4(%rip), %rdi      # 0x18e3f
leaq 0xa4e7(%rip), %rsi      # 0x18e39
movl $0x146, %edx            # imm = 0x146
jmp 0xe96c
leaq 0xa721(%rip), %rdi      # 0x19081
leaq 0xa4d2(%rip), %rsi      # 0x18e39
movl $0x17d, %edx            # imm = 0x17D
xorl %eax, %eax
callq 0xc84f
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa4df(%rip), %rdi      # 0x18e6f
leaq 0xa4a2(%rip), %rsi      # 0x18e39
movl $0x154, %edx            # imm = 0x154
movl %r12d, %ecx
xorl %eax, %eax
callq 0xc84f
jmp 0xe973
imull %ebp, %r12d
addl $0x200, %r12d           # imm = 0x200
movslq %r12d, %r12
movq 0x20(%rbx), %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x2300
testl %eax, %eax
je 0xea08
movq %r14, %rdi
callq 0x2030
callq 0x2060
movl (%rax), %edi
callq 0x23b0
leaq 0xa54d(%rip), %rdi      # 0x18f30
leaq 0xa44f(%rip), %rsi      # 0x18e39
movl $0x163, %edx            # imm = 0x163
movl %ebp, %ecx
movq %r12, %r8
movq %rax, %r9
xorl %eax, %eax
callq 0xc84f
movl $0x29, %ebp
jmp 0xe978
movl 0x268(%rbx), %edx
movq 0x20(%rbx), %rcx
movl $0x1, %esi
movq %r14, %rdi
callq 0x20c0
movq %rax, %r12
cmpl $0x0, 0x444(%rbx)
je 0xea48
leaq 0xa534(%rip), %rdi      # 0x18f66
leaq 0xa400(%rip), %rsi      # 0x18e39
movl $0x169, %edx            # imm = 0x169
movl %r12d, %ecx
xorl %eax, %eax
callq 0xc84f
movl 0x268(%rbx), %r13d
cmpl %r12d, %r13d
jne 0xeb1b
cmpl $0x0, 0x444(%rbx)
je 0xea82
leaq 0xa562(%rip), %rdi      # 0x18fca
leaq 0xa3ca(%rip), %rsi      # 0x18e39
movl $0x171, %edx            # imm = 0x171
xorl %eax, %eax
callq 0xc84f
movl 0x268(%rbx), %r12d
movl %r12d, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x2260
cmpl $0x0, 0x444(%rbx)
je 0xeb5a
leaq 0xa564(%rip), %rdi      # 0x19008
leaq 0xa38e(%rip), %rsi      # 0x18e39
xorl %ebp, %ebp
movl $0x175, %edx            # imm = 0x175
xorl %eax, %eax
callq 0xc84f
movl 0x444(%rbx), %r15d
movq %r14, %rdi
callq 0x2030
testl %r15d, %r15d
je 0xe978
leaq 0xa56f(%rip), %rdi      # 0x19047
leaq 0xa35a(%rip), %rsi      # 0x18e39
xorl %ebp, %ebp
movl $0x17a, %edx            # imm = 0x17A
xorl %eax, %eax
callq 0xc84f
cmpl $0x0, 0x444(%rbx)
je 0xe978
leaq 0xa5af(%rip), %rdi      # 0x190b0
leaq 0xa331(%rip), %rsi      # 0x18e39
xorl %ebp, %ebp
movl $0x181, %edx            # imm = 0x181
xorl %eax, %eax
callq 0xc84f
jmp 0xe978
movq %r14, %rdi
callq 0x2030
movl $0x2a, %ebp
cmpl $0x0, 0x448(%rbx)
je 0xe978
leaq 0xa45a(%rip), %rdi      # 0x18f96
leaq 0xa2f6(%rip), %rsi      # 0x18e39
movl $0x16d, %edx            # imm = 0x16D
movl %r13d, %ecx
movl %r12d, %r8d
xorl %eax, %eax
callq 0xc84f
jmp 0xe978
movq %r14, %rdi
callq 0x2030
xorl %ebp, %ebp
jmp 0xe978