~ubuntu-branches/ubuntu/trusty/recoll/trusty

« back to all changes in this revision

Viewing changes to utils/base64.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Kartik Mistry
  • Date: 2008-05-29 23:25:40 UTC
  • mfrom: (1.1.5 upstream) (4.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080529232540-9kuyznin2g8zmjn9
Tags: 1.10.2-1
* New upstream release
  + Updated patch debian/patches/02_gcc-snapshot-missing-headers-fix.dpatch
    Some portions are now merged with upstream
* debian/copyright:
  + Fixed indentation to 80 characters
  + Updated Debian package copyright year

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#ifndef lint
2
 
static char rcsid[] = "@(#$Id: base64.cpp,v 1.7 2007/12/13 06:58:22 dockes Exp $ (C) 2005 J.F.Dockes";
 
2
static char rcsid[] = "@(#$Id: base64.cpp,v 1.8 2008/04/18 11:37:50 dockes Exp $ (C) 2005 J.F.Dockes";
3
3
#endif
4
4
/*
5
5
 *   This program is free software; you can redistribute it and/or modify
223
223
 
224
224
#ifdef TEST_BASE64
225
225
#include <stdio.h>
226
 
const char *values[] = {"", "1", "12", "123", "1234", "12345", "123456"};
227
 
int nvalues = sizeof(values) / sizeof(char *);
 
226
 
 
227
#include "readfile.h"
 
228
 
 
229
const char *thisprog;
 
230
static char usage [] = "testfile\n\n"
 
231
;
 
232
static void
 
233
Usage(void)
 
234
{
 
235
    fprintf(stderr, "%s: usage:\n%s", thisprog, usage);
 
236
    exit(1);
 
237
}
 
238
 
 
239
static int     op_flags;
 
240
#define OPT_MOINS 0x1
 
241
#define OPT_i     0x2 
 
242
#define OPT_P     0x4 
 
243
 
228
244
int main(int argc, char **argv)
229
245
{
230
 
    string in, out, back;
231
 
    int err = 0;
232
 
    for (int i = 0; i < nvalues; i++) {
233
 
        in = values[i];
 
246
    thisprog = argv[0];
 
247
    argc--; argv++;
 
248
 
 
249
    while (argc > 0 && **argv == '-') {
 
250
        (*argv)++;
 
251
        if (!(**argv))
 
252
            /* Cas du "adb - core" */
 
253
            Usage();
 
254
        while (**argv)
 
255
            switch (*(*argv)++) {
 
256
            case 'i':   op_flags |= OPT_i; break;
 
257
            default: Usage();   break;
 
258
            }
 
259
        argc--; argv++;
 
260
    }
 
261
    
 
262
    if (op_flags & OPT_i)  {
 
263
        const char *values[] = {"", "1", "12", "123", "1234", 
 
264
                                "12345", "123456"};
 
265
        int nvalues = sizeof(values) / sizeof(char *);
 
266
        string in, out, back;
 
267
        int err = 0;
 
268
        for (int i = 0; i < nvalues; i++) {
 
269
            in = values[i];
 
270
            base64_encode(in, out);
 
271
            base64_decode(out, back);
 
272
            if (in != back) {
 
273
                fprintf(stderr, "In [%s] %d != back [%s] %d (out [%s] %d\n", 
 
274
                        in.c_str(), int(in.length()), 
 
275
                        back.c_str(), int(back.length()),
 
276
                        out.c_str(), int(out.length())
 
277
                        );
 
278
                err++;
 
279
            }
 
280
        }
 
281
        in.erase();
 
282
        in += char(0);
 
283
        in += char(0);
 
284
        in += char(0);
 
285
        in += char(0);
234
286
        base64_encode(in, out);
235
287
        base64_decode(out, back);
236
288
        if (in != back) {
237
289
            fprintf(stderr, "In [%s] %d != back [%s] %d (out [%s] %d\n", 
238
 
                    in.c_str(),in.length(), 
239
 
                    back.c_str(), back.length(),
240
 
                    out.c_str(), out.length()
 
290
                    in.c_str(), int(in.length()), 
 
291
                    back.c_str(), int(back.length()),
 
292
                    out.c_str(), int(out.length())
241
293
                    );
242
294
            err++;
243
295
        }
244
 
    }
245
 
    in.erase();
246
 
    in += char(0);
247
 
    in += char(0);
248
 
    in += char(0);
249
 
    in += char(0);
250
 
    base64_encode(in, out);
251
 
    base64_decode(out, back);
252
 
    if (in != back) {
253
 
        fprintf(stderr, "In [%s] %d != back [%s] %d (out [%s] %d\n", 
254
 
                in.c_str(),in.length(), 
255
 
                back.c_str(), back.length(),
256
 
                out.c_str(), out.length()
257
 
                );
258
 
        err++;
259
 
    }
260
 
    exit(!(err == 0));
 
296
        exit(!(err == 0));
 
297
    } else {
 
298
        if (argc > 1)
 
299
            Usage();
 
300
        string infile;
 
301
        if (argc == 1)
 
302
            infile = *argv++;argc--;
 
303
        string idata, reason;
 
304
        if (!file_to_string(infile, idata, &reason)) {
 
305
            fprintf(stderr, "Can't read file: %s\n", reason.c_str());
 
306
            exit(1);
 
307
        }
 
308
        string odata;
 
309
        if (!base64_decode(idata, odata)) {
 
310
            fprintf(stderr, "Decoding failed\n");
 
311
            exit(1);
 
312
        }
 
313
        write(1, odata.c_str(), 
 
314
              odata.size() * sizeof(string::value_type));
 
315
        exit(0);
 
316
    }
261
317
}
262
318
#endif