From 851735c1f080668ece6a33cc60dbbd9dacaff09a Mon Sep 17 00:00:00 2001 From: louib Date: Sun, 5 Jan 2020 19:11:24 -0500 Subject: [PATCH] Formatting pass module. --- .github/workflows/ci.yml | 2 +- .github/workflows/ci_ubuntu-16.04.yml | 2 +- src/pass.c | 1263 ++++++++++++------------- src/pass.h | 10 +- 4 files changed, 637 insertions(+), 640 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 62686b4..fd53f9a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,4 +23,4 @@ jobs: - name: verifying code style # TODO use check-format when all the code has been formatted. # run: export PATH=$PATH:/usr/lib/llvm-5.0/bin && make check-format - run: export PATH=$PATH:/usr/lib/llvm-5.0/bin && clang-format -i -style=file src/context.h src/device.h src/device.c src/nwipe.c src/nwipe.h src/options.c src/options.h src/version.h src/version.c src/method.h src/method.c && git diff-index --quiet HEAD + run: export PATH=$PATH:/usr/lib/llvm-5.0/bin && clang-format -i -style=file src/context.h src/device.h src/device.c src/nwipe.c src/nwipe.h src/options.c src/options.h src/version.h src/version.c src/method.h src/method.c src/pass.c src/pass.h && git diff-index --quiet HEAD diff --git a/.github/workflows/ci_ubuntu-16.04.yml b/.github/workflows/ci_ubuntu-16.04.yml index b494c69..55735b2 100644 --- a/.github/workflows/ci_ubuntu-16.04.yml +++ b/.github/workflows/ci_ubuntu-16.04.yml @@ -23,4 +23,4 @@ jobs: - name: verifying code style # TODO use check-format when all the code has been formatted. # run: export PATH=$PATH:/usr/lib/llvm-5.0/bin && make check-format - run: export PATH=$PATH:/usr/lib/llvm-5.0/bin && clang-format -i -style=file src/context.h src/device.h src/device.c src/nwipe.c src/nwipe.h src/options.c src/options.h src/version.h src/version.c src/method.h src/method.c && git diff-index --quiet HEAD + run: export PATH=$PATH:/usr/lib/llvm-5.0/bin && clang-format -i -style=file src/context.h src/device.h src/device.c src/nwipe.c src/nwipe.h src/options.c src/options.h src/version.h src/version.c src/method.h src/method.c src/pass.c src/pass.h && git diff-index --quiet HEAD diff --git a/src/pass.c b/src/pass.c index 515f1f9..cd10b10 100644 --- a/src/pass.c +++ b/src/pass.c @@ -2,9 +2,9 @@ * pass.c: Routines that read and write patterns to block devices. * * Copyright Darik Horn . - * + * * Modifications to original dwipe Copyright Andy Beverley - * + * * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, version 2. @@ -16,7 +16,7 @@ * * You should have received a copy of the GNU General Public License along with * this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * */ @@ -32,806 +32,805 @@ #include "logging.h" #include "gui.h" - int nwipe_random_verify( nwipe_context_t* c ) { -/** - * Verifies that a random pass was correctly written to the device. - * - */ + /** + * Verifies that a random pass was correctly written to the device. + * + */ - /* The result holder. */ - int r; + /* The result holder. */ + int r; - /* The IO size. */ - size_t blocksize; + /* The IO size. */ + size_t blocksize; - /* The result buffer for calls to lseek. */ - off64_t offset; + /* The result buffer for calls to lseek. */ + off64_t offset; - /* The input buffer. */ - char* b; + /* The input buffer. */ + char* b; - /* The pattern buffer that is used to check the input buffer. */ - char* d; + /* The pattern buffer that is used to check the input buffer. */ + char* d; - /* The number of bytes remaining in the pass. */ - u64 z = c->device_size; + /* The number of bytes remaining in the pass. */ + u64 z = c->device_size; + if( c->prng_seed.s == NULL ) + { + nwipe_log( NWIPE_LOG_SANITY, "Null seed pointer." ); + return -1; + } - if( c->prng_seed.s == NULL ) - { - nwipe_log( NWIPE_LOG_SANITY, "Null seed pointer." ); - return -1; - } + if( c->prng_seed.length <= 0 ) + { + nwipe_log( NWIPE_LOG_SANITY, "The entropy length member is %i.", c->prng_seed.length ); + return -1; + } - if( c->prng_seed.length <= 0 ) - { - nwipe_log( NWIPE_LOG_SANITY, "The entropy length member is %i.", c->prng_seed.length ); - return -1; - } + /* Create the input buffer. */ + b = malloc( c->device_stat.st_blksize ); - /* Create the input buffer. */ - b = malloc( c->device_stat.st_blksize ); + /* Check the memory allocation. */ + if( !b ) + { + nwipe_perror( errno, __FUNCTION__, "malloc" ); + nwipe_log( NWIPE_LOG_FATAL, "Unable to allocate memory for the input buffer." ); + return -1; + } - /* Check the memory allocation. */ - if( ! b ) - { - nwipe_perror( errno, __FUNCTION__, "malloc" ); - nwipe_log( NWIPE_LOG_FATAL, "Unable to allocate memory for the input buffer." ); - return -1; - } + /* Create the pattern buffer */ + d = malloc( c->device_stat.st_blksize ); - /* Create the pattern buffer */ - d = malloc( c->device_stat.st_blksize ); + /* Check the memory allocation. */ + if( !d ) + { + nwipe_perror( errno, __FUNCTION__, "malloc" ); + nwipe_log( NWIPE_LOG_FATAL, "Unable to allocate memory for the pattern buffer." ); + free( b ); + return -1; + } - /* Check the memory allocation. */ - if( ! d ) - { - nwipe_perror( errno, __FUNCTION__, "malloc" ); - nwipe_log( NWIPE_LOG_FATAL, "Unable to allocate memory for the pattern buffer." ); - free(b); - return -1; - } + /* Reset the file pointer. */ + offset = lseek( c->device_fd, 0, SEEK_SET ); - /* Reset the file pointer. */ - offset = lseek( c->device_fd, 0, SEEK_SET ); + /* Reset the pass byte counter. */ + c->pass_done = 0; - /* Reset the pass byte counter. */ - c->pass_done = 0; + if( offset == (off64_t) -1 ) + { + nwipe_perror( errno, __FUNCTION__, "lseek" ); + nwipe_log( NWIPE_LOG_FATAL, "Unable to reset the '%s' file offset.", c->device_name ); + free( b ); + free( d ); + return -1; + } - if( offset == (off64_t)-1 ) - { - nwipe_perror( errno, __FUNCTION__, "lseek" ); - nwipe_log( NWIPE_LOG_FATAL, "Unable to reset the '%s' file offset.", c->device_name ); - free(b); - free(d); - return -1; - } + if( offset != 0 ) + { + /* This is system insanity. */ + nwipe_log( NWIPE_LOG_SANITY, "lseek() returned a bogus offset on '%s'.", c->device_name ); + free( b ); + free( d ); + return -1; + } - if( offset != 0 ) - { - /* This is system insanity. */ - nwipe_log( NWIPE_LOG_SANITY, "lseek() returned a bogus offset on '%s'.", c->device_name ); - free(b); - free(d); - return -1; - } + /* Tell our parent that we are syncing the device. */ + c->sync_status = 1; - /* Tell our parent that we are syncing the device. */ - c->sync_status = 1; + /* Sync the device. */ + r = fdatasync( c->device_fd ); - /* Sync the device. */ - r = fdatasync( c->device_fd ); + /* Tell our parent that we have finished syncing the device. */ + c->sync_status = 0; - /* 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? */ + nwipe_perror( errno, __FUNCTION__, "fdatasync" ); + nwipe_log( NWIPE_LOG_WARNING, "Buffer flush failure on '%s'.", c->device_name ); + } - if( r != 0 ) - { - /* FIXME: Is there a better way to handle this? */ - nwipe_perror( errno, __FUNCTION__, "fdatasync" ); - nwipe_log( NWIPE_LOG_WARNING, "Buffer flush failure on '%s'.", c->device_name ); - } + /* Reseed the PRNG. */ + c->prng->init( &c->prng_state, &c->prng_seed ); - /* Reseed the PRNG. */ - c->prng->init( &c->prng_state, &c->prng_seed ); + while( z > 0 ) + { + if( c->device_stat.st_blksize <= z ) + { + blocksize = c->device_stat.st_blksize; + } + 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; + nwipe_log( NWIPE_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 ); + } - while( z > 0 ) - { - if( c->device_stat.st_blksize <= z ) - { - blocksize = c->device_stat.st_blksize; - } - 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; - nwipe_log( NWIPE_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. */ + c->prng->read( &c->prng_state, d, blocksize ); - /* Fill the output buffer with the random pattern. */ - c->prng->read( &c->prng_state, d, blocksize ); + /* Read the buffer in from the device. */ + r = read( c->device_fd, b, blocksize ); - /* Read the buffer in from the device. */ - r = read( c->device_fd, b, blocksize ); + /* Check the result. */ + if( r < 0 ) + { + nwipe_perror( errno, __FUNCTION__, "read" ); + nwipe_log( NWIPE_LOG_ERROR, "Unable to read from '%s'.", c->device_name ); + return -1; + } - /* Check the result. */ - if( r < 0 ) - { - nwipe_perror( errno, __FUNCTION__, "read" ); - nwipe_log( NWIPE_LOG_ERROR, "Unable to read from '%s'.", c->device_name ); - return -1; - } + /* Check for a partial read. */ + if( r != blocksize ) + { + /* TODO: Handle a partial read. */ - /* Check for a partial read. */ - if( r != blocksize ) - { - /* TODO: Handle a partial read. */ + /* The number of bytes that were not read. */ + int s = blocksize - r; - /* The number of bytes that were not read. */ - int s = blocksize - r; + nwipe_log( + NWIPE_LOG_WARNING, "%s: Partial read from '%s', %i bytes short.", __FUNCTION__, c->device_name, s ); - nwipe_log( NWIPE_LOG_WARNING, "%s: Partial read from '%s', %i bytes short.", __FUNCTION__, c->device_name, s ); - - /* Increment the error count. */ - c->verify_errors += 1; + /* Increment the error count. */ + c->verify_errors += 1; - /* Bump the file pointer to the next block. */ - offset = lseek( c->device_fd, s, SEEK_CUR ); + /* Bump the file pointer to the next block. */ + offset = lseek( c->device_fd, s, SEEK_CUR ); - if( offset == (off64_t)-1 ) - { - nwipe_perror( errno, __FUNCTION__, "lseek" ); - nwipe_log( NWIPE_LOG_ERROR, "Unable to bump the '%s' file offset after a partial read.", c->device_name ); - return -1; - } + if( offset == (off64_t) -1 ) + { + nwipe_perror( errno, __FUNCTION__, "lseek" ); + nwipe_log( + NWIPE_LOG_ERROR, "Unable to bump the '%s' file offset after a partial read.", c->device_name ); + return -1; + } - } /* partial read */ + } /* partial read */ - /* Compare buffer contents. */ - if( memcmp( b, d, blocksize ) != 0 ) { c->verify_errors += 1; } + /* Compare buffer contents. */ + if( memcmp( b, d, blocksize ) != 0 ) + { + c->verify_errors += 1; + } - /* Decrement the bytes remaining in this pass. */ - z -= r; + /* Decrement the bytes remaining in this pass. */ + z -= r; - /* Increment the total progress counters. */ - c->pass_done += r; - c->round_done += r; + /* Increment the total progress counters. */ + c->pass_done += r; + c->round_done += r; - pthread_testcancel(); - - } /* while bytes remaining */ + pthread_testcancel(); - /* Release the buffers. */ - free( b ); - free( d ); + } /* while bytes remaining */ - /* We're done. */ - return 0; + /* Release the buffers. */ + free( b ); + free( d ); + + /* We're done. */ + return 0; } /* nwipe_random_verify */ - - int nwipe_random_pass( NWIPE_METHOD_SIGNATURE ) { -/** - * Writes a random pattern to the device. - * - */ + /** + * Writes a random pattern to the device. + * + */ - /* The result holder. */ - int r; + /* The result holder. */ + int r; - /* The IO size. */ - size_t blocksize; + /* The IO size. */ + size_t blocksize; - /* The result buffer for calls to lseek. */ - off64_t offset; + /* The result buffer for calls to lseek. */ + off64_t offset; - /* The output buffer. */ - char* b; + /* The output buffer. */ + char* b; - /* The number of bytes remaining in the pass. */ - u64 z = c->device_size; - - /* Number of writes to do before a fdatasync. */ - int syncRate = nwipe_options.sync; - - /* Counter to track when to do a fdatasync. */ - int i = 0; + /* The number of bytes remaining in the pass. */ + u64 z = c->device_size; - if( c->prng_seed.s == NULL ) - { - nwipe_log( NWIPE_LOG_SANITY, "__FUNCTION__: Null seed pointer." ); - return -1; - } + /* Number of writes to do before a fdatasync. */ + int syncRate = nwipe_options.sync; - if( c->prng_seed.length <= 0 ) - { - nwipe_log( NWIPE_LOG_SANITY, "__FUNCTION__: The entropy length member is %i.", c->prng_seed.length ); - return -1; - } + /* Counter to track when to do a fdatasync. */ + int i = 0; + if( c->prng_seed.s == NULL ) + { + nwipe_log( NWIPE_LOG_SANITY, "__FUNCTION__: Null seed pointer." ); + return -1; + } - /* Create the output buffer. */ - b = malloc( c->device_stat.st_blksize ); + if( c->prng_seed.length <= 0 ) + { + nwipe_log( NWIPE_LOG_SANITY, "__FUNCTION__: The entropy length member is %i.", c->prng_seed.length ); + return -1; + } - /* Check the memory allocation. */ - if( ! b ) - { - nwipe_perror( errno, __FUNCTION__, "malloc" ); - nwipe_log( NWIPE_LOG_FATAL, "Unable to allocate memory for the output buffer." ); - return -1; - } + /* Create the output buffer. */ + b = malloc( c->device_stat.st_blksize ); - /* Seed the PRNG. */ - c->prng->init( &c->prng_state, &c->prng_seed ); + /* Check the memory allocation. */ + if( !b ) + { + nwipe_perror( errno, __FUNCTION__, "malloc" ); + nwipe_log( NWIPE_LOG_FATAL, "Unable to allocate memory for the output buffer." ); + return -1; + } - /* Reset the file pointer. */ - offset = lseek( c->device_fd, 0, SEEK_SET ); + /* Seed the PRNG. */ + c->prng->init( &c->prng_state, &c->prng_seed ); - /* Reset the pass byte counter. */ - c->pass_done = 0; + /* Reset the file pointer. */ + offset = lseek( c->device_fd, 0, SEEK_SET ); - if( offset == (off64_t)-1 ) - { - nwipe_perror( errno, __FUNCTION__, "lseek" ); - nwipe_log( NWIPE_LOG_FATAL, "Unable to reset the '%s' file offset.", c->device_name ); - free(b); - return -1; - } + /* Reset the pass byte counter. */ + c->pass_done = 0; - if( offset != 0 ) - { - /* This is system insanity. */ - nwipe_log( NWIPE_LOG_SANITY, "__FUNCTION__: lseek() returned a bogus offset on '%s'.", c->device_name ); - free(b); - return -1; - } + if( offset == (off64_t) -1 ) + { + nwipe_perror( errno, __FUNCTION__, "lseek" ); + nwipe_log( NWIPE_LOG_FATAL, "Unable to reset the '%s' file offset.", c->device_name ); + free( b ); + return -1; + } + if( offset != 0 ) + { + /* This is system insanity. */ + nwipe_log( NWIPE_LOG_SANITY, "__FUNCTION__: lseek() returned a bogus offset on '%s'.", c->device_name ); + free( b ); + return -1; + } - while( z > 0 ) - { - if( c->device_stat.st_blksize <= z ) - { - blocksize = c->device_stat.st_blksize; - } - 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; - nwipe_log( NWIPE_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 ); - } + while( z > 0 ) + { + if( c->device_stat.st_blksize <= z ) + { + blocksize = c->device_stat.st_blksize; + } + 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; + nwipe_log( NWIPE_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. */ - c->prng->read( &c->prng_state, b, blocksize ); + /* Fill the output buffer with the random pattern. */ + c->prng->read( &c->prng_state, b, blocksize ); - /* Write the next block out to the device. */ - r = write( c->device_fd, b, blocksize ); + /* Write the next block out to the device. */ + r = write( c->device_fd, b, blocksize ); - /* Check the result for a fatal error. */ - if( r < 0 ) - { - nwipe_perror( errno, __FUNCTION__, "write" ); - nwipe_log( NWIPE_LOG_FATAL, "Unable to read from '%s'.", c->device_name ); - return -1; - } + /* Check the result for a fatal error. */ + if( r < 0 ) + { + nwipe_perror( errno, __FUNCTION__, "write" ); + nwipe_log( NWIPE_LOG_FATAL, "Unable to read from '%s'.", c->device_name ); + return -1; + } - /* Check for a partial write. */ - if( r != blocksize ) - { - /* TODO: Handle a partial write. */ + /* Check for a partial write. */ + if( r != blocksize ) + { + /* TODO: Handle a partial write. */ - /* The number of bytes that were not written. */ - int s = blocksize - r; - - /* Increment the error count by the number of bytes that were not written. */ - c->pass_errors += s; + /* The number of bytes that were not written. */ + int s = blocksize - r; - nwipe_log( NWIPE_LOG_WARNING, "Partial write on '%s', %i bytes short.", c->device_name, s ); + /* Increment the error count by the number of bytes that were not written. */ + c->pass_errors += s; - /* Bump the file pointer to the next block. */ - offset = lseek( c->device_fd, s, SEEK_CUR ); + nwipe_log( NWIPE_LOG_WARNING, "Partial write on '%s', %i bytes short.", c->device_name, s ); - if( offset == (off64_t)-1 ) - { - nwipe_perror( errno, __FUNCTION__, "lseek" ); - nwipe_log( NWIPE_LOG_ERROR, "Unable to bump the '%s' file offset after a partial write.", c->device_name ); - return -1; - } + /* Bump the file pointer to the next block. */ + offset = lseek( c->device_fd, s, SEEK_CUR ); - } /* partial write */ + if( offset == (off64_t) -1 ) + { + nwipe_perror( errno, __FUNCTION__, "lseek" ); + nwipe_log( + NWIPE_LOG_ERROR, "Unable to bump the '%s' file offset after a partial write.", c->device_name ); + return -1; + } - /* Decrement the bytes remaining in this pass. */ - z -= r; + } /* partial write */ - /* Increment the total progress counters. */ - c->pass_done += r; - c->round_done += r; - - /* Perodic Sync */ - if( syncRate > 0 ) - { - i++; - - if( i >= syncRate ) - { - /* Tell our parent that we are syncing the device. */ - c->sync_status = 1; + /* Decrement the bytes remaining in this pass. */ + z -= r; - /* Sync the device. */ - r = fdatasync( c->device_fd ); + /* Increment the total progress counters. */ + c->pass_done += r; + c->round_done += r; - /* Tell our parent that we have finished syncing the device. */ - c->sync_status = 0; - - if( r != 0 ) - { - nwipe_perror( errno, __FUNCTION__, "fdatasync" ); - nwipe_log( NWIPE_LOG_WARNING, "Buffer flush failure on '%s'.", c->device_name ); - nwipe_log( NWIPE_LOG_WARNING, "Wrote %llu bytes on '%s'.", c->pass_done, c->device_name); - free(b); - return -1; - } - - i = 0; - } - - } + /* Perodic Sync */ + if( syncRate > 0 ) + { + i++; - pthread_testcancel(); + if( i >= syncRate ) + { + /* Tell our parent that we are syncing the device. */ + c->sync_status = 1; - } /* remaining bytes */ + /* Sync the device. */ + r = fdatasync( c->device_fd ); - /* Release the output buffer. */ - free( b ); + /* Tell our parent that we have finished syncing the device. */ + c->sync_status = 0; - /* Tell our parent that we are syncing the device. */ - c->sync_status = 1; + if( r != 0 ) + { + nwipe_perror( errno, __FUNCTION__, "fdatasync" ); + nwipe_log( NWIPE_LOG_WARNING, "Buffer flush failure on '%s'.", c->device_name ); + nwipe_log( NWIPE_LOG_WARNING, "Wrote %llu bytes on '%s'.", c->pass_done, c->device_name ); + free( b ); + return -1; + } - /* Sync the device. */ - r = fdatasync( c->device_fd ); + i = 0; + } + } - /* Tell our parent that we have finished syncing the device. */ - c->sync_status = 0; + pthread_testcancel(); - if( r != 0 ) - { - /* FIXME: Is there a better way to handle this? */ - nwipe_perror( errno, __FUNCTION__, "fdatasync" ); - nwipe_log( NWIPE_LOG_WARNING, "Buffer flush failure on '%s'.", c->device_name ); - } + } /* remaining bytes */ - /* We're done. */ - return 0; + /* Release the output buffer. */ + free( b ); + + /* 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? */ + nwipe_perror( errno, __FUNCTION__, "fdatasync" ); + nwipe_log( NWIPE_LOG_WARNING, "Buffer flush failure on '%s'.", c->device_name ); + } + + /* We're done. */ + return 0; } /* nwipe_random_pass */ - - int nwipe_static_verify( NWIPE_METHOD_SIGNATURE, nwipe_pattern_t* pattern ) { -/** - * Verifies that a static pass was correctly written to the device. - * - */ + /** + * Verifies that a static pass was correctly written to the device. + */ - /* The result holder. */ - int r; + /* The result holder. */ + int r; - /* The IO size. */ - size_t blocksize; + /* The IO size. */ + size_t blocksize; - /* The result buffer for calls to lseek. */ - off64_t offset; + /* The result buffer for calls to lseek. */ + off64_t offset; - /* The input buffer. */ - char* b; + /* The input buffer. */ + char* b; - /* The pattern buffer that is used to check the input buffer. */ - char* d; + /* The pattern buffer that is used to check the input buffer. */ + char* d; - /* A pointer into the pattern buffer. */ - char* q; + /* A pointer into the pattern buffer. */ + char* q; - /* The pattern buffer window offset. */ - int w = 0; + /* The pattern buffer window offset. */ + int w = 0; - /* The number of bytes remaining in the pass. */ - u64 z = c->device_size; + /* The number of bytes remaining in the pass. */ + u64 z = c->device_size; - if( pattern == NULL ) - { - /* Caught insanity. */ - nwipe_log( NWIPE_LOG_SANITY, "nwipe_static_verify: Null entropy pointer." ); - return -1; - } + if( pattern == NULL ) + { + /* Caught insanity. */ + nwipe_log( NWIPE_LOG_SANITY, "nwipe_static_verify: Null entropy pointer." ); + return -1; + } - if( pattern->length <= 0 ) - { - /* Caught insanity. */ - nwipe_log( NWIPE_LOG_SANITY, "nwipe_static_verify: The pattern length member is %i.", pattern->length ); - return -1; - } + if( pattern->length <= 0 ) + { + /* Caught insanity. */ + nwipe_log( NWIPE_LOG_SANITY, "nwipe_static_verify: The pattern length member is %i.", pattern->length ); + return -1; + } - /* Create the input buffer. */ - b = malloc( c->device_stat.st_blksize ); + /* Create the input buffer. */ + b = malloc( c->device_stat.st_blksize ); - /* Check the memory allocation. */ - if( ! b ) - { - nwipe_perror( errno, __FUNCTION__, "malloc" ); - nwipe_log( NWIPE_LOG_FATAL, "Unable to allocate memory for the input buffer." ); - return -1; - } + /* Check the memory allocation. */ + if( !b ) + { + nwipe_perror( errno, __FUNCTION__, "malloc" ); + nwipe_log( NWIPE_LOG_FATAL, "Unable to allocate memory for the input buffer." ); + return -1; + } - /* Create the pattern buffer */ - d = malloc( c->device_stat.st_blksize + pattern->length * 2 ); + /* Create the pattern buffer */ + d = malloc( c->device_stat.st_blksize + pattern->length * 2 ); - /* Check the memory allocation. */ - if( ! d ) - { - nwipe_perror( errno, __FUNCTION__, "malloc" ); - nwipe_log( NWIPE_LOG_FATAL, "Unable to allocate memory for the pattern buffer." ); - free(b); - return -1; - } + /* Check the memory allocation. */ + if( !d ) + { + nwipe_perror( errno, __FUNCTION__, "malloc" ); + nwipe_log( NWIPE_LOG_FATAL, "Unable to allocate memory for the pattern buffer." ); + free( b ); + return -1; + } - for( q = d ; q < d + c->device_stat.st_blksize + pattern->length ; q += pattern->length ) - { - /* Fill the pattern buffer with the pattern. */ - memcpy( q, pattern->s, pattern->length ); - } + for( q = d; q < d + c->device_stat.st_blksize + 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; + /* Tell our parent that we are syncing the device. */ + c->sync_status = 1; - /* Sync the device. */ - r = fdatasync( c->device_fd ); + /* Sync the device. */ + r = fdatasync( c->device_fd ); - /* Tell our parent that we have finished syncing the device. */ - c->sync_status = 0; + /* 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? */ - nwipe_perror( errno, __FUNCTION__, "fdatasync" ); - nwipe_log( NWIPE_LOG_WARNING, "Buffer flush failure on '%s'.", c->device_name ); - } + if( r != 0 ) + { + /* FIXME: Is there a better way to handle this? */ + nwipe_perror( errno, __FUNCTION__, "fdatasync" ); + nwipe_log( NWIPE_LOG_WARNING, "Buffer flush failure on '%s'.", c->device_name ); + } + /* Reset the file pointer. */ + offset = lseek( c->device_fd, 0, SEEK_SET ); - /* Reset the file pointer. */ - offset = lseek( c->device_fd, 0, SEEK_SET ); + /* Reset the pass byte counter. */ + c->pass_done = 0; - /* Reset the pass byte counter. */ - c->pass_done = 0; + if( offset == (off64_t) -1 ) + { + nwipe_perror( errno, __FUNCTION__, "lseek" ); + nwipe_log( NWIPE_LOG_FATAL, "Unable to reset the '%s' file offset.", c->device_name ); + free( b ); + return -1; + } - if( offset == (off64_t)-1 ) - { - nwipe_perror( errno, __FUNCTION__, "lseek" ); - nwipe_log( NWIPE_LOG_FATAL, "Unable to reset the '%s' file offset.", c->device_name ); - free(b); - return -1; - } + if( offset != 0 ) + { + /* This is system insanity. */ + nwipe_log( NWIPE_LOG_SANITY, "nwipe_static_verify: lseek() returned a bogus offset on '%s'.", c->device_name ); + free( b ); + free( d ); + return -1; + } - if( offset != 0 ) - { - /* This is system insanity. */ - nwipe_log( NWIPE_LOG_SANITY, "nwipe_static_verify: lseek() returned a bogus offset on '%s'.", c->device_name ); - free(b); - free(d); - return -1; - } + while( z > 0 ) + { + if( c->device_stat.st_blksize <= z ) + { + blocksize = c->device_stat.st_blksize; + } + 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; + nwipe_log( NWIPE_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 ); - while( z > 0 ) - { - if( c->device_stat.st_blksize <= z ) - { - blocksize = c->device_stat.st_blksize ; - } - 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; - nwipe_log( NWIPE_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 ); - } + /* Check the result. */ + if( r < 0 ) + { + nwipe_perror( errno, __FUNCTION__, "read" ); + nwipe_log( NWIPE_LOG_ERROR, "Unable to read from '%s'.", c->device_name ); + return -1; + } - /* Fill the output buffer with the random pattern. */ - /* Read the buffer in from the device. */ - r = read( c->device_fd, b, blocksize ); + /* 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; - /* Check the result. */ - if( r < 0 ) - { - nwipe_perror( errno, __FUNCTION__, "read" ); - nwipe_log( NWIPE_LOG_ERROR, "Unable to read from '%s'.", c->device_name ); - return -1; - } + /* TODO: Handle a partial read. */ - /* 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; + /* Increment the error count. */ + c->verify_errors += 1; - /* TODO: Handle a partial read. */ + nwipe_log( NWIPE_LOG_WARNING, "Partial read on '%s', %i bytes short.", c->device_name, s ); - /* Increment the error count. */ - c->verify_errors += 1; - - nwipe_log( NWIPE_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 ); - /* Bump the file pointer to the next block. */ - offset = lseek( c->device_fd, s, SEEK_CUR ); + if( offset == (off64_t) -1 ) + { + nwipe_perror( errno, __FUNCTION__, "lseek" ); + nwipe_log( + NWIPE_LOG_ERROR, "Unable to bump the '%s' file offset after a partial read.", c->device_name ); + return -1; + } - if( offset == (off64_t)-1 ) - { - nwipe_perror( errno, __FUNCTION__, "lseek" ); - nwipe_log( NWIPE_LOG_ERROR, "Unable to bump the '%s' file offset after a partial read.", c->device_name ); - return -1; - } + } /* partial read */ - } /* partial read */ + /* Adjust the window. */ + w = ( c->device_stat.st_blksize + w ) % pattern->length; - /* Adjust the window. */ - w = ( c->device_stat.st_blksize + w ) % pattern->length; + /* Intuition check: + * If the pattern length evenly divides the block size + * then ( w == 0 ) always. + */ - /* Intuition check: - * If the pattern length evenly divides the block size - * then ( w == 0 ) always. - */ + /* Decrement the bytes remaining in this pass. */ + z -= r; - /* Decrement the bytes remaining in this pass. */ - z -= r; + /* Increment the total progress counters. */ + c->pass_done += r; + c->round_done += r; - /* Increment the total progress counters. */ - c->pass_done += r; - c->round_done += r; + pthread_testcancel(); - pthread_testcancel(); + } /* while bytes remaining */ - } /* while bytes remaining */ + /* Release the buffers. */ + free( b ); + free( d ); - /* Release the buffers. */ - free( b ); - free( d ); + /* We're done. */ + return 0; - /* We're done. */ - return 0; - } /* nwipe_static_verify */ - - int nwipe_static_pass( NWIPE_METHOD_SIGNATURE, nwipe_pattern_t* pattern ) { -/** - * Writes a static pattern to the device. - * - */ + /** + * Writes a static pattern to the device. + */ - /* The result holder. */ - int r; + /* The result holder. */ + int r; - /* The IO size. */ - size_t blocksize; + /* The IO size. */ + size_t blocksize; - /* The result buffer for calls to lseek. */ - off64_t offset; + /* The result buffer for calls to lseek. */ + off64_t offset; - /* The output buffer. */ - char* b; + /* The output buffer. */ + char* b; - /* A pointer into the output buffer. */ - char* p; + /* A pointer into the output buffer. */ + char* p; - /* The output buffer window offset. */ - int w = 0; + /* The output buffer window offset. */ + int w = 0; - /* The number of bytes remaining in the pass. */ - u64 z = c->device_size; - - /* Number of writes to do before a fdatasync. */ - int syncRate = nwipe_options.sync; - - /* Counter to track when to do a fdatasync. */ - int i = 0; + /* The number of bytes remaining in the pass. */ + u64 z = c->device_size; - if( pattern == NULL ) - { - /* Caught insanity. */ - nwipe_log( NWIPE_LOG_SANITY, "__FUNCTION__: Null pattern pointer." ); - return -1; - } + /* Number of writes to do before a fdatasync. */ + int syncRate = nwipe_options.sync; - if( pattern->length <= 0 ) - { - /* Caught insanity. */ - nwipe_log( NWIPE_LOG_SANITY, "__FUNCTION__: The pattern length member is %i.", pattern->length ); - return -1; - } + /* Counter to track when to do a fdatasync. */ + int i = 0; - /* Create the output buffer. */ - b = malloc( c->device_stat.st_blksize + pattern->length * 2 ); + if( pattern == NULL ) + { + /* Caught insanity. */ + nwipe_log( NWIPE_LOG_SANITY, "__FUNCTION__: Null pattern pointer." ); + return -1; + } - /* Check the memory allocation. */ - if( ! b ) - { - nwipe_perror( errno, __FUNCTION__, "malloc" ); - nwipe_log( NWIPE_LOG_FATAL, "Unable to allocate memory for the pattern buffer." ); - return -1; - } + if( pattern->length <= 0 ) + { + /* Caught insanity. */ + nwipe_log( NWIPE_LOG_SANITY, "__FUNCTION__: The pattern length member is %i.", pattern->length ); + return -1; + } - for( p = b ; p < b + c->device_stat.st_blksize + pattern->length ; p += pattern->length ) - { - /* Fill the output buffer with the pattern. */ - memcpy( p, pattern->s, pattern->length ); - } -/// - /* Reset the file pointer. */ - offset = lseek( c->device_fd, 0, SEEK_SET ); + /* Create the output buffer. */ + b = malloc( c->device_stat.st_blksize + pattern->length * 2 ); - /* Reset the pass byte counter. */ - c->pass_done = 0; + /* Check the memory allocation. */ + if( !b ) + { + nwipe_perror( errno, __FUNCTION__, "malloc" ); + nwipe_log( NWIPE_LOG_FATAL, "Unable to allocate memory for the pattern buffer." ); + return -1; + } - if( offset == (off64_t)-1 ) - { - nwipe_perror( errno, __FUNCTION__, "lseek" ); - nwipe_log( NWIPE_LOG_FATAL, "Unable to reset the '%s' file offset.", c->device_name ); - return -1; - } + for( p = b; p < b + c->device_stat.st_blksize + pattern->length; p += pattern->length ) + { + /* Fill the output buffer with the pattern. */ + memcpy( p, pattern->s, pattern->length ); + } + /// + /* Reset the file pointer. */ + offset = lseek( c->device_fd, 0, SEEK_SET ); - if( offset != 0 ) - { - /* This is system insanity. */ - nwipe_log( NWIPE_LOG_SANITY, "__FUNCTION__: lseek() returned a bogus offset on '%s'.", c->device_name ); - return -1; - } + /* Reset the pass byte counter. */ + c->pass_done = 0; - while( z > 0 ) - { - if( c->device_stat.st_blksize <= z ) - { - blocksize = c->device_stat.st_blksize ; - } - 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; - nwipe_log( NWIPE_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 ); - } + if( offset == (off64_t) -1 ) + { + nwipe_perror( errno, __FUNCTION__, "lseek" ); + nwipe_log( NWIPE_LOG_FATAL, "Unable to reset the '%s' file offset.", c->device_name ); + return -1; + } - /* Fill the output buffer with the random pattern. */ - /* Write the next block out to the device. */ - r = write( c->device_fd, &b[w], blocksize ); + if( offset != 0 ) + { + /* This is system insanity. */ + nwipe_log( NWIPE_LOG_SANITY, "__FUNCTION__: lseek() returned a bogus offset on '%s'.", c->device_name ); + return -1; + } - /* Check the result for a fatal error. */ - if( r < 0 ) - { - nwipe_perror( errno, __FUNCTION__, "write" ); - nwipe_log( NWIPE_LOG_FATAL, "Unable to write to '%s'.", c->device_name ); - return -1; - } + while( z > 0 ) + { + if( c->device_stat.st_blksize <= z ) + { + blocksize = c->device_stat.st_blksize; + } + 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; + nwipe_log( NWIPE_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 ); + } - /* Check for a partial write. */ - if( r != blocksize ) - { - /* TODO: Handle a partial write. */ + /* Fill the output buffer with the random pattern. */ + /* Write the next block out to the device. */ + r = write( c->device_fd, &b[w], blocksize ); - /* The number of bytes that were not written. */ - int s = blocksize - r; - - /* Increment the error count. */ - c->pass_errors += s; + /* Check the result for a fatal error. */ + if( r < 0 ) + { + nwipe_perror( errno, __FUNCTION__, "write" ); + nwipe_log( NWIPE_LOG_FATAL, "Unable to write to '%s'.", c->device_name ); + return -1; + } - nwipe_log( NWIPE_LOG_WARNING, "Partial write on '%s', %i bytes short.", c->device_name, s ); + /* Check for a partial write. */ + if( r != blocksize ) + { + /* TODO: Handle a partial write. */ - /* Bump the file pointer to the next block. */ - offset = lseek( c->device_fd, s, SEEK_CUR ); + /* The number of bytes that were not written. */ + int s = blocksize - r; - if( offset == (off64_t)-1 ) - { - nwipe_perror( errno, __FUNCTION__, "lseek" ); - nwipe_log( NWIPE_LOG_ERROR, "Unable to bump the '%s' file offset after a partial write.", c->device_name ); - return -1; - } + /* Increment the error count. */ + c->pass_errors += s; - } /* partial write */ + nwipe_log( NWIPE_LOG_WARNING, "Partial write 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 ); - /* Adjust the window. */ - w = ( c->device_stat.st_blksize + w ) % pattern->length; + if( offset == (off64_t) -1 ) + { + nwipe_perror( errno, __FUNCTION__, "lseek" ); + nwipe_log( + NWIPE_LOG_ERROR, "Unable to bump the '%s' file offset after a partial write.", c->device_name ); + return -1; + } - /* Intuition check: - * - * If the pattern length evenly divides the block size - * then ( w == 0 ) always. - */ + } /* partial write */ - /* Decrement the bytes remaining in this pass. */ - z -= r; + /* Adjust the window. */ + w = ( c->device_stat.st_blksize + w ) % pattern->length; - /* Increment the total progress counterr. */ - c->pass_done += r; - c->round_done += r; - - /* Perodic Sync */ - if( syncRate > 0 ) - { - i++; - - if( i >= syncRate ) - { - /* Tell our parent that we are syncing the device. */ - c->sync_status = 1; + /* Intuition check: + * + * If the pattern length evenly divides the block size + * then ( w == 0 ) always. + */ - /* Sync the device. */ - r = fdatasync( c->device_fd ); + /* Decrement the bytes remaining in this pass. */ + z -= r; - /* Tell our parent that we have finished syncing the device. */ - c->sync_status = 0; - - if( r != 0 ) - { - nwipe_perror( errno, __FUNCTION__, "fdatasync" ); - nwipe_log( NWIPE_LOG_WARNING, "Buffer flush failure on '%s'.", c->device_name ); - nwipe_log( NWIPE_LOG_WARNING, "Wrote %llu bytes on '%s'.", c->pass_done, c->device_name); - free(b); - return -1; - } - - i = 0; - } - - } + /* Increment the total progress counterr. */ + c->pass_done += r; + c->round_done += r; - pthread_testcancel(); + /* Perodic Sync */ + if( syncRate > 0 ) + { + i++; - } /* remaining bytes */ + if( i >= syncRate ) + { + /* Tell our parent that we are syncing the device. */ + c->sync_status = 1; - /* Tell our parent that we are syncing the device. */ - c->sync_status = 1; + /* Sync the device. */ + r = fdatasync( c->device_fd ); - /* Sync the device. */ - r = fdatasync( c->device_fd ); + /* Tell our parent that we have finished syncing the device. */ + c->sync_status = 0; - /* Tell our parent that we have finished syncing the device. */ - c->sync_status = 0; + if( r != 0 ) + { + nwipe_perror( errno, __FUNCTION__, "fdatasync" ); + nwipe_log( NWIPE_LOG_WARNING, "Buffer flush failure on '%s'.", c->device_name ); + nwipe_log( NWIPE_LOG_WARNING, "Wrote %llu bytes on '%s'.", c->pass_done, c->device_name ); + free( b ); + return -1; + } - if( r != 0 ) - { - /* FIXME: Is there a better way to handle this? */ - nwipe_perror( errno, __FUNCTION__, "fdatasync" ); - nwipe_log( NWIPE_LOG_WARNING, "Buffer flush failure on '%s'.", c->device_name ); - } + i = 0; + } + } - /* Release the output buffer. */ - free( b ); - - /* We're done. */ - return 0; + pthread_testcancel(); + + } /* remaining bytes */ + + /* 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? */ + nwipe_perror( errno, __FUNCTION__, "fdatasync" ); + nwipe_log( NWIPE_LOG_WARNING, "Buffer flush failure on '%s'.", c->device_name ); + } + + /* Release the output buffer. */ + free( b ); + + /* We're done. */ + return 0; } /* nwipe_static_pass */ - -/* eof */ - diff --git a/src/pass.h b/src/pass.h index dcda129..8f27f43 100644 --- a/src/pass.h +++ b/src/pass.h @@ -2,7 +2,7 @@ * pass.h: Routines that read and write patterns to block devices. * * Copyright Darik Horn . - * + * * Modifications to original dwipe Copyright Andy Beverley * * This program is free software; you can redistribute it and/or modify it under @@ -16,20 +16,18 @@ * * You should have received a copy of the GNU General Public License along with * this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * */ #ifndef PASS_H_ #define PASS_H_ -int nwipe_random_pass ( nwipe_context_t* c ); +int nwipe_random_pass( nwipe_context_t* c ); int nwipe_random_verify( nwipe_context_t* c ); -int nwipe_static_pass ( nwipe_context_t* c, nwipe_pattern_t* pattern ); +int nwipe_static_pass( nwipe_context_t* c, nwipe_pattern_t* pattern ); int nwipe_static_verify( nwipe_context_t* c, nwipe_pattern_t* pattern ); void test_functionn( int count, nwipe_context_t** c ); #endif /* PASS_H_ */ - -/* eof */