diff options
author | Leo Howell <leo@lwh.jp> | 2009-09-26 11:45:30 +0900 |
---|---|---|
committer | Leo Howell <leo@lwh.jp> | 2009-09-26 13:24:48 +0900 |
commit | 64a96442629b720b2e2b1cc3997ee3dbc6a71b90 (patch) | |
tree | 1f7be34bd5da6f21ac5e91e2539b9dfcc6b400b0 /lpg | |
parent | 825c45bed29693e9420abecdeb4ebcd03aa3accd (diff) | |
download | pdf-simple-sign-64a96442629b720b2e2b1cc3997ee3dbc6a71b90.tar.gz pdf-simple-sign-64a96442629b720b2e2b1cc3997ee3dbc6a71b90.tar.xz pdf-simple-sign-64a96442629b720b2e2b1cc3997ee3dbc6a71b90.zip |
get ready for multiple RS blocks
Diffstat (limited to 'lpg')
-rw-r--r-- | lpg/libqr/code-create.c | 94 | ||||
-rw-r--r-- | lpg/libqr/rs-encode.c | 12 | ||||
-rw-r--r-- | lpg/libqr/rs.h | 4 |
3 files changed, 80 insertions, 30 deletions
diff --git a/lpg/libqr/code-create.c b/lpg/libqr/code-create.c index 389dcd5..9ed59de 100644 --- a/lpg/libqr/code-create.c +++ b/lpg/libqr/code-create.c @@ -26,22 +26,6 @@ static void x_dump(struct bitstream * bits) printf("\n"); } -static int add_ecc(struct bitstream * bits, int format, enum qr_ec_level ec) -{ - puts("Before ecc:"); - x_dump(bits); - { - int rs_words = 10; /* 1-M */ - struct bitstream * rs; - - rs = rs_generate_words(rs_words, bits); - puts("ecc part:"); - x_dump(rs); - bitstream_destroy(rs); - } - return -1; -} - static int pad_data(struct bitstream * bits, size_t limit) { /* This function is not very nice. Sorry. */ @@ -86,23 +70,87 @@ static struct bitstream * make_data(int format, enum qr_ec_level ec, struct bitstream * data) { - size_t data_bits = 16 * 8 /*XXX*/; - struct bitstream * out; - - out = bitstream_copy(data); + const size_t total_bits = code_total_capacity(format); + const size_t total_words = total_bits / 8; + size_t block_count, data_words, rs_words; + size_t i; + struct bitstream * dcopy = 0; + struct bitstream * out = 0; + struct bitstream ** blocks = 0; + + /* Set up the output stream */ + out = bitstream_create(); if (!out) return 0; - if (pad_data(out, data_bits) != 0) + if (bitstream_resize(out, total_bits) != 0) goto fail; - if (add_ecc(out, format, ec) != 0) + /** + * XXX: For our test case (1-M) there is only one RS block. + * This is not the case for most other formats, so we'll + * have to deal with this eventually. + */ + block_count = 1; + data_words = 16; + rs_words = 10; + assert(data_words + rs_words == total_words); + + /* Make a copy of the data and pad it */ + dcopy = bitstream_copy(data); + if (!dcopy) + goto fail; + + if (pad_data(dcopy, data_words * 8) != 0) goto fail; + puts("Pad data:"); + x_dump(dcopy); + + /* Make space for the RS blocks */ + blocks = calloc(block_count, sizeof(*blocks)); + if (!blocks) + goto fail; + + /* Generate RS codewords */ + bitstream_seek(dcopy, 0); + puts("Generate RS blocks:"); + for (i = 0; i < block_count; ++i) { + /* XXX: some blocks may be longer */ + blocks[i] = rs_generate_words(dcopy, data_words, rs_words); + if (!blocks[i]) { + while (i--) + bitstream_destroy(blocks[i]); + free(blocks); + blocks = 0; + goto fail; + } + x_dump(blocks[i]); + } + + /* Finally, write everything out in the correct order */ + /* XXX: need to handle multiple blocks */ + bitstream_cat(out, dcopy); + bitstream_cat(out, blocks[0]); + bitstream_write(out, 0, total_bits - total_words * 8); + + puts("Final bitstream:"); + x_dump(out); +exit: + if (blocks) { + while (block_count--) + bitstream_destroy(blocks[block_count]); + free(blocks); + } + if (dcopy) + bitstream_destroy(dcopy); + return out; + fail: bitstream_destroy(out); - return 0; + out = 0; + goto exit; } struct qr_code * qr_code_create(enum qr_ec_level ec, diff --git a/lpg/libqr/rs-encode.c b/lpg/libqr/rs-encode.c index ea54d77..1e6c6bf 100644 --- a/lpg/libqr/rs-encode.c +++ b/lpg/libqr/rs-encode.c @@ -46,17 +46,17 @@ static unsigned int * make_generator(int k) return g; } -struct bitstream * rs_generate_words(int n, struct bitstream * data) +struct bitstream * rs_generate_words(struct bitstream * data, + size_t data_words, + size_t rs_words) { struct bitstream * ec = 0; unsigned int * b = 0; unsigned int * g; - size_t dlen; + size_t n = rs_words; int i, r; - dlen = bitstream_size(data); - assert(dlen % 8 == 0); - dlen /= 8; + assert(bitstream_remaining(data) >= data_words * 8); ec = bitstream_create(); if (!ec) @@ -75,7 +75,7 @@ struct bitstream * rs_generate_words(int n, struct bitstream * data) /* First, prepare the registers (b) with data bits */ bitstream_seek(data, 0); - for (i = 0; i < dlen; ++i) { + for (i = 0; i < data_words; ++i) { unsigned int x = b[n-1] ^ bitstream_read(data, 8); for (r = n-1; r > 0; --r) b[r] = b[r-1] ^ gf_mult(g[r], x); diff --git a/lpg/libqr/rs.h b/lpg/libqr/rs.h index 48b1f01..e640044 100644 --- a/lpg/libqr/rs.h +++ b/lpg/libqr/rs.h @@ -3,7 +3,9 @@ #include "bitstream.h" -struct bitstream * rs_generate_words(int n, struct bitstream * data); +struct bitstream * rs_generate_words(struct bitstream * data, + size_t data_words, + size_t rs_words); #endif |