private void Parse(ByteVector data) { String currentKey, currentValue; int keyLen, valueLen; try { do { keyLen = (int)data.ToUInt(true); data.RemoveRange(0, 4); valueLen = (int)data.ToUInt(true); data.RemoveRange(0, 4); currentKey = data.ToString(TagLib.StringType.UTF8, 0, keyLen); data.RemoveRange(0, keyLen); currentValue = data.ToString(TagLib.StringType.UTF8, 0, valueLen); data.RemoveRange(0, valueLen); tags.Add(new KeyValuePair <string, string>(currentKey, currentValue)); if (data.Count != 0) { data.RemoveRange(0, 1); } }while(data.Count >= 4); } catch (Exception) { } if (data.Count != 0) { throw new CorruptFileException(); } }
protected override void ParseFields(ByteVector data, byte version) { if (data.Count < 4) { throw new CorruptFileException( "An object frame must contain at least 4 bytes."); } int start = 0; text_encoding = (StringType)data [start++]; int end = data.Find( ByteVector.TextDelimiter(StringType.Latin1), start); if (end < start) { return; } mime_type = data.ToString(StringType.Latin1, start, end - start); ByteVector delim = ByteVector.TextDelimiter( text_encoding); start = end + 1; end = data.Find(delim, start, delim.Count); if (end < start) { return; } file_name = data.ToString(text_encoding, start, end - start); start = end + delim.Count; end = data.Find(delim, start, delim.Count); if (end < start) { return; } description = data.ToString(text_encoding, start, end - start); start = end + delim.Count; data.RemoveRange(0, start); this.data = data; }
/// <summary> /// Populates the current instance by parsing the contents of /// a raw AudibleMetadata tag. /// </summary> /// <param name="data"> /// A <see cref="ByteVector" /> object containing the whole tag /// object /// </param> /// <exception cref="CorruptFileException"> /// <paramref name="data" /> is less than 128 bytes or does /// not start with <see cref="FileIdentifier" />. /// </exception> private void Parse(ByteVector data) { String currentKey, currentValue; int keyLen, valueLen; try { do { keyLen = (int)data.ToUInt(true); data.RemoveRange(0, 4); valueLen = (int)data.ToUInt(true); data.RemoveRange(0, 4); currentKey = data.ToString(TagLib.StringType.UTF8, 0, keyLen); data.RemoveRange(0, keyLen); currentValue = data.ToString(TagLib.StringType.UTF8, 0, valueLen); data.RemoveRange(0, valueLen); tags.Add(new KeyValuePair <string, string>(currentKey, currentValue)); //StringHandle (currentKey, currentValue); // if it is not the last item remove the end byte (null terminated) if (data.Count != 0) { data.RemoveRange(0, 1); } }while (data.Count >= 4); } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e); // } if (data.Count != 0) { throw new CorruptFileException(); } }
protected void ParseRawData() { if (raw_data == null) { return; } int pos = 0; int offset; text_encoding = (StringType)raw_data[pos++]; if (raw_version > 2) { offset = raw_data.Find(ByteVector.TextDelimiter(StringType.Latin1), pos); if (offset < pos) { return; } mime_type = raw_data.ToString(StringType.Latin1, pos, offset - pos); pos = offset + 1; } else { ByteVector ext = raw_data.Mid(pos, 3); if (ext == "JPG") { mime_type = "image/jpeg"; } else if (ext == "PNG") { mime_type = "image/png"; } else { mime_type = "image/unknown"; } pos += 3; } ByteVector delim = ByteVector.TextDelimiter(text_encoding); type = (PictureType)raw_data[pos++]; offset = raw_data.Find(delim, pos, delim.Count); if (offset < pos) { return; } description = raw_data.ToString(text_encoding, pos, offset - pos); pos = offset + delim.Count; raw_data.RemoveRange(0, pos); this.data = raw_data; this.raw_data = null; }
/// <summary> /// Performs the actual parsing of the raw data. /// </summary> /// <remarks> /// Because of the high parsing cost and relatively low usage /// of the class, <see cref="ParseFields" /> only stores the /// field data so it can be parsed on demand. Whenever a /// property or method is called which requires the data, /// this method is called, and only on the first call does it /// actually parse the data. /// </remarks> protected void ParseRawData() { if (file != null) { Load(); } if (raw_data == null) { return; } data = raw_data; raw_data = null; int pos = 0; int offset; encoding = (StringType)data[pos++]; ByteVector delim = ByteVector.TextDelimiter(encoding); if (header.FrameId == FrameType.APIC) { // Retrieve an ID3v2 Attached Picture (APIC) if (raw_version > 2) { offset = data.Find(ByteVector.TextDelimiter(StringType.Latin1), pos); if (offset < pos) { return; } mime_type = data.ToString(StringType.Latin1, pos, offset - pos); pos = offset + 1; } else { ByteVector ext = data.Mid(pos, 3); mime_type = Picture.GetMimeFromExtension(ext.ToString()); pos += 3; } Type = (PictureType)data[pos++]; offset = data.Find(delim, pos, delim.Count); } else if (header.FrameId == FrameType.GEOB) { // Retrieve an ID3v2 General Encapsulated Object (GEOB) offset = data.Find(ByteVector.TextDelimiter(StringType.Latin1), pos); if (offset < pos) { return; } mime_type = data.ToString(StringType.Latin1, pos, offset - pos); pos = offset + 1; offset = data.Find(delim, pos, delim.Count); if (offset < pos) { return; } filename = data.ToString(encoding, pos, offset - pos); pos = offset + delim.Count; offset = data.Find(delim, pos, delim.Count); Type = PictureType.NotAPicture; } else { throw new InvalidOperationException("Bad Frame type"); } if (offset < pos) { return; } description = data.ToString(encoding, pos, offset - pos); pos = offset + delim.Count; data.RemoveRange(0, pos); }
protected override void ParseFields(ByteVector data, byte version) { if (data.Count < 4) { throw new CorruptFileException("An object frame must contain at least 4 bytes."); } int offset = 0; this.encoding = (StringType) data[offset++]; int num2 = data.Find(ByteVector.TextDelimiter(StringType.Latin1), offset); if (num2 >= offset) { this.mime_type = data.ToString(StringType.Latin1, offset, num2 - offset); ByteVector pattern = ByteVector.TextDelimiter(this.encoding); offset = num2 + 1; num2 = data.Find(pattern, offset, pattern.Count); if (num2 >= offset) { this.file_name = data.ToString(this.encoding, offset, num2 - offset); offset = num2 + pattern.Count; num2 = data.Find(pattern, offset, pattern.Count); if (num2 >= offset) { this.description = data.ToString(this.encoding, offset, num2 - offset); offset = num2 + pattern.Count; data.RemoveRange(0, offset); this.data = data; } } } }
/// <summary> /// Populates the values in the current instance by parsing /// its field data in a specified version. /// </summary> /// <param name="data"> /// A <see cref="ByteVector" /> object containing the /// extracted field data. /// </param> /// <param name="version"> /// A <see cref="byte" /> indicating the ID3v2 version the /// field data is encoded in. /// </param> /// <exception cref="CorruptFileException"> /// <paramref name="data" /> contains less than 5 bytes. /// </exception> protected override void ParseFields(ByteVector data, byte version) { if (data.Count < 4) throw new CorruptFileException ( "An object frame must contain at least 4 bytes."); int start = 0; encoding = (StringType) data [start++]; int end = data.Find ( ByteVector.TextDelimiter (StringType.Latin1), start); if (end < start) return; mime_type = data.ToString (StringType.Latin1, start, end - start); ByteVector delim = ByteVector.TextDelimiter ( encoding); start = end + 1; end = data.Find (delim, start, delim.Count); if (end < start) return; file_name = data.ToString (encoding, start, end - start); start = end + delim.Count; end = data.Find (delim, start, delim.Count); if (end < start) return; description = data.ToString (encoding, start, end - start); start = end + delim.Count; data.RemoveRange (0, start); this.data = data; }
/// <summary> /// Populates the current instance by parsing the contents of /// a raw AudibleMetadata tag. /// </summary> /// <param name="data"> /// A <see cref="ByteVector" /> object containing the whole tag /// object /// </param> /// <exception cref="CorruptFileException"> /// <paramref name="data" /> is less than 128 bytes or does /// not start with <see cref="FileIdentifier" />. /// </exception> private void Parse (ByteVector data) { String currentKey, currentValue; int keyLen, valueLen; try { do { keyLen = (int) data.ToUInt(true); data.RemoveRange (0, 4); valueLen = (int) data.ToUInt(true); data.RemoveRange (0, 4); currentKey = data.ToString ( TagLib.StringType.UTF8, 0, keyLen ); data.RemoveRange (0, keyLen); currentValue = data.ToString ( TagLib.StringType.UTF8, 0, valueLen ); data.RemoveRange (0, valueLen); tags.Add( new KeyValuePair<string, string>(currentKey, currentValue) ); //StringHandle (currentKey, currentValue); // if it is not the last item remove the end byte (null terminated) if (data.Count != 0) data.RemoveRange(0,1); } while (data.Count >= 4); } catch (Exception) { // } if (data.Count != 0) throw new CorruptFileException(); }