Skip to content

Commit 0269588

Browse files
committed
Add fallback when single symbol
1 parent 115f61c commit 0269588

3 files changed

Lines changed: 18 additions & 4 deletions

File tree

‎Cargo.lock‎

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎rs/Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ version = "0.1.0"
99
crate-type = ["cdylib"]
1010

1111
[dependencies]
12-
compress = { git = "https://github.com/neri/compress.git" }
12+
compress = { git = "https://github.com/neri/compress.git", rev = "fbf52c474557f641418acdf17a3234500b517de6" }
1313
# compress = { path = "../../compress" }
1414

1515
serde = { version = "1.0", features = ["derive"] }

‎rs/src/lib.rs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
extern crate alloc;
44

55
use compress::{
6-
deflate::Deflate,
6+
deflate,
77
entropy::prefix::{CanonicalPrefixCoder, HuffmanTreeNode, PermutationFlavor},
88
num::bits::{BitSize, BitStreamWriter, VarBitValue},
99
};
@@ -75,6 +75,11 @@ pub fn _encode_chc(input: &[u8], max_len: u8) -> Result<String, EncodeError> {
7575

7676
let mut zip = BitStreamWriter::new();
7777
{
78+
let mut prefix_table = prefix_table.to_owned();
79+
if freq_table.len() == 1 {
80+
// fallback when single symbol
81+
prefix_table[256] = Some(VarBitValue::with_bool(true));
82+
}
7883
let zlib_meta = CanonicalPrefixCoder::encode_single_prefix_table(
7984
&prefix_table,
8085
PermutationFlavor::Deflate,
@@ -173,7 +178,7 @@ pub fn _encode_chc(input: &[u8], max_len: u8) -> Result<String, EncodeError> {
173178

174179
#[wasm_bindgen]
175180
pub fn decode_chc(input: &[u8], len: usize) -> Result<Vec<u8>, DecodeError> {
176-
Deflate::inflate(input, len).map_err(|_| DecodeError::InvalidData)
181+
deflate::inflate(input, len).map_err(|_| DecodeError::InvalidData)
177182
}
178183

179184
fn stringify_char(data: u8) -> String {

0 commit comments

Comments
 (0)