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