-
Notifications
You must be signed in to change notification settings - Fork 35
/
gzip.rs
1267 lines (1182 loc) · 37 KB
/
gzip.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
//! The encoder and decoder of the GZIP format.
//!
//! The GZIP format is defined in [RFC-1952](https://tools.ietf.org/html/rfc1952).
//!
//! # Examples
//! ```
//! use std::io::{self, Read};
//! use libflate::gzip::{Encoder, Decoder};
//!
//! // Encoding
//! let mut encoder = Encoder::new(Vec::new()).unwrap();
//! io::copy(&mut &b"Hello World!"[..], &mut encoder).unwrap();
//! let encoded_data = encoder.finish().into_result().unwrap();
//!
//! // Decoding
//! let mut decoder = Decoder::new(&encoded_data[..]).unwrap();
//! let mut decoded_data = Vec::new();
//! decoder.read_to_end(&mut decoded_data).unwrap();
//!
//! assert_eq!(decoded_data, b"Hello World!");
//! ```
use byteorder::LittleEndian;
use byteorder::ReadBytesExt;
use byteorder::WriteBytesExt;
use std::ffi::CString;
use std::io;
use std::mem;
use std::time;
use checksum;
use deflate;
use finish::{Complete, Finish};
use lz77;
const GZIP_ID: [u8; 2] = [31, 139];
const COMPRESSION_METHOD_DEFLATE: u8 = 8;
const OS_FAT: u8 = 0;
const OS_AMIGA: u8 = 1;
const OS_VMS: u8 = 2;
const OS_UNIX: u8 = 3;
const OS_VM_CMS: u8 = 4;
const OS_ATARI_TOS: u8 = 5;
const OS_HPFS: u8 = 6;
const OS_MACINTOSH: u8 = 7;
const OS_Z_SYSTEM: u8 = 8;
const OS_CPM: u8 = 9;
const OS_TOPS20: u8 = 10;
const OS_NTFS: u8 = 11;
const OS_QDOS: u8 = 12;
const OS_ACORN_RISCOS: u8 = 13;
const OS_UNKNOWN: u8 = 255;
const F_TEXT: u8 = 0b00_0001;
const F_HCRC: u8 = 0b00_0010;
const F_EXTRA: u8 = 0b00_0100;
const F_NAME: u8 = 0b00_1000;
const F_COMMENT: u8 = 0b01_0000;
/// Compression levels defined by the GZIP format.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum CompressionLevel {
/// Compressor used fastest algorithm.
Fastest,
/// Compressor used maximum compression, slowest algorithm.
Slowest,
/// No information about compression method.
Unknown,
}
impl CompressionLevel {
fn to_u8(&self) -> u8 {
match *self {
CompressionLevel::Fastest => 4,
CompressionLevel::Slowest => 2,
CompressionLevel::Unknown => 0,
}
}
fn from_u8(x: u8) -> Self {
match x {
4 => CompressionLevel::Fastest,
2 => CompressionLevel::Slowest,
_ => CompressionLevel::Unknown,
}
}
}
impl From<lz77::CompressionLevel> for CompressionLevel {
fn from(f: lz77::CompressionLevel) -> Self {
match f {
lz77::CompressionLevel::Fast => CompressionLevel::Fastest,
lz77::CompressionLevel::Best => CompressionLevel::Slowest,
_ => CompressionLevel::Unknown,
}
}
}
#[derive(Debug, Clone)]
pub(crate) struct Trailer {
crc32: u32,
input_size: u32,
}
impl Trailer {
pub fn crc32(&self) -> u32 {
self.crc32
}
pub fn read_from<R>(mut reader: R) -> io::Result<Self>
where
R: io::Read,
{
Ok(Trailer {
crc32: reader.read_u32::<LittleEndian>()?,
input_size: reader.read_u32::<LittleEndian>()?,
})
}
fn write_to<W>(&self, mut writer: W) -> io::Result<()>
where
W: io::Write,
{
writer.write_u32::<LittleEndian>(self.crc32)?;
writer.write_u32::<LittleEndian>(self.input_size)?;
Ok(())
}
}
/// GZIP header builder.
#[derive(Debug, Clone)]
pub struct HeaderBuilder {
header: Header,
}
impl HeaderBuilder {
/// Makes a new builder instance.
///
/// # Examples
/// ```
/// use libflate::gzip::{HeaderBuilder, CompressionLevel, Os};
///
/// let header = HeaderBuilder::new().finish();
/// assert_eq!(header.compression_level(), CompressionLevel::Unknown);
/// assert_eq!(header.os(), Os::Unix);
/// assert_eq!(header.is_text(), false);
/// assert_eq!(header.is_verified(), false);
/// assert_eq!(header.extra_field(), None);
/// assert_eq!(header.filename(), None);
/// assert_eq!(header.comment(), None);
/// ```
pub fn new() -> Self {
let modification_time = time::UNIX_EPOCH
.elapsed()
.map(|d| d.as_secs() as u32)
.unwrap_or(0);
let header = Header {
modification_time,
compression_level: CompressionLevel::Unknown,
os: Os::Unix,
is_text: false,
is_verified: false,
extra_field: None,
filename: None,
comment: None,
};
HeaderBuilder { header }
}
/// Sets the modification time (UNIX timestamp).
///
/// # Examples
/// ```
/// use libflate::gzip::HeaderBuilder;
///
/// let header = HeaderBuilder::new().modification_time(10).finish();
/// assert_eq!(header.modification_time(), 10);
/// ```
pub fn modification_time(&mut self, modification_time: u32) -> &mut Self {
self.header.modification_time = modification_time;
self
}
/// Sets the OS type.
///
/// ```
/// use libflate::gzip::{HeaderBuilder, Os};
///
/// let header = HeaderBuilder::new().os(Os::Ntfs).finish();
/// assert_eq!(header.os(), Os::Ntfs);
/// ```
pub fn os(&mut self, os: Os) -> &mut Self {
self.header.os = os;
self
}
/// Indicates the encoding data is a ASCII text.
///
/// # Examples
/// ```
/// use libflate::gzip::HeaderBuilder;
///
/// let header = HeaderBuilder::new().text().finish();
/// assert_eq!(header.is_text(), true);
/// ```
pub fn text(&mut self) -> &mut Self {
self.header.is_text = true;
self
}
/// Specifies toe verify header bytes using CRC-16.
///
/// # Examples
/// ```
/// use libflate::gzip::HeaderBuilder;
///
/// let header = HeaderBuilder::new().verify().finish();
/// assert_eq!(header.is_verified(), true);
/// ```
pub fn verify(&mut self) -> &mut Self {
self.header.is_verified = true;
self
}
/// Sets the extra field.
///
/// # Examples
/// ```
/// use libflate::gzip::{HeaderBuilder, ExtraField, ExtraSubField};
///
/// let subfield = ExtraSubField{id: [0, 1], data: vec![2, 3, 4]};
/// let extra = ExtraField{subfields: vec![subfield]};
/// let header = HeaderBuilder::new().extra_field(extra.clone()).finish();
/// assert_eq!(header.extra_field(), Some(&extra));
/// ```
pub fn extra_field(&mut self, extra: ExtraField) -> &mut Self {
self.header.extra_field = Some(extra);
self
}
/// Sets the file name.
///
/// # Examples
/// ```
/// use std::ffi::CString;
/// use libflate::gzip::HeaderBuilder;
///
/// let header = HeaderBuilder::new().filename(CString::new("foo").unwrap()).finish();
/// assert_eq!(header.filename(), Some(&CString::new("foo").unwrap()));
/// ```
pub fn filename(&mut self, filename: CString) -> &mut Self {
self.header.filename = Some(filename);
self
}
/// Sets the comment.
///
/// # Examples
/// ```
/// use std::ffi::CString;
/// use libflate::gzip::HeaderBuilder;
///
/// let header = HeaderBuilder::new().comment(CString::new("foo").unwrap()).finish();
/// assert_eq!(header.comment(), Some(&CString::new("foo").unwrap()));
/// ```
pub fn comment(&mut self, comment: CString) -> &mut Self {
self.header.comment = Some(comment);
self
}
/// Returns the result header.
pub fn finish(&self) -> Header {
self.header.clone()
}
}
impl Default for HeaderBuilder {
fn default() -> Self {
Self::new()
}
}
/// GZIP Header.
#[derive(Debug, Clone)]
pub struct Header {
modification_time: u32,
compression_level: CompressionLevel,
os: Os,
is_text: bool,
is_verified: bool,
extra_field: Option<ExtraField>,
filename: Option<CString>,
comment: Option<CString>,
}
impl Header {
/// Returns the modification time (UNIX timestamp).
pub fn modification_time(&self) -> u32 {
self.modification_time
}
/// Returns the compression level.
pub fn compression_level(&self) -> CompressionLevel {
self.compression_level.clone()
}
/// Returns the OS type.
pub fn os(&self) -> Os {
self.os.clone()
}
/// Returns `true` if the stream is probably ASCII text, `false` otherwise.
pub fn is_text(&self) -> bool {
self.is_text
}
/// Returns `true` if the header bytes is verified by CRC-16, `false` otherwise.
pub fn is_verified(&self) -> bool {
self.is_verified
}
/// Returns the extra field.
pub fn extra_field(&self) -> Option<&ExtraField> {
self.extra_field.as_ref()
}
/// Returns the file name.
pub fn filename(&self) -> Option<&CString> {
self.filename.as_ref()
}
/// Returns the comment.
pub fn comment(&self) -> Option<&CString> {
self.comment.as_ref()
}
fn flags(&self) -> u8 {
[
(F_TEXT, self.is_text),
(F_HCRC, self.is_verified),
(F_EXTRA, self.extra_field.is_some()),
(F_NAME, self.filename.is_some()),
(F_COMMENT, self.comment.is_some()),
]
.iter()
.filter(|e| e.1)
.map(|e| e.0)
.sum()
}
fn crc16(&self) -> u16 {
let mut crc = checksum::Crc32::new();
let mut buf = Vec::new();
Header {
is_verified: false,
..self.clone()
}
.write_to(&mut buf)
.unwrap();
crc.update(&buf);
crc.value() as u16
}
fn write_to<W>(&self, mut writer: W) -> io::Result<()>
where
W: io::Write,
{
writer.write_all(&GZIP_ID)?;
writer.write_u8(COMPRESSION_METHOD_DEFLATE)?;
writer.write_u8(self.flags())?;
writer.write_u32::<LittleEndian>(self.modification_time)?;
writer.write_u8(self.compression_level.to_u8())?;
writer.write_u8(self.os.to_u8())?;
if let Some(ref x) = self.extra_field {
x.write_to(&mut writer)?;
}
if let Some(ref x) = self.filename {
writer.write_all(x.as_bytes_with_nul())?;
}
if let Some(ref x) = self.comment {
writer.write_all(x.as_bytes_with_nul())?;
}
if self.is_verified {
writer.write_u16::<LittleEndian>(self.crc16())?;
}
Ok(())
}
pub(crate) fn read_from<R>(mut reader: R) -> io::Result<Self>
where
R: io::Read,
{
let mut this = HeaderBuilder::new().finish();
let mut id = [0; 2];
reader.read_exact(&mut id)?;
if id != GZIP_ID {
return Err(invalid_data_error!(
"Unexpected GZIP ID: value={:?}, \
expected={:?}",
id,
GZIP_ID
));
}
let compression_method = reader.read_u8()?;
if compression_method != COMPRESSION_METHOD_DEFLATE {
return Err(invalid_data_error!(
"Compression methods other than DEFLATE(8) are \
unsupported: method={}",
compression_method
));
}
let flags = reader.read_u8()?;
this.modification_time = reader.read_u32::<LittleEndian>()?;
this.compression_level = CompressionLevel::from_u8(reader.read_u8()?);
this.os = Os::from_u8(reader.read_u8()?);
if flags & F_EXTRA != 0 {
this.extra_field = Some(ExtraField::read_from(&mut reader)?);
}
if flags & F_NAME != 0 {
this.filename = Some(read_cstring(&mut reader)?);
}
if flags & F_COMMENT != 0 {
this.comment = Some(read_cstring(&mut reader)?);
}
// Checksum verification is skipped during fuzzing
// so that random data from fuzzer can reach actually interesting code.
// Compilation flag 'fuzzing' is automatically set by all 3 Rust fuzzers.
if flags & F_HCRC != 0 && cfg!(not(fuzzing)) {
let crc = reader.read_u16::<LittleEndian>()?;
let expected = this.crc16();
if crc != expected {
return Err(invalid_data_error!(
"CRC16 of GZIP header mismatched: value={}, \
expected={}",
crc,
expected
));
}
this.is_verified = true;
}
Ok(this)
}
}
fn read_cstring<R>(mut reader: R) -> io::Result<CString>
where
R: io::Read,
{
let mut buf = Vec::new();
loop {
let b = reader.read_u8()?;
if b == 0 {
return Ok(unsafe { CString::from_vec_unchecked(buf) });
}
buf.push(b);
}
}
/// Extra field of a GZIP header.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ExtraField {
/// Data of the extra field.
pub subfields: Vec<ExtraSubField>,
}
impl ExtraField {
fn read_from<R>(mut reader: R) -> io::Result<Self>
where
R: io::Read,
{
let mut subfields = Vec::new();
let data_size = reader.read_u16::<LittleEndian>()? as usize;
let mut reader = reader.take(data_size as u64);
while reader.limit() > 0 {
subfields.push(ExtraSubField::read_from(&mut reader)?);
}
Ok(ExtraField { subfields })
}
fn write_to<W>(&self, mut writer: W) -> io::Result<()>
where
W: io::Write,
{
let len = self.subfields.iter().map(|f| f.write_len()).sum::<usize>();
if len > 0xFFFF {
return Err(invalid_data_error!("extra field too long: {}", len));
}
writer.write_u16::<LittleEndian>(len as u16)?;
for f in &self.subfields {
f.write_to(&mut writer)?;
}
Ok(())
}
}
/// A sub field in the extra field of a GZIP header.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ExtraSubField {
/// ID of the field.
pub id: [u8; 2],
/// Data of the field.
pub data: Vec<u8>,
}
impl ExtraSubField {
fn read_from<R>(mut reader: R) -> io::Result<Self>
where
R: io::Read,
{
let mut field = ExtraSubField {
id: [0; 2],
data: Vec::new(),
};
reader.read_exact(&mut field.id)?;
let data_size = reader.read_u16::<LittleEndian>()? as usize;
field.data.resize(data_size, 0);
reader.read_exact(&mut field.data)?;
Ok(field)
}
fn write_to<W>(&self, mut writer: W) -> io::Result<()>
where
W: io::Write,
{
writer.write_all(&self.id)?;
writer.write_u16::<LittleEndian>(self.data.len() as u16)?;
writer.write_all(&self.data)?;
Ok(())
}
fn write_len(&self) -> usize {
4 + self.data.len()
}
}
/// OS type.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Os {
/// FAT filesystem (MS-DOS, OS/2, NT/Win32)
Fat,
/// Amiga
Amiga,
/// VMS (or OpenVMS)
Vms,
/// Unix
Unix,
/// VM/CMS
VmCms,
/// Atari TOS
AtariTos,
/// HPFS filesystem (OS/2, NT)
Hpfs,
/// Macintosh
Macintosh,
/// Z-System
ZSystem,
/// CP/M
CpM,
/// TOPS-20
Tops20,
/// NTFS filesystem (NT)
Ntfs,
/// QDOS
Qdos,
/// Acorn RISCOS
AcornRiscos,
/// Unknown
Unknown,
/// Undefined value in RFC-1952
Undefined(u8),
}
impl Os {
fn to_u8(&self) -> u8 {
match *self {
Os::Fat => OS_FAT,
Os::Amiga => OS_AMIGA,
Os::Vms => OS_VMS,
Os::Unix => OS_UNIX,
Os::VmCms => OS_VM_CMS,
Os::AtariTos => OS_ATARI_TOS,
Os::Hpfs => OS_HPFS,
Os::Macintosh => OS_MACINTOSH,
Os::ZSystem => OS_Z_SYSTEM,
Os::CpM => OS_CPM,
Os::Tops20 => OS_TOPS20,
Os::Ntfs => OS_NTFS,
Os::Qdos => OS_QDOS,
Os::AcornRiscos => OS_ACORN_RISCOS,
Os::Unknown => OS_UNKNOWN,
Os::Undefined(os) => os,
}
}
fn from_u8(x: u8) -> Self {
match x {
OS_FAT => Os::Fat,
OS_AMIGA => Os::Amiga,
OS_VMS => Os::Vms,
OS_UNIX => Os::Unix,
OS_VM_CMS => Os::VmCms,
OS_ATARI_TOS => Os::AtariTos,
OS_HPFS => Os::Hpfs,
OS_MACINTOSH => Os::Macintosh,
OS_Z_SYSTEM => Os::ZSystem,
OS_CPM => Os::CpM,
OS_TOPS20 => Os::Tops20,
OS_NTFS => Os::Ntfs,
OS_QDOS => Os::Qdos,
OS_ACORN_RISCOS => Os::AcornRiscos,
OS_UNKNOWN => Os::Unknown,
os => Os::Undefined(os),
}
}
}
/// Options for a GZIP encoder.
#[derive(Debug)]
pub struct EncodeOptions<E>
where
E: lz77::Lz77Encode,
{
header: Header,
options: deflate::EncodeOptions<E>,
}
impl Default for EncodeOptions<lz77::DefaultLz77Encoder> {
fn default() -> Self {
EncodeOptions {
header: HeaderBuilder::new().finish(),
options: Default::default(),
}
}
}
impl EncodeOptions<lz77::DefaultLz77Encoder> {
/// Makes a default instance.
///
/// # Examples
/// ```
/// use libflate::gzip::{Encoder, EncodeOptions};
///
/// let options = EncodeOptions::new();
/// let encoder = Encoder::with_options(Vec::new(), options).unwrap();
/// ```
pub fn new() -> Self {
Self::default()
}
}
impl<E> EncodeOptions<E>
where
E: lz77::Lz77Encode,
{
/// Specifies the LZ77 encoder used to compress input data.
///
/// # Example
/// ```
/// use libflate::lz77::DefaultLz77Encoder;
/// use libflate::gzip::{Encoder, EncodeOptions};
///
/// let options = EncodeOptions::with_lz77(DefaultLz77Encoder::new());
/// let encoder = Encoder::with_options(Vec::new(), options).unwrap();
/// ```
pub fn with_lz77(lz77: E) -> Self {
let mut header = HeaderBuilder::new().finish();
header.compression_level = From::from(lz77.compression_level());
EncodeOptions {
header,
options: deflate::EncodeOptions::with_lz77(lz77),
}
}
/// Disables LZ77 compression.
///
/// # Example
/// ```
/// use libflate::lz77::DefaultLz77Encoder;
/// use libflate::gzip::{Encoder, EncodeOptions};
///
/// let options = EncodeOptions::new().no_compression();
/// let encoder = Encoder::with_options(Vec::new(), options).unwrap();
/// ```
pub fn no_compression(mut self) -> Self {
self.options = self.options.no_compression();
self.header.compression_level = CompressionLevel::Unknown;
self
}
/// Sets the GZIP header which will be written to the output stream.
///
/// # Example
/// ```
/// use libflate::gzip::{Encoder, EncodeOptions, HeaderBuilder};
///
/// let header = HeaderBuilder::new().text().modification_time(100).finish();
/// let options = EncodeOptions::new().header(header);
/// let encoder = Encoder::with_options(Vec::new(), options).unwrap();
/// ```
pub fn header(mut self, header: Header) -> Self {
self.header = header;
self
}
/// Specifies the hint of the size of a DEFLATE block.
///
/// The default value is `deflate::DEFAULT_BLOCK_SIZE`.
///
/// # Example
/// ```
/// use libflate::gzip::{Encoder, EncodeOptions};
///
/// let options = EncodeOptions::new().block_size(512 * 1024);
/// let encoder = Encoder::with_options(Vec::new(), options).unwrap();
/// ```
pub fn block_size(mut self, size: usize) -> Self {
self.options = self.options.block_size(size);
self
}
/// Specifies to compress with fixed huffman codes.
///
/// # Example
/// ```
/// use libflate::gzip::{Encoder, EncodeOptions};
///
/// let options = EncodeOptions::new().fixed_huffman_codes();
/// let encoder = Encoder::with_options(Vec::new(), options).unwrap();
/// ```
pub fn fixed_huffman_codes(mut self) -> Self {
self.options = self.options.fixed_huffman_codes();
self
}
}
/// GZIP encoder.
pub struct Encoder<W, E = lz77::DefaultLz77Encoder> {
header: Header,
crc32: checksum::Crc32,
input_size: u32,
writer: deflate::Encoder<W, E>,
}
impl<W> Encoder<W, lz77::DefaultLz77Encoder>
where
W: io::Write,
{
/// Makes a new encoder instance.
///
/// Encoded GZIP stream is written to `inner`.
///
/// # Examples
/// ```
/// use std::io::Write;
/// use libflate::gzip::Encoder;
///
/// let mut encoder = Encoder::new(Vec::new()).unwrap();
/// encoder.write_all(b"Hello World!").unwrap();
/// encoder.finish().into_result().unwrap();
/// ```
pub fn new(inner: W) -> io::Result<Self> {
Self::with_options(inner, EncodeOptions::new())
}
}
impl<W, E> Encoder<W, E>
where
W: io::Write,
E: lz77::Lz77Encode,
{
/// Makes a new encoder instance with specified options.
///
/// Encoded GZIP stream is written to `inner`.
///
/// # Examples
/// ```
/// use std::io::Write;
/// use libflate::gzip::{Encoder, EncodeOptions, HeaderBuilder};
///
/// let header = HeaderBuilder::new().modification_time(123).finish();
/// let options = EncodeOptions::new().no_compression().header(header);
/// let mut encoder = Encoder::with_options(Vec::new(), options).unwrap();
/// encoder.write_all(b"Hello World!").unwrap();
///
/// assert_eq!(encoder.finish().into_result().unwrap(),
/// &[31, 139, 8, 0, 123, 0, 0, 0, 0, 3, 1, 12, 0, 243, 255, 72, 101, 108, 108,
/// 111, 32, 87, 111, 114, 108, 100, 33, 163, 28, 41, 28, 12, 0, 0, 0][..]);
/// ```
pub fn with_options(mut inner: W, options: EncodeOptions<E>) -> io::Result<Self> {
options.header.write_to(&mut inner)?;
Ok(Encoder {
header: options.header.clone(),
crc32: checksum::Crc32::new(),
input_size: 0,
writer: deflate::Encoder::with_options(inner, options.options),
})
}
/// Returns the header of the GZIP stream.
///
/// # Examples
/// ```
/// use libflate::gzip::{Encoder, Os};
///
/// let encoder = Encoder::new(Vec::new()).unwrap();
/// assert_eq!(encoder.header().os(), Os::Unix);
/// ```
pub fn header(&self) -> &Header {
&self.header
}
/// Writes the GZIP trailer and returns the inner stream.
///
/// # Examples
/// ```
/// use std::io::Write;
/// use libflate::gzip::Encoder;
///
/// let mut encoder = Encoder::new(Vec::new()).unwrap();
/// encoder.write_all(b"Hello World!").unwrap();
///
/// assert!(encoder.finish().as_result().is_ok())
/// ```
///
/// # Note
///
/// If you are not concerned the result of this encoding,
/// it may be convenient to use `AutoFinishUnchecked` instead of the explicit invocation of this method.
///
/// ```
/// use std::io;
/// use libflate::finish::AutoFinishUnchecked;
/// use libflate::gzip::Encoder;
///
/// let plain = b"Hello World!";
/// let mut buf = Vec::new();
/// let mut encoder = AutoFinishUnchecked::new(Encoder::new(&mut buf).unwrap());
/// io::copy(&mut &plain[..], &mut encoder).unwrap();
/// ```
pub fn finish(self) -> Finish<W, io::Error> {
let trailer = Trailer {
crc32: self.crc32.value(),
input_size: self.input_size,
};
let mut inner = finish_try!(self.writer.finish());
match trailer.write_to(&mut inner).and_then(|_| inner.flush()) {
Ok(_) => Finish::new(inner, None),
Err(e) => Finish::new(inner, Some(e)),
}
}
/// Returns the immutable reference to the inner stream.
pub fn as_inner_ref(&self) -> &W {
self.writer.as_inner_ref()
}
/// Returns the mutable reference to the inner stream.
pub fn as_inner_mut(&mut self) -> &mut W {
self.writer.as_inner_mut()
}
/// Unwraps the `Encoder`, returning the inner stream.
pub fn into_inner(self) -> W {
self.writer.into_inner()
}
}
impl<W, E> io::Write for Encoder<W, E>
where
W: io::Write,
E: lz77::Lz77Encode,
{
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let written_size = self.writer.write(buf)?;
self.crc32.update(&buf[..written_size]);
self.input_size = self.input_size.wrapping_add(written_size as u32);
Ok(written_size)
}
fn flush(&mut self) -> io::Result<()> {
self.writer.flush()
}
}
impl<W, E> Complete for Encoder<W, E>
where
W: io::Write,
E: lz77::Lz77Encode,
{
fn complete(self) -> io::Result<()> {
self.finish().into_result().map(|_| ())
}
}
/// GZIP decoder.
#[derive(Debug)]
pub struct Decoder<R> {
header: Header,
reader: deflate::Decoder<R>,
crc32: checksum::Crc32,
eos: bool,
}
impl<R> Decoder<R>
where
R: io::Read,
{
/// Makes a new decoder instance.
///
/// `inner` is to be decoded GZIP stream.
///
/// # Examples
/// ```
/// use std::io::Read;
/// use libflate::gzip::Decoder;
///
/// let encoded_data = [31, 139, 8, 0, 123, 0, 0, 0, 0, 3, 1, 12, 0, 243, 255,
/// 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33,
/// 163, 28, 41, 28, 12, 0, 0, 0];
///
/// let mut decoder = Decoder::new(&encoded_data[..]).unwrap();
/// let mut buf = Vec::new();
/// decoder.read_to_end(&mut buf).unwrap();
///
/// assert_eq!(buf, b"Hello World!");
/// ```
pub fn new(mut inner: R) -> io::Result<Self> {
let header = Header::read_from(&mut inner)?;
Ok(Self::with_header(inner, header))
}
/// Returns the header of the GZIP stream.
///
/// # Examples
/// ```
/// use libflate::gzip::{Decoder, Os};
///
/// let encoded_data = [31, 139, 8, 0, 123, 0, 0, 0, 0, 3, 1, 12, 0, 243, 255,
/// 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33,
/// 163, 28, 41, 28, 12, 0, 0, 0];
///
/// let decoder = Decoder::new(&encoded_data[..]).unwrap();
/// assert_eq!(decoder.header().os(), Os::Unix);
/// ```
pub fn header(&self) -> &Header {
&self.header
}
/// Returns the immutable reference to the inner stream.
pub fn as_inner_ref(&self) -> &R {
self.reader.as_inner_ref()
}
/// Returns the mutable reference to the inner stream.
pub fn as_inner_mut(&mut self) -> &mut R {
self.reader.as_inner_mut()
}
/// Unwraps this `Decoder`, returning the underlying reader.
///
/// # Examples
/// ```
/// use std::io::Cursor;
/// use libflate::gzip::Decoder;
///
/// let encoded_data = [31, 139, 8, 0, 123, 0, 0, 0, 0, 3, 1, 12, 0, 243, 255,
/// 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33,
/// 163, 28, 41, 28, 12, 0, 0, 0];
///
/// let decoder = Decoder::new(Cursor::new(&encoded_data[..])).unwrap();
/// assert_eq!(decoder.into_inner().into_inner(), &encoded_data[..]);
/// ```
pub fn into_inner(self) -> R {
self.reader.into_inner()
}
fn with_header(inner: R, header: Header) -> Self {
Decoder {
header,
reader: deflate::Decoder::new(inner),
crc32: checksum::Crc32::new(),
eos: false,
}
}
}
impl<R> io::Read for Decoder<R>
where
R: io::Read,
{
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
if self.eos {
Ok(0)
} else {
let read_size = self.reader.read(buf)?;
self.crc32.update(&buf[..read_size]);
if read_size == 0 {
self.eos = true;
let trailer = Trailer::read_from(self.reader.as_inner_mut())?;
// checksum verification is skipped during fuzzing
// so that random data from fuzzer can reach actually interesting code
// Compilation flag 'fuzzing' is automatically set by all 3 Rust fuzzers.
if cfg!(not(fuzzing)) && trailer.crc32 != self.crc32.value() {
Err(invalid_data_error!(
"CRC32 mismatched: value={}, expected={}",
self.crc32.value(),
trailer.crc32
))
} else {