summaryrefslogtreecommitdiffstats
path: root/dev/base64/base64rev-gen.c
blob: faffc87163a5ba40c89a569f53f8a2a47a1cbda3 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
 * base64rev generator
 *
 * Copyright 2009-2010 Krzysztof Piotr Oledzki <ole@ans.pl>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 *
 */

#include <stdio.h>

const char base64tab[65]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
char base64rev[128];

#define base '#'	/* arbitrary chosen base value */
#define B64MAX	64
#define B64PADV B64MAX

int main() {
	char *p, c;
	int i, min = 255, max = 0;

	for (i = 0; i < sizeof(base64rev); i++)
		base64rev[i] = base;

	for (i = 0;  i < B64MAX; i++) {
		c = base64tab[i];

		if (min > c)
			min = c;

		if (max < c)
			max = c;
	}

	for (i = 0;  i < B64MAX; i++) {
		c = base64tab[i];

		if (base+i+1 > 127) {
			printf("Wrong base value @%d\n", i);
			return 1;
		}

		base64rev[c - min] = base+i+1;
	}

	base64rev['=' - min] = base + B64PADV;

	base64rev[max - min + 1] = '\0';

	printf("#define B64BASE '%c'\n", base);
	printf("#define B64CMIN '%c'\n", min);
	printf("#define B64CMAX '%c'\n", max);
	printf("#define B64PADV %u\n", B64PADV);

	p = base64rev;
	printf("const char base64rev[]=\"");
	for (p = base64rev; *p; p++) {
		if (*p == '\\')
			printf("\\%c", *p);
		else
			printf("%c", *p);
	}
	printf("\"\n");

	return 0;
}