-
Notifications
You must be signed in to change notification settings - Fork 1
/
xor_store.rs
151 lines (129 loc) · 3.82 KB
/
xor_store.rs
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
/// A XOR based "encryption" resource transformer and resource name provider.
use crate::common::{
BoxedReader, ReaderTrait, ResourceId, ResourceNameProvider, ResourceStoreError,
ResourceTransformer,
};
use crate::file_store::FileStore;
use async_std::io::{Read, Seek, SeekFrom};
use async_std::task::{Context, Poll};
use base64::engine::general_purpose::{GeneralPurpose, GeneralPurposeConfig};
use base64::Engine;
use pin_project_lite::pin_project;
use std::pin::Pin;
struct XorNameProvider {
xor: u8,
base64: GeneralPurpose,
}
impl XorNameProvider {
pub fn new(xor: u8) -> Self {
Self {
xor,
base64: GeneralPurpose::new(&base64::alphabet::BCRYPT, GeneralPurposeConfig::new()),
}
}
/// Transform a string in a xored + base64 version, safely usable as a file name.
pub fn transform(&self, what: &str) -> String {
let xored: Vec<u8> = what.chars().map(|c| (c as u8) ^ self.xor).collect();
self.base64.encode(xored)
}
}
impl ResourceNameProvider for XorNameProvider {
fn metadata_name(&self, id: &ResourceId) -> String {
self.transform(&format!("{id}.meta"))
}
fn variant_name(&self, id: &ResourceId, variant: &str) -> String {
self.transform(&format!("{id}.{variant}.content"))
}
}
pub struct XorTransformer {
xor: u8,
}
impl XorTransformer {
pub fn new(xor: u8) -> Self {
Self { xor }
}
}
fn xor_buffer(xor: u8, buffer: &mut [u8], max: usize) {
let len = std::cmp::min(max, buffer.len());
for item in buffer.iter_mut().take(len) {
*item ^= xor;
}
}
impl ResourceTransformer for XorTransformer {
fn transform_to(&self, source: BoxedReader) -> BoxedReader {
Box::new(XorReader::new(self.xor, source))
}
fn transform_from(&self, source: BoxedReader) -> BoxedReader {
Box::new(XorReader::new(self.xor, source))
}
fn transform_array_to(&self, source: &[u8]) -> Vec<u8> {
source.iter().map(|e| e ^ self.xor).collect()
}
fn transform_array_from(&self, source: &[u8]) -> Vec<u8> {
source.iter().map(|e| e ^ self.xor).collect()
}
}
pin_project! {
/// Wrap a reader to perform XOR transformation.
struct XorReader<R> {
#[pin]
inner: R,
xor: u8,
}
}
impl<R: ReaderTrait> XorReader<R> {
fn new(xor: u8, inner: R) -> Self {
Self { xor, inner }
}
fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut R> {
self.project().inner
}
}
impl<R: ReaderTrait> Seek for XorReader<R> {
fn poll_seek(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
from: SeekFrom,
) -> Poll<Result<u64, std::io::Error>> {
self.as_mut().get_pin_mut().poll_seek(cx, from)
}
}
impl<R: ReaderTrait> Read for XorReader<R> {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<async_std::io::Result<usize>> {
let res = self.as_mut().get_pin_mut().poll_read(cx, buf);
match res {
Poll::Pending => Poll::Pending,
Poll::Ready(Ok(nread)) => {
xor_buffer(self.xor, buf, nread);
Poll::Ready(Ok(nread))
}
Poll::Ready(Err(err)) => Poll::Ready(Err(err)),
}
}
}
impl<R: ReaderTrait> ReaderTrait for XorReader<R> {}
pub async fn new_xor_store<P>(path: P, xor: u8) -> Result<FileStore, ResourceStoreError>
where
P: AsRef<async_std::path::Path>,
{
FileStore::new(
path,
Box::new(XorNameProvider::new(xor)),
Box::new(XorTransformer::new(xor)),
)
.await
}
#[test]
fn xor_buffer_roundtrip() {
let mut buf = [0u8; 2];
xor_buffer(32, &mut buf, 2);
assert_eq!(buf[0], 32);
assert_eq!(buf[0], 32);
xor_buffer(32, &mut buf, 2);
assert_eq!(buf[0], 0);
assert_eq!(buf[0], 0);
}