sharutils  4.15.2
About: GNU shar utilities (shar, uuencode, remsync) prepare data for sending via e-mail.
  Fossies Dox: sharutils-4.15.2.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

uuencode.c
Go to the documentation of this file.
1 
2 static const char cright_years_z[] =
3 
4 /* uuencode utility.
5  Copyright (C) */ "1994-2015";
6 
7 /* Free Software Foundation, Inc.
8 
9  This program is free software; you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 3, or (at your option)
12  any later version.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License along
20  with this program. If not, see <http://www.gnu.org/licenses/>.
21 
22  --copyright-mark "(copyright \\(c\\)[ \t]+[*]/ *\")([12][90][0-9][0-9])"
23  */
24 
25 /* Copyright (c) 1983 Regents of the University of California.
26  All rights reserved.
27 
28  Redistribution and use in source and binary forms, with or without
29  modification, are permitted provided that the following conditions
30  are met:
31  1. Redistributions of source code must retain the above copyright
32  notice, this list of conditions and the following disclaimer.
33  2. Redistributions in binary form must reproduce the above copyright
34  notice, this list of conditions and the following disclaimer in the
35  documentation and/or other materials provided with the distribution.
36  3. All advertising materials mentioning features or use of this software
37  must display the following acknowledgement:
38  This product includes software developed by the University of
39  California, Berkeley and its contributors.
40  4. Neither the name of the University nor the names of its contributors
41  may be used to endorse or promote products derived from this software
42  without specific prior written permission.
43 
44  THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47  ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54  SUCH DAMAGE. */
55 
56 /* Reworked to GNU style by Ian Lance Taylor, ian@airs.com, August 93. */
57 
58 /*=======================================================\
59 | uuencode [INPUT] OUTPUT |
60 | |
61 | Encode a file so it can be mailed to a remote system. |
62 \=======================================================*/
63 
64 #define UUENCODE_C 1
65 #include "uuencode-opts.h"
66 
67 #if HAVE_LOCALE_H
68 #else
69 # define setlocale(Category, Locale)
70 #endif
71 
72 #if __CYGWIN__
73 # include <fcntl.h>
74 # include <io.h>
75 #endif
76 
77 #define IRWALL_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
78 char * output_name = NULL;
79 char * input_name = NULL;
80 
81 static inline void try_putchar (int);
82 static void encode (void);
83 
84 /* standard uuencoding translation table */
85 const char uu_std[64] =
86 {
87  '`', '!', '"', '#', '$', '%', '&', '\'',
88  '(', ')', '*', '+', ',', '-', '.', '/',
89  '0', '1', '2', '3', '4', '5', '6', '7',
90  '8', '9', ':', ';', '<', '=', '>', '?',
91  '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
92  'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
93  'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
94  'X', 'Y', 'Z', '[', '\\', ']', '^', '_'
95 };
96 
97 /* Pointer to the translation table we currently use. */
98 const char * trans_ptr = uu_std;
99 
100 /* ENC is the basic 1 character encoding function to make a char printing. */
101 #define ENC(Char) (trans_ptr[(Char) & 077])
102 
103 static inline void
104 try_putchar (int c)
105 {
106  if (putchar (c) == EOF)
107  fserr (UUENCODE_EXIT_FAILURE, "putchar", _("standard output"));
108 }
109 
110 /*------------------------------------------------.
111 | Copy from IN to OUT, encoding as you go along. |
112 `------------------------------------------------*/
113 
114 static size_t
115 encode_block (char * out, unsigned char const * in, size_t in_len)
116 {
117  char * start = out;
118 
119  while (in_len >= 3)
120  {
121  *(out++) = ENC (in[0] >> 2);
122  *(out++) = ENC (((in[0] & 0x03) << 4) + ((in[1] >> 4) & 0x0F));
123  *(out++) = ENC (((in[1] & 0x0F) << 2) + ((in[2] >> 6) & 0x03));
124  *(out++) = ENC (in[2] & 0x3F);
125  in_len -= 3;
126  in += 3;
127  }
128 
129  if (in_len > 0)
130  {
131  unsigned char lc = (in_len > 1) ? in[1] : '\0';
132  *(out++) = ENC (in[0] >> 2);
133  *(out++) = ENC (((in[0] & 0x03) << 4) + ((lc >> 4) & 0x0F));
134  *(out++) = ENC ((lc & 0x0F) << 2);
135  *(out++) = ENC (0);
136  }
137 
138  return out - start;
139 }
140 
141 /*------------------------------------------.
142 | Copy from IN to OUT, encoding as you go. |
143 `------------------------------------------*/
144 
145 static void
146 encode (void)
147 {
148  int finishing = 0;
149 
150  do
151  {
152  unsigned char buf[45];
153  char buf_out[64];
154  int rdct = fread (buf, 1, sizeof(buf), stdin);
155  size_t wrct = sizeof (buf_out);
156 
157  if (rdct <= 0)
158  {
159  if (ferror (stdin))
161  break;
162  }
163 
164  if (rdct < 45)
165  {
166  if (! feof (stdin))
168  finishing = 1;
169  }
170 
171  if (! HAVE_OPT(BASE64))
172  {
173  try_putchar (ENC ((unsigned int)rdct));
174  wrct = encode_block (buf_out, buf, rdct);
175  }
176  else
177  {
178  base64_encode ((char *)buf, (size_t)rdct, buf_out, wrct);
179  wrct = strlen(buf_out);
180  }
181 
182  buf_out[wrct++] = '\n';
183  if (fwrite (buf_out, 1, wrct, stdout) != wrct)
184  fserr (UUENCODE_EXIT_FAILURE, "fwrite", _("standard output"));
185  } while (! finishing);
186 
187  if (fclose (stdin) != 0)
189 
190  if (! HAVE_OPT(BASE64))
191  {
192  try_putchar (ENC ('\0'));
193  try_putchar ('\n');
194  }
195 }
196 
197 static void
198 process_opts (int argc, char ** argv, int * mode)
199 {
200  /* Set global variables. */
201  setlocale (LC_ALL, "");
202 
203  /* Set the text message domain. */
204  bindtextdomain (PACKAGE, LOCALEDIR);
205  textdomain (PACKAGE);
206 
207  input_name = _("standard input");
208 
209  {
210  int ct = optionProcess (&uuencodeOptions, argc, argv);
211  argc -= ct;
212  argv += ct;
213  }
214 
215  switch (argc)
216  {
217  case 2:
218  /* Optional first argument is input file. */
219  {
220  struct stat sb;
221 #if S_IRWXU != 0700
222  choke me - Must translate mode argument
223 #endif
224 
225  FILE * fp = freopen (*argv, "r" FOPEN_BINARY, stdin);
226  input_name = *argv;
227  if (fp != stdin)
228  fserr (UUENCODE_EXIT_FAILURE, _("freopen of stdin"), input_name);
229  if (fstat (fileno (stdin), &sb) != 0)
231  *mode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
232  output_name = argv[1];
233  break;
234  }
235 
236  case 1:
237 #if __CYGWIN__ || __MINGW32__
238  if (! isatty (STDIN_FILENO))
239  setmode (STDIN_FILENO, O_BINARY);
240 #endif
241 
242  *mode = IRWALL_MODE & ~umask (IRWALL_MODE);
243  output_name = *argv;
244  break;
245 
246  case 0:
247  default:
249  }
250 
251  if (HAVE_OPT(ENCODE_FILE_NAME))
252  {
253  size_t nmlen = strlen (output_name);
254  size_t bfsz = nmlen + (nmlen / 3) + 4;;
255  char * p = malloc (bfsz);
256  if (p == NULL)
257  fserr (UUENCODE_EXIT_FAILURE, "malloc", _("file name"));
258  base64_encode ((char *)output_name, nmlen, p, bfsz);
259  output_name = p;
260  }
261 }
262 
263 int
264 main (int argc, char ** argv)
265 {
266  int mode;
267  process_opts (argc, argv, &mode);
268 
269  if (printf ("begin%s%s %o %s\n",
270  HAVE_OPT(BASE64) ? "-base64" : "",
271  HAVE_OPT(ENCODE_FILE_NAME) ? "-encoded" : "",
272  mode, output_name) < 0)
273  fserr (UUENCODE_EXIT_FAILURE, "printf", _("standard output"));
274 
275  encode ();
276 
277  if (ferror (stdout))
278  fserr (UUENCODE_EXIT_FAILURE, "ferror", _("standard output"));
279  if (puts (HAVE_OPT(BASE64) ? "====" : "end") == EOF)
280  fserr (UUENCODE_EXIT_FAILURE, "puts", _("standard output"));
281  if (fclose (stdout) != 0)
282  fserr (UUENCODE_EXIT_FAILURE, "fclose", _("standard output"));
283 
284  exit (UUENCODE_EXIT_SUCCESS);
285 }
286 /*
287  * Local Variables:
288  * mode: C
289  * c-file-style: "gnu"
290  * indent-tabs-mode: nil
291  * End:
292  * end of src/uuencode.c */
IRWALL_MODE
#define IRWALL_MODE
Definition: uuencode.c:77
encode_block
static size_t encode_block(char *out, unsigned char const *in, size_t in_len)
Definition: uuencode.c:115
UUENCODE_EXIT_USAGE_ERROR
Definition: uuencode-opts.h:127
S_IRWXG
#define S_IRWXG
Definition: sys_stat.in.h:296
uuencodeOptions
tOptions uuencodeOptions
Definition: uuencode-opts.c:502
ENC
#define ENC(Char)
Definition: uuencode.c:101
input_name
char * input_name
Definition: uuencode.c:79
fserr
void fserr(int exit_code, char const *op, char const *fn)
Definition: uudecode-opts.c:510
NULL
#define NULL
Definition: getopt1.c:36
base64_encode
void base64_encode(const char *restrict in, size_t inlen, char *restrict out, size_t outlen)
Definition: base64.c:88
process_opts
static void process_opts(int argc, char **argv, int *mode)
Definition: uuencode.c:198
O_BINARY
#define O_BINARY
Definition: fcntl.in.h:320
main
int main(int argc, char **argv)
Definition: uuencode.c:264
trans_ptr
const char * trans_ptr
Definition: uuencode.c:98
try_putchar
static void try_putchar(int)
Definition: uuencode.c:104
bindtextdomain
#define bindtextdomain(Domainname, Dirname)
Definition: gettext.h:87
uuencode-opts.h
textdomain
#define textdomain(Domainname)
Definition: gettext.h:85
UUENCODE_EXIT_SUCCESS
Definition: uuencode-opts.h:125
cright_years_z
static const char cright_years_z[]
Definition: uuencode.c:2
malloc
#define malloc(_s)
Definition: autoopts.c:73
FOPEN_BINARY
#define FOPEN_BINARY
Definition: local.h:60
STDIN_FILENO
#define STDIN_FILENO
Definition: unistd.in.h:226
S_IRWXO
#define S_IRWXO
Definition: sys_stat.in.h:299
S_IRWXU
#define S_IRWXU
Definition: sys_stat.in.h:293
setlocale
#define setlocale(Category, Locale)
Definition: uuencode.c:69
encode
static void encode(void)
Definition: uuencode.c:146
output_name
char * output_name
Definition: uuencode.c:78
optionProcess
int optionProcess(tOptions *opts, int a_ct, char **a_v)
Definition: autoopts.c:333
_
#define _
Definition: ctype.in.h:37
UUENCODE_EXIT_FAILURE
Definition: uuencode-opts.h:126
HAVE_OPT
#define HAVE_OPT(n)
Definition: shar-opts.h:126
uu_std
const char uu_std[64]
Definition: uuencode.c:85
USAGE
#define USAGE(c)
Definition: shar-opts.h:346