1 /*
2 cdecoder.c - c source to a base64 decoding algorithm implementation
3
4 This is part of the libb64 project, and has been placed in the public domain.
5 For details, see http://sourceforge.net/projects/libb64
6 */
7
8 #include <minizinc/_thirdparty/b64/cdecode.h>
9
base64_decode_value(char value_in)10 int base64_decode_value(char value_in) {
11 static const char decoding[] = {62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1,
12 -1, -1, -2, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
13 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
14 -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
15 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51};
16 static const char decoding_size = sizeof(decoding);
17 value_in -= 43;
18 if (value_in < 0 || value_in >= decoding_size) return -1;
19 return decoding[(int)value_in];
20 }
21
base64_init_decodestate(base64_decodestate * state_in)22 void base64_init_decodestate(base64_decodestate* state_in) {
23 state_in->step = step_a;
24 state_in->plainchar = 0;
25 }
26
base64_decode_block(const char * code_in,const int length_in,char * plaintext_out,base64_decodestate * state_in)27 int base64_decode_block(const char* code_in, const int length_in, char* plaintext_out,
28 base64_decodestate* state_in) {
29 const char* codechar = code_in;
30 char* plainchar = plaintext_out;
31 char fragment;
32
33 *plainchar = state_in->plainchar;
34
35 switch (state_in->step) {
36 while (1) {
37 case step_a:
38 do {
39 if (codechar == code_in + length_in) {
40 state_in->step = step_a;
41 state_in->plainchar = *plainchar;
42 return plainchar - plaintext_out;
43 }
44 fragment = (char)base64_decode_value(*codechar++);
45 } while (fragment < 0);
46 *plainchar = (fragment & 0x03f) << 2;
47 case step_b:
48 do {
49 if (codechar == code_in + length_in) {
50 state_in->step = step_b;
51 state_in->plainchar = *plainchar;
52 return plainchar - plaintext_out;
53 }
54 fragment = (char)base64_decode_value(*codechar++);
55 } while (fragment < 0);
56 *plainchar++ |= (fragment & 0x030) >> 4;
57 *plainchar = (fragment & 0x00f) << 4;
58 case step_c:
59 do {
60 if (codechar == code_in + length_in) {
61 state_in->step = step_c;
62 state_in->plainchar = *plainchar;
63 return plainchar - plaintext_out;
64 }
65 fragment = (char)base64_decode_value(*codechar++);
66 } while (fragment < 0);
67 *plainchar++ |= (fragment & 0x03c) >> 2;
68 *plainchar = (fragment & 0x003) << 6;
69 case step_d:
70 do {
71 if (codechar == code_in + length_in) {
72 state_in->step = step_d;
73 state_in->plainchar = *plainchar;
74 return plainchar - plaintext_out;
75 }
76 fragment = (char)base64_decode_value(*codechar++);
77 } while (fragment < 0);
78 *plainchar++ |= (fragment & 0x03f);
79 }
80 }
81 /* control should not reach here */
82 return plainchar - plaintext_out;
83 }
84