1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
2 /* vi: set expandtab shiftwidth=4 tabstop=4: */
3 
4 /**
5  * B64FAST - High performance base64 encoder/decoder
6  * Version 1.1 -- 20-Feb-2005
7  *
8  * Copyright 2005, 2006 Nick Galbreath -- nickg [at] modp [dot] com
9  * All rights reserved.
10  *
11  * http://modp.com/release/base64
12  *
13  * Released under bsd license.  See b64fast.c for details.
14  *
15  * Data table generator.  This generates a ".h" file for use
16  * in compiling b64fast.c.  This does not need to be exported.
17  *
18  */
19 
20 
21 /****************************/
22 /* To change the alphabet   */
23 /* Edit the following lines */
24 /* and do a 'make'          */
25 /****************************/
26 
27 /****************************/
28 
29 
30 #include "arraytoc.h"
31 #include <stdint.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <stdio.h>
35 
36 static unsigned char b64chars[64] = {
37     'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
38     'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
39     'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
40     'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
41     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
42 } ;
43 
44 static unsigned char padchar = '=';
45 
printStart()46 void printStart()
47 {
48     printf("#include <stdint.h>\n");
49     printf("#define CHAR62 '%c'\n", b64chars[62]);
50     printf("#define CHAR63 '%c'\n", b64chars[63]);
51     printf("#define CHARPAD '%c'\n", padchar);
52 }
53 
clearDecodeTable(uint32_t * ary)54 void clearDecodeTable(uint32_t* ary)
55 {
56     int i = 0;
57     for (i = 0; i < 256; ++i) {
58         ary[i] = 0x01FFFFFF;
59     }
60 }
61 
main(int argc,char ** argv)62 int main(int argc, char** argv)
63 {
64     // over-ride standard alphabet
65     if (argc == 2) {
66         unsigned char* replacements = (unsigned char*) argv[1];
67         if (strlen((char*)replacements) != 3) {
68             fprintf(stderr, "input must be a string of 3 characters '-', '.' or '_'\n");
69             exit(1);
70         }
71         fprintf(stderr, "fusing '%s' as replacements in base64 encoding\n", replacements);
72         b64chars[62] = replacements[0];
73         b64chars[63] = replacements[1];
74         padchar = replacements[2];
75     }
76 
77     uint32_t x;
78     uint32_t i = 0;
79     char cary[256];
80     uint32_t ary[256];
81 
82     printStart();
83 
84     for (i = 0; i < 256; ++i) {
85         cary[i] = (char)(b64chars[i >> 2 & 0x3f]);
86     }
87     char_array_to_c(cary, 256, "e0");
88 
89     for (i = 0; i < 256; ++i) {
90         cary[i] = (char) b64chars[(i & 0x3F)]; }
91     char_array_to_c(cary, 256, "e1");
92 
93     for (i = 0; i < 256; ++i) { cary[i] = (char) b64chars[(i & 0x3F)]; }
94     char_array_to_c(cary, 256, "e2");
95 
96 
97     printf("\n\n#ifdef WORDS_BIGENDIAN\n");
98     printf("\n\n/* SPECIAL DECODE TABLES FOR BIG ENDIAN (IBM/MOTOROLA/SUN) CPUS */\n\n");
99 
100 
101     clearDecodeTable(ary);
102     for (i = 0; i < 64; ++i) {
103         x = b64chars[i];
104         ary[x] = i << 18;
105     }
106     uint32_array_to_c_hex(ary, 256,"d0");
107     printf("\n\n");
108 
109     clearDecodeTable(ary);
110     for (i = 0; i < 64; ++i) {
111         x = b64chars[i];
112         ary[x] = i << 12;
113     }
114     uint32_array_to_c_hex(ary, 256, "d1");
115     printf("\n\n");
116 
117     clearDecodeTable(ary);
118     for (i = 0; i < 64; ++i) {
119         x = b64chars[i];
120         ary[x] = i << 6;
121     }
122     uint32_array_to_c_hex(ary, 256, "d2");
123     printf("\n\n");
124 
125     clearDecodeTable(ary);
126     for (i = 0; i < 64; ++i) {
127         x = b64chars[i];
128         ary[x] = i;
129     }
130     uint32_array_to_c_hex(ary, 256, "d3");
131     printf("\n\n");
132 
133     printf("#else\n");
134 
135     printf("\n\n/* SPECIAL DECODE TABLES FOR LITTLE ENDIAN (INTEL) CPUS */\n\n");
136 
137     clearDecodeTable(ary);
138     for (i = 0; i < 64; ++i) {
139         x = b64chars[i];
140         ary[x] = i << 2;
141     }
142     uint32_array_to_c_hex(ary, 256,"d0");
143     printf("\n\n");
144 
145     clearDecodeTable(ary);
146     for (i = 0; i < 64; ++i) {
147         x = b64chars[i];
148         ary[x] = ((i & 0x30) >> 4) | ((i & 0x0F) << 12);
149     }
150     uint32_array_to_c_hex(ary, 256, "d1");
151     printf("\n\n");
152 
153     clearDecodeTable(ary);
154     for (i = 0; i < 64; ++i) {
155         x = b64chars[i];
156         ary[x] = ((i & 0x03) << 22) | ((i & 0x3c) << 6);
157     }
158     uint32_array_to_c_hex(ary, 256, "d2");
159     printf("\n\n");
160 
161     clearDecodeTable(ary);
162     for (i = 0; i < 64; ++i) {
163         x = b64chars[i];
164         ary[x] = i << 16;
165     }
166     uint32_array_to_c_hex(ary, 256, "d3");
167     printf("\n\n");
168 
169 
170     printf("#endif\n");
171 
172     return 0;
173 }
174