Skip to content

Commit

Permalink
QRspec_getEccSpec() now accepts an int array instead to return multip…
Browse files Browse the repository at this point in the history
…le values instead of returning dynamic allocated array.
  • Loading branch information
fukuchi committed May 14, 2009
1 parent b62c825 commit 9600e4d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 51 deletions.
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
2009.05.14 Kentaro FUKUCHI <fukuchi@megaui.net>
* qrinput.c, qrencode.c, qrspe.c:
- More return value checks. Mainly for ENOMEM error.
* qrspe.[ch], qrencode.c, tests/test_qrspec.c:
- QRspec_getEccSpec() now accepts an int array instead to return multiple
values instead of returning dynamic allocated array.

2009.05.12 Kentaro FUKUCHI <fukuchi@megaui.net>
* bitstream.c:
Expand Down
10 changes: 2 additions & 8 deletions qrencode.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ static void RSblock_init(RSblock *block, int dl, unsigned char *data, int el)
__STATIC QRRawCode *QRraw_new(QRinput *input)
{
QRRawCode *raw;
int *spec;
int spec[6];
int i;
RSblock *rsblock;
unsigned char *p;
Expand All @@ -83,11 +83,7 @@ __STATIC QRRawCode *QRraw_new(QRinput *input)

raw = (QRRawCode *)malloc(sizeof(QRRawCode));
raw->datacode = p;
spec = QRspec_getEccSpec(input->version, input->level);
if(spec == NULL) {
free(raw);
return NULL;
}
QRspec_getEccSpec(input->version, input->level, spec);
raw->version = input->version;
raw->blocks = QRspec_rsBlockNum(spec);
raw->rsblock = (RSblock *)malloc(sizeof(RSblock) * raw->blocks);
Expand All @@ -113,8 +109,6 @@ __STATIC QRRawCode *QRraw_new(QRinput *input)
raw->eccLength = QRspec_rsBlockNum(spec) * QRspec_rsEccCodes1(spec);
raw->count = 0;

free(spec);

return raw;
}

Expand Down
28 changes: 11 additions & 17 deletions qrspec.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,35 +227,29 @@ static const int eccTable[QRSPEC_VERSION_MAX+1][4][2] = {
{{19, 6}, {18, 31}, {34, 34}, {20, 61}},//40
};

int *QRspec_getEccSpec(int version, QRecLevel level)
void QRspec_getEccSpec(int version, QRecLevel level, int spec[6])
{
int b1, b2;
int data, ecc;
int *array;

b1 = eccTable[version][level][0];
b2 = eccTable[version][level][1];
data = QRspec_getDataLength(version, level);
ecc = QRspec_getECCLength(version, level);

array = (int *)malloc(sizeof(int) * 6);
if(array == NULL) return NULL;

if(b2 == 0) {
array[0] = b1;
array[1] = data / b1;
array[2] = ecc / b1;
array[3] = array[4] = array[5] = 0;
spec[0] = b1;
spec[1] = data / b1;
spec[2] = ecc / b1;
spec[3] = spec[4] = spec[5] = 0;
} else {
array[0] = b1;
array[1] = data / (b1 + b2);
array[2] = ecc / (b1 + b2);
array[3] = b2;
array[4] = array[1] + 1;
array[5] = (ecc - (array[2] * b1)) / b2;
spec[0] = b1;
spec[1] = data / (b1 + b2);
spec[2] = ecc / (b1 + b2);
spec[3] = b2;
spec[4] = spec[1] + 1;
spec[5] = (ecc - (spec[2] * b1)) / b2;
}

return array;
}

/******************************************************************************
Expand Down
5 changes: 2 additions & 3 deletions qrspec.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,11 @@ extern int QRspec_maximumWords(QRencodeMode mode, int version);
* Return an array of ECC specification.
* @param version
* @param level
* @return an array of ECC specification contains as following:
* @param spec an array of ECC specification contains as following:
* {# of type1 blocks, # of data code, # of ecc code,
* # of type2 blocks, # of data code, # of ecc code}
* It can be freed by calling free().
*/
int *QRspec_getEccSpec(int version, QRecLevel level);
void QRspec_getEccSpec(int version, QRecLevel level, int spec[6]);

