/// <summary> /// Parses the raw frame data. /// </summary> /// <param name="frameData">The raw frame data.</param> /// <param name="version">The ID3v2 version of the tag being parsed.</param> protected override void ParseFrameData(byte[] frameData, ID3Versions version) { if (frameData.Length >= 5) { // Encoding type TextEncodingType encType = (TextEncodingType)frameData[0]; // Language char[] languageBytes = new char[COMMFrame.LanguageFieldLength]; Array.Copy(frameData, 1, languageBytes, 0, languageBytes.Length); string languageStr = (new String(languageBytes)).ToUpper(); language = new EncodedString(LanguageEncodingType, languageStr); // Text byte[] textBytes = new byte[frameData.Length - 4]; Array.Copy(frameData, 4, textBytes, 0, textBytes.Length); IList <EncodedString> strings = EncodedString.CreateStrings(encType, textBytes); if (strings.Count >= 2) { description = strings[0]; base.EncodedText = strings[1]; SetEncodedStringSettings(); } } }
/// <summary> /// Creates a new UserDefindedTextFrame. /// </summary> /// <param name="description">the description.</param> /// <param name="value">the value.</param> /// <param name="type">the text encoding.</param> public UserDefinedTextFrame(string description, string value, TextEncodingType type) { Descriptor.ID = "TXXX"; Description = description; Value = value; TextEncoding = type; }
/// <summary> /// Creates and returns a string from a portion of a byte array. /// </summary> /// <param name="startIndex">The index (in the byte array) at which to begin /// creating the string.</param> /// <param name="endIndex">The index (in the byte array) at which to finish /// creating the string.</param> /// <param name="stringData">The byte array from which to create the string.</param> /// <param name="encType">The TextEncodingType of the byte array.</param> /// <returns>A string from a portion of a byte array.</returns> private static EncodedString CreateString( int startIndex, int endIndex, byte[] stringData, TextEncodingType encType) { byte[] stringBytes = new byte[endIndex - startIndex]; Array.Copy(stringData, startIndex, stringBytes, 0, stringBytes.Length); return(new EncodedString(encType, stringBytes)); }
internal static Encoding GetEncodingFromEnum(TextEncodingType type) { Encoding aSCII = Encoding.ASCII; switch (type) { case TextEncodingType.String: return Encoding.Unicode; case TextEncodingType.Unicode: return Encoding.Unicode; case TextEncodingType.BigEndianUnicode: return Encoding.BigEndianUnicode; case TextEncodingType.Utf8: return Encoding.UTF8; case TextEncodingType.Utf7: return Encoding.UTF7; case TextEncodingType.Ascii: return Encoding.ASCII; } return Encoding.ASCII; }
internal static Encoding GetEncodingFromEnum(TextEncodingType type) { Encoding aSCII = Encoding.ASCII; switch (type) { case TextEncodingType.String: return(Encoding.Unicode); case TextEncodingType.Unicode: return(Encoding.Unicode); case TextEncodingType.BigEndianUnicode: return(Encoding.BigEndianUnicode); case TextEncodingType.Utf8: return(Encoding.UTF8); case TextEncodingType.Utf7: return(Encoding.UTF7); case TextEncodingType.Ascii: return(Encoding.ASCII); } return(Encoding.ASCII); }
internal static Encoding GetEncoding(TextEncodingType encoding) { switch (encoding) { case TextEncodingType.Binary: return(BinaryEncoding.Instance); case TextEncodingType.Unicode: return(Encoding.Unicode); case TextEncodingType.BigEndianUnicode: return(Encoding.BigEndianUnicode); case TextEncodingType.UTF8: return(Encoding.UTF8); case TextEncodingType.UTF32: return(Encoding.UTF32); case TextEncodingType.UTF7: return(Encoding.UTF7); default: throw new ArgumentException("Unknown text encoding", nameof(encoding)); } }
/// <summary> /// Creates a new UserDefinedURLLinkFrame /// </summary> /// <param name="description">the Description</param> /// <param name="url">The URL</param> /// <param name="type">The text encoding type.</param> public UserDefinedURLLinkFrame(string description, string url, TextEncodingType type) { Descriptor.ID = "WXXX"; Description = description; URL = url; TextEncoding = type; }
/// <summary> /// Create / encode a byte array and set it as payload, considering all properties specified /// in the record class. /// </summary> private void AssemblePayload(string languageCode, TextEncodingType textEncoding, string text) { //Debug.WriteLine("Text record - AssemblePayload: {0}, {1}, {2}", languageCode, textEncoding, text); // Convert the language code to a byte array var languageEncoding = Encoding.UTF8; var encodedLanguage = languageEncoding.GetBytes(languageCode); // Encode and convert the text to a byte array var encoding = (textEncoding == TextEncodingType.Utf8) ? Encoding.UTF8 : Encoding.BigEndianUnicode; var encodedText = encoding.GetBytes(text); // Calculate the length of the payload & create the array var payloadLength = 1 + encodedLanguage.Length + encodedText.Length; Payload = new byte[payloadLength]; // Assemble the status byte Payload[0] = 0; // Make sure also the RFU bit is set to 0 // Text encoding if (textEncoding == TextEncodingType.Utf8) { Payload[0] &= 0x7F; // ~0x80 } else { Payload[0] |= 0x80; } // Language code length Payload[0] |= (byte)(0x3f & (byte)encodedLanguage.Length); // Language code Array.Copy(encodedLanguage, 0, Payload, 1, encodedLanguage.Length); // Text Array.Copy(encodedText, 0, Payload, 1 + encodedLanguage.Length, encodedText.Length); }
/// <summary> /// Creates a new instance of CommentFrame. /// </summary> /// <param name="descriptor">the descriptor.</param> /// <param name="language">the language.</param> /// <param name="text">the text.</param> /// <param name="type">the text encoding.</param> public CommentFrame(string language, string descriptor, string text, TextEncodingType type) { Descriptor.ID = "COMM"; Language = language; ContentDescriptor = descriptor; Text = text; TextEncoding = type; }
/// <summary> /// Creates a new instance of PictureFrame. /// </summary> /// <param name="encoding">the text encoding</param> /// <param name="mimeType">the MIME type</param> /// <param name="description">the description</param> /// <param name="picture">the picture type</param> /// <param name="data">the picture bytes</param> public PictureFrame(TextEncodingType encoding, string mimeType, string description, PictureType picture, byte[] data) { TextEncoding = encoding; MimeType = mimeType; Description = description; PictureCoding = picture; PictureData = data; }
protected InvalidTextEncodingTypeException(SerializationInfo info, StreamingContext context) : base(info, context) { if (info == null) { throw new ArgumentNullException("info"); } invalidEncoding = (TextEncodingType)info.GetByte("invalidEncoding"); }
/// <summary> /// Gets the termination byte array under the given encoding. /// </summary> private byte[] GetTermination(TextEncodingType encType) { if (encType == TextEncodingType.UTF_16 || encType == TextEncodingType.UTF_16BE) { return(new byte[] { 0x00, 0x00 }); } else { return(new byte[] { 0x00 }); } }
/// <summary> /// Gets the length of the termination byte array under the given encoding. /// </summary> private int GetTerminationLength(TextEncodingType encType) { if (encType == TextEncodingType.UTF_16 || encType == TextEncodingType.UTF_16BE) { return(2); } else { return(1); } }
/// <summary> /// Prepares a byte array for an EncodedString in byte form by prepending /// an encoding-type byte and adding termination, according to the settings. /// </summary> private byte[] PrepareBytes(TextEncodingType encType) { int encodingLength = 0; int terminationLength = 0; byte[] termination = this.GetTermination(encType); if (hasEncodingTypePrepended) { encodingLength = 1; } if (isTerminated) { terminationLength = termination.Length; } int length = 0; switch (encType) { case TextEncodingType.ISO_8859_1: length = str.Length; break; case TextEncodingType.UTF_16: byte[] bom = utf16Encoding.GetPreamble(); length = utf16Encoding.GetByteCount(str) + bom.Length; break; case TextEncodingType.UTF_16BE: length = utf16BEEncoding.GetByteCount(str); break; case TextEncodingType.UTF_8: length = utf8Encoding.GetByteCount(str); break; } length += encodingLength + terminationLength; byte[] stringBytes = new byte[length]; if (hasEncodingTypePrepended) { stringBytes[0] = (byte)encType; } if (isTerminated) { Array.Copy(termination, 0, stringBytes, length - terminationLength, terminationLength); } return(stringBytes); }
public static void CommonalizeEncoding(params EncodedString[] strings) { TextEncodingType commonType = TextEncodingType.ISO_8859_1; foreach (EncodedString encString in strings) { if ((byte)encString.TextEncodingType > (byte)commonType) { commonType = encString.TextEncodingType; } } foreach (EncodedString encString in strings) { encString.TextEncodingType = commonType; } }
/// <summary> /// Parses a byte array and creates and returns one or more strings /// from the byte array. NULL bytes are interpreted as string terminators. /// Strings up to and including maxStrings are created and returned; the remainder /// of the byte array (if any) is passed back using the CreateStringsLeftoverBytes /// delegate. /// </summary> /// <param name="encType">The text encoding of the byte-array string.</param> /// <param name="stringData">A byte-array string.</param> /// <param name="maxStrings">The maximum number of strings to create.</param> /// <param name="leftoversDelegate">A delegate to be used to pass back any bytes /// left over from the string parsing.</param> /// <returns>One or more strings created from the byte array.</returns> public static ReadOnlyCollection <EncodedString> CreateStrings(TextEncodingType encType, byte[] stringData, int maxStrings, CreateStringsLeftoverBytes leftoversDelegate) { List <EncodedString> strings = new List <EncodedString>(); List <int> nullIndices; int incrementSize; if (encType == TextEncodingType.ISO_8859_1 || encType == TextEncodingType.UTF_8) { nullIndices = FindSingleByteNulls(stringData, maxStrings); incrementSize = 1; } else { nullIndices = FindDoubleByteNulls(stringData, maxStrings); incrementSize = 2; } int start = 0; foreach (int nullIndex in nullIndices) { strings.Add(CreateString(start, nullIndex, stringData, encType)); start = nullIndex + incrementSize; } if (strings.Count < maxStrings) { // Make a string from the remaining bytes strings.Add(CreateString(start, stringData.Length, stringData, encType)); } else { // Add the remaining bytes to the end byte[] remainingBytes = new byte[stringData.Length - start]; Array.Copy(stringData, start, remainingBytes, 0, remainingBytes.Length); if (leftoversDelegate != null) { leftoversDelegate(remainingBytes); } } return(new ReadOnlyCollection <EncodedString>(strings)); }
/// <summary> /// Parses the raw frame data. /// </summary> /// <param name="frameData">The raw frame data.</param> /// <param name="version">The ID3v2 version of the tag being parsed.</param> protected override void ParseFrameData(byte[] frameData, ID3Versions version) { // Encoding type TextEncodingType encType = (TextEncodingType)frameData[0]; // MIME type int mimeEnd = FindNull(frameData, 1); byte[] mimeBytes = new byte[mimeEnd - 1]; Array.Copy(frameData, 1, mimeBytes, 0, mimeBytes.Length); mimeType = new EncodedString(MimeTypeEncodingType, mimeBytes); byte[] remainingBytes = new byte[(frameData.Length - mimeEnd) - 1]; Array.Copy(frameData, mimeEnd + 1, remainingBytes, 0, remainingBytes.Length); IList <EncodedString> strings = EncodedString.CreateStrings(encType, remainingBytes, 2, delegate(byte[] leftoverBytes) { encapsulatedObject = leftoverBytes; }); filename = strings[0]; description = strings[1]; SetEncodedStringSettings(); }
internal static Encoding GetEncodingFromEnum(TextEncodingType type) { Encoding encoding = Encoding.ASCII; switch (type) { case TextEncodingType.String: encoding = Encoding.Unicode; break; case TextEncodingType.Unicode: encoding = Encoding.Unicode; break; case TextEncodingType.BigEndianUnicode: encoding = Encoding.BigEndianUnicode; break; case TextEncodingType.Utf8: encoding = Encoding.UTF8; break; case TextEncodingType.Utf7: encoding = Encoding.UTF7; break; case TextEncodingType.Ascii: encoding = Encoding.ASCII; break; default: // Default to unicode encoding encoding = Encoding.ASCII; break; } return(encoding); }
public static int GetTerminationCharLength(TextEncodingType type) { int length; switch (type) { case TextEncodingType.ISO_8859_1: length = 1; break; case TextEncodingType.UTF16: length = 2; break; case TextEncodingType.UTF16_BE: length = 2; break; case TextEncodingType.UTF8: length = 1; break; default: throw new ID3TagException("Could not extract TextEncoding (" + type + ")."); } return length; }
/// <summary> /// Parses a byte array and creates and returns one or more strings /// from the byte array. NULL bytes are interpreted as string terminators. /// Strings up to and including maxStrings are created and returned; the remainder /// of the byte array (if any) is passed back using the CreateStringsLeftoverBytes /// delegate. /// </summary> /// <param name="stringDataWithEncoding">A byte-array string whose first byte /// is a text encoding field.</param> /// <param name="maxStrings">The maximum number of strings to create.</param> /// <param name="leftoversDelegate">A delegate to be used to pass back any bytes /// left over from the string parsing.</param> /// <returns>One or more strings created from the byte array.</returns> public static ReadOnlyCollection <EncodedString> CreateStrings(byte[] stringDataWithEncoding, int maxStrings, CreateStringsLeftoverBytes leftoversDelegate) { TextEncodingType encType = (TextEncodingType)stringDataWithEncoding[0]; byte[] textBytes = new byte[stringDataWithEncoding.Length - 1]; Array.Copy(stringDataWithEncoding, 1, textBytes, 0, textBytes.Length); ReadOnlyCollection <EncodedString> strings; if (textBytes.Length > 0) { strings = CreateStrings(encType, textBytes, maxStrings, leftoversDelegate); } else { List <EncodedString> stringsList = new List <EncodedString>(); stringsList.Add(new EncodedString(encType, "")); strings = new ReadOnlyCollection <EncodedString>(stringsList); } return(strings); }
/// <summary> /// Creates a new EncodedString. /// </summary> /// <param name="encodingType">The type of encoding to use.</param> /// <param name="stringBytes">The byte array from which to create the EncodedString.</param> public EncodedString(TextEncodingType encodingType, byte[] stringBytes) { this.encodingType = encodingType; switch (encodingType) { case TextEncodingType.ISO_8859_1: this.SetISO_8859_1Bytes(stringBytes); break; case TextEncodingType.UTF_16: this.SetUTF_16Bytes(stringBytes); break; case TextEncodingType.UTF_16BE: this.SetUTF_16BEBytes(stringBytes); break; case TextEncodingType.UTF_8: this.SetUTF_8Bytes(stringBytes); break; } }
/// <summary> /// Removes a byte order mark (BOM), prepended text encoding type byte /// and appended string termination, if present according to the /// settings of this EncodedString. /// </summary> /// <returns>A byte array with only the character data.</returns> private byte[] GetStringDataOnly(byte[] stringBytesWithExtras, TextEncodingType encType) { int startIndex = 0; int endIndex = 0; if (hasEncodingTypePrepended) { startIndex += 1; } if (encType == TextEncodingType.UTF_16) { startIndex += 2; } if (isTerminated) { endIndex += this.GetTerminationLength(encType); } int totalExtraLength = startIndex + endIndex; byte[] bytesWithoutExtras = new byte[stringBytesWithExtras.Length - totalExtraLength]; Array.Copy(stringBytesWithExtras, startIndex, bytesWithoutExtras, 0, bytesWithoutExtras.Length); return(bytesWithoutExtras); }
public void SetEncoding(TextEncodingType textEncodingType) { switch (textEncodingType) { case TextEncodingType.UTF8: _encoding = Encoding.UTF8; break; case TextEncodingType.ASCII: _encoding = Encoding.ASCII; break; case TextEncodingType.BigEndianUnicode: _encoding = Encoding.BigEndianUnicode; break; case TextEncodingType.Default: _encoding = Encoding.Default; break; case TextEncodingType.Unicode: _encoding = Encoding.Unicode; break; case TextEncodingType.UTF32: _encoding = Encoding.UTF32; break; case TextEncodingType.UTF7: _encoding = Encoding.UTF7; break; default: break; } }
/// <summary> /// Creates a new TextFrame. /// </summary> /// <param name="id">the frame id.</param> /// <param name="content">the content.</param> /// <param name="type">the text encoding.</param> public TextFrame(string id, string content, TextEncodingType type) { Descriptor.ID = id; Content = content; TextEncoding = type; }
CreateStrings(TextEncodingType encType, byte[] stringData) { return(CreateStrings(encType, stringData, stringData.Length, null)); }
public InvalidTextEncodingTypeException(string message, TextEncodingType invalidEncoding , Exception inner) : base(message, inner) { this.invalidEncoding = invalidEncoding; }
public InvalidTextEncodingTypeException(TextEncodingType invalidEncoding) : base() { this.invalidEncoding = invalidEncoding; }
public static char[] Extract(TextEncodingType type, byte[] content) { return PerformExtract(type, content, false); }
public static char[] Extract(TextEncodingType type, byte[] content, bool abortAfterTermination) { return PerformExtract(type, content, abortAfterTermination); }
private static char[] PerformExtract(TextEncodingType type, byte[] content, bool abortAfterTermination) { char[] chars; switch (type) { case TextEncodingType.ISO_8859_1: chars = ExtractISO8859_1(content, abortAfterTermination); break; case TextEncodingType.UTF16: chars = ExtractUTF16(content, abortAfterTermination); break; case TextEncodingType.UTF16_BE: // Ohne BOM chars = ExtractUTF16_BigEndian(content, abortAfterTermination); break; case TextEncodingType.UTF8: chars = ExtractUTF8(content, abortAfterTermination); break; default: throw new ID3TagException("Could not extract TextEncoding (" + type + ")."); } return chars; }
public static char[] Extract(TextEncodingType type, List<byte> content) { var contentBytes = content.ToArray(); return PerformExtract(type, contentBytes, false); }
internal static Encoding GetEncodingFromEnum(TextEncodingType type) { Encoding encoding = Encoding.ASCII; switch (type) { case TextEncodingType.String: encoding = Encoding.Unicode; break; case TextEncodingType.Unicode: encoding = Encoding.Unicode; break; case TextEncodingType.BigEndianUnicode: encoding = Encoding.BigEndianUnicode; break; case TextEncodingType.Utf8: encoding = Encoding.UTF8; break; case TextEncodingType.Utf7: encoding = Encoding.UTF7; break; case TextEncodingType.Ascii: encoding = Encoding.ASCII; break; default: // Default to unicode encoding encoding = Encoding.ASCII; break; } return encoding; }
/// <summary> /// Create / encode a byte array and set it as payload, considering all properties specified /// in the record class. /// </summary> private void AssemblePayload(string languageCode, TextEncodingType textEncoding, string text) { //Debug.WriteLine("Text record - AssemblePayload: {0}, {1}, {2}", languageCode, textEncoding, text); // Convert the language code to a byte array var languageEncoding = Encoding.UTF8; var encodedLanguage = languageEncoding.GetBytes(languageCode); // Encode and convert the text to a byte array var encoding = (textEncoding == TextEncodingType.Utf8) ? Encoding.UTF8 : Encoding.BigEndianUnicode; var encodedText = encoding.GetBytes(text); // Calculate the length of the payload & create the array var payloadLength = 1 + encodedLanguage.Length + encodedText.Length; Payload = new byte[payloadLength]; // Assemble the status byte Payload[0] = 0; // Make sure also the RFU bit is set to 0 // Text encoding if (textEncoding == TextEncodingType.Utf8) Payload[0] &= 0x7F; // ~0x80 else Payload[0] |= 0x80; // Language code length Payload[0] |= (byte)(0x3f & (byte)encodedLanguage.Length); // Language code Array.Copy(encodedLanguage, 0, Payload, 1, encodedLanguage.Length); // Text Array.Copy(encodedText, 0, Payload, 1 + encodedLanguage.Length, encodedText.Length); }
private static bool ValidateTextEncoding(TextEncodingType type) { bool result; if (type == TextEncodingType.ISO_8859_1 || type == TextEncodingType.UTF16) { result = true; } else { // UTF_BE and UTF8 is not supported in ID3v2.3! result = false; } return result; }
/// <summary> /// Creates a new EncodedString. /// </summary> /// <param name="encodingType">The type of encoding to use.</param> /// <param name="str">The string from which to create the EncodedString.</param> public EncodedString(TextEncodingType encodingType, string str) { this.TextEncodingType = encodingType; this.String = str; }
internal static byte[] GetContentBytes(TextEncodingType encodingType, string content) { byte[] contentBytes; switch (encodingType) { case TextEncodingType.ISO_8859_1: var asciiEncoding = new ASCIIEncoding(); contentBytes = asciiEncoding.GetBytes(content); break; case TextEncodingType.UTF16: var utf16Encoding = new UnicodeEncoding(false, true); var bom = utf16Encoding.GetPreamble(); var dataBytes = utf16Encoding.GetBytes(content); // Copy BOM and Content together in an array contentBytes = new byte[bom.Length + dataBytes.Length]; Array.Copy(bom, 0, contentBytes, 0, bom.Length); Array.Copy(dataBytes, 0, contentBytes, bom.Length, dataBytes.Length); break; case TextEncodingType.UTF16_BE: var utf16BEEncoding = new UnicodeEncoding(true, true); contentBytes = utf16BEEncoding.GetBytes(content); break; case TextEncodingType.UTF8: var utf8Encoding = new UTF8Encoding(); contentBytes = utf8Encoding.GetBytes(content); break; default: throw new ID3TagException("Unknown Encoding Type found!"); } return contentBytes; }