xref: /PHP-7.3/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 
61   buffer_new(b);
62 
63   /*	Fill dtable with character encodings.  */
64 
65   for (i = 0; i < 26; i++) {
66     dtable[i] = 'A' + i;
67     dtable[26 + i] = 'a' + i;
68   }
69   for (i = 0; i < 10; i++) {
70     dtable[52 + i] = '0' + i;
71   }
72   dtable[62] = '+';
73   dtable[63] = '/';
74 
75   while (!hiteof) {
76     unsigned char igroup[3], ogroup[4];
77 	int c, n;
78 
79     igroup[0] = igroup[1] = igroup[2] = 0;
80     for (n = 0; n < 3; n++) {
81       c = *(source++);
82       offset++;
83       if (offset > length || offset <= 0) {
84 	hiteof = 1;
85 	break;
86       }
87       igroup[n] = (unsigned char) c;
88     }
89     if (n > 0) {
90       ogroup[0] = dtable[igroup[0] >> 2];
91       ogroup[1] = dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)];
92       ogroup[2] = dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)];
93       ogroup[3] = dtable[igroup[2] & 0x3F];
94 
95       /* Replace characters in output stream with "=" pad
96 	 characters if fewer than three characters were
97 	 read from the end of the input stream. */
98 
99       if (n < 3) {
100 	ogroup[3] = '=';
101 	if (n < 2) {
102 	  ogroup[2] = '=';
103 	}
104       }
105       for (i = 0; i < 4; i++) {
106 	buffer_add(b, ogroup[i]);
107 	if (!(b->offset % 72)) {
108 	  /* buffer_add(b, '\r'); */
109 	  buffer_add(b, '\n');
110 	}
111       }
112     }
113   }
114   /* buffer_add(b, '\r'); */
115   buffer_add(b, '\n');
116 }
117 
base64_decode_xmlrpc(struct buffer_st * bfr,const char * source,int length)118 void base64_decode_xmlrpc(struct buffer_st *bfr, const char *source, int length)
119 {
120     int i;
121     int offset = 0;
122     int endoffile;
123     int count;
124 
125     buffer_new(bfr);
126 
127     for (i = 0; i < 255; i++) {
128 	dtable[i] = 0x80;
129     }
130     for (i = 'A'; i <= 'Z'; i++) {
131         dtable[i] = 0 + (i - 'A');
132     }
133     for (i = 'a'; i <= 'z'; i++) {
134         dtable[i] = 26 + (i - 'a');
135     }
136     for (i = '0'; i <= '9'; i++) {
137         dtable[i] = 52 + (i - '0');
138     }
139     dtable['+'] = 62;
140     dtable['/'] = 63;
141     dtable['='] = 0;
142 
143     endoffile = 0;
144 
145     /*CONSTANTCONDITION*/
146     while (1) {
147 	unsigned char a[4], b[4], o[3];
148 
149 	for (i = 0; i < 4; i++) {
150 	    int c;
151 	    while (1) {
152 	      c = *(source++);
153 	      offset++;
154 	      if (offset > length) endoffile = 1;
155 	      if (isspace(c) || c == '\n' || c == '\r') continue;
156 	      break;
157 	    }
158 
159 	    if (endoffile) {
160 	      /*
161 		if (i > 0) {
162                     fprintf(stderr, "Input file incomplete.\n");
163 		    exit(1);
164 		}
165 	      */
166 		return;
167 	    }
168 
169 	    if (dtable[(unsigned char)c] & 0x80) {
170 	      /*
171 	      fprintf(stderr, "Offset %i length %i\n", offset, length);
172 	      fprintf(stderr, "character '%c:%x:%c' in input file.\n", c, c, dtable[c]);
173 	      exit(1);
174 	      */
175 	      i--;
176 	      continue;
177 	    }
178 	    a[i] = (unsigned char) c;
179 	    b[i] = (unsigned char) dtable[c];
180 	}
181 	o[0] = (b[0] << 2) | (b[1] >> 4);
182 	o[1] = (b[1] << 4) | (b[2] >> 2);
183 	o[2] = (b[2] << 6) | b[3];
184         i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
185 	count = 0;
186 	while (count < i) {
187 	  buffer_add(bfr, o[count++]);
188 	}
189 	if (i < 3) {
190 	    return;
191 	}
192     }
193 }
194