////------------------------------------------------------------------------------------------------------------------------------ private static void ValidateHeader(MusicMatchHeader header, MusicMatchHeader footer) { #if DEBUG if (header != null && footer != null) { if (header.MusicMatchVersion != footer.MusicMatchVersion) { throw new System.IO.InvalidDataException( String.Format("The MusicMatch header version {0} does not match footer version {1}.", header.MusicMatchVersion, footer.MusicMatchVersion)); } } if (header != null) { if (header.Padding1.Any(c => c != 0x00)) { throw new System.IO.InvalidDataException( String.Format( "The MusicMatch header has invalid padding1 bytes: {0}.", String.Join(" ", header.Padding1.Where(c => c != 0x00).Select(c => String.Format("0:x2"))))); } if (header.Padding2.Any(c => c != 0x00)) { throw new System.IO.InvalidDataException( String.Format( "The MusicMatch header has invalid padding2 bytes: {0}.", String.Join(" ", header.Padding2.Where(c => c != 0x00).Select(c => String.Format("0:x2"))))); } if (header.Padding3.Any(c => c != 0x00)) { throw new System.IO.InvalidDataException( String.Format( "The MusicMatch header has invalid padding3 bytes: {0}.", String.Join(" ", header.Padding3.Where(c => c != 0x00).Select(c => String.Format("0:x2"))))); } if (header.SpacePadding2.Any(c => c != 0x20)) { throw new System.IO.InvalidDataException( String.Format( "The MusicMatch header has invalid space padding bytes: {0}.", String.Join(" ", header.Padding1.Where(c => c != 0x20).Select(c => String.Format("0:x2"))))); } } if (footer != null) { } #else return; #endif }
private static MusicMatchHeader ReadHeaderFooter(StreamBuffer stream, long startHeaderPosition, long endHeaderPosition, IList<byte> identifierBytes, TagOrigin tagOrigin) { if (stream == null) throw new ArgumentNullException("stream"); stream.Position = startHeaderPosition; while (startHeaderPosition < endHeaderPosition) { int y = 0; while (stream.ReadByte() == identifierBytes[y++]) { startHeaderPosition++; if (y != identifierBytes.Count) continue; if (tagOrigin == TagOrigin.Start) { MusicMatchHeader header = new MusicMatchHeader { Position = stream.Position - identifierBytes.Count, Padding1 = new byte[2], Padding2 = new byte[2], Padding3 = new byte[2], SpacePadding2 = new byte[226] }; // 0x00 0x00 Padding stream.Read(header.Padding1, header.Padding1.Length); // <8-byte numerical ASCII string> header.XingEncoderVersion = stream.ReadString(8); // 0x00 0x00 Padding stream.Read(header.Padding2, header.Padding2.Length); // Xing encoder version <8-byte numerical ASCII string> header.MusicMatchVersion = stream.ReadString(8); // 0x00 0x00 Padding stream.Read(header.Padding3, header.Padding3.Length); // Space padding <226 * 0x20 > stream.Read(header.SpacePadding2, header.SpacePadding2.Length); ValidateHeader(header, null); return header; } if (tagOrigin == TagOrigin.End) { MusicMatchHeader footer = new MusicMatchHeader { Position = stream.Position - identifierBytes.Count, SpacePadding1 = new byte[13], SpacePadding2 = new byte[12] }; // Space padding <13 * 0x20> stream.Read(footer.SpacePadding1, 13); // version <4-byte numerical ASCII string> e.g. 3.05 footer.MusicMatchVersion = stream.ReadString(4); // Space padding <12 * 0x20> stream.Read(footer.SpacePadding2, 12); ValidateHeader(null, footer); return footer; } } startHeaderPosition++; } return null; }
/// <summary> /// Equals the specified <see cref="MusicMatchHeader"/>. /// </summary> /// <param name="hdr">The <see cref="MusicMatchHeader"/>.</param> /// <returns>true if equal; false otherwise.</returns> public bool Equals(MusicMatchHeader hdr) { if (ReferenceEquals(null, hdr)) return false; if (ReferenceEquals(this, hdr)) return true; return (hdr.Identifier == Identifier) && (hdr.XingEncoderVersion == XingEncoderVersion) && (hdr.MusicMatchVersion == MusicMatchVersion); }