File: base64.c

package info (click to toggle)
emil 2.1.0-beta9-9
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,152 kB
  • ctags: 587
  • sloc: ansic: 10,369; yacc: 412; makefile: 326; sh: 172
file content (274 lines) | stat: -rw-r--r-- 6,467 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
** 
** Copyright (C) 1993 Swedish University Network (SUNET)
** 
** 
** This program is developed by UDAC, Uppsala University by commission
** of the Swedish University Network (SUNET). 
** 
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
** 
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITTNESS 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
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
** 
** 
**                                           Martin.Wendel@its.uu.se
** 				             Torbjorn.Wictorin@its.uu.se
** 
**                                           ITS	
**                                           P.O. Box 887
**                                           S-751 08 Uppsala
**                                           Sweden
** 
*/
#include "emil.h"

char tob64[] = 
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";



int fromb64[] = {
FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,  
FAIL, SKIP, SKIP, FAIL, FAIL, SKIP, FAIL, FAIL,
FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,  
FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,  

SKIP, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
FAIL, FAIL, FAIL, 0x3e, FAIL, FAIL, FAIL, 0x3f,
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,  
0x3c, 0x3d, FAIL, FAIL, FAIL, SKIP, FAIL, FAIL,

FAIL, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,  
0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,  
0x17, 0x18, 0x19, FAIL, FAIL, FAIL, FAIL, FAIL,

FAIL, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,  
0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,  
0x31, 0x32, 0x33, FAIL, FAIL, FAIL, FAIL, FAIL,

FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,  
FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,  
FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,  
FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,  
FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,  
FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,  
FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,  
FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,  
FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL
};

int
encode_base64(struct message *m)
{
  struct data *inbuf, *outbuf;
  int linelen;
  int i, l, left;
  unsigned long triple;
  unsigned char *inb;
  
  inbuf = m->td;
  inbuf->offset = inbuf->bodystart;
  logger(LOG_DEBUG, "encode base64");
  /* Exit on empty input */
  if (!inbuf->size)
    return(NOK);

#ifdef DEBUG
  if (edebug)
    fprintf(stderr, "Encoding base64");
#endif
  linelen = 1;

  outbuf = (struct data *)Yalloc(sizeof(struct data ));

  outbuf->encoding = EBASE64;
  
  /* Initialize working pointers */
  inb = (unsigned char *)inbuf->contents + inbuf->offset;
  i = 0;
  triple = 0;
  left = inbuf->bodyend - inbuf->offset;
#ifdef DEBUG
  if (edebug)
    fprintf(stderr, ", data length: %d\n", left);
#endif
  /*
   * Process entire inbuf.
   */
  while (left != 0)
    {
      i++;
      left--;

      triple = (triple <<8) | *inb;
      if (i == 3 || left == 0)
	{
	  switch (i) 
	    {
	    case 1:
	      triple = triple<<4;
	      break;
	    case 2:
	      triple = triple<<2;
	      break;
	    default:
	      break;
	    }
	  for (l = i; l >= 0; l--)
	    {
	      /* register */ 
	      int rr; rr = 0x3f & (triple>>(6*l)); assert (rr < 64); 
	      append_char(outbuf, tob64[rr], pz);
	      if (linelen == 72)
		{
		  append_char(outbuf, '\n', pz);
		  outbuf->lineend += 1;
		  linelen = 1;
		}
	      else
		{
		  linelen++; 
		}
	    }
	  if (left == 0)
	    switch(i)
	      {
	      case 2:
		append_data(outbuf, "=\n", 2, pz);
		break;
	      case 1:
		append_data(outbuf, "==\n", 3, pz);
		break;
	      default:
		break;
	      }
	  triple = 0;
	  i = 0;
	}
      inb++;
      inbuf->offset += 1;
    }
  safe_mchange(m, outbuf);
  return(OK);
}

int
decode_base64(struct message *m)
{
  struct data *inbuf, *outbuf = NULL;
  char *inb;
  int i;
  int l;
  int left;
  unsigned long triple;

  inbuf = m->td;
  inbuf->offset = inbuf->bodystart;
#ifdef DEBUG
  if (edebug)
    fprintf(stderr, "Decoding base64 from offset %lu to bodyend %lu\n",
	    inbuf->offset, inbuf->bodyend);
#endif
  logger(LOG_DEBUG, "decode base64");
  /* Exit on empty input */
  if (!inbuf->size)
    return(NOK);



  /* Initialize working pointers */
  inb = inbuf->contents + inbuf->offset;
  if (process)
    {
      outbuf = (struct data *)Yalloc(sizeof(struct data ));
    }
  l = 0;
  triple = 0;
  left = inbuf->bodyend - inbuf->offset;
  /*
   * Process entire inbuf.
   */
  while (left != 0)
    {
      left--;
      i = fromb64[(unsigned char)*inb];
      switch(i)
	{
	case FAIL:
#ifdef DEBUG
	  if (edebug)
	    fprintf(stderr, ", illegal char at %lu: %d (failed).\n", 
		    inbuf->offset, *inb);
#endif
	  sprintf(ebuf, "decode_base64: Illegal character: %d", *inb);
	  logger(LOG_WARNING, ebuf);
	  return(NOK);
	  break;
	case SKIP:
	  break;
	default:
	  triple = triple<<6 | (0x3f & i);
	  l++;
	  break;
	}
      if (l == 4 || left == 0)
	{
	  switch(l)
	    {
	    case 2:
	      triple = triple>>4;
	      break;
	    case 3:
	      triple = triple>>2;
	      break;
	    default:
	      break;
	    }
	  if (process)
	    for (l  -= 2; l >= 0; l--)
	      {
		append_char(outbuf,( 0xff & (triple>>(l*8))), pz);
	      }
	  triple = 0;
	  l = 0;
	}
      inb++;
      inbuf->offset += 1;
    }
  if (process)
    {
      safe_mchange(m, outbuf);
      if (match(m->sd->type, "TEXT"))
	{
	  outbuf->encoding = E7BIT;
	  check_bits(m->td);
	}
      else
	outbuf->encoding = EBINARY;
    }
#ifdef DEBUG
  if (edebug)
    fprintf(stderr, ", done.\n");
#endif
  return(OK);
}