Line data Source code
1 : /* b64dec.c - Simple Base64 decoder.
2 : * Copyright (C) 2008, 2011 Free Software Foundation, Inc.
3 : * Copyright (C) 2008, 2011, 2016 g10 Code GmbH
4 : *
5 : * This file is part of GnuPG.
6 : *
7 : * This file is free software; you can redistribute it and/or modify
8 : * it under the terms of the GNU Lesser General Public License as
9 : * published by the Free Software Foundation; either version 2.1 of
10 : * the License, or (at your option) any later version.
11 : *
12 : * This file is distributed in the hope that it will be useful,
13 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : * GNU General Public License for more details.
16 : *
17 : * You should have received a copy of the GNU Lesser General Public License
18 : * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 : */
20 :
21 : #include <config.h>
22 : #include <stdio.h>
23 : #include <stdlib.h>
24 : #include <string.h>
25 : #include <assert.h>
26 :
27 : #include "gpgme.h"
28 : #include "util.h"
29 :
30 :
31 : /* The reverse base-64 list used for base-64 decoding. */
32 : static unsigned char const asctobin[128] =
33 : {
34 : 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
35 : 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
36 : 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
37 : 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
38 : 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
39 : 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
40 : 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
41 : 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
42 : 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
43 : 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
44 : 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
45 : 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
46 : 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
47 : 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
48 : 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
49 : 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff
50 : };
51 :
52 : enum decoder_states
53 : {
54 : s_init, s_idle, s_lfseen, s_beginseen, s_waitheader, s_waitblank, s_begin,
55 : s_b64_0, s_b64_1, s_b64_2, s_b64_3,
56 : s_waitendtitle, s_waitend
57 : };
58 :
59 :
60 :
61 : /* Initialize the context for the base64 decoder. If TITLE is NULL a
62 : plain base64 decoding is done. If it is the empty string the
63 : decoder will skip everything until a "-----BEGIN " line has been
64 : seen, decoding ends at a "----END " line. */
65 : gpg_error_t
66 0 : _gpgme_b64dec_start (struct b64state *state, const char *title)
67 : {
68 0 : memset (state, 0, sizeof *state);
69 0 : if (title)
70 : {
71 0 : state->title = strdup (title);
72 0 : if (!state->title)
73 0 : state->lasterr = gpg_error_from_syserror ();
74 : else
75 0 : state->idx = s_init;
76 : }
77 : else
78 0 : state->idx = s_b64_0;
79 0 : return state->lasterr;
80 : }
81 :
82 :
83 : /* Do in-place decoding of base-64 data of LENGTH in BUFFER. Stores the
84 : new length of the buffer at R_NBYTES. */
85 : gpg_error_t
86 0 : _gpgme_b64dec_proc (struct b64state *state, void *buffer, size_t length,
87 : size_t *r_nbytes)
88 : {
89 0 : enum decoder_states ds = state->idx;
90 0 : unsigned char val = state->radbuf[0];
91 0 : int pos = state->quad_count;
92 : char *d, *s;
93 :
94 0 : if (state->lasterr)
95 0 : return state->lasterr;
96 :
97 0 : if (state->stop_seen)
98 : {
99 0 : *r_nbytes = 0;
100 0 : state->lasterr = gpg_error (GPG_ERR_EOF);
101 0 : free (state->title);
102 0 : state->title = NULL;
103 0 : return state->lasterr;
104 : }
105 :
106 0 : for (s=d=buffer; length && !state->stop_seen; length--, s++)
107 : {
108 : again:
109 0 : switch (ds)
110 : {
111 : case s_idle:
112 0 : if (*s == '\n')
113 : {
114 0 : ds = s_lfseen;
115 0 : pos = 0;
116 : }
117 0 : break;
118 : case s_init:
119 0 : ds = s_lfseen;
120 : case s_lfseen:
121 0 : if (*s != "-----BEGIN "[pos])
122 : {
123 0 : ds = s_idle;
124 0 : goto again;
125 : }
126 0 : else if (pos == 10)
127 : {
128 0 : pos = 0;
129 0 : ds = s_beginseen;
130 : }
131 : else
132 0 : pos++;
133 0 : break;
134 : case s_beginseen:
135 0 : if (*s != "PGP "[pos])
136 0 : ds = s_begin; /* Not a PGP armor. */
137 0 : else if (pos == 3)
138 0 : ds = s_waitheader;
139 : else
140 0 : pos++;
141 0 : break;
142 : case s_waitheader:
143 0 : if (*s == '\n')
144 0 : ds = s_waitblank;
145 0 : break;
146 : case s_waitblank:
147 0 : if (*s == '\n')
148 0 : ds = s_b64_0; /* blank line found. */
149 0 : else if (*s == ' ' || *s == '\r' || *s == '\t')
150 : ; /* Ignore spaces. */
151 : else
152 : {
153 : /* Armor header line. Note that we don't care that our
154 : * FSM accepts a header prefixed with spaces. */
155 0 : ds = s_waitheader; /* Wait for next header. */
156 : }
157 0 : break;
158 : case s_begin:
159 0 : if (*s == '\n')
160 0 : ds = s_b64_0;
161 0 : break;
162 : case s_b64_0:
163 : case s_b64_1:
164 : case s_b64_2:
165 : case s_b64_3:
166 : {
167 : int c;
168 :
169 0 : if (*s == '-' && state->title)
170 : {
171 : /* Not a valid Base64 character: assume end
172 : header. */
173 0 : ds = s_waitend;
174 : }
175 0 : else if (*s == '=')
176 : {
177 : /* Pad character: stop */
178 0 : if (ds == s_b64_1)
179 0 : *d++ = val;
180 0 : ds = state->title? s_waitendtitle : s_waitend;
181 : }
182 0 : else if (*s == '\n' || *s == ' ' || *s == '\r' || *s == '\t')
183 : ; /* Skip white spaces. */
184 0 : else if ( (*s & 0x80)
185 0 : || (c = asctobin[*(unsigned char *)s]) == 255)
186 : {
187 : /* Skip invalid encodings. */
188 0 : state->invalid_encoding = 1;
189 : }
190 0 : else if (ds == s_b64_0)
191 : {
192 0 : val = c << 2;
193 0 : ds = s_b64_1;
194 : }
195 0 : else if (ds == s_b64_1)
196 : {
197 0 : val |= (c>>4)&3;
198 0 : *d++ = val;
199 0 : val = (c<<4)&0xf0;
200 0 : ds = s_b64_2;
201 : }
202 0 : else if (ds == s_b64_2)
203 : {
204 0 : val |= (c>>2)&15;
205 0 : *d++ = val;
206 0 : val = (c<<6)&0xc0;
207 0 : ds = s_b64_3;
208 : }
209 : else
210 : {
211 0 : val |= c&0x3f;
212 0 : *d++ = val;
213 0 : ds = s_b64_0;
214 : }
215 : }
216 0 : break;
217 : case s_waitendtitle:
218 0 : if (*s == '-')
219 0 : ds = s_waitend;
220 0 : break;
221 : case s_waitend:
222 0 : if ( *s == '\n')
223 0 : state->stop_seen = 1;
224 0 : break;
225 : default:
226 0 : assert (!"invalid state");
227 : }
228 : }
229 :
230 :
231 0 : state->idx = ds;
232 0 : state->radbuf[0] = val;
233 0 : state->quad_count = pos;
234 0 : *r_nbytes = (d -(char*) buffer);
235 0 : return 0;
236 : }
237 :
238 :
239 : /* This function needs to be called before releasing the decoder
240 : state. It may return an error code in case an encoding error has
241 : been found during decoding. */
242 : gpg_error_t
243 0 : _gpgme_b64dec_finish (struct b64state *state)
244 : {
245 0 : if (state->lasterr)
246 0 : return state->lasterr;
247 :
248 0 : free (state->title);
249 0 : state->title = NULL;
250 0 : return state->invalid_encoding? gpg_error(GPG_ERR_BAD_DATA): 0;
251 : }
|