// File: 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:
subq $0x98, %rsp
movq 0xa0(%rsp), %rax
movq %rdi, 0x88(%rsp)
movl %esi, 0x84(%rsp)
movl %edx, 0x80(%rsp)
movq %rcx, 0x78(%rsp)
movl %r8d, 0x74(%rsp)
movq %r9, 0x68(%rsp)
movl $0x1, 0x64(%rsp)
movl 0x80(%rsp), %eax
cmpl 0x84(%rsp), %eax
jbe 0x150a
leaq 0x3b45(%rip), %rdi      # 0x5038
movb $0x0, %al
callq 0x10e0
movl $0xffffffff, 0x94(%rsp) # imm = 0xFFFFFFFF
jmp 0x17dd
movl $0x0, 0x60(%rsp)
movl 0x60(%rsp), %eax
cmpl 0x74(%rsp), %eax
jge 0x154f
movq 0x68(%rsp), %rax
movslq 0x60(%rsp), %rcx
movl $0xffffffff, (%rax,%rcx,8) # imm = 0xFFFFFFFF
movl 0x80(%rsp), %edx
movq 0x68(%rsp), %rax
movslq 0x60(%rsp), %rcx
movl %edx, 0x4(%rax,%rcx,8)
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
jmp 0x1512
movq 0x88(%rsp), %rdi
movl 0x80(%rsp), %esi
callq 0x1350
movl %eax, 0x5c(%rsp)
movl $0x0, 0x58(%rsp)
movl 0x58(%rsp), %eax
cmpl 0x74(%rsp), %eax
jge 0x1654
movl 0x5c(%rsp), %eax
movq 0x78(%rsp), %rcx
movslq 0x58(%rsp), %rdx
cmpl (%rcx,%rdx,4), %eax
jne 0x1642
leaq 0x30(%rsp), %rdi
xorl %esi, %esi
movl $0x21, %edx
callq 0x1110
movq 0x88(%rsp), %rdi
movslq 0x58(%rsp), %rax
addq %rax, %rdi
movl 0x80(%rsp), %esi
leaq 0x30(%rsp), %rdx
callq 0x33b0
leaq 0x30(%rsp), %rdi
movq 0xa0(%rsp), %rax
movslq 0x58(%rsp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x1140
cmpl $0x0, %eax
jne 0x1640
movq 0x68(%rsp), %rax
movslq 0x58(%rsp), %rcx
movl $0x0, (%rax,%rcx,8)
movl 0x80(%rsp), %edx
movq 0x68(%rsp), %rax
movslq 0x58(%rsp), %rcx
movl %edx, 0x4(%rax,%rcx,8)
movl 0x80(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x64(%rsp)
movq 0x88(%rsp), %rdi
movl 0x64(%rsp), %eax
subl $0x1, %eax
movl %eax, %eax
addq %rax, %rdi
movl 0x80(%rsp), %esi
callq 0x1350
movl %eax, 0x5c(%rsp)
jmp 0x1654
jmp 0x1642
jmp 0x1644
movl 0x58(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x58(%rsp)
jmp 0x156f
movl 0x64(%rsp), %eax
movl %eax, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
movl 0x84(%rsp), %ecx
subl 0x80(%rsp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jae 0x17d2
movl 0x5c(%rsp), %edi
movq 0x88(%rsp), %rax
movl 0x2c(%rsp), %ecx
addl 0x80(%rsp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movb (%rax,%rcx), %dl
movq 0x88(%rsp), %rax
movl 0x2c(%rsp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movb (%rax,%rcx), %al
movl 0x80(%rsp), %ecx
movzbl %dl, %esi
movzbl %al, %edx
callq 0x13e0
movl %eax, 0x5c(%rsp)
movl $0x0, 0x28(%rsp)
movl 0x28(%rsp), %eax
cmpl 0x74(%rsp), %eax
jge 0x17c0
movl 0x5c(%rsp), %eax
movq 0x78(%rsp), %rcx
movslq 0x28(%rsp), %rdx
cmpl (%rcx,%rdx,4), %eax
jne 0x17ae
movq %rsp, %rdi
xorl %esi, %esi
movl $0x21, %edx
callq 0x1110
movq 0x88(%rsp), %rdi
movl 0x2c(%rsp), %eax
addq %rax, %rdi
movl 0x80(%rsp), %esi
movq %rsp, %rdx
callq 0x33b0
movq %rsp, %rdi
movq 0xa0(%rsp), %rax
movslq 0x28(%rsp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x1140
cmpl $0x0, %eax
jne 0x17ac
movl 0x2c(%rsp), %edx
movq 0x68(%rsp), %rax
movslq 0x28(%rsp), %rcx
movl %edx, (%rax,%rcx,8)
movl 0x80(%rsp), %edx
movq 0x68(%rsp), %rax
movslq 0x28(%rsp), %rcx
movl %edx, 0x4(%rax,%rcx,8)
movl 0x80(%rsp), %eax
subl $0x1, %eax
addl 0x2c(%rsp), %eax
movl %eax, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
movl 0x84(%rsp), %ecx
subl 0x80(%rsp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jae 0x17aa
movq 0x88(%rsp), %rdi
movl 0x2c(%rsp), %eax
addq %rax, %rdi
movl 0x80(%rsp), %esi
callq 0x1350
movl %eax, 0x5c(%rsp)
jmp 0x17ac
jmp 0x17c0
jmp 0x17b0
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0x16ca
jmp 0x17c2
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x165c
movl $0xffffffff, 0x94(%rsp) # imm = 0xFFFFFFFF
movl 0x94(%rsp), %eax
addq $0x98, %rsp
retq
nopl (%rax)