From a06be21df9a1b3667871663e3a970ed32c442319 Mon Sep 17 00:00:00 2001 From: "Sergey \"Shnatsel\" Davidoff" Date: Thu, 27 Jun 2019 00:46:55 +0200 Subject: [PATCH 1/6] Port the blocking API to rle_decode_helper; non-blocking still TODO --- Cargo.toml | 1 + src/deflate/decode.rs | 15 ++------------- src/lib.rs | 1 + 3 files changed, 4 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 44fcf32..d2afb54 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ codecov = {repository = "sile/libflate"} adler32 = "1" byteorder = "1" crc32fast = "1" +rle-decode-helper = {git = "https://github.com/Shnatsel/rle-decode-helper.git"} [dev-dependencies] clap = "2" diff --git a/src/deflate/decode.rs b/src/deflate/decode.rs index 862992e..300d5f2 100644 --- a/src/deflate/decode.rs +++ b/src/deflate/decode.rs @@ -3,6 +3,7 @@ use byteorder::ReadBytesExt; use std::cmp; use std::io; use std::io::Read; +use rle_decode_helper::rle_decode; use super::symbol; use bit; @@ -116,19 +117,7 @@ where distance )); } - let old_len = self.buffer.len(); - self.buffer.reserve(length as usize); - unsafe { - self.buffer.set_len(old_len + length as usize); - let start = old_len - distance as usize; - let ptr = self.buffer.as_mut_ptr(); - util::ptr_copy( - ptr.add(start), - ptr.add(old_len), - length as usize, - length > distance, - ); - } + rle_decode(&mut self.buffer, usize::from(distance), usize::from(length)); } symbol::Symbol::EndOfBlock => { break; diff --git a/src/lib.rs b/src/lib.rs index cb4ed7c..12876dd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ extern crate adler32; extern crate byteorder; extern crate crc32fast; +extern crate rle_decode_helper; pub use finish::Finish; From 8598b8124705ecb539ceb91e99f2965278cdaf93 Mon Sep 17 00:00:00 2001 From: "Sergey \"Shnatsel\" Davidoff" Date: Sun, 30 Jun 2019 15:40:47 +0200 Subject: [PATCH 2/6] Update to new crate name --- Cargo.toml | 2 +- src/deflate/decode.rs | 2 +- src/lib.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4df8719..d17beb0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ codecov = {repository = "sile/libflate"} adler32 = "1" byteorder = "1" crc32fast = "1" -rle-decode-helper = {git = "https://github.com/Shnatsel/rle-decode-helper.git"} +rle-decode-fast = {git = "https://github.com/Shnatsel/rle-decode-helper.git"} take_mut = "0.2.2" [dev-dependencies] diff --git a/src/deflate/decode.rs b/src/deflate/decode.rs index 300d5f2..ec7688a 100644 --- a/src/deflate/decode.rs +++ b/src/deflate/decode.rs @@ -3,7 +3,7 @@ use byteorder::ReadBytesExt; use std::cmp; use std::io; use std::io::Read; -use rle_decode_helper::rle_decode; +use rle_decode_fast::rle_decode; use super::symbol; use bit; diff --git a/src/lib.rs b/src/lib.rs index d37975c..a414e02 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,7 @@ extern crate adler32; extern crate byteorder; extern crate crc32fast; -extern crate rle_decode_helper; +extern crate rle_decode_fast; extern crate take_mut; pub use finish::Finish; From db26a84837c0d07dc4dc01a61be36b2ea0fa8649 Mon Sep 17 00:00:00 2001 From: "Sergey \"Shnatsel\" Davidoff" Date: Sun, 30 Jun 2019 16:39:33 +0200 Subject: [PATCH 3/6] Use released version of rle-decode-fast crate --- Cargo.toml | 2 +- src/deflate/decode.rs | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d17beb0..93d14bd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ codecov = {repository = "sile/libflate"} adler32 = "1" byteorder = "1" crc32fast = "1" -rle-decode-fast = {git = "https://github.com/Shnatsel/rle-decode-helper.git"} +rle-decode-fast = "1.0.0" take_mut = "0.2.2" [dev-dependencies] diff --git a/src/deflate/decode.rs b/src/deflate/decode.rs index ec7688a..3f0217c 100644 --- a/src/deflate/decode.rs +++ b/src/deflate/decode.rs @@ -8,7 +8,6 @@ use rle_decode_fast::rle_decode; use super::symbol; use bit; use lz77; -use util; /// DEFLATE decoder. #[derive(Debug)] From 0a802e46b557602358b90ffb61553da4410af6f0 Mon Sep 17 00:00:00 2001 From: "Sergey \"Shnatsel\" Davidoff" Date: Sun, 30 Jun 2019 16:45:13 +0200 Subject: [PATCH 4/6] Convert non-blocking decoder to fast and safe RLE decode implementation --- src/non_blocking/deflate/decode.rs | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/src/non_blocking/deflate/decode.rs b/src/non_blocking/deflate/decode.rs index 438b72a..f672911 100644 --- a/src/non_blocking/deflate/decode.rs +++ b/src/non_blocking/deflate/decode.rs @@ -3,12 +3,11 @@ use byteorder::ReadBytesExt; use std::cmp; use std::io; use std::io::Read; +use rle_decode_fast::rle_decode; use deflate::symbol::{self, HuffmanCodec}; use lz77; use non_blocking::transaction::TransactionalBitReader; -use util; - /// DEFLATE decoder which supports non-blocking I/O. #[derive(Debug)] pub struct Decoder { @@ -210,19 +209,7 @@ impl BlockDecoder { distance )); } - let old_len = self.buffer.len(); - self.buffer.reserve(length as usize); - unsafe { - self.buffer.set_len(old_len + length as usize); - let start = old_len - distance as usize; - let ptr = self.buffer.as_mut_ptr(); - util::ptr_copy( - ptr.add(start), - ptr.add(old_len), - length as usize, - length > distance, - ); - } + rle_decode(&mut self.buffer, usize::from(distance), usize::from(length)); } symbol::Symbol::EndOfBlock => { self.eob = true; From 62b520aaf9cbaf8012670ddec03e19b81e9fe2e3 Mon Sep 17 00:00:00 2001 From: "Sergey \"Shnatsel\" Davidoff" Date: Sun, 30 Jun 2019 16:46:37 +0200 Subject: [PATCH 5/6] Drop bespoke unsafe function that was used for RLE decoding, it's no longer needed --- src/util.rs | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/util.rs b/src/util.rs index 682bfa7..8fc6ccf 100644 --- a/src/util.rs +++ b/src/util.rs @@ -2,17 +2,6 @@ use std::io::{self, Read}; use std::ptr; -#[inline] -pub unsafe fn ptr_copy(src: *const u8, dst: *mut u8, count: usize, is_overlapping: bool) { - if !is_overlapping { - ptr::copy_nonoverlapping(src, dst, count); - } else { - for i in 0..count { - ptr::copy_nonoverlapping(src.add(i), dst.add(i), 1); - } - } -} - #[cfg(test)] pub struct WouldBlockReader { inner: R, From 912fabdc8a41d4878e30c43044cc79cbb71b0201 Mon Sep 17 00:00:00 2001 From: "Sergey \"Shnatsel\" Davidoff" Date: Sun, 30 Jun 2019 16:49:00 +0200 Subject: [PATCH 6/6] Drop unused import --- src/util.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/util.rs b/src/util.rs index 8fc6ccf..82f58d1 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,6 +1,5 @@ #[cfg(test)] use std::io::{self, Read}; -use std::ptr; #[cfg(test)] pub struct WouldBlockReader {