Blame view

examples/base64.c 2.87 KB
1
/* GNU Mailutils -- a suite of utilities for electronic mail
2 3
   Copyright (C) 1999, 2000, 2001, 2002, 2005, 
   2007 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, write to the Free Software
17 18
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
   MA 02110-1301 USA */
19

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

30 31
#define ISPRINT(c) ((c)>=' '&&(c)<127) 

32 33 34
int
main (int argc, char * argv [])
{
35
  mu_stream_t in, out, flt;
36 37 38 39 40 41
  unsigned char buffer;
  int c, verbose = 0;
  int printable = 0;
  size_t size, total = 0;
  int mode = MU_FILTER_ENCODE;
  char *input = NULL, *output = NULL;
42 43 44
  char *encoding = "base64";
    
  while ((c = getopt (argc, argv, "deE:hi:o:pv")) != EOF)
45 46 47 48 49 50 51 52 53 54 55 56 57
    switch (c)
      {
      case 'i':
	input = optarg;
	break;

      case 'o':
	output = optarg;
	break;
	
      case 'd':
	mode = MU_FILTER_DECODE;
	break;
58 59 60 61

      case 'E':
	encoding = optarg;
	break;
62 63 64 65 66 67
	
      case 'e':
	mode = MU_FILTER_ENCODE;
	break;

      case 'p':
Sergey Poznyakoff authored
68
 	printable = 1;
69 70 71 72 73 74 75
	break;
	
      case 'v':
	verbose = 1;
	break;

      case 'h':
76
	printf ("usage: base64 [-vpde][-E encoding][-i infile][-o outfile]\n");
77 78 79 80 81 82 83
	exit (0);
	  
      default:
	exit (1);
      }

  if (input)
84
    MU_ASSERT (mu_file_stream_create (&in, input, MU_STREAM_READ));
85
  else
86 87 88
    MU_ASSERT (mu_stdio_stream_create (&in, stdin, 0));
  MU_ASSERT (mu_filter_create (&flt, in, encoding, mode, MU_STREAM_READ));
  MU_ASSERT (mu_stream_open (in));
89 90

  if (output)
91 92
    MU_ASSERT (mu_file_stream_create (&out, output, 
                                      MU_STREAM_WRITE|MU_STREAM_CREAT));
93
  else
94 95
    MU_ASSERT (mu_stdio_stream_create (&out, stdout, 0));
  MU_ASSERT (mu_stream_open (out));
96
  
97
  while (mu_stream_read (flt, &buffer, sizeof (buffer), total, &size) == 0
98 99
	 && size > 0)
    {
100
      if (printable && !ISPRINT (buffer))
101 102 103
	{
	  char outbuf[24];
	  sprintf (outbuf, "\\%03o", (unsigned int) buffer);
104
	  mu_stream_sequential_write (out, outbuf, strlen (outbuf));
Sergey Poznyakoff authored
105
	} 
106
      else
107
	mu_stream_sequential_write (out, &buffer, size);
108 109 110
      total += size;
    }

111 112 113
  mu_stream_sequential_write (out, "\n", 1);
  mu_stream_close (in);
  mu_stream_close (out);
114 115 116 117
  if (verbose)
    fprintf (stderr, "total: %lu bytes\n", (unsigned long) total);
  return 0;
}