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