1 /*
2    Copyright (C) 2003-2006 MySQL AB
3     All rights reserved. Use is subject to license terms.
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License, version 2.0,
7    as published by the Free Software Foundation.
8 
9    This program is also distributed with certain software (including
10    but not limited to OpenSSL) that is licensed under separate terms,
11    as designated in a particular file or component or in included license
12    documentation.  The authors of MySQL hereby grant you an additional
13    permission to link the program and your derivative works with the
14    separately licensed software that they have included with MySQL.
15 
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License, version 2.0, for more details.
20 
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
24 */
25 
26 
27 #include <ndb_global.h>
28 
29 /* ENC is the basic 1 character encoding function to make a char printing */
30 /* DEC is single character decode */
31 #define	ENC(c) ((c) ? ((c) & 077) + ' ': '`')
32 #define	DEC(c) (((c) - ' ') & 077)
33 
34 /*
35  * copy from in to out, encoding as you go along.
36  */
37 void
uuencode(const char * data,int dataLen,FILE * out)38 uuencode(const char * data, int dataLen, FILE * out)
39 {
40   int ch, n;
41   const char *p = data;
42 
43   fprintf(out, "begin\n");
44 
45   while (dataLen > 0){
46     n = dataLen > 45 ? 45 : dataLen;
47     dataLen -= n;
48     ch = ENC(n);
49     if (putc(ch, out) == EOF)
50       break;
51     for (; n > 0; n -= 3, p += 3) {
52       char p_0 = * p;
53       char p_1 = 0;
54       char p_2 = 0;
55 
56       if(n >= 2){
57 	p_1 = p[1];
58       }
59       if(n >= 3){
60 	p_2 = p[2];
61       }
62 
63       ch = p_0 >> 2;
64       ch = ENC(ch);
65       if (putc(ch, out) == EOF)
66 	break;
67       ch = ((p_0 << 4) & 060) | ((p_1 >> 4) & 017);
68       ch = ENC(ch);
69       if (putc(ch, out) == EOF)
70 	break;
71       ch = ((p_1 << 2) & 074) | ((p_2 >> 6) & 03);
72       ch = ENC(ch);
73       if (putc(ch, out) == EOF)
74 	break;
75       ch = p_2 & 077;
76       ch = ENC(ch);
77       if (putc(ch, out) == EOF)
78 	break;
79     }
80     if (putc('\n', out) == EOF)
81       break;
82   }
83   ch = ENC('\0');
84   putc(ch, out);
85   putc('\n', out);
86   fprintf(out, "end\n");
87 }
88 
89 int
uudecode(FILE * input,char * outBuf,int bufLen)90 uudecode(FILE * input, char * outBuf, int bufLen){
91   int n;
92   char ch, *p, returnCode;
93   char buf[255];
94 
95   returnCode = 0;
96   /* search for header line */
97   do {
98     if (!fgets(buf, sizeof(buf), input)) {
99       return 1;
100     }
101   } while (strncmp(buf, "begin", 5));
102 
103   /* for each input line */
104   for (;;) {
105     if (!fgets(p = buf, sizeof(buf), input)) {
106       return 1;
107     }
108     /*
109      * `n' is used to avoid writing out all the characters
110      * at the end of the file.
111      */
112     if ((n = DEC(*p)) <= 0)
113       break;
114     if(n >= bufLen){
115       returnCode = 1;
116       break;
117     }
118     for (++p; n > 0; p += 4, n -= 3)
119       if (n >= 3) {
120 	ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
121 	* outBuf = ch; outBuf++; bufLen--;
122 	ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
123 	* outBuf = ch; outBuf++; bufLen--;
124 	ch = DEC(p[2]) << 6 | DEC(p[3]);
125 	* outBuf = ch; outBuf++; bufLen--;
126       } else {
127 	if (n >= 1) {
128 	  ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
129 	  * outBuf = ch; outBuf++; bufLen--;
130 	}
131 	if (n >= 2) {
132 	  ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
133 	  * outBuf = ch; outBuf++; bufLen--;
134 	}
135 	if (n >= 3) {
136 	  ch = DEC(p[2]) << 6 | DEC(p[3]);
137 	  * outBuf = ch; outBuf++; bufLen--;
138 	}
139       }
140   }
141   if (!fgets(buf, sizeof(buf), input) || strcmp(buf, "end\n")) {
142     return 1;
143   }
144   return returnCode;
145 }
146 
147 int
uuencode_mem(char * dst,const char * data,int dataLen)148 uuencode_mem(char * dst, const char * data, int dataLen)
149 {
150   int sz = 0;
151 
152   int ch, n;
153   const char *p = data;
154 
155   while (dataLen > 0){
156     n = dataLen > 45 ? 45 : dataLen;
157     dataLen -= n;
158     ch = ENC(n);
159     * dst = ch; dst++; sz++;
160     for (; n > 0; n -= 3, p += 3) {
161       char p_0 = * p;
162       char p_1 = 0;
163       char p_2 = 0;
164 
165       if(n >= 2){
166 	p_1 = p[1];
167       }
168       if(n >= 3){
169 	p_2 = p[2];
170       }
171 
172       ch = p_0 >> 2;
173       ch = ENC(ch);
174       * dst = ch; dst++; sz++;
175 
176       ch = ((p_0 << 4) & 060) | ((p_1 >> 4) & 017);
177       ch = ENC(ch);
178       * dst = ch; dst++; sz++;
179 
180       ch = ((p_1 << 2) & 074) | ((p_2 >> 6) & 03);
181       ch = ENC(ch);
182       * dst = ch; dst++; sz++;
183 
184       ch = p_2 & 077;
185       ch = ENC(ch);
186       * dst = ch; dst++; sz++;
187     }
188 
189     * dst = '\n'; dst++; sz++;
190   }
191   ch = ENC('\0');
192   * dst = ch; dst++; sz++;
193 
194   * dst = '\n'; dst++; sz++;
195   * dst = 0;    dst++; sz++;
196 
197   return sz;
198 }
199 
200 int
uudecode_mem(char * outBuf,int bufLen,const char * src)201 uudecode_mem(char * outBuf, int bufLen, const char * src){
202   int n;
203   char ch;
204   int sz = 0;
205   const char * p = src;
206 
207   /*
208    * `n' is used to avoid writing out all the characters
209    * at the end of the file.
210    */
211   if ((n = DEC(*p)) <= 0)
212     return 0;
213   if(n >= bufLen){
214     return -1;
215   }
216   for (++p; n > 0; p += 4, n -= 3){
217     if (n >= 3) {
218       ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
219       * outBuf = ch; outBuf++; bufLen--; sz++;
220       ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
221       * outBuf = ch; outBuf++; bufLen--; sz++;
222       ch = DEC(p[2]) << 6 | DEC(p[3]);
223       * outBuf = ch; outBuf++; bufLen--; sz++;
224     } else {
225       if (n >= 1) {
226 	ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
227 	* outBuf = ch; outBuf++; bufLen--; sz++;
228       }
229       if (n >= 2) {
230 	ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
231 	* outBuf = ch; outBuf++; bufLen--; sz++;
232       }
233       if (n >= 3) {
234 	ch = DEC(p[2]) << 6 | DEC(p[3]);
235 	* outBuf = ch; outBuf++; bufLen--; sz++;
236       }
237     }
238   }
239   return sz;
240 }
241 
242 
243 
244