// File: /workspace/github_c_makefile_star2_O1/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 0xeabb
movq %rdi, %rbx
cmpq $0x0, 0x20(%rdi)
je 0xead0
movq %rdx, %r15
movl %esi, %ebp
movl 0x268(%rbx), %r12d
movq %r12, %rdi
callq 0x22b0
testq %rax, %rax
je 0xeaf4
movq %rax, %r14
cmpl $0x0, 0x444(%rbx)
je 0xea27
leaq 0xa4b2(%rip), %rdi      # 0x18ec1
leaq 0xa433(%rip), %rsi      # 0x18e49
movl $0x158, %edx            # imm = 0x158
movl %ebp, %ecx
movq %r15, %r8
xorl %eax, %eax
callq 0xc960
movl 0x268(%rbx), %r8d
movl %r8d, %eax
imull %ebp, %eax
addl $0x200, %eax            # imm = 0x200
movslq %eax, %r12
cmpl $0x0, 0x444(%rbx)
je 0xea62
leaq 0xa4ab(%rip), %rdi      # 0x18ef7
leaq 0xa3f6(%rip), %rsi      # 0x18e49
movl $0x15d, %edx            # imm = 0x15D
movq %r12, %rcx
xorl %eax, %eax
callq 0xc960
movq 0x20(%rbx), %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x2310
testl %eax, %eax
je 0xeb1e
movq %r14, %rdi
callq 0x2030
callq 0x2060
movl (%rax), %edi
callq 0x23c0
leaq 0xa4ad(%rip), %rdi      # 0x18f40
leaq 0xa3af(%rip), %rsi      # 0x18e49
xorl %r14d, %r14d
movl $0x163, %edx            # imm = 0x163
movl %ebp, %ecx
movq %r12, %r8
movq %rax, %r9
xorl %eax, %eax
callq 0xc960
movl $0x29, %ebp
jmp 0xec33
leaq 0xa38d(%rip), %rdi      # 0x18e4f
leaq 0xa380(%rip), %rsi      # 0x18e49
movl $0x146, %edx            # imm = 0x146
jmp 0xeae3
leaq 0xa5ba(%rip), %rdi      # 0x19091
leaq 0xa36b(%rip), %rsi      # 0x18e49
movl $0x17d, %edx            # imm = 0x17D
xorl %eax, %eax
callq 0xc960
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0xec61
leaq 0xa384(%rip), %rdi      # 0x18e7f
leaq 0xa347(%rip), %rsi      # 0x18e49
xorl %r14d, %r14d
movl $0x154, %edx            # imm = 0x154
movl %r12d, %ecx
xorl %eax, %eax
callq 0xc960
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0xec33
movl 0x268(%rbx), %edx
movq 0x20(%rbx), %rcx
movl $0x1, %esi
movq %r14, %rdi
callq 0x20c0
movq %rax, %r12
cmpl $0x0, 0x444(%rbx)
je 0xeb5e
leaq 0xa42e(%rip), %rdi      # 0x18f76
leaq 0xa2fa(%rip), %rsi      # 0x18e49
movl $0x169, %edx            # imm = 0x169
movl %r12d, %ecx
xorl %eax, %eax
callq 0xc960
movl 0x268(%rbx), %r13d
cmpl %r12d, %r13d
jne 0xebf5
cmpl $0x0, 0x444(%rbx)
je 0xeb91
leaq 0xa45c(%rip), %rdi      # 0x18fda
leaq 0xa2c4(%rip), %rsi      # 0x18e49
movl $0x171, %edx            # imm = 0x171
xorl %eax, %eax
callq 0xc960
movl 0x268(%rbx), %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x2270
cmpl $0x0, 0x444(%rbx)
je 0xebc5
leaq 0xa466(%rip), %rdi      # 0x19018
leaq 0xa290(%rip), %rsi      # 0x18e49
movl $0x175, %edx            # imm = 0x175
xorl %eax, %eax
callq 0xc960
movq %r14, %rdi
callq 0x2030
movb $0x1, %r14b
cmpl $0x0, 0x444(%rbx)
je 0xebf3
leaq 0xa477(%rip), %rdi      # 0x19057
leaq 0xa262(%rip), %rsi      # 0x18e49
movl $0x17a, %edx            # imm = 0x17A
xorl %eax, %eax
callq 0xc960
jmp 0xec33
movq %r14, %rdi
callq 0x2030
movl $0x2a, %ebp
cmpl $0x0, 0x448(%rbx)
je 0xec30
leaq 0xa394(%rip), %rdi      # 0x18fa6
leaq 0xa230(%rip), %rsi      # 0x18e49
xorl %r14d, %r14d
movl $0x16d, %edx            # imm = 0x16D
movl %r13d, %ecx
movl %r12d, %r8d
xorl %eax, %eax
callq 0xc960
jmp 0xec33
xorl %r14d, %r14d
testb %r14b, %r14b
je 0xec61
cmpl $0x0, 0x444(%rbx)
je 0xec5f
leaq 0xa478(%rip), %rdi      # 0x190c0
leaq 0xa1fa(%rip), %rsi      # 0x18e49
xorl %ebp, %ebp
movl $0x181, %edx            # imm = 0x181
xorl %eax, %eax
callq 0xc960
jmp 0xec61
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq