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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
//! The encoder and decoder of the GZIP format.
//!
//! The GZIP format is defined in [RFC-1952](https://tools.ietf.org/html/rfc1952).
//!
//! # Examples
//! ```
//! #[cfg(feature = "no_std")]
//! use core2::io::{Read, Write};
//! #[cfg(not(feature = "no_std"))]
//! use std::io::{Read, Write};
//! use libflate::gzip::{Encoder, Decoder};
//!
//! // Encoding
//! let mut encoder = Encoder::new(Vec::new()).unwrap();
//! encoder.write_all(b"Hello World!".as_ref()).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 crate::checksum;
use crate::deflate;
use crate::finish::{Complete, Finish};
use crate::lz77;
#[cfg(feature = "no_std")]
use alloc::{ffi::CString, vec::Vec};
#[cfg(feature = "no_std")]
use core2::io;
#[cfg(not(feature = "no_std"))]
use std::{ffi::CString, io, time};

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,
    {
        let mut buf = [0; 4];
        reader.read_exact(&mut buf)?;
        let crc32 = u32::from_le_bytes(buf);
        reader.read_exact(&mut buf)?;
        let input_size = u32::from_le_bytes(buf);
        Ok(Trailer { crc32, input_size })
    }
    fn write_to<W>(&self, mut writer: W) -> io::Result<()>
    where
        W: io::Write,
    {
        writer.write_all(&self.crc32.to_le_bytes())?;
        writer.write_all(&self.input_size.to_le_bytes())?;
        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 {
        // wasm-unknown-unknown does not implement the time module
        #[cfg(all(not(target_arch = "wasm32"), not(feature = "no_std")))]
        let modification_time = time::UNIX_EPOCH
            .elapsed()
            .map(|d| d.as_secs() as u32)
            .unwrap_or(0);
        #[cfg(any(target_arch = "wasm32", feature = "no_std"))]
        let modification_time = 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
    /// ```
    /// #[cfg(feature = "no_std")]
    /// extern crate alloc;
    /// #[cfg(feature = "no_std")]
    /// use alloc::ffi::CString;
    /// #[cfg(not(feature = "no_std"))]
    /// 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
    /// ```
    /// #[cfg(feature = "no_std")]
    /// extern crate alloc;
    /// #[cfg(feature = "no_std")]
    /// use alloc::ffi::CString;
    /// #[cfg(not(feature = "no_std"))]
    /// 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_all(&[COMPRESSION_METHOD_DEFLATE, self.flags()])?;
        writer.write_all(&self.modification_time.to_le_bytes())?;
        writer.write_all(&[self.compression_level.to_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_all(&self.crc16().to_le_bytes())?;
        }
        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 buf = [0; 2 + 1 + 1 + 4 + 1 + 1];
        reader.read_exact(&mut buf)?;
        let id = &buf[0..2];
        if id != GZIP_ID {
            return Err(invalid_data_error!(
                "Unexpected GZIP ID: value={:?}, \
                 expected={:?}",
                id,
                GZIP_ID
            ));
        }
        let compression_method = buf[2];
        if compression_method != COMPRESSION_METHOD_DEFLATE {
            return Err(invalid_data_error!(
                "Compression methods other than DEFLATE(8) are \
                 unsupported: method={}",
                compression_method
            ));
        }
        let flags = buf[3];
        this.modification_time = u32::from_le_bytes([buf[4], buf[5], buf[6], buf[7]]);
        this.compression_level = CompressionLevel::from_u8(buf[8]);
        this.os = Os::from_u8(buf[9]);
        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 mut buf = [0; 2];
            reader.read_exact(&mut buf)?;
            let crc = u16::from_le_bytes(buf);
            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 mut cbuf = [0; 1];
        reader.read_exact(&mut cbuf)?;
        if cbuf[0] == 0 {
            return Ok(CString::new(buf).unwrap());
        }
        buf.push(cbuf[0]);
    }
}

/// 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 mut buf = [0; 2];
        reader.read_exact(&mut buf)?;
        let data_size = u16::from_le_bytes(buf) 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_all(&(len as u16).to_le_bytes())?;
        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 mut buf = [0; 2];
        reader.read_exact(&mut buf)?;
        let data_size = u16::from_le_bytes(buf) 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_all(&(self.data.len() as u16).to_le_bytes())?;
        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
    /// ```
    /// #[cfg(feature = "no_std")]
    /// use core2::io::Write;
    /// #[cfg(not(feature = "no_std"))]
    /// 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
    /// ```
    /// #[cfg(feature = "no_std")]
    /// use core2::io::Write;
    /// #[cfg(not(feature = "no_std"))]
    /// 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
    /// ```
    /// #[cfg(feature = "no_std")]
    /// use core2::io::Write;
    /// #[cfg(not(feature = "no_std"))]
    /// 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.
    ///
    /// ```
    /// #[cfg(feature = "no_std")]
    /// use core2::io::Write;
    /// #[cfg(not(feature = "no_std"))]
    /// 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());
    /// #[cfg(feature = "no_std")]
    /// encoder.write_all(plain.as_ref()).unwrap();
    /// #[cfg(not(feature = "no_std"))]
    /// 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
    /// ```
    /// #[cfg(feature = "no_std")]
    /// use core2::io::Read;
    /// #[cfg(not(feature = "no_std"))]
    /// 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
    /// ```
    /// #[cfg(feature = "no_std")]
    /// use core2::io::Cursor;
    /// #[cfg(not(feature = "no_std"))]
    /// 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()
    }

    /// Returns the data that has been decoded but has not yet been read.
    ///
    /// This method is useful to retrieve partial decoded data when the decoding process is failed.
    pub fn unread_decoded_data(&self) -> &[u8] {
        self.reader.unread_decoded_data()
    }

    fn with_header(inner: R, header: Header) -> Self {
        Decoder {
            header,
            reader: deflate::Decoder::new(inner),
            crc32: checksum::Crc32::new(),
            eos: false,
        }
    }

    fn reset(&mut self, header: Header) {
        self.header = header;
        self.reader.reset();
        self.crc32 = checksum::Crc32::new();
        self.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 {
                if buf.is_empty() {
                    return Ok(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 {
                    Ok(0)
                }
            } else {
                Ok(read_size)
            }
        }
    }
}

