summaryrefslogtreecommitdiffstats
path: root/src/decode.rs
blob: 11600ccf3370d93410fd0ad59988548f48ac3cbf (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Copyright 2019 Alexandros Frantzis
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
// SPDX-License-Identifier: MPL-2.0

//! Base64 and quoted-printable decoding.

use crate::Result;

const PAD: u8 = 64; // The pseudo-index of the PAD character.
const INV: u8 = 99; // An invalid index.

static BASE64_INDICES: &'static [u8] = &[
     //   0    1    2    3    4    5    6    7    8    9    A    B    C    D    E    F
/* 0 */ INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV,
/* 1 */ INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV,
/* 2 */ INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV,  62, INV, INV, INV,  63,
/* 3 */  52,  53,  54,  55,  56,  57,  58,  59,  60,  61, INV, INV, INV, PAD, INV, INV,
/* 4 */ INV,   0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14,
/* 5 */  15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25, INV, INV, INV, INV, INV,
/* 6 */ INV,  26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,  37,  38,  39,  40,
/* 7 */  41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  51, INV, INV, INV, INV, INV,
/* 8 */ INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV,
/* 9 */ INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV,
/* A */ INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV,
/* B */ INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV,
/* C */ INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV,
/* D */ INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV,
/* E */ INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV,
/* F */ INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV,
];

/// A base64 value.
enum Base64Value {
    /// A valid base64 numeric value.
    Some(u8),
    /// The pad symbol.
    Pad,
    /// No value.
    None,
}

/// Returns the value of the next base64 character. Skips invalid
/// characters (rfc2045: All line breaks or other characters not
/// found in Table 1 must be ignored by decoding software).
fn next_valid_base64_value(iter: &mut dyn Iterator<Item=&u8>) -> Base64Value {
    while let Some(c) = iter.next() {
        let b = BASE64_INDICES[*c as usize];
        if b < PAD {
            return Base64Value::Some(b);
        }
        if b == PAD {
            return Base64Value::Pad;
        }
    }
    return Base64Value::None;
}

/// Decodes base64 encoded data, appending the decoded data to a Vec<u8>.
///
/// During decoding all line breaks and invalid characters are ignored.
/// Decoding is finished at the first pad character or end of input.  If an
/// error is encountered during decoding, the already decoded data in the output
/// buffer is left intact. It's up to the caller to deal with the partial
/// decoded data in case of failure
pub fn base64_decode_into_buf(input: &[u8], output: &mut Vec<u8>) -> Result<()> {
    let mut iter = input.iter();

    let expected_paddings =
        loop {
            let c0 = match next_valid_base64_value(&mut iter) {
                Base64Value::Some(c) => c,
                Base64Value::Pad => return Err("Invalid base64 padding".into()),
                Base64Value::None => return Ok(()),
            };

            let c1 = match next_valid_base64_value(&mut iter) {
                Base64Value::Some(c) => { output.push((c0 << 2) | ((c & 0x3f) >> 4)); c }
                Base64Value::Pad => return Err("Invalid base64 padding".into()),
                Base64Value::None => return Err("Invalid base64 encoding".into()),
            };

            let c2 = match next_valid_base64_value(&mut iter) {
                Base64Value::Some(c) => { output.push((c1 << 4) | ((c & 0x3f) >> 2)); c }
                Base64Value::Pad => break 1,
                Base64Value::None => return Err("Invalid base64 padding".into()),
            };

            match next_valid_base64_value(&mut iter) {
                Base64Value::Some(c) => { output.push((c2 << 6) | ((c & 0x3f))); }
                Base64Value::Pad => break 0,
                Base64Value::None => return Err("Invalid base64 padding".into()),
            };
        };

    let mut found_paddings = 0;

    while let Some(c) = iter.next() {
        if *c == b'=' {
            found_paddings += 1;
            continue;
        }
        let b = BASE64_INDICES[*c as usize];
        if b < PAD {
            return Err("Unexpected characters after base64 padding".into());
        }
    }

    if found_paddings != expected_paddings {
        return Err("Invalid base64 padding".into());
    }

    Ok(())
}

/// Converts an ascii byte representing a hex digit to it's numerical value.
fn hexdigit_to_num(mut a: u8) -> Option<u8> {
    if a.is_ascii_digit() {
        return Some(a - b'0');
    }

    a.make_ascii_lowercase();

    if a >= b'a' && a <= b'f' {
        return Some(a - b'a' + 10);
    }

    None
}

/// Decodes quoted-printable encoded data, appending the decoding data to a
/// Vec<u8>.
///
/// During decoding all line breaks and invalid characters are ignored.
/// If an error is encountered during decoding, the already decoded data in the
/// output buffer is left intact. It's up to the caller to deal with the partial
/// decoded data in case of failure.
pub fn qp_decode_into_buf(input: &[u8], output: &mut Vec<u8>) -> Result<()> {
    let mut iter = input.iter().peekable();

    'outer: loop {
        loop {
            match iter.next() {
                Some(b'=') => break,
                Some(c) => output.push(*c),
                None => break 'outer,
            }
        }

        // At this point we have encountered a '=', so check
        // to see what follows.
        if let Some(&first) = iter.next() {
            // A CRLF/LF after '=' marks a line continuation, and
            // is effectively dropped.
            if first == b'\r' {
                if iter.peek() == Some(&&b'\n') {
                    iter.next();
                    continue;
                }
            } else if first == b'\n' {
                continue;
            } else if let Some(first_num) = hexdigit_to_num(first) {
                // A valid pair of hexdigits represent the raw byte value.
                if let Some(&&second) = iter.peek() {
                    if let Some(second_num) = hexdigit_to_num(second) {
                        output.push(first_num * 16 +