/// <summary> /// Gets the length of the byte order marker. <see cref="Id3v2FrameEncodingType"/> for possible encoding types. /// </summary> /// <param name="encodingType">Type of the encoding.</param> /// <returns>The length of the byte order marker, in bytes.</returns> public static int GetBomLength(Id3v2FrameEncodingType encodingType) { switch (encodingType) { case Id3v2FrameEncodingType.Default: case Id3v2FrameEncodingType.UTF16BigEndianWithoutBom: return 0; case Id3v2FrameEncodingType.UTF16BigEndian: case Id3v2FrameEncodingType.UTF16LittleEndian: return 2; case Id3v2FrameEncodingType.UTF8: case Id3v2FrameEncodingType.UTF7: return 3; } return 0; }
/// <summary> /// Determines whether the string is a valid string in the specified frame encoding type and if it contains new line characters. /// </summary> /// <param name="textString">The string to check.</param> /// <param name="frameEncodingType">Type of the frame encoding.</param> /// <param name="newLineAllowed">if set to <c>true</c>, new lines are allowed in the string.</param> /// <returns> /// <c>true</c> if the string is a valid string in the specified frame encoding type; otherwise, <c>false</c>. /// </returns> /// <remarks> /// An <see cref="Id3v2FrameEncodingType.Default"/> string is represented as characters in the range 0x20 - 0xFF. /// In <see cref="Id3v2FrameEncodingType.Default"/> and <see cref="Id3v2FrameEncodingType.UTF8"/> /// a new line is represented, when allowed, with 0x0A only. /// In <see cref="Id3v2FrameEncodingType.UTF16LittleEndian"/> a new line is represented, when allowed, with 0x0A 0x00. /// In <see cref="Id3v2FrameEncodingType.UTF16BigEndian"/> and <see cref="Id3v2FrameEncodingType.UTF16BigEndianWithoutBom"/> /// a new line is represented, when allowed, with 0x00 0x0A. /// </remarks> public static bool IsValidTextString(string textString, Id3v2FrameEncodingType frameEncodingType, bool newLineAllowed) { if (textString == null) throw new ArgumentNullException("textString"); switch (frameEncodingType) { case Id3v2FrameEncodingType.Default: return textString.All(c => ((c >= (char)0x20) && (c <= (char)0xFF)) || (newLineAllowed && (c == '\n'))); case Id3v2FrameEncodingType.UTF16LittleEndian: case Id3v2FrameEncodingType.UTF16BigEndian: case Id3v2FrameEncodingType.UTF16BigEndianWithoutBom: case Id3v2FrameEncodingType.UTF7: case Id3v2FrameEncodingType.UTF8: return textString.All(c => (newLineAllowed && (c == '\n')) || (c != '\0')); default: return true; } }
/// <summary> /// Gets the encoding for the specific encoding type. /// </summary> /// <param name="encodingType">Type of the encoding.</param> /// <returns> /// The encoding for the specific encoding type. /// </returns> /// <remarks> /// See <see cref="Id3v2FrameEncodingType"/> for possible values. /// </remarks> public static Encoding GetEncoding(Id3v2FrameEncodingType encodingType) { switch (encodingType) { case Id3v2FrameEncodingType.Default: return Encoding.GetEncoding("ISO-8859-1"); case Id3v2FrameEncodingType.UTF16LittleEndian: case Id3v2FrameEncodingType.UTF16BigEndian: return (encodingType == Id3v2FrameEncodingType.UTF16LittleEndian) ? new UnicodeEncoding(false, true) : new UnicodeEncoding(true, true); case Id3v2FrameEncodingType.UTF16BigEndianWithoutBom: return new UnicodeEncoding(true, false); case Id3v2FrameEncodingType.UTF8: return new UTF8Encoding(false); case Id3v2FrameEncodingType.UTF7: return new UTF7Encoding(); } return Encoding.Default; }
/// <summary> /// Determines whether the specified encoding type is valid. /// </summary> /// <param name="encodingType">Type of the encoding.</param> /// <returns> /// <c>true</c> if the specified encoding type is valid; otherwise, <c>false</c>. /// </returns> public static bool IsValidEncodingType(Id3v2FrameEncodingType encodingType) { return Enum.TryParse(encodingType.ToString(), true, out encodingType); }
/// <summary> /// Gets the real value of the encoding type as defined in the Id3v2 specs. /// </summary> /// <param name="encodingType">Type of the encoding.</param> /// <returns>The real value of the encoding type, as defined in the Id3v2 specs.</returns> public static byte GetEncodingTypeValue(Id3v2FrameEncodingType encodingType) { if ((byte)encodingType >= EncodingTypes.Length) throw new ArgumentOutOfRangeException("encodingType"); return EncodingTypes[(byte)encodingType]; }
/// <summary> /// Gets the preamble of the specified encoding. /// </summary> /// <param name="encodingType">Type of the encoding.</param> /// <returns>The preamble of the encoding, or an empty byte array if the encoding does not use a preamble.</returns> public static byte[] GetEncodingPreamble(Id3v2FrameEncodingType encodingType) { switch (encodingType) { case Id3v2FrameEncodingType.Default: case Id3v2FrameEncodingType.UTF16BigEndianWithoutBom: case Id3v2FrameEncodingType.UTF8: case Id3v2FrameEncodingType.UTF7: return new byte[0]; case Id3v2FrameEncodingType.UTF16LittleEndian: case Id3v2FrameEncodingType.UTF16BigEndian: return ((encodingType == Id3v2FrameEncodingType.UTF16LittleEndian) ? Encoding.Unicode : Encoding.BigEndianUnicode).GetPreamble(); default: throw new ArgumentOutOfRangeException("encodingType"); } }