Esempio n. 1
0
        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;
        }
Esempio n. 2
0
        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;
        }
Esempio n. 3
0
        /// <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);
        }
Esempio n. 4
0
        /// <summary>
        /// uses the <see cref="Search"/> class to find the next id3 / mp3 frame start at the buffer.
        /// </summary>
        /// <returns>Returns the search result.</returns>
        Search FindFrame()
        {
            // initialize the search and run it at the buffer
            var search = new Search();

            while (true)
            {
                // fill the buffer
                if (!m_Reader.EnsureBuffer(1024) && (m_Reader.Available < 4))
                {
                    if (m_Reader.Available == 0)
                    {
                        return(null);
                    }

                    // end of stream
                    var buffer = m_Reader.GetBuffer(m_Reader.Available);
                    InvalidData(buffer);
                    return(null);
                }

                // run the search
                {
                    if (m_Reader.Contains(search))
                    {
                        return(search);
                    }

                    // nothing found, enqueue invalid data...
                    var buffer = m_Reader.GetBuffer(m_Reader.Available - 2);
                    InvalidData(buffer);

                    // .. and start new search
                    search = new Search();
                }
            }
        }
Esempio n. 5
0
        /// <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);
        }