summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJustus Winter <justus@pep-project.org>2017-11-27 18:07:49 +0100
committerJustus Winter <justus@pep-project.org>2017-11-27 18:07:49 +0100
commit016d63fde13220a7c7255c27b3e55e42a4be1fac (patch)
tree68607f4fecf1ec00b21741620f56ed95cc0eb261 /src
parentca419d02e4f66826d1f4b513fcabe88ed01c08fe (diff)
Fix the size of the stash.
- Base64 encodes three bytes in four. We stash unencoded bytes, therefore we need to stash at most two bytes. Assert that in interesting places. - Reverse the items in the stash directly after populating it. This is is less surprising for the reader.
Diffstat (limited to 'src')
-rw-r--r--src/armor.rs12
1 files changed, 8 insertions, 4 deletions
diff --git a/src/armor.rs b/src/armor.rs
index 5711ba3a..349b1d99 100644
--- a/src/armor.rs
+++ b/src/armor.rs
@@ -97,7 +97,7 @@ impl<'a, W: Write> Writer<'a, W> {
Writer {
sink: inner,
kind: kind,
- stash: Vec::<u8>::with_capacity(3),
+ stash: Vec::<u8>::with_capacity(2),
column: 0,
crc: CRC::new(),
initialized: false,
@@ -177,6 +177,7 @@ impl<'a, W: Write> Write for Writer<'a, W> {
// First of all, if there are stashed bytes, fill the stash
// and encode it.
+ assert!(self.stash.len() < 3);
if self.stash.len() > 0 {
while self.stash.len() < 3 {
self.stash.push(input[0]);
@@ -200,6 +201,7 @@ impl<'a, W: Write> Write for Writer<'a, W> {
}
// We popped values from the end of the input, fix the order.
self.stash.reverse();
+ assert!(self.stash.len() < 3);
// We know that we have a multiple of 3 bytes, encode them and write them out.
assert!(input.len() % 3 == 0);
@@ -244,7 +246,7 @@ impl<'a, R: Read> Reader<'a, R> {
Reader {
source: inner,
kind: kind,
- stash: Vec::<u8>::with_capacity(3),
+ stash: Vec::<u8>::with_capacity(2),
crc: CRC::new(),
expect_crc: None,
initialized: false,
@@ -377,7 +379,7 @@ impl<'a, W: Read> Read for Reader<'a, W> {
let mut read = 0;
/* See if there are stashed bytes, and use them. */
- self.stash.reverse();
+ assert!(self.stash.len() < 3);
while self.stash.len() > 0 && buf.len() > read {
buf[read] = self.stash.pop().unwrap();
read += 1;
@@ -430,7 +432,7 @@ impl<'a, W: Read> Read for Reader<'a, W> {
/* We got more than we wanted, spill the surplus into our
* stash. */
let spill = decoded.len() - (buf.len() - read);
- assert!(spill <= 3);
+ assert!(spill < 3);
&mut buf[read..read + decoded.len() - spill].copy_from_slice(
&decoded[..decoded.len() - spill]);
@@ -438,6 +440,8 @@ impl<'a, W: Read> Read for Reader<'a, W> {
for c in &decoded[decoded.len() - spill..] {
self.stash.push(*c);
}
+ assert!(self.stash.len() < 3);
+ self.stash.reverse();
read += decoded.len() - spill;
}