~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to storage/ndb/src/common/util/uucode.c

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2003 MySQL AB
 
2
 
 
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.
 
6
 
 
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.
 
11
 
 
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
 
 
17
#include <ndb_global.h>
 
18
 
 
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)
 
23
 
 
24
/*
 
25
 * copy from in to out, encoding as you go along.
 
26
 */
 
27
void
 
28
uuencode(const char * data, int dataLen, FILE * out)
 
29
{
 
30
  int ch, n;
 
31
  const char *p = data;
 
32
 
 
33
  fprintf(out, "begin\n");
 
34
  
 
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;
 
45
 
 
46
      if(n >= 2){
 
47
        p_1 = p[1];
 
48
      }
 
49
      if(n >= 3){
 
50
        p_2 = p[2];
 
51
      }
 
52
      
 
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;
 
69
    }
 
70
    if (putc('\n', out) == EOF)
 
71
      break;
 
72
  }
 
73
  ch = ENC('\0');
 
74
  putc(ch, out);
 
75
  putc('\n', out);
 
76
  fprintf(out, "end\n");
 
77
}
 
78
 
 
79
int
 
80
uudecode(FILE * input, char * outBuf, int bufLen){
 
81
  int n;
 
82
  char ch, *p, returnCode;
 
83
  char buf[255];
 
84
 
 
85
  returnCode = 0;
 
86
  /* search for header line */
 
87
  do {
 
88
    if (!fgets(buf, sizeof(buf), input)) {
 
89
      return 1;
 
90
    }
 
91
  } while (strncmp(buf, "begin", 5));
 
92
  
 
93
  /* for each input line */
 
94
  for (;;) {
 
95
    if (!fgets(p = buf, sizeof(buf), input)) {
 
96
      return 1;
 
97
    }
 
98
    /*
 
99
     * `n' is used to avoid writing out all the characters
 
100
     * at the end of the file.
 
101
     */
 
102
    if ((n = DEC(*p)) <= 0)
 
103
      break;
 
104
    if(n >= bufLen){
 
105
      returnCode = 1;
 
106
      break;
 
107
    }
 
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--;
 
120
        }
 
121
        if (n >= 2) {
 
122
          ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
 
123
          * outBuf = ch; outBuf++; bufLen--;
 
124
        }
 
125
        if (n >= 3) {
 
126
          ch = DEC(p[2]) << 6 | DEC(p[3]);
 
127
          * outBuf = ch; outBuf++; bufLen--;
 
128
        }
 
129
      }
 
130
  }
 
131
  if (!fgets(buf, sizeof(buf), input) || strcmp(buf, "end\n")) {
 
132
    return 1;
 
133
  }
 
134
  return returnCode;
 
135
}
 
136
 
 
137
int 
 
138
uuencode_mem(char * dst, const char * data, int dataLen)
 
139
{
 
140
  int sz = 0;
 
141
 
 
142
  int ch, n;
 
143
  const char *p = data;
 
144
  
 
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;
 
154
 
 
155
      if(n >= 2){
 
156
        p_1 = p[1];
 
157
      }
 
158
      if(n >= 3){
 
159
        p_2 = p[2];
 
160
      }
 
161
      
 
162
      ch = p_0 >> 2;
 
163
      ch = ENC(ch);
 
164
      * dst = ch; dst++; sz++;
 
165
 
 
166
      ch = ((p_0 << 4) & 060) | ((p_1 >> 4) & 017);
 
167
      ch = ENC(ch);
 
168
      * dst = ch; dst++; sz++;
 
169
 
 
170
      ch = ((p_1 << 2) & 074) | ((p_2 >> 6) & 03);
 
171
      ch = ENC(ch);
 
172
      * dst = ch; dst++; sz++;
 
173
 
 
174
      ch = p_2 & 077;
 
175
      ch = ENC(ch);
 
176
      * dst = ch; dst++; sz++;
 
177
    }
 
178
    
 
179
    * dst = '\n'; dst++; sz++;
 
180
  }
 
181
  ch = ENC('\0');
 
182
  * dst = ch; dst++; sz++;
 
183
  
 
184
  * dst = '\n'; dst++; sz++;
 
185
  * dst = 0;    dst++; sz++;
 
186
  
 
187
  return sz;
 
188
}
 
189
 
 
190
int
 
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;
 
196
 
 
197
  /*
 
198
   * `n' is used to avoid writing out all the characters
 
199
   * at the end of the file.
 
200
   */
 
201
  if ((n = DEC(*p)) <= 0)
 
202
    return 0;
 
203
  if(n >= bufLen){
 
204
    return -1;
 
205
  }
 
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++;
 
218
      }
 
219
      if (n >= 2) {
 
220
        ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
 
221
        * outBuf = ch; outBuf++; bufLen--; sz++;
 
222
      }
 
223
      if (n >= 3) {
 
224
        ch = DEC(p[2]) << 6 | DEC(p[3]);
 
225
        * outBuf = ch; outBuf++; bufLen--; sz++;
 
226
      }
 
227
    }
 
228
  }
 
229
  return sz;
 
230
}
 
231
 
 
232
 
 
233