1 /* Copyright (c) 2003-2005 MySQL AB
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
17 #include <ndb_global.h>
19 /* ENC is the basic 1 character encoding function to make a char printing */
20 /* DEC is single character decode */
21 #define ENC(c) ((c) ? ((c) & 077) + ' ': '`')
22 #define DEC(c) (((c) - ' ') & 077)
25 * copy from in to out, encoding as you go along.
28 uuencode(const char * data
, int dataLen
, FILE * out
)
33 fprintf(out
, "begin\n");
36 n
= dataLen
> 45 ? 45 : dataLen
;
39 if (putc(ch
, out
) == EOF
)
41 for (; n
> 0; n
-= 3, p
+= 3) {
55 if (putc(ch
, out
) == EOF
)
57 ch
= ((p_0
<< 4) & 060) | ((p_1
>> 4) & 017);
59 if (putc(ch
, out
) == EOF
)
61 ch
= ((p_1
<< 2) & 074) | ((p_2
>> 6) & 03);
63 if (putc(ch
, out
) == EOF
)
67 if (putc(ch
, out
) == EOF
)
70 if (putc('\n', out
) == EOF
)
76 fprintf(out
, "end\n");
80 uudecode(FILE * input
, char * outBuf
, int bufLen
){
82 char ch
, *p
, returnCode
;
86 /* search for header line */
88 if (!fgets(buf
, sizeof(buf
), input
)) {
91 } while (strncmp(buf
, "begin", 5));
93 /* for each input line */
95 if (!fgets(p
= buf
, sizeof(buf
), input
)) {
99 * `n' is used to avoid writing out all the characters
100 * at the end of the file.
102 if ((n
= DEC(*p
)) <= 0)
108 for (++p
; n
> 0; p
+= 4, n
-= 3)
110 ch
= DEC(p
[0]) << 2 | DEC(p
[1]) >> 4;
111 * outBuf
= ch
; outBuf
++; bufLen
--;
112 ch
= DEC(p
[1]) << 4 | DEC(p
[2]) >> 2;
113 * outBuf
= ch
; outBuf
++; bufLen
--;
114 ch
= DEC(p
[2]) << 6 | DEC(p
[3]);
115 * outBuf
= ch
; outBuf
++; bufLen
--;
118 ch
= DEC(p
[0]) << 2 | DEC(p
[1]) >> 4;
119 * outBuf
= ch
; outBuf
++; bufLen
--;
122 ch
= DEC(p
[1]) << 4 | DEC(p
[2]) >> 2;
123 * outBuf
= ch
; outBuf
++; bufLen
--;
126 ch
= DEC(p
[2]) << 6 | DEC(p
[3]);
127 * outBuf
= ch
; outBuf
++; bufLen
--;
131 if (!fgets(buf
, sizeof(buf
), input
) || strcmp(buf
, "end\n")) {
138 uuencode_mem(char * dst
, const char * data
, int dataLen
)
143 const char *p
= data
;
146 n
= dataLen
> 45 ? 45 : dataLen
;
149 * dst
= ch
; dst
++; sz
++;
150 for (; n
> 0; n
-= 3, p
+= 3) {
164 * dst
= ch
; dst
++; sz
++;
166 ch
= ((p_0
<< 4) & 060) | ((p_1
>> 4) & 017);
168 * dst
= ch
; dst
++; sz
++;
170 ch
= ((p_1
<< 2) & 074) | ((p_2
>> 6) & 03);
172 * dst
= ch
; dst
++; sz
++;
176 * dst
= ch
; dst
++; sz
++;
179 * dst
= '\n'; dst
++; sz
++;
182 * dst
= ch
; dst
++; sz
++;
184 * dst
= '\n'; dst
++; sz
++;
185 * dst
= 0; dst
++; sz
++;
191 uudecode_mem(char * outBuf
, int bufLen
, const char * src
){
195 const char * p
= src
;
198 * `n' is used to avoid writing out all the characters
199 * at the end of the file.
201 if ((n
= DEC(*p
)) <= 0)
206 for (++p
; n
> 0; p
+= 4, n
-= 3){
208 ch
= DEC(p
[0]) << 2 | DEC(p
[1]) >> 4;
209 * outBuf
= ch
; outBuf
++; bufLen
--; sz
++;
210 ch
= DEC(p
[1]) << 4 | DEC(p
[2]) >> 2;
211 * outBuf
= ch
; outBuf
++; bufLen
--; sz
++;
212 ch
= DEC(p
[2]) << 6 | DEC(p
[3]);
213 * outBuf
= ch
; outBuf
++; bufLen
--; sz
++;
216 ch
= DEC(p
[0]) << 2 | DEC(p
[1]) >> 4;
217 * outBuf
= ch
; outBuf
++; bufLen
--; sz
++;
220 ch
= DEC(p
[1]) << 4 | DEC(p
[2]) >> 2;
221 * outBuf
= ch
; outBuf
++; bufLen
--; sz
++;
224 ch
= DEC(p
[2]) << 6 | DEC(p
[3]);
225 * outBuf
= ch
; outBuf
++; bufLen
--; sz
++;