21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

spc_file_wipe( )<br />

A wrapper around the first function, which uses a FILE object instead of a file<br />

descriptor. If an error occurs while performing the wipe operation, the return<br />

value is –1; otherwise, a successful operation returns zero.<br />

SpcWipeFile( )<br />

A Windows-specific function that uses the Win32 API for file access. It requires<br />

an open file handle as its only argument and returns a boolean indicating success<br />

or failure.<br />

Note that for all three functions, the file descriptor, FILE object, or file handle passed<br />

as an argument must be open with write access to the file to be wiped; otherwise, the<br />

wiping functions will fail. As written, these functions will probably not work very<br />

well on media other than disk because they are constantly seeking back to the beginning<br />

of the file. Another issue that may arise is filesystem caching. All the writes<br />

made to the file may not actually be written to the physical media.<br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#define SPC_WIPE_BUFSIZE 4096<br />

static int write_data(int fd, const void *buf, size_t nbytes) {<br />

size_t towrite, written = 0;<br />

ssize_t result;<br />

do {<br />

if (nbytes - written > SSIZE_MAX) towrite = SSIZE_MAX;<br />

else towrite = nbytes - written;<br />

if ((result = write(fd, (const char *)buf + written, towrite)) >= 0)<br />

written += result;<br />

else if (errno != EINTR) return 0;<br />

} while (written < nbytes);<br />

return 1;<br />

}<br />

static int random_pass(int fd, size_t nbytes)<br />

{<br />

size_t towrite;<br />

unsigned char buf[SPC_WIPE_BUFSIZE];<br />

if (lseek(fd, 0, SEEK_SET) != 0) return -1;<br />

while (nbytes > 0) {<br />

towrite = (nbytes > sizeof(buf) ? sizeof(buf) : nbytes);<br />

spc_rand(buf, towrite);<br />

if (!write_data(fd, buf, towrite)) return -1;<br />

nbytes -= towrite;<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.<br />

Erasing Files Securely | 49

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!