]> git.sur5r.net Git - iec16022/blobdiff - iec16022ecc200.c
Imported Upstream version 0.2
[iec16022] / iec16022ecc200.c
index c5bc79ef52e0288aade48b0f8db1d1f6706c8f4b..da67a97b9abf0afcd0e870f78c2ae432608e0058 100644 (file)
@@ -80,8 +80,7 @@ ecc200matrix[] =
 };
 
  // simple checked response malloc
-static void *
-safemalloc (int n)
+static void * safemalloc (int n)
 {
    void *p = malloc (n);
    if (!p)
@@ -93,8 +92,8 @@ safemalloc (int n)
 }
 
 // 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)
    {
@@ -109,8 +108,8 @@ ecc200placementbit (int *array, int NR, int NC, int r, int c, int p, char b)
    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);
@@ -122,8 +121,7 @@ ecc200placementblock (int *array, int NR, int NC, int r, int c, int p)
    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);
@@ -135,8 +133,7 @@ ecc200placementcornerA (int *array, int NR, int NC, int p)
    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);
@@ -148,8 +145,7 @@ ecc200placementcornerB (int *array, int NR, int NC, int p)
    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);
@@ -161,8 +157,7 @@ ecc200placementcornerC (int *array, int NR, int NC, int p)
    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);
@@ -175,8 +170,7 @@ ecc200placementcornerD (int *array, int NR, int NC, int p)
 }
 
 // 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,
@@ -230,8 +224,8 @@ ecc200placement (int *array, int NR, int NC)
 }
 
 // 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;
@@ -252,10 +246,14 @@ ecc200 (unsigned char *binary, int bytes, int datablock, int rsblock)
    }
 }
 
-// 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,
@@ -269,7 +267,8 @@ ecc200encode (unsigned char *t, int tl, unsigned char *s, int sl, char *encoding
    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)
@@ -340,7 +339,8 @@ ecc200encode (unsigned char *t, int tl, unsigned char *s, int sl, char *encoding
                            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;
                         }
                      }
@@ -475,7 +475,10 @@ ecc200encode (unsigned char *t, int tl, unsigned char *s, int sl, char *encoding
    }
    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 
 }
 
@@ -502,25 +505,36 @@ unsigned char switchcost[E_MAX][E_MAX] = {
    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)
@@ -545,7 +559,8 @@ encmake (int l, unsigned char *s, int *lenp, char exact)
       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;
@@ -574,7 +589,9 @@ encmake (int l, unsigned char *s, int *lenp, char exact)
          }
       } 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;
       }
@@ -583,13 +600,15 @@ encmake (int l, unsigned char *s, int *lenp, char exact)
          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;
          }
@@ -618,7 +637,9 @@ encmake (int l, unsigned char *s, int *lenp, char exact)
          }
       } 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;
       }
@@ -627,7 +648,8 @@ encmake (int l, unsigned char *s, int *lenp, char exact)
          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;
@@ -647,7 +669,8 @@ encmake (int l, unsigned char *s, int *lenp, char exact)
       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;
@@ -664,13 +687,15 @@ encmake (int l, unsigned char *s, int *lenp, char exact)
          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;
          }
@@ -690,7 +715,9 @@ encmake (int l, unsigned char *s, int *lenp, char exact)
             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;
@@ -704,7 +731,9 @@ encmake (int l, unsigned char *s, int *lenp, char exact)
                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;
@@ -718,7 +747,9 @@ encmake (int l, unsigned char *s, int *lenp, char exact)
                   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;
@@ -733,14 +764,18 @@ encmake (int l, unsigned char *s, int *lenp, char exact)
                   } 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;
@@ -758,7 +793,9 @@ encmake (int l, unsigned char *s, int *lenp, char exact)
       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;
@@ -767,7 +804,11 @@ encmake (int l, unsigned char *s, int *lenp, char exact)
       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;
@@ -779,7 +820,8 @@ encmake (int l, unsigned char *s, int *lenp, char exact)
            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;
@@ -795,18 +837,24 @@ encmake (int l, unsigned char *s, int *lenp, char exact)
    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,
@@ -825,7 +873,8 @@ iec16022ecc200 (int *Wptr, int *Hptr, char **encodingptr, int barcodelen, unsign
    // 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);
@@ -852,19 +901,22 @@ iec16022ecc200 (int *Wptr, int *Hptr, char **encodingptr, int barcodelen, unsign
       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;
       }
@@ -876,7 +928,8 @@ iec16022ecc200 (int *Wptr, int *Hptr, char **encodingptr, int barcodelen, unsign
       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;
@@ -916,7 +969,8 @@ iec16022ecc200 (int *Wptr, int *Hptr, char **encodingptr, int barcodelen, unsign
             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");
       }