Beispiel #1
0
        public Id3TagInfo Read(Stream inputStream)
        {
            if (inputStream == null)
            {
                throw new ArgumentNullException("inputStream");
            }

            if (!inputStream.CanRead)
            {
                throw new ID3IOException("Cannot read data stream.");
            }

            //
            //  Read the bytes from the I/O stream.
            //
            var tagInfo = new Id3TagInfo();
            byte[] rawTagContent;

            using (var reader = new BinaryReader(inputStream))
            {
                var headerBytes = new byte[10];
                reader.Read(headerBytes, 0, 10);

                var rawTagLength = AnalyseHeader(headerBytes, tagInfo);
                rawTagContent = new byte[rawTagLength];

                reader.Read(rawTagContent, 0, rawTagLength);
            }

            //
            //  Check for Unsynchronisation Bytes
            //
            byte[] tagContent;
            if (tagInfo.UnsynchronisationFlag)
            {
                // Scan for unsynchronisation bytes!
                tagContent = RemoveUnsyncBytes(rawTagContent);
            }
            else
            {
                tagContent = rawTagContent;
            }

            Stream tagStream = new MemoryStream(tagContent);
            var length = tagContent.Length;
            using (var reader = new BinaryReader(tagStream))
            {
                //
                //  Check for Extended Header
                //
                if (tagInfo.ExtendedHeaderAvailable)
                {
                    AnalyseExtendedHeader(reader, tagInfo);
                }

                //
                //  Read all frames
                //
                var frameBytes = new List<byte>();
                var pos = reader.BaseStream.Position;
                while ((pos + 10) < length)
                {
                    var continueReading = AnalyseFrame(reader,tagInfo,frameBytes);
                    if (!continueReading)
                    {
                        break;
                    }

                    pos = reader.BaseStream.Position;
                }

                //
                //  Check CRC if available
                //
                if (tagInfo.ExtendHeaderV3 != null && tagInfo.ExtendHeaderV3.CRCDataPresent)
                {
                    var tagData = frameBytes.ToArray();
                    var crc32Value = tagInfo.ExtendHeaderV3.CRC;

                    var crc32 = new Crc32(Crc32.DefaultPolynom);
                    var crcOK = crc32.Validate(tagData, crc32Value);

                    if (!crcOK)
                    {
                        throw new ID3TagException("The CRC32 validation failed!");
                    }
                }
            }

            return tagInfo;
        }
Beispiel #2
0
        public Id3TagInfo Read(Stream inputStream)
        {
            if (inputStream == null)
            {
                throw new ArgumentNullException("inputStream");
            }

            if (!inputStream.CanRead)
            {
                throw new ID3IOException("Cannot read data stream.");
            }

            //
            //  Read the bytes from the I/O stream.
            //
            var tagInfo = new Id3TagInfo();
            byte[] rawTagContent;

            using (var reader = new BinaryReader(inputStream))
            {
                var headerBytes = new byte[10];
                reader.Read(headerBytes, 0, 10);

                var rawTagLength = AnalyseHeader(headerBytes, tagInfo);
                rawTagContent = new byte[rawTagLength];

                reader.Read(rawTagContent, 0, rawTagLength);
            }

            //
            //  Check for Unsynchronisation Bytes
            //
            byte[] tagContent;
            if (tagInfo.UnsynchronisationFlag)
            {
                // Scan for unsynchronisation bytes!
                tagContent = RemoveUnsyncBytes(rawTagContent);
            }
            else
            {
                tagContent = rawTagContent;
            }

            Stream tagStream = new MemoryStream(tagContent);
            var length = tagContent.Length;
            using (var reader = new BinaryReader(tagStream))
            {
                //
                //  Check for Extended Header
                //
                if (tagInfo.ExtendedHeaderAvailable)
                {
                    AnalyseExtendedHeader(reader, tagInfo);
                }

                //
                //  Read the content
                //
                var frameBytes = new List<byte>();
                var pos = reader.BaseStream.Position;
                while ((pos + 10) < length)
                {
                    var continueReading = ReadContent(reader, tagInfo, frameBytes);
                    if (!continueReading)
                    {
                        break;
                    }

                    pos = reader.BaseStream.Position;
                }

                //
                //  Check CRC if available
                //
                if (tagInfo.ExtendedHeader != null && tagInfo.ExtendedHeader.CrcDataPresent)
                {
                    if (tagInfo.MajorVersion == 3)
                    {
                        var tagData = frameBytes.ToArray();
                        var crc32Value = tagInfo.ExtendedHeader.Crc32;

                        var crc32 = new Crc32(Crc32.DefaultPolynom);
                        var crcOk = crc32.Validate(tagData, crc32Value);

                        if (!crcOk)
                        {
                            throw new ID3TagException("The CRC32 validation failed!");
                        }
                    }
                    else
                    {
                        /*
                         *    c - CRC data present

                             If this flag is set, a CRC-32 [ISO-3309] data is included in the
                             extended header. The CRC is calculated on all the data between the
                             header and footer as indicated by the header's tag length field,
                             minus the extended header. Note that this includes the padding (if
                             there is any), but excludes the footer. The CRC-32 is stored as an
                             35 bit synchsafe integer, leaving the upper four bits always
                             zeroed.

                                Flag data length       $05
                                Total frame CRC    5 * %0xxxxxxx

                         */

                        // TODO: Implement the CRC32 check for ID3v2.4
                        throw new NotSupportedException("CRC32 check is not support for > ID3 V2.3");
                    }
                }
            }

            return tagInfo;
        }
