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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//! A base64 (with padding) encoding and decoding library, which implements the encode() and decode() methods for the `String` and `Vec<u8>` types.

use std::{error::Error, fmt, str};

const TABLE: &str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Base64Error {
    /// Invalid input data length (must be a multiple of 4)
    InvalidDataLenght,
    /// Incorrectly encoded input data
    InvalidBase64Data,
    /// Encoding error
    EncodingError,
}

impl Error for Base64Error {}

impl fmt::Display for Base64Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("Base64 error : ")?;
        f.write_str(match self {
            Base64Error::InvalidDataLenght => "Invalid input data length",
            Base64Error::InvalidBase64Data => "Invalid base64 data",
            Base64Error::EncodingError => "Cannot encode input data",
        })
    }
}

impl From<std::string::FromUtf8Error> for Base64Error {
    fn from(_e: std::string::FromUtf8Error) -> Base64Error {
        Base64Error::InvalidBase64Data
    }
}

impl From<std::str::Utf8Error> for Base64Error {
    fn from(_e: std::str::Utf8Error) -> Base64Error {
        Base64Error::EncodingError
    }
}

impl From<std::num::ParseIntError> for Base64Error {
    fn from(_e: std::num::ParseIntError) -> Base64Error {
        Base64Error::EncodingError
    }
}

impl From<Box<dyn Error>> for Base64Error {
    fn from(_e: Box<dyn Error>) -> Base64Error {
        Base64Error::EncodingError
    }
}

pub trait Base64 {
    fn encode(&self) -> Result<String, Base64Error>;
    fn decode(&self) -> Result<String, Base64Error>;
}

impl Base64 for String {
    /// Encodes a `String` with the base64 scheme
    ///
    /// Example:
    /// ```
    /// use lib_base64::Base64;
    /// let s = String::from("Test");
    /// assert_eq!(Ok(String::from("VGVzdA==")), s.encode())
    /// ```
    fn encode(&self) -> Result<String, Base64Error> {
        let a = self.as_bytes();

        let mut octal = String::new();
        let mut i = 0;

        // The number of full sextets to process
        let inputlenmod = a.len() % 3;
        let blockstoprocess = if inputlenmod == 0 {
            a.len()
        } else {
            a.len() - inputlenmod
        };
        let padding = if inputlenmod != 0 {
            3 - (a.len() - blockstoprocess)
        } else {
            0
        };

        // Creating octal output from bytes converted to sextets (3 * 8 bytes = 24 bits = four sextets)
        while i < blockstoprocess {
            octal.push_str(
                format!("{:o}", u32::from_be_bytes([0, a[i], a[i + 1], a[i + 2]])).as_str(),
            );
            i += 3;
        }

        match padding {
            1 => {
                octal
                    .push_str(format!("{:o}", u32::from_be_bytes([0, a[i], a[i + 1], 0])).as_str());
            }
            2 => {
                octal.push_str(format!("{:o}", u32::from_be_bytes([0, a[i], 0, 0])).as_str());
            }
            _ => {}
        };

        // Converting octal output to a decimal index vector
        let sextets = octal
            .as_bytes()
            .chunks(2)
            .map(str::from_utf8)
            .map(|u| {
                u.map_err::<Box<dyn Error>, _>(|e| e.into())
                    .and_then(|u| usize::from_str_radix(u, 8).map_err(|e| e.into()))
            })
            .collect::<Result<Vec<_>, _>>()?;

        let mut result = String::new();

        for i in 0..(sextets.len() - padding) {
            result.push_str(&TABLE[sextets[i]..(sextets[i] + 1)]);
        }
        match padding {
            1 => result.push('='),
            2 => result.push_str("=="),
            _ => {}
        };
        Ok(result)
    }

