summaryrefslogtreecommitdiff
path: root/test/base64.c
blob: 7b09205472e53dc5649f1fcd95ff571e36456f6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
 * Copyright 2014 Vincent Sanders <vince@netsurf-browser.org>
 *
 * This file is part of libnsutils.
 *
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 */

/**
 * \file
 *
 * Base64 test program. Reads data from stdin and en/de codes it to/from base64
 */

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

#include <nsutils/base64.h>

int main(int argc, char**argv)
{
    uint8_t *buffer;
    size_t buffer_len=0;
    uint8_t *output;
    size_t output_len;

    if (scanf("%1024mc%n", &buffer, (int *)&buffer_len) < 1) {
        return 1;
    }
    
    if (argc == 1) {
        /* encode */
        nsu_base64_encode_alloc(buffer, buffer_len, &output, &output_len);
    } else if ((argv[1][0] == '-') && (argv[1][1] == 'd')) {
        /* decode */
        nsu_base64_decode_alloc(buffer, buffer_len, &output, &output_len);
    } else {
        fprintf(stderr, "Usage: %s [-d]\n", argv[0]);
        return 1;
    }

    if (output != NULL) {
        printf("%.*s", (int)output_len, output);
        free(output);
    }

    free(buffer);

    return 0;
}