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.

for this list is not insignificant. To choose a word, the function chooses an index into<br />

the list of offsets, moves the file pointer to the proper offset, and reads the word.<br />

Word lengths can be determined by computing the difference between the next offset<br />

and the selected one.<br />

#include <br />

#include <br />

#include <br />

#define SPC_WORDLIST_FILE "/usr/share/dict/words"<br />

static FILE *spc_wordlist_file;<br />

static size_t *spc_wordlist_offsets;<br />

static size_t spc_wordlist_shortest;<br />

static unsigned int spc_wordlist_count;<br />

static int load_wordlist(void) {<br />

char buf[80];<br />

FILE *f;<br />

size_t *offsets, shortest, *tmp;<br />

unsigned int count;<br />

if (!(f = fopen(SPC_WORDLIST_FILE, "r"))) return 0;<br />

if (!(offsets = (size_t *)malloc(sizeof(size_t) * 1024))) {<br />

fclose(f);<br />

return 0;<br />

}<br />

count = 0;<br />

shortest = ~0;<br />

offsets[0] = 0;<br />

while (fgets(buf, sizeof(buf), f))<br />

if (buf[strlen(buf) - 1] = = '\n') {<br />

if (!((count + 1) % 1024)) {<br />

if (!(offsets = (size_t *)realloc((tmp = offsets),<br />

sizeof(size_t) * (count + 1025)))) {<br />

fclose(f);<br />

free(tmp);<br />

return 0;<br />

}<br />

}<br />

offsets[++count] = ftell(f);<br />

if (offsets[count] - offsets[count - 1] < shortest)<br />

shortest = offsets[count] - offsets[count - 1];<br />

}<br />

if (!feof(f)) {<br />

fclose(f);<br />

free(offsets);<br />

return 0;<br />

}<br />

if (ftell(f) - offsets[count - 1] < shortest)<br />

shortest = ftell(f) - offsets[count - 1];<br />

Generating Random Passwords and Passphrases | 389<br />

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

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

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

Saved successfully!

Ooh no, something went wrong!