/
compress.rs
1382 lines (1242 loc) · 52.9 KB
/
compress.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::fmt;
use std::io::{self, Read, Seek, SeekFrom, Take, Write};
use bincode::Options;
use brotli::writer::StandardAlloc;
use brotli::BrotliState;
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use serde::{Deserialize, Serialize};
use crate::layers::traits::{
InnerWriterTrait, InnerWriterType, LayerFailSafeReader, LayerReader, LayerWriter,
};
use crate::{Error, BINCODE_MAX_DESERIALIZE};
use crate::config::{ArchiveWriterConfig, ConfigResult};
use crate::errors::ConfigError;
use super::traits::InnerReaderTrait;
// ---------- Config ----------
/// A bigger value means a better compression ratio, less indexes to save (in
/// memory), but also a slower random access. In the worst case, an access may
/// implies decompressing a whole block to obtain just the last byte.
///
/// According to benchmarking on compression of representative data, 4MB seems
/// to be a good choice
const UNCOMPRESSED_DATA_SIZE: u32 = 4 * 1024 * 1024;
/// A bigger value means a better compression ratio, but a slower compression
///
/// According to benchmarking on compression of representative data, level 5
/// seems to be a good choice
const DEFAULT_COMPRESSION_LEVEL: u32 = 5;
/// Default value which seems advised by brotli libraries
const BROTLI_LOG_WINDOW: u32 = 22;
pub struct CompressionConfig {
compression_level: u32,
}
impl std::default::Default for CompressionConfig {
fn default() -> Self {
CompressionConfig {
compression_level: DEFAULT_COMPRESSION_LEVEL,
}
}
}
impl ArchiveWriterConfig {
/// Set the compression level
/// compression level (0-11); bigger values cause denser, but slower compression
pub fn with_compression_level(&mut self, compression_level: u32) -> ConfigResult {
if compression_level > 11 {
Err(ConfigError::CompressionLevelOutOfRange)
} else {
self.compress.compression_level = compression_level;
Ok(self)
}
}
}
// ---------- Reader ----------
/// See `CompressionLayerWriter` for more information
enum CompressionLayerReaderState<R: Read> {
/// Ready contains the real inner destination
Ready(R),
/// How many uncompressed bytes have already been read for the current
/// block
InData {
read: u32,
uncompressed_size: u32,
/// Use a Box to avoid a too big enum
/// Use a `Take` to instanciate the `Decompressor` only on the current block's compressed bytes
decompressor: Box<brotli::Decompressor<Take<R>>>,
},
/// Empty is a placeholder to allow state replacement
Empty,
}
impl<R: Read> fmt::Debug for CompressionLayerReaderState<R> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CompressionLayerReaderState::Ready(_inner) => write!(f, "Ready"),
CompressionLayerReaderState::InData { .. } => write!(f, "InData"),
CompressionLayerReaderState::Empty => write!(f, "Empty"),
}
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct SizesInfo {
/// Ordered list of chunk compressed size; only set at init
pub compressed_sizes: Vec<u32>,
/// Last block uncompressed size
last_block_size: u32,
}
impl SizesInfo {
/// Get the uncompressed block size of block `block_num`
fn uncompressed_block_size_at(&self, block_num: usize) -> u32 {
if block_num < self.compressed_sizes.len() - 1 {
UNCOMPRESSED_DATA_SIZE
} else {
self.last_block_size
}
}
/// Get the compressed block at position `uncompressed_pos`
fn compressed_block_size_at(&self, uncompressed_pos: u64) -> u32 {
let block_num = uncompressed_pos / (UNCOMPRESSED_DATA_SIZE as u64);
self.compressed_sizes[block_num as usize]
}
/// Maximum uncompressed available position
fn max_uncompressed_pos(&self) -> u64 {
(self.compressed_sizes.len() as u64 - 1) * UNCOMPRESSED_DATA_SIZE as u64
+ self.last_block_size as u64
}
// Sum the compressed_sizes
pub fn get_compressed_size(&self) -> u64 {
self.compressed_sizes.iter().map(|v| *v as u64).sum()
}
}
pub struct CompressionLayerReader<'a, R: 'a + Read> {
state: CompressionLayerReaderState<Box<dyn 'a + LayerReader<'a, R>>>,
pub sizes_info: Option<SizesInfo>,
/// Position in the under-layer (uncompressed stream)
// /!\ Due to the decompressor having a block size of the compressed size,
// any read on it may forward the inner layer to the beginning of the next
// block
//
// [compressed block][compressed block]
// ^ ^
// | The inner layer is here
// We're actually here
//
// Additionnaly, the `brotli` implementation may consume more or less bytes
// than presumed. For instance, the compression may dump n bytes, while the
// decompressor is able to recover the decompressed part with only n -
// epsilon bytes.
//
// As a result, `underlayer_pos` and `inner` position
// corrected with `sizes_info` may seems unsync; `underlayer_pos` is the one
// to trust.
underlayer_pos: u64,
}
impl<R: Read> CompressionLayerReaderState<R> {
fn into_inner(self) -> R {
match self {
CompressionLayerReaderState::Ready(inner) => inner,
CompressionLayerReaderState::InData { decompressor, .. } => {
decompressor.into_inner().into_inner()
}
// `panic!` explicitly called to avoid propagating an error which
// must never happens (ie, calling `into_inner` in an inconsistent
// internal state)
_ => panic!("[Reader] Empty type to inner is impossible"),
}
}
}
impl<'a, R: 'a + Read> CompressionLayerReader<'a, R> {
pub fn new(mut inner: Box<dyn 'a + LayerReader<'a, R>>) -> Result<Self, Error> {
let underlayer_pos = inner.stream_position()?;
Ok(Self {
state: CompressionLayerReaderState::Ready(inner),
sizes_info: None,
underlayer_pos,
})
}
/// Returns whether `uncompressed_pos` is in the data stream
/// If no index is used, always return `true`
fn pos_in_stream(&self, uncompressed_pos: u64) -> bool {
match &self.sizes_info {
Some(sizes_info) => {
let pos_max = sizes_info.max_uncompressed_pos();
uncompressed_pos < pos_max
}
None => true,
}
}
/// Instantiate a new decompressor at position `uncompressed_pos`
/// `uncompressed_pos` must be a compressed block's starting position
fn new_decompressor_at<S: Read + Seek>(
&self,
inner: S,
uncompressed_pos: u64,
) -> Result<brotli::Decompressor<Take<S>>, Error> {
// Ensure it's a starting position
if uncompressed_pos % (UNCOMPRESSED_DATA_SIZE as u64) != 0 {
return Err(Error::BadAPIArgument(
"[new_decompressor_at] not a starting position".to_string(),
));
}
// Check we are still in the stream
if !self.pos_in_stream(uncompressed_pos) {
// No more in the compressed stream -> nothing to read
return Err(Error::EndOfStream);
}
match &self.sizes_info {
Some(sizes_info) => {
// Use index for faster decompression
let compressed_block_size =
sizes_info.compressed_block_size_at(uncompressed_pos) as usize;
Ok(brotli::Decompressor::new(
// Make the Decompressor work only on the compressed block's bytes, no more
inner.take(compressed_block_size as u64),
compressed_block_size,
))
}
None => Err(Error::MissingMetadata),
}
}
// TODO add regression test
/// Get the uncompressed block size at position `uncompressed_pos`
/// `uncompressed_pos` must be a compressed block's starting position
fn uncompressed_block_size_at(&self, uncompressed_pos: u64) -> Result<u32, Error> {
// Ensure it's a starting position
if uncompressed_pos % (UNCOMPRESSED_DATA_SIZE as u64) != 0 {
return Err(Error::BadAPIArgument(
"[uncompressed_block_size_at] not a starting position".to_string(),
));
}
// Check we are still in the stream
if !self.pos_in_stream(uncompressed_pos) {
// No more in the compressed stream -> nothing to read
return Err(Error::EndOfStream);
}
match &self.sizes_info {
Some(sizes_info) => {
// Use index for faster decompression
// Get the uncompressed block size
let block_num = uncompressed_pos / (UNCOMPRESSED_DATA_SIZE as u64);
Ok(sizes_info.uncompressed_block_size_at(block_num as usize))
}
None => Err(Error::MissingMetadata),
}
}
// TODO add regression test
/// Resynchronize the inner layer with `uncompressed_pos` (ie., seek inner with expected position)
/// `uncompressed_pos` must be a compressed block's starting position
fn sync_inner_with_uncompressed_pos<S: Read + Seek>(
&self,
inner: &mut S,
uncompressed_pos: u64,
) -> Result<(), Error> {
// Ensure it's a starting position
if uncompressed_pos % (UNCOMPRESSED_DATA_SIZE as u64) != 0 {
return Err(Error::BadAPIArgument(
"[sync_inner_with_uncompressed_pos] not a starting position".to_string(),
));
}
// Check we are still in the stream
if !self.pos_in_stream(uncompressed_pos) {
// No more in the compressed stream -> nothing to read
return Err(Error::EndOfStream);
}
// Find the right block
let block_num = uncompressed_pos / (UNCOMPRESSED_DATA_SIZE as u64);
match &self.sizes_info {
Some(SizesInfo {
compressed_sizes, ..
}) => {
// Move the underlayer at the start of the block
let start_position = compressed_sizes
.iter()
.take(block_num as usize)
.map(|size| *size as u64)
.sum();
inner.seek(SeekFrom::Start(start_position))?;
}
None => {
return Err(Error::MissingMetadata);
}
}
Ok(())
}
}
impl<'a, R: 'a + InnerReaderTrait> LayerReader<'a, R> for CompressionLayerReader<'a, R> {
fn into_inner(self) -> Option<Box<dyn 'a + LayerReader<'a, R>>> {
Some(self.state.into_inner())
}
fn into_raw(self: Box<Self>) -> R {
self.state.into_inner().into_raw()
}
fn initialize(&mut self) -> Result<(), Error> {
match &mut self.state {
CompressionLayerReaderState::Ready(inner) => {
// Recursive call
inner.initialize()?;
// Read the footer: [SizesInfo][SizesInfo length, on 4 bytes]
let pos = inner.seek(SeekFrom::End(-4))?;
let len = inner.read_u32::<LittleEndian>()? as u64;
// Read SizesInfo
inner.seek(SeekFrom::Start(pos - len))?;
self.sizes_info = match bincode::options()
.with_limit(BINCODE_MAX_DESERIALIZE)
.with_fixint_encoding()
.deserialize_from(inner.take(len))
{
Ok(sinfo) => Some(sinfo),
_ => {
return Err(Error::DeserializationError);
}
};
Ok(())
}
_ => {
// At init, should not be in this state
Err(Error::WrongReaderState(
"[Compression Layer]: on initialization, must be in Ready state".to_string(),
))
}
}
}
}
impl<'a, R: 'a + Read + Seek> Read for CompressionLayerReader<'a, R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
if !self.pos_in_stream(self.underlayer_pos) {
// No more in the compressed stream -> nothing to read
return Ok(0);
}
// Use this mem::replace trick to be able to get back the compressor
// inner and freely move from CompressionLayerReaderState to others
let old_state = std::mem::replace(&mut self.state, CompressionLayerReaderState::Empty);
match old_state {
CompressionLayerReaderState::Ready(mut inner) => {
self.sync_inner_with_uncompressed_pos(&mut inner, self.underlayer_pos)?;
let decompressor = Box::new(self.new_decompressor_at(inner, self.underlayer_pos)?);
let uncompressed_size = self.uncompressed_block_size_at(self.underlayer_pos)?;
self.state = CompressionLayerReaderState::InData {
read: 0,
uncompressed_size,
decompressor,
};
self.read(buf)
}
CompressionLayerReaderState::InData {
read,
uncompressed_size,
mut decompressor,
} => {
if read > uncompressed_size {
return Err(Error::WrongReaderState(
"[Compression Layer] Too much data read".to_string(),
)
.into());
}
if read == uncompressed_size {
self.state =
CompressionLayerReaderState::Ready(decompressor.into_inner().into_inner());
// Start a new block, fill it with new values!
return self.read(buf);
}
let size = std::cmp::min((uncompressed_size - read) as usize, buf.len());
let read_add = decompressor.read(&mut buf[..size])?;
self.underlayer_pos += read_add as u64;
self.state = CompressionLayerReaderState::InData {
read: read + read_add as u32,
uncompressed_size,
decompressor,
};
Ok(read_add)
}
CompressionLayerReaderState::Empty => Err(Error::WrongReaderState(
"[Compression Layer] Should never happens, unless an error already occurs before"
.to_string(),
)
.into()),
}
}
}
impl<'a, R: Read + Seek> Seek for CompressionLayerReader<'a, R> {
/// Seek to the position `pos` in the uncompressed stream
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
// Seeking may instantiate a decompressor, and therefore position the
// inner layer at the end of the asked position's compressed block
match &self.sizes_info {
Some(_sizes_info) => {
match pos {
SeekFrom::Start(pos) => {
// Find the right block
let inside_block = pos % (UNCOMPRESSED_DATA_SIZE as u64);
let rounded_pos = pos - inside_block;
// Move the underlayer at the start of the block
let old_state =
std::mem::replace(&mut self.state, CompressionLayerReaderState::Empty);
let mut inner = old_state.into_inner();
self.sync_inner_with_uncompressed_pos(&mut inner, rounded_pos)?;
// New decompressor at the start of the block
let mut decompressor = self.new_decompressor_at(inner, rounded_pos)?;
let uncompressed_size = self.uncompressed_block_size_at(rounded_pos)?;
// Move forward inside the block to reach the expected position
io::copy(&mut (&mut decompressor).take(inside_block), &mut io::sink())?;
self.state = CompressionLayerReaderState::InData {
read: inside_block as u32,
uncompressed_size,
decompressor: Box::new(decompressor),
};
self.underlayer_pos = pos;
Ok(pos)
}
SeekFrom::Current(pos) => {
// Get the position and do nothing
if pos == 0 {
Ok(self.underlayer_pos)
} else {
self.seek(SeekFrom::Start((pos + self.underlayer_pos as i64) as u64))
}
// TODO: Possible optimization:
// - if pos is positive and inside the current block,
// just advance the decompressor
}
SeekFrom::End(pos) => {
if pos > 0 {
// Seeking past the end is unsupported
return Err(Error::EndOfStream.into());
}
let end_pos = self.sizes_info.as_ref().unwrap().max_uncompressed_pos();
let distance_from_end = -pos;
self.seek(SeekFrom::Start(end_pos - distance_from_end as u64))
}
}
}
None => Err(Error::MissingMetadata.into()),
}
}
}
// ---------- Writer ----------
/// Wrap a Writer with counting of written bytes
struct WriterWithCount<W: Write> {
inner: W,
pos: u32,
}
impl<W: Write> WriterWithCount<W> {
fn new(inner: W) -> Self {
Self { inner, pos: 0 }
}
fn into_inner(self) -> W {
self.inner
}
}
impl<W: Write> Write for WriterWithCount<W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.inner.write(buf).map(|i| {
self.pos += i as u32;
i
})
}
fn flush(&mut self) -> io::Result<()> {
self.inner.flush()
}
}
enum CompressionLayerWriterState<W: Write> {
/// Ready contains the real inner destination
Ready(W),
/// How many uncompressed bytes have already been written for the current
/// block
// Use a Box to avoid a too big enum
InData(u32, Box<brotli::CompressorWriter<WriterWithCount<W>>>),
/// Empty is a placeholder to allow state replacement
Empty,
}
/// Compression layer is made of independent CompressedBlock, ending by an index for seekable accesses
/// [CompressedBlock][CompressedBlock]...[CompressedBlock][Index]
///
/// Compression is made of nested independent compressed block of a fixed
/// uncompressed size
/// Pro:
/// * no need to store the compressed size
/// * compression can be streamed (storing the compressed size before the
/// compressed block leads to either seekable stream, which is not an option
/// here, or full-memory compression before actual write, which add limits to
/// the memory footprint)
/// Cons:
/// * if the index is lost, a slow decompression with a block size of 1 is
/// needed to found the CompressedBlock boundaries
pub struct CompressionLayerWriter<'a, W: 'a + InnerWriterTrait> {
state: CompressionLayerWriterState<InnerWriterType<'a, W>>,
// Ordered list of compressed size of block of `UNCOMPRESSED_DATA_SIZE`
// bytes
//
// Thus, accessing the `n`th byte in the sublayer, is accessing the `n %
// C`th uncompressed byte in the chunk beginning at `sum(compressed_sizes[:n
// / C])`, with `C = UNCOMPRESSED_DATA_SIZE`
compressed_sizes: Vec<u32>,
// From config
compression_level: u32,
}
impl<W: InnerWriterTrait> CompressionLayerWriterState<W> {
fn into_inner(self) -> W {
match self {
CompressionLayerWriterState::Ready(inner) => inner,
CompressionLayerWriterState::InData(_written, compress) => {
compress.into_inner().into_inner()
}
// `panic!` explicitly called to avoid propagating an error which
// must never happens (ie, calling `into_inner` in an inconsistent
// internal state)
_ => panic!("[Writer] Empty type to inner is impossible"),
}
}
}
impl<'a, W: 'a + InnerWriterTrait> CompressionLayerWriter<'a, W> {
pub fn new(
inner: InnerWriterType<'a, W>,
config: &CompressionConfig,
) -> CompressionLayerWriter<'a, W> {
Self {
state: CompressionLayerWriterState::Ready(inner),
compressed_sizes: Vec::new(),
compression_level: config.compression_level,
}
}
}
impl<'a, W: 'a + InnerWriterTrait> LayerWriter<'a, W> for CompressionLayerWriter<'a, W> {
fn into_inner(self) -> Option<InnerWriterType<'a, W>> {
Some(self.state.into_inner())
}
fn into_raw(self: Box<Self>) -> W {
self.state.into_inner().into_raw()
}
fn finalize(&mut self) -> Result<(), Error> {
// Use this mem::replace trick to be able to get back the compressor
// inner and freely move from CompressionLayerWriterState to others
let old_state = std::mem::replace(&mut self.state, CompressionLayerWriterState::Empty);
let mut last_block_size = 0;
let mut inner = match old_state {
CompressionLayerWriterState::Ready(inner) => inner,
CompressionLayerWriterState::InData(written, compress) => {
let inner_count = compress.into_inner();
self.compressed_sizes.push(inner_count.pos);
last_block_size = written;
inner_count.into_inner()
}
CompressionLayerWriterState::Empty => {
// Should never happens, except if an error already occurs before
return Err(Error::WrongReaderState("[Compression Layer] bad state in finalization, an error may already occurs before".to_string()));
}
};
// Footer:
// [SizesInfo][SizesInfo length]
// `std::mem::replace` used to perform zero-copy serialization of `self.compressed_sizes`
// The values is restored just after the operation (non-thread safe, but
// in a multi-thread env, we will already required a lock for the
// writing)
let compressed_sizes = std::mem::take(&mut self.compressed_sizes);
let sinfo = SizesInfo {
compressed_sizes,
last_block_size,
};
if bincode::options()
.with_limit(BINCODE_MAX_DESERIALIZE)
.with_fixint_encoding()
.serialize_into(&mut inner, &sinfo)
.is_err()
{
return Err(Error::SerializationError);
};
match bincode::serialized_size(&sinfo) {
Ok(size) => {
inner.write_u32::<LittleEndian>(size as u32)?;
}
Err(_) => {
return Err(Error::SerializationError);
}
};
self.compressed_sizes = sinfo.compressed_sizes;
// Recursive call
inner.finalize()?;
// Store inner, for further into_inner / into_raw calls
self.state = CompressionLayerWriterState::Ready(inner);
Ok(())
}
}
impl<'a, W: 'a + InnerWriterTrait> Write for CompressionLayerWriter<'a, W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
// Use this mem::replace trick to be able to get back the compressor
// inner and freely move from CompressionLayerWriterState to others
let old_state = std::mem::replace(&mut self.state, CompressionLayerWriterState::Empty);
match old_state {
CompressionLayerWriterState::Ready(inner) => {
let inner_count = WriterWithCount::new(inner);
let mut compress = brotli::CompressorWriter::new(
inner_count,
0,
self.compression_level,
BROTLI_LOG_WINDOW,
);
let size = std::cmp::min(UNCOMPRESSED_DATA_SIZE as usize, buf.len());
let written = compress.write(&buf[..size])?;
self.state = CompressionLayerWriterState::InData(written as u32, Box::new(compress));
Ok(written)
}
CompressionLayerWriterState::InData(written, mut compress) => {
if written > UNCOMPRESSED_DATA_SIZE {
return Err(Error::WrongReaderState(
"[Compression Layer] Too much written".to_string(),
).into());
}
if written == UNCOMPRESSED_DATA_SIZE {
let inner_count = compress.into_inner();
self.compressed_sizes.push(inner_count.pos);
self.state = CompressionLayerWriterState::Ready(inner_count.into_inner());
// Start a new block, fill it with new values!
return self.write(buf);
}
let size = std::cmp::min((UNCOMPRESSED_DATA_SIZE - written) as usize, buf.len());
let written_add = compress.write(&buf[..size])?;
self.state =
CompressionLayerWriterState::InData(written + written_add as u32, compress);
Ok(written_add)
}
CompressionLayerWriterState::Empty => {
Err(Error::WrongReaderState("[Compression Layer] On write, should never happens, unless an error already occurs before".to_string()).into())
}
}
}
fn flush(&mut self) -> io::Result<()> {
match &mut self.state {
CompressionLayerWriterState::Ready(inner) => inner.flush(),
CompressionLayerWriterState::InData(_written, compress) => compress.flush(),
CompressionLayerWriterState::Empty => {
// Should never happens, except if an error already occurs before
Err(Error::WrongReaderState("[Compression Layer] On flush, should never happens, unless an error already occurs before".to_string()).into())
}
}
}
}
// ---------- Fail-Safe Reader ----------
/// Internal state for the `CompressionLayerFailSafeReader`
enum CompressionLayerFailSafeReaderState<R: Read> {
/// Ready contains the real inner destination
/// Only used for the initialization
Ready(R),
/// Inside a decompression stream
InData {
/// While decompressing, one doesn't know in advance the number of compressed bytes
/// As a result, the following is done:
/// 1. read from the source inside a buffer
/// 2. decompress the data from the buffer
/// - if there is still data to decompress, go to 1.
/// - if this is the end of the stream, continue to 3.
/// 3. the decompressor may have read too many byte, ie. `[end of stream n-1][start of stream n]`
/// ^ ^
/// input_offset last read position
/// 4. rewind, using the cache, to `input_offset`
///
/// A cache must be used, as the source is `Read` but not `Seek`.
/// `input_offset` is guaranted to be in the cache because it must be in the decompressor working buffer,
/// and the working buffer is contained in the cache (in the worst case, it is the whole cache)
///
/// Cache management:
/// ```ascii
/// cache_filled_offset
/// v
/// cache: [................ ]
/// ^
/// read_offset
/// ```
/// Data read from the source, not yet used
/// Invariant:
/// - cache.len() == FAIL_SAFE_BUFFER_SIZE (cache always allocated)
cache: Vec<u8>,
/// Bytes valid in the cache : [0..`cache_filled_offset`[ (0 -> no valid data)
cache_filled_offset: usize,
/// Next offset to read from the cache
/// Invariant:
/// - `read_offset` <= `cache_filled_offset`
read_offset: usize,
/// Internal decompressor state
state: Box<BrotliState<StandardAlloc, StandardAlloc, StandardAlloc>>,
/// Number of bytes decompressed and returned for the current stream
uncompressed_read: u32,
/// Inner layer (data source)
inner: R,
},
/// Empty is a placeholder to allow state replacement
Empty,
}
impl<R: Read> CompressionLayerFailSafeReaderState<R> {
fn into_inner(self) -> R {
match self {
CompressionLayerFailSafeReaderState::Ready(inner) => inner,
CompressionLayerFailSafeReaderState::InData { inner, .. } => inner,
// `panic!` explicitly called to avoid propagating an error which
// must never happens (ie, calling `into_inner` in an inconsistent
// internal state)
_ => panic!("[Reader] Empty type to inner is impossible"),
}
}
}
pub struct CompressionLayerFailSafeReader<'a, R: 'a + Read> {
state: CompressionLayerFailSafeReaderState<Box<dyn 'a + LayerFailSafeReader<'a, R>>>,
}
impl<'a, R: 'a + Read> CompressionLayerFailSafeReader<'a, R> {
pub fn new(inner: Box<dyn 'a + LayerFailSafeReader<'a, R>>) -> Result<Self, Error> {
Ok(Self {
state: CompressionLayerFailSafeReaderState::Ready(inner),
})
}
}
impl<'a, R: 'a + Read> LayerFailSafeReader<'a, R> for CompressionLayerFailSafeReader<'a, R> {
fn into_inner(self) -> Option<Box<dyn 'a + LayerFailSafeReader<'a, R>>> {
Some(self.state.into_inner())
}
fn into_raw(self: Box<Self>) -> R {
self.state.into_inner().into_raw()
}
}
const FAIL_SAFE_BUFFER_SIZE: usize = 4096;
impl<'a, R: 'a + Read> Read for CompressionLayerFailSafeReader<'a, R> {
/// This `read` is expected to end by failing
///
/// Even in the best configuration, when the inner layer is not broken, the
/// decompression will fail while reading not-compressed data such as
/// CompressionLayerReader footer
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
// Use this mem::replace trick to be able to get back the compressor
// inner and freely move from CompressionLayerReaderState to others
let old_state =
std::mem::replace(&mut self.state, CompressionLayerFailSafeReaderState::Empty);
match old_state {
CompressionLayerFailSafeReaderState::Ready(inner) => {
self.state = CompressionLayerFailSafeReaderState::InData {
cache: vec![0u8; FAIL_SAFE_BUFFER_SIZE],
read_offset: 0,
cache_filled_offset: 0,
state: Box::new(BrotliState::new(
StandardAlloc::default(),
StandardAlloc::default(),
StandardAlloc::default(),
)),
uncompressed_read: 0,
inner,
};
self.read(buf)
}
CompressionLayerFailSafeReaderState::InData {
mut cache,
mut read_offset,
mut cache_filled_offset,
mut state,
mut uncompressed_read,
mut inner,
} => {
if uncompressed_read > UNCOMPRESSED_DATA_SIZE {
return Err(Error::WrongReaderState(
"[Compress FailSafe Layer] Too much data read".to_string(),
)
.into());
}
if read_offset == cache_filled_offset
&& cache_filled_offset == FAIL_SAFE_BUFFER_SIZE
{
// Cache is full and there is no more data to read from
// -> cache must be reset
cache.fill(0);
cache_filled_offset = 0;
read_offset = 0;
}
// Try to fill the cache from the inner source
match inner.read(&mut cache[cache_filled_offset..]) {
Ok(read) => {
if read == 0 && read_offset == cache_filled_offset {
// No more data from inner and the cache has been fully read
// -> return either an error or Ok(0)
if uncompressed_read > 0 {
// Inside a stream and no more data available
return Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
"No more data from the inner layer",
));
} else {
// No more data available but not in a stream
return Ok(0);
}
}
cache_filled_offset += read;
}
error => {
if read_offset == cache_filled_offset {
// No more data in the cache
return error;
}
// There is still data in the cache to read
// Will fail and return the error on the next .read()
}
}
// Number of byte available in the source
let mut available_in = cache_filled_offset - read_offset;
// IN: Offset in the source
// OUT: Offset in the source after the decompression pass
let mut input_offset = 0;
// Available spaces in the output
let mut available_out = std::cmp::min(
buf.len(),
(UNCOMPRESSED_DATA_SIZE - uncompressed_read) as usize,
);
// IN: Offset in the output
// OUT: number of bytes written in the output
let mut output_offset = 0;
// OUT: total number of byte written for the current stream (cumulative)
let mut written = 0;
let ret = match brotli::BrotliDecompressStream(
&mut available_in,
&mut input_offset,
&cache[read_offset..cache_filled_offset],
&mut available_out,
&mut output_offset,
buf,
&mut written,
&mut state,
) {
brotli::BrotliResult::ResultSuccess => {
// End of stream reached
// Rewind the cache to the actual start of the new block
// input_offset \in [0; cache_filled_offset - read_offset[
read_offset += input_offset;
// Reset others
state = Box::new(BrotliState::new(
StandardAlloc::default(),
StandardAlloc::default(),
StandardAlloc::default(),
));
uncompressed_read = 0;
Ok(output_offset)
}
brotli::BrotliResult::NeedsMoreInput => {
// Bytes may have been read and produced
read_offset += input_offset;
uncompressed_read += output_offset as u32;
Ok(output_offset)
}
brotli::BrotliResult::NeedsMoreOutput => {
// Bytes may have been read and produced
read_offset += input_offset;
uncompressed_read += output_offset as u32;
Ok(output_offset)
}
brotli::BrotliResult::ResultFailure => Err(io::Error::new(
io::ErrorKind::InvalidData,
"Invalid Data while decompressing",
)),
};
self.state = CompressionLayerFailSafeReaderState::InData {
cache,
cache_filled_offset,
read_offset,
state,
uncompressed_read,
inner,
};
ret
}
CompressionLayerFailSafeReaderState::Empty => Err(Error::WrongReaderState(
"[Compression Layer] Should never happens, unless an error already occurs before"
.to_string(),
)
.into()),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Layers;
use crate::layers::raw::{RawLayerFailSafeReader, RawLayerReader, RawLayerWriter};
use brotli::writer::StandardAlloc;
use rand::distributions::{Alphanumeric, Distribution, Standard};
use rand::SeedableRng;
use std::io::{Cursor, Read, Write};
use std::time::Instant;
// Use few UNCOMPRESSED_DATA_SIZE to force few blocks, and
// UNCOMPRESSED_DATA_SIZE / 2 to add a non complete one
static SIZE: usize = (UNCOMPRESSED_DATA_SIZE * 2 + UNCOMPRESSED_DATA_SIZE / 2) as usize;
// Return a vector of data of size SIZE
fn get_data() -> Vec<u8> {
// Use only alphanumeric charset to allow for compression
let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0);
let data: Vec<u8> = Alphanumeric.sample_iter(&mut rng).take(SIZE).collect();
assert_eq!(data.len(), SIZE);
data
}
// Return a vector of uncompressable data (ie. purely random) of size SIZE
fn get_uncompressable_data() -> Vec<u8> {
// Use only alphanumeric charset to allow for compression
let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0);
let data: Vec<u8> = Standard.sample_iter(&mut rng).take(SIZE).collect();
assert_eq!(data.len(), SIZE);
data
}
#[test]
fn compress_layer_writer() {
// Test with one "CompressedBlock"
let file = Vec::new();
let mut comp = Box::new(CompressionLayerWriter::new(
Box::new(RawLayerWriter::new(file)),
&CompressionConfig::default(),
));
let mut fake_data = vec![1, 2, 3, 4];
let fake_data2 = vec![5, 6, 7, 8];
comp.write_all(fake_data.as_slice()).unwrap();
comp.write_all(fake_data2.as_slice()).unwrap();
let file = comp.into_raw();
let mut src = Cursor::new(file.as_slice());
let mut reader = brotli::Decompressor::new(&mut src, 0);
let mut buf = Vec::new();
reader.read_to_end(&mut buf).unwrap();
println!("{:?}", buf);
fake_data.extend(fake_data2);
assert_eq!(fake_data, buf);
}
#[test]
fn compress_layer_several() {
// Test with several CompressedBlock - ensure that having only
// compressed blocks without header is enough to be able to distinguish
// them at decompression, knowing the uncompressed block size
let data = get_data();
let bytes = data.as_slice();
let file = Vec::new();