Description: Fix cases where data might be lost.
Author: Reimar Döffinger <Reimar.Doeffinger@gmx.de>
Origin: upstream, https://github.com/rdoeffinger/iec16022/commit/ebbb6e2849464938c70e5af3934183f232d65134
Bug-Debian: https://bugs.debian.org/835296
Last-Update: 2024-03-15
    
    For some strings like "UEXPLR4-CBR3A3-001-TSK 13471 3216"
    or "BR3A3-001-TSK 13471 3216" the last characters would
    be dropped instead of signalling that the string is not
    possible to encode in the give barcode size.

diff --git a/iec16022ecc200.c b/iec16022ecc200.c
index b9d62fe..22c5a5a 100644
--- a/iec16022ecc200.c
+++ b/iec16022ecc200.c
@@ -327,11 +327,11 @@ static char ecc200encode(unsigned char *t, int tl, unsigned char *s, int sl,
 							}
 						}
 					}
-					if (p == 2 && tp + 2 == tl && sp == sl)
-						out[p++] = 0;	// shift 1 pad at end
-					while (p >= 3) {
-						int v =
-						    out[0] * 1600 +
+					while (p >= 3 || (p && sp == sl)) {
+						int v;
+						while (p < 3) out[p++] = 0; // pad at end
+						if (tp + 2 >= tl) return 0; // not enough space
+						v = out[0] * 1600 +
 						    out[1] * 40 + out[2] + 1;
 						if (enc != newenc) {
 							if (enc == 'c'
@@ -434,6 +434,7 @@ static char ecc200encode(unsigned char *t, int tl, unsigned char *s, int sl,
 					t[tp] = s[sp++] + (((tp + 1) * 149) % 255) + 1;	// see annex H
 					tp++;
 				}
+				if (l) return 0; // not enough space
 				enc = 'a';	// reverse to ASCII at end
 			}
 			break;
