Skip to content

Commit

Permalink
Refactored some code for later use
Browse files Browse the repository at this point in the history
  • Loading branch information
AnTasMes committed Aug 10, 2024
1 parent 893758e commit 0cdd05a
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 21 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,5 @@ FodyWeavers.xsd

# JetBrains Rider
*.sln.iml
/File.Validator/File.Validator.MVC.Test
/File.Validator/File.Validator.Test
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions File.Validator/File.Validator/File.Validator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
</PropertyGroup>

</Project>
19 changes: 19 additions & 0 deletions File.Validator/File.Validator/FileExtensions/FileExtension.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace File.Validator.FileExtensions
{
Expand All @@ -9,5 +10,23 @@ public abstract class FileExtensionInfo
public abstract string Name { get; }
public abstract int Offset { get; }
public abstract string[] Hex { get; }

public virtual bool IsValid (BinaryReader reader)
{
// Postavi offset
reader.BaseStream.Seek(this.Offset, SeekOrigin.Begin);

// Izvlaci broj bajtova u najduzem hex potpisu
string longestSignature = this.Hex.OrderByDescending(h => h.Length).First();
int longestSignatureLen = Regex.Matches(longestSignature, "[a-zA-Z0-9][a-zA-Z0-9]").Count;

// Procitaj N bajtova iz niza
byte[] data = reader.ReadBytes(longestSignatureLen);
string data_as_hex = BitConverter.ToString(data);
reader.Close();

// Proveriti da li data_as_hex poseduje neki do potpisa u nizu
return this.Hex.Any(h => data_as_hex.Contains(h));
}
}
}
23 changes: 2 additions & 21 deletions File.Validator/File.Validator/FileValidator.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
using File.Validator;
using File.Validator.FileExtensions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using File.Validator.FileExtensions;
using System.Reflection;
using System.Text.RegularExpressions;

namespace File.Validator
{
Expand Down Expand Up @@ -43,20 +37,7 @@ public static bool isValidExtension(Stream stream, string expectedExtension)
if (fileExtensionInfo == null)
return false;

// Postavi offset
reader.BaseStream.Seek(fileExtensionInfo.Offset, SeekOrigin.Begin);

// Izvlaci broj bajtova u najduzem hex potpisu
string longestSignature = fileExtensionInfo.Hex.OrderByDescending(h => h.Length).First();
int longestSignatureLen = Regex.Matches(longestSignature, "[a-zA-Z0-9][a-zA-Z0-9]").Count;

// Procitaj N bajtova iz niza
byte[] data = reader.ReadBytes(longestSignatureLen);
string data_as_hex = BitConverter.ToString(data);
reader.Close();

// Proveriti da li data_as_hex poseduje neki do potpisa u nizu
return fileExtensionInfo.Hex.Any(h => data_as_hex.Contains(h));
return fileExtensionInfo.IsValid(reader);
}

/// <summary>
Expand Down

0 comments on commit 0cdd05a

Please sign in to comment.