Skip to content

Commit

Permalink
Code cleanups and refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
fukuchi committed Aug 15, 2013
1 parent e6d9bf2 commit 76b4461
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 19 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2013.08.15 Kentaro FUKUCHI <kentaro@fukuchi.org>
* rsecc.[ch], qrencode.c, tests/test_rc.c:
- Code cleanups and refactoring.

2013.08.15 Kentaro FUKUCHI <kentaro@fukuchi.org>
* rsecc.[ch], rscode.[ch], Makefile.am, qrencode.c:
- Reed-Solomon error correction code has been completely rewritten.
Expand Down
2 changes: 1 addition & 1 deletion qrencode.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ static void RSblock_initBlock(RSblock *block, int dl, unsigned char *data, int e
block->eccLength = el;
block->ecc = ecc;

RSECC_encode(el, 255 - dl - el, data, ecc);
RSECC_encode(dl, el, data, ecc);
}

static int RSblock_init(RSblock *blocks, int spec[5], unsigned char *data, unsigned char *ecc)
Expand Down
29 changes: 15 additions & 14 deletions rsecc.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@
static int initialized = 0;

#define SYMBOL_SIZE (8)
static const int proot = 0x11d; /* stands for x^8+x^4+x^3+x^2+1 (see pp.37 of JIS X0510:2004) */
#define symbols ((1 << SYMBOL_SIZE) - 1)
#define max_generatorSize (30)
static const int proot = 0x11d; /* stands for x^8+x^4+x^3+x^2+1 (see pp.37 of JIS X0510:2004) */

/* min/max codeword length of ECC, calculated from the specification. */
#define min_length (2)
#define max_length (max_generatorSize)
#define max_length (30)
#define max_generatorSize (max_length)

static unsigned char alpha[symbols + 1];
static unsigned char aindex[symbols + 1];
Expand Down Expand Up @@ -96,7 +97,7 @@ static void generator_init(int length)
generatorInitialized[length - min_length] = 1;
}

int RSECC_encode(int length, int pad, const unsigned char *data, unsigned char *ecc)
int RSECC_encode(int datalength, int ecclength, const unsigned char *data, unsigned char *ecc)
{
int i, j;
unsigned char feedback;
Expand All @@ -106,24 +107,24 @@ int RSECC_encode(int length, int pad, const unsigned char *data, unsigned char *
RSECC_init();
}

if(length > max_length) return -1;
if(ecclength > max_length) return -1;

memset(ecc, 0, length);
if(!generatorInitialized[length - min_length]) generator_init(length);
gen = generator[length - min_length];
memset(ecc, 0, ecclength);
if(!generatorInitialized[ecclength - min_length]) generator_init(ecclength);
gen = generator[ecclength - min_length];

for(i = 0; i < symbols - length - pad; i++) {
for(i = 0; i < datalength; i++) {
feedback = aindex[data[i] ^ ecc[0]];
if(feedback != symbols) {
for(j = 1; j < length; j++) {
ecc[j] ^= alpha[(feedback + gen[length - j]) % symbols];
for(j = 1; j < ecclength; j++) {
ecc[j] ^= alpha[(feedback + gen[ecclength - j]) % symbols];
}
}
memmove(&ecc[0], &ecc[1], length - 1);
memmove(&ecc[0], &ecc[1], ecclength - 1);
if(feedback != symbols) {
ecc[length - 1] = alpha[(feedback + gen[0]) % symbols];
ecc[ecclength - 1] = alpha[(feedback + gen[0]) % symbols];
} else {
ecc[length - 1] = 0;
ecc[ecclength - 1] = 0;
}
}

Expand Down
2 changes: 1 addition & 1 deletion rsecc.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
#ifndef __RSECC_H__
#define __RSECC_H__

extern int RSECC_encode(int length, int pad, const unsigned char *data, unsigned char *ecc);
extern int RSECC_encode(int datalength, int ecclength, const unsigned char *data, unsigned char *ecc);

#endif /* __RSECC_H__ */
6 changes: 3 additions & 3 deletions tests/test_rs.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ static void compareRS(unsigned char data[])
dl = QRspec_rsDataCodes1(spec);
el = QRspec_rsEccCodes1(spec);
rs = init_rs(8, 0x11d, 0, 1, el, 255 - dl - el);
RSECC_encode(el, 255 - dl - el, data, ecc_rscodec);
RSECC_encode(dl, el, data, ecc_rscodec);
encode_rs_char(rs, data, ecc_expected);
assert_zero(memcmp(ecc_expected, ecc_rscodec, el), "Invalid ECC found: length %d.\n", el);
free_rs_char(rs);
Expand All @@ -53,7 +53,7 @@ static void compareRS(unsigned char data[])
el = QRspec_rsEccCodes2(spec);
if(dl != 0) {
rs = init_rs(8, 0x11d, 0, 1, el, 255 - dl - el);
RSECC_encode(el, 255 - dl - el, data, ecc_rscodec);
RSECC_encode(dl, el, data, ecc_rscodec);
encode_rs_char(rs, data, ecc_expected);
assert_zero(memcmp(ecc_expected, ecc_rscodec, el), "Invalid ECC found: length %d.\n", el);
free_rs_char(rs);
Expand All @@ -75,7 +75,7 @@ static void compareRSMQR(unsigned char data[])
el = MQRspec_getECCLength(i, (QRecLevel)j);
if(dl != 0) {
rs = init_rs(8, 0x11d, 0, 1, el, 255 - dl - el);
RSECC_encode(el, 255 - dl - el, data, ecc_rscodec);
RSECC_encode(dl, el, data, ecc_rscodec);
encode_rs_char(rs, data, ecc_expected);
assert_zero(memcmp(ecc_expected, ecc_rscodec, el), "Invalid ECC found: length %d.\n", el);
free_rs_char(rs);
Expand Down

0 comments on commit 76b4461

Please sign in to comment.