From 6dbd62c27dfe788bbdec4b524b05cb76b77af045 Mon Sep 17 00:00:00 2001 From: Stefan Bilharz Date: Fri, 21 Feb 2020 18:35:01 +0100 Subject: [PATCH 1/3] Pass message length instead of strlen call strlen stops at the first NUL byte which breaks getting a proper grid when the message contains any of them. The message length is known at this point and can be passed to iec16022init instead of the message pointer. --- ext/iec16022ecc200.c | 7 +++---- ext/semacode.c | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/ext/iec16022ecc200.c b/ext/iec16022ecc200.c index ce4b1fd..c7b81db 100755 --- a/ext/iec16022ecc200.c +++ b/ext/iec16022ecc200.c @@ -778,13 +778,12 @@ encmake (int l, unsigned char *s, int *lenp, char exact) return encoding; } -void iec16022init(int *Wptr, int *Hptr, const char *barcode) +void iec16022init(int *Wptr, int *Hptr, int barcode_length) { - if(Wptr == NULL || Hptr == NULL || barcode == NULL) return; + if(Wptr == NULL || Hptr == NULL) return; - int barcodelen = strlen(barcode) + 1; struct ecc200matrix_s *matrix; - for (matrix = ecc200matrix; matrix->bytes < barcodelen; matrix++); + for (matrix = ecc200matrix; matrix->bytes <= barcode_length; matrix++); *Wptr = matrix->W; *Hptr = matrix->H; } diff --git a/ext/semacode.c b/ext/semacode.c index 5d8aea4..e09e4f7 100644 --- a/ext/semacode.c +++ b/ext/semacode.c @@ -58,7 +58,7 @@ encode_string(semacode_t *semacode, int message_length, char *message) message_length++; // choose the best grid that will hold our message - iec16022init(&semacode->width, &semacode->height, message); + iec16022init(&semacode->width, &semacode->height, message_length); // encode the actual data semacode->data = (char *) iec16022ecc200( From eb696c9d995f13b2124cdb2b23d22df3ef6205cb Mon Sep 17 00:00:00 2001 From: Stefan Bilharz Date: Mon, 24 Feb 2020 11:12:44 +0100 Subject: [PATCH 2/3] Remove weird workaround code It seems odd to believe the underlying library has a bug requires us to always append a blank at the end of the input. There might be some edge cases where this is necessary but I did not encounter any so far. --- ext/semacode.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/ext/semacode.c b/ext/semacode.c index e09e4f7..54a8b4c 100644 --- a/ext/semacode.c +++ b/ext/semacode.c @@ -53,10 +53,6 @@ encode_string(semacode_t *semacode, int message_length, char *message) bzero(semacode, sizeof(semacode_t)); - // work around encoding bug by appending an extra character. - strcat(message, " "); - message_length++; - // choose the best grid that will hold our message iec16022init(&semacode->width, &semacode->height, message_length); From 15d8ff6efbc7552676099a11cb14938d44029d97 Mon Sep 17 00:00:00 2001 From: Stefan Bilharz Date: Mon, 24 Feb 2020 11:33:11 +0100 Subject: [PATCH 3/3] Remove comment about weird workaround code --- ext/semacode.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/ext/semacode.c b/ext/semacode.c index 54a8b4c..5f5518c 100644 --- a/ext/semacode.c +++ b/ext/semacode.c @@ -25,14 +25,9 @@ structure is consulted for any operations, such as to get the semacode dimensions. It deallocates any previous data before generating a new encoding. -Due to a bug in the underlying encoder, we do two things - - * append a space character before encoding, to get around - an off by one error lurking in the C code - - * manually select the best barcode dimensions, to avoid - an encoder bug where sometimes no suitable encoding would - be found +Due to a bug in the underlying encoder, we manually select the best barcode +dimensions to avoid an encoder bug where sometimes no suitable encoding +would be found */ semacode_t*