private void MakeMatches() { Matchlet[] matchlets = new Matchlet [30]; while (br.PeekChar() != -1) { int priority = -1; string mime_type = ReadPriorityAndMimeType(ref priority); if (mime_type != null) { Match match = new Match(); match.Priority = priority; match.MimeType = mime_type; while (true) { int indent = 0; // indent char c; if (br.PeekChar() != '>') { StringBuilder indent_string = new StringBuilder(); //string indent_string = String.Empty; while (true) { if (br.PeekChar() == '>') { break; } c = br.ReadChar(); //indent_string += c; indent_string.Append(c); } indent = Convert.ToInt32(indent_string.ToString()); } int offset = 0; // offset if (br.PeekChar() == '>') { br.ReadChar(); offset = ReadValue(); } int value_length = 0; byte[] value = null; // value length and value if (br.PeekChar() == '=') { br.ReadChar(); // read 2 bytes value length (always big endian) byte first = br.ReadByte(); byte second = br.ReadByte(); value_length = first * 256 + second; value = br.ReadBytes(value_length); } // mask byte[] mask = null; if (br.PeekChar() == '&') { br.ReadChar(); mask = br.ReadBytes(value_length); } // word_size int word_size = 1; if (br.PeekChar() == '~') { br.ReadChar(); c = br.ReadChar(); word_size = Convert.ToInt32(c - 0x30); // data is stored in big endian format. if (word_size > 1 && System.BitConverter.IsLittleEndian) { //convert the value and, if available, the mask data to little endian if (word_size == 2) { if (value != null) { for (int i = 0; i < value.Length; i += 2) { byte one = value [i]; byte two = value [i + 1]; value [i] = two; value [i + 1] = one; } } if (mask != null) { for (int i = 0; i < mask.Length; i += 2) { byte one = mask [i]; byte two = mask [i + 1]; mask [i] = two; mask [i + 1] = one; } } } else if (word_size == 4) { if (value != null) { for (int i = 0; i < value.Length; i += 4) { byte one = value [i]; byte two = value [i + 1]; byte three = value [i + 2]; byte four = value [i + 3]; value [i] = four; value [i + 1] = three; value [i + 2] = two; value [i + 3] = one; } } if (mask != null) { for (int i = 0; i < mask.Length; i += 4) { byte one = mask [i]; byte two = mask [i + 1]; byte three = mask [i + 2]; byte four = mask [i + 3]; mask [i] = four; mask [i + 1] = three; mask [i + 2] = two; mask [i + 3] = one; } } } } } // range length int range_length = 1; if (br.PeekChar() == '+') { br.ReadChar(); range_length = ReadValue(); } // read \n br.ReadChar(); // create the matchlet matchlets [indent] = new Matchlet(); matchlets [indent].Offset = offset; matchlets [indent].OffsetLength = range_length; matchlets [indent].ByteValue = value; if (mask != null) { matchlets [indent].Mask = mask; } if (indent == 0) { match.Matchlets.Add(matchlets [indent]); } else { matchlets [indent - 1].Matchlets.Add(matchlets [indent]); } if (max_offset_and_range < matchlets [indent].Offset + matchlets [indent].OffsetLength + matchlets [indent].ByteValue.Length + 1) { max_offset_and_range = matchlets [indent].Offset + matchlets [indent].OffsetLength + matchlets [indent].ByteValue.Length + 1; } // if '[' move to next mime type if (br.PeekChar() == '[') { break; } } if (priority < 80) { Mime.MatchesBelow80.Add(match); } else { Mime.Matches80Plus.Add(match); } } } }
private void MakeMatches () { Matchlet[] matchlets = new Matchlet [30]; while (br.PeekChar () != -1) { int priority = -1; string mime_type = ReadPriorityAndMimeType (ref priority); if (mime_type != null) { Match match = new Match (); match.Priority = priority; match.MimeType = mime_type; while (true) { int indent = 0; // indent char c; if (br.PeekChar () != '>') { StringBuilder indent_string = new StringBuilder (); //string indent_string = String.Empty; while (true) { if (br.PeekChar () == '>') break; c = br.ReadChar (); //indent_string += c; indent_string.Append (c); } indent = Convert.ToInt32 (indent_string.ToString ()); } int offset = 0; // offset if (br.PeekChar () == '>') { br.ReadChar (); offset = ReadValue (); } int value_length = 0; byte[] value = null; // value length and value if (br.PeekChar () == '=') { br.ReadChar (); // read 2 bytes value length (always big endian) byte first = br.ReadByte (); byte second = br.ReadByte (); value_length = first * 256 + second; value = br.ReadBytes (value_length); } // mask byte[] mask = null; if (br.PeekChar () == '&') { br.ReadChar (); mask = br.ReadBytes (value_length); } // word_size int word_size = 1; if (br.PeekChar () == '~') { br.ReadChar (); c = br.ReadChar (); word_size = Convert.ToInt32 (c - 0x30); // data is stored in big endian format. if (word_size > 1 && System.BitConverter.IsLittleEndian) { //convert the value and, if available, the mask data to little endian if (word_size == 2) { if (value != null) { for (int i = 0; i < value.Length; i += 2) { byte one = value [i]; byte two = value [i + 1]; value [i] = two; value [i + 1] = one; } } if (mask != null) { for (int i = 0; i < mask.Length; i += 2) { byte one = mask [i]; byte two = mask [i + 1]; mask [i] = two; mask [i + 1] = one; } } } else if (word_size == 4) { if (value != null) { for (int i = 0; i < value.Length; i += 4) { byte one = value [i]; byte two = value [i + 1]; byte three = value [i + 2]; byte four = value [i + 3]; value [i] = four; value [i + 1] = three; value [i + 2] = two; value [i + 3] = one; } } if (mask != null) { for (int i = 0; i < mask.Length; i += 4) { byte one = mask [i]; byte two = mask [i + 1]; byte three = mask [i + 2]; byte four = mask [i + 3]; mask [i] = four; mask [i + 1] = three; mask [i + 2] = two; mask [i + 3] = one; } } } } } // range length int range_length = 1; if (br.PeekChar () == '+') { br.ReadChar (); range_length = ReadValue (); } // read \n br.ReadChar (); // create the matchlet matchlets [indent] = new Matchlet (); matchlets [indent].Offset = offset; matchlets [indent].OffsetLength = range_length; matchlets [indent].ByteValue = value; if (mask != null) matchlets [indent].Mask = mask; if (indent == 0) { match.Matchlets.Add (matchlets [indent]); } else { matchlets [indent - 1].Matchlets.Add (matchlets [indent]); } if (max_offset_and_range < matchlets [indent].Offset + matchlets [indent].OffsetLength + matchlets [indent].ByteValue.Length + 1) max_offset_and_range = matchlets [indent].Offset + matchlets [indent].OffsetLength + matchlets [indent].ByteValue.Length + 1; // if '[' move to next mime type if (br.PeekChar () == '[') break; } if (priority < 80) Mime.MatchesBelow80.Add (match); else Mime.Matches80Plus.Add (match); } } }
private bool TestMatchlet(Matchlet matchlet) { // using a simple brute force search algorithm // compare each (masked) value from the buffer with the (masked) value from the matchlet // no need to check if the offset + the bytevalue length exceed the # bytes read if (matchlet.Offset + matchlet.ByteValue.Length > bytes_read) { return(false); } for (int offset_counter = 0; offset_counter < matchlet.OffsetLength; offset_counter++) { if (matchlet.Offset + offset_counter + matchlet.ByteValue.Length > bytes_read) { return(false); } if (matchlet.Mask == null) { if (buffer [matchlet.Offset + offset_counter] == matchlet.ByteValue [0]) { if (matchlet.ByteValue.Length == 1) { if (matchlet.Matchlets.Count > 0) { foreach (Matchlet sub_matchlet in matchlet.Matchlets) { if (TestMatchlet(sub_matchlet)) { return(true); } } } else { return(true); } } int minus = 0; // check if the last matchlet byte value is the same as the byte value in the buffer... if (matchlet.ByteValue.Length > 2) { if (buffer [matchlet.Offset + offset_counter + matchlet.ByteValue.Length - 1] != matchlet.ByteValue [matchlet.ByteValue.Length - 1]) { return(false); } minus = 1; } for (int i = 1; i < matchlet.ByteValue.Length - minus; i++) { if (buffer [matchlet.Offset + offset_counter + i] != matchlet.ByteValue [i]) { return(false); } } if (matchlet.Matchlets.Count > 0) { foreach (Matchlet sub_matchlets in matchlet.Matchlets) { if (TestMatchlet(sub_matchlets)) { return(true); } } } else { return(true); } } } else { if ((buffer [matchlet.Offset + offset_counter] & matchlet.Mask [0]) == (matchlet.ByteValue [0] & matchlet.Mask [0])) { if (matchlet.ByteValue.Length == 1) { if (matchlet.Matchlets.Count > 0) { foreach (Matchlet sub_matchlets in matchlet.Matchlets) { if (TestMatchlet(sub_matchlets)) { return(true); } } } else { return(true); } } int minus = 0; // check if the last matchlet byte value is the same as the byte value in the buffer... if (matchlet.ByteValue.Length > 2) { if ((buffer [matchlet.Offset + offset_counter + matchlet.ByteValue.Length - 1] & matchlet.Mask [matchlet.ByteValue.Length - 1]) != (matchlet.ByteValue [matchlet.ByteValue.Length - 1] & matchlet.Mask [matchlet.ByteValue.Length - 1])) { return(false); } minus = 1; } for (int i = 1; i < matchlet.ByteValue.Length - minus; i++) { if ((buffer [matchlet.Offset + offset_counter + i] & matchlet.Mask [i]) != (matchlet.ByteValue [i] & matchlet.Mask [i])) { return(false); } } if (matchlet.Matchlets.Count > 0) { foreach (Matchlet sub_matchlets in matchlet.Matchlets) { if (TestMatchlet(sub_matchlets)) { return(true); } } } else { return(true); } } } } return(false); }
private bool TestMatchlet (Matchlet matchlet) { // using a simple brute force search algorithm // compare each (masked) value from the buffer with the (masked) value from the matchlet // no need to check if the offset + the bytevalue length exceed the # bytes read if (matchlet.Offset + matchlet.ByteValue.Length > bytes_read) return false; for (int offset_counter = 0; offset_counter < matchlet.OffsetLength; offset_counter++) { if (matchlet.Offset + offset_counter + matchlet.ByteValue.Length > bytes_read) return false; if (matchlet.Mask == null) { if (buffer [matchlet.Offset + offset_counter] == matchlet.ByteValue [0]) { if (matchlet.ByteValue.Length == 1) { if (matchlet.Matchlets.Count > 0) { foreach (Matchlet sub_matchlet in matchlet.Matchlets) { if (TestMatchlet (sub_matchlet)) return true; } } else return true; } int minus = 0; // check if the last matchlet byte value is the same as the byte value in the buffer... if (matchlet.ByteValue.Length > 2) { if (buffer [matchlet.Offset + offset_counter + matchlet.ByteValue.Length - 1] != matchlet.ByteValue [matchlet.ByteValue.Length - 1]) return false; minus = 1; } for (int i = 1; i < matchlet.ByteValue.Length - minus; i++) { if (buffer [matchlet.Offset + offset_counter + i] != matchlet.ByteValue [i]) return false; } if (matchlet.Matchlets.Count > 0) { foreach (Matchlet sub_matchlets in matchlet.Matchlets) { if (TestMatchlet (sub_matchlets)) return true; } } else return true; } } else { if ((buffer [matchlet.Offset + offset_counter] & matchlet.Mask [0]) == (matchlet.ByteValue [0] & matchlet.Mask [0])) { if (matchlet.ByteValue.Length == 1) { if (matchlet.Matchlets.Count > 0) { foreach (Matchlet sub_matchlets in matchlet.Matchlets) { if (TestMatchlet (sub_matchlets)) return true; } } else return true; } int minus = 0; // check if the last matchlet byte value is the same as the byte value in the buffer... if (matchlet.ByteValue.Length > 2) { if ((buffer [matchlet.Offset + offset_counter + matchlet.ByteValue.Length - 1] & matchlet.Mask [matchlet.ByteValue.Length - 1]) != (matchlet.ByteValue [matchlet.ByteValue.Length - 1] & matchlet.Mask [matchlet.ByteValue.Length - 1])) return false; minus = 1; } for (int i = 1; i < matchlet.ByteValue.Length - minus; i++) { if ((buffer [matchlet.Offset + offset_counter + i] & matchlet.Mask [i]) != (matchlet.ByteValue [i] & matchlet.Mask [i])) return false; } if (matchlet.Matchlets.Count > 0) { foreach (Matchlet sub_matchlets in matchlet.Matchlets) { if (TestMatchlet (sub_matchlets)) return true; } } else return true; } } } return false; }