expose pub(crate) methods to convert compression methods

This commit is contained in:
Danny McClanahan 2024-05-18 04:29:26 -04:00
parent e1c92e2f21
commit cf2d980612
No known key found for this signature in database
GPG key ID: 6105C10F1A199CC7
4 changed files with 31 additions and 37 deletions

View file

@ -90,13 +90,7 @@ impl CompressionMethod {
pub const AES: Self = CompressionMethod::Unsupported(99);
}
impl CompressionMethod {
/// Converts an u16 to its corresponding CompressionMethod
#[deprecated(
since = "0.5.7",
note = "use a constant to construct a compression method"
)]
pub const fn from_u16(val: u16) -> CompressionMethod {
#[allow(deprecated)]
pub(crate) const fn parse_from_u16(val: u16) -> Self {
match val {
0 => CompressionMethod::Stored,
#[cfg(feature = "_deflate-any")]
@ -111,18 +105,21 @@ impl CompressionMethod {
93 => CompressionMethod::Zstd,
#[cfg(feature = "aes-crypto")]
99 => CompressionMethod::Aes,
#[allow(deprecated)]
v => CompressionMethod::Unsupported(v),
}
}
/// Converts a CompressionMethod to a u16
/// Converts an u16 to its corresponding CompressionMethod
#[deprecated(
since = "0.5.7",
note = "to match on other compression methods, use a constant"
note = "use a constant to construct a compression method"
)]
pub const fn to_u16(self) -> u16 {
#[allow(deprecated)]
pub const fn from_u16(val: u16) -> CompressionMethod {
Self::parse_from_u16(val)
}
pub(crate) const fn serialize_to_u16(self) -> u16 {
match self {
CompressionMethod::Stored => 0,
#[cfg(feature = "_deflate-any")]
@ -137,10 +134,19 @@ impl CompressionMethod {
CompressionMethod::Zstd => 93,
#[cfg(feature = "lzma")]
CompressionMethod::Lzma => 14,
#[allow(deprecated)]
CompressionMethod::Unsupported(v) => v,
}
}
/// Converts a CompressionMethod to a u16
#[deprecated(
since = "0.5.7",
note = "to match on other compression methods, use a constant"
)]
pub const fn to_u16(self) -> u16 {
self.serialize_to_u16()
}
}
impl Default for CompressionMethod {
@ -180,10 +186,8 @@ mod test {
#[test]
fn from_eq_to() {
for v in 0..(u16::MAX as u32 + 1) {
#[allow(deprecated)]
let from = CompressionMethod::from_u16(v as u16);
#[allow(deprecated)]
let to = from.to_u16() as u32;
let from = CompressionMethod::parse_from_u16(v as u16);
let to = from.serialize_to_u16() as u32;
assert_eq!(v, to);
}
}
@ -191,12 +195,9 @@ mod test {
#[test]
fn to_eq_from() {
fn check_match(method: CompressionMethod) {
#[allow(deprecated)]
let to = method.to_u16();
#[allow(deprecated)]
let from = CompressionMethod::from_u16(to);
#[allow(deprecated)]
let back = from.to_u16();
let to = method.serialize_to_u16();
let from = CompressionMethod::parse_from_u16(to);
let back = from.serialize_to_u16();
assert_eq!(to, back);
}

View file

@ -1085,10 +1085,7 @@ fn central_header_to_zip_file_inner<R: Read>(
version_made_by: version_made_by as u8,
encrypted,
using_data_descriptor,
compression_method: {
#[allow(deprecated)]
CompressionMethod::from_u16(compression_method)
},
compression_method: CompressionMethod::parse_from_u16(compression_method),
compression_level: None,
last_modified_time: DateTime::try_from_msdos(last_mod_date, last_mod_time).ok(),
crc32,
@ -1171,8 +1168,7 @@ fn parse_extra_field(file: &mut ZipFileData) -> ZipResult<()> {
let mut out = [0u8];
reader.read_exact(&mut out)?;
let aes_mode = out[0];
#[allow(deprecated)]
let compression_method = CompressionMethod::from_u16(reader.read_u16_le()?);
let compression_method = CompressionMethod::parse_from_u16(reader.read_u16_le()?);
if vendor_id != 0x4541 {
return Err(ZipError::InvalidArchive("Invalid AES vendor"));

View file

@ -629,8 +629,7 @@ impl ZipFileData {
let is_utf8: bool = flags & (1 << 11) != 0;
/* flags & (1 << 3) != 0 */
let using_data_descriptor: bool = flags & (1 << 3) == 1 << 3;
#[allow(deprecated)]
let compression_method = crate::CompressionMethod::from_u16(compression_method);
let compression_method = crate::CompressionMethod::parse_from_u16(compression_method);
let file_name_length: usize = file_name_length.into();
let extra_field_length: usize = extra_field_length.into();
@ -733,8 +732,7 @@ impl ZipFileData {
magic: spec::LOCAL_FILE_HEADER_SIGNATURE,
version_made_by: self.version_needed(),
flags: self.flags(),
#[allow(deprecated)]
compression_method: self.compression_method.to_u16(),
compression_method: self.compression_method.serialize_to_u16(),
last_mod_time: last_modified_time.timepart(),
last_mod_date: last_modified_time.datepart(),
crc32: self.crc32,
@ -756,8 +754,7 @@ impl ZipFileData {
version_made_by: (self.system as u16) << 8 | (self.version_made_by as u16),
version_to_extract: self.version_needed(),
flags: self.flags(),
#[allow(deprecated)]
compression_method: self.compression_method.to_u16(),
compression_method: self.compression_method.serialize_to_u16(),
last_mod_time: last_modified_time.timepart(),
last_mod_date: last_modified_time.datepart(),
crc32: self.crc32,

View file

@ -1707,6 +1707,7 @@ fn update_aes_extra_data<W: Write + io::Seek>(
let mut buf = Vec::new();
/* TODO: implement this using the Block trait! */
// Extra field header ID.
buf.write_u16_le(0x9901)?;
// Data size.
@ -1718,8 +1719,7 @@ fn update_aes_extra_data<W: Write + io::Seek>(
// AES encryption strength.
buf.write_all(&[aes_mode as u8])?;
// Real compression method.
#[allow(deprecated)]
buf.write_u16_le(compression_method.to_u16())?;
buf.write_u16_le(compression_method.serialize_to_u16())?;
writer.write_all(&buf)?;