"Fossies" - the Fresh Open Source Software Archive

Member "CSSC-1.4.1/testutils/uu_decode.c" (7 May 2019, 7611 Bytes) of package /linux/privat/CSSC-1.4.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "uu_decode.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.4.0_vs_1.4.1.

    1 /*
    2  * uu_decode: Part of GNU CSSC.
    3  *
    4  * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010,
    5  * 2001, 2014, 2019 Free Software Foundation, Inc.
    6  *
    7  * This program is free software: you can redistribute it and/or modify
    8  * it under the terms of the GNU General Public License as published by
    9  * the Free Software Foundation, either version 3 of the License, or
   10  * (at your option) any later version.
   11  *
   12  * This program is distributed in the hope that it will be useful,
   13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15  * GNU General Public License for more details.
   16  *
   17  * You should have received a copy of the GNU General Public License
   18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
   19  *
   20  *
   21  * Some of the files in the test suite are provided uuencoded.
   22  * Not all systems have uudecode.  In particular, Cygwin lacks it.
   23  * Hence we provide our own.
   24  *
   25  * $Id: uu_decode.c,v 1.6 2007/12/19 00:21:14 jay Exp $
   26  */
   27 
   28 /*
   29  * This system (uuencoding) will not work on non-ascii machines,
   30  * because it assumed that there is a block of printable characters
   31  * following the space charcter in the execution character set.
   32  *
   33  * Octal is quite convenient for thinking about uuencoding since
   34  * two octal digits make six bits.
   35  *
   36 */
   37 #include <config.h>
   38 
   39 #include <stdio.h>
   40 #include <string.h>
   41 #include <stdlib.h>
   42 #include <errno.h>
   43 
   44 #include <sys/stat.h>
   45 
   46 
   47 
   48 
   49 #define UUDEC(c)        (((c) - 040) & 077)
   50 #define UUENC(c)        (((c) & 077) + 040)
   51 
   52 static inline void
   53 encode(const char in[3], char out[4])
   54 {
   55   /* Notice that the bitmasks always add up to 077. */
   56   out[0] = UUENC(((in[0] >> 2)));
   57   out[1] = UUENC(((in[0] << 4) & 060) | ((in[1] >> 4) & 017));
   58   out[2] = UUENC(((in[1] << 2) & 074) | ((in[2] >> 6) & 003));
   59   out[3] = UUENC(((in[2]       & 077)));
   60 }
   61 
   62 static inline void
   63 decode(const char in[4], char out[3])
   64 {
   65   /* Only the bottom six bits of t0,t1,t2,t3 are ever set,
   66    * but we use ints for their speed not their size.
   67    */
   68   const int t0 = UUDEC(in[0]);
   69   const int t1 = UUDEC(in[1]);
   70   const int t2 = UUDEC(in[2]);
   71   const int t3 = UUDEC(in[3]);
   72 
   73   /* Shift counts always add to six; number of bits
   74    * provided by each line is eight.
   75    *
   76    * A left shift of N provides (8-N) bits of value
   77    * and a right shift provides (6-N) bits of value.
   78    */
   79   out[0] = (t0 << 2) | (t1 >> 4); /* 6 + 2 */
   80   out[1] = (t1 << 4) | (t2 >> 2); /* 4 + 4 */
   81   out[2] = (t2 << 6) | (t3     ); /* 2 + 6 */
   82 }
   83 
   84 
   85 /*
   86  * Lines in the UUENCODEd format look like this:--
   87  *
   88  * M<F]O=#HZ,#HP.G)O;W0Z+W)O;W0Z+V)I;B]B87-H"F)I;CHJ.C$Z,3IB:6XZ
   89  *
   90  * The first character is an uppercase "M".  It's really a
   91  * character count.  "M" represents the largest possible
   92  * count (total line length 60 [M + 60 chars + newline]).
   93  */
   94 
   95 /* decode a line, returning the number of characters in it. */
   96 int
   97 decode_line(const char in[], char out[])
   98 {
   99   int len = UUDEC(in[0]);
  100   int n;
  101 
  102   if (len <= 0)
  103     return 0;
  104 
  105   ++in;                         /* step over byte count. */
  106 
  107   for (n=0; n<len; n+=3)
  108     {
  109       decode(in, out);
  110       in += 4;
  111       out += 3;
  112     }
  113   return len;
  114 }
  115 
  116 
  117 /* encode a line, returning the number of characters in it. */
  118 void
  119 encode_line(const char in[], char out[], int len)
  120 {
  121   *out++ = UUENC(len);
  122 
  123   while (len > 0)
  124     {
  125       encode(in, out);
  126       in += 3;
  127       out += 4;
  128       len -= 3;
  129     }
  130   *out++ = '\n';
  131   *out++ = '\0';
  132 }
  133 
  134 int
  135 encode_stream(FILE *fin, FILE *fout)
  136 {
  137   char inbuf[80], outbuf[80];
  138   int len;
  139 
  140   do
  141     {
  142       len = fread(inbuf, 1, 45, fin);
  143       encode_line(inbuf, outbuf, len);
  144       fprintf(fout, "%s", outbuf);
  145     }
  146   while (len);
  147 
  148   return ferror(fin) || ferror(fout);
  149 }
  150 
  151 
  152 
  153 
  154 int
  155 test_encode(const char *arg)
  156 {
  157     /* Rather than figure out if we support stat, just lie.
  158      * the test suite never uses this anyway.
  159      */
  160     int rv;
  161 
  162     printf("begin 600 %s\n", arg);
  163     rv = encode_stream(stdin, stdout);
  164     printf("end\n");
  165     return rv;
  166 }
  167 
  168 int
  169 test_decode(const char *arg)
  170 {
  171   char inbuf[80], outbuf[80];
  172   int mode, nf, expect_end_line;
  173   FILE *fp_output;
  174 
  175   (void) arg;
  176 
  177   if ( 0 != fgets(inbuf, sizeof(inbuf)-1, stdin))
  178     {
  179       nf = sscanf(inbuf, "begin %o %[^\n]", &mode, outbuf);
  180       if (nf < 1)
  181         {
  182           fprintf(stderr, "No \"begin\" line\n");
  183           return 1;
  184         }
  185       if (nf != 2)
  186         {
  187           fprintf(stderr, "No filename on \"begin\" line\n");
  188           return 1;
  189         }
  190       else
  191         {
  192           fp_output = fopen(outbuf, "wb");
  193           if (NULL == fp_output)
  194             {
  195               perror(outbuf);
  196               return 1;
  197             }
  198           fchmod(fileno(fp_output), mode);
  199         }
  200     }
  201 
  202   expect_end_line = 0;
  203   while ( 0 != fgets(inbuf, sizeof(inbuf)-1, stdin) )
  204     {
  205         if (expect_end_line)
  206         {
  207             if (0 != strcmp(inbuf, "end\n"))
  208             {
  209                 fprintf(stderr, "Expected \"end\" line\n");
  210                 return 1;
  211             }
  212             else
  213             {
  214                 return 0;
  215             }
  216         }
  217         else
  218         {
  219             int len = decode_line(inbuf, outbuf);
  220             if (0 == len)
  221             {
  222                 expect_end_line = 1;
  223             }
  224             else
  225             {
  226                 fwrite(outbuf, 1, len, fp_output);
  227             }
  228         }
  229     }
  230 
  231   if (errno)
  232       perror("Error reading input file");
  233   else
  234       fprintf(stderr, "Unexcpectedly reached end-of-file\n");
  235   return 1;
  236 }
  237 
  238 
  239 /* Test all possible inputs for encode(); decode its
  240  * outputs and check that they decode back to the correct value.
  241  */
  242 int test_all(const char *arg)
  243 {
  244   union lunch { long l; char ch[4]; } in, out;
  245   long l0, l1, l2, lo;
  246   long i;
  247 
  248   (void) arg;
  249 
  250   /* i only has to hold a 24-bit value. */
  251   const long maxval = 0xff | (0xff<<8) | (0xff<<16);
  252   const double dmaxval = maxval;
  253   for (i=0; i<=maxval; i++)
  254     {
  255       if ( 0x7FFFF == (i & 0x7FFFF) )
  256         {
  257           double completed = (100.0 * i) / dmaxval;
  258           printf("%06lx %3.0f%%...\n", i, completed);
  259         }
  260 
  261 
  262       in.ch[0] = (i & 0x0000ff) >>  0;
  263       in.ch[1] = (i & 0x00ff00) >>  8;
  264       in.ch[2] = (i & 0xff0000) >> 16;
  265       in.ch[3] = '\0';
  266 
  267       encode(in.ch, out.ch);
  268       decode(out.ch, in.ch);
  269       l0 = ((unsigned char) in.ch[0]) & 0xff;
  270       l1 = ((unsigned char) in.ch[1]) & 0xff;
  271       l2 = ((unsigned char) in.ch[2]) & 0xff;
  272       lo = l0 | l1<<8 | l2<<16;
  273 
  274       if (lo != i)
  275         {
  276           fprintf(stderr,
  277                   "Asymmetry!\n"
  278                   "Input was %06lx, output was %05lx\n",
  279                   i, lo);
  280           return 1;
  281         }
  282     }
  283   printf("Success!\n");
  284   return 0;
  285 }
  286 
  287 const char *options[] = { "--encode", "--decode", "--all" };
  288 int (* const actions[])(const char *) = { test_encode, test_decode, test_all };
  289 
  290 #define NELEM(array)   (sizeof(array)/sizeof(array[0]))
  291 
  292 
  293 static void
  294 usage(const char *prog)
  295 {
  296   size_t i;
  297   fprintf(stderr, "Usage: %s [", prog ? prog : "uu_decode");
  298   for (i=0; i<NELEM(options); ++i)
  299     {
  300       fprintf(stderr, "%s %s", (i>0) ? " |" : "", options[i]);
  301     }
  302   fprintf(stderr, " ]\n");
  303 }
  304 
  305 int
  306 main(int argc, char *argv[])
  307 {
  308   size_t i;
  309   const char *argument;
  310 
  311   if (argc == 3)
  312   {
  313       argument = argv[2];
  314   }
  315   else if (argc == 2)
  316   {
  317       argument = NULL;
  318   }
  319   else
  320   {
  321       usage(argv[0]);
  322       return 1;
  323   }
  324 
  325   for (i=0; i<NELEM(options); ++i)
  326   {
  327       if (0 == strcmp(options[i], argv[1]))
  328       {
  329           return (actions[i])(argument);
  330       }
  331   }
  332 
  333 
  334   fprintf(stderr, "Unknown option %s\n", argv[1]);
  335   usage(argv[0]);
  336   return 1;
  337 }