mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / storage / ndb / src / common / util / uucode.c
blob4f2d66c9903497b62f303506b5215385d7374b57
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.
27 void
28 uuencode(const char * data, int dataLen, FILE * out)
30 int ch, n;
31 const char *p = data;
33 fprintf(out, "begin\n");
35 while (dataLen > 0){
36 n = dataLen > 45 ? 45 : dataLen;
37 dataLen -= n;
38 ch = ENC(n);
39 if (putc(ch, out) == EOF)
40 break;
41 for (; n > 0; n -= 3, p += 3) {
42 char p_0 = * p;
43 char p_1 = 0;
44 char p_2 = 0;
46 if(n >= 2){
47 p_1 = p[1];
49 if(n >= 3){
50 p_2 = p[2];
53 ch = p_0 >> 2;
54 ch = ENC(ch);
55 if (putc(ch, out) == EOF)
56 break;
57 ch = ((p_0 << 4) & 060) | ((p_1 >> 4) & 017);
58 ch = ENC(ch);
59 if (putc(ch, out) == EOF)
60 break;
61 ch = ((p_1 << 2) & 074) | ((p_2 >> 6) & 03);
62 ch = ENC(ch);
63 if (putc(ch, out) == EOF)
64 break;
65 ch = p_2 & 077;
66 ch = ENC(ch);
67 if (putc(ch, out) == EOF)
68 break;
70 if (putc('\n', out) == EOF)
71 break;
73 ch = ENC('\0');
74 putc(ch, out);
75 putc('\n', out);
76 fprintf(out, "end\n");
79 int
80 uudecode(FILE * input, char * outBuf, int bufLen){
81 int n;
82 char ch, *p, returnCode;
83 char buf[255];
85 returnCode = 0;
86 /* search for header line */
87 do {
88 if (!fgets(buf, sizeof(buf), input)) {
89 return 1;
91 } while (strncmp(buf, "begin", 5));
93 /* for each input line */
94 for (;;) {
95 if (!fgets(p = buf, sizeof(buf), input)) {
96 return 1;
99 * `n' is used to avoid writing out all the characters
100 * at the end of the file.
102 if ((n = DEC(*p)) <= 0)
103 break;
104 if(n >= bufLen){
105 returnCode = 1;
106 break;
108 for (++p; n > 0; p += 4, n -= 3)
109 if (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--;
116 } else {
117 if (n >= 1) {
118 ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
119 * outBuf = ch; outBuf++; bufLen--;
121 if (n >= 2) {
122 ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
123 * outBuf = ch; outBuf++; bufLen--;
125 if (n >= 3) {
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")) {
132 return 1;
134 return returnCode;
137 int
138 uuencode_mem(char * dst, const char * data, int dataLen)
140 int sz = 0;
142 int ch, n;
143 const char *p = data;
145 while (dataLen > 0){
146 n = dataLen > 45 ? 45 : dataLen;
147 dataLen -= n;
148 ch = ENC(n);
149 * dst = ch; dst++; sz++;
150 for (; n > 0; n -= 3, p += 3) {
151 char p_0 = * p;
152 char p_1 = 0;
153 char p_2 = 0;
155 if(n >= 2){
156 p_1 = p[1];
158 if(n >= 3){
159 p_2 = p[2];
162 ch = p_0 >> 2;
163 ch = ENC(ch);
164 * dst = ch; dst++; sz++;
166 ch = ((p_0 << 4) & 060) | ((p_1 >> 4) & 017);
167 ch = ENC(ch);
168 * dst = ch; dst++; sz++;
170 ch = ((p_1 << 2) & 074) | ((p_2 >> 6) & 03);
171 ch = ENC(ch);
172 * dst = ch; dst++; sz++;
174 ch = p_2 & 077;
175 ch = ENC(ch);
176 * dst = ch; dst++; sz++;
179 * dst = '\n'; dst++; sz++;
181 ch = ENC('\0');
182 * dst = ch; dst++; sz++;
184 * dst = '\n'; dst++; sz++;
185 * dst = 0; dst++; sz++;
187 return sz;
191 uudecode_mem(char * outBuf, int bufLen, const char * src){
192 int n;
193 char ch;
194 int sz = 0;
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)
202 return 0;
203 if(n >= bufLen){
204 return -1;
206 for (++p; n > 0; p += 4, n -= 3){
207 if (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++;
214 } else {
215 if (n >= 1) {
216 ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
217 * outBuf = ch; outBuf++; bufLen--; sz++;
219 if (n >= 2) {
220 ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
221 * outBuf = ch; outBuf++; bufLen--; sz++;
223 if (n >= 3) {
224 ch = DEC(p[2]) << 6 | DEC(p[3]);
225 * outBuf = ch; outBuf++; bufLen--; sz++;
229 return sz;