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