~ubuntu-branches/ubuntu/utopic/nettle/utopic

« back to all changes in this revision

Viewing changes to examples/base64dec.c

  • Committer: Package Import Robot
  • Author(s): Magnus Holmgren
  • Date: 2013-05-07 22:57:14 UTC
  • mfrom: (8.1.8 sid)
  • Revision ID: package-import@ubuntu.com-20130507225714-s331yr8ov53dtt17
Tags: 2.7-2
Tag some (ECC related) symbols that only exist on some architectures.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* base64dec -- an decoder for base64
 
2
 *
 
3
 * Copyright (C) 2006, 2012 Jeronimo Pellegrini, Niels Möller
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU Lesser General Public License as published by
 
7
 * the Free Software Foundation; either version 2.1 of the License, or (at your
 
8
 * option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful, but
 
11
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
12
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 
13
 * License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU Lesser General Public License
 
16
 * along with the nettle library; see the file COPYING.LIB.  If not, write to
 
17
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 
18
 * MA 02111-1301, USA.
 
19
 */
 
20
 
 
21
#if HAVE_CONFIG_H
 
22
# include "config.h"
 
23
#endif
 
24
 
 
25
#include <errno.h>
 
26
#include <stdlib.h>
 
27
#include <stdio.h>
 
28
#include <string.h>
 
29
#ifdef WIN32
 
30
#include <fcntl.h>
 
31
#endif
 
32
 
 
33
#include "base64.h"
 
34
 
 
35
#include "io.h"
 
36
 
 
37
#define CHUNK_SIZE 16392
 
38
 
 
39
/* The maximum number of bytes generated for each chunk: */
 
40
#define DECODED_SIZE BASE64_DECODE_LENGTH(CHUNK_SIZE)
 
41
 
 
42
 
 
43
/*
 
44
 * Reads base-64 encoded from stdin, writes decoded to stdout.
 
45
 */
 
46
int
 
47
main(int argc UNUSED, char **argv UNUSED)
 
48
{
 
49
  /* "buffer" will hold the bytes from disk: */
 
50
  uint8_t * buffer = xalloc (CHUNK_SIZE);
 
51
 
 
52
  /* "result" will hold bytes before output: */
 
53
  uint8_t * result = xalloc (DECODED_SIZE);
 
54
 
 
55
  /* We need a Base64 context for decoding: */
 
56
  struct base64_decode_ctx b64_ctx;
 
57
 
 
58
  /* Init the context: */
 
59
  base64_decode_init(&b64_ctx);
 
60
 
 
61
#ifdef WIN32
 
62
  _setmode(1, O_BINARY);
 
63
#endif
 
64
 
 
65
  for (;;)
 
66
    {
 
67
      int nbytes; /* Number of bytes read frmo disk at each iteration */
 
68
      unsigned decoded_bytes; /* Bytes actually generated at each iteration */
 
69
 
 
70
      nbytes = fread(buffer, 1, CHUNK_SIZE, stdin);
 
71
 
 
72
      if (nbytes < CHUNK_SIZE && ferror(stdin))
 
73
        {
 
74
          werror ("Error reading file: %s\n", strerror(errno));
 
75
          return EXIT_FAILURE;
 
76
        }
 
77
 
 
78
      decoded_bytes = BASE64_DECODE_LENGTH(nbytes);
 
79
 
 
80
      /* Decodes one chunk: */
 
81
      if (!base64_decode_update(&b64_ctx, &decoded_bytes, result, nbytes, buffer))
 
82
        {
 
83
          werror ("Error decoding input (not base64?)\n");
 
84
          return EXIT_FAILURE;
 
85
        }
 
86
 
 
87
      if (!write_string (stdout, decoded_bytes, result))
 
88
        {
 
89
          werror ("Error writing file: %s\n", strerror(errno));
 
90
          return EXIT_FAILURE;
 
91
        }
 
92
 
 
93
      if (nbytes < CHUNK_SIZE)
 
94
        {
 
95
          /* Check if decoding finalized OK: */
 
96
          if (!base64_decode_final(&b64_ctx))
 
97
            {
 
98
              werror ("Decoding did not finish properly.\n");
 
99
              return EXIT_FAILURE;
 
100
            }
 
101
          break;
 
102
        }
 
103
    }
 
104
 
 
105
  if (fflush (stdout) != 0)
 
106
    {
 
107
      werror ("Error writing file: %s\n", strerror(errno));
 
108
      return EXIT_FAILURE;
 
109
    }
 
110
 
 
111
  free (buffer);
 
112
  free (result);
 
113
 
 
114
  return EXIT_SUCCESS;
 
115
}