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
use std::io::{self, Read, ErrorKind};

const READ_SIZE: usize = 4096 * 3;

/// Read base64 data and decode them to plain data.
#[derive(Derivative)]
#[derivative(Debug)]
pub struct FromBase64Reader<R: Read> {
    #[derivative(Debug = "ignore")]
    inner: R,
    buf: Vec<u8>,
}

impl<R: Read> FromBase64Reader<R> {
    #[inline]
    pub fn new(inner: R) -> FromBase64Reader<R> {
        FromBase64Reader {
            inner,
            buf: Vec::new(),
        }
    }
}

impl<R: Read> Read for FromBase64Reader<R> {
    fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> {
        let buf_len = buf.len();

        if buf_len < 3 {
            return Err(io::Error::new(ErrorKind::Other, "the buffer needs to be equal to or more than 3 bytes"));
        }

        self.buf.clear();

        let actual_max_read_size = buf_len / 3 * 4;

        self.buf.reserve(actual_max_read_size);

        unsafe { self.buf.set_len(actual_max_read_size) };

        let c = {
            let mut buf = &mut self.buf[..actual_max_read_size];

            let mut c = 0;

            loop {
                match self.inner.read(buf) {
                    Ok(0) => break,
                    Ok(n) => {
                        let tmp = buf;
                        buf = &mut tmp[n..];
                        c += n;
                    }
                    Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
                    Err(e) => return Err(e),
                }
            }

            c
        };

        Ok(base64::decode_config_slice(&self.buf[..c], base64::STANDARD, buf).map_err(|err| io::Error::new(ErrorKind::Other, err.to_string()))?)
    }

    fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, io::Error> {
        self.buf.clear();

        let actual_max_read_size = READ_SIZE;

        self.buf.reserve(actual_max_read_size);

        unsafe { self.buf.set_len(actual_max_read_size) };

        let mut sum = 0;

        loop {
            let c = {
                let mut buf = &mut self.buf[..actual_max_read_size];

                let mut c = 0;

                loop {
                    match self.inner.read(buf) {
                        Ok(0) => break,
                        Ok(n) => {
                            let tmp = buf;
                            buf = &mut tmp[n..];
                            c += n;
                        }
                        Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
                        Err(e) => return Err(e),
                    }
                }

                c
            };

            if c == 0 {
                break;
            }

            let old_len = buf.len();

            base64::decode_config_buf(&self.buf[..c], base64::STANDARD, buf).map_err(|err| io::Error::new(ErrorKind::Other, err.to_string()))?;

            sum += buf.len() - old_len;
        }

        Ok(sum)
    }

    fn read_to_string(&mut self, buf: &mut String) -> Result<usize, io::Error> {
        self.buf.clear();

        let actual_max_read_size = READ_SIZE;

        self.buf.reserve(actual_max_read_size);

        unsafe { self.buf.set_len(actual_max_read_size) };

        let mut sum = 0;

        loop {
            let c = {
                let mut buf = &mut self.buf[..actual_max_read_size];

                let mut c = 0;

                loop {
                    match self.inner.read(buf) {
                        Ok(0) => break,
                        Ok(n) => {
                            let tmp = buf;
                            buf = &mut tmp[n..];
                            c += n;
                        }
                        Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
                        Err(e) => return Err(e),
                    }
                }

                c
            };

            if c == 0 {
                break;
            }

            let mut temp = Vec::new();

            base64::decode_config_buf(&self.buf[..c], base64::STANDARD, &mut temp).map_err(|err| io::Error::new(ErrorKind::Other, err.to_string()))?;

            let temp = String::from_utf8(temp).map_err(|_| io::Error::new(ErrorKind::InvalidData, "stream did not contain valid UTF-8"))?;

            sum += temp.len();

            buf.push_str(&temp);
        }

        Ok(sum)
    }
}