    /// Decodes a `String` encoded with the base64 scheme
    ///
    /// Example:
    /// ```
    /// use lib_base64::Base64;
    /// let s = String::from("VGVzdA==");
    /// assert_eq!(Ok(String::from("Test")), s.decode())
    /// ```
    fn decode(&self) -> Result<String, Base64Error> {
        let mut encoded_data = self.to_owned();
        let padding = encoded_data.matches('=').count();

        if encoded_data.len() % 4 != 0 {
            return Err(Base64Error::InvalidDataLenght);
        };

        // replaces padding characters by characters decoded as zero
        for _ in 0..padding {
            encoded_data.pop();
        }

        for _ in 0..padding {
            encoded_data.push('A');
        }

        // Retrieves octal indexes of encoded characters
        let octal = encoded_data
            .chars()
            .map(|c| format!("{:02o}", TABLE.find(c).unwrap_or(65))) // 65 for invalid base64 input detection
            .collect::<Vec<String>>();

        // Gathers the 4 sextets (24 bits) collection
        let mut octalsextets = Vec::new();
        let mut n = 0;
        while n < encoded_data.len() {
            let mut s = String::new();
            for i in 0..4 {
                if octal[n + i] == "101" {
                    return Err(Base64Error::InvalidBase64Data);
                } // 65 decimal = 101 octal
                s.push_str(octal[n + i].as_str());
            }
            n += 4;
            octalsextets.push(s);
        }

        // Decodes the 4 sextets to 3 bytes
        let decimal = octalsextets
            .iter()
            .map(|s| usize::from_str_radix(s, 8))
            .collect::<Result<Vec<_>, _>>()?;

        // Extracts the significants bytes of the usize to a vector of bytes
        let mut bytes: Vec<u8> = Vec::new();
        for i in 0..decimal.len() {
            let a = decimal[i].to_be_bytes();
            bytes.push(a[5]);
            bytes.push(a[6]);
            bytes.push(a[7]);
        }

        // Removes padding bytes inserted for decoding
        for _ in 0..padding {
            bytes.pop();
        }

        let result = String::from_utf8(bytes)?;
        Ok(result)
    }
}

impl Base64 for Vec<u8> {
    /// Encodes a `Vec<u8>` with the base64 scheme
    ///
    /// Example:
    /// ```
    /// use lib_base64::Base64;
    /// let v = vec![0x4d, 0x61, 0x6e];
    /// assert_eq!(Ok(String::from("TWFu")), v.encode())
    /// ```
    fn encode(&self) -> Result<String, Base64Error> {
        let table = TABLE.as_bytes();

        let mut input_buffer = Vec::new();
        let l = self.len();
        let mut i = 0;

        // The number of full sextets to process
        let inputlenmod = l % 3;
        let blockstoprocess = if inputlenmod == 0 { l } else { l - inputlenmod };

        let padding = if inputlenmod != 0 {
            3 - (l - blockstoprocess)
        } else {
            0
        };

        let mut base64_buffer: Vec<u8> = Vec::new();

        // Creating octal output from bytes converted to sextets (3 * 8 bytes = 24 bits = four sextets)
        // step 1 : put 3 bytes (24 bits) in a 32-bit word
        while i < blockstoprocess {
            input_buffer.push(u32::from_be_bytes([0, self[i], self[i + 1], self[i + 2]]));
            i += 3;
        }

        match padding {
            1 => {
                input_buffer.push(u32::from_be_bytes([0, self[i], self[i + 1], 0]));
            }
            2 => {
                input_buffer.push(u32::from_be_bytes([0, self[i], 0, 0]));
            }
            _ => {}
        };

        // step 2 : octal conversion (three bytes = four sextets)
        i = 0;
        while i < input_buffer.len() {
            let t0 = ((input_buffer[i] & 0xFC0000) >> 18) as u8;
            let t1 = ((input_buffer[i] & 0x3F000) >> 12) as u8;
            let t2 = ((input_buffer[i] & 0xFC0) >> 6) as u8;
            let t3 = (input_buffer[i] & 0x3F) as u8;
            base64_buffer.push(table[t0 as usize]);
            base64_buffer.push(table[t1 as usize]);
            base64_buffer.push(table[t2 as usize]);
            base64_buffer.push(table[t3 as usize]);
            i = i + 1;
        }

        let mut result = String::from_utf8(base64_buffer)?;
        match padding {
            1 => {
                result.pop();
                result.push('=');
            }
            2 => {
                result.pop();
                result.pop();
                result.push_str("==");
            }
            _ => {}
        };

        Ok(result)
    }

