File: base64.c

package info (click to toggle)
netsurf 3.11-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 87,296 kB
  • sloc: ansic: 403,115; xml: 81,988; cpp: 6,246; perl: 4,605; makefile: 2,907; yacc: 2,246; python: 2,057; sh: 1,500; jsp: 1,156; lex: 623; javascript: 551; ruby: 329; asm: 326; lisp: 151; php: 6
file content (80 lines) | stat: -rw-r--r-- 1,982 bytes parent folder | download | duplicates (3)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * 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 <unistd.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;
        int opt;
        int decode = 0;
        int url = 0;


        while ((opt = getopt(argc, argv, "du")) != -1) {
                switch (opt) {
                case 'd':
                        decode = 1;
                        break;
                case 'u':
                        url = 1;
                        break;

                default: /* '?' */
                        fprintf(stderr, "Usage: %s [-d] [-u]\n", argv[0]);
                        exit(EXIT_FAILURE);

                }
        }

        if (scanf("%1024mc%n", &buffer, (int *)&buffer_len) < 1) {
                return 1;
        }


        if (decode) {
                /* decode */
                if (url) {
                        nsu_base64_decode_alloc_url(buffer, buffer_len, &output, &output_len);
                } else {

                        nsu_base64_decode_alloc(buffer, buffer_len, &output, &output_len);
                }
        } else {
                /* encode */
                if (url) {
                        nsu_base64_encode_alloc_url(buffer, buffer_len, &output, &output_len);
                } else {
                        nsu_base64_encode_alloc(buffer, buffer_len, &output, &output_len);
                }
        }

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

        free(buffer);

        return 0;
}