home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / sharutil.2 / sharutil / sharutils-4.2 / src / uudecode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-02  |  12.4 KB  |  449 lines

  1. /* uudecode utility.
  2.    Copyright (C) 1994, 1995 Free Software Foundation, Inc.
  3.  
  4.    This product is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This product is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this product; see the file COPYING.  If not, write to
  16.    the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  17.    02111-1307, USA.  */
  18.  
  19. /* Copyright (c) 1983 Regents of the University of California.
  20.    All rights reserved.
  21.  
  22.    Redistribution and use in source and binary forms, with or without
  23.    modification, are permitted provided that the following conditions
  24.    are met:
  25.    1. Redistributions of source code must retain the above copyright
  26.       notice, this list of conditions and the following disclaimer.
  27.    2. Redistributions in binary form must reproduce the above copyright
  28.       notice, this list of conditions and the following disclaimer in the
  29.       documentation and/or other materials provided with the distribution.
  30.    3. All advertising materials mentioning features or use of this software
  31.       must display the following acknowledgement:
  32.      This product includes software developed by the University of
  33.      California, Berkeley and its contributors.
  34.    4. Neither the name of the University nor the names of its contributors
  35.       may be used to endorse or promote products derived from this software
  36.       without specific prior written permission.
  37.  
  38.    THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  39.    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40.    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  41.    ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  42.    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  43.    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  44.    OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  45.    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  46.    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  47.    OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  48.    SUCH DAMAGE.  */
  49.  
  50. /* Reworked to GNU style by Ian Lance Taylor, ian@airs.com, August 93.  */
  51.  
  52. #include "system.h"
  53.  
  54. /*=====================================================================\
  55. | uudecode [FILE ...]                               |
  56. |                                        |
  57. | Create the specified FILE, decoding as you go.  Used with uuencode.  |
  58. \=====================================================================*/
  59.  
  60. #include <pwd.h>
  61. #include "getopt.h"
  62.  
  63. struct passwd *getpwnam ();
  64.  
  65. static struct option longopts[] =
  66. {
  67.   { "version", no_argument, NULL, 'v' },
  68.   { "help", no_argument, NULL, 'h' },
  69.   { "output-file", required_argument, NULL, 'o' },
  70.   { NULL, 0, NULL, 0 }
  71. };
  72.  
  73. static int read_stduu __P ((const char *inname));
  74. static int read_base64 __P ((const char *inname));
  75. static int decode __P ((const char *, const char *));
  76. static void usage __P ((int));
  77.  
  78. /* The name this program was run with. */
  79. const char *program_name;
  80.  
  81. /* Single character decode.  */
  82. #define    DEC(Char) (((Char) - ' ') & 077)
  83.  
  84.  
  85. static int
  86. read_stduu (inname)
  87.      const char *inname;
  88. {
  89.   char buf[2 * BUFSIZ];
  90.  
  91.   while (1)
  92.     {
  93.       int n;
  94.       char *p;
  95.  
  96.       if (fgets (buf, sizeof(buf), stdin) == NULL)
  97.     {
  98.       error (0, 0, _("%s: Short file"), inname);
  99.       return 1;
  100.     }
  101.       p = buf;
  102.  
  103.       /* N is used to avoid writing out all the characters at the end of
  104.      the file.  */
  105.  
  106.       n = DEC (*p);
  107.       if (n <= 0)
  108.     break;
  109.       for (++p; n > 0; p += 4, n -= 3)
  110.     {
  111.       char ch;
  112.  
  113.       if (n >= 3)
  114.         {
  115.           ch = DEC (p[0]) << 2 | DEC (p[1]) >> 4;
  116.           putchar (ch);
  117.           ch = DEC (p[1]) << 4 | DEC (p[2]) >> 2;
  118.           putchar (ch);
  119.           ch = DEC (p[2]) << 6 | DEC (p[3]);
  120.           putchar (ch);
  121.         }
  122.       else
  123.         {
  124.           if (n >= 1)
  125.         {
  126.           ch = DEC (p[0]) << 2 | DEC (p[1]) >> 4;
  127.           putchar (ch);
  128.         }
  129.           if (n >= 2)
  130.         {
  131.           ch = DEC (p[1]) << 4 | DEC (p[2]) >> 2;
  132.           putchar (ch);
  133.         }
  134.         }
  135.     }
  136.     }
  137.  
  138.   if (fgets (buf, sizeof(buf), stdin) == NULL
  139.       || strcmp (buf, "end\n"))
  140.     {
  141.       error (0, 0, _("%s: No `end' line"), inname);
  142.       return 1;
  143.     }
  144.  
  145.   return 0;
  146. }
  147.  
  148. static int
  149. read_base64 (inname)
  150.      const char *inname;
  151. {
  152.   static const char b64_tab[256] =
  153.   {
  154.     '\177', '\177', '\177', '\177', '\177', '\177', '\177', '\177', /*000-007*/
  155.     '\177', '\177', '\177', '\177', '\177', '\177', '\177', '\177', /*010-017*/
  156.     '\177', '\177', '\177', '\177', '\177', '\177', '\177', '\177', /*020-027*/
  157.     '\177', '\177', '\177', '\177', '\177', '\177', '\177', '\177', /*030-037*/
  158.     '\177', '\177', '\177', '\177', '\177', '\177', '\177', '\177', /*040-047*/
  159.     '\177', '\177', '\177', '\76',  '\177', '\177', '\177', '\77',  /*050-057*/
  160.     '\64',  '\65',  '\66',  '\67',  '\70',  '\71',  '\72',  '\73',  /*060-067*/
  161.     '\74',  '\75',  '\177', '\177', '\177', '\100', '\177', '\177', /*070-077*/
  162.     '\177', '\0',   '\1',   '\2',   '\3',   '\4',   '\5',   '\6',   /*100-107*/
  163.     '\7',   '\10',  '\11',  '\12',  '\13',  '\14',  '\15',  '\16',  /*110-117*/
  164.     '\17',  '\20',  '\21',  '\22',  '\23',  '\24',  '\25',  '\26',  /*120-127*/
  165.     '\27',  '\30',  '\31',  '\177', '\177', '\177', '\177', '\177', /*130-137*/
  166.     '\177', '\32',  '\33',  '\34',  '\35',  '\36',  '\37',  '\40',  /*140-147*/
  167.     '\41',  '\42',  '\43',  '\44',  '\45',  '\46',  '\47',  '\50',  /*150-157*/
  168.     '\51',  '\52',  '\53',  '\54',  '\55',  '\56',  '\57',  '\60',  /*160-167*/
  169.     '\61',  '\62',  '\63',  '\177', '\177', '\177', '\177', '\177', /*170-177*/
  170.     '\177', '\177', '\177', '\177', '\177', '\177', '\177', '\177', /*200-207*/
  171.     '\177', '\177', '\177', '\177', '\177', '\177', '\177', '\177', /*210-217*/
  172.     '\177', '\177', '\177', '\177', '\177', '\177', '\177', '\177', /*220-227*/
  173.     '\177', '\177', '\177', '\177', '\177', '\177', '\177', '\177', /*230-237*/
  174.     '\177', '\177', '\177', '\177', '\177', '\177', '\177', '\177', /*240-247*/
  175.     '\177', '\177', '\177', '\177', '\177', '\177', '\177', '\177', /*250-257*/
  176.     '\177', '\177', '\177', '\177', '\177', '\177', '\177', '\177', /*260-267*/
  177.     '\177', '\177', '\177', '\177', '\177', '\177', '\177', '\177', /*270-277*/
  178.     '\177', '\177', '\177', '\177', '\177', '\177', '\177', '\177', /*300-307*/
  179.     '\177', '\177', '\177', '\177', '\177', '\177', '\177', '\177', /*310-317*/
  180.     '\177', '\177', '\177', '\177', '\177', '\177', '\177', '\177', /*320-327*/
  181.     '\177', '\177', '\177', '\177', '\177', '\177', '\177', '\177', /*330-337*/
  182.     '\177', '\177', '\177', '\177', '\177', '\177', '\177', '\177', /*340-347*/
  183.     '\177', '\177', '\177', '\177', '\177', '\177', '\177', '\177', /*350-357*/
  184.     '\177', '\177', '\177', '\177', '\177', '\177', '\177', '\177', /*360-367*/
  185.     '\177', '\177', '\177', '\177', '\177', '\177', '\177', '\177', /*370-377*/
  186.   };
  187.   unsigned char buf[2 * BUFSIZ];
  188.  
  189.   while (1)
  190.     {
  191.       int last_data = 0;
  192.       unsigned char *p;
  193.  
  194.       if (fgets (buf, sizeof(buf), stdin) == NULL)
  195.     {
  196.       error (0, 0, _("%s: Short file"), inname);
  197.       return 1;
  198.     }
  199.       p = buf;
  200.  
  201.       if (memcmp (buf, "====", 4) == 0)
  202.     break;
  203.       if (last_data != 0)
  204.     {
  205.       error (0, 0, _("%s: data following `=' padding character"), inname);
  206.       return 1;
  207.     }
  208.  
  209.       /* The following implementation of the base64 decoding might look
  210.      a bit clumsy but I only try to follow the POSIX standard:
  211.      ``All line breaks or other characters not found in the table
  212.        [with base64 characters] shall be ignored by decoding
  213.        software.''  */
  214.       while (*p != '\n')
  215.     {
  216.       char c1, c2, c3;
  217.  
  218.       while ((b64_tab[*p] & '\100') != 0)
  219.         if (*p == '\n' || *p++ == '=')
  220.           break;
  221.       if (*p == '\n')
  222.         /* This leaves the loop.  */
  223.         continue;
  224.       c1 = b64_tab[*p++];
  225.  
  226.       while ((b64_tab[*p] & '\100') != 0)
  227.         if (*p == '\n' || *p++ == '=')
  228.           {
  229.         error (0, 0, _("%s: illegal line"), inname);
  230.         return 1;
  231.           }
  232.       c2 = b64_tab[*p++];
  233.  
  234.       while (b64_tab[*p] == '\177')
  235.         if (*p++ == '\n')
  236.           {
  237.         error (0, 0, _("%s: illegal line"), inname);
  238.         return 1;
  239.           }
  240.       if (*p == '=')
  241.         {
  242.           putchar (c1 << 2 | c2 >> 4);
  243.           last_data = 1;
  244.           break;
  245.         }
  246.       c3 = b64_tab[*p++];
  247.  
  248.       while (b64_tab[*p] == '\177')
  249.         if (*p++ == '\n')
  250.           {
  251.         error (0, 0, _("%s: illegal line"), inname);
  252.         return 1;
  253.           }
  254.       putchar (c1 << 2 | c2 >> 4);
  255.       putchar (c2 << 4 | c3 >> 2);
  256.       if (*p == '=')
  257.         {
  258.           last_data = 1;
  259.           break;
  260.         }
  261.       else
  262.         putchar (c3 << 6 | b64_tab[*p++]);
  263.     }
  264.     }
  265.  
  266.   return 0;
  267. }
  268.  
  269.  
  270. static int
  271. decode (inname, forced_outname)
  272.      const char *inname;
  273.      const char *forced_outname;
  274. {
  275.   struct passwd *pw;
  276.   register int n;
  277.   register char *p;
  278.   int mode, n1;
  279.   char buf[2 * BUFSIZ];
  280.   char *outname;
  281.   int do_base64 = 0;
  282.  
  283.   /* Search for header line.  */
  284.  
  285.   while (1)
  286.     {
  287.       if (fgets (buf, sizeof (buf), stdin) == NULL)
  288.     {
  289.       error (0, 0, _("%s: No `begin' line"), inname);
  290.       return 1;
  291.     }
  292.  
  293.       if (strncmp (buf, "begin", 5) == 0)
  294.     {
  295.       if (sscanf (buf, "begin-base64 %o %s", &mode, buf) == 2)
  296.         {
  297.           do_base64 = 1;
  298.           break;
  299.         }
  300.       else if (sscanf (buf, "begin %o %s", &mode, buf) == 2)
  301.         break;
  302.     }
  303.     }
  304.  
  305.   /* If the output file name is given on the command line this rules.  */
  306.   if (forced_outname != NULL)
  307.     outname = (char *) forced_outname;
  308.   else
  309.     {
  310.       /* Handle ~user/file format.  */
  311.  
  312.       if (buf[0] != '~')
  313.     outname = buf;
  314.       else
  315.     {
  316.       p = buf + 1;
  317.       while (*p != '/')
  318.         ++p;
  319.       if (*p == '\0')
  320.         {
  321.           error (0, 0, _("%s: Illegal ~user"), inname);
  322.           return 1;
  323.         }
  324.       *p++ = '\0';
  325.       pw = getpwnam (buf + 1);
  326.       if (pw == NULL)
  327.         {
  328.           error (0, 0, _("%s: No user `%s'"), inname, buf + 1);
  329.           return 1;
  330.         }
  331.       n = strlen (pw->pw_dir);
  332.       n1 = strlen (p);
  333.       outname = (char *) alloca ((size_t) (n + n1 + 2));
  334.       memcpy (outname + n + 1, p, (size_t) (n1 + 1));
  335.       memcpy (outname, pw->pw_dir, (size_t) n);
  336.       outname[n] = '/';
  337.     }
  338.     }
  339.  
  340.   /* Create output file and set mode.  */
  341.  
  342.   if (strcmp (outname, "/dev/stdout") != 0 && strcmp (outname, "-") != 0
  343.       && (freopen (outname, "w", stdout) == NULL
  344. #if HAVE_FCHMOD
  345.       || fchmod (fileno (stdout), mode & (S_IRWXU | S_IRWXG | S_IRWXO))
  346. #else
  347.       || chmod (outname, mode & (S_IRWXU | S_IRWXG | S_IRWXO))
  348. #endif
  349.       ))
  350.     {
  351.       error (0, errno, "%s: %s", outname, inname);
  352.       return 1;
  353.     }
  354.  
  355.   /* We differenciate decoding standard UU encoding and base64.  A
  356.      common function would only slow down the program.  */
  357.  
  358.   /* For each input line:  */
  359.   if (do_base64)
  360.     return read_base64 (inname);
  361.   else
  362.     return read_stduu (inname);
  363. }
  364.  
  365. static void
  366. usage (status)
  367.      int status;
  368. {
  369.   if (status != 0)
  370.     fprintf (stderr, _("Try `%s --help' for more information.\n"),
  371.          program_name);
  372.   else
  373.     {
  374.       printf (_("Usage: %s [FILE]...\n"), program_name);
  375.       printf (_("\
  376. Mandatory arguments to long options are mandatory to short options too.\n\
  377.   -h, --help               display this help and exit\n\
  378.   -v, --version            output version information and exit\n\
  379.   -o, --output-file=FILE   direct output to FILE\n"));
  380.     }
  381.   exit (status);
  382. }
  383.  
  384. int
  385. main (argc, argv)
  386.      int argc;
  387.      char *const *argv;
  388. {
  389.   int opt;
  390.   int exit_status;
  391.   const char *outname;
  392.  
  393.   program_name = argv[0];
  394.   outname = NULL;
  395.   setlocale (LC_ALL, "");
  396.  
  397.   /* Set the text message domain.  */
  398.   bindtextdomain (PACKAGE, LOCALEDIR);
  399.   textdomain (PACKAGE);
  400.  
  401.   while (opt = getopt_long (argc, argv, "ho:v", longopts, (int *) NULL),
  402.      opt != EOF)
  403.     {
  404.       switch (opt)
  405.     {
  406.     case 'h':
  407.       usage (EXIT_SUCCESS);
  408.  
  409.     case 'o':
  410.       outname = optarg;
  411.       break;
  412.  
  413.     case 'v':
  414.       printf ("%s - GNU %s %s\n", program_name, PACKAGE, VERSION);
  415.       exit (EXIT_SUCCESS);
  416.  
  417.     case 0:
  418.       break;
  419.  
  420.     default:
  421.       usage (EXIT_FAILURE);
  422.     }
  423.     }
  424.  
  425.   if (optind == argc)
  426.     exit_status = decode ("stdin", outname) == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
  427.   else
  428.     {
  429.       exit_status = EXIT_SUCCESS;
  430.       do
  431.     {
  432.       if (freopen (argv[optind], "r", stdin) != NULL)
  433.         {
  434.           if (decode (argv[optind], outname) != 0)
  435.         exit_status = EXIT_FAILURE;
  436.         }
  437.       else
  438.         {
  439.           error (0, errno, "%s", argv[optind]);
  440.           exit_status = EXIT_FAILURE;
  441.         }
  442.       optind++;
  443.     }
  444.       while (optind < argc);
  445.     }
  446.  
  447.   exit (exit_status);
  448. }
  449.