};
// simple checked response malloc
-static void *
-safemalloc (int n)
+static void * safemalloc (int n)
{
void *p = malloc (n);
if (!p)
}
// Annex M placement alorithm low level
-static void
-ecc200placementbit (int *array, int NR, int NC, int r, int c, int p, char b)
+static void ecc200placementbit (int *array, int NR, int NC, int r, int c, \
+ int p, char b)
{
if (r < 0)
{
array[r * NC + c] = (p << 3) + b;
}
-static void
-ecc200placementblock (int *array, int NR, int NC, int r, int c, int p)
+static void ecc200placementblock (int *array, int NR, int NC, int r, \
+ int c, int p)
{
ecc200placementbit (array, NR, NC, r - 2, c - 2, p, 7);
ecc200placementbit (array, NR, NC, r - 2, c - 1, p, 6);
ecc200placementbit (array, NR, NC, r - 0, c - 0, p, 0);
}
-static void
-ecc200placementcornerA (int *array, int NR, int NC, int p)
+static void ecc200placementcornerA (int *array, int NR, int NC, int p)
{
ecc200placementbit (array, NR, NC, NR - 1, 0, p, 7);
ecc200placementbit (array, NR, NC, NR - 1, 1, p, 6);
ecc200placementbit (array, NR, NC, 3, NC - 1, p, 0);
}
-static void
-ecc200placementcornerB (int *array, int NR, int NC, int p)
+static void ecc200placementcornerB (int *array, int NR, int NC, int p)
{
ecc200placementbit (array, NR, NC, NR - 3, 0, p, 7);
ecc200placementbit (array, NR, NC, NR - 2, 0, p, 6);
ecc200placementbit (array, NR, NC, 1, NC - 1, p, 0);
}
-static void
-ecc200placementcornerC (int *array, int NR, int NC, int p)
+static void ecc200placementcornerC (int *array, int NR, int NC, int p)
{
ecc200placementbit (array, NR, NC, NR - 3, 0, p, 7);
ecc200placementbit (array, NR, NC, NR - 2, 0, p, 6);
ecc200placementbit (array, NR, NC, 3, NC - 1, p, 0);
}
-static void
-ecc200placementcornerD (int *array, int NR, int NC, int p)
+static void ecc200placementcornerD (int *array, int NR, int NC, int p)
{
ecc200placementbit (array, NR, NC, NR - 1, 0, p, 7);
ecc200placementbit (array, NR, NC, NR - 1, NC - 1, p, 6);
}
// Annex M placement alorithm main function
-static void
-ecc200placement (int *array, int NR, int NC)
+static void ecc200placement (int *array, int NR, int NC)
{
int r,
c,
}
// calculate and append ecc code, and if necessary interleave
-static void
-ecc200 (unsigned char *binary, int bytes, int datablock, int rsblock)
+static void ecc200 (unsigned char *binary, int bytes, int datablock, \
+ int rsblock)
{
int blocks = (bytes + 2) / datablock,
b;
}
}
-// perform encoding for ecc200, source s len sl, to target t len tl, using optional encoding control string e
-// return 1 if OK, 0 if failed. Does all necessary padding to tl
-char
-ecc200encode (unsigned char *t, int tl, unsigned char *s, int sl, char *encoding, int *lenp)
+/*
+ * perform encoding for ecc200, source s len sl, to target t len tl, using
+ * optional encoding control string e return 1 if OK, 0 if failed. Does all
+ * necessary padding to tl
+ */
+
+char ecc200encode (unsigned char *t, int tl, unsigned char *s, int sl, \
+ char *encoding, int *lenp)
{
char enc = 'a'; // start in ASCII encoding mode
int tp = 0,
while (sp < sl && tp < tl)
{
char newenc = enc; // suggest new encoding
- if (tl - tp <= 1 && (enc == 'c' || enc == 't') || tl - tp <= 2 && enc == 'x')
+ if (tl - tp <= 1 && (enc == 'c' || enc == 't') || tl - tp <= 2 && enc \
+ == 'x')
enc = 'a'; // auto revert to ASCII
newenc = tolower (encoding[sp]);
switch (newenc)
out[p++] = (w - s3);
} else
{
- fprintf (stderr, "Could not encode 0x%02X, should not happen\n", c);
+ fprintf (stderr, "Could not encode 0x%02X, should \
+ not happen\n", c);
return 0;
}
}
}
if (tp > tl || sp < sl)
return 0; // did not fit
- //for (tp = 0; tp < tl; tp++) fprintf (stderr, "%02X ", t[tp]); fprintf (stderr, "\n");
+ /*
+ * for (tp = 0; tp < tl; tp++) fprintf (stderr, "%02X ", t[tp]); \
+ * fprintf (stderr, "\n");
+ */
return 1; // OK
}
0, 1, 1, 1, 1, 0, // From E_BINARY
};
-// Creates a encoding list (malloc)
-// returns encoding string
-// if lenp not null, target len stored
-// if error, null returned
-// if exact specified, then assumes shortcuts applicable for exact fit in target
-// 1. No unlatch to return to ASCII for last encoded byte after C40 or Text or X12
-// 2. No unlatch to return to ASCII for last 1 or 2 encoded bytes after EDIFACT
-// 3. Final C40 or text encoding exactly in last 2 bytes can have a shift 0 to pad to make a tripple
-// Only use the encoding from an exact request if the len matches the target, otherwise free the result and try again with exact=0
-static char *
-encmake (int l, unsigned char *s, int *lenp, char exact)
+/*
+ * Creates a encoding list (malloc)
+ * returns encoding string
+ * if lenp not null, target len stored
+ * if error, null returned
+ * if exact specified, then assumes shortcuts applicable for exact fit
+ * in target
+ * 1. No unlatch to return to ASCII for last encoded byte after C40 or
+ * Text or X12
+ * 2. No unlatch to return to ASCII for last 1 or 2 encoded bytes after
+ * EDIFACT
+ * 3. Final C40 or text encoding exactly in last 2 bytes can have a shift
+ * 0 to pad to make a tripple
+ * Only use the encoding from an exact request if the len matches the target,
+ * otherwise free the result and try again with exact=0
+ */
+
+static char * encmake (int l, unsigned char *s, int *lenp, char exact)
{
char *encoding = 0;
int p = l;
char e;
struct
{
- short s; // number of bytes of source that can be encoded in a row at this point using this encoding mode
- short t; // number of bytes of target generated encoding from this point to end if already in this encoding mode
+ // number of bytes of source that can be encoded in a row at this point
+ // using this encoding mode
+ short s;
+ // number of bytes of target generated encoding from this point to end if
+ // already in this encoding mode
+ short t;
} enc[MAXBARCODE][E_MAX];
memset (&enc, 0, sizeof (enc));
if (!l)
bl = 0;
if (p + sl < l)
for (e = 0; e < E_MAX; e++)
- if (enc[p + sl][e].t && ((t = enc[p + sl][e].t + switchcost[E_ASCII][e]) < bl || !bl))
+ if (enc[p + sl][e].t && ((t = enc[p + sl][e].t + \
+ switchcost[E_ASCII][e]) < bl || !bl))
{
bl = t;
b = e;
}
} while (sub && p + sl < l);
if (exact && sub == 2 && p + sl == l)
- { // special case, can encode last block with shift 0 at end (Is this valid when not end of target buffer?)
+ {
+ // special case, can encode last block with shift 0 at end (Is this
+ // valid when not end of target buffer?)
sub = 0;
tl += 2;
}
bl = 0;
if (p + sl < l)
for (e = 0; e < E_MAX; e++)
- if (enc[p + sl][e].t && ((t = enc[p + sl][e].t + switchcost[E_C40][e]) < bl || !bl))
+ if (enc[p + sl][e].t && ((t = enc[p + sl][e].t + \
+ switchcost[E_C40][e]) < bl || !bl))
{
bl = t;
b = e;
}
if (exact && enc[p + sl][E_ASCII].t == 1 && 1 < bl)
- { // special case, switch to ASCII for last bytes
+ {
+ // special case, switch to ASCII for last bytes
bl = 1;
b = E_ASCII;
}
}
} while (sub && p + sl < l);
if (exact && sub == 2 && p + sl == l)
- { // special case, can encode last block with shift 0 at end (Is this valid when not end of target buffer?)
+ {
+ // special case, can encode last block with shift 0 at end (Is this
+ // valid when not end of target buffer?)
sub = 0;
tl += 2;
}
bl = 0;
if (p + sl < l)
for (e = 0; e < E_MAX; e++)
- if (enc[p + sl][e].t && ((t = enc[p + sl][e].t + switchcost[E_TEXT][e]) < bl || !bl))
+ if (enc[p + sl][e].t && ((t = enc[p + sl][e].t + \
+ switchcost[E_TEXT][e]) < bl || !bl))
{
bl = t;
b = e;
do
{
unsigned char c = s[p + sl++];
- if (c != 13 && c != '*' && c != '>' && c != ' ' && !isdigit (c) && !isupper (c))
+ if (c != 13 && c != '*' && c != '>' && c != ' ' && !isdigit (c) && \
+ !isupper (c))
{
sl = 0;
break;
bl = 0;
if (p + sl < l)
for (e = 0; e < E_MAX; e++)
- if (enc[p + sl][e].t && ((t = enc[p + sl][e].t + switchcost[E_X12][e]) < bl || !bl))
+ if (enc[p + sl][e].t && ((t = enc[p + sl][e].t + \
+ switchcost[E_X12][e]) < bl || !bl))
{
bl = t;
b = e;
}
if (exact && enc[p + sl][E_ASCII].t == 1 && 1 < bl)
- { // special case, switch to ASCII for last bytes
+ {
+ // special case, switch to ASCII for last bytes
bl = 1;
b = E_ASCII;
}
bs = 1;
} else
for (e = 0; e < E_MAX; e++)
- if (e != E_EDIFACT && enc[p + 1][e].t && ((t = 2 + enc[p + 1][e].t + switchcost[E_ASCII][e]) < bl || !bl)) // E_ASCII as allowed for unlatch
+ if (e != E_EDIFACT && enc[p + 1][e].t && ((t = 2 + \
+ enc[p + 1][e].t + switchcost[E_ASCII][e]) \
+ < bl || !bl)) // E_ASCII as allowed for unlatch
{
bs = 1;
bl = t;
bs = 2;
} else
for (e = 0; e < E_MAX; e++)
- if (e != E_EDIFACT && enc[p + 2][e].t && ((t = 3 + enc[p + 2][e].t + switchcost[E_ASCII][e]) < bl || !bl)) // E_ASCII as allowed for unlatch
+ if (e != E_EDIFACT && enc[p + 2][e].t && ((t = 3 + \
+ enc[p + 2][e].t + switchcost[E_ASCII][e]) \
+ < bl || !bl)) // E_ASCII as allowed for unlatch
{
bs = 2;
bl = t;
bs = 3;
} else
for (e = 0; e < E_MAX; e++)
- if (e != E_EDIFACT && enc[p + 3][e].t && ((t = 3 + enc[p + 3][e].t + switchcost[E_ASCII][e]) < bl || !bl)) // E_ASCII as allowed for unlatch
+ if (e != E_EDIFACT && enc[p + 3][e].t && ((t = 3 + \
+ enc[p + 3][e].t + switchcost[E_ASCII][e]) \
+ < bl || !bl)) // E_ASCII as allowed for unlatch
{
bs = 3;
bl = t;
} else
{
for (e = 0; e < E_MAX; e++)
- if (enc[p + 4][e].t && ((t = 3 + enc[p + 4][e].t + switchcost[E_EDIFACT][e]) < bl || !bl))
+ if (enc[p + 4][e].t && ((t = 3 + enc[p + 4][e].t + \
+ switchcost[E_EDIFACT][e]) < bl || !bl))
{
bs = 4;
bl = t;
b = e;
}
- if (exact && enc[p + 4][E_ASCII].t && enc[p + 4][E_ASCII].t <= 2 && (t = 3 + enc[p + 4][E_ASCII].t) < bl)
- { // special case, switch to ASCII for last 1 ot two bytes
+ if (exact && enc[p + 4][E_ASCII].t && enc[p + \
+ 4][E_ASCII].t <= 2 && (t = 3 + enc[p + \
+ 4][E_ASCII].t) < bl)
+ {
+ // special case, switch to ASCII for last 1 ot two bytes
bs = 4;
bl = t;
b = E_ASCII;
bl = 0;
for (e = 0; e < E_MAX; e++)
if (enc[p + 1][e].t
- && ((t = enc[p + 1][e].t + switchcost[E_BINARY][e] + ((e == E_BINARY && enc[p + 1][e].t == 249) ? 1 : 0)) < bl || !bl))
+ && ((t = enc[p + 1][e].t + switchcost[E_BINARY][e] + ((e == \
+ E_BINARY && enc[p + 1][e].t == 249) ? 1 : 0)) \
+ < bl || !bl))
{
bl = t;
b = e;
enc[p][E_BINARY].s = 1;
if (bl && b == E_BINARY)
enc[p][b].s += enc[p + 1][b].s;
- //fprintf (stderr, "%d:", p); for (e = 0; e < E_MAX; e++) fprintf (stderr, " %c*%d/%d", encchr[e], enc[p][e].s, enc[p][e].t); fprintf (stderr, "\n");
+ /*
+ * fprintf (stderr, "%d:", p); for (e = 0; e < E_MAX; e++) fprintf \
+ * (stderr, " %c*%d/%d", encchr[e], enc[p][e].s, enc[p][e].t); \
+ * fprintf (stderr, "\n");
+ */
}
encoding = safemalloc (l + 1);
p = 0;
m = 0;
char b = 0;
for (e = 0; e < E_MAX; e++)
- if (enc[p][e].t && ((t = enc[p][e].t + switchcost[cur][e]) < m || t == m && e == cur || !m))
+ if (enc[p][e].t && ((t = enc[p][e].t + switchcost[cur][e]) < m || \
+ t == m && e == cur || !m))
{
b = e;
m = t;
encoding[p] = 0;
return encoding;
}
+/*
+ * Main encoding function
+ * Returns the grid (malloced) containing the matrix. L corner at 0,0.
+ * Takes suggested size in *Wptr, *Hptr, or 0,0. Fills in actual size.
+ * Takes barcodelen and barcode to be encoded
+ * Note, if *encodingptr is null, then fills with auto picked (malloced)
+ * encoding
+ * If lenp not null, then the length of encoded data before any final
+ * unlatch or pad is stored
+ * If maxp not null, then the max storage of this size code is stored
+ * If eccp not null, then the number of ecc bytes used in this size is
+ * stored
+ * Returns 0 on error (writes to stderr with details).
+ */
-// Main encoding function
-// Returns the grid (malloced) containing the matrix. L corner at 0,0.
-// Takes suggested size in *Wptr, *Hptr, or 0,0. Fills in actual size.
-// Takes barcodelen and barcode to be encoded
-// Note, if *encodingptr is null, then fills with auto picked (malloced) encoding
-// If lenp not null, then the length of encoded data before any final unlatch or pad is stored
-// If maxp not null, then the max storage of this size code is stored
-// If eccp not null, then the number of ecc bytes used in this size is stored
-// Returns 0 on error (writes to stderr with details).
-unsigned char *
-iec16022ecc200 (int *Wptr, int *Hptr, char **encodingptr, int barcodelen, unsigned char *barcode, int *lenp, int *maxp, int *eccp)
+unsigned char * iec16022ecc200 (int *Wptr, int *Hptr, char **encodingptr, \
+ int barcodelen, unsigned char *barcode, \
+ int *lenp, int *maxp, int *eccp)
{
unsigned char binary[3000]; // encoded raw data and ecc to place in barcode
int W = 0,
// encoding
if (W)
{ // known size
- for (matrix = ecc200matrix; matrix->W && (matrix->W != W || matrix->H != H); matrix++);
+ for (matrix = ecc200matrix; matrix->W && (matrix->W != W || \
+ matrix->H != H); matrix++);
if (!matrix->W)
{
fprintf (stderr, "Invalid size %dx%d\n", W, H);
if (encoding)
{ // find one that fits chosen encoding
for (matrix = ecc200matrix; matrix->W; matrix++)
- if (ecc200encode (binary, matrix->bytes, barcode, barcodelen, encoding, 0))
+ if (ecc200encode (binary, matrix->bytes, barcode, barcodelen, \
+ encoding, 0))
break;
} else
{
int len;
char *e;
e = encmake (barcodelen, barcode, &len, 1);
- for (matrix = ecc200matrix; matrix->W && matrix->bytes != len; matrix++);
+ for (matrix = ecc200matrix; matrix->W && matrix->bytes != len; \
+ matrix++);
if (e && !matrix->W)
{ // try for non exact fit
free (e);
e = encmake (barcodelen, barcode, &len, 0);
- for (matrix = ecc200matrix; matrix->W && matrix->bytes < len; matrix++);
+ for (matrix = ecc200matrix; matrix->W && matrix->bytes < len; \
+ matrix++);
}
encoding = e;
}
W = matrix->W;
H = matrix->H;
}
- if (!ecc200encode (binary, matrix->bytes, barcode, barcodelen, encoding, lenp))
+ if (!ecc200encode (binary, matrix->bytes, barcode, barcodelen, \
+ encoding, lenp))
{
fprintf (stderr, "Barcode too long for %dx%d\n", W, H);
return 0;
int v = places[(NR - y - 1) * NC + x];
//fprintf (stderr, "%4d", v);
if (v == 1 || v > 7 && (binary[(v >> 3) - 1] & (1 << (v & 7))))
- grid[(1 + y + 2 * (y / (matrix->FH - 2))) * W + 1 + x + 2 * (x / (matrix->FW - 2))] = 1;
+ grid[(1 + y + 2 * (y / (matrix->FH - 2))) * W + 1 + x + 2 * \
+ (x / (matrix->FW - 2))] = 1;
}
//fprintf (stderr, "\n");
}