// File: /workspace/github_cpp_makefile_star2_O1/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
movl %esi, %r10d
subl %edx, %r10d
jae 0x14e2
leaq 0x2e3d(%rip), %rdi      # 0x4315
callq 0x1070
jmp 0x17f0
movq %rcx, %r14
movl %edx, %ecx
movq %rdi, %r11
movl %r8d, %r12d
testl %r8d, %r8d
jle 0x1509
xorl %eax, %eax
movl $0xffffffff, (%r9,%rax,8) # imm = 0xFFFFFFFF
movl %ecx, 0x4(%r9,%rax,8)
incq %rax
cmpq %rax, %r12
jne 0x14f4
movq %r9, 0x38(%rsp)
movl $0x1, %r15d
movl %ecx, %eax
movq %rax, 0x20(%rsp)
movl $0x1, %ebp
testl %ecx, %ecx
je 0x1572
movl $0x1, %edx
xorl %eax, %eax
movl $0x80078071, %edi       # imm = 0x80078071
xorl %ebp, %ebp
movq 0x20(%rsp), %r9
movzbl (%r11,%rax), %esi
addl %edx, %esi
movl %esi, %edx
imulq %rdi, %rsi
shrq $0x2f, %rsi
imull $0xfff1, %esi, %esi     # imm = 0xFFF1
subl %esi, %edx
addl %edx, %ebp
movq %rbp, %rsi
imulq %rdi, %rsi
shrq $0x2f, %rsi
imull $0xfff1, %esi, %esi     # imm = 0xFFF1
subl %esi, %ebp
incq %rax
cmpq %rax, %r9
jne 0x1537
shll $0x10, %ebp
orl %edx, %ebp
movl %r10d, 0x8(%rsp)
movl %r8d, 0xc(%rsp)
testl %r8d, %r8d
movq %rcx, 0x30(%rsp)
movq %r11, 0x10(%rsp)
jle 0x167f
leal -0x1(%rcx), %eax
movl %eax, 0x18(%rsp)
leal -0x2(%rcx), %eax
addq %r11, %rax
movq %rax, 0x28(%rsp)
movl $0x1, %r15d
xorl %ebx, %ebx
movl $0x80078071, %r13d      # imm = 0x80078071
cmpl (%r14,%rbx,4), %ebp
jne 0x1673
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movb $0x0, 0x60(%rsp)
leaq (%r11,%rbx), %rdi
movl %ecx, %esi
leaq 0x40(%rsp), %rdx
callq 0x2d0c
movq 0xa0(%rsp), %rax
movq (%rax,%rbx,8), %rsi
leaq 0x40(%rsp), %rdi
callq 0x1130
movq 0x30(%rsp), %rcx
testl %eax, %eax
jne 0x166a
movq 0x38(%rsp), %rdx
movl $0x0, (%rdx,%rbx,8)
movl %ecx, 0x4(%rdx,%rbx,8)
testl %ecx, %ecx
je 0x165e
movl $0x1, %edx
xorl %edi, %edi
xorl %ebp, %ebp
movq 0x20(%rsp), %r8
movq 0x28(%rsp), %r9
movzbl (%r9,%rdi), %esi
addl %edx, %esi
movl %esi, %edx
imulq %r13, %rsi
shrq $0x2f, %rsi
imull $0xfff1, %esi, %esi     # imm = 0xFFF1
subl %esi, %edx
addl %edx, %ebp
movq %rbp, %rsi
imulq %r13, %rsi
shrq $0x2f, %rsi
imull $0xfff1, %esi, %esi     # imm = 0xFFF1
subl %esi, %ebp
incq %rdi
cmpq %rdi, %r8
jne 0x1621
shll $0x10, %ebp
orl %edx, %ebp
jmp 0x1663
movl $0x1, %ebp
movl 0x18(%rsp), %edx
movl %edx, %r15d
testl %eax, %eax
movq 0x10(%rsp), %r11
je 0x167f
incq %rbx
cmpq %r12, %rbx
jne 0x15af
movl 0x8(%rsp), %r13d
incl %r13d
cmpl %r13d, %r15d
movl 0xc(%rsp), %ebx
jae 0x17f0
leal -0x1(%rcx), %eax
movq %rax, 0x28(%rsp)
movl %r13d, 0x8(%rsp)
movq 0x28(%rsp), %rax
addl %r15d, %eax
movq %rax, 0x18(%rsp)
movzbl (%r11,%rax), %esi
leal -0x1(%r15), %eax
movzbl (%r11,%rax), %edx
movl %ebp, %edi
callq 0x13fb
movl %eax, %ebp
testl %ebx, %ebx
jle 0x16ec
xorl %ebx, %ebx
movq 0x30(%rsp), %rcx
movq 0x10(%rsp), %r11
cmpl (%r14,%rbx,4), %ebp
je 0x16fb
incq %rbx
cmpq %rbx, %r12
jne 0x16d5
movl 0xc(%rsp), %ebx
jmp 0x17db
movq 0x30(%rsp), %rcx
movq 0x10(%rsp), %r11
jmp 0x17db
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movb $0x0, 0x60(%rsp)
movl %r15d, %edi
addq %r11, %rdi
movl %ecx, %esi
leaq 0x40(%rsp), %rdx
movq %rcx, %r13
callq 0x2d0c
movq 0xa0(%rsp), %rax
movq (%rax,%rbx,8), %rsi
leaq 0x40(%rsp), %rdi
callq 0x1130
testl %eax, %eax
je 0x1748
movl 0xc(%rsp), %ebx
movq %r13, %rcx
jmp 0x17d1
movq 0x38(%rsp), %rax
movl %r15d, (%rax,%rbx,8)
movl %r13d, 0x4(%rax,%rbx,8)
movq 0x18(%rsp), %r15
cmpl 0x8(%rsp), %r15d
movq %r13, %rcx
jae 0x17cd
testl %ecx, %ecx
movl 0xc(%rsp), %ebx
movl 0x8(%rsp), %r13d
movq 0x10(%rsp), %r11
movq 0x20(%rsp), %r8
movl $0x80078071, %r9d       # imm = 0x80078071
je 0x17e9
movl %r15d, %eax
addq %r11, %rax
movl $0x1, %edx
xorl %edi, %edi
xorl %ebp, %ebp
movzbl (%rax,%rdi), %esi
addl %edx, %esi
movl %esi, %edx
imulq %r9, %rsi
shrq $0x2f, %rsi
imull $0xfff1, %esi, %esi     # imm = 0xFFF1
subl %esi, %edx
addl %edx, %ebp
movq %rbp, %rsi
imulq %r9, %rsi
shrq $0x2f, %rsi
imull $0xfff1, %esi, %esi     # imm = 0xFFF1
subl %esi, %ebp
incq %rdi
cmpq %rdi, %r8
jne 0x1791
shll $0x10, %ebp
orl %edx, %ebp
jmp 0x17db
movl 0xc(%rsp), %ebx
movl 0x8(%rsp), %r13d
movq 0x10(%rsp), %r11
incl %r15d
cmpl %r13d, %r15d
jb 0x16a1
jmp 0x17f0
movl $0x1, %ebp
jmp 0x17db
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq