Debian Codegraph @

third_party/rust/zip/src/compression.rs

raw
1 //! Possible ZIP compression methods.
2
3 use std::fmt;
4
5 #[allow(deprecated)]
6 /// Identifies the storage format used to compress a file within a ZIP archive.
7 ///
8 /// Each file's compression method is stored alongside it, allowing the
9 /// contents to be read without context.
10 ///
11 /// When creating ZIP files, you may choose the method to use with
12 /// [`crate::write::FileOptions::compression_method`]
13 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
14 #[cfg_attr(fuzzing, derive(arbitrary::Arbitrary))]
15 #[non_exhaustive]
16 pub enum CompressionMethod {
17 /// Store the file as is
18 Stored,
19 /// Compress the file using Deflate
20 #[cfg(feature = "_deflate-any")]
21 Deflated,
22 /// Compress the file using Deflate64.
23 /// Decoding deflate64 is supported but encoding deflate64 is not supported.
24 #[cfg(feature = "deflate64")]
25 Deflate64,
26 /// Compress the file using BZIP2
27 #[cfg(feature = "bzip2")]
28 Bzip2,
29 /// Encrypted using AES.
30 ///
31 /// The actual compression method has to be taken from the AES extra data field
32 /// or from `ZipFileData`.
33 #[cfg(feature = "aes-crypto")]
34 Aes,
35 /// Compress the file using ZStandard
36 #[cfg(feature = "zstd")]
37 Zstd,
38 /// Compress the file using LZMA
39 #[cfg(feature = "lzma")]
40 Lzma,
41 /// Unsupported compression method
42 #[cfg_attr(
43 not(fuzzing),
44 deprecated(since = "0.5.7", note = "use the constants instead")
45 )]
46 Unsupported(u16),
47 }
48 #[allow(deprecated, missing_docs)]
49 /// All compression methods defined for the ZIP format
50 impl CompressionMethod {
51 pub const STORE: Self = CompressionMethod::Stored;
52 pub const SHRINK: Self = CompressionMethod::Unsupported(1);
53 pub const REDUCE_1: Self = CompressionMethod::Unsupported(2);
54 pub const REDUCE_2: Self = CompressionMethod::Unsupported(3);
55 pub const REDUCE_3: Self = CompressionMethod::Unsupported(4);
56 pub const REDUCE_4: Self = CompressionMethod::Unsupported(5);
57 pub const IMPLODE: Self = CompressionMethod::Unsupported(6);
58 #[cfg(feature = "_deflate-any")]
59 pub const DEFLATE: Self = CompressionMethod::Deflated;
60 #[cfg(not(feature = "_deflate-any"))]
61 pub const DEFLATE: Self = CompressionMethod::Unsupported(8);
62 #[cfg(feature = "deflate64")]
63 pub const DEFLATE64: Self = CompressionMethod::Deflate64;
64 #[cfg(not(feature = "deflate64"))]
65 pub const DEFLATE64: Self = CompressionMethod::Unsupported(9);
66 pub const PKWARE_IMPLODE: Self = CompressionMethod::Unsupported(10);
67 #[cfg(feature = "bzip2")]
68 pub const BZIP2: Self = CompressionMethod::Bzip2;
69 #[cfg(not(feature = "bzip2"))]
70 pub const BZIP2: Self = CompressionMethod::Unsupported(12);
71 #[cfg(not(feature = "lzma"))]
72 pub const LZMA: Self = CompressionMethod::Unsupported(14);
73 #[cfg(feature = "lzma")]
74 pub const LZMA: Self = CompressionMethod::Lzma;
75 pub const IBM_ZOS_CMPSC: Self = CompressionMethod::Unsupported(16);
76 pub const IBM_TERSE: Self = CompressionMethod::Unsupported(18);
77 pub const ZSTD_DEPRECATED: Self = CompressionMethod::Unsupported(20);
78 #[cfg(feature = "zstd")]
79 pub const ZSTD: Self = CompressionMethod::Zstd;
80 #[cfg(not(feature = "zstd"))]
81 pub const ZSTD: Self = CompressionMethod::Unsupported(93);
82 pub const MP3: Self = CompressionMethod::Unsupported(94);
83 pub const XZ: Self = CompressionMethod::Unsupported(95);
84 pub const JPEG: Self = CompressionMethod::Unsupported(96);
85 pub const WAVPACK: Self = CompressionMethod::Unsupported(97);
86 pub const PPMD: Self = CompressionMethod::Unsupported(98);
87 #[cfg(feature = "aes-crypto")]
88 pub const AES: Self = CompressionMethod::Aes;
89 #[cfg(not(feature = "aes-crypto"))]
90 pub const AES: Self = CompressionMethod::Unsupported(99);
91 }
92 impl CompressionMethod {
93 pub(crate) const fn parse_from_u16(val: u16) -> Self {
94 match val {
95 0 => CompressionMethod::Stored,
96 #[cfg(feature = "_deflate-any")]
97 8 => CompressionMethod::Deflated,
98 #[cfg(feature = "deflate64")]
99 9 => CompressionMethod::Deflate64,
100 #[cfg(feature = "bzip2")]
101 12 => CompressionMethod::Bzip2,
102 #[cfg(feature = "lzma")]
103 14 => CompressionMethod::Lzma,
104 #[cfg(feature = "zstd")]
105 93 => CompressionMethod::Zstd,
106 #[cfg(feature = "aes-crypto")]
107 99 => CompressionMethod::Aes,
108 #[allow(deprecated)]
109 v => CompressionMethod::Unsupported(v),
110 }
111 }
112
113 /// Converts a u16 to its corresponding CompressionMethod
114 #[deprecated(
115 since = "0.5.7",
116 note = "use a constant to construct a compression method"
117 )]
118 pub const fn from_u16(val: u16) -> CompressionMethod {
119 Self::parse_from_u16(val)
120 }
121
122 pub(crate) const fn serialize_to_u16(self) -> u16 {
123 match self {
124 CompressionMethod::Stored => 0,
125 #[cfg(feature = "_deflate-any")]
126 CompressionMethod::Deflated => 8,
127 #[cfg(feature = "deflate64")]
128 CompressionMethod::Deflate64 => 9,
129 #[cfg(feature = "bzip2")]
130 CompressionMethod::Bzip2 => 12,
131 #[cfg(feature = "aes-crypto")]
132 CompressionMethod::Aes => 99,
133 #[cfg(feature = "zstd")]
134 CompressionMethod::Zstd => 93,
135 #[cfg(feature = "lzma")]
136 CompressionMethod::Lzma => 14,
137 #[allow(deprecated)]
138 CompressionMethod::Unsupported(v) => v,
139 }
140 }
141
142 /// Converts a CompressionMethod to a u16
143 #[deprecated(
144 since = "0.5.7",
145 note = "to match on other compression methods, use a constant"
146 )]
147 pub const fn to_u16(self) -> u16 {
148 self.serialize_to_u16()
149 }
150 }
151
152 impl Default for CompressionMethod {
153 fn default() -> Self {
154 #[cfg(feature = "_deflate-any")]
155 return CompressionMethod::Deflated;
156
157 #[cfg(not(feature = "_deflate-any"))]
158 return CompressionMethod::Stored;
159 }
160 }
161
162 impl fmt::Display for CompressionMethod {
163 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
164 // Just duplicate what the Debug format looks like, i.e, the enum key:
165 write!(f, "{self:?}")
166 }
167 }
168
169 /// The compression methods which have been implemented.
170 pub const SUPPORTED_COMPRESSION_METHODS: &[CompressionMethod] = &[
171 CompressionMethod::Stored,
172 #[cfg(feature = "_deflate-any")]
173 CompressionMethod::Deflated,
174 #[cfg(feature = "deflate64")]
175 CompressionMethod::Deflate64,
176 #[cfg(feature = "bzip2")]
177 CompressionMethod::Bzip2,
178 #[cfg(feature = "zstd")]
179 CompressionMethod::Zstd,
180 ];
181
182 #[cfg(test)]
183 mod test {
184 use super::{CompressionMethod, SUPPORTED_COMPRESSION_METHODS};
185
186 #[test]
187 fn from_eq_to() {
188 for v in 0..(u16::MAX as u32 + 1) {
189 let from = CompressionMethod::parse_from_u16(v as u16);
190 let to = from.serialize_to_u16() as u32;
191 assert_eq!(v, to);
192 }
193 }
194
195 #[test]
196 fn to_eq_from() {
197 fn check_match(method: CompressionMethod) {
198 let to = method.serialize_to_u16();
199 let from = CompressionMethod::parse_from_u16(to);
200 let back = from.serialize_to_u16();
201 assert_eq!(to, back);
202 }
203
204 for &method in SUPPORTED_COMPRESSION_METHODS {
205 check_match(method);
206 }
207 }
208
209 #[test]
210 fn to_display_fmt() {
211 fn check_match(method: CompressionMethod) {
212 let debug_str = format!("{method:?}");
213 let display_str = format!("{method}");
214 assert_eq!(debug_str, display_str);
215 }
216
217 for &method in SUPPORTED_COMPRESSION_METHODS {
218 check_match(method);
219 }
220 }
221 }