doc: Update time stamp documentation to 2014-09-15 specification
[libisds.git] / src / cdecode.c
blob6eba8bd9940f2595cc97c6ae75f304754d78e0c6
1 /*
2 cdecoder.c - c source to a base64 decoding algorithm implementation
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 */
8 #include "cdecode.h"
9 #include "utils.h"
11 static int8_t base64_decode_value(int8_t value_in) {
12 static const int8_t decoding[] = {
13 62, -1, -1, -1, 63, 52, 53, 54,
14 55, 56, 57, 58, 59, 60, 61, -1,
15 -1, -1, -2, -1, -1, -1, 0, 1,
16 2, 3, 4, 5, 6, 7, 8, 9,
17 10, 11, 12, 13, 14, 15, 16, 17,
18 18, 19, 20, 21, 22, 23, 24, 25,
19 -1, -1, -1, -1, -1, -1, 26, 27,
20 28, 29, 30, 31, 32, 33, 34, 35,
21 36, 37, 38, 39, 40, 41, 42, 43,
22 44, 45, 46, 47, 48, 49, 50, 51 };
23 static const size_t decoding_size = sizeof(decoding)/sizeof(*decoding);
24 value_in -= 43;
25 if (value_in < 0 || value_in >= decoding_size) return -1;
26 return decoding[value_in];
30 _hidden void _isds_base64_init_decodestate(base64_decodestate *state_in) {
31 state_in->step = step_a;
32 state_in->plainchar = 0;
36 _hidden size_t _isds_base64_decode_block(const int8_t *code_in,
37 const size_t length_in, int8_t *plaintext_out,
38 base64_decodestate *state_in) {
39 const int8_t *codechar = code_in;
40 int8_t *plainchar = plaintext_out;
41 int8_t fragment;
43 *plainchar = state_in->plainchar;
45 switch (state_in->step) {
46 while (1) {
47 case step_a:
48 do {
49 if (codechar == code_in + length_in) {
50 state_in->step = step_a;
51 state_in->plainchar = *plainchar;
52 return plainchar - plaintext_out;
54 fragment = base64_decode_value(*codechar++);
55 } while (fragment < 0);
56 *plainchar = (fragment & 0x03f) << 2;
57 case step_b:
58 do {
59 if (codechar == code_in+length_in) {
60 state_in->step = step_b;
61 state_in->plainchar = *plainchar;
62 return plainchar - plaintext_out;
64 fragment = base64_decode_value(*codechar++);
65 } while (fragment < 0);
66 *plainchar++ |= (fragment & 0x030) >> 4;
67 *plainchar = (fragment & 0x00f) << 4;
68 case step_c:
69 do {
70 if (codechar == code_in+length_in) {
71 state_in->step = step_c;
72 state_in->plainchar = *plainchar;
73 return plainchar - plaintext_out;
75 fragment = base64_decode_value(*codechar++);
76 } while (fragment < 0);
77 *plainchar++ |= (fragment & 0x03c) >> 2;
78 *plainchar = (fragment & 0x003) << 6;
79 case step_d:
80 do {
81 if (codechar == code_in+length_in) {
82 state_in->step = step_d;
83 state_in->plainchar = *plainchar;
84 return plainchar - plaintext_out;
86 fragment = base64_decode_value(*codechar++);
87 } while (fragment < 0);
88 *plainchar++ |= (fragment & 0x03f);
89 } /* while */
90 } /* switch */
92 /* control should not reach here */
93 return plainchar - plaintext_out;