Skip to content

Commit

Permalink
0.0.6 beta4 (Muraad#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkis117 committed Jun 10, 2018
1 parent 71a799a commit 63afbf3
Show file tree
Hide file tree
Showing 21 changed files with 742 additions and 782 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace MimeDetective.Analyzers
{
public sealed class ArrayBasedTrie : IFileAnalyzer
public sealed class ArrayTrie : IFileAnalyzer
{
private const int NullStandInValue = 256;
private const int MaxNodeSize = 257;
Expand All @@ -14,7 +14,7 @@ public sealed class ArrayBasedTrie : IFileAnalyzer
/// <summary>
/// Constructs an empty ArrayBasedTrie, <see cref="Insert(FileType)"/> to add definitions
/// </summary>
public ArrayBasedTrie()
public ArrayTrie()
{
OffsetNodes[0] = new OffsetNode(0);
}
Expand All @@ -23,10 +23,10 @@ public ArrayBasedTrie()
/// Constructs an ArrayBasedTrie from an Enumerable of FileTypes, <see cref="Insert(FileType)"/> to add more definitions
/// </summary>
/// <param name="types"></param>
public ArrayBasedTrie(IEnumerable<FileType> types)
public ArrayTrie(IEnumerable<FileType> types)
{
if (types is null)
throw new ArgumentNullException(nameof(types));
ThrowHelpers.FileTypeEnumerableIsNull();

OffsetNodes[0] = new OffsetNode(0);

Expand Down Expand Up @@ -55,7 +55,7 @@ public FileType Search(in ReadResult readResult)
int currentVal = readResult.Array[i];
Node node = prevNode[currentVal];

if (node.Children == null)
if (node.Children is null)
{
node = prevNode[NullStandInValue];

Expand Down Expand Up @@ -83,7 +83,7 @@ public FileType Search(in ReadResult readResult)
public void Insert(FileType type)
{
if (type is null)
throw new ArgumentNullException(nameof(type));
ThrowHelpers.FileTypeArgumentIsNull();

ref OffsetNode match = ref OffsetNodes[0];
bool matchFound = false;
Expand All @@ -102,17 +102,17 @@ public void Insert(FileType type)

if (!matchFound)
{
int newNodePos = offsetNodesLength;

if (newNodePos >= OffsetNodes.Length)
if (offsetNodesLength >= OffsetNodes.Length)
{
int newOffsetNodeCount = OffsetNodes.Length * 2 + 1;
//TODO put max size check
int newOffsetNodeCalc = OffsetNodes.Length * 2;
int newOffsetNodeCount = newOffsetNodeCalc > 560 ? 560 : newOffsetNodeCalc;
var newOffsetNodes = new OffsetNode[newOffsetNodeCount];
Array.Copy(OffsetNodes, newOffsetNodes, offsetNodesLength);
OffsetNodes = newOffsetNodes;
}

match = ref OffsetNodes[newNodePos];
match = ref OffsetNodes[offsetNodesLength];
match = new OffsetNode(type.HeaderOffset);
offsetNodesLength++;
}
Expand All @@ -121,19 +121,15 @@ public void Insert(FileType type)

for (int i = 0; i < type.Header.Length; i++)
{
byte? value = type.Header[i];
int arrayPos = value ?? NullStandInValue;
int arrayPos = type.Header[i] ?? NullStandInValue;
ref Node node = ref prevNode[arrayPos];

if (node.Children is null)
{
FileType record = null;
//TODO maybe short circuit it
if (i == type.Header.Length - 1)
node.Record = type;

if (i == type.Header.Length - 1)
record = type;

node = new Node(record);
}
if (node.Children is null)
node.Children = new Node[MaxNodeSize];

prevNode = node.Children;
}
Expand All @@ -155,14 +151,7 @@ private struct Node
{
public Node[] Children;

//if complete node then this not null
public FileType Record;

public Node(FileType record)
{
Children = new Node[MaxNodeSize];
Record = record;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace MimeDetective.Analyzers
{
public sealed class DictionaryBasedTrie : IFileAnalyzer
public sealed class DictionaryTrie : IFileAnalyzer
{
private const ushort NullStandInValue = 256;

Expand All @@ -14,7 +14,7 @@ public sealed class DictionaryBasedTrie : IFileAnalyzer
/// <summary>
/// Constructs an empty DictionaryBasedTrie
/// </summary>
public DictionaryBasedTrie()
public DictionaryTrie()
{

}
Expand All @@ -23,14 +23,15 @@ public DictionaryBasedTrie()
/// Constructs a DictionaryBasedTrie from an Enumerable of FileTypes
/// </summary>
/// <param name="types"></param>
public DictionaryBasedTrie(IEnumerable<FileType> types)
public DictionaryTrie(IEnumerable<FileType> types)
{
if (types is null)
throw new ArgumentNullException(nameof(types));
ThrowHelpers.FileTypeEnumerableIsNull();

foreach (var type in types)
{
Insert(type);
if ((object)type != null)
Insert(type);
}
}

Expand Down Expand Up @@ -69,7 +70,7 @@ public FileType Search(in ReadResult readResult)
public void Insert(FileType type)
{
if (type is null)
throw new ArgumentNullException(nameof(type));
ThrowHelpers.FileTypeArgumentIsNull();

if (!Nodes.TryGetValue(type.HeaderOffset, out var offsetNode))
{
Expand Down
10 changes: 10 additions & 0 deletions src/Mime-Detective/Analyzers/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,15 @@ public static void GreaterThanMaxHeaderSize()
{
throw new ArgumentException("Offset cannot be greater than MaxHeaderSize - 1");
}

public static void FileTypeArgumentIsNull()
{
throw new ArgumentNullException("FileType argument cannot be null");
}

public static void FileTypeEnumerableIsNull()
{
throw new ArgumentNullException("FileType Enumerable cannot be null");
}
}
}
17 changes: 9 additions & 8 deletions src/Mime-Detective/Analyzers/HybridTrie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ public HybridTrie()
public HybridTrie(IEnumerable<FileType> types)
{
if (types is null)
throw new ArgumentNullException(nameof(types));
ThrowHelpers.FileTypeArgumentIsNull();

OffsetNodes[0] = new OffsetNode(0);

foreach (var type in types)
{
Insert(type);
if ((object)type != null)
Insert(type);
}
}

Expand Down Expand Up @@ -106,7 +107,7 @@ public FileType Search(in ReadResult readResult)
public void Insert(FileType type)
{
if (type is null)
throw new ArgumentNullException(nameof(type));
ThrowHelpers.FileTypeArgumentIsNull();

ref OffsetNode match = ref OffsetNodes[0];
bool matchFound = false;
Expand All @@ -126,17 +127,16 @@ public void Insert(FileType type)
//handle expanding collection
if (!matchFound)
{
int newNodePos = offsetNodesLength;

if (newNodePos >= OffsetNodes.Length)
if (offsetNodesLength >= OffsetNodes.Length)
{
int newOffsetNodeCount = OffsetNodes.Length * 2 + 1;
int newOffsetNodeCalc = OffsetNodes.Length * 2;
int newOffsetNodeCount = newOffsetNodeCalc > 560 ? 560 : newOffsetNodeCalc;
var newOffsetNodes = new OffsetNode[newOffsetNodeCount];
Array.Copy(OffsetNodes, newOffsetNodes, offsetNodesLength);
OffsetNodes = newOffsetNodes;
}

match = ref OffsetNodes[newNodePos];
match = ref OffsetNodes[offsetNodesLength];
match = new OffsetNode(type.HeaderOffset);
offsetNodesLength++;
}
Expand Down Expand Up @@ -175,6 +175,7 @@ public void Insert(FileType type)
node.Record = type;
}

//TODO make a base-1 dict
private sealed class Node
{
//if complete node then this not null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,74 +1,80 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace MimeDetective.Analyzers
{
public class LinearCountingAnalyzer : IFileAnalyzer
public class LinearCounting : IFileAnalyzer
{
private readonly List<FileType> types;
private FileType[] types = new FileType[20];
private int typesLength = 0;

/// <summary>
/// Constructs an empty LinearCountingAnalyzer, use <see cref="Insert(FileType)"/> to add file types
/// </summary>
public LinearCountingAnalyzer()
public LinearCounting()
{
types = new List<FileType>();
}

/// <summary>
/// Constructs a LinearCountingAnalyzer using the supplied IEnumerable<FileType>
/// </summary>
/// <param name="fileTypes"></param>
public LinearCountingAnalyzer(IEnumerable<FileType> fileTypes)
public LinearCounting(IEnumerable<FileType> fileTypes)
{
if (fileTypes is null)
throw new ArgumentNullException(nameof(fileTypes));

types = new List<FileType>();
ThrowHelpers.FileTypeEnumerableIsNull();

foreach (var fileType in fileTypes)
{
if ((object)fileType != null)
Insert(fileType);
}

//types.OrderBy(x => x.HeaderOffset);
//todo sort
//Array.Sort<FileType>(types, (x,y) => x.HeaderOffset.CompareTo(y.HeaderOffset));
//types = types;
}

public void Insert(FileType fileType)
{
if (fileType is null)
throw new ArgumentNullException(nameof(fileType));
ThrowHelpers.FileTypeArgumentIsNull();

if (typesLength >= types.Length)
{
int newTypesCount = types.Length * 2;
var newTypes = new FileType[newTypesCount];
Array.Copy(types, newTypes, typesLength);
types = newTypes;
}

types.Add(fileType);
types[typesLength] = fileType;
typesLength++;
}

public FileType Search(in ReadResult readResult)
{
if (readResult.ReadLength == 0)
return null;

uint highestMatchingCount = 0;
FileType highestMatchingType = null;

// compare the file header to the stored file headers
for (int typeIndex = 0; typeIndex < types.Count; typeIndex++)
for (int typeIndex = 0; typeIndex < typesLength; typeIndex++)
{
FileType type = types[typeIndex];

uint matchingCount = 0;
int iOffset = type.HeaderOffset;
int readEnd = iOffset + type.Header.Length;

if (readEnd > readResult.ReadLength)
continue;

for (int i = 0; iOffset < readEnd; i++, iOffset++)
for (int i = 0, iOffset = type.HeaderOffset; iOffset < readResult.ReadLength && i < type.Header.Length; i++, iOffset++)
{
if (type.Header[i] is null || type.Header[i].Value == readResult.Array[iOffset])
matchingCount++;
else
break;
}

//TODO should this be default behavior
//TODO should this be default behavior?
if (type.Header.Length == matchingCount && matchingCount >= highestMatchingCount)
{
highestMatchingType = type;
Expand Down
Loading

0 comments on commit 63afbf3

Please sign in to comment.