示例#1
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;
        }
示例#2
0
        public void Remove(Stream input, Stream output)
        {
            CheckStreams(input, output);

            try
            {
                long audioBytesCount = GetAudioBytesCount(input);
                input.Seek(0, SeekOrigin.Begin);

                Utils.WriteAudioStream(output, input, audioBytesCount);
            }
            catch (Exception ex)
            {
                var ioEx = new Id3IOException("Cannot write ID3v1 tag", ex);
                Logger.LogError(ioEx);

                throw ioEx;
            }
        }
示例#3
0
        /// <summary>
        /// Writes a new ID3v1 tag using specified code page for text encoding.
        /// </summary>
        /// <param name="tag">the tag.</param>
        /// <param name="input">the audio input stream.</param>
        /// <param name="output">the target stream.</param>
        /// <param name="codePage">The code page for text encoding.</param>
        public void Write(Id3V1Tag tag, Stream input, Stream output, int codePage)
        {
            //
            //  Validate the parameter.
            //
            if (tag == null)
            {
                var ex = new ArgumentNullException("tag");
                Logger.LogError(ex);

                throw ex;
            }

            CheckStreams(input, output);

            try
            {
                //
                //  Read the last 128 Bytes from the stream (ID3v1 Position)
                //
                long audioBytesCount = GetAudioBytesCount(input);

                //
                //  Write the audio data and tag
                //
                input.Seek(0, SeekOrigin.Begin);
                Utils.WriteAudioStream(output, input, audioBytesCount);

                byte[] tagBytes = ConvertToByte(tag, codePage);
                output.Write(tagBytes, 0, tagBytes.Length);
            }
            catch (Exception ex)
            {
                var ioEx = new Id3IOException("Cannot write ID3v1 tag", ex);
                Logger.LogError(ioEx);

                throw ioEx;
            }
        }
示例#4
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;
        }
示例#5
0
        private static void WriteToStream(Stream input, Stream output, byte[] tagBytes)
        {
            try
            {
                //
                // Write the tag
                //
                output.Write(tagBytes, 0, tagBytes.Length);

                //
                // Write the audio content
                //
                byte[] bytes = SuppressTags(input);
                if (bytes != null)
                {
                    // No Tag available. Write the already read bytes.
                    output.Write(bytes, 0, bytes.Length);
                }
                long length = input.Length;
                Utils.WriteAudioStream(output, input, length);
            }
            catch (Exception ex)
            {
                var ioException = new Id3IOException("Cannot write Tag.", ex);
                Logger.LogError(ioException);
                throw ioException;
            }
        }
示例#6
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);
        }
示例#7
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);
        }