Esempio n. 1
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;
        }
Esempio n. 2
0
        /// <summary>
        /// Reads the ID3v1 tag using specified code page for text encoding.
        /// </summary>
        /// <param name="inputStream">the input stream (i.e. the file.)</param>
        /// <param name="codePage">The code page for text encoding.</param>
        /// <returns>An ID3v1 container.</returns>
        public Id3V1Tag Read(Stream inputStream, int codePage)
        {
            if (!inputStream.CanSeek)
            {
                var ex = new Id3TagException("Cannot read ID3v1 tag because the stream does not support seek.");
                Logger.LogError(ex);

                throw ex;
            }

            if (inputStream.Length < 128)
            {
                var ex = new Id3IOException("Cannot read ID3v1 tag because the stream is too short");
                Logger.LogError(ex);

                throw ex;
            }

            //
            //  Read the last 128 Bytes from the stream (ID3v1 Position)
            //
            var tagBytes = new byte[128];
            inputStream.Seek(-128, SeekOrigin.End);
            inputStream.Read(tagBytes, 0, 128);

            bool isValidTag = CheckID(tagBytes);
            if (!isValidTag)
            {
                var ex = new Id3HeaderNotFoundException("TAG header not found");
                Logger.LogError(ex);

                throw ex;
            }

            Id3V1Tag v1Tag = ExtractTag(tagBytes, codePage);
            return v1Tag;
        }
Esempio n. 3
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;
        }
Esempio n. 4
0
        /// <summary>
        /// Reads the ID3v1 tag using specified code page for text encoding.
        /// </summary>
        /// <param name="file">the file.</param>
        /// <param name="codePage">The code page.</param>
        /// <returns>An ID3v1 container.</returns>
        public Id3V1Tag Read(FileInfo file, int codePage)
        {
            bool fileExists = file.Exists;
            if (!fileExists)
            {
                var ex = new FileNotFoundException("File " + file.FullName + " not found!.");
                Logger.LogError(ex);

                throw ex;
            }

            FileStream fs = null;
            Id3V1Tag info;
            try
            {
                fs = File.Open(file.FullName, FileMode.Open, FileAccess.Read);
                info = Read(fs, codePage);
            }
            catch (Id3TagException id3TagException)
            {
                Logger.LogError(id3TagException);
                throw;
            }
            catch (Exception ex)
            {
                var tagEx = new Id3TagException("Unknown Exception during reading.", ex);
                Logger.LogError(tagEx);

                throw tagEx;
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
            }

            return info;
        }
Esempio n. 5
0
        private static void CheckStreams(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;
            }

            if (!input.CanSeek)
            {
                var ex = new Id3TagException("Cannot write ID3V1 tag because the source does not support seek.");
                Logger.LogError(ex);

                throw ex;
            }

            if (!output.CanWrite)
            {
                var ex = new Id3TagException("Cannot write ID3V1 tag because the output does not support writing.");
                Logger.LogError(ex);

                throw ex;
            }
        }
Esempio n. 6
0
        private static byte[] GetTagHeader(TagContainer tagContainer)
        {
            var tagHeader = new byte[10];
            tagHeader[0] = 0x49;
            tagHeader[1] = 0x44;
            tagHeader[2] = 0x33;

            switch (tagContainer.TagVersion)
            {
                case TagVersion.Id3V23:
                    TagDescriptorV3 descriptorV3 = tagContainer.GetId3V23Descriptor();
                    tagHeader[3] = 0x03;
                    tagHeader[4] = 0x00;

                    if (descriptorV3.Unsynchronisation)
                    {
                        tagHeader[5] |= 0x80;
                    }

                    if (descriptorV3.ExtendedHeader)
                    {
                        tagHeader[5] |= 0x40;
                    }

                    if (descriptorV3.ExperimentalIndicator)
                    {
                        tagHeader[5] |= 0x20;
                    }
                    break;

                case TagVersion.Id3V24:
                    TagDescriptorV4 descriptorV4 = tagContainer.GetId3V24Descriptor();
                    tagHeader[3] = 0x04;
                    tagHeader[4] = 0x00;

                    if (descriptorV4.Unsynchronisation)
                    {
                        tagHeader[5] |= 0x80;
                    }

                    if (descriptorV4.ExtendedHeader)
                    {
                        tagHeader[5] |= 0x40;
                    }

                    if (descriptorV4.ExperimentalIndicator)
                    {
                        tagHeader[5] |= 0x20;
                    }

                    if (descriptorV4.Footer)
                    {
                        tagHeader[5] |= 0x10;
                    }

                    break;
                default:
                    var ex = new Id3TagException("Unknown version!");
                    Logger.LogError(ex);

                    throw ex;
            }

            return tagHeader;
        }
