// File: /workspace/github_c_makefile_star2_O2/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 0xe2f9
movq %rdi, %rbx
cmpq $0x0, 0x20(%rdi)
je 0xe30e
movq %rdx, %r15
movl %esi, %ebp
movl 0x268(%rbx), %r12d
movq %r12, %rdi
callq 0x22a0
testq %rax, %rax
je 0xe33c
movq %rax, %r14
cmpl $0x0, 0x444(%rbx)
je 0xe35b
leaq 0x9c11(%rip), %rdi      # 0x17eb1
leaq 0x9b92(%rip), %rsi      # 0x17e39
movl $0x158, %edx            # imm = 0x158
movl %ebp, %ecx
movq %r15, %r8
xorl %eax, %eax
callq 0xc2a1
movl 0x268(%rbx), %r8d
movl %r8d, %eax
imull %ebp, %eax
addl $0x200, %eax            # imm = 0x200
cmpl $0x0, 0x444(%rbx)
movslq %eax, %r12
je 0xe369
leaq 0x9c06(%rip), %rdi      # 0x17ee7
leaq 0x9b51(%rip), %rsi      # 0x17e39
movl $0x15d, %edx            # imm = 0x15D
movq %r12, %rcx
xorl %eax, %eax
callq 0xc2a1
jmp 0xe369
leaq 0x9b3f(%rip), %rdi      # 0x17e3f
leaq 0x9b32(%rip), %rsi      # 0x17e39
movl $0x146, %edx            # imm = 0x146
jmp 0xe321
leaq 0x9d6c(%rip), %rdi      # 0x18081
leaq 0x9b1d(%rip), %rsi      # 0x17e39
movl $0x17d, %edx            # imm = 0x17D
xorl %eax, %eax
callq 0xc2a1
pushq $-0x1
popq %rbp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9b2c(%rip), %rdi      # 0x17e6f
leaq 0x9aef(%rip), %rsi      # 0x17e39
movl $0x154, %edx            # imm = 0x154
movl %r12d, %ecx
xorl %eax, %eax
callq 0xc2a1
jmp 0xe328
imull %ebp, %r12d
addl $0x200, %r12d           # imm = 0x200
movslq %r12d, %r12
movq 0x20(%rbx), %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x2310
testl %eax, %eax
je 0xe3b8
movq %r14, %rdi
callq 0x2030
callq 0x2060
movl (%rax), %edi
callq 0x23c0
leaq 0x9b9a(%rip), %rdi      # 0x17f30
leaq 0x9a9c(%rip), %rsi      # 0x17e39
movl $0x163, %edx            # imm = 0x163
movl %ebp, %ecx
movq %r12, %r8
movq %rax, %r9
xorl %eax, %eax
callq 0xc2a1
pushq $0x29
jmp 0xe32a
movl 0x268(%rbx), %edx
movq 0x20(%rbx), %rcx
pushq $0x1
popq %rsi
movq %r14, %rdi
callq 0x20c0
movq %rax, %r12
cmpl $0x0, 0x444(%rbx)
je 0xe3f6
leaq 0x9b86(%rip), %rdi      # 0x17f66
leaq 0x9a52(%rip), %rsi      # 0x17e39
movl $0x169, %edx            # imm = 0x169
movl %r12d, %ecx
xorl %eax, %eax
callq 0xc2a1
movl 0x268(%rbx), %r13d
cmpl %r12d, %r13d
jne 0xe4c9
cmpl $0x0, 0x444(%rbx)
je 0xe430
leaq 0x9bb4(%rip), %rdi      # 0x17fca
leaq 0x9a1c(%rip), %rsi      # 0x17e39
movl $0x171, %edx            # imm = 0x171
xorl %eax, %eax
callq 0xc2a1
movl 0x268(%rbx), %r12d
movl %r12d, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x2260
cmpl $0x0, 0x444(%rbx)
je 0xe506
leaq 0x9bb6(%rip), %rdi      # 0x18008
leaq 0x99e0(%rip), %rsi      # 0x17e39
xorl %ebp, %ebp
movl $0x175, %edx            # imm = 0x175
xorl %eax, %eax
callq 0xc2a1
movl 0x444(%rbx), %r15d
movq %r14, %rdi
callq 0x2030
testl %r15d, %r15d
je 0xe32b
leaq 0x9bc1(%rip), %rdi      # 0x18047
leaq 0x99ac(%rip), %rsi      # 0x17e39
xorl %ebp, %ebp
movl $0x17a, %edx            # imm = 0x17A
xorl %eax, %eax
callq 0xc2a1
cmpl $0x0, 0x444(%rbx)
je 0xe32b
leaq 0x9c01(%rip), %rdi      # 0x180b0
leaq 0x9983(%rip), %rsi      # 0x17e39
xorl %ebp, %ebp
movl $0x181, %edx            # imm = 0x181
xorl %eax, %eax
callq 0xc2a1
jmp 0xe32b
movq %r14, %rdi
callq 0x2030
pushq $0x2a
popq %rbp
cmpl $0x0, 0x448(%rbx)
je 0xe32b
leaq 0x9aae(%rip), %rdi      # 0x17f96
leaq 0x994a(%rip), %rsi      # 0x17e39
movl $0x16d, %edx            # imm = 0x16D
movl %r13d, %ecx
movl %r12d, %r8d
xorl %eax, %eax
callq 0xc2a1
jmp 0xe32b
movq %r14, %rdi
callq 0x2030
xorl %ebp, %ebp
jmp 0xe32b