private static byte[] EncodeUrlLink <TFrame>(Id3Frame id3Frame) where TFrame : UrlLinkFrame { var frame = (TFrame)id3Frame; return(frame.Url != null?TextEncodingHelper.GetDefaultEncoding().GetBytes(frame.Url) : new byte[0]); }
private static Id3Frame DecodeComment(byte[] data) { var frame = new CommentFrame { EncodingType = (Id3TextEncoding)data[0] }; string language = TextEncodingHelper.GetDefaultEncoding().GetString(data, 1, 3).ToLowerInvariant(); if (!Enum.IsDefined(typeof(Id3Language), language)) { frame.Language = Id3Language.eng; } else { frame.Language = (Id3Language)Enum.Parse(typeof(Id3Language), language, true); } string[] splitStrings = TextEncodingHelper.GetSplitStrings(data, 4, data.Length - 4, frame.EncodingType); if (splitStrings.Length > 1) { frame.Description = splitStrings[0]; frame.Comment = splitStrings[1]; } else if (splitStrings.Length == 1) { frame.Comment = splitStrings[0]; } return(frame); }
private static TFrame DecodeText <TFrame>(byte[] data) where TFrame : TextFrameBase, new() { var frame = new TFrame(); byte encodingByte = data[0]; string value; if (encodingByte == 0 || encodingByte == 1) { frame.EncodingType = (Id3TextEncoding)encodingByte; Encoding encoding = TextEncodingHelper.GetEncoding(frame.EncodingType); value = encoding.GetString(data, 1, data.Length - 1); if (value.Length > 0 && frame.EncodingType == Id3TextEncoding.Unicode && (value[0] == '\xFFFE' || value[0] == '\xFEFF')) { value = value.Remove(0, 1); } } else { frame.EncodingType = Id3TextEncoding.Iso8859_1; Encoding encoding = TextEncodingHelper.GetEncoding(frame.EncodingType); value = encoding.GetString(data, 0, data.Length); } frame.TextValue = value; return(frame); }
private static Id3Frame DecodePicture(byte[] data) { var frame = new PictureFrame { EncodingType = (Id3TextEncoding)data[0] }; byte[] mimeType = ByteArrayHelper.GetBytesUptoSequence(data, 1, new byte[] { 0x00 }); if (mimeType == null) { frame.MimeType = "image/"; return(frame); } frame.MimeType = TextEncodingHelper.GetDefaultString(mimeType, 0, mimeType.Length); int currentPos = mimeType.Length + 2; frame.PictureType = (PictureType)data[currentPos]; currentPos++; byte[] description = ByteArrayHelper.GetBytesUptoSequence(data, currentPos, TextEncodingHelper.GetSplitterBytes(frame.EncodingType)); if (description == null) { return(frame); } frame.Description = TextEncodingHelper.GetString(description, 0, description.Length, frame.EncodingType); currentPos += description.Length + TextEncodingHelper.GetSplitterBytes(frame.EncodingType).Length; frame.PictureData = new byte[data.Length - currentPos]; Array.Copy(data, currentPos, frame.PictureData, 0, frame.PictureData.Length); return(frame); }
private static byte[] EncodePicture(Id3Frame id3Frame) { var frame = (PictureFrame)id3Frame; var bytes = new List <byte> { (byte)frame.EncodingType }; Encoding defaultEncoding = TextEncodingHelper.GetDefaultEncoding(); bytes.AddRange(!string.IsNullOrEmpty(frame.MimeType) ? defaultEncoding.GetBytes(frame.MimeType) : defaultEncoding.GetBytes("image/")); bytes.Add(0); bytes.Add((byte)frame.PictureType); Encoding descriptionEncoding = TextEncodingHelper.GetEncoding(frame.EncodingType); bytes.AddRange(descriptionEncoding.GetPreamble()); if (!string.IsNullOrEmpty(frame.Description)) { bytes.AddRange(descriptionEncoding.GetBytes(frame.Description)); } bytes.AddRange(TextEncodingHelper.GetSplitterBytes(frame.EncodingType)); if (frame.PictureData != null && frame.PictureData.Length > 0) { bytes.AddRange(frame.PictureData); } return(bytes.ToArray()); }
private static Id3Frame DecodeCustomUrlLink(byte[] data) { var frame = new CustomUrlLinkFrame { EncodingType = (Id3TextEncoding)data[0] }; byte[][] splitBytes = ByteArrayHelper.SplitBySequence(data, 1, data.Length - 1, TextEncodingHelper.GetSplitterBytes(frame.EncodingType)); string url = null; if (splitBytes.Length > 1) { frame.Description = TextEncodingHelper.GetString(splitBytes[0], 0, splitBytes[0].Length, frame.EncodingType); url = TextEncodingHelper.GetDefaultString(splitBytes[1], 0, splitBytes[1].Length); } else if (splitBytes.Length == 1) { url = TextEncodingHelper.GetDefaultString(splitBytes[0], 0, splitBytes[0].Length); } frame.Url = url; return(frame); }
private static byte[] EncodeComment(Id3Frame id3Frame) { var frame = (CommentFrame)id3Frame; var bytes = new List <byte> { (byte)frame.EncodingType }; bytes.AddRange(TextEncodingHelper.GetDefaultEncoding().GetBytes(frame.Language.ToString())); Encoding encoding = TextEncodingHelper.GetEncoding(frame.EncodingType); bytes.AddRange(encoding.GetPreamble()); if (!string.IsNullOrEmpty(frame.Description)) { bytes.AddRange(encoding.GetBytes(frame.Description)); } bytes.AddRange(TextEncodingHelper.GetSplitterBytes(frame.EncodingType)); bytes.AddRange(encoding.GetPreamble()); if (!string.IsNullOrEmpty(frame.Comment)) { bytes.AddRange(encoding.GetBytes(frame.Comment)); } return(bytes.ToArray()); }
private static TFrame DecodeUrlLink <TFrame>(byte[] data) where TFrame : UrlLinkFrame, new() { var frame = new TFrame { Url = TextEncodingHelper.GetDefaultString(data, 0, data.Length) }; return(frame); }
private static byte[] EncodePrivate(Id3Frame id3Frame) { var frame = (PrivateFrame)id3Frame; var bytes = new List <byte>(); bytes.AddRange(TextEncodingHelper.GetEncoding(Id3TextEncoding.Iso8859_1).GetBytes(frame.OwnerId)); bytes.AddRange(TextEncodingHelper.GetSplitterBytes(Id3TextEncoding.Iso8859_1)); bytes.AddRange(frame.Data ?? new byte[0]); return(bytes.ToArray()); }
private static Id3Frame DecodePrivate(byte[] data) { var frame = new PrivateFrame(); byte[] splitterSequence = TextEncodingHelper.GetSplitterBytes(Id3TextEncoding.Iso8859_1); byte[] ownerIdBytes = ByteArrayHelper.GetBytesUptoSequence(data, 0, splitterSequence); frame.OwnerId = TextEncodingHelper.GetString(ownerIdBytes, 0, ownerIdBytes.Length, Id3TextEncoding.Iso8859_1); frame.Data = new byte[data.Length - ownerIdBytes.Length - splitterSequence.Length]; Array.Copy(data, ownerIdBytes.Length + splitterSequence.Length, frame.Data, 0, frame.Data.Length); return(frame); }
private static string ReadTagString(byte[] bytes, int index, int length) { int endIndex = ByteArrayHelper.LocateSequence(bytes, index, length, new byte[] { 0 }); if (endIndex == -1 || endIndex <= index) { endIndex = index + length; } string tagString = TextEncodingHelper.GetDefaultString(bytes, index, endIndex - index).Trim(); return(tagString); }
internal override bool WriteTag(Stream stream, Id3Tag tag) { Encoding encoding = TextEncodingHelper.GetDefaultEncoding(); var bytes = new byte[128]; encoding.GetBytes("TAG").CopyTo(bytes, 0); byte[] itemBytes; if (!string.IsNullOrEmpty(tag.Title.Value)) { itemBytes = encoding.GetBytes(tag.Title.Value); Array.Copy(itemBytes, 0, bytes, 3, Math.Min(30, itemBytes.Length)); } if (!string.IsNullOrEmpty(tag.Artists.TextValue)) { itemBytes = encoding.GetBytes(tag.Artists.TextValue); Array.Copy(itemBytes, 0, bytes, 33, Math.Min(30, itemBytes.Length)); } if (!string.IsNullOrEmpty(tag.Album.Value)) { itemBytes = encoding.GetBytes(tag.Album.Value); Array.Copy(itemBytes, 0, bytes, 63, Math.Min(30, itemBytes.Length)); } if (!string.IsNullOrEmpty(tag.Year.TextValue)) { itemBytes = encoding.GetBytes(tag.Year.TextValue); Array.Copy(itemBytes, 0, bytes, 93, Math.Min(4, itemBytes.Length)); } if (tag.Comments.Count > 0) { itemBytes = encoding.GetBytes(tag.Comments[0].Comment); int maxCommentLength = tag.Track.Value == -1 ? 30 : 28; Array.Copy(itemBytes, 0, bytes, 97, Math.Min(maxCommentLength, itemBytes.Length)); } if (tag.Track.Value >= 0) { bytes[126] = (byte)tag.Track.Value; } if (HasTag(stream)) { stream.Seek(-128, SeekOrigin.End); } else { stream.Seek(0, SeekOrigin.End); } stream.Write(bytes, 0, bytes.Length); return(true); }
internal override bool HasTag(Stream stream) { if (stream.Length < 128) { return(false); } stream.Seek(-128, SeekOrigin.End); byte[] magicBytes = new byte[3]; stream.Read(magicBytes, 0, 3); string magic = TextEncodingHelper.GetDefaultString(magicBytes, 0, 3); return(magic == "TAG"); }
private static byte[] EncodeText <TFrame>(Id3Frame id3Frame) where TFrame : TextFrameBase { var frame = (TFrame)id3Frame; Encoding encoding = TextEncodingHelper.GetEncoding(frame.EncodingType); byte[] preamble = encoding.GetPreamble(); byte[] textBytes = encoding.GetBytes(frame.TextValue); var data = new byte[1 + preamble.Length + textBytes.Length]; data[0] = (byte)frame.EncodingType; preamble.CopyTo(data, 1); textBytes.CopyTo(data, preamble.Length + 1); return(data); }
private static byte[] EncodeCustomUrlLink(Id3Frame id3Frame) { var frame = (CustomUrlLinkFrame)id3Frame; var bytes = new List <byte> { (byte)frame.EncodingType }; Encoding encoding = TextEncodingHelper.GetEncoding(frame.EncodingType); bytes.AddRange(encoding.GetPreamble()); if (!string.IsNullOrEmpty(frame.Description)) { bytes.AddRange(encoding.GetBytes(frame.Description)); } bytes.AddRange(TextEncodingHelper.GetSplitterBytes(frame.EncodingType)); if (frame.Url != null) { bytes.AddRange(TextEncodingHelper.GetDefaultEncoding().GetBytes(frame.Url)); } return(bytes.ToArray()); }