@@ -87,12 +87,29 @@ struct MagicCookie {
8787}
8888
8989impl MagicCookie {
90- fn try_read < B : ReadBytes + FiniteStream > ( reader : & mut B ) -> Result < MagicCookie > {
91- // The magic cookie is either 24 or 48 bytes long.
92- if reader. byte_len ( ) != 24 && reader. byte_len ( ) != 48 {
90+ fn try_parse ( mut buf : & [ u8 ] ) -> Result < MagicCookie > {
91+ // The magic cookie must be atleast 24 bytes long.
92+ if buf. len ( ) < 24 {
93+ return unsupported_error ( "alac: magic cookie size too small" ) ;
94+ }
95+
96+ // The magic cookie may be preceeded by a FRMA atom. Skip over the FRMA atom.
97+ if buf[ 4 ..8 ] == * b"frma" {
98+ buf = & buf[ 12 ..] ;
99+ }
100+
101+ // The magic cookie may be preceeded by an ALAC atom. Skip over the ALAC atom.
102+ if buf[ 4 ..8 ] == * b"alac" {
103+ buf = & buf[ 12 ..] ;
104+ }
105+
106+ // The magic cookie must be either 24 or 48 bytes long.
107+ if buf. len ( ) != 24 && buf. len ( ) != 48 {
93108 return unsupported_error ( "alac: invalid magic cookie size" ) ;
94109 }
95110
111+ let mut reader = BufReader :: new ( buf) ;
112+
96113 let mut config = MagicCookie {
97114 frame_length : reader. read_be_u32 ( ) ?,
98115 compatible_version : reader. read_u8 ( ) ?,
@@ -416,7 +433,7 @@ impl AlacDecoder {
416433
417434 // Read the config (magic cookie).
418435 let config = if let Some ( extra_data) = & params. extra_data {
419- MagicCookie :: try_read ( & mut BufReader :: new ( extra_data) ) ?
436+ MagicCookie :: try_parse ( extra_data) ?
420437 }
421438 else {
422439 return unsupported_error ( "alac: missing extra data" ) ;
0 commit comments