/// <summary>Initializes a new instance of the <see cref="ID3v2FrameHeader"/> class.</summary> /// <param name="header">The header.</param> /// <param name="reader">The reader.</param> /// <exception cref="NotSupportedException"></exception> public ID3v2FrameHeader(ID3v2Header header, DataFrameReader reader) { TagHeader = header; switch (header.Version) { case 2: ParseVersion2(reader.Read(0, 6)); break; case 3: ParseVersion3(reader.Read(0, 10)); break; case 4: ParseVersion4(reader.Read(0, 10)); break; default: throw new NotSupportedException(string.Format("Unsupported ID3v2 Version {0}", header.Version)); } }
/// <summary>Initializes a new instance of the <see cref="ID3v2Frame" /> class.</summary> /// <param name="header">The header.</param> /// <param name="reader">The reader.</param> /// <exception cref="ArgumentNullException">Header.</exception> /// <exception cref="NotSupportedException"></exception> public ID3v2Frame(ID3v2Header header, DataFrameReader reader) { if (reader == null) { throw new ArgumentNullException("Reader"); } m_Header = new ID3v2FrameHeader(header, reader); // prepare content (has to be decoded, decrypted, decompressed, ... m_Content = reader.Read(m_Header.HeaderSize, m_Header.ContentSize); switch (header.Version) { case 2: /*nothing to do, raw plain content data*/ break; case 3: ParseVersion3(reader); break; case 4: ParseVersion4(reader); break; default: throw new NotSupportedException(string.Format("ID3v2.{0} is not supported!", header.Version)); } // copy raw data and remove from reader m_Data = reader.GetBuffer(m_Header.HeaderSize + m_Header.ContentSize); }
bool ParseVersion3(DataFrameReader reader) { if (!reader.EnsureBuffer(4)) { return false; } // calc size byte[] sizeBytes = reader.Read(0, 4); int size = 0; for (int i = 0; i < 4; i++) { size = (size << 8) | sizeBytes[i]; } size += 4; // get data if (!reader.EnsureBuffer(size)) { return false; } m_Data = reader.GetBuffer(size); // get flags m_Flags = ID3v2ExtendedHeaderFlags.FromID3v23(m_Data); return true; }
/// <summary> /// Parses the specified buffer starting at index to load all data for this frame. /// </summary> /// <param name="reader">FrameReader to read from.</param> public override bool Parse(DataFrameReader reader) { if (reader == null) { throw new ArgumentNullException("Reader"); } if (!reader.EnsureBuffer(10)) { return(false); } m_Data = reader.Read(0, 10); ParseData(); return(true); }
bool ParseVersion4(DataFrameReader reader) { if (!reader.EnsureBuffer(4)) { return false; } // calc size int size = ID3v2DeUnsync.Int32(reader.Read(0, 4), 0); // get data if (!reader.EnsureBuffer(size)) { return false; } m_Data = reader.GetBuffer(size); // get flags m_Flags = ID3v2ExtendedHeaderFlags.FromID3v24(m_Data); return true; }
/// <summary> /// Parses the specified stream to load all fields for this instance. /// </summary> /// <param name="reader">FrameReader to read from.</param> public override bool Parse(DataFrameReader reader) { if (reader == null) { throw new ArgumentNullException("Reader"); } if (!reader.EnsureBuffer(4)) { return(false); } var headerData = reader.Read(0, 4); Header = new MP3AudioFrameHeader(headerData); if (Header.Validation != MP3AudioFrameHeadervalidation.Valid) { return(false); } var dataLength = Header.Length; if (dataLength == 0) { return(false); } if (!reader.EnsureBuffer(dataLength)) { return(false); } m_Data = reader.Read(0, dataLength); // check next header if (reader.EnsureBuffer(dataLength + 4)) { var nextHeaderBuffer = reader.Read(dataLength, 4); var next = new MP3AudioFrameHeader(nextHeaderBuffer); if (next.Validation != MP3AudioFrameHeadervalidation.Valid) { if ((nextHeaderBuffer[0] == 'I') && (nextHeaderBuffer[1] == 'D') && (nextHeaderBuffer[2] == '3')) { // ID3 v2 tag incoming } else if ((nextHeaderBuffer[0] == 'T') && (nextHeaderBuffer[1] == 'A') && (nextHeaderBuffer[2] == 'G')) { // ID3 v1 tag incoming } else { // next header is invalid, check if the padding bit is set incorrectly // there is a high pobability that the padding bit is invalid if // the framestart is not directly after our buffer but one byte late var newStart = dataLength + (Header.Padding ? -1 : 1); nextHeaderBuffer = reader.Read(newStart, 4); next = new MP3AudioFrameHeader(nextHeaderBuffer); if (next.Validation == MP3AudioFrameHeadervalidation.Valid) { if (!Header.Padding) { // frame has a padding byte but the header padding bit is not set m_Data = reader.Read(0, newStart); } else { // frame has no padding byte but the header padding bit is set m_Data = reader.Read(0, newStart); } InvalidPaddingCorrected = true; } } } } reader.Remove(m_Data.Length); return(true); }