#include "cellb_util.h"

/*
 * compute the number of cells that were traversed in num_bytes from the
 * beginning of data.  After function return, num_bytes will contain the
 * number of bytes that were used by num_cells (which may not be equal to the
 * number of bytes passed in
 */
int 
num_cells(Xil_unsigned8 * data, int *num_bytes)
{
    int curbytes = 0;
    int maxbytes = *num_bytes - 4;
    int numcells = 0;

    while (curbytes <= maxbytes) {
	if (data[curbytes] >= 128) {	/* skip code */
	    numcells += data[curbytes] - 127;
	    curbytes++;
	}
	else {
	    numcells++;
	    curbytes += 4;
	}
    }
    while ((curbytes < maxbytes + 4) && (data[curbytes] >= 128)) {
	numcells += data[curbytes] - 127;
	curbytes++;
    }
    *num_bytes = curbytes;
    return numcells;
}

/*
 * add the right number of skip codes for num_to_skip missing cells, return
 * the number of additional bytes
 */
int 
add_skip(Xil_unsigned8 * data, int num_to_skip)
{
    int numbytes = (num_to_skip + 31) >> 5;

    while (num_to_skip > 32) {
	*data++ = 0x80 + 31;
	num_to_skip -= 32;
    }
    *data = 0x80 + num_to_skip - 1;
    return numbytes;
}
