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
use rand::prelude::*;

pub struct SBXF<T> {
    handler: T,
}

impl<T: Handler> SBXF<T> {
    pub fn new(handler: T) -> Self {
        SBXF { handler }
    }

    pub fn upload_file(&self, data: Vec<u8>) -> Result<(), ()> {
        let block_size = self.calculate_blocksize(data.len());
        let mut blocks = data.len() / block_size;

        if data.len() % block_size != 0 {
            blocks += 1;
        };

        for i in 0..blocks {
            let block_start = i * block_size;
            let block_end = (block_start + block_size).min(data.len());

            let block = self.size_block(&data[block_start..block_end], block_size);
            let encrypted_block = self.encrypt_block(block);

            self.handler.save_block(encrypted_block)?;
        }

        let _ = self.handler.save_meta(data);
        Ok(())
    }

    fn calculate_blocksize(&self, size: usize) -> usize {
        (size.next_power_of_two() >> 3)
            .max(4 * 1024) // 4 KB minimum block size.
            .min(64 * 1024 * 1024) // 64 MB maximum block size.
    }

    fn size_block(&self, raw_block: &[u8], block_size: usize) -> Vec<u8> {
        let block = if raw_block.len() == block_size {
            raw_block.to_owned()
        } else {
            let mut rng = rand::thread_rng();
            let mut new_block = raw_block.to_owned();
            let padding_len = block_size - new_block.len();

            // Pad the block with random data, to counter cracking the encryption when there is not a lot of entropy in the block.
            for _ in 0..padding_len {
                new_block.push(rng.gen());
            }

            new_block
        };

        block
    }

    fn encrypt_block(&self, block: Vec<u8>) -> Vec<u8> {
        //TODO
        block
    }
}

pub trait Handler {
    fn save_meta(&self, data: Vec<u8>) -> Result<(), ()>;
    fn save_block(&self, data: Vec<u8>) -> Result<(), ()>;
}

#[cfg(test)]
mod tests {
    use super::*;

    struct DummyHandler;

    impl Handler for DummyHandler {
        fn save_meta(&self, _: Vec<u8>) -> Result<(), ()> {
            Ok(())
        }

        fn save_block(&self, _: Vec<u8>) -> Result<(), ()> {
            Ok(())
        }
    }

    #[test]
    fn upload_smoke() {
        let sbxf = SBXF::new(DummyHandler);

        sbxf.upload_file(vec![42, 42, 255])
            .expect("failed to upload");
    }

    #[test]
    fn calculate_blocksize() {
        let sbxf = SBXF::new(DummyHandler);

        assert_eq!(32 * 1024, sbxf.calculate_blocksize(200 * 1024));
        assert_eq!(32 * 1024, sbxf.calculate_blocksize(256 * 1024));
        assert_eq!(64 * 1024, sbxf.calculate_blocksize((256 * 1024) + 1));
    }

    #[test]
    fn calculate_small_blocksize() {
        let sbxf = SBXF::new(DummyHandler);

        assert_eq!(4096, sbxf.calculate_blocksize(0));
        assert_eq!(4096, sbxf.calculate_blocksize(1));
        assert_eq!(4096, sbxf.calculate_blocksize(4242));
        assert_eq!(4096, sbxf.calculate_blocksize(32 * 1024)); // 32 KB
        assert_eq!(8192, sbxf.calculate_blocksize((32 * 1024) + 1)); // 32 KB + 1 B
    }

    #[test]
    fn calculate_large_blocksize() {
        let sbxf = SBXF::new(DummyHandler);

        assert_eq!(
            32 * 1024 * 1024,
            sbxf.calculate_blocksize(256 * 1024 * 1024)
        ); // 0.25 GB
        assert_eq!(
            64 * 1024 * 1024,
            sbxf.calculate_blocksize(256 * 1024 * 1024 + 1)
        ); // 0.25 GB + 1 B
        assert_eq!(
            64 * 1024 * 1024,
            sbxf.calculate_blocksize(1024 * 1024 * 1024)
        ); // 1 GB
        assert_eq!(
            64 * 1024 * 1024,
            sbxf.calculate_blocksize(1024 * 1024 * 1024 * 1024)
        ); // 1 TB
    }

    #[test]
    fn resize_block() {
        let sbxf = SBXF::new(DummyHandler);
        let new_block = sbxf.size_block(&[42, 255, 0], 666);

        assert_eq!(666, new_block.len());
        assert_eq!(42, new_block[0]);
        assert_eq!(255, new_block[1]);
        assert_eq!(0, new_block[2]);
    }
}