Blame view

examples/base64.c 5.01 KB
1
/* GNU Mailutils -- a suite of utilities for electronic mail
2 3
   Copyright (C) 1999, 2000, 2001, 2002, 2005, 2007, 2010 Free Software
   Foundation, Inc.
4

5
   GNU Mailutils is free software; you can redistribute it and/or modify
6
   it under the terms of the GNU General Public License as published by
7
   the Free Software Foundation; either version 3, or (at your option)
8 9
   any later version.

10
   GNU Mailutils is distributed in the hope that it will be useful,
11 12 13 14 15
   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
16
   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
17

18 19 20
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
21 22 23 24
#include <unistd.h>
#include <stdio.h>
#include <assert.h>
#include <ctype.h>
25
#include <string.h>
26 27
#include <mailutils/mailutils.h>

28 29
#define ISPRINT(c) ((c)>=' '&&(c)<127) 

30 31 32 33 34 35
int verbose = 0;
int printable = 0;

static void
c_copy (mu_stream_t out, mu_stream_t in)
{
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
  mu_stream_stat_buffer instat, outstat;

  if (verbose)
    {
      mu_stream_set_stat (in,
			  MU_STREAM_STAT_MASK (MU_STREAM_STAT_IN) |
			  MU_STREAM_STAT_MASK (MU_STREAM_STAT_READS) |
			  MU_STREAM_STAT_MASK (MU_STREAM_STAT_SEEKS),
			  instat);
      mu_stream_set_stat (out,
			  MU_STREAM_STAT_MASK (MU_STREAM_STAT_OUT) |
			  MU_STREAM_STAT_MASK (MU_STREAM_STAT_WRITES) |
			  MU_STREAM_STAT_MASK (MU_STREAM_STAT_SEEKS),
			  outstat);
    }

Sergey Poznyakoff authored
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  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
72
    MU_ASSERT (mu_stream_copy (out, in, 0, NULL));
Sergey Poznyakoff authored
73
  mu_stream_write (out, "\n", 1, NULL);
74 75 76
  mu_stream_close (out);
  mu_stream_close (in);
  if (verbose)
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    {
      fprintf (stderr, "\nInput stats:\n");
      fprintf (stderr, "Bytes in: %lu\n",
	       (unsigned long) instat[MU_STREAM_STAT_IN]);
      fprintf (stderr, "Reads: %lu\n",
	       (unsigned long) instat[MU_STREAM_STAT_READS]);
      fprintf (stderr, "Seeks: %lu\n",
	       (unsigned long) instat[MU_STREAM_STAT_SEEKS]);

      fprintf (stderr, "\nOutput stats:\n");
      fprintf (stderr, "Bytes out: %lu\n",
	       (unsigned long) outstat[MU_STREAM_STAT_OUT]);
      fprintf (stderr, "Writes: %lu\n",
	       (unsigned long) outstat[MU_STREAM_STAT_WRITES]);
      fprintf (stderr, "Seeks: %lu\n",
	       (unsigned long) outstat[MU_STREAM_STAT_SEEKS]);
    }
94 95
}

96 97 98
int
main (int argc, char * argv [])
{
99
  mu_stream_t in, out, flt;
100
  int c;
101
  int mode = MU_FILTER_ENCODE;
102
  int flags = MU_STREAM_READ;
103
  char *input = NULL, *output = NULL;
104
  char *encoding = "base64";
105
  mu_off_t shift = 0;
106 107 108
  char *line_length_option = NULL;
  char *fargv[5];
  size_t fargc = 0;
109
  
Sergey Poznyakoff authored
110
  while ((c = getopt (argc, argv, "deE:hi:l:o:ps:vw")) != EOF)
111 112 113 114 115 116 117 118 119 120 121 122 123
    switch (c)
      {
      case 'i':
	input = optarg;
	break;

      case 'o':
	output = optarg;
	break;
	
      case 'd':
	mode = MU_FILTER_DECODE;
	break;
124 125 126 127

      case 'E':
	encoding = optarg;
	break;
128 129 130 131 132
	
      case 'e':
	mode = MU_FILTER_ENCODE;
	break;

Sergey Poznyakoff authored
133
      case 'l':
134
	line_length_option = optarg;
Sergey Poznyakoff authored
135 136
	break;
	
137
      case 'p':
Sergey Poznyakoff authored
138
 	printable = 1;
139
	break;
140 141 142 143

      case 's':
	shift = strtoul (optarg, NULL, 0); 
	break;
144 145 146 147 148 149
	
      case 'v':
	verbose = 1;
	break;

      case 'h':
150
	printf ("usage: base64 [-vpde][-E encoding][-i infile][-o outfile]\n");
151
	exit (0);
152 153 154 155 156

      case 'w':
	flags = MU_STREAM_WRITE;
	break;
	
157 158 159 160 161
      default:
	exit (1);
      }

  if (input)
162
    MU_ASSERT (mu_file_stream_create (&in, input, MU_STREAM_READ|MU_STREAM_SEEK));
163
  else
164
    MU_ASSERT (mu_stdio_stream_create (&in, MU_STDIN_FD, 0));
165 166

  if (output)
167 168
    MU_ASSERT (mu_file_stream_create (&out, output, 
                                      MU_STREAM_WRITE|MU_STREAM_CREAT));
169
  else
170 171
    MU_ASSERT (mu_stdio_stream_create (&out, MU_STDOUT_FD, 0));

172 173 174 175 176 177 178 179 180 181
  fargv[fargc++] = encoding;
  if (line_length_option && strcmp (line_length_option, "0"))
    {
      if (mu_c_strcasecmp (encoding, "base64") == 0)
	fargv[0] = "B"; /* B encoding has no length limit */
      fargv[fargc++] = "+";
      fargv[fargc++] = (mode == MU_FILTER_DECODE) ? "~linelen" : "linelen";
      fargv[fargc++] = line_length_option;
    }
  fargv[fargc] = NULL;
Sergey Poznyakoff authored
182
  
183
  if (flags == MU_STREAM_READ)
184
    {
185 186 187 188
      MU_ASSERT (mu_filter_chain_create (&flt, in, mode,
					 MU_STREAM_READ|MU_STREAM_SEEK,
					 fargc, fargv));
      mu_stream_unref (in);
189 190
      if (shift)
	MU_ASSERT (mu_stream_seek (flt, shift, MU_SEEK_SET, NULL));
191
      c_copy (out, flt);
192
    }
193 194
  else
    {
195 196 197 198
      MU_ASSERT (mu_filter_chain_create (&flt, out, mode,
					 MU_STREAM_WRITE,
					 fargc, fargv));
      mu_stream_unref (out);
199 200
      if (shift)
	MU_ASSERT (mu_stream_seek (in, shift, MU_SEEK_SET, NULL));
201 202 203
      c_copy (flt, in);
    }
      
204 205
  return 0;
}