Пример #1
0
 protected void Read(byte[] tagBytes)
 {
     using (var ms = new MemoryStream(tagBytes))
     {
         m_TagInfo = m_Controller.Read(ms);
     }
 }
Пример #2
0
        public Id3TagInfo Encode(TagContainer container)
        {
            var tagInfo = new Id3TagInfo();

            switch (container.TagVersion)
            {
                case TagVersion.Id3V23:
                    tagInfo.MajorVersion = 3;
                    tagInfo.Revision = 0;
                    EncodeV3(tagInfo, container);
                    break;
                case TagVersion.Id3V24:
                    tagInfo.MajorVersion = 4;
                    tagInfo.Revision = 0;
                    EncodeV4(tagInfo, container);
                    break;
                default:
                    throw new ID3TagException("Unknown version!");
            }

            foreach (var frame in container)
            {
                var rawFrame = frame.Convert(container.TagVersion);
                tagInfo.Frames.Add(rawFrame);
            }

            return tagInfo;
        }
Пример #3
0
        public TagContainer Decode(Id3TagInfo info)
        {
            var container = new TagContainer();
            var descriptor = container.Tag;

            // Decode the ID3 Tag info
            var majorVersion = info.MajorVersion;
            var revision = info.Revision;

            descriptor.SetVersion(majorVersion, revision);
            descriptor.SetHeaderFlags(info.UnsynchronisationFlag, info.ExtendedHeaderAvailable, info.Experimental);

            if (info.ExtendedHeaderAvailable)
            {
                var extendedHeader = info.ExtendHeaderV3;
                descriptor.SetExtendedHeader(extendedHeader.PaddingSize, extendedHeader.CRCDataPresent);
                if (extendedHeader.CRCDataPresent)
                {
                    descriptor.SetCrc32(extendedHeader.CRC);
                }
            }

            foreach (var rawFrame in info.Frames)
            {
                //
                //  Analyse the frame ID
                //
                var frame = AnalyseFrameId(rawFrame);
                if (frame != null)
                {
                    frame.Import(rawFrame);
                    container.Add(frame);
                }
                else
                {
                    throw new ID3TagException("Frame analysing failed!");
                }
            }

            return container;
        }
Пример #4
0
        /// <summary>
        /// Decodes a low level tag into a high level.
        /// </summary>
        /// <param name="info">the low level tag.</param>
        /// <param name="codePage">Default code page for Ansi encoding. Pass 0 to use default system encoding.</param>
        /// <returns>the high level tag representation.</returns>
        public TagContainer Decode(Id3TagInfo info, int codePage)
        {
            TagContainer container;
            switch (info.MajorVersion)
            {
                case 3:
                    container = DecodeV3Tag(info);
                    break;
                case 4:
                    container = DecodeV4Tag(info);
                    break;
                default:
                    throw new ID3TagException("This major revision is not supported!");
            }

            //
            //  Import the frames
            //
            IFrameCreationService creationService = new FrameContainer();
            foreach (var rawFrame in info.Frames)
            {
                //
                //  Analyse the frame ID
                //
                var frame = AnalyseFrameId(rawFrame, creationService);
                if (frame != null)
                {
                    frame.Import(rawFrame, codePage);
                    container.Add(frame);
                }
                else
                {
                    throw new ID3TagException("Frame analysing failed!");
                }
            }

            return container;
        }
Пример #5
0
        public Id3TagInfo Encode(TagContainer container)
        {
            var tagInfo = new Id3TagInfo();
            var tag = container.Tag;

            tagInfo.MajorVersion = tag.MajorVersion;
            tagInfo.Revision = tag.Revision;
            tagInfo.Experimental = tag.ExperimentalIndicator;
            tagInfo.UnsynchronisationFlag = tag.Unsynchronisation;
            tagInfo.ExtendedHeaderAvailable = tag.ExtendedHeader;
            if (tagInfo.ExtendedHeaderAvailable)
            {
                tagInfo.ExtendHeaderV3 = ExtendedTagHeaderV3.Create(tag.PaddingSize, tag.CrcDataPresent, tag.Crc);
            }

            foreach (var frame in container)
            {
                var rawFrame = frame.Convert();
                tagInfo.Frames.Add(rawFrame);
            }

            return tagInfo;
        }
