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