    /// Decodes a `Vec<u8>` encoded with the base64 scheme
    ///
    /// Example:
    /// ```
    /// use lib_base64::Base64;
    /// let s = vec![0x54, 0x57, 0x45, 0x3d];
    /// assert_eq!(Ok(String::from("Ma")), s.decode())
    /// ```
    fn decode(&self) -> Result<String, Base64Error> {
        let temp_string = self.to_owned();
        let mut encoded_data = String::from_utf8(temp_string)?;
        let padding = encoded_data.matches('=').count();

        if encoded_data.len() % 4 != 0 {
            return Err(Base64Error::InvalidDataLenght);
        };

        // replaces padding characters by characters decoded as zero
        for _ in 0..padding {
            encoded_data.pop();
        }

        for _ in 0..padding {
            encoded_data.push('A');
        }

        // Retrieves octal indexes of encoded characters
        let octal = encoded_data
            .chars()
            .map(|c| format!("{:02o}", TABLE.find(c).unwrap_or(65))) // 65 for invalid base64 input detection
            .collect::<Vec<String>>();

        // Gathers the 4 sextets (24 bits) collection
        let mut octalsextets = Vec::new();
        let mut n = 0;
        while n < encoded_data.len() {
            let mut s = String::new();
            for i in 0..4 {
                if octal[n + i] == "101" {
                    return Err(Base64Error::InvalidBase64Data);
                } // 65 decimal = 101 octal
                s.push_str(octal[n + i].as_str());
            }
            n += 4;
            octalsextets.push(s);
        }

        // Decodes the 4 sextets to 3 bytes
        let decimal = octalsextets
            .iter()
            .map(|s| usize::from_str_radix(s, 8))
            .collect::<Result<Vec<_>, _>>()?;

        // Extracts the significants bytes of the usize to a vector of bytes
        let mut bytes: Vec<u8> = Vec::new();
        for i in 0..decimal.len() {
            let a = decimal[i].to_be_bytes();
            bytes.push(a[5]);
            bytes.push(a[6]);
            bytes.push(a[7]);
        }

        // Removes padding bytes inserted for decoding
        for _ in 0..padding {
            bytes.pop();
        }

        let result = String::from_utf8(bytes)?;
        Ok(result)
    }
}

#[cfg(test)]
mod tests {
    use crate::Base64;
    #[test]
    fn encode_works() {
        assert_eq!(
            Ok(String::from("SmUgdCdhaW1lIG1hIGNow6lyaWU=")),
            String::from("Je t'aime ma chérie").encode()
        );
    }

    #[test]
    fn encode_no_padding() {
        assert_eq!(Ok(String::from("TWFu")), String::from("Man").encode());
    }

    #[test]
    fn encode_one_padding() {
        assert_eq!(Ok(String::from("TWE=")), String::from("Ma").encode());
    }

    #[test]
    fn encode_two_padding() {
        assert_eq!(Ok(String::from("TQ==")), String::from("M").encode());
    }

    #[test]
    fn decode_works() {
        assert_eq!(
            Ok(String::from("Joyeux anniversaire !")),
            String::from("Sm95ZXV4IGFubml2ZXJzYWlyZSAh").decode()
        );
    }

    #[test]
    fn datalength_check() {
        assert_eq!(
            Err(crate::Base64Error::InvalidDataLenght),
            String::from("TWF").decode()
        );
    }

    #[test]
    fn validb64data_check() {
        assert_eq!(
            Err(crate::Base64Error::InvalidBase64Data),
            String::from("TWF$").decode()
        );
    }

    #[test]
    fn encode_u8_no_padding() {
        let input: Vec<u8> = vec![0x4d, 0x61, 0x6e];
        assert_eq!(Ok(String::from("TWFu")), input.encode());
    }

    #[test]
    fn encode_u8_one_padding() {
        let input: Vec<u8> = vec![0x4d, 0x61];
        assert_eq!(Ok(String::from("TWE=")), input.encode());
    }

    #[test]
    fn encode_u8_two_padding() {
        let input: Vec<u8> = vec![0x4d];
        assert_eq!(Ok(String::from("TQ==")), input.encode());
    }

    #[test]
    fn encode_u8() {
        let input: Vec<u8> = String::from("light work.").as_bytes().to_vec();
        assert_eq!(Ok(String::from("bGlnaHQgd29yay4=")), input.encode());
    }

    #[test]
    fn decode_u8() {
        let input: Vec<u8> = vec![
            0x62, 0x47, 0x6C, 0x6E, 0x61, 0x48, 0x51, 0x67, 0x64, 0x32, 0x39, 0x79,
        ];
        assert_eq!(Ok(String::from("light wor")), input.decode());
    }

    #[test]
    fn decode_u8_one_padding() {
        let input: Vec<u8> = vec![
            0x62, 0x47, 0x6C, 0x6E, 0x61, 0x48, 0x51, 0x67, 0x64, 0x32, 0x39, 0x79, 0x61, 0x79,
            0x34, 0x3D,
        ];
        assert_eq!(Ok(String::from("light work.")), input.decode());
    }

    #[test]
    fn decode_u8_two_padding() {
        let input: Vec<u8> = vec![
            0x62, 0x47, 0x6C, 0x6E, 0x61, 0x48, 0x51, 0x67, 0x64, 0x32, 0x39, 0x79, 0x61, 0x77,
            0x3D, 0x3D,
        ];
        assert_eq!(Ok(String::from("light work")), input.decode());
    }
}