From f7133a8a56650a2e5cefc66b6d9b87809a890b2e Mon Sep 17 00:00:00 2001 From: Leo Uino Date: Fri, 15 Jul 2011 14:57:03 +0900 Subject: Remove (a few) magic numbers --- lpg/libqr/code-create.c | 33 +++++++++++++-------------------- lpg/libqr/code-layout.c | 4 ++-- lpg/libqr/code-parse.c | 20 ++++++++++---------- lpg/libqr/constants.h | 16 ++++++++++++++++ 4 files changed, 41 insertions(+), 32 deletions(-) (limited to 'lpg/libqr') diff --git a/lpg/libqr/code-create.c b/lpg/libqr/code-create.c index f29778f..d00a41f 100644 --- a/lpg/libqr/code-create.c +++ b/lpg/libqr/code-create.c @@ -170,7 +170,6 @@ static struct qr_bitstream * make_data(int version, struct qr_bitstream * data) { const size_t total_bits = qr_code_total_capacity(version); - const size_t total_words = total_bits / 8; const size_t total_data = QR_DATA_WORD_COUNT[version - 1][ec ^ 0x1]; int block_count[2], data_length[2], ec_length[2]; int total_blocks; @@ -195,7 +194,7 @@ static struct qr_bitstream * make_data(int version, if (!dcopy) goto fail; - if (pad_data(dcopy, total_data * 8) != 0) + if (pad_data(dcopy, total_data * QR_WORD_BITS) != 0) goto fail; fputs("Pad data:\n", stderr); @@ -235,9 +234,8 @@ static struct qr_bitstream * make_data(int version, (i - block_count[0]) * (data_length[1] - data_length[0]) : 0); - qr_bitstream_seek(dcopy, di * 8); - qr_bitstream_write(out, - qr_bitstream_read(dcopy, 8), 8); + qr_bitstream_seek(dcopy, di * QR_WORD_BITS); + qr_bitstream_copy(out, dcopy, QR_WORD_BITS); } } for (i = 0; i < total_blocks; ++i) @@ -245,10 +243,9 @@ static struct qr_bitstream * make_data(int version, assert(block_count[1] == 0 || ec_length[1] == ec_length[0]); for (w = 0; w < ec_length[0]; ++w) for (i = 0; i < total_blocks; ++i) - qr_bitstream_write(out, - qr_bitstream_read(blocks[i], 8), 8); + qr_bitstream_copy(out, blocks[i], QR_WORD_BITS); - qr_bitstream_write(out, 0, total_bits - total_words * 8); + qr_bitstream_write(out, 0, total_bits % QR_WORD_BITS); fputs("Final bitstream:\n", stderr); x_dump(out); @@ -299,8 +296,8 @@ struct qr_code * qr_code_create(const struct qr_data * data) goto fail; qr_bitstream_seek(bits, 0); - while (qr_bitstream_remaining(bits) >= 8) - qr_layout_write(layout, qr_bitstream_read(bits, 8)); + while (qr_bitstream_remaining(bits) >= QR_WORD_BITS) + qr_layout_write(layout, qr_bitstream_read(bits, QR_WORD_BITS)); qr_layout_end(layout); mask = mask_data(code); @@ -566,15 +563,12 @@ static int calc_format_bits(enum qr_ec_level ec, int mask) bits = (ec & 0x3) << 3 | (mask & 0x7); - /* Compute (15, 5) BCH code with - * G(x) = x^10 + x^8 + x^5 + x^4 + x^2 + x + 1 - */ + /* Compute (15, 5) BCH code */ bits <<= 15 - 5; - bits |= (unsigned int)gf_residue(bits, 0x537); + bits |= (unsigned int)gf_residue(bits, QR_FORMAT_POLY); - /* XOR mask: 101 0100 0001 0010 */ - bits ^= 0x5412; + bits ^= QR_FORMAT_MASK; return bits; } @@ -585,11 +579,10 @@ static long calc_version_bits(int version) bits = version & 0x3F; - /* (18, 6) BCH code - * G(x) = x^12 + x^11 + x^10 + x^9 + x^8 + x^5 + x^2 + 1 - */ + /* (18, 6) BCH code */ + bits <<= 18 - 6; - bits |= gf_residue(bits, 0x1F25); + bits |= gf_residue(bits, QR_VERSION_POLY); fprintf(stderr, "version bits: %lx\n", bits); return bits; diff --git a/lpg/libqr/code-layout.c b/lpg/libqr/code-layout.c index 97cf3af..8b4522d 100644 --- a/lpg/libqr/code-layout.c +++ b/lpg/libqr/code-layout.c @@ -167,7 +167,7 @@ unsigned int qr_layout_read(struct qr_iterator * i) unsigned int x = 0; int b; - for (b = 0; b < 8; ++b) { + for (b = 0; b < QR_WORD_BITS; ++b) { x = (x << 1) | ((*i->p & i->mask) ? 1 : 0); advance(i); } @@ -179,7 +179,7 @@ void qr_layout_write(struct qr_iterator * i, unsigned int x) { int b; - for (b = 0; b < 8; ++b) { + for (b = 0; b < QR_WORD_BITS; ++b) { *i->p |= (x & 0x80) ? i->mask : 0; advance(i); x <<= 1; diff --git a/lpg/libqr/code-parse.c b/lpg/libqr/code-parse.c index 690734b..8e36a15 100644 --- a/lpg/libqr/code-parse.c +++ b/lpg/libqr/code-parse.c @@ -30,7 +30,7 @@ static int unpack_bits(int version, { /* FIXME: more comments to explain the algorithm */ - int total_words = qr_code_total_capacity(version) / 8; + int total_words = qr_code_total_capacity(version) / QR_WORD_BITS; int block_count[2], data_length[2], ec_length[2]; int total_blocks; int i, w, block; @@ -42,7 +42,7 @@ static int unpack_bits(int version, status = qr_bitstream_resize(bits_out, (block_count[0] * data_length[0] + - block_count[1] * data_length[1]) * 8); + block_count[1] * data_length[1]) * QR_WORD_BITS); if (status != 0) goto cleanup; @@ -60,7 +60,7 @@ static int unpack_bits(int version, if (blocks[i] == NULL) goto cleanup; status = qr_bitstream_resize(blocks[i], - (data_length[type] + ec_length[type]) * 8); + (data_length[type] + ec_length[type]) * QR_WORD_BITS); if (status != 0) goto cleanup; } @@ -78,7 +78,7 @@ static int unpack_bits(int version, /* Skip the short blocks, if there are any */ block += block_count[0]; } - qr_bitstream_write(blocks[block], qr_bitstream_read(raw_bits, 8), 8); + qr_bitstream_copy(blocks[block], raw_bits, QR_WORD_BITS); block = (block + 1) % total_blocks; } @@ -88,7 +88,7 @@ static int unpack_bits(int version, int type = (block >= block_count[0]); struct qr_bitstream * stream = blocks[block]; qr_bitstream_seek(stream, 0); - qr_bitstream_copy(bits_out, stream, data_length[type] * 8); + qr_bitstream_copy(bits_out, stream, data_length[type] * QR_WORD_BITS); } status = 0; @@ -108,7 +108,7 @@ static int read_bits(const struct qr_code * code, struct qr_bitstream * data_bits) { const size_t total_bits = qr_code_total_capacity(code->version); - const size_t total_words = total_bits / 8; + const size_t total_words = total_bits / QR_WORD_BITS; struct qr_bitstream * raw_bits; struct qr_iterator * layout; int w; @@ -118,7 +118,7 @@ static int read_bits(const struct qr_code * code, if (raw_bits == NULL) goto cleanup; - ret = qr_bitstream_resize(raw_bits, total_words * 8); + ret = qr_bitstream_resize(raw_bits, total_words * QR_WORD_BITS); if (ret != 0) goto cleanup; @@ -126,7 +126,7 @@ static int read_bits(const struct qr_code * code, if (layout == NULL) goto cleanup; for (w = 0; w < total_words; ++w) - qr_bitstream_write(raw_bits, qr_layout_read(layout), 8); + qr_bitstream_write(raw_bits, qr_layout_read(layout), QR_WORD_BITS); qr_layout_end(layout); ret = unpack_bits(code->version, ec, raw_bits, data_bits); @@ -294,7 +294,7 @@ cleanup: int qr_decode_format(unsigned bits, enum qr_ec_level * ec, int * mask) { - bits ^= 0x5412; + bits ^= QR_FORMAT_MASK; /* TODO: check and fix errors */ @@ -321,7 +321,7 @@ int qr_decode_version(unsigned long bits, int * version) /* see calc_version_bits() */ version_bits = v; version_bits <<= 12; - version_bits |= gf_residue(version_bits, 0x1F25); + version_bits |= gf_residue(version_bits, QR_VERSION_POLY); /* count errors */ errors = 0; diff --git a/lpg/libqr/constants.h b/lpg/libqr/constants.h index fbfadc8..e6cfa3c 100644 --- a/lpg/libqr/constants.h +++ b/lpg/libqr/constants.h @@ -3,6 +3,22 @@ #include +/* XOR mask for format data: 101 0100 0001 0010 */ +#define QR_FORMAT_MASK (0x5412) + +/* Format info EC polynomial + * G(x) = x^10 + x^8 + x^5 + x^4 + x^2 + x + 1 + */ +#define QR_FORMAT_POLY (0x537) + +/* Version info EC polynomial + * G(x) = x^12 + x^11 + x^10 + x^9 + x^8 + x^5 + x^2 + 1 + */ +#define QR_VERSION_POLY (0x1F25) + +/* A QR-code word is always 8 bits, but CHAR_BIT might not be */ +#define QR_WORD_BITS (8) + extern const int QR_ALIGNMENT_LOCATION[40][7]; extern const int QR_DATA_WORD_COUNT[40][4]; /* See qr_get_rs_block_sizes() */ -- cgit v1.2.3-70-g09d2