0 | |
Description: Port to base64 v0.10
|
1 | |
Origin: vendor
|
2 | |
Forwarded: https://github.com/jcreekmore/pem-rs/pull/16
|
3 | |
Author: Nicolas Braud-Santoni <nicoo@debian.org>
|
4 | |
Last-Update: 2019-08-06
|
5 | |
Applied-Upstream: 2e476b7aa6c45f03b9515b527a90478c45ae47f0
|
6 | |
|
7 | |
--- a/Cargo.toml
|
8 | |
+++ b/Cargo.toml
|
9 | |
@@ -26,7 +26,7 @@
|
10 | |
name = "pem_benchmark"
|
11 | |
harness = false
|
12 | |
[dependencies.base64]
|
13 | |
-version = "0.9.0"
|
14 | |
+version = "^0.10"
|
15 | |
|
16 | |
[dependencies.failure]
|
17 | |
version = "0.1"
|
18 | |
--- a/src/lib.rs
|
19 | |
+++ b/src/lib.rs
|
20 | |
@@ -110,6 +110,7 @@
|
21 | |
|
22 | |
pub use errors::PemError;
|
23 | |
use regex::bytes::{Captures, Regex};
|
24 | |
+use std::str;
|
25 | |
|
26 | |
/// The `pem` result type.
|
27 | |
pub type Result<T> = ::std::result::Result<T, PemError>;
|
28 | |
@@ -117,6 +118,9 @@
|
29 | |
const REGEX_STR: &'static str =
|
30 | |
r"(?s)-----BEGIN (?P<begin>.*?)-----\s*(?P<data>.*?)-----END (?P<end>.*?)-----\s*";
|
31 | |
|
32 | |
+/// The line length for PEM encoding
|
33 | |
+const LINE_WRAP: usize = 64;
|
34 | |
+
|
35 | |
lazy_static! {
|
36 | |
static ref ASCII_ARMOR: Regex = Regex::new(REGEX_STR).unwrap();
|
37 | |
}
|
38 | |
@@ -133,7 +137,7 @@
|
39 | |
impl Pem {
|
40 | |
fn new_from_captures(caps: Captures) -> Result<Pem> {
|
41 | |
fn as_utf8<'a>(bytes: &'a [u8]) -> Result<&'a str> {
|
42 | |
- Ok(std::str::from_utf8(bytes).map_err(PemError::NotUtf8)?)
|
43 | |
+ Ok(str::from_utf8(bytes).map_err(PemError::NotUtf8)?)
|
44 | |
}
|
45 | |
|
46 | |
// Verify that the begin section exists
|
47 | |
@@ -158,12 +162,18 @@
|
48 | |
}
|
49 | |
|
50 | |
// If they did, then we can grab the data section
|
51 | |
- let data = as_utf8(caps.name("data")
|
52 | |
+ let raw_data = as_utf8(caps.name("data")
|
53 | |
.ok_or_else(|| PemError::MissingData)?
|
54 | |
.as_bytes())?;
|
55 | |
|
56 | |
+ // We need to get rid of newlines for base64::decode
|
57 | |
+ // As base64 requires an AsRef<[u8]>, this must involve a copy
|
58 | |
+ let data : String = raw_data
|
59 | |
+ .lines().map(str::trim_end)
|
60 | |
+ .collect();
|
61 | |
+
|
62 | |
// And decode it from Base64 into a vector of u8
|
63 | |
- let contents = base64::decode_config(&data, base64::MIME)
|
64 | |
+ let contents = base64::decode_config(&data, base64::STANDARD)
|
65 | |
.map_err(PemError::InvalidData)?;
|
66 | |
|
67 | |
Ok(Pem {
|
68 | |
@@ -321,13 +331,14 @@
|
69 | |
contents = base64::encode_config(&pem.contents, base64::Config::new(
|
70 | |
base64::CharacterSet::Standard,
|
71 | |
true,
|
72 | |
- true,
|
73 | |
- base64::LineWrap::Wrap(64, base64::LineEnding::CRLF)
|
74 | |
));
|
75 | |
}
|
76 | |
|
77 | |
output.push_str(&format!("-----BEGIN {}-----\r\n", pem.tag));
|
78 | |
- output.push_str(&format!("{}\r\n", contents));
|
79 | |
+ for c in contents.as_bytes().chunks(LINE_WRAP) {
|
80 | |
+ output.push_str(str::from_utf8(c).unwrap());
|
81 | |
+ output.push_str("\r\n");
|
82 | |
+ }
|
83 | |
output.push_str(&format!("-----END {}-----\r\n", pem.tag));
|
84 | |
|
85 | |
output
|