Beispiel #3
0
        public Id3TagInfo Read(Stream inputStream)
        {
            if (inputStream == null)
            {
                var ex = new ArgumentNullException("inputStream");
                Logger.LogError(ex);

                throw ex;
            }

            if (!inputStream.CanRead)
            {
                var ex = new Id3IOException("Cannot read data stream.");
                Logger.LogError(ex);

                throw ex;
            }

            //
            //  Read the bytes from the I/O stream.
            //
            var tagInfo = new Id3TagInfo();
            byte[] rawTagContent;

            Logger.LogInfo("Reading ID3v2 tag from Stream.");
            using (var reader = new BinaryReader(inputStream))
            {
                Logger.LogInfo("Reading ID3v2 Header");
                var headerBytes = new byte[10];
                reader.Read(headerBytes, 0, 10);

                int rawTagLength = AnalyseHeader(headerBytes, tagInfo);
                long bytesLeft = reader.BaseStream.Length - reader.BaseStream.Position;
                if (rawTagLength > bytesLeft)
                {
                    var ex = new Id3TagException(
                        String.Format(
                            CultureInfo.InvariantCulture, "Specified tag size {0} exceeds actual content size {1}.",
                            rawTagLength, bytesLeft));

                    Logger.LogError(ex);
                    throw ex;
                }

                Logger.LogInfo("Reading ID3v2 Content");
                rawTagContent = new byte[rawTagLength];
                reader.Read(rawTagContent, 0, rawTagLength);
            }

            //
            //  Check for Unsynchronisation Bytes
            //
            byte[] tagContent;
            if (tagInfo.Unsynchronised)
            {
                // Scan for unsynchronisation bytes!
                Logger.LogInfo("Remove Unsynchronisatzion bytes.");
                tagContent = RemoveUnsyncBytes(rawTagContent);
            }
            else
            {
                tagContent = rawTagContent;
            }

            Stream tagStream = new MemoryStream(tagContent);
            int length = tagContent.Length;
            using (var reader = new BinaryReader(tagStream))
            {
                //
                //  Check for Extended Header
                //
                if (tagInfo.ExtendedHeaderAvailable)
                {
                    Logger.LogInfo("Analyse Extended Header");
                    AnalyseExtendedHeader(reader, tagInfo);
                }

                Logger.LogInfo(String.Format("Start reading ID3v2.{0} frame.", tagInfo.MajorVersion));

                //
                //  Read the content
                //
                var frameBytes = new List<byte>();
                long pos = reader.BaseStream.Position;
                while ((pos + 10) < length)
                {
                    Logger.LogInfo("Getting frame...");
                    bool continueReading = ReadContent(reader, tagInfo, frameBytes);
                    if (!continueReading)
                    {
                        break;
                    }

                    pos = reader.BaseStream.Position;
                }

                //
                //  Check CRC if available
                //
                if (tagInfo.ExtendedHeader != null && tagInfo.ExtendedHeader.CrcDataPresent)
                {
                    if (tagInfo.MajorVersion == 3)
                    {
                        byte[] tagData = frameBytes.ToArray();
                        ReadOnlyCollection<byte> crc32Value = tagInfo.ExtendedHeader.Crc32;

                        var crc32 = new Crc32(Crc32.DefaultPolynom);
                        bool crcOk = crc32.Validate(tagData, crc32Value);

                        if (!crcOk)
                        {
                            var ex = new Id3TagException("The CRC32 validation failed!");
                            Logger.LogError(ex);

                            throw ex;
                        }
                    }
                    else
                    {
                        /*
                         *    c - CRC data present

                             If this flag is set, a CRC-32 [ISO-3309] data is included in the
                             extended header. The CRC is calculated on all the data between the
                             header and footer as indicated by the header's tag length field,
                             minus the extended header. Note that this includes the padding (if
                             there is any), but excludes the footer. The CRC-32 is stored as an
                             35 bit synchsafe integer, leaving the upper four bits always
                             zeroed.

                                Flag data length       $05
                                Total frame CRC    5 * %0xxxxxxx

                         */

                        // TODO: Implement the CRC32 check for ID3v2.4
                        var ex = new NotSupportedException("CRC32 check is not support for > ID3 V2.3");
                        Logger.LogError(ex);

                        throw ex;
                    }
                }
            }

            return tagInfo;
        }