Exemple #1
0
        /// <summary>Creates the specified header.</summary>
        /// <param name="header">The header.</param>
        /// <param name="flags">The flags.</param>
        /// <param name="description">The description.</param>
        /// <param name="type">The type.</param>
        /// <param name="mimeType">Type of the MIME.</param>
        /// <param name="imageData">The image data.</param>
        /// <returns></returns>
        /// <exception cref="NotSupportedException"></exception>
        public static ID3v2APICFrame Create(ID3v2Header header, ID3v2FrameFlags flags, string description, ID3v2PictureType type, string mimeType, byte[] imageData)
        {
            ID3v2EncodingType encoding = ID3v2Encoding.Select(header, description + mimeType);

            // header, encoding[1], mimeType+0, pitureType[1], description+0, data
            byte[] descriptionBytes = ID3v2Encoding.GetBytes(encoding, description, true);
            byte[] mimeTypeBytes    = ID3v2Encoding.GetBytes(encoding, mimeType, true);
            int    contentSize      = descriptionBytes.Length + mimeTypeBytes.Length + 1 + 1 + imageData.Length;
            var    frameHeader      = ID3v2FrameHeader.Create(header, "APIC", flags, contentSize);

            using (var ms = new MemoryStream())
            {
                var writer = new DataWriter(ms);
                writer.Write(frameHeader.Data);
                writer.Write((byte)encoding);
                writer.Write(mimeTypeBytes);
                writer.Write((byte)type);
                writer.Write(descriptionBytes);
                writer.Write(imageData);
                if (frameHeader.HeaderSize + contentSize != ms.Position)
                {
                    throw new Exception();
                }

                return(new ID3v2APICFrame(new ID3v2Frame(header, ms.ToArray())));
            }
        }
        /// <summary>Gets the bytes.</summary>
        /// <param name="encoding">The encoding.</param>
        /// <param name="value">The value.</param>
        /// <param name="termination">if set to <c>true</c> [use string zero termination].</param>
        /// <returns></returns>
        public static byte[] GetBytes(ID3v2EncodingType encoding, string value, bool termination)
        {
            if (termination)
            {
                value += "\0";
            }

            return(Get(encoding).GetBytes(value));
        }
        /// <summary>
        /// Gets whether 8 bit char index search (true) shall be used or 16 bit (false).
        /// </summary>
        /// <param name="encoding">The ID3v2 encoding number.</param>
        /// <returns>Returns true if 8 bit char index search may be used.</returns>
        public static bool Is8Bit(ID3v2EncodingType encoding)
        {
            switch (encoding)
            {
            case ID3v2EncodingType.UTF8:
            case ID3v2EncodingType.ISO88591_OLD:
            case ID3v2EncodingType.ISO88591: return(true);

            case ID3v2EncodingType.Unicode:
            case ID3v2EncodingType.BigEndianUnicode: return(false);

            default: throw new NotSupportedException(string.Format("Encoding {0} is not supported!", encoding));
            }
        }
Exemple #4
0
        /// <summary>Creates a new ID3v2TextFrame.</summary>
        /// <param name="header">The header.</param>
        /// <param name="flags">The flags.</param>
        /// <param name="id">The identifier.</param>
        /// <param name="text">The text.</param>
        /// <returns></returns>
        /// <exception cref="NotSupportedException"></exception>
        public static ID3v2TextFrame Create(ID3v2Header header, ID3v2FrameFlags flags, string id, string text)
        {
            ID3v2EncodingType encoding = ID3v2Encoding.Select(header, text);

            // header, encoding[1], name+0
            byte[] textBytes   = ID3v2Encoding.GetBytes(encoding, text, true);
            int    contentSize = 1 + textBytes.Length;
            var    frameHeader = ID3v2FrameHeader.Create(header, id, flags, contentSize);

            using (var ms = new MemoryStream())
            {
                var writer = new DataWriter(ms);
                writer.Write(frameHeader.Data);
                writer.Write((byte)encoding);
                writer.Write(textBytes);
                return(new ID3v2TextFrame(new ID3v2Frame(header, ms.ToArray())));
            }
        }
Exemple #5
0
        /// <summary>Creates a new header.</summary>
        /// <param name="header">The tag header.</param>
        /// <param name="flags">The flags.</param>
        /// <param name="name">The name.</param>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        /// <exception cref="NotSupportedException"></exception>
        public static ID3v2TXXXFrame Create(ID3v2Header header, ID3v2FrameFlags flags, string name, string value)
        {
            ID3v2EncodingType encoding = ID3v2Encoding.Select(header, name + value);

            // header, encoding[1], name+0, value+0
            byte[] nameBytes   = ID3v2Encoding.GetBytes(encoding, name, true);
            byte[] valueBytes  = ID3v2Encoding.GetBytes(encoding, value, true);
            int    contentSize = nameBytes.Length + valueBytes.Length + 1;
            var    frameHeader = ID3v2FrameHeader.Create(header, "TXXX", flags, contentSize);

            using (var ms = new MemoryStream())
            {
                var writer = new DataWriter(ms);
                writer.Write(frameHeader.Data);
                writer.Write((byte)encoding);
                writer.Write(nameBytes);
                writer.Write(valueBytes);
                return(new ID3v2TXXXFrame(new ID3v2Frame(header, ms.ToArray())));
            }
        }
        /// <summary>
        /// Parses a string from the specified data and returns the length in bytes.
        /// </summary>
        /// <param name="encoding">The ID3v2 encoding number.</param>
        /// <param name="data">The bytes to parse.</param>
        /// <param name="index">The start index to begin at.</param>
        /// <param name="text">The string output.</param>
        /// <returns>Returns the number of bytes parsed.</returns>
        public static int Parse(ID3v2EncodingType encoding, byte[] data, int index, out string text)
        {
            if (data == null)
            {
                throw new ArgumentNullException("Data");
            }

            int len;
            int l_MarkerLength;

            if (Is8Bit(encoding))
            {
                len            = Array.IndexOf <byte>(data, 0, index) - index;
                l_MarkerLength = 1;
            }
            else
            {
                len            = IndexOfNull16Bit(data, index) - index;
                l_MarkerLength = 2;
            }
            var enc = Get(encoding);

            if (len < 0)
            {
                len            = data.Length - index;
                l_MarkerLength = 0;
                if (len == 0)
                {
                    text = string.Empty;
                    return(0);
                }
            }
            text = enc.GetString(data, index, len).Trim('\uFFFE', '\uFEFF', '\u200B', '\u180E', '\u202F', '\u205F', ' ', '\t');
            if (text.StartsWith(ASCII.Strings.UTF8BOM))
            {
                text = text.Substring(ASCII.Strings.UTF8BOM.Length);
            }

            return(len + l_MarkerLength);
        }