Skip to content

Commit 3e46d4c

Browse files
committed
cleanup base32
1 parent ddf0490 commit 3e46d4c

File tree

1 file changed

+27
-64
lines changed

1 file changed

+27
-64
lines changed

lightning/src/util/base32.rs

+27-64
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
1-
/// Copied from https://crates.io/crates/base32 v0.4.0
2-
/// License: MIT or Apache-2.0
3-
use std::cmp::min;
1+
// Source: https://crates.io/crates/base32 v0.4.0
2+
// License: MIT or Apache-2.0
3+
// Copyright (c) 2015 The base32 Developers
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
11+
// The above copyright notice and this permission notice shall be included in all
12+
// copies or substantial portions of the Software.
13+
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
// SOFTWARE.
21+
// (reference https://github.com/andreasots/base32/blob/master/LICENSE-MIT)
422

523
#[derive(Copy, Clone)]
624
pub enum Alphabet {
@@ -65,21 +83,18 @@ const CROCKFORD_INV_ALPHABET: [i8; 43] = [
6583

6684
// Decode a base32 string into a byte vector.
6785
pub fn decode(alphabet: Alphabet, data: &str) -> Option<Vec<u8>> {
68-
if !data.is_ascii() {
69-
return None;
70-
}
7186
let data = data.as_bytes();
7287
let alphabet = match alphabet {
7388
Alphabet::RFC4648 { .. } => RFC4648_INV_ALPHABET,
7489
Alphabet::Crockford => CROCKFORD_INV_ALPHABET,
7590
};
7691
let mut unpadded_data_length = data.len();
77-
for i in 1..min(6, data.len()) + 1 {
78-
if data[data.len() - i] != b'=' {
79-
break;
80-
}
81-
unpadded_data_length -= 1;
82-
}
92+
data.iter().rev().take(6).for_each(|&c| {
93+
if c != b'=' {
94+
return;
95+
}
96+
unpadded_data_length -= 1;
97+
});
8398
let output_length = unpadded_data_length * 5 / 8;
8499
let mut ret = Vec::with_capacity((output_length + 4) / 5 * 5);
85100
for chunk in data.chunks(8) {
@@ -108,22 +123,13 @@ pub fn decode(alphabet: Alphabet, data: &str) -> Option<Vec<u8>> {
108123
mod test {
109124
use super::Alphabet::{Crockford, RFC4648};
110125
use super::{decode, encode};
111-
use quickcheck::{self, Arbitrary, Gen};
112126
use std::fmt::{Debug, Error, Formatter};
113127

114128
#[derive(Clone)]
115129
struct B32 {
116130
c: u8,
117131
}
118132

119-
impl Arbitrary for B32 {
120-
fn arbitrary(g: &mut Gen) -> B32 {
121-
B32 {
122-
c: *g.choose(b"0123456789ABCDEFGHJKMNPQRSTVWXYZ").unwrap(),
123-
}
124-
}
125-
}
126-
127133
impl Debug for B32 {
128134
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
129135
(self.c as char).fmt(f)
@@ -216,49 +222,6 @@ mod test {
216222
}
217223
}
218224

219-
#[test]
220-
fn invertible_crockford() {
221-
fn test(data: Vec<u8>) -> bool {
222-
decode(Crockford, encode(Crockford, data.as_ref()).as_ref()).unwrap() == data
223-
}
224-
quickcheck::quickcheck(test as fn(Vec<u8>) -> bool)
225-
}
226-
227-
#[test]
228-
fn invertible_rfc4648() {
229-
fn test(data: Vec<u8>) -> bool {
230-
decode(
231-
RFC4648 { padding: true },
232-
encode(RFC4648 { padding: true }, data.as_ref()).as_ref(),
233-
)
234-
.unwrap()
235-
== data
236-
}
237-
quickcheck::quickcheck(test as fn(Vec<u8>) -> bool)
238-
}
239-
#[test]
240-
fn invertible_unpadded_rfc4648() {
241-
fn test(data: Vec<u8>) -> bool {
242-
decode(
243-
RFC4648 { padding: false },
244-
encode(RFC4648 { padding: false }, data.as_ref()).as_ref(),
245-
)
246-
.unwrap()
247-
== data
248-
}
249-
quickcheck::quickcheck(test as fn(Vec<u8>) -> bool)
250-
}
251-
252-
#[test]
253-
fn lower_case() {
254-
fn test(data: Vec<B32>) -> bool {
255-
let data: String = data.iter().map(|e| e.c as char).collect();
256-
decode(Crockford, data.as_ref())
257-
== decode(Crockford, data.to_ascii_lowercase().as_ref())
258-
}
259-
quickcheck::quickcheck(test as fn(Vec<B32>) -> bool)
260-
}
261-
262225
#[test]
263226
#[allow(non_snake_case)]
264227
fn iIlL1_oO0() {

0 commit comments

Comments
 (0)