Switch to unified view

a/src/utils/base64.cpp b/src/utils/base64.cpp
1
#ifndef lint
1
#ifndef lint
2
static char rcsid[] = "@(#$Id: base64.cpp,v 1.3 2005-11-24 07:16:16 dockes Exp $ (C) 2005 J.F.Dockes";
2
static char rcsid[] = "@(#$Id: base64.cpp,v 1.4 2005-11-25 08:49:31 dockes Exp $ (C) 2005 J.F.Dockes";
3
#endif
3
#endif
4
4
5
#include <sys/types.h>
5
#include <sys/types.h>
6
6
7
#include <string>
7
#include <string>
...
...
23
bool base64_decode(const string& in, string& out)
23
bool base64_decode(const string& in, string& out)
24
{
24
{
25
    int io = 0, state = 0, ch;
25
    int io = 0, state = 0, ch;
26
    char *pos;
26
    char *pos;
27
    unsigned int ii = 0;
27
    unsigned int ii = 0;
28
    out.erase();
28
    out.reserve(in.length());
29
    out.reserve(in.length());
29
30
30
    for (ii = 0; ii < in.length(); ii++) {
31
    for (ii = 0; ii < in.length(); ii++) {
31
    ch = in[ii];
32
    ch = in[ii];
32
    if (isspace((unsigned char)ch))        /* Skip whitespace anywhere. */
33
    if (isspace((unsigned char)ch))        /* Skip whitespace anywhere. */
...
...
122
        DPRINT((stderr, "base64_dec: bad extra bits!\n"));
123
        DPRINT((stderr, "base64_dec: bad extra bits!\n"));
123
        // Well, there are bad encoders out there. Let it pass
124
        // Well, there are bad encoders out there. Let it pass
124
        out[io] = 0;
125
        out[io] = 0;
125
        // return false;
126
        // return false;
126
        }
127
        }
128
      // We've appended an extra 0.
129
      out.resize(io);
127
    }
130
    }
128
    } else {
131
    } else {
129
    /*
132
    /*
130
     * We ended by seeing the end of the string.  Make sure we
133
     * We ended by seeing the end of the string.  Make sure we
131
     * have no partial bytes lying around.
134
     * have no partial bytes lying around.
...
...
148
{
151
{
149
    size_t datalength = 0;
152
    size_t datalength = 0;
150
    unsigned char input[3];
153
    unsigned char input[3];
151
    unsigned char output[4];
154
    unsigned char output[4];
152
    size_t i;
155
    size_t i;
156
157
    out.erase();
153
158
154
    int srclength = in.length();
159
    int srclength = in.length();
155
    int sidx = 0;
160
    int sidx = 0;
156
    while (2 < srclength) {
161
    while (2 < srclength) {
157
    input[0] = in[sidx++];
162
    input[0] = in[sidx++];
...
...
199
    return;
204
    return;
200
}
205
}
201
206
202
#ifdef TEST_BASE64
207
#ifdef TEST_BASE64
203
#include <stdio.h>
208
#include <stdio.h>
209
const char *values[] = {"", "1", "12", "123", "1234", "12345", "123456"};
210
int nvalues = sizeof(values) / sizeof(char *);
204
int main(int agrc, char **argv)
211
int main(int argc, char **argv)
205
{
212
{
206
    string in = "12345";
207
    string out;
213
    string in, out, back;
214
    int err = 0;
215
    for (int i = 0; i < nvalues; i++) {
216
  in = values[i];
217
  base64_encode(in, out);
218
  base64_decode(out, back);
219
  if (in != back) {
220
      fprintf(stderr, "In [%s] %d != back [%s] %d (out [%s] %d\n", 
221
          in.c_str(),in.length(), 
222
          back.c_str(), back.length(),
223
          out.c_str(), out.length()
224
          );
225
      err++;
226
  }
227
    }
228
    in.erase();
229
    in += char(0);
230
    in += char(0);
231
    in += char(0);
232
    in += char(0);
208
    base64_encode(in, out);
233
    base64_encode(in, out);
209
    printf("in %s out %s\n", in.c_str(), out.c_str());
234
    base64_decode(out, back);
235
    if (in != back) {
236
  fprintf(stderr, "In [%s] %d != back [%s] %d (out [%s] %d\n", 
237
      in.c_str(),in.length(), 
238
      back.c_str(), back.length(),
239
      out.c_str(), out.length()
240
      );
241
  err++;
242
    }
243
    exit(!(err == 0));
210
}
244
}
211
#endif
245
#endif