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
use base::{Base, len, enc, dec};
use tool::{div_ceil, chunk, chunk_mut};
fn block<B: Base>(base: &B, input: &[u8], output: &mut [u8]) -> Result<(), Error> {
let mut x = 0u64;
for j in 0 .. input.len() {
let y = try!(base.val(input[j]).ok_or(BadCharacter(j)));
x |= (y as u64) << base.bit() * (dec(base) - 1 - j);
}
for j in 0 .. output.len() {
output[j] = (x >> 8 * (enc(base) - 1 - j)) as u8;
}
Ok(())
}
fn last_block<B: Base>(base: &B, input: &[u8], output: &mut [u8]) -> Result<usize, Error> {
let bit = base.bit();
let enc = enc(base);
let dec = dec(base);
let mut r = 0;
let mut x = 0u64;
for j in 0 .. dec {
if bit * j / 8 > r {
r += 1;
if input[j] == base.pad() {
for k in j .. dec {
check!(BadCharacter(k), input[k] == base.pad());
}
let s = bit * j - 8 * r;
let p = (x >> 8 * (enc - 1 - r)) as u8 >> 8 - s;
check!(BadPadding, p == 0);
break;
}
}
let y = try!(base.val(input[j]).ok_or(BadCharacter(j)));
x |= (y as u64) << bit * (dec - 1 - j);
if j == dec - 1 { r += 1; }
}
for j in 0 .. r {
output[j] = (x >> 8 * (enc - 1 - j)) as u8;
}
Ok(r)
}
pub fn decode_len<B: Base>(base: &B, len: usize) -> usize {
div_ceil(len, dec(base)) * enc(base)
}
pub fn decode_mut<B: Base>(base: &B, input: &[u8], output: &mut [u8]) -> Result<usize, Error> {
let enc = enc(base);
let dec = dec(base);
let ilen = input.len();
if ilen == 0 { return Ok(0); }
if ilen % dec != 0 { return Err(BadLength); }
assert_eq!(output.len(), decode_len(base, ilen));
let n = ilen / dec - 1;
for i in 0 .. n {
try!(block(base, chunk(input, dec, i), chunk_mut(output, enc, i))
.map_err(|e| e.shift(dec * i)));
}
last_block(base, chunk(input, dec, n), chunk_mut(output, enc, n))
.map_err(|e| e.shift(dec * n))
.map(|r| enc * n + r)
}
pub fn decode<B: Base>(base: &B, input: &[u8]) -> Result<Vec<u8>, Error> {
let mut output = vec![0u8; decode_len(base, input.len())];
let len = try!(decode_mut(base, input, &mut output));
output.truncate(len);
Ok(output)
}
#[derive(Copy,Clone,Debug,PartialEq,Eq)]
pub enum Error {
BadLength,
BadCharacter(usize),
BadPadding,
}
use self::Error::*;
impl Error {
pub fn shift(self, delta: usize) -> Error {
match self {
BadCharacter(pos) => BadCharacter(pos + delta),
other => other,
}
}
}
use std::fmt;
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&BadCharacter(p) => write!(f, "Unexpected character at {}", p),
&BadLength => write!(f, "Unexpected length"),
&BadPadding => write!(f, "Non-zero padding"),
}
}
}
use std::error;
impl error::Error for Error {
fn description(&self) -> &str {
match self {
&BadCharacter(_) => "unexpected character",
&BadLength => "unexpected length",
&BadPadding => "non-zero padding",
}
}
}