21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Discussion<br />

This function spc_words2bin() uses the wordlist.h file provided on the book’s web<br />

site, and it can be changed as described in Recipe 4.7.<br />

#include <br />

#include <br />

#include <br />

#include "wordlist.h"<br />

#define BITS_IN_LIST 11<br />

#define MAX_WORDLEN 4<br />

unsigned char *spc_words2bin(unsigned char *str, size_t *outlen) {<br />

int cmp, i;<br />

size_t bitsinword, curbits, needed, reslen;<br />

unsigned int ix, min, max;<br />

unsigned char *p = str, *r, *res, word[MAX_WORDLEN + 1];<br />

curbits = reslen = *outlen = 0;<br />

if(!(r = res = (unsigned char *)malloc((strlen(str) + 1) / 2))<br />

return 0;<br />

memset(res, 0, (strlen(str) + 1) / 2);<br />

for (;;) {<br />

while (isspace(*p)) p++;<br />

if (!*p) break;<br />

/* The +1 is because we expect to see a space or a NULL after each and every<br />

* word; otherwise, there's a syntax error.<br />

*/<br />

for (i = 0; i < MAX_WORDLEN + 1; i++) {<br />

if (!*p || isspace(*p)) break;<br />

if (islower(*p)) word[i] = *p++ - ' ';<br />

else if (isupper(*p)) word[i] = *p++;<br />

else {<br />

free(res);<br />

return 0;<br />

}<br />

}<br />

if (i = = MAX_WORDLEN + 1) {<br />

free(res);<br />

return 0;<br />

}<br />

word[i] = 0;<br />

min = 0;<br />

max = (1

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

Saved successfully!

Ooh no, something went wrong!