base64.c
4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001, 2002, 2005, 2007, 2010 Free Software
Foundation, Inc.
GNU Mailutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Mailutils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Mailutils; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301 USA */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <unistd.h>
#include <stdio.h>
#include <assert.h>
#include <ctype.h>
#include <string.h>
#include <mailutils/mailutils.h>
#define ISPRINT(c) ((c)>=' '&&(c)<127)
int verbose = 0;
int printable = 0;
static void
c_copy (mu_stream_t out, mu_stream_t in)
{
if (printable)
{
char c;
size_t size;
while (mu_stream_read (in, &c, 1, &size) == 0 && size > 0)
{
int rc;
if (printable && !ISPRINT (c))
{
char outbuf[24];
sprintf (outbuf, "\\%03o", (unsigned char) c);
rc = mu_stream_write (out, outbuf, strlen (outbuf), NULL);
}
else
rc = mu_stream_write (out, &c, 1, NULL);
}
}
else
MU_ASSERT (mu_stream_copy (out, in, 0));
mu_stream_write (out, "\n", 1, NULL);
mu_stream_close (out);
mu_stream_close (in);
if (verbose)
fprintf (stderr, "\ntotal: %lu/%lu bytes\n",
(unsigned long) mu_stream_bytes_in (in),
(unsigned long) mu_stream_bytes_out (out));
}
/* Set the maximum line length for the filter NAME to LENGTH.
FIXME: This is a kludge. Perhaps API should provide a function
for that. */
static void
reset_line_length (const char *name, size_t length)
{
mu_list_t list;
int status;
mu_filter_record_t frec;
mu_filter_get_list (&list);
status = mu_list_locate (list, (void*)name, (void**)&frec);
if (status == 0)
frec->max_line_length = length;
/* don't bail out, leave that to mu_filter_create */
}
int
main (int argc, char * argv [])
{
mu_stream_t in, out, flt;
int c;
int mode = MU_FILTER_ENCODE;
int flags = MU_STREAM_READ;
char *input = NULL, *output = NULL;
char *encoding = "base64";
mu_off_t shift = 0;
size_t line_length;
int line_length_option = 0;
while ((c = getopt (argc, argv, "deE:hi:l:o:ps:vw")) != EOF)
switch (c)
{
case 'i':
input = optarg;
break;
case 'o':
output = optarg;
break;
case 'd':
mode = MU_FILTER_DECODE;
break;
case 'E':
encoding = optarg;
break;
case 'e':
mode = MU_FILTER_ENCODE;
break;
case 'l':
line_length = strtoul (optarg, NULL, 10);
line_length_option = 1;
break;
case 'p':
printable = 1;
break;
case 's':
shift = strtoul (optarg, NULL, 0);
break;
case 'v':
verbose = 1;
break;
case 'h':
printf ("usage: base64 [-vpde][-E encoding][-i infile][-o outfile]\n");
exit (0);
case 'w':
flags = MU_STREAM_WRITE;
break;
default:
exit (1);
}
if (input)
MU_ASSERT (mu_file_stream_create (&in, input, MU_STREAM_READ|MU_STREAM_SEEK));
else
MU_ASSERT (mu_stdio_stream_create (&in, MU_STDIN_FD, 0));
MU_ASSERT (mu_stream_open (in));
if (output)
MU_ASSERT (mu_file_stream_create (&out, output,
MU_STREAM_WRITE|MU_STREAM_CREAT));
else
MU_ASSERT (mu_stdio_stream_create (&out, MU_STDOUT_FD, 0));
MU_ASSERT (mu_stream_open (out));
if (line_length_option)
reset_line_length (encoding, line_length);
if (flags == MU_STREAM_READ)
{
MU_ASSERT (mu_filter_create (&flt, in, encoding, mode,
MU_STREAM_READ|MU_STREAM_SEEK));
if (shift)
MU_ASSERT (mu_stream_seek (flt, shift, MU_SEEK_SET, NULL));
c_copy (out, flt);
}
else
{
MU_ASSERT (mu_filter_create (&flt, out, encoding, mode,
MU_STREAM_WRITE));
if (shift)
MU_ASSERT (mu_stream_seek (in, shift, MU_SEEK_SET, NULL));
c_copy (flt, in);
}
return 0;
}