~ubuntu-branches/ubuntu/saucy/nettle/saucy-proposed

« back to all changes in this revision

Viewing changes to base64-decode.c

  • Committer: Bazaar Package Importer
  • Author(s): Marek Habersack
  • Date: 2004-05-04 15:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20040504155602-7jbhw5mabvwksl3j
Tags: upstream-1.10
Import upstream version 1.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* base64-encode.c
 
2
 *
 
3
 */
 
4
 
 
5
/* nettle, low-level cryptographics library
 
6
 *
 
7
 * Copyright (C) 2002 Niels M�ller
 
8
 *  
 
9
 * The nettle library is free software; you can redistribute it and/or modify
 
10
 * it under the terms of the GNU Lesser General Public License as published by
 
11
 * the Free Software Foundation; either version 2.1 of the License, or (at your
 
12
 * option) any later version.
 
13
 * 
 
14
 * The nettle library is distributed in the hope that it will be useful, but
 
15
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
16
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 
17
 * License for more details.
 
18
 * 
 
19
 * You should have received a copy of the GNU Lesser General Public License
 
20
 * along with the nettle library; see the file COPYING.LIB.  If not, write to
 
21
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 
22
 * MA 02111-1307, USA.
 
23
 */
 
24
 
 
25
#if HAVE_CONFIG_H
 
26
# include "config.h"
 
27
#endif
 
28
 
 
29
#include <assert.h>
 
30
#include <stdlib.h>
 
31
 
 
32
#include "base64.h"
 
33
 
 
34
#define TABLE_INVALID -1
 
35
#define TABLE_SPACE -2
 
36
#define TABLE_END -3
 
37
 
 
38
/* FIXME: Make sure that all whitespace characters, SPC, HT, VT, FF,
 
39
 * CR and LF are ignored. */
 
40
static const signed char
 
41
decode_table[0x100] =
 
42
{
 
43
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -2, -1, -1, -2, -1, -1, 
 
44
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
 
45
  -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
 
46
  52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -3, -1, -1,
 
47
  -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
 
48
  15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
 
49
  -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
 
50
  41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
 
51
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
 
52
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
 
53
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
 
54
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
 
55
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
 
56
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
 
57
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
 
58
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
 
59
};
 
60
 
 
61
void
 
62
base64_decode_init(struct base64_decode_ctx *ctx)
 
63
{
 
64
  ctx->word = ctx->bits = ctx->padding = 0;
 
65
}
 
66
 
 
67
int
 
68
base64_decode_single(struct base64_decode_ctx *ctx,
 
69
                     uint8_t *dst,
 
70
                     uint8_t src)
 
71
{
 
72
  int data;
 
73
  
 
74
  data = decode_table[src];
 
75
 
 
76
  switch(data)
 
77
    {
 
78
    default:
 
79
      assert(data >= 0 && data < 0x40);
 
80
 
 
81
      if (ctx->padding)
 
82
        return -1;
 
83
      
 
84
      ctx->word = ctx->word << 6 | data;
 
85
      ctx->bits += 6;
 
86
 
 
87
      if (ctx->bits >= 8)
 
88
        {
 
89
          ctx->bits -= 8;
 
90
          dst[0] = ctx->word >> ctx->bits;
 
91
          return 1;
 
92
        }
 
93
      else return 0;
 
94
 
 
95
    case TABLE_INVALID:
 
96
      return -1;
 
97
 
 
98
    case TABLE_SPACE:
 
99
      return 0;
 
100
      
 
101
    case TABLE_END:
 
102
      /* There can be at most two padding characters. */
 
103
      if (!ctx->bits || ctx->padding > 2)
 
104
        return -1;
 
105
      
 
106
      if (ctx->word & ( (1<<ctx->bits) - 1))
 
107
        /* We shouldn't have any leftover bits */
 
108
        return -1;
 
109
 
 
110
      ctx->padding++;
 
111
      ctx->bits -= 2;
 
112
      return 0;
 
113
    }
 
114
}
 
115
 
 
116
int
 
117
base64_decode_update(struct base64_decode_ctx *ctx,
 
118
                     unsigned *dst_length,
 
119
                     uint8_t *dst,
 
120
                     unsigned src_length,
 
121
                     const uint8_t *src)
 
122
{
 
123
  unsigned done;
 
124
  unsigned i;
 
125
 
 
126
  assert(*dst_length >= BASE64_DECODE_LENGTH(src_length));
 
127
  
 
128
  for (i = 0, done = 0; i<src_length; i++)
 
129
    switch(base64_decode_single(ctx, dst + done, src[i]))
 
130
      {
 
131
      case -1:
 
132
        return 0;
 
133
      case 1:
 
134
        done++;
 
135
        /* Fall through */
 
136
      case 0:
 
137
        break;
 
138
      default:
 
139
        abort();
 
140
      }
 
141
  
 
142
  assert(done <= BASE64_DECODE_LENGTH(src_length));
 
143
 
 
144
  *dst_length = done;
 
145
  return 1;
 
146
}
 
147
 
 
148
int
 
149
base64_decode_final(struct base64_decode_ctx *ctx)
 
150
{
 
151
  return ctx->bits == 0;
 
152
}