/// A decoder that decodes all members in a GZIP stream.
#[derive(Debug)]
pub struct MultiDecoder<R> {
    decoder: Decoder<R>,
    eos: bool,
}
impl<R> MultiDecoder<R>
where
    R: io::Read,
{
    /// Makes a new decoder instance.
    ///
    /// `inner` is to be decoded GZIP stream.
    ///
    /// # Examples
    /// ```
    /// #[cfg(feature = "no_std")]
    /// use core2::io::Read;
    /// #[cfg(not(feature = "no_std"))]
    /// use std::io::Read;
    /// use libflate::gzip::MultiDecoder;
    ///
    /// let mut encoded_data = Vec::new();
    ///
    /// // Add a member (a GZIP binary that represents "Hello ")
    /// encoded_data.extend(&[31, 139, 8, 0, 51, 206, 75, 90, 0, 3, 5, 128, 49, 9, 0, 0, 0, 194, 170, 24,
    ///                       199, 34, 126, 3, 251, 127, 163, 131, 71, 192, 252, 45, 234, 6, 0, 0, 0][..]);
    ///
    /// // Add another member (a GZIP binary that represents "World!")
    /// encoded_data.extend(&[31, 139, 8, 0, 227, 207, 75, 90, 0, 3, 5, 128, 49, 9, 0, 0, 0, 194, 178, 152,
    ///                       202, 2, 158, 130, 96, 255, 99, 120, 111, 4, 222, 157, 40, 118, 6, 0, 0, 0][..]);
    ///
    /// let mut decoder = MultiDecoder::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(inner: R) -> io::Result<Self> {
        let decoder = Decoder::new(inner)?;
        Ok(MultiDecoder {
            decoder,
            eos: false,
        })
    }

    /// Returns the header of the current member in the GZIP stream.
    ///
    /// # Examples
    /// ```
    /// use libflate::gzip::{MultiDecoder, 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 = MultiDecoder::new(&encoded_data[..]).unwrap();
    /// assert_eq!(decoder.header().os(), Os::Unix);
    /// ```
    pub fn header(&self) -> &Header {
        self.decoder.header()
    }

    /// Returns the immutable reference to the inner stream.
    pub fn as_inner_ref(&self) -> &R {
        self.decoder.as_inner_ref()
    }

    /// Returns the mutable reference to the inner stream.
    pub fn as_inner_mut(&mut self) -> &mut R {
        self.decoder.as_inner_mut()
    }

    /// Unwraps this `MultiDecoder`, returning the underlying reader.
    ///
    /// # Examples
    /// ```
    /// #[cfg(feature = "no_std")]
    /// use core2::io::Cursor;
    /// #[cfg(not(feature = "no_std"))]
    /// use std::io::Cursor;
    /// use libflate::gzip::MultiDecoder;
    ///
    /// 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 = MultiDecoder::new(Cursor::new(&encoded_data[..])).unwrap();
    /// assert_eq!(decoder.into_inner().into_inner(), &encoded_data[..]);
    /// ```
    pub fn into_inner(self) -> R {
        self.decoder.into_inner()
    }
}
impl<R> io::Read for MultiDecoder<R>
where
    R: io::Read,
{
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        if self.eos {
            return Ok(0);
        }

        let read_size = self.decoder.read(buf)?;
        if read_size == 0 {
            match Header::read_from(self.as_inner_mut()) {
                Err(e) => {
                    if e.kind() == io::ErrorKind::UnexpectedEof {
                        self.eos = true;
                        Ok(0)
                    } else {
                        Err(e)
                    }
                }
                Ok(header) => {
                    self.decoder.reset(header);
                    self.read(buf)
                }
            }
        } else {
            Ok(read_size)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::finish::AutoFinish;
    #[cfg(feature = "no_std")]
    use core2::io::{Read, Write};
    #[cfg(not(feature = "no_std"))]
    use std::io::{self, Read, Write};

    fn decode(buf: &[u8]) -> io::Result<Vec<u8>> {
        let mut decoder = Decoder::new(buf).unwrap();
        let mut buf = Vec::with_capacity(buf.len());
        decoder.read_to_end(&mut buf)?;
        Ok(buf)
    }

    fn decode_multi(buf: &[u8]) -> io::Result<Vec<u8>> {
        let mut decoder = MultiDecoder::new(buf).unwrap();
        let mut buf = Vec::with_capacity(buf.len());
        decoder.read_to_end(&mut buf).unwrap();
        Ok(buf)
    }

    fn encode(text: &[u8]) -> io::Result<Vec<u8>> {
        let mut encoder = Encoder::new(Vec::new()).unwrap();
        encoder.write_all(text).unwrap();
        encoder.finish().into_result()
    }

    #[test]
    fn encode_works() {
        let plain = b"Hello World! Hello GZIP!!";
        let mut encoder = Encoder::new(Vec::new()).unwrap();
        encoder.write_all(plain.as_ref()).unwrap();
        let encoded = encoder.finish().into_result().unwrap();
        assert_eq!(decode(&encoded).unwrap(), plain);
    }

    #[test]
    fn encoder_auto_finish_works() {
        let plain = b"Hello World! Hello GZIP!!";
        let mut buf = Vec::new();
        {
            let mut encoder = AutoFinish::new(Encoder::new(&mut buf).unwrap());
            encoder.write_all(plain.as_ref()).unwrap();
        }
        assert_eq!(decode(&buf).unwrap(), plain);
    }

    #[test]
    fn multi_decode_works() {
        #[cfg(feature = "no_std")]
        use core::iter;
        #[cfg(not(feature = "no_std"))]
        use std::iter;
        let text = b"Hello World!";
        let encoded: Vec<u8> = iter::repeat(encode(text).unwrap())
            .take(2)
            .flat_map(|b| b)
            .collect();
        assert_eq!(decode(&encoded).unwrap(), b"Hello World!");
        assert_eq!(decode_multi(&encoded).unwrap(), b"Hello World!Hello World!");
    }

    #[test]
    /// See: https://github.com/sile/libflate/issues/15 and https://github.com/RazrFalcon/usvg/issues/20
    fn issue_15_1() {
        let data = b"\x1F\x8B\x08\xC1\x7B\x7B\x7B\x7B\x7B\x7B\x7B\x7B\x7B\x7B\x7B\x7B\x7B\x7B\x7B\x7B\x7B\x7B\x80\x80\x80\x80\x7B\x7B\x7B\x7B\x7B\x7B\x97\x7B\x7B\x7B\x86\x27\xEB\x60\xA7\xA8\x46\x6E\x1F\x33\x51\x5C\x34\xE0\xD2\x2E\xE8\x0C\x19\x1D\x3D\x3C\xFD\x3B\x6A\xFA\x63\xDF\x28\x87\x86\xF2\xA6\xAC\x87\x86\xF2\xA6\xAC\xD5";
        assert!(decode(&data[..]).is_err());
    }

    #[test]
    /// See: https://github.com/sile/libflate/issues/15 and https://github.com/RazrFalcon/usvg/issues/21
    fn issue_15_2() {
        let data = b"\x1F\x8B\x08\xC1\x7B\x7B\x7B\x7B\x7B\xFC\x5D\x2D\xDC\x08\xC1\x7B\x7B\x7B\x7B\x7B\xFC\x5D\x2D\xDC\x08\xC1\x7B\x7F\x7B\x7B\x7B\xFC\x5D\x2D\xDC\x69\x32\x48\x22\x5A\x81\x81\x42\x42\x81\x7E\x81\x81\x81\x81\xF2\x17";
        assert!(decode(&data[..]).is_err());
    }

    #[test]
    /// See: https://github.com/sile/libflate/issues/15 and https://github.com/RazrFalcon/usvg/issues/22
    fn issue_15_3() {
        let data = b"\x1F\x8B\x08\xC1\x91\x28\x71\xDC\xF2\x2D\x34\x35\x31\x35\x34\x30\x70\x6E\x60\x35\x31\x32\x32\x33\x32\x33\x37\x32\x36\x38\xDD\x1C\xE5\x2A\xDD\xDD\xDD\x22\xDD\xDD\xDD\xDC\x88\x13\xC9\x40\x60\xA7";
        assert!(decode(&data[..]).is_err());
    }

    #[test]
    /// See: https://github.com/sile/libflate/issues/61
    fn issue_61() {
        let data = encode(b"Hello World").unwrap();
        let mut decoder = Decoder::new(&data[..]).unwrap();
        let mut buf = Vec::new();
        decoder.read(&mut buf).unwrap();
        decoder.read_to_end(&mut buf).unwrap();
        assert_eq!(buf, b"Hello World");
    }

    #[test]
    fn extra_field() {
        let f = ExtraField {
            subfields: vec![ExtraSubField {
                id: [0, 0x42],
                data: "abc".into(),
            }],
        };

        let mut buf = Vec::new();
        f.write_to(&mut buf).unwrap();

        assert_eq!(ExtraField::read_from(&buf[..]).unwrap(), f);
    }

    #[test]
    #[cfg(not(feature = "no_std"))]
    fn encode_with_extra_field() {
        let mut buf = Vec::new();
        let extra_field = ExtraField {
            subfields: vec![ExtraSubField {
                id: [0, 0x42],
                data: "abc".into(),
            }],
        };
        {
            // encode
            let header = HeaderBuilder::new()
                .extra_field(extra_field.clone())
                .finish();

            let ops = EncodeOptions::new().header(header);
            let mut encoder = Encoder::with_options(&mut buf, ops).unwrap();
            write!(encoder, "hello world").unwrap();
            encoder.finish().as_result().unwrap();
        }
        {
            // decode
            let mut decoder = Decoder::new(&buf[..]).unwrap();
            io::copy(&mut decoder, &mut io::sink()).unwrap();
            assert_eq!(decoder.header().extra_field(), Some(&extra_field));
        }
    }
}