Skip to content

Commit

Permalink
add option for bare stream
Browse files Browse the repository at this point in the history
  • Loading branch information
ryancdotorg committed Nov 22, 2020
1 parent 25735a9 commit 2b03734
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/bin/brotli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,10 @@ fn main() {
params.byte_align = true;
continue;
}
if (argument == "-bare" || argument == "--bare") && !double_dash {
params.bare_stream = true;
continue;
}
if (argument == "-appendable" || argument == "--appendable") && !double_dash {
params.appendable = true;
continue;
Expand Down
2 changes: 2 additions & 0 deletions src/enc/backward_references/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ pub struct BrotliEncoderParams {
// insert empty metadata blocks before and after the compressed data
// this allows for concatonation by byte copying with catable/appendable
pub byte_align: bool,
// do not emit a stream header or empty last block at end of data
pub bare_stream: bool,
// construct brotli in such a way that it may be concatenated with another brotli file using appropriate bit ops
pub catable: bool,
// can use the dictionary (default yes unless catable is set)
Expand Down
29 changes: 23 additions & 6 deletions src/enc/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,13 @@ value: u32) -> i32 {
params.byte_align = value != 0;
return 1i32;
}
if p as (i32) == BrotliEncoderParameter::BROTLI_PARAM_BARE_STREAM as (i32) {
params.bare_stream = value != 0;
if !params.byte_align {
params.byte_align = value != 0;
}
return 1i32;
}
0i32
}

Expand Down Expand Up @@ -403,6 +410,7 @@ pub fn BrotliEncoderInitParams() -> BrotliEncoderParams {
prior_bitmask_detection: 0,
literal_adaptation: [(0,0);4],
byte_align: false,
bare_stream: false,
catable: false,
use_dictionary: true,
appendable: false,
Expand Down Expand Up @@ -627,7 +635,10 @@ pub fn SanitizeParams(params: &mut BrotliEncoderParams) {
}
}
if params.catable {
params.appendable = true;
params.appendable = true;
}
if params.bare_stream {
params.byte_align = true;
}
}

Expand Down Expand Up @@ -727,7 +738,9 @@ fn EnsureInitialized<Alloc: BrotliAlloc>
if (*s).params.quality == 0i32 || (*s).params.quality == 1i32 {
lgwin = brotli_max_int(lgwin, 18i32);
}
EncodeWindowBits(lgwin, s.params.large_window, &mut (*s).last_bytes_, &mut (*s).last_bytes_bits_);
if !(*s).params.bare_stream {
EncodeWindowBits(lgwin, s.params.large_window, &mut (*s).last_bytes_, &mut (*s).last_bytes_bits_);
}
}
if (*s).params.quality == 0i32 {
InitCommandPrefixCodes(&mut (*s).cmd_depths_[..],
Expand Down Expand Up @@ -2024,7 +2037,9 @@ fn WriteMetaBlockInternal<Alloc: BrotliAlloc,
if params.byte_align && ((*storage_ix & 7u32 as (usize)) != 0) {
BrotliStoreSyncMetaBlock(storage_ix, storage);
}
BrotliWriteEmptyLastMetaBlock(storage_ix, storage)
if !params.bare_stream {
BrotliWriteEmptyLastMetaBlock(storage_ix, storage)
}
}
return;
}
Expand Down Expand Up @@ -2168,7 +2183,9 @@ fn WriteMetaBlockInternal<Alloc: BrotliAlloc,
if params.byte_align && ((*storage_ix & 7u32 as (usize)) != 0) {
BrotliStoreSyncMetaBlock(storage_ix, storage);
}
BrotliWriteEmptyLastMetaBlock(storage_ix, storage)
if !params.bare_stream {
BrotliWriteEmptyLastMetaBlock(storage_ix, storage)
}
}
}

Expand Down Expand Up @@ -2255,7 +2272,7 @@ fn EncodeData<Alloc: BrotliAlloc,
}
let mut catable_header_size = 0;
if let IsFirst::NothingWritten = s.is_first_mb {
if s.params.magic_number || (s.params.byte_align && !s.params.catable && !s.params.appendable) {
if s.params.magic_number || (s.params.byte_align && !s.params.catable && !s.params.appendable && !s.params.bare_stream) {
if s.params.magic_number {
BrotliWriteMetadataMetaBlock(&s.params, &mut storage_ix, (*s).storage_.slice_mut());
} else {
Expand All @@ -2276,7 +2293,7 @@ fn EncodeData<Alloc: BrotliAlloc,
}
if let IsFirst::BothCatableBytesWritten = s.is_first_mb {
// nothing to do here, move along
} else if !s.params.catable {
} else if !s.params.catable || s.params.bare_stream {
s.is_first_mb = IsFirst::BothCatableBytesWritten;
} else if bytes != 0 {
assert!(s.last_processed_pos_ < 2 || s.custom_dictionary);
Expand Down
2 changes: 1 addition & 1 deletion src/enc/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub enum BrotliEncoderParameter {
BROTLI_PARAM_NO_DICTIONARY = 170,
BROTLI_PARAM_FAVOR_EFFICIENCY = 171,
BROTLI_PARAM_BYTE_ALIGN = 172,
BROTLI_PARAM_BARE_STREAM = 173,
UNUSED7=7,
UNUSED8=8,
UNUSED9=9,
Expand Down Expand Up @@ -174,7 +175,6 @@ pub enum BrotliEncoderParameter {
UNUSED147=147,
UNUSED148=148,
UNUSED149=149,
UNUSED173=173,
UNUSED174=174,
UNUSED175=175,
UNUSED176=176,
Expand Down

0 comments on commit 2b03734

Please sign in to comment.