From 213dc3d8266cc6d84711643eb7ba44b32e18ee34 Mon Sep 17 00:00:00 2001 From: koushiro Date: Wed, 4 Feb 2026 02:21:33 +0800 Subject: [PATCH 1/2] deps: upgrade nom6 to nom8 --- Cargo.toml | 2 +- src/lib.rs | 3 - src/parse/audio.rs | 109 ++++++++++++++++----------------- src/parse/mod.rs | 121 ++++++++++++++++++------------------ src/parse/script.rs | 145 +++++++++++++++++++++----------------------- src/parse/video.rs | 87 +++++++++++++------------- 6 files changed, 224 insertions(+), 243 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6e3de9c..a89843e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,4 +34,4 @@ default = ["std"] std = ["nom/std"] [dependencies] -nom = { version = "6.1", default-features = false, features = ["alloc"] } +nom = { version = "8", default-features = false, features = ["alloc"] } diff --git a/src/lib.rs b/src/lib.rs index ffb6a44..3c15707 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,9 +8,6 @@ #[cfg(not(feature = "std"))] extern crate alloc; -#[macro_use] -extern crate nom; - mod parse; pub use nom::{ diff --git a/src/parse/audio.rs b/src/parse/audio.rs index 968473b..213bc69 100644 --- a/src/parse/audio.rs +++ b/src/parse/audio.rs @@ -1,4 +1,8 @@ -use nom::{Err as NomErr, IResult, Needed, number::streaming::be_u8}; +use nom::{ + Err as NomErr, IResult, Needed, + error::{Error, ErrorKind}, + number::streaming::be_u8, +}; /// The tag data part of `audio` FLV tag, including `tag data header` and `tag data body`. #[derive(Clone, Debug, PartialEq)] @@ -12,15 +16,9 @@ pub struct AudioTag<'a> { impl<'a> AudioTag<'a> { /// Parse audio tag data. pub fn parse(input: &'a [u8], size: usize) -> IResult<&'a [u8], AudioTag<'a>> { - do_parse!( - input, - // parse audio tag header - header: call!(AudioTagHeader::parse, size) >> - // parse audio tag body - body: call!(AudioTagBody::parse, size - 1) >> - - (AudioTag { header, body }) - ) + let (input, header) = AudioTagHeader::parse(input, size)?; + let (input, body) = AudioTagBody::parse(input, size.saturating_sub(1))?; + Ok((input, AudioTag { header, body })) } } @@ -109,45 +107,45 @@ impl AudioTagHeader { return Err(NomErr::Incomplete(Needed::new(1))); } - let (remain, (sound_format, sound_rate, sound_size, sound_type)) = try_parse!( - input, - bits!(tuple!( - // parse sound format - switch!(take_bits!(4u8), - 0 => value!(SoundFormat::PcmPlatformEndian) | - 1 => value!(SoundFormat::ADPCM) | - 2 => value!(SoundFormat::MP3) | - 3 => value!(SoundFormat::PcmLittleEndian) | - 4 => value!(SoundFormat::Nellymoser16kHzMono) | - 5 => value!(SoundFormat::Nellymoser8kHzMono) | - 6 => value!(SoundFormat::Nellymoser) | - 7 => value!(SoundFormat::PcmALaw) | - 8 => value!(SoundFormat::PcmMuLaw) | - 9 => value!(SoundFormat::Reserved) | - 10 => value!(SoundFormat::AAC) | - 11 => value!(SoundFormat::Speex) | - 14 => value!(SoundFormat::MP3_8kHz) | - 15 => value!(SoundFormat::DeviceSpecific) - ), - // parse sound rate - switch!(take_bits!(2u8), - 0 => value!(SoundRate::_5_5KHZ) | - 1 => value!(SoundRate::_11KHZ) | - 2 => value!(SoundRate::_22KHZ) | - 3 => value!(SoundRate::_44KHZ) - ), - // parse sound sample size - switch!(take_bits!(1u8), - 0 => value!(SoundSize::_8Bit) | - 1 => value!(SoundSize::_16Bit) - ), - // parse sound type - switch!(take_bits!(1u8), - 0 => value!(SoundType::Mono) | - 1 => value!(SoundType::Stereo) - ) - )) - ); + let (remain, byte) = be_u8(input)?; + + let sound_format = match byte >> 4 { + 0 => SoundFormat::PcmPlatformEndian, + 1 => SoundFormat::ADPCM, + 2 => SoundFormat::MP3, + 3 => SoundFormat::PcmLittleEndian, + 4 => SoundFormat::Nellymoser16kHzMono, + 5 => SoundFormat::Nellymoser8kHzMono, + 6 => SoundFormat::Nellymoser, + 7 => SoundFormat::PcmALaw, + 8 => SoundFormat::PcmMuLaw, + 9 => SoundFormat::Reserved, + 10 => SoundFormat::AAC, + 11 => SoundFormat::Speex, + 14 => SoundFormat::MP3_8kHz, + 15 => SoundFormat::DeviceSpecific, + _ => SoundFormat::Reserved, + }; + + let sound_rate = match (byte >> 2) & 0b11 { + 0 => SoundRate::_5_5KHZ, + 1 => SoundRate::_11KHZ, + 2 => SoundRate::_22KHZ, + 3 => SoundRate::_44KHZ, + _ => SoundRate::_5_5KHZ, + }; + + let sound_size = match (byte >> 1) & 0b1 { + 0 => SoundSize::_8Bit, + 1 => SoundSize::_16Bit, + _ => SoundSize::_8Bit, + }; + + let sound_type = match byte & 0b1 { + 0 => SoundType::Mono, + 1 => SoundType::Stereo, + _ => SoundType::Mono, + }; Ok((remain, AudioTagHeader { sound_format, sound_rate, sound_size, sound_type })) } @@ -199,13 +197,12 @@ pub fn aac_audio_packet(input: &[u8], size: usize) -> IResult<&[u8], AACAudioPac return Err(NomErr::Incomplete(Needed::new(1))); } - let (_, packet_type) = try_parse!( - input, - switch!(be_u8, - 0 => value!(AACPacketType::SequenceHeader) | - 1 => value!(AACPacketType::Raw) - ) - ); + let (remain, packet_type_byte) = be_u8(input)?; + let packet_type = match packet_type_byte { + 0 => AACPacketType::SequenceHeader, + 1 => AACPacketType::Raw, + _ => return Err(NomErr::Error(Error::new(remain, ErrorKind::Switch))), + }; Ok((&input[size..], AACAudioPacket { packet_type, aac_data: &input[1..size] })) } diff --git a/src/parse/mod.rs b/src/parse/mod.rs index 66e3638..4eabe22 100644 --- a/src/parse/mod.rs +++ b/src/parse/mod.rs @@ -10,13 +10,25 @@ use alloc::vec::Vec; use nom::{ IResult, - number::streaming::{be_u8, be_u24, be_u32}, + Err as NomErr, + bytes::streaming::{tag, take}, + combinator::complete, + error::{Error, ErrorKind}, + multi::many0, + number::streaming::{be_u8, be_u32}, + Parser, }; pub use self::{audio::*, script::*, video::*}; const FLV_HEADER_SIGNATURE: [u8; 3] = [0x46, 0x4c, 0x56]; +fn be_u24(input: &[u8]) -> IResult<&[u8], u32> { + let (input, bytes) = take(3usize)(input)?; + let value = (u32::from(bytes[0]) << 16) | (u32::from(bytes[1]) << 8) | u32::from(bytes[2]); + Ok((input, value)) +} + /// The FLV file structure, including header and body. #[derive(Clone, Debug, PartialEq)] pub struct FlvFile<'a> { @@ -29,15 +41,9 @@ pub struct FlvFile<'a> { impl<'a> FlvFile<'a> { /// Parse FLV file. pub fn parse(input: &'a [u8]) -> IResult<&'a [u8], FlvFile<'a>> { - do_parse!( - input, - // parse file header - header: call!(FlvFileHeader::parse) >> - // parse file body - body: call!(FlvFileBody::parse) >> - - (FlvFile { header, body }) - ) + let (input, header) = FlvFileHeader::parse(input)?; + let (input, body) = FlvFileBody::parse(input)?; + Ok((input, FlvFile { header, body })) } } @@ -64,26 +70,22 @@ pub struct FlvFileHeader { impl FlvFileHeader { /// Parse FLV file header. pub fn parse(input: &[u8]) -> IResult<&[u8], FlvFileHeader> { - do_parse!( + let (input, _) = tag(FLV_HEADER_SIGNATURE.as_slice())(input)?; + let (input, version) = be_u8(input)?; + let (input, flags) = be_u8(input)?; + let (input, data_offset) = be_u32(input)?; + + Ok(( input, - // FLV Signature - tag!(FLV_HEADER_SIGNATURE) >> - // FLV File Version - version: be_u8 >> - // Flags the presents whether `audio` tags or `video` tags are exist. - flags: be_u8 >> - // The length of this header in bytes - data_offset: be_u32 >> - - (FlvFileHeader { + FlvFileHeader { signature: FLV_HEADER_SIGNATURE, version, flags, has_audio: flags & 4 == 4, has_video: flags & 1 == 1, data_offset, - }) - ) + }, + )) } } @@ -100,15 +102,10 @@ impl<'a> FlvFileBody<'a> { // https://github.com/Geal/nom/issues/790 - many0 returns Incomplete in weird cases. /// Parse FLV file body. pub fn parse(input: &'a [u8]) -> IResult<&'a [u8], FlvFileBody<'a>> { - do_parse!( - input, - // The first previous tag size. - first_previous_tag_size: be_u32 >> - // FLV Tag and the size of the tag. - tags: many0!(complete!(tuple!(call!(FlvTag::parse), be_u32))) >> + let (input, first_previous_tag_size) = be_u32(input)?; + let (input, tags) = many0(complete((|i| FlvTag::parse(i), be_u32))).parse(input)?; - (FlvFileBody { first_previous_tag_size, tags }) - ) + Ok((input, FlvFileBody { first_previous_tag_size, tags })) } } @@ -129,15 +126,9 @@ pub struct FlvTag<'a> { impl<'a> FlvTag<'a> { /// Parse FLV tag. pub fn parse(input: &'a [u8]) -> IResult<&'a [u8], FlvTag<'a>> { - do_parse!( - input, - // parse tag header - header: call!(FlvTagHeader::parse) >> - // parse tag data - data: call!(FlvTagData::parse, header.tag_type, header.data_size as usize) >> - - (FlvTag { header, data }) - ) + let (input, header) = FlvTagHeader::parse(input)?; + let (input, data) = FlvTagData::parse(input, header.tag_type, header.data_size as usize)?; + Ok((input, FlvTag { header, data })) } } @@ -175,30 +166,27 @@ pub enum FlvTagType { impl FlvTagHeader { /// Parse FLV tag header. pub fn parse(input: &[u8]) -> IResult<&[u8], FlvTagHeader> { - do_parse!( + let (input, tag_type_byte) = be_u8(input)?; + let tag_type = match tag_type_byte { + 8 => FlvTagType::Audio, + 9 => FlvTagType::Video, + 18 => FlvTagType::Script, + _ => return Err(NomErr::Error(Error::new(input, ErrorKind::Switch))), + }; + let (input, data_size) = be_u24(input)?; + let (input, timestamp) = be_u24(input)?; + let (input, timestamp_extended) = be_u8(input)?; + let (input, stream_id) = be_u24(input)?; + + Ok(( input, - // Tag Type - tag_type: switch!(be_u8, - 8 => value!(FlvTagType::Audio) | - 9 => value!(FlvTagType::Video) | - 18 => value!(FlvTagType::Script) - ) >> - // The size of the tag's data part - data_size: be_u24 >> - // The timestamp (in milliseconds) of the tag - timestamp: be_u24 >> - // Extension of the timestamp field to form a SI32 value - timestamp_extended: be_u8 >> - // The id of stream - stream_id: be_u24 >> - - (FlvTagHeader { + FlvTagHeader { tag_type, data_size, timestamp: (u32::from(timestamp_extended) << 24) + timestamp, stream_id, - }) - ) + }, + )) } } @@ -221,9 +209,18 @@ impl<'a> FlvTagData<'a> { size: usize, ) -> IResult<&'a [u8], FlvTagData<'a>> { match tag_type { - FlvTagType::Audio => map!(input, call!(AudioTag::parse, size), FlvTagData::Audio), - FlvTagType::Video => map!(input, call!(VideoTag::parse, size), FlvTagData::Video), - FlvTagType::Script => map!(input, call!(ScriptTag::parse, size), FlvTagData::Script), + FlvTagType::Audio => { + let (input, tag) = AudioTag::parse(input, size)?; + Ok((input, FlvTagData::Audio(tag))) + } + FlvTagType::Video => { + let (input, tag) = VideoTag::parse(input, size)?; + Ok((input, FlvTagData::Video(tag))) + } + FlvTagType::Script => { + let (input, tag) = ScriptTag::parse(input, size)?; + Ok((input, FlvTagData::Script(tag))) + } } } } diff --git a/src/parse/script.rs b/src/parse/script.rs index e5cac0a..cbb8b8a 100644 --- a/src/parse/script.rs +++ b/src/parse/script.rs @@ -3,8 +3,15 @@ use alloc::vec::Vec; use core::str; use nom::{ + Err as NomErr, IResult, + Parser, + bytes::streaming::tag, + combinator::map_res, + error::{Error, ErrorKind}, + multi::{count, length_data, many0}, number::streaming::{be_f64, be_i16, be_u8, be_u16, be_u32}, + sequence::terminated, }; const SCRIPT_DATA_VALUE_STRING_TYPE: [u8; 1] = [0x02]; @@ -26,18 +33,10 @@ pub struct ScriptTag<'a> { impl<'a> ScriptTag<'a> { /// Parse script tag data. pub fn parse(input: &'a [u8], _size: usize) -> IResult<&'a [u8], ScriptTag<'a>> { - do_parse!( - input, - // ScriptTagValue.Type = 2 (String) - tag!(SCRIPT_DATA_VALUE_STRING_TYPE) >> - // Method or object name. - name: call!(ScriptDataValue::parse_string) >> - // AMF arguments or object properties. - // ScriptTagValue.Type = 8 (ECMA array) - value: call!(ScriptDataValue::parse) >> - - (ScriptTag { name, value }) - ) + let (input, _) = tag(SCRIPT_DATA_VALUE_STRING_TYPE.as_slice())(input)?; + let (input, name) = ScriptDataValue::parse_string(input)?; + let (input, value) = ScriptDataValue::parse(input)?; + Ok((input, ScriptTag { name, value })) } } @@ -73,23 +72,50 @@ pub enum ScriptDataValue<'a> { impl<'a> ScriptDataValue<'a> { /// Parse script tag data value. pub fn parse(input: &'a [u8]) -> IResult<&'a [u8], ScriptDataValue<'a>> { - switch!(input, - // parse script value type - be_u8, - // parse script data value - 0 => map!(Self::parse_number, ScriptDataValue::Number) | - 1 => map!(Self::parse_boolean, |v| ScriptDataValue::Boolean(v != 0)) | - 2 => map!(Self::parse_string, ScriptDataValue::String) | - 3 => map!(Self::parse_object, ScriptDataValue::Object) | - 4 => value!(ScriptDataValue::MovieClip) | - 5 => value!(ScriptDataValue::Null) | - 6 => value!(ScriptDataValue::Undefined) | - 7 => map!(Self::parse_reference, ScriptDataValue::Reference) | - 8 => map!(Self::parse_ecma_array, ScriptDataValue::ECMAArray) | - 10 => map!(Self::parse_strict_array, ScriptDataValue::StrictArray) | - 11 => map!(Self::parse_date, ScriptDataValue::Date) | - 12 => map!(Self::parse_long_string, ScriptDataValue::LongString) - ) + let original_input = input; + let (input, value_type) = be_u8(input)?; + match value_type { + 0 => { + let (input, number) = Self::parse_number(input)?; + Ok((input, ScriptDataValue::Number(number))) + } + 1 => { + let (input, v) = Self::parse_boolean(input)?; + Ok((input, ScriptDataValue::Boolean(v != 0))) + } + 2 => { + let (input, s) = Self::parse_string(input)?; + Ok((input, ScriptDataValue::String(s))) + } + 3 => { + let (input, object) = Self::parse_object(input)?; + Ok((input, ScriptDataValue::Object(object))) + } + 4 => Ok((input, ScriptDataValue::MovieClip)), + 5 => Ok((input, ScriptDataValue::Null)), + 6 => Ok((input, ScriptDataValue::Undefined)), + 7 => { + let (input, reference) = Self::parse_reference(input)?; + Ok((input, ScriptDataValue::Reference(reference))) + } + 8 => { + let (input, array) = Self::parse_ecma_array(input)?; + Ok((input, ScriptDataValue::ECMAArray(array))) + } + 10 => { + let (input, array) = Self::parse_strict_array(input)?; + Ok((input, ScriptDataValue::StrictArray(array))) + } + 11 => { + let (input, date) = Self::parse_date(input)?; + Ok((input, ScriptDataValue::Date(date))) + } + 12 => { + let (input, s) = Self::parse_long_string(input)?; + Ok((input, ScriptDataValue::LongString(s))) + } + _ => Err(NomErr::Error(Error::new(original_input, ErrorKind::Switch))), + } } /// Parse script tag data number value. @@ -104,36 +130,26 @@ impl<'a> ScriptDataValue<'a> { /// Parse script tag data string value. pub fn parse_string(input: &[u8]) -> IResult<&[u8], &str> { - map_res!(input, length_data!(be_u16), str::from_utf8) + map_res(length_data(be_u16), str::from_utf8).parse(input) } /// Parse script tag data object value. pub fn parse_object(input: &'a [u8]) -> IResult<&'a [u8], Vec>> { - terminated!( - input, - // parse object properties - many0!(Self::parse_object_property), - // parse object end marker - call!(Self::parse_object_end_marker) - ) + terminated(many0(Self::parse_object_property), Self::parse_object_end_marker).parse(input) } /// Parse script tag data object property. fn parse_object_property(input: &'a [u8]) -> IResult<&'a [u8], ScriptDataObjectProperty<'a>> { - do_parse!( - input, - // parse object property name - name: call!(Self::parse_string) >> - // parse object property value - value: call!(Self::parse) >> - - (ScriptDataObjectProperty { name, value }) - ) + if input.starts_with(&OBJECT_END_MARKER) { + return Err(NomErr::Error(Error::new(input, ErrorKind::Tag))); + } + let (input, (name, value)) = (Self::parse_string, Self::parse).parse(input)?; + Ok((input, ScriptDataObjectProperty { name, value })) } /// Parse script tag data object end marker. fn parse_object_end_marker(input: &[u8]) -> IResult<&[u8], &[u8]> { - tag!(input, OBJECT_END_MARKER) + tag(OBJECT_END_MARKER.as_slice())(input) } /// Parse script tag data reference value. @@ -146,48 +162,27 @@ impl<'a> ScriptDataValue<'a> { input: &'a [u8], ) -> IResult<&'a [u8], Vec>> { // The list contains approximately ECMA Array Length number of items. - do_parse!( - input, - // parse ECMA array length - _length: be_u32 >> - // parse object Properties and Object End marker - object: call!(Self::parse_object) >> - - (object) - ) + let (input, _) = be_u32(input)?; + Self::parse_object(input) } /// Parse script tag data strict array value. pub fn parse_strict_array(input: &'a [u8]) -> IResult<&'a [u8], Vec>> { // The list shall contain Strict Array Length number of values. // No terminating record follows the list. - do_parse!( - input, - // parse strict array length - length: be_u32 >> - // parse values - value: count!(call!(Self::parse), length as usize) >> - - (value) - ) + let (input, length) = be_u32(input)?; + count(Self::parse, length as usize).parse(input) } /// Parse script tag data date value. pub fn parse_date(input: &[u8]) -> IResult<&[u8], ScriptDataDate> { - do_parse!( - input, - // Number of milliseconds since UNIX_EPOCH. - date_time: be_f64 >> - // Local time offset in minutes from UTC. - local_date_time_offset: be_i16 >> - - (ScriptDataDate { date_time, local_date_time_offset }) - ) + let (input, (date_time, local_date_time_offset)) = (be_f64, be_i16).parse(input)?; + Ok((input, ScriptDataDate { date_time, local_date_time_offset })) } /// Parse script tag data long string value. pub fn parse_long_string(input: &[u8]) -> IResult<&[u8], &str> { - map_res!(input, length_data!(be_u32), str::from_utf8) + map_res(length_data(be_u32), str::from_utf8).parse(input) } } diff --git a/src/parse/video.rs b/src/parse/video.rs index 86362a6..245010b 100644 --- a/src/parse/video.rs +++ b/src/parse/video.rs @@ -1,8 +1,18 @@ use nom::{ Err as NomErr, IResult, Needed, - number::streaming::{be_i24, be_u8}, + bytes::streaming::take, + number::streaming::be_u8, }; +fn be_i24(input: &[u8]) -> IResult<&[u8], i32> { + let (input, bytes) = take(3usize)(input)?; + let mut value = (i32::from(bytes[0]) << 16) | (i32::from(bytes[1]) << 8) | i32::from(bytes[2]); + if (value & 0x0080_0000) != 0 { + value |= !0x00ff_ffff; + } + Ok((input, value)) +} + /// The tag data part of `video` FLV tag, including `tag data header` and `tag data body`. #[derive(Clone, Debug, PartialEq)] pub struct VideoTag<'a> { @@ -15,15 +25,9 @@ pub struct VideoTag<'a> { impl<'a> VideoTag<'a> { /// Parse video tag data. pub fn parse(input: &'a [u8], size: usize) -> IResult<&'a [u8], VideoTag<'a>> { - do_parse!( - input, - // parse video tag data header - header: call!(VideoTagHeader::parse, size) >> - // parse video tag data body - body: call!(VideoTagBody::parse, size - 1) >> - - (VideoTag {header, body }) - ) + let (input, header) = VideoTagHeader::parse(input, size)?; + let (input, body) = VideoTagBody::parse(input, size.saturating_sub(1))?; + Ok((input, VideoTag { header, body })) } } @@ -80,30 +84,25 @@ impl VideoTagHeader { return Err(NomErr::Incomplete(Needed::new(1))); } - let (remain, (frame_type, codec_id)) = try_parse!( - input, - bits!(tuple!( - // parse frame type - switch!(take_bits!(4u8), - 1 => value!(FrameType::Key) | - 2 => value!(FrameType::Inter) | - 3 => value!(FrameType::DisposableInter) | - 4 => value!(FrameType::Generated) | - 5 => value!(FrameType::Command) | - _ => value!(FrameType::Unknown) - ), - // parse code id - switch!(take_bits!(4u8), - 2 => value!(CodecID::SorensonH263) | - 3 => value!(CodecID::Screen1) | - 4 => value!(CodecID::VP6) | - 5 => value!(CodecID::VP6Alpha) | - 6 => value!(CodecID::Screen2) | - 7 => value!(CodecID::AVC) | - _ => value!(CodecID::Unknown) - ) - )) - ); + let (remain, byte) = be_u8(input)?; + let frame_type = match byte >> 4 { + 1 => FrameType::Key, + 2 => FrameType::Inter, + 3 => FrameType::DisposableInter, + 4 => FrameType::Generated, + 5 => FrameType::Command, + _ => FrameType::Unknown, + }; + + let codec_id = match byte & 0x0f { + 2 => CodecID::SorensonH263, + 3 => CodecID::Screen1, + 4 => CodecID::VP6, + 5 => CodecID::VP6Alpha, + 6 => CodecID::Screen2, + 7 => CodecID::AVC, + _ => CodecID::Unknown, + }; Ok((remain, VideoTagHeader { frame_type, codec_id })) } @@ -166,18 +165,14 @@ pub fn avc_video_packet(input: &[u8], size: usize) -> IResult<&[u8], AvcVideoPac return Err(NomErr::Incomplete(Needed::new(4))); } - let (_, (packet_type, composition_time)) = try_parse!( - input, - tuple!( - switch!(be_u8, - 0 => value!(AvcPacketType::SequenceHeader) | - 1 => value!(AvcPacketType::NALU) | - 2 => value!(AvcPacketType::EndOfSequence) | - _ => value!(AvcPacketType::Unknown) - ), - be_i24 - ) - ); + let (remain, packet_type_byte) = be_u8(input)?; + let packet_type = match packet_type_byte { + 0 => AvcPacketType::SequenceHeader, + 1 => AvcPacketType::NALU, + 2 => AvcPacketType::EndOfSequence, + _ => AvcPacketType::Unknown, + }; + let (_, composition_time) = be_i24(remain)?; Ok(( &input[size..], From a87d4afaa6bb44dd05e4dc0f95112c11d7725a9c Mon Sep 17 00:00:00 2001 From: koushiro Date: Wed, 4 Feb 2026 02:25:32 +0800 Subject: [PATCH 2/2] fix fmtg --- src/parse/mod.rs | 10 ++++------ src/parse/script.rs | 22 ++++++++++------------ src/parse/video.rs | 6 +----- 3 files changed, 15 insertions(+), 23 deletions(-) diff --git a/src/parse/mod.rs b/src/parse/mod.rs index 4eabe22..3e07122 100644 --- a/src/parse/mod.rs +++ b/src/parse/mod.rs @@ -9,14 +9,12 @@ mod video; use alloc::vec::Vec; use nom::{ - IResult, - Err as NomErr, + Err as NomErr, IResult, Parser, bytes::streaming::{tag, take}, combinator::complete, error::{Error, ErrorKind}, multi::many0, number::streaming::{be_u8, be_u32}, - Parser, }; pub use self::{audio::*, script::*, video::*}; @@ -212,15 +210,15 @@ impl<'a> FlvTagData<'a> { FlvTagType::Audio => { let (input, tag) = AudioTag::parse(input, size)?; Ok((input, FlvTagData::Audio(tag))) - } + }, FlvTagType::Video => { let (input, tag) = VideoTag::parse(input, size)?; Ok((input, FlvTagData::Video(tag))) - } + }, FlvTagType::Script => { let (input, tag) = ScriptTag::parse(input, size)?; Ok((input, FlvTagData::Script(tag))) - } + }, } } } diff --git a/src/parse/script.rs b/src/parse/script.rs index cbb8b8a..95a5940 100644 --- a/src/parse/script.rs +++ b/src/parse/script.rs @@ -3,9 +3,7 @@ use alloc::vec::Vec; use core::str; use nom::{ - Err as NomErr, - IResult, - Parser, + Err as NomErr, IResult, Parser, bytes::streaming::tag, combinator::map_res, error::{Error, ErrorKind}, @@ -78,42 +76,42 @@ impl<'a> ScriptDataValue<'a> { 0 => { let (input, number) = Self::parse_number(input)?; Ok((input, ScriptDataValue::Number(number))) - } + }, 1 => { let (input, v) = Self::parse_boolean(input)?; Ok((input, ScriptDataValue::Boolean(v != 0))) - } + }, 2 => { let (input, s) = Self::parse_string(input)?; Ok((input, ScriptDataValue::String(s))) - } + }, 3 => { let (input, object) = Self::parse_object(input)?; Ok((input, ScriptDataValue::Object(object))) - } + }, 4 => Ok((input, ScriptDataValue::MovieClip)), 5 => Ok((input, ScriptDataValue::Null)), 6 => Ok((input, ScriptDataValue::Undefined)), 7 => { let (input, reference) = Self::parse_reference(input)?; Ok((input, ScriptDataValue::Reference(reference))) - } + }, 8 => { let (input, array) = Self::parse_ecma_array(input)?; Ok((input, ScriptDataValue::ECMAArray(array))) - } + }, 10 => { let (input, array) = Self::parse_strict_array(input)?; Ok((input, ScriptDataValue::StrictArray(array))) - } + }, 11 => { let (input, date) = Self::parse_date(input)?; Ok((input, ScriptDataValue::Date(date))) - } + }, 12 => { let (input, s) = Self::parse_long_string(input)?; Ok((input, ScriptDataValue::LongString(s))) - } + }, _ => Err(NomErr::Error(Error::new(original_input, ErrorKind::Switch))), } } diff --git a/src/parse/video.rs b/src/parse/video.rs index 245010b..edb0923 100644 --- a/src/parse/video.rs +++ b/src/parse/video.rs @@ -1,8 +1,4 @@ -use nom::{ - Err as NomErr, IResult, Needed, - bytes::streaming::take, - number::streaming::be_u8, -}; +use nom::{Err as NomErr, IResult, Needed, bytes::streaming::take, number::streaming::be_u8}; fn be_i24(input: &[u8]) -> IResult<&[u8], i32> { let (input, bytes) = take(3usize)(input)?;