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.

}<br />

/* Create an array folded 'layers' times, with num_elem elements */<br />

spc_array_t *spc_array_fold(int sz_elem, int num_elem, int layers) {<br />

double size = (sz_elem * num_elem) + sizeof(spc_array_t);<br />

spc_array_t *a;<br />

size = ((double)sz_elem * (double)num_elem) + (double)sizeof(spc_array_t);<br />

if (size > (double)INT_MAX) return 0;<br />

if (!(a = (spc_array_t *)calloc((size_t)size, 1))) return 0;<br />

a->type = SPC_ARRAY_FOLD;<br />

a->sz_elem = sz_elem;<br />

a->num_elem = num_elem;<br />

a->split = layers;<br />

return a;<br />

}<br />

/* Create a flattened array of num_dimen dimensions with num_elem elements per<br />

* dimension, flattened to a single dimension<br />

*/<br />

spc_array_t *spc_array_flat(int sz_elem, int num_elem, int num_dimen) {<br />

double size;<br />

spc_array_t *a;<br />

size = ((double)sz_elem * (double)num_elem * (double)num_dimen) +<br />

(double)sizeof(spc_array_t);<br />

if (size > (double)INT_MAX) return 0;<br />

if (!(a = (spc_array_t *)calloc((size_t)size, 1))) return 0;<br />

a->type = SPC_ARRAY_FLAT;<br />

a->sz_elem = sz_elem;<br />

a->num_elem = num_elem * num_dimen;<br />

a->split = num_dimen;<br />

return a;<br />

}<br />

/* return the real index of element 'idx' in array 'subarray' */<br />

static int array_index(spc_array_t *a, int subarray, int idx) {<br />

int index = -1, num_row, diff;<br />

num_row = a->num_elem / a->split;<br />

switch (a->type) {<br />

case SPC_ARRAY_SPLIT:<br />

if (idx % a->split) index = idx / a->split;<br />

else index = (a->num_elem / a->split) + (idx / a->split);<br />

break;<br />

case SPC_ARRAY_MERGE:<br />

/* a->split = = size diff between array 1 and 2 */<br />

if (a->split < 0) {<br />

subarray = !subarray;<br />

diff = a->split * -1;<br />

} else diff = a->split;<br />

if (!subarray) index = idx + idx / diff;<br />

else index = diff + (idx * (diff + 1));<br />

674 | Chapter 12: Anti-Tampering<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!