Пример #6
0
        private static bool ReadContent(BinaryReader reader, Id3TagInfo tagInfo, List<byte> frameBytes)
        {
            /*
             * 3.4.   ID3v2 footer

               To speed up the process of locating an ID3v2 tag when searching from
               the end of a file, a footer can be added to the tag. It is REQUIRED
               to add a footer to an appended tag, i.e. a tag located after all
               audio data. The footer is a copy of the header, but with a different
               identifier.

                 ID3v2 identifier           "3DI"
                 ID3v2 version              $04 00
                 ID3v2 flags                %abcd0000
                 ID3v2 size             4 * %0xxxxxxx

             */

            //
            //  Read the header ( footer or frame )
            //
            var frameHeader = new byte[10];
            reader.Read(frameHeader, 0, 10);

            var isFooter = frameHeader[0] == 0x33 && frameHeader[1] == 0x44 && frameHeader[2] == 0x49;
            if (!isFooter)
            {
                //
                //  Frame found!
                //
                var frameIdBytes = new byte[4];
                var sizeBytes = new byte[4];
                var flagsBytes = new byte[2];

                Array.Copy(frameHeader, 0, frameIdBytes, 0, 4);
                Array.Copy(frameHeader, 4, sizeBytes, 0, 4);
                Array.Copy(frameHeader, 8, flagsBytes, 0, 2);

                if (frameIdBytes[0] == 0 ||
                    frameIdBytes[1] == 0 ||
                    frameIdBytes[2] == 0 ||
                    frameIdBytes[3] == 0)
                {
                    // No valid frame. Padding bytes?
                    return false;
                }

                //
                //  Read the frame bytes
                //
				var frameId = Encoding.ASCII.GetString(frameIdBytes);
                var size = Utils.CalculateFrameHeaderSize(sizeBytes);
                var payloadBytes = new byte[size];
                reader.Read(payloadBytes, 0, (int) size);

                RawFrame frame;
                switch (tagInfo.MajorVersion)
                {
                    case 3:
                        frame = RawFrame.CreateV3Frame(frameId, flagsBytes, payloadBytes);
                        break;
                    case 4:
                        frame = RawFrame.CreateV4Frame(frameId, flagsBytes, payloadBytes);
                        break;
                    default:
                        throw new ID3TagException("Not supported major revision found!");
                }

                tagInfo.Frames.Add(frame);

                // Add the frames to the buffer ( for CRC computing )
                frameBytes.AddRange(frameHeader);
                frameBytes.AddRange(payloadBytes);

                return true;
            }

            return false;
        }
Пример #7
0
        private static void AnalyseExtendedHeader(BinaryReader reader, Id3TagInfo tagInfo)
        {
            var extendedHeaderSize = new byte[4];
            reader.Read(extendedHeaderSize, 0, 4);

            var size = Utils.CalculateExtendedHeaderSize(extendedHeaderSize);
            byte[] content;

            ExtendedHeader extendedHeader;
            switch (tagInfo.MajorVersion)
            {
                case 3:
                    //
                    // Read the ID3v2.3 extended header
                    //
                    content = new byte[size];
                    reader.Read(content, 0, size);
                    extendedHeader = ExtendedTagHeaderV3.Create(content);
                    break;
                case 4:
                    //
                    //  Read the ID3v2.4 extended header
                    //
                    // We already read the length of the header...
                    size = size - 4;

                    content = new byte[size];
                    reader.Read(content, 0, size);
                    extendedHeader = ExtendedTagHeaderV4.Create(content);
                    break;
                default:
                    throw new ID3TagException("Unknown extended header found! ");
            }

            tagInfo.ExtendedHeader = extendedHeader;
        }
Пример #8
0
        private static int AnalyseHeader(byte[] headerBytes, Id3TagInfo tagInfo)
        {
            // Check ID3 pattern
            var id3PatternFound = (headerBytes[0] == 0x49) && (headerBytes[1] == 0x44) && (headerBytes[2] == 0x33);

            if (!id3PatternFound)
            {
                throw new ID3HeaderNotFoundException();
            }

            var majorVersion = Convert.ToInt32(headerBytes[3]);
            var revision = Convert.ToInt32(headerBytes[4]);
            var flagByte = headerBytes[5];
            var sizeBytes = new byte[4];

            // Analyse the header...
            tagInfo.MajorVersion = majorVersion;
            tagInfo.Revision = revision;

            var unsynchronisationFlag = (flagByte & 0x80) == 0x80;
            var extendedHeaderFlag = (flagByte & 0x40) == 0x40;
            var experimentalFlag = (flagByte & 0x20) == 0x20;

            tagInfo.UnsynchronisationFlag = unsynchronisationFlag;
            tagInfo.ExtendedHeaderAvailable = extendedHeaderFlag;
            tagInfo.Experimental = experimentalFlag;

            if (majorVersion == 4)
            {
                //
                //  ID3V2.4 tag found! check for footer.
                //

                var footerFlag = (flagByte & 0x10) == 0x10;
                tagInfo.FooterFlag = footerFlag;
            }

            Array.Copy(headerBytes, 6, sizeBytes, 0, 4);
            var size = Utils.CalculateTagHeaderSize(sizeBytes);

            return size;
        }
Пример #9
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;
        }
Пример #10
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;
        }
