mirror of
https://github.com/Serial-ATA/lofty-rs
synced 2024-09-20 14:21:57 +00:00
ID3v2: Expose {synch_u32, unsynch_u32}
This commit is contained in:
parent
efedf0f443
commit
ec4fb07042
7 changed files with 72 additions and 57 deletions
|
@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
- **A proc macro for file creation**:
|
||||
- With the new `lofty_attr` crate, file creation has been simplified significantly.
|
||||
It is available for both internal and external usage.
|
||||
- **ID3v2**: Exposed internal functions `id3::v2::util::{synch_u32, unsynch_u32}`
|
||||
|
||||
### Changed
|
||||
- **TaggedFile**: `tag{_mut}` no longer takes a reference to `TagType`
|
||||
|
|
|
@ -699,7 +699,7 @@ impl FileType {
|
|||
|
||||
// TODO: APE tags in the beginning of the file
|
||||
pub(crate) fn from_buffer_inner(buf: &[u8]) -> (Option<Self>, Option<u32>) {
|
||||
use crate::id3::v2::unsynch_u32;
|
||||
use crate::id3::v2::util::unsynch_u32;
|
||||
|
||||
// Start out with an empty return: (File type, id3 size)
|
||||
// Only one can be set
|
||||
|
|
|
@ -80,7 +80,7 @@ where
|
|||
|
||||
// unsynch the frame size if necessary
|
||||
if synchsafe {
|
||||
size = crate::id3::v2::unsynch_u32(size);
|
||||
size = crate::id3::v2::util::unsynch_u32(size);
|
||||
}
|
||||
|
||||
let frame_id = FrameID::new(id_str)?;
|
||||
|
|
|
@ -8,10 +8,11 @@
|
|||
//! * [Frame]
|
||||
|
||||
mod flags;
|
||||
pub(crate) mod util;
|
||||
pub mod util;
|
||||
|
||||
use crate::error::{ID3v2Error, ID3v2ErrorKind, Result};
|
||||
use crate::macros::err;
|
||||
use util::unsynch_u32;
|
||||
|
||||
use std::io::Read;
|
||||
|
||||
|
@ -66,24 +67,6 @@ pub enum ID3v2Version {
|
|||
V4,
|
||||
}
|
||||
|
||||
// https://github.com/polyfloyd/rust-id3/blob/e142ec656bf70a8153f6e5b34a37f26df144c3c1/src/stream/unsynch.rs#L18-L20
|
||||
pub(crate) fn unsynch_u32(n: u32) -> u32 {
|
||||
n & 0xFF | (n & 0xFF00) >> 1 | (n & 0xFF_0000) >> 2 | (n & 0xFF00_0000) >> 3
|
||||
}
|
||||
|
||||
#[cfg(feature = "id3v2")]
|
||||
// https://github.com/polyfloyd/rust-id3/blob/e142ec656bf70a8153f6e5b34a37f26df144c3c1/src/stream/unsynch.rs#L9-L15
|
||||
pub(crate) fn synch_u32(n: u32) -> Result<u32> {
|
||||
if n > 0x1000_0000 {
|
||||
err!(TooMuchData);
|
||||
}
|
||||
|
||||
let mut x: u32 = n & 0x7F | (n & 0xFFFF_FF80) << 1;
|
||||
x = x & 0x7FFF | (x & 0xFFFF_8000) << 1;
|
||||
x = x & 0x7F_FFFF | (x & 0xFF80_0000) << 1;
|
||||
Ok(x)
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub(crate) struct ID3v2Header {
|
||||
#[cfg(feature = "id3v2")]
|
||||
|
|
|
@ -1,12 +1,14 @@
|
|||
//! Utilities for working with ID3v2 tags
|
||||
|
||||
pub(crate) mod text_utils;
|
||||
#[cfg(feature = "id3v2")]
|
||||
pub(crate) mod upgrade;
|
||||
|
||||
#[cfg(feature = "id3v2")]
|
||||
use crate::error::{ID3v2Error, ID3v2ErrorKind, Result};
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(feature = "id3v2")] {
|
||||
pub(crate) mod upgrade;
|
||||
|
||||
#[cfg(feature = "id3v2")]
|
||||
pub(in crate::id3::v2) fn unsynch_content(content: &[u8]) -> Result<Vec<u8>> {
|
||||
use crate::error::{ID3v2Error, ID3v2ErrorKind, Result};
|
||||
|
||||
pub(in crate::id3::v2) fn unsynch_content(content: &[u8]) -> Result<Vec<u8>> {
|
||||
let mut unsynch_content = Vec::new();
|
||||
|
||||
let mut discard = false;
|
||||
|
@ -48,6 +50,35 @@ pub(in crate::id3::v2) fn unsynch_content(content: &[u8]) -> Result<Vec<u8>> {
|
|||
}
|
||||
|
||||
Ok(unsynch_content)
|
||||
}
|
||||
|
||||
/// Create a synchsafe integer
|
||||
///
|
||||
/// See [`FrameFlags::unsynchronisation`](crate::id3::v2::FrameFlags::unsynchronisation) for an explanation.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// `n` doesn't fit in 28 bits
|
||||
// https://github.com/polyfloyd/rust-id3/blob/e142ec656bf70a8153f6e5b34a37f26df144c3c1/src/stream/unsynch.rs#L9-L15
|
||||
pub fn synch_u32(n: u32) -> Result<u32> {
|
||||
if n > 0x1000_0000 {
|
||||
crate::macros::err!(TooMuchData);
|
||||
}
|
||||
|
||||
let mut x: u32 = n & 0x7F | (n & 0xFFFF_FF80) << 1;
|
||||
x = x & 0x7FFF | (x & 0xFFFF_8000) << 1;
|
||||
x = x & 0x7F_FFFF | (x & 0xFF80_0000) << 1;
|
||||
Ok(x)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Unsynchronise a synchsafe integer
|
||||
///
|
||||
/// See [`FrameFlags::unsynchronisation`](crate::id3::v2::FrameFlags::unsynchronisation) for an explanation.
|
||||
// https://github.com/polyfloyd/rust-id3/blob/e142ec656bf70a8153f6e5b34a37f26df144c3c1/src/stream/unsynch.rs#L18-L20
|
||||
pub fn unsynch_u32(n: u32) -> u32 {
|
||||
n & 0xFF | (n & 0xFF00) >> 1 | (n & 0xFF_0000) >> 2 | (n & 0xFF00_0000) >> 3
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use crate::error::{ID3v2Error, ID3v2ErrorKind, Result};
|
||||
use crate::id3::v2::frame::{FrameFlags, FrameRef, FrameValue};
|
||||
use crate::id3::v2::synch_u32;
|
||||
use crate::id3::v2::util::synch_u32;
|
||||
|
||||
use std::io::Write;
|
||||
|
||||
|
|
|
@ -6,8 +6,8 @@ use crate::error::Result;
|
|||
use crate::file::FileType;
|
||||
use crate::id3::find_id3v2;
|
||||
use crate::id3::v2::frame::FrameRef;
|
||||
use crate::id3::v2::synch_u32;
|
||||
use crate::id3::v2::tag::Id3v2TagRef;
|
||||
use crate::id3::v2::util::synch_u32;
|
||||
use crate::macros::err;
|
||||
use crate::probe::Probe;
|
||||
|
||||
|
|
Loading…
Reference in a new issue