#define QRspec_rsBlockNum(__spec__) (__spec__[0] + __spec__[3])
#define QRspec_rsBlockNum1(__spec__) (__spec__[0])
Expand Down
43 changes: 20 additions & 23 deletions tests/test_qrspec.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,21 @@ void print_eccTable(void)
int i, j;
int ecc;
int data;
int *bl;
int spec[6];

for(i=1; i<=QRSPEC_VERSION_MAX; i++) {
printf("Version %2d\n", i);
for(j=0; j<4; j++) {
bl = QRspec_getEccSpec(i, (QRecLevel)j);
data = bl[0] * bl[1] + bl[3] * bl[4];
ecc = bl[0] * bl[2] + bl[3] * bl[5];
QRspec_getEccSpec(i, (QRecLevel)j, spec);
data = spec[0] * spec[1] + spec[3] * spec[4];
ecc = spec[0] * spec[2] + spec[3] * spec[5];
printf("%3d\t", ecc);
printf("%2d\t", bl[0]);
printf("(%3d, %3d)\n", bl[1]+bl[2], bl[1]);
if(bl[3]>0) {
printf("\t%2d\t", bl[3]);
printf("(%3d, %3d)\n", bl[4]+bl[5], bl[4]);
printf("%2d\t", spec[0]);
printf("(%3d, %3d)\n", spec[1]+spec[2], spec[1]);
if(spec[3]>0) {
printf("\t%2d\t", spec[3]);
printf("(%3d, %3d)\n", spec[4]+spec[5], spec[4]);
}
free(bl);
}
}
}
Expand All @@ -34,25 +33,24 @@ void test_eccTable(void)
int ecc;
int data;
int err = 0;
int *bl;
int spec[6];

testStart("Checking ECC table.");
for(i=1; i<=QRSPEC_VERSION_MAX; i++) {
for(j=0; j<4; j++) {
bl = QRspec_getEccSpec(i, (QRecLevel)j);
data = bl[0] * bl[1] + bl[3] * bl[4];
ecc = bl[0] * bl[2] + bl[3] * bl[5];
QRspec_getEccSpec(i, (QRecLevel)j, spec);
data = spec[0] * spec[1] + spec[3] * spec[4];
ecc = spec[0] * spec[2] + spec[3] * spec[5];
if(data + ecc != QRspec_getDataLength(i, (QRecLevel)j) + QRspec_getECCLength(i, (QRecLevel)j)) {
printf("Error in version %d, level %d: invalid size\n", i, j);
printf("%d %d %d %d %d %d\n", bl[0], bl[1], bl[2], bl[3], bl[4], bl[5]);
printf("%d %d %d %d %d %d\n", spec[0], spec[1], spec[2], spec[3], spec[4], spec[5]);
err++;
}
if(ecc != QRspec_getECCLength(i, (QRecLevel)j)) {
printf("Error in version %d, level %d: invalid data\n", i, j);
printf("%d %d %d %d %d %d\n", bl[0], bl[1], bl[2], bl[3], bl[4], bl[5]);
printf("%d %d %d %d %d %d\n", spec[0], spec[1], spec[2], spec[3], spec[4], spec[5]);
err++;
}
free(bl);
}
}
testEnd(err);
Expand All @@ -64,7 +62,7 @@ void test_eccTable2(void)
int idx;
int err;
int terr = 0;
int *bl;
int spec[6];

const int correct[7][6] = {
{ 8, 1, 0, 2, 60, 38},
Expand All @@ -79,17 +77,16 @@ void test_eccTable2(void)
testStart("Checking ECC table(2)");
for(i=0; i<7; i++) {
err = 0;
bl = QRspec_getEccSpec(correct[i][0], (QRecLevel)correct[i][1]);
QRspec_getEccSpec(correct[i][0], (QRecLevel)correct[i][1], spec);
idx = correct[i][2] * 3;
if(bl[idx] != correct[i][3]) err++;
if(bl[idx+1] + bl[idx+2] != correct[i][4]) err++;
if(bl[idx+1] != correct[i][5]) err++;
if(spec[idx] != correct[i][3]) err++;
if(spec[idx+1] + spec[idx+2] != correct[i][4]) err++;
if(spec[idx+1] != correct[i][5]) err++;
if(err) {
printf("Error in version %d, level %d: invalid data\n",
correct[i][0], correct[i][1]);
terr++;
}
free(bl);
}
testEnd(terr);
}
Expand Down

0 comments on commit 9600e4d

Please sign in to comment.