Пример #11
0
        private static bool AnalyseFrame(BinaryReader reader, Id3TagInfo tagInfo,List<byte> frameBytes)
        {
            var frameHeader = new byte[10];
            reader.Read(frameHeader, 0, 10);

            var frameIDBytes = new byte[4];
            var sizeBytes = new byte[4];
            var flagsBytes = new byte[2];

            Array.Copy(frameHeader, 0, frameIDBytes, 0, 4);
            Array.Copy(frameHeader, 4, sizeBytes, 0, 4);
            Array.Copy(frameHeader, 8, flagsBytes, 0, 2);

            if (frameIDBytes[0] == 0 ||
                frameIDBytes[1] == 0 ||
                frameIDBytes[2] == 0 ||
                frameIDBytes[3] == 0)
            {
                // No valid frame. Padding bytes?
                return false;
            }

            //
            //  Read the frame bytes
            //
            var frameID = Utils.GetFrameID(frameIDBytes);
            var size = Utils.CalculateFrameHeaderSize(sizeBytes);
            var payloadBytes = new byte[size];
            reader.Read(payloadBytes, 0, (int)size);

            var frame = RawFrame.CreateFrame(frameID, flagsBytes, payloadBytes);
            tagInfo.Frames.Add(frame);

            // Add the frames to the buffer ( for CRC computing )
            frameBytes.AddRange(frameHeader);
            frameBytes.AddRange(payloadBytes);

            return true;
        }
Пример #12
0
        private static void EncodeV3(Id3TagInfo tagInfo, TagContainer container)
        {
            var descriptor = container.GetId3V23Descriptor();

            tagInfo.Experimental = descriptor.ExperimentalIndicator;
            tagInfo.ExtendedHeaderAvailable = descriptor.ExtendedHeader;
            tagInfo.UnsynchronisationFlag = descriptor.Unsynchronisation;

            if (descriptor.ExtendedHeader)
            {
                tagInfo.ExtendedHeader = ExtendedTagHeaderV3.Create(descriptor.PaddingSize, descriptor.CrcDataPresent,
                                                                    descriptor.Crc);
            }
        }
Пример #13
0
        private static void EncodeV4(Id3TagInfo tagInfo, TagContainer container)
        {
            var descriptor = container.GetId3V24Descriptor();

            tagInfo.Experimental = descriptor.ExperimentalIndicator;
            tagInfo.ExtendedHeaderAvailable = descriptor.ExtendedHeader;
            tagInfo.UnsynchronisationFlag = descriptor.Unsynchronisation;
            tagInfo.FooterFlag = descriptor.Footer;

            if (descriptor.ExtendedHeader)
            {
                tagInfo.ExtendedHeader = ExtendedTagHeaderV4.Create(descriptor.UpdateTag, descriptor.CrcDataPresent,
                                                                    descriptor.RestrictionPresent,
                                                                    descriptor.Restriction, descriptor.Crc);
            }
        }
Пример #14
0
        private static TagContainer DecodeV3Tag(Id3TagInfo info)
        {
            var container = new TagContainerV3();
            var descriptor = container.Tag;

            descriptor.SetHeaderFlags(info.UnsynchronisationFlag, info.ExtendedHeaderAvailable, info.Experimental);

            if (info.ExtendedHeaderAvailable)
            {
                var extendedHeader = info.ExtendedHeader.ConvertToV23();
                descriptor.SetExtendedHeader(extendedHeader.PaddingSize, extendedHeader.CrcDataPresent);
                if (extendedHeader.CrcDataPresent)
                {
                    descriptor.SetCrc32(extendedHeader.Crc32);
                }
            }

            return container;
        }
Пример #15
0
 /// <summary>
 /// Decodes a low level tag into a high level.
 /// </summary>
 /// <param name="info">the low level tag.</param>
 /// <returns>the high level tag representation.</returns>
 public TagContainer Decode(Id3TagInfo info)
 {
     return Decode(info, 0);
 }
Пример #16
0
        private static TagContainer DecodeV4Tag(Id3TagInfo info)
        {
            var container = new TagContainerV4();
            var descriptor = container.Tag;

            descriptor.SetHeaderFlags(info.UnsynchronisationFlag, info.ExtendedHeaderAvailable, info.Experimental,
                                      info.FooterFlag);
            if (info.ExtendedHeaderAvailable)
            {
                var extendedHeader = info.ExtendedHeader.ConvertToV24();
                descriptor.SetExtendedHeader(extendedHeader.CrcDataPresent, extendedHeader.UpdateTag,
                                             extendedHeader.RestrictionPresent, extendedHeader.Restriction);

                if (extendedHeader.CrcDataPresent)
                {
                    descriptor.SetCrc32(extendedHeader.Crc32);
                }
            }

            return container;
        }