Ejemplo n.º 1
0
 protected void Read(byte[] tagBytes)
 {
     using (var ms = new MemoryStream(tagBytes))
     {
         m_TagInfo = m_Controller.Read(ms);
     }
 }
Ejemplo n.º 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:
                    var ex = new Id3TagException("Unknown version!");
                    Logger.LogError(ex);

                    throw ex;
            }

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

            return tagInfo;
        }
Ejemplo n.º 3
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:
                    var ex = new Id3TagException("This major revision is not supported!");
                    Logger.LogError(ex);
                    throw ex;
            }

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

                    throw ex;
                }
            }

            return container;
        }
Ejemplo n.º 4
0
        private static void EncodeV3(Id3TagInfo tagInfo, TagContainer container)
        {
            TagDescriptorV3 descriptor = container.GetId3V23Descriptor();

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

            if (descriptor.ExtendedHeader)
            {
                tagInfo.ExtendedHeader = ExtendedTagHeaderV3.Create(descriptor.PaddingSize, descriptor.CrcDataPresent,
                                                                    descriptor.Crc);
            }
        }
Ejemplo n.º 5
0
        private static void EncodeV4(Id3TagInfo tagInfo, TagContainer container)
        {
            TagDescriptorV4 descriptor = container.GetId3V24Descriptor();

            tagInfo.Experimental = descriptor.ExperimentalIndicator;
            tagInfo.ExtendedHeaderAvailable = descriptor.ExtendedHeader;
            tagInfo.Unsynchronised = descriptor.Unsynchronisation;
            tagInfo.HasFooter = descriptor.Footer;

            if (descriptor.ExtendedHeader)
            {
                tagInfo.ExtendedHeader = ExtendedTagHeaderV4.Create(descriptor.UpdateTag, descriptor.CrcDataPresent,
                                                                    descriptor.RestrictionPresent,
                                                                    descriptor.Restriction, descriptor.Crc);
            }
        }
Ejemplo n.º 6
0
        private static TagContainer DecodeV3Tag(Id3TagInfo info)
        {
            var container = new TagContainerV3();
            TagDescriptorV3 descriptor = container.Tag;

            descriptor.SetHeaderOptions(info.Unsynchronised, info.ExtendedHeaderAvailable, info.Experimental);

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

            return container;
        }
Ejemplo n.º 7
0
        private static TagContainer DecodeV4Tag(Id3TagInfo info)
        {
            var container = new TagContainerV4();
            TagDescriptorV4 descriptor = container.Tag;

            descriptor.SetHeaderOptions(info.Unsynchronised, info.ExtendedHeaderAvailable, info.Experimental,
                                        info.HasFooter);
            if (info.ExtendedHeaderAvailable)
            {
                ExtendedTagHeaderV4 extendedHeader = info.ExtendedHeader.ConvertToV24();
                descriptor.SetExtendedHeader(extendedHeader.CrcDataPresent, extendedHeader.UpdateTag,
                                             extendedHeader.RestrictionPresent, extendedHeader.Restriction);

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

            return container;
        }
Ejemplo n.º 8
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);
 }
Ejemplo n.º 9
0
        private static int AnalyseHeader(byte[] headerBytes, Id3TagInfo tagInfo)
        {
            // Check ID3 pattern
            bool id3PatternFound = (headerBytes[0] == 0x49) && (headerBytes[1] == 0x44) && (headerBytes[2] == 0x33);

            if (!id3PatternFound)
            {
                var ex = new Id3HeaderNotFoundException();
                Logger.LogError(ex);

                throw ex;
            }

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

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

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

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

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

                bool footerFlag = (flagByte & 0x10) == 0x10;
                tagInfo.HasFooter = footerFlag;
            }

            Array.Copy(headerBytes, 6, sizeBytes, 0, 4);
            int size = Utils.Convert7BitEncodedToInt32(sizeBytes);

            return size;
        }
Ejemplo n.º 10
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;
        }
Ejemplo n.º 11
0
        public void Remove(Stream input, Stream output)
        {
            if (input == null)
            {
                var ex = new ArgumentNullException("input");
                Logger.LogError(ex);

                throw ex;
            }
            if (output == null)
            {
                var ex = new ArgumentNullException("output");
                Logger.LogError(ex);

                throw ex;
            }

            var tagId = new byte[3];
            input.Read(tagId, 0, tagId.Length);
            input.Position = 0;

            bool id3TagFound = IsId3V2TagAvailable(tagId);
            if (!id3TagFound)
            {
                Logger.LogInfo("ID3 V2.3 tag not found!");
                throw new Id3HeaderNotFoundException("ID3V2 tag not found!");
            }

            Logger.LogInfo("ID3 V2.3 tag found. Remove it.");

            // Read the ID3 Tag and ignore it...
            var headerBytes = new byte[10];
            input.Read(headerBytes, 0, headerBytes.Length);
            var tagInfo = new Id3TagInfo();
            int length = AnalyseHeader(headerBytes, tagInfo);

            // Ignore the header bytes and write the audio content to the output stream
            input.Position = input.Position + length;

            Logger.LogInfo("Write audio file");
            Utils.WriteAudioStream(output, input, input.Length);
        }
Ejemplo n.º 12
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);

            bool 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
                //
                string frameId = Encoding.ASCII.GetString(frameIdBytes);
                int size = Utils.CalculateSize(sizeBytes);
                Logger.LogInfo(String.Format("Frame found : ID = {0}, Size = {1}", frameId, size));

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

                    Logger.LogError(ex);
                    throw ex;
                }

                var payloadBytes = new byte[size];
                reader.Read(payloadBytes, 0, 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:
                        var ex = new Id3TagException("Not supported major revision found!");
                        Logger.LogError(ex);

                        throw ex;
                }

                tagInfo.Frames.Add(frame);

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

                return true;
            }

            return false;
        }
Ejemplo n.º 13
0
        private static void AnalyseExtendedHeader(BinaryReader reader, Id3TagInfo tagInfo)
        {
            var extendedHeaderSize = new byte[4];
            reader.Read(extendedHeaderSize, 0, 4);

            int size = Utils.CalculateSize(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:
                    var ex = new Id3TagException("Unknown extended header found! ");
                    Logger.LogError(ex);
                    throw ex;
            }

            tagInfo.ExtendedHeader = extendedHeader;
        }