// File: /workspace/github_cpp_makefile_star2_O2/akw0088[P]openssl/rsync_upload.c
// Function: adler32_scan
int adler32_scan(unsigned char *data, unsigned int length, unsigned int block_size, unsigned int *hash_array, int num_hash, block_t *block, char **md5_array)
{
	unsigned int start_offset = 1;
	if (block_size > length)
	{
		printf("Block size larger than file\r\n");
		return -1;
	}

	// initialize output array
	for (int i = 0; i < num_hash; i++)
	{
		block[i].offset = -1;
		block[i].length = block_size;
	}


	// init checksum with adler32 of first block
	unsigned int checksum = adler32(&data[0], block_size);
	for (int i = 0; i < num_hash; i++)
	{
		// Checksum matches, do a strong hash to avoid false positives
		if (checksum == hash_array[i])
		{
			char hash[MD5_SIZE + 1] = { 0 };

			md5sum((char *)&data[i], block_size, (char *)&hash);

			if (strcmp(hash, md5_array[i]) == 0)
			{
				block[i].offset = 0;
				block[i].length = block_size;
				start_offset = block_size - 1;

				// Found a block with strong hash, skip past it then reinit adler32 for next block
				checksum = adler32(&data[start_offset - 1], block_size);
				break;
			}
		}
	}

	// Start the adler32 rolling hash (note ader32_roll is where majority of processing occurs)
	for(unsigned int i = start_offset; i < length - block_size + 1; i++)
	{
		checksum = adler32_roll(checksum,
			data[i + block_size - 1],
			data[i - 1],
			block_size);
#ifdef DEBUG
		// For debugging roll function compare it to the full adler32 calc of the block
		unsigned int full_hash = adler32(&data[i], block_size);
		//		checksum = full_hash;
#endif
		for (int j = 0; j < num_hash; j++)
		{
			// Checksum matches, do a strong hash to avoid false positives
			if (checksum == hash_array[j])
			{
				char hash[MD5_SIZE + 1] = { 0 };

				md5sum((char *)&data[i], block_size, (char *)&hash);

				if (strcmp(hash, md5_array[j]) == 0)
				{
					// Found a block with strong hash, skip past it then reinit adler32 for next block
					block[j].offset = i;
					block[j].length = block_size;
					i += block_size - 1;

					// be sure we arent skipping past end of data
					if (i < length - block_size + 1)
					{
						checksum = adler32(&data[i], block_size);
					}
				}
				break;
			}
		}

#ifdef DEBUG
		if (checksum != full_hash)
		{
			printf("adler32_roll failed at index %d\r\n", i);
			exit(0);
		}
#endif

	}
	return -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdi, 0x10(%rsp)
subl %edx, %esi
movl %esi, 0xc(%rsp)
jae 0x1464
leaq 0x2ec8(%rip), %rdi      # 0x4315
callq 0x1070
pushq $-0x1
popq %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r8d, %r14d
movq %rcx, %r15
movl %edx, %r13d
xorl %eax, %eax
testl %r8d, %r8d
cmovlel %eax, %r14d
cmpq %rax, %r14
je 0x148a
orl $-0x1, (%r9,%rax,8)
movl %r13d, 0x4(%r9,%rax,8)
incq %rax
jmp 0x1476
movq %r9, 0x20(%rsp)
movq 0x10(%rsp), %rdi
movl %r13d, %esi
callq 0x134c
movl %eax, %ebp
xorl %r12d, %r12d
leaq 0x30(%rsp), %rbx
cmpq %r12, %r14
je 0x14f4
cmpl (%r15,%r12,4), %ebp
jne 0x14ef
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movb $0x0, 0x50(%rsp)
movq 0x10(%rsp), %rax
leaq (%rax,%r12), %rdi
movl %r13d, %esi
movq %rbx, %rdx
callq 0x26a0
movq 0xa0(%rsp), %rax
movq (%rax,%r12,8), %rsi
movq %rbx, %rdi
callq 0x1140
testl %eax, %eax
je 0x1502
incq %r12
jmp 0x14a6
leal -0x1(%r13), %eax
movq %rax, 0x28(%rsp)
pushq $0x1
popq %rbx
jmp 0x152d
movq 0x20(%rsp), %rax
andl $0x0, (%rax,%r12,8)
movl %r13d, 0x4(%rax,%r12,8)
leal -0x1(%r13), %ebx
leal -0x2(%r13), %edi
addq 0x10(%rsp), %rdi
movl %r13d, %esi
callq 0x134c
movl %eax, %ebp
movq %rbx, 0x28(%rsp)
incl 0xc(%rsp)
movq %r13, 0x18(%rsp)
cmpl 0xc(%rsp), %ebx
jae 0x1452
movq 0x28(%rsp), %rax
leal (%rax,%rbx), %r12d
movq 0x10(%rsp), %rcx
leaq (%rcx,%r12), %rax
movq %rax, 0x60(%rsp)
movzbl (%rcx,%r12), %esi
leal -0x1(%rbx), %eax
movzbl (%rcx,%rax), %edx
movl %ebp, %edi
movl %r13d, %ecx
callq 0x1390
movl %eax, %ebp
xorl %eax, %eax
cmpq %rax, %r14
je 0x15d0
leaq 0x1(%rax), %r13
cmpl (%r15,%rax,4), %ebp
movq %r13, %rax
jne 0x1571
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movb $0x0, 0x50(%rsp)
movl %ebx, %edi
addq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
leaq 0x30(%rsp), %rdx
callq 0x26a0
movq 0xa0(%rsp), %rax
movq -0x8(%rax,%r13,8), %rsi
leaq 0x30(%rsp), %rdi
callq 0x1140
testl %eax, %eax
je 0x15d7
movl %ebx, %r12d
movq 0x18(%rsp), %r13
jmp 0x1604
movq 0x18(%rsp), %r13
jmp 0x1607
movq 0x20(%rsp), %rax
movl %ebx, -0x8(%rax,%r13,8)
movq 0x18(%rsp), %rcx
movl %ecx, -0x4(%rax,%r13,8)
cmpl 0xc(%rsp), %r12d
movq %rcx, %r13
jae 0x1604
movq 0x60(%rsp), %rdi
movl %r13d, %esi
callq 0x134c
movl %eax, %ebp
movl %r12d, %ebx
incl %ebx
jmp 0x1536