Esempio n. 7
0
        private static bool ValidateTag(TagContainer tagContainer, out string message)
        {
            Validator validator;

            Logger.LogInfo(String.Format("Writing version {0}", tagContainer.TagVersion));
            switch (tagContainer.TagVersion)
            {
                case TagVersion.Id3V23:
                    validator = new Id3V2Validator();
                    break;
                case TagVersion.Id3V24:
                    validator = new Id3V24Validator();
                    break;
                default:
                    var ex = new Id3TagException("Unknown version!");
                    Logger.LogError(ex);
                    throw ex;
            }
            Logger.LogInfo("Validating tag content..");
            bool isValid = validator.Validate(tagContainer);

            Logger.LogInfo(String.Format("IsValid = {0}", isValid));
            if (isValid)
            {
                message = String.Empty;
            }
            else
            {
                message = validator.FailureDescription;
            }

            return isValid;
        }
Esempio n. 8
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;
        }
Esempio n. 9
0
        public FileState DetermineTagStatus(Stream audioStream)
        {
            if (audioStream == null)
            {
                var ex = new ArgumentNullException("audioStream");
                Logger.LogError(ex);

                throw ex;
            }

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

                throw ex;
            }

            if (!audioStream.CanSeek)
            {
                var ex = new Id3TagException("Cannot read ID3v1 tag because the stream does not support seek.");
                Logger.LogError(ex);

                throw ex;
            }

            if (audioStream.Length < 128)
            {
                var ex = new Id3IOException("Cannot read ID3v1 tag because the stream is too short");
                Logger.LogError(ex);

                throw ex;
            }

            bool id3V1Found;
            bool id3V2Found;
            //
            // Search for ID3v2 tags
            //
            using (var reader = new BinaryReader(audioStream))
            {
                //
                // Search for ID3v2 tags
                //
                byte[] tagId = reader.ReadBytes(3);
                id3V2Found = IsId3V2TagAvailable(tagId);

                //
                // Search for ID3v1 tags
                //
                var tagBytes = new byte[3];
                audioStream.Seek(-128, SeekOrigin.End);
                audioStream.Read(tagBytes, 0, tagBytes.Length);

                id3V1Found = (tagBytes[0] == 0x54) && (tagBytes[1] == 0x41) && (tagBytes[2] == 0x47);
            }

            return new FileState(id3V1Found, id3V2Found);
        }
Esempio n. 10
0
        public void Write(TagContainer tagContainer, 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;
            }
            if (tagContainer == null)
            {
                var ex = new ArgumentNullException("tagContainer");
                Logger.LogError(ex);

                throw ex;
            }

            Logger.LogInfo("Starting with the write operation.");

            //
            //  Validate whether the tag container is in ID3V2.3 formaz
            //
            string message;
            bool isTagValid = ValidateTag(tagContainer, out message);
            if (!isTagValid)
            {
                var ex = new InvalidId3StructureException(message);
                Logger.LogError(ex);

                throw ex;
            }

            //
            //  OK. Id3Tag is valid. Let's write the tag.
            //
            byte[] tagBytes;
            switch (tagContainer.TagVersion)
            {
                case TagVersion.Id3V23:
                    tagBytes = BuildId3V3Tag(tagContainer);
                    break;
                case TagVersion.Id3V24:
                    tagBytes = BuildId3V4Tag(tagContainer);
                    break;
                default:
                    var ex = new Id3TagException("This TagVersion is not supported!");
                    Logger.LogError(ex);

                    throw ex;
            }

            //
            //  encode the length
            //
            long length;
            if (tagContainer.TagVersion == TagVersion.Id3V24)
            {
                TagDescriptorV4 descriptor = tagContainer.GetId3V24Descriptor();
                if (descriptor.Footer)
                {
                    Logger.LogInfo("Writing ID3 V2.4 footer");
                    length = tagBytes.LongLength - 20;
                }
                else
                {
                    Logger.LogInfo("Ignore ID3 V2.4 footer");
                    length = tagBytes.LongLength - 10;
                }
            }
            else
            {
                length = tagBytes.LongLength - 10;
            }

            List<int> bits = GetBitCoding(length);
            var lengthBytes = new byte[4];

            EncodeLength(bits, lengthBytes);
            Array.Copy(lengthBytes, 0, tagBytes, 6, 4);

            //
            //  Build the tag bytes and start writing.
            //
            if (!input.CanRead)
            {
                var ex = new Id3IOException("Cannot read input stream");
                Logger.LogError(ex);

                throw ex;
            }
            if (!output.CanWrite)
            {
                var ex = new Id3IOException("Cannot write to output stream");
                Logger.LogError(ex);

                throw ex;
            }

            Logger.LogInfo("Write to Stream..");
            WriteToStream(input, output, tagBytes);
        }
Esempio n. 11
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;
        }
Esempio n. 12
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;
        }