/// <summary> /// Set text of specific TextFrame /// </summary> /// <param name="FrameID">FrameID</param> /// <param name="Text">Text to set</param> public void SetTextFrame(string FrameID, string Text) { if (!FramesInfo.IsValidFrameID(FrameID)) { return; } TextFrame[] TF = TextFrames.ToArray(); for (int i = 0; i < TextFrames.Count - 1; i++) { if (TF[i].FrameID == FrameID) { TextFrames.RemoveAt(i); break; } } if (Text != "") { HaveTag = true; TextFrames.Add(FrameID, new TextFrame(FrameID, new FrameFlags(), Text, (StaticMethods.IsAscii(Text) ? TextEncodings.Ascii : _DefaultUnicodeEncoding), _Version.Minor, TStream.FS)); } }
private void WriteFrameHeader(int MinorVersion) { byte[] Buf; int FrameIDLength = MinorVersion == 2 ? 3 : 4; // Length of FrameID according to version string Temp = _FrameID; // if minor version of ID3 were 2, the frameID is 3 character length if (MinorVersion == 2) { Temp = FramesInfo.Get3CharID(Temp); if (Temp == null) // This frame is not availabe in this version { return; } } Tag.WriteText(Temp, TextEncodings.Ascii, false); // Write FrameID Buf = BitConverter.GetBytes(Length); Array.Reverse(Buf); if (MinorVersion == 2) { Tag.FS.Write(Buf, 1, Buf.Length - 1); // Write Frame Size } else { Tag.FS.Write(Buf, 0, Buf.Length); // Write Frame Size } if (MinorVersion != 2) { // If newer than version 2 it have Flags Buf = BitConverter.GetBytes((ushort)_FrameFlags); Array.Reverse(Buf); Tag.FS.Write(Buf, 0, Buf.Length); // Write Frame Flag } }
/// <summary> /// Write all frames to specific TagStream /// </summary> /// <param name="writer">TagStream to write data to</param> /// <param name="Ver">Minor Version of ID3</param> private void WriteFrames(int Ver) { foreach (FrameCollectionBase Coll in _CollectionFrames.Values) { if (Coll.Name != CollectionIndex.Unknown.ToString() || (Coll.Name == CollectionIndex.Unknown.ToString() && !_DropUnknown)) { foreach (Frame Fr in Coll) { // If Frame is not valid and is not UserTextFrame we ignore it if (!FramesInfo.IsCompatible(Fr.FrameID, Ver) && FramesInfo.IsTextFrame(Fr.FrameID, Ver) != 2) { AddError(new ID3Exception("nonCompatible Frame found on Frames and will not save with file", Fr.FrameID, ExceptionLevels.Warning)); continue; } if (Fr.IsValid) { Fr.WriteData(Ver); } } } } foreach (Frame Fr in _SingleFrames.Values) { if (FramesInfo.IsCompatible(Fr.FrameID, Ver) && Fr.IsValid) { Fr.WriteData(Ver); } } }
/// <summary> /// Create new TextFrame with specific information /// </summary> /// <param name="Text">Text of TextFrame</param> /// <param name="TextEncoding">TextEncoding of TextFrame</param> /// <param name="FrameID">FrameID of TextFrame</param> /// <param name="Flags">Flags of Frame</param> public TextFrame(string FrameID, FrameFlags Flags, string Text, TextEncodings TextEncoding, int Ver) : base(FrameID, Flags) { if (FramesInfo.IsTextFrame(FrameID, Ver) != 1) { throw (new ArgumentException(FrameID + " is not valid TextFrame FrameID")); } this.Text = Text; this.TextEncoding = TextEncoding; }
/// <summary> /// Create new UserTextFrame from specific information /// </summary> /// <param name="FrameID">FrameID of frame</param> /// <param name="Flags">Frame flags</param> /// <param name="Text">Frame text</param> /// <param name="Description">Frame description</param> /// <param name="TextEncoding">TextEncoding of texts</param> /// <param name="Ver">Minor version of ID3v2</param> public UserTextFrame(string FrameID, FrameFlags Flags, string Text, string Description, TextEncodings TextEncoding, int Ver) : base(FrameID, Flags) { if (FramesInfo.IsTextFrame(FrameID, Ver) != 2) { throw (new ArgumentException(FrameID + " is not valid for UserTextFrame class")); } this.Text = Text; this.TextEncoding = TextEncoding; this.Description = Description; }
public ImagingBitmapInfo() { this.framesInfo = new FramesInfo(); this.Metadata = new MetadataInfo(); this.Metadata.ApplicationName = string.Empty; this.Metadata.Author = new List <string>() { }; this.Metadata.CameraManufacturer = string.Empty; this.Metadata.CameraModel = string.Empty; this.Metadata.Comment = string.Empty; this.Metadata.Copyright = string.Empty; this.Metadata.DateTaken = string.Empty; this.Metadata.Subject = string.Empty; this.Metadata.Title = string.Empty; }
protected bool ValidatingFrameID(string FrameIdentifier, ValidatingErrorTypes ErrorType) { bool IsValid = FramesInfo.IsValidFrameID(FrameIdentifier); if (!IsValid) { if (ErrorType == ValidatingErrorTypes.Exception) { throw (new ArgumentException("FrameID must be 4 capital letters")); } else if (ErrorType == ValidatingErrorTypes.ID3Error) { ErrorOccur(FrameIdentifier + " is not valid FrameID", true); } } return(IsValid); }
/// <summary> /// Indicate is specific FrameID valid or not /// </summary> /// <param name="FrameIdentifier">FrameID to check</param> /// <param name="ErrorType">Error type that must occur if frame identifier was not valid</param> /// <returns>true if frame identifer was valid otherwise false</returns> protected bool ValidatingFrameID(string FrameIdentifier, ExceptionLevels ErrorType) { bool IsValid = FramesInfo.IsValidFrameID(FrameIdentifier); if (!IsValid) { ID3Exception Ex = new ID3Exception(FrameIdentifier + " is not valid FrameID", FrameIdentifier, ErrorType); if (ErrorType == ExceptionLevels.Error) { throw Ex; } else if (ErrorType == ExceptionLevels.Error) { ExceptionOccured(Ex); } } return(IsValid); }
/// <summary> /// Get header of current frame according to specific Size /// </summary> /// <param name="MinorVersion">Minor version of ID3v2</param> /// <returns>MemoryStream contain frame header</returns> protected MemoryStream FrameHeader(int MinorVersion) { byte[] Buf; MemoryStream ms = new MemoryStream(); int FrameIDLength = MinorVersion == 2 ? 3 : 4; // Length of FrameID according to version string Temp = _FrameID; // if minor version of ID3 were 2, the frameID is 3 character length if (MinorVersion == 2) { Temp = FramesInfo.Get3CharID(Temp); if (Temp == null) // This frame is not availabe in this version { return(null); } } ms.Write(Encoding.ASCII.GetBytes(Temp), 0, FrameIDLength); // Write FrameID Buf = BitConverter.GetBytes(Length); Array.Reverse(Buf); if (MinorVersion == 2) { ms.Write(Buf, 1, Buf.Length - 1); // Write Frame Size } else { ms.Write(Buf, 0, Buf.Length); // Write Frame Size } if (MinorVersion != 2) { // If newer than version 2 it have Flags Buf = BitConverter.GetBytes((ushort)_FrameFlags); Array.Reverse(Buf); ms.Write(Buf, 0, Buf.Length); // Write Frame Flag } return(ms); }
/// <summary> /// Add Frame information to where it must store /// </summary> /// <param name="Data">FileStream contain Frame</param> /// <param name="FrameID">FrameID of frame</param> /// <param name="Length">Maximum available length to read</param> /// <param name="Flags">Flags of frame</param> private bool AddFrame(string FrameID, int Length, FrameFlags Flags, Stream FS) { // NOTE: All FrameIDs must be capital letters if (!FramesInfo.IsValidFrameID(FrameID)) { AddError(new ID3Exception("nonValid Frame found and dropped", FrameID, ExceptionLevels.Repaired)); return(false); } int IsText = FramesInfo.IsTextFrame(FrameID, _Version.Minor); if (IsText == 1) { TextFrame TempTextFrame = new TextFrame(FrameID, Flags, Length, FS); if (TempTextFrame.IsValid && !TextFrames.ContainsKey(FrameID)) { TextFrames.Add(FrameID, TempTextFrame); return(true); } return(false); } else if (IsText == 2) { UserTextFrame TempUserTextFrame = new UserTextFrame(FrameID, Flags, Length, FS); if (TempUserTextFrame.IsValid && FrameID != "TXXX" && !UserTextFrames.ContainsKey(FrameID)) { UserTextFrames.Add(FrameID, TempUserTextFrame); return(true); } return(false); } else if (FrameID == "LINK") { LinkFrame LF = new LinkFrame(FrameID, Flags, Length, FS); if (LF.IsValid && !LinkFrames.ContainsKey(FrameID)) { LinkFrames.Add(FrameID, LF); if (_LoadLinkedFrames) { LoadFrameFromFile(LF.FrameIdentifier, LF.URL); return(true); } } else { AddError(LF.Exception); } } Frame F = null; if (!FramesInfo.ExcludedList.Any(t => t == FrameID)) { FrameInfo Info = FramesInfo.GetFrame(FrameID); if (Info == null || Info.ClassType == null) { AddError(new ID3Exception("Unknown Frame found and dropped according to setting", FrameID, ExceptionLevels.Warning)); return(true); } try { F = Info.Constuctor(FrameID, Flags, Length, TStream.FS); } catch { } try { if (Info.IsSingle) { if (_SingleFrames.Contains(FrameID)) { _SingleFrames.Remove(FrameID); } _SingleFrames.Add(FrameID, F); return(true); } else { foreach (FrameCollectionBase Coll in _CollectionFrames.Values) { if (Coll.CollectionType == Info.ClassType) { Coll.Remove(F.FrameID); Coll.Add(F.FrameID, F); return(true); } } } AddError(new ID3Exception("ClassType not found in Collection list", FrameID, ExceptionLevels.Error)); } catch {} } return(false); }
/// <summary> /// Read all frames from specific FileStream /// </summary> /// <param name="Data">FileStream to read data from</param> /// <param name="Length">Length of data to read from FileStream</param> private void ReadFrames(int Length) { string FrameID; int FrameLength; FrameFlags Flags = new FrameFlags(); byte Buf; // If ID3v2 is ID3v2.2 FrameID, FrameLength of Frames is 3 byte // otherwise it's 4 character int FrameIDLen = Version.Minor == 2 ? 3 : 4; // Minimum frame size is 10 because frame header is 10 byte while (Length > 10) { // check for padding( 00 bytes ) Buf = TStream.ReadByte(TStream.FS); if (Buf == 0) { Length--; continue; } // if readed byte is not zero. it must read as FrameID TStream.FS.Seek(-1, SeekOrigin.Current); // ---------- Read Frame Header ----------------------- FrameID = TStream.ReadText(FrameIDLen, TextEncodings.Ascii); if (FrameIDLen == 3) { FrameID = FramesInfo.Get4CharID(FrameID); } if (!FramesInfo.ExcludedList.All(t => t == FrameID)) { try { var len = Convert.ToInt32(TStream.ReadUInt(FrameIDLen)); FrameLength = len; } catch { FrameLength = 0; } if (FrameIDLen == 4) { Flags = (FrameFlags)TStream.ReadUInt(2); } else { Flags = 0; // must set to default flag } long Position = TStream.FS.Position; if (Length > 0x10000000) { throw (new FileLoadException("This file contain frame that have more than 256MB data. This is not valid for ID3.")); } bool Added = false; if (IsAddable(FrameID)) // Check if frame is not filter { Added = AddFrame(FrameID, FrameLength, Flags, TStream.FS); } if (!Added) { // if don't read this frame // we must go forward to read next frame TStream.FS.Position = Position + FrameLength; } Length -= FrameLength + 10; // 10 for Frame Header header } } }