// File: /workspace/github_c_makefile_star2_O3/sTeeLM[P]dwipe/pass.c
// Function: dwipe_static_verify
int dwipe_static_verify( DWIPE_METHOD_SIGNATURE, dwipe_pattern_t* pattern )
{
/**
 * Verifies that a static pass was correctly written to the device.
 *
 */

	/* The result holder. */
	int r;

	/* The IO size. */
	size_t blocksize;

	/* The result buffer for calls to lseek. */
	loff_t offset;

	/* The input buffer. */
	char* b;

	/* The pattern buffer that is used to check the input buffer. */
	char* d;

	/* A pointer into the pattern buffer. */
	char* q;

	/* The pattern buffer window offset. */
	int w = 0;

	/* The number of bytes remaining in the pass. */
	u64 z = c->device_size;

	if( pattern == NULL )
	{
		/* Caught insanity. */	
		dwipe_log( DWIPE_LOG_SANITY, "dwipe_static_verify: Null entropy pointer." );
		return -1;
	}

	if( pattern->length <= 0 )
	{
		/* Caught insanity. */	
		dwipe_log( DWIPE_LOG_SANITY, "dwipe_static_verify: The pattern length member is %i.", pattern->length );
		return -1;
	}

	/* Create the input buffer. */
	b = malloc( c->device_stat.st_blksize * 1024 );

	/* Check the memory allocation. */
	if( ! b )
	{
		dwipe_perror( errno, __FUNCTION__, "malloc" );
		dwipe_log( DWIPE_LOG_FATAL, "Unable to allocate memory for the input buffer." );
		return -1;
	}

	/* Create the pattern buffer */
	d = malloc( c->device_stat.st_blksize * 1024 + pattern->length * 2 );

	/* Check the memory allocation. */
	if( ! d )
	{
		dwipe_perror( errno, __FUNCTION__, "malloc" );
		dwipe_log( DWIPE_LOG_FATAL, "Unable to allocate memory for the pattern buffer." );
		return -1;
	}

	for( q = d ; q < d + c->device_stat.st_blksize * 1024 + pattern->length ; q += pattern->length )
	{
		/* Fill the pattern buffer with the pattern. */
		memcpy( q, pattern->s, pattern->length );
	}

	/* Tell our parent that we are syncing the device. */
	c->sync_status = 1;

	/* Sync the device. */
	r = fdatasync( c->device_fd );

	/* Tell our parent that we have finished syncing the device. */
	c->sync_status = 0;

	if( r != 0 )
	{
		/* FIXME: Is there a better way to handle this? */
		dwipe_perror( errno, __FUNCTION__, "fdatasync" );
		dwipe_log( DWIPE_LOG_WARNING, "Buffer flush failure on '%s'.", c->device_name );
	}


	/* Reset the file pointer. */
	offset = lseek( c->device_fd, 0, SEEK_SET );

	if( offset == (loff_t)-1 )
	{
		dwipe_perror( errno, __FUNCTION__, "lseek" );
		dwipe_log( DWIPE_LOG_FATAL, "Unable to reset the '%s' file offset.", c->device_name );
		return -1;
	}

	if( offset != 0 )
	{
		/* This is system insanity. */
		dwipe_log( DWIPE_LOG_SANITY, "dwipe_static_verify: lseek() returned a bogus offset on '%s'.", c->device_name );
		return -1;
	}


	while( z > 0 )
	{
		if( c->device_stat.st_blksize * 1024 < z )
		{
			blocksize = c->device_stat.st_blksize * 1024;
		}
		else
		{
			/* This is a seatbelt for buggy drivers and programming errors because */
			/* the device size should always be an even multiple of its blocksize. */
			blocksize = z;
			dwipe_log( DWIPE_LOG_WARNING,
			  "%s: The size of '%s' is not a multiple of its block size %i.",
			  __FUNCTION__, c->device_name, c->device_stat.st_blksize );
		}

		/* Fill the output buffer with the random pattern. */
		/* Read the buffer in from the device. */
		r = read( c->device_fd, b, blocksize );

		/* Check the result. */
		if( r < 0 )
		{
			dwipe_perror( errno, __FUNCTION__, "read" );
			dwipe_log( DWIPE_LOG_ERROR, "Unable to read from '%s'.", c->device_name );
			return -1;
		}

		/* Check for a partial read. */
		if( r == blocksize )
		{
			/* Check every byte in the buffer. */
			if( memcmp( b, &d[w], r ) != 0 ) { c->verify_errors += 1; }
		}
		else
		{
			/* The number of bytes that were not read. */
			int s = blocksize - r;

			/* TODO: Handle a partial read. */

			/* Increment the error count. */
			c->verify_errors += 1;
			
			dwipe_log( DWIPE_LOG_WARNING, "Partial read on '%s', %i bytes short.", c->device_name, s );

			/* Bump the file pointer to the next block. */
			offset = lseek( c->device_fd, s, SEEK_CUR );

			if( offset == (loff_t)-1 )
			{
				dwipe_perror( errno, __FUNCTION__, "lseek" );
				dwipe_log( DWIPE_LOG_ERROR, "Unable to bump the '%s' file offset after a partial read.", c->device_name );
				return -1;
			}

		} /* partial read */

		/* Adjust the window. */
		w = ( c->device_stat.st_blksize * 1024 + w ) % pattern->length;

		/* Intuition check: 
		 *   If the pattern length evenly divides the block size
		 *   then ( w == 0 ) always.
		 */

		/* Decrement the bytes remaining in this pass. */
		z -= r;

		/* Increment the total progress counters. */
		c->round_done += r;
		c->pass_done += r;

	} /* while bytes remaining */

	/* Release the buffers. */
	free( b );
	free( d );

	/* We're done. */
	return 0;
		
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
testq %rsi, %rsi
je 0x9b18
movq %rsi, %r14
movl (%rsi), %r13d
testl %r13d, %r13d
jle 0x9b26
movq %rdi, %rbx
movq 0x230(%rdi), %r15
movq 0x270(%rdi), %rbp
shlq $0xa, %rbp
movq %rbp, %rdi
callq 0x3300
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x9b3e
leaq (,%r13,2), %rdi
addq %rbp, %rdi
callq 0x3300
testq %rax, %rax
je 0x9b61
movq %r15, 0x20(%rsp)
movq %r14, 0x18(%rsp)
addq %rax, %rbp
movq %rbp, %rcx
addq %r13, %rcx
movq %rax, 0x8(%rsp)
cmpq %rcx, %rax
jae 0x997f
movq 0x18(%rsp), %rax
movq 0x8(%rax), %r12
movq %r13, %r14
negq %r14
movq 0x8(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x32a0
addq %r13, %r15
leaq (%r15,%r14), %rax
cmpq %rbp, %rax
jb 0x9965
movw $0x1, 0x57c(%rbx)
movl 0x8(%rbx), %edi
callq 0x3080
movw $0x0, 0x57c(%rbx)
testl %eax, %eax
je 0x99d1
callq 0x3070
movl (%rax), %edi
leaq 0x4873(%rip), %rsi      # 0xe21e
leaq 0x45f4(%rip), %rdx      # 0xdfa6
callq 0x7cc3
movq 0x228(%rbx), %rdx
leaq 0x45eb(%rip), %rsi      # 0xdfb0
movl $0x4, %edi
xorl %eax, %eax
callq 0x7a7c
movl 0x8(%rbx), %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3420
cmpq $-0x1, %rax
movq 0x20(%rsp), %r14
je 0x9ba2
testq %rax, %rax
jne 0x9bd1
testq %r14, %r14
je 0x9b00
movq 0x270(%rbx), %r8
xorl %ebp, %ebp
movq %r8, %r13
shlq $0xa, %r13
cmpq %r14, %r13
jb 0x9a37
movq 0x228(%rbx), %rcx
movl $0x4, %edi
leaq 0x45a8(%rip), %rsi      # 0xdfce
leaq 0x47f1(%rip), %rdx      # 0xe21e
xorl %eax, %eax
callq 0x7a7c
movq %r14, %r13
movl 0x8(%rbx), %edi
movq 0x10(%rsp), %rsi
movq %r13, %rdx
callq 0x3240
testl %eax, %eax
js 0x9be6
movl %eax, %r15d
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
cmpq %r13, %r15
jne 0x9a84
movslq %ebp, %r12
movq 0x8(%rsp), %rax
leaq (%rax,%r12), %rsi
movq 0x10(%rsp), %rdi
movq %r13, %rdx
callq 0x3320
testl %eax, %eax
je 0x9ac8
incq 0x588(%rbx)
jmp 0x9ac8
subq %rax, %r13
incq 0x588(%rbx)
movq 0x228(%rbx), %rdx
movl $0x4, %edi
leaq 0x47cf(%rip), %rsi      # 0xe270
movl %r13d, %ecx
xorl %eax, %eax
callq 0x7a7c
movl 0x8(%rbx), %edi
movslq %r13d, %rsi
movl $0x1, %edx
callq 0x3420
cmpq $-0x1, %rax
je 0x9c10
movslq %ebp, %r12
movq 0x270(%rbx), %r8
movq %r8, %rax
shlq $0xa, %rax
addq %r12, %rax
movq 0x18(%rsp), %rcx
movslq (%rcx), %rcx
cqto
idivq %rcx
movq %rdx, %rbp
addq %r15, 0x340(%rbx)
addq %r15, 0x2f0(%rbx)
subq %r15, %r14
jne 0x9a07
movq 0x10(%rsp), %rdi
callq 0x3060
movq 0x8(%rsp), %rdi
callq 0x3060
xorl %eax, %eax
jmp 0x9b93
leaq 0x469e(%rip), %rsi      # 0xe1bd
movl $0x7, %edi
jmp 0x9b87
leaq 0x46bb(%rip), %rsi      # 0xe1e8
movl $0x7, %edi
movl %r13d, %edx
xorl %eax, %eax
callq 0x7a7c
jmp 0x9b8e
callq 0x3070
movl (%rax), %edi
leaq 0x46d2(%rip), %rsi      # 0xe21e
leaq 0x205d(%rip), %rdx      # 0xbbb0
callq 0x7cc3
leaq 0x43e5(%rip), %rsi      # 0xdf44
jmp 0x9b82
callq 0x3070
movl (%rax), %edi
leaq 0x46af(%rip), %rsi      # 0xe21e
leaq 0x203a(%rip), %rdx      # 0xbbb0
callq 0x7cc3
leaq 0x43f2(%rip), %rsi      # 0xdf74
movl $0x6, %edi
xorl %eax, %eax
callq 0x7a7c
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3070
movl (%rax), %edi
leaq 0x466e(%rip), %rsi      # 0xe21e
leaq 0x21e2(%rip), %rdx      # 0xbd99
callq 0x7cc3
movq 0x228(%rbx), %rdx
leaq 0x21fb(%rip), %rsi      # 0xbdc5
movl $0x6, %edi
jmp 0x9c3d
movq 0x228(%rbx), %rdx
leaq 0x4653(%rip), %rsi      # 0xe232
movl $0x7, %edi
jmp 0x9c3d
callq 0x3070
movl (%rax), %edi
leaq 0x462a(%rip), %rsi      # 0xe21e
leaq 0x388c(%rip), %rdx      # 0xd487
callq 0x7cc3
movq 0x228(%rbx), %rdx
leaq 0x43fd(%rip), %rsi      # 0xe00b
jmp 0x9c38
callq 0x3070
movl (%rax), %edi
leaq 0x4600(%rip), %rsi      # 0xe21e
leaq 0x2174(%rip), %rdx      # 0xbd99
callq 0x7cc3
movq 0x228(%rbx), %rdx
leaq 0x4419(%rip), %rsi      # 0xe051
movl $0x5, %edi
xorl %eax, %eax
callq 0x7a7c
jmp 0x9b8e