1 /*
2 
3 	   Encode or decode file as MIME base64 (RFC 1341)
4 
5 			    by John Walker
6 		       http://www.fourmilab.ch/
7 
8 		This program is in the public domain.
9 
10 */
11 #include <stdio.h>
12 
13 /*  ENCODE  --	Encode binary file into base64.  */
14 #include <stdlib.h>
15 #include <ctype.h>
16 #include <php.h>
17 
18 #include "base64.h"
19 
20 static unsigned char dtable[512];
21 
buffer_new(struct buffer_st *b)22 void buffer_new(struct buffer_st *b)
23 {
24   b->length = 512;
25   b->data = emalloc(sizeof(char)*(b->length));
26   b->data[0] = 0;
27   b->ptr = b->data;
28   b->offset = 0;
29 }
30 
buffer_add(struct buffer_st *b, char c)31 void buffer_add(struct buffer_st *b, char c)
32 {
33   if ((INT_MAX - b->length) <= 512) {
34 		return;
35   }
36   *(b->ptr++) = c;
37   b->offset++;
38   if (b->offset == b->length) {
39     b->length += 512;
40     b->data = erealloc(b->data, b->length);
41     b->ptr = b->data + b->offset;
42   }
43 }
44 
buffer_delete(struct buffer_st *b)45 void buffer_delete(struct buffer_st *b)
46 {
47   efree(b->data);
48   b->length = 0;
49   b->offset = 0;
50   b->ptr = NULL;
51   b->data = NULL;
52 }
53 
base64_encode_xmlrpc(struct buffer_st *b, const char *source, int length)54 void base64_encode_xmlrpc(struct buffer_st *b, const char *source, int length)
55 {
56   int i, hiteof = 0;
57   int offset = 0;
58 
59   buffer_new(b);
60 
61   /*	Fill dtable with character encodings.  */
62 
63   for (i = 0; i < 26; i++) {
64     dtable[i] = 'A' + i;
65     dtable[26 + i] = 'a' + i;
66   }
67   for (i = 0; i < 10; i++) {
68     dtable[52 + i] = '0' + i;
69   }
70   dtable[62] = '+';
71   dtable[63] = '/';
72 
73   while (!hiteof) {
74     unsigned char igroup[3], ogroup[4];
75 	int c, n;
76 
77     igroup[0] = igroup[1] = igroup[2] = 0;
78     for (n = 0; n < 3; n++) {
79       c = *(source++);
80       offset++;
81       if (offset > length || offset <= 0) {
82 	hiteof = 1;
83 	break;
84       }
85       igroup[n] = (unsigned char) c;
86     }
87     if (n > 0) {
88       ogroup[0] = dtable[igroup[0] >> 2];
89       ogroup[1] = dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)];
90       ogroup[2] = dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)];
91       ogroup[3] = dtable[igroup[2] & 0x3F];
92 
93       /* Replace characters in output stream with "=" pad
94 	 characters if fewer than three characters were
95 	 read from the end of the input stream. */
96 
97       if (n < 3) {
98 	ogroup[3] = '=';
99 	if (n < 2) {
100 	  ogroup[2] = '=';
101 	}
102       }
103       for (i = 0; i < 4; i++) {
104 	buffer_add(b, ogroup[i]);
105 	if (!(b->offset % 72)) {
106 	  /* buffer_add(b, '\r'); */
107 	  buffer_add(b, '\n');
108 	}
109       }
110     }
111   }
112   /* buffer_add(b, '\r'); */
113   buffer_add(b, '\n');
114 }
115 
base64_decode_xmlrpc(struct buffer_st *bfr, const char *source, int length)116 void base64_decode_xmlrpc(struct buffer_st *bfr, const char *source, int length)
117 {
118     int i;
119     int offset = 0;
120     int endoffile;
121     int count;
122 
123     buffer_new(bfr);
124 
125     for (i = 0; i < 255; i++) {
126 	dtable[i] = 0x80;
127     }
128     for (i = 'A'; i <= 'Z'; i++) {
129         dtable[i] = 0 + (i - 'A');
130     }
131     for (i = 'a'; i <= 'z'; i++) {
132         dtable[i] = 26 + (i - 'a');
133     }
134     for (i = '0'; i <= '9'; i++) {
135         dtable[i] = 52 + (i - '0');
136     }
137     dtable['+'] = 62;
138     dtable['/'] = 63;
139     dtable['='] = 0;
140 
141     endoffile = 0;
142 
143     /*CONSTANTCONDITION*/
144     while (1) {
145 	unsigned char a[4], b[4], o[3];
146 
147 	for (i = 0; i < 4; i++) {
148 	    int c;
149 	    while (1) {
150 	      c = *(source++);
151 	      offset++;
152 	      if (offset > length) endoffile = 1;
153 	      if (isspace(c) || c == '\n' || c == '\r') continue;
154 	      break;
155 	    }
156 
157 	    if (endoffile) {
158 	      /*
159 		if (i > 0) {
160                     fprintf(stderr, "Input file incomplete.\n");
161 		    exit(1);
162 		}
163 	      */
164 		return;
165 	    }
166 
167 	    if (dtable[(unsigned char)c] & 0x80) {
168 	      /*
169 	      fprintf(stderr, "Offset %i length %i\n", offset, length);
170 	      fprintf(stderr, "character '%c:%x:%c' in input file.\n", c, c, dtable[c]);
171 	      exit(1);
172 	      */
173 	      i--;
174 	      continue;
175 	    }
176 	    a[i] = (unsigned char) c;
177 	    b[i] = (unsigned char) dtable[c];
178 	}
179 	o[0] = (b[0] << 2) | (b[1] >> 4);
180 	o[1] = (b[1] << 4) | (b[2] >> 2);
181 	o[2] = (b[2] << 6) | b[3];
182         i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
183 	count = 0;
184 	while (count < i) {
185 	  buffer_add(bfr, o[count++]);
186 	}
187 	if (i < 3) {
188 	    return;
189 	}
190     }
191 }
192