aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/base64/plain64/dec_tail.c
blob: 7db2408190a0e6423b6b44c396493134bb5c5e2f (plain) (blame)
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
		if (srclen-- == 0) {
			ret = 1;
			break;
		}
		if ((q = plain64_base64_table_dec[*c++]) >= 254) {
			st.eof = 1;
			// Treat character '=' as invalid for byte 0:
			break;
		}
		st.carry = q << 2;
		st.bytes++;

	case 1:	if (srclen-- == 0) {
			ret = 1;
			break;
		}
		if ((q = plain64_base64_table_dec[*c++]) >= 254) {
			st.eof = 1;
			// Treat character '=' as invalid for byte 1:
			break;
		}
		*o++ = st.carry | (q >> 4);
		st.carry = q << 4;
		st.bytes++;
		outl++;

	case 2:	if (srclen-- == 0) {
			ret = 1;
			break;
		}
		if ((q = plain64_base64_table_dec[*c++]) >= 254) {
			st.eof = 1;
			// When q == 254, the input char is '='. Return 1 and EOF.
			// Technically, should check if next byte is also '=', but never mind.
			// When q == 255, the input char is invalid. Return 0 and EOF.
			ret = (q == 254) ? 1 : 0;
			break;
		}
		*o++ = st.carry | (q >> 2);
		st.carry = q << 6;
		st.bytes++;
		outl++;

	case 3:	if (srclen-- == 0) {
			ret = 1;
			break;
		}
		if ((q = plain64_base64_table_dec[*c++]) >= 254) {
			st.eof = 1;
			// When q == 254, the input char is '='. Return 1 and EOF.
			// When q == 255, the input char is invalid. Return 0 and EOF.
			ret = (q == 254) ? 1 : 0;
			break;
		}
		*o++ = st.carry | q;
		st.carry = 0;
		st.bytes = 0;
		outl++;
	}
}
state->eof = st.eof;
state->bytes = st.bytes;
state->carry = st.carry;
*outlen = outl;
return ret;