C# linq expression. How to get different offsets and 'any' offset? - c#

I want to read out the header of a file and compare it to a given signature. The start of the signature can have an offset.
This is my current function:
public FileTypeVerifyResult Verify(Stream stream)
{
stream.Position = 0;
var reader = new BinaryReader(stream);
var headerBytes = reader.ReadBytes(SignatureLength + this.OffSet);
return new FileTypeVerifyResult
{
Name = Name,
Description = Description,
IsVerified = Signatures.Any(signature =>
headerBytes.Skip(this.OffSet).Take(signature.Length)
.SequenceEqual(signature)
)
};
}
This currently works with one offset but extensions exists that can have multiple offsets. So my first thought was to make the OffSet property an int[] with all offsets but then I don't know if this can be easily build into this linq expression.
Also, the file can have the offset any (or any other value that means anywhere in the file like just -1). How could such a thing build in?

To be able to handle more offsets, this is my solution:
protected List<int> OffSets { get; set; }
public int OffSetLength => OffSets.Max(m => m);
public FileTypeVerifyResult Verify(Stream stream)
{
stream.Position = 0;
var reader = new BinaryReader(stream);
var headerBytes = reader.ReadBytes(SignatureLength + OffSetLength);
reader.Close();
return new FileTypeVerifyResult
{
Name = Name,
Description = Description,
IsVerified = Signatures.Any(signature =>
OffSets.Any(offSet =>
headerBytes.Skip(offSet).Take(signature.Length)
.SequenceEqual(signature))
)
};
}
I don't think I can get the any/-1 offset in this function without rewriting everything.

I think this is the case where a swiss knife is not what you want, Linq is good for many things, but it really doesn't add any value to the problem at hand IMHO because writing such a query will make ones eyeballs bleed, but here is my take at the problem itself with a sparse use of LINQ if at all :)
I have allowed myself to deduct missing items that were not supplied and rationalize naming a bit.
public class FileType
{
public class FileTypeVerifyResult
{
public string Name { get; set; }
public string Description { get; set; }
public bool IsVerified { get; set; }
}
public class Signature
{
/// <summary>
/// Actual signature
/// </summary>
public byte[] Payload { get; set; }
public int Length { get => Payload.Length; }
public List<int> OffSets { get; set; }
}
public string Name { get; set; }
public string Description { get; set; }
public List<Signature> Signatures { get; set; }
public FileTypeVerifyResult Verify(Stream stream)
{
if (stream == null) throw new ArgumentException(nameof(stream));
if(stream.CanSeek)
stream.Position = 0;
else
//TODO: Consider if we need this: throw new ArgumentException("Stream does not support seek", nameof(stream));
var bytes = new byte[stream.Length];
int wasRead = stream.Read(bytes, 0, (int)stream.Length);
if (wasRead == 0) throw new ArgumentException("Unable to read from stream");
foreach (var signature in Signatures)
{
foreach(var offset in signature.OffSets)
{
if(offset == -1)
{
//TODO: read signature length from each position first 0, then 1 until what is left is less than length of signature and if it fits
return createResultObject(true);
}
var candidateBytes = new byte[signature.Length];
candidateBytes = bytes.Skip(offset).Take(signature.Length).ToArray();
// I'd not use linq, instead Array.Copy(bytes, offset, candidateBytes, 0, signature.Length);
if(signature.Payload.Equals(candidateBytes))
{
return createResultObject(true);
}
}
}
return createResultObject();
FileTypeVerifyResult createResultObject(bool isValid = false)
{
return new FileTypeVerifyResult
{
Name = Name,
Description = Description,
IsVerified = isValid
};
}
}
}

Related

Unable to store and retrieve data from LiteDb

I am trying to write one object to LiteDb and read it back.
I have tried following code but somehow all parameters of LiteDbParamEntry object are returned as NULL or zero.
public class LiteDbParamEntry
{
[BsonId]
public int Id
{
get { return (Index * 0x100) + SubIndex; }
}
public ushort Index;
public ushort SubIndex;
public string Text;
}
public class LiteDbParamValueStorage
{
private const string _liteDbPath = "MyLiteData.db";
public LiteDbParamValueStorage()
{
WriteEntry(123, 25);
ReadEntry(123, 25);
}
public void WriteEntry(ushort index, ushort subIndex)
{
using (var db = new LiteDatabase(_liteDbPath))
{
var entry = new LiteDbParamEntry {Index = index, SubIndex = subIndex, Text = "SomeText"};
var entries = db.GetCollection<LiteDbParamEntry>("LiteDbParamEntry");
entries.Insert(entry);
}
}
public void ReadEntry(ushort index, ushort subIndex)
{
using (var db = new LiteDatabase(_liteDbPath))
{
var collection = db.GetCollection<LiteDbParamEntry>("LiteDbParamEntry");
var paramEntry = collection.FindById((index * 0x100) + subIndex);
if (paramEntry != null)
Console.WriteLine(paramEntry.Text); //paramEntry.Text is returned as Null
else
return;
}
}
}
Can somebody please point me in right direction?
Finally, i've found what's wrong with your code... You forgot to add { get; set; } instruction on the right side of field declaration ;)
Take a look here:
public class LiteDbParamEntry
{
[BsonId]
public int Id
{
get { return (Index * 0x100) + SubIndex; }
}
public ushort Index { get; set; } //getter/setter added
public ushort SubIndex { get; set; } //getter/setter added
public string Text { get; set; } //getter/setter added
}
Good luck!

Checking if data exist in sqlite-net before inserting them to database

Im making an Xamarin.Android application that will use "templates" , template is this:
[Table("Templates")]
public class Template
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public int Category { get; set; }
//[TextBlob("imagesBlobbed")]
[OneToMany, Unique]
public List<TemplateImage> TemplateImages { get; set; }
public string ImagesHash { get; set; }
//public string imagesBlobbed { get; set; }
}
[Table("TemplateImages")]
public class TemplateImage
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public int Category { get; set; }
public string ImagesHash { get; set; }
public int Image { get; set; }
[ForeignKey(typeof(Template))]
public int TemplateId { get; set; }
}
i want all the TemplateImages objects to be unique in my database, the attribute Unique doesnt do anything, cause i guess because TemplateImage table has an auto increment Id will always be unique, no matter if the Image or ImageHash is the same in 2 records.
I was thinking then how else i can be sure that TemplateImages will be unique (Notice: when i say unique i mean that there isn't any other List that have the exact Image for every TemplateImage, in the same order).
Also i was using the ResourceID of the images, as image, which is wrong cause they might change on every new compiled app update.
So i decided to make a hash md5 from the images. They only way that i have find to load resource images (my images are vector .xml files) in Xamarin.Android is by converting the resource to bitmap, and then convert the bitmap to byte, and then the byte to md5.
And also i should have a hash string for the template item too. so im creating an md5 hash for the template by combining all the byte[] of the images into one and then hasing that.
I create this crazy code for this job:
public static string GetMD5Hash(byte[] content)
{
using (var md5 = MD5.Create())
{
byte[] computedHash = md5.ComputeHash(Encoding.UTF8.GetBytes(BitConverter.ToString(content).Replace("-", "")));
return new System.Runtime.Remoting.Metadata.W3cXsd2001.SoapHexBinary(computedHash).ToString();
}
}
private static SQLiteConnection instance;
public static SQLiteConnection db()
{
if (instance == null)
instance = new SQLiteConnection(System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "TemplatesData.db"));
return instance;
}
public static void CloseConnection()
{
if (instance != null)
{
instance.Close();
instance.Dispose();
instance = null;
}
}
}
public class TemplateDB
{
public static byte[] ConcatByteList(List<byte[]> list)
{
return list
.SelectMany(a => a)
.ToArray();
}
public static Bitmap GetBitmapFromVectorDrawable(Context context, int drawableId)
{
Drawable drawable = ContextCompat.GetDrawable(context, drawableId);
if (Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.Lollipop)
{
drawable = (DrawableCompat.Wrap(drawable)).Mutate();
}
Bitmap bitmap = Bitmap.CreateBitmap(drawable.IntrinsicWidth,
drawable.IntrinsicWidth, Bitmap.Config.Argb8888);
Canvas canvas = new Canvas(bitmap);
drawable.SetBounds(0, 0, canvas.Width, canvas.Height);
drawable.Draw(canvas);
return bitmap;
}
public byte[] DrawableToByteArray(int resourceId)
{
var context = AppState.ApplicationState;
using (var bitmap = GetBitmapFromVectorDrawable(context, resourceId))
{
int size = bitmap.ByteCount;
byte[] byteArray = new byte[size];
ByteBuffer byteBuffer = ByteBuffer.Allocate(size);
bitmap.CopyPixelsToBuffer(byteBuffer);
byteBuffer.Rewind();
byteBuffer.Get(byteArray);
return byteArray;
}
}
public static void AddTemplate(int category, List<int> images)
{
var templateDB = new TemplateDB();
var imageByteList = new List<byte[]>();
foreach (int image in images)
{
imageByteList.Add(templateDB.DrawableToByteArray(image));
}
var tmpl = new Template()
{
Category = category,
};
var img1 = new TemplateImage()
{
Category = category,
Image = images[0],
ImagesHash = DatabaseHelper.GetMD5Hash(imageByteList[0]),
};
var img2 = new TemplateImage()
{
Category = category,
Image = images[1],
ImagesHash = DatabaseHelper.GetMD5Hash(imageByteList[1]),
};
var img3 = new TemplateImage()
{
Category = category,
Image = images[2],
ImagesHash = DatabaseHelper.GetMD5Hash(imageByteList[2]),
};
var img4 = new TemplateImage()
{
Category = category,
Image = images[3],
ImagesHash = DatabaseHelper.GetMD5Hash(imageByteList[3]),
};
var img5 = new TemplateImage()
{
Category = category,
Image = images[4],
ImagesHash = DatabaseHelper.GetMD5Hash(imageByteList[4]),
};
tmpl.TemplateImages = new List<TemplateImage>() { img1, img2, img3, img4, img5 };
tmpl.ImagesHash = DatabaseHelper.GetMD5Hash(ConcatByteList(imageByteList));
var result = DatabaseHelper.db().Query<TemplateImage>("Select * from Templates where ImagesHash=?", tmpl.ImagesHash);
if (result.Count == 0)
{
DatabaseHelper.db().InsertAll(tmpl.TemplateImages);
DatabaseHelper.db().Insert(tmpl);
DatabaseHelper.db().UpdateWithChildren(tmpl);
}
}
Which all of a sudden, gives out of memory exception.
After thinking for a while that i should stop programming and start belly dancing, i think that since im giving in sqlite AddTemplate function a list of ResourceIds that i have to manually create (can't avoid it), then why not to give them my own string hash code and compare that to find if a record exist?
What is the correct approach?
In the end, i was thinking to use guid as extra id fields and add the parameter unique,in that case i could be sure which templates have been inserted, but i decided to use transactions. With transactions i can know that all my data is created or none and also save the creation into a variable in SharedPreferences and avoid recreating or even checking if the database exist.
For checking if the data is correctly updated and no recreation is needed i used this code:
public bool FirstRun { get; set; } = true;
public int DatabaseCreatedVersionOf { get; set; } = -1;
public override void OnCreate()
{
base.OnCreate();
}
public void UpdateDatabaseCreatedVersion()
{
DatabaseCreatedVersionOf = Preferences.Get(Settings.DatabaseCreatedVersionOfKey,
Settings.DatabaseCreatedVersionOfDefault);
}
public void CreateTemplateDB()
{
UpdateDatabaseCreatedVersion();
if (DatabaseCreatedVersionOf == -1)
TemplateDB.CreateDB();
FirstRun = false;
}
public Template GetTemplateById(int id)
{
if (FirstRun)
{
CreateTemplateDB();
FirstRun = false;
}
return TemplateDB.GetTemplate(id);
}
public List<Template> GetAllTemplates()
{
if (FirstRun)
{
CreateTemplateDB();
FirstRun = false;
}
return TemplateDB.GetAllTemplates();
}
Now all i have to do is call GetTemplateById or GetAllTemplates and if any creation is required, it will happen.

Skip reading the first line of the csv file

I am a beginner in programming,It's really difficult for me to analyze and debug how to skip reading the first line of the csv file. I need some help.
I need my id to fill my combobox in my form that contains all
Id's.In order to not include the header in browsing and
displaying.I need to skip the first line.
public bool ReadEntrie(int id, ref string name, ref string lastname, ref
string phone, ref string mail, ref string website)
{
int count = 0;
CreateConfigFile();
try
{
fs = new FileStream(data_path, FileMode.Open);
sr = new StreamReader(fs);
string temp = "";
bool cond = true;
while (cond == true)
{
if ((temp = sr.ReadLine()) == null)
{
sr.Close();
fs.Close();
cond = false;
if (count == 0)
return false;
}
if (count == id)
{
string[] stringSplit = temp.Split(',');
int _maxIndex = stringSplit.Length;
name = stringSplit[0].Trim('"');
lastname = stringSplit[1].Trim('"');
phone = stringSplit[2].Trim('"');
mail = stringSplit[3].Trim('"');
website = stringSplit[4].Trim('"');
}
count++;
}
sr.Close();
fs.Close();
return true;
}
catch
{
return false;
}
}
#Somadina's answer is correct, but I would suggest a better alternative. You could use a CSV file parser library such as CSV Helpers.
You can get the library from Nuget or Git. Nuget command would be:
Install-Package CsvHelper
Declare the following namespaces:
using CsvHelper;
using CsvHelper.Configuration;
Here's how simple your code looks when you use such a library:
class Program
{
static void Main(string[] args)
{
var csv = new CsvReader(File.OpenText("Path_to_your_csv_file"));
csv.Configuration.IgnoreHeaderWhiteSpace = true;
csv.Configuration.RegisterClassMap<MyCustomObjectMap>();
var myCustomObjects = csv.GetRecords<MyCustomObject>();
foreach (var item in myCustomObjects.ToList())
{
// Apply your application logic here.
Console.WriteLine(item.Name);
}
}
}
public class MyCustomObject
{
// Note: You may want to use a type converter to convert the ID to an integer.
public string ID { get; set; }
public string Name { get; set; }
public string Lastname { get; set; }
public string Phone { get; set; }
public string Mail { get; set; }
public string Website { get; set; }
public override string ToString()
{
return Name.ToString();
}
}
public sealed class MyCustomObjectMap : CsvClassMap<MyCustomObject>
{
public MyCustomObjectMap()
{
// In the name method, you provide the header text - i.e. the header value set in the first line of the CSV file.
Map(m => m.ID).Name("id");
Map(m => m.Name).Name("name");
Map(m => m.Lastname).Name("lastname");
Map(m => m.Phone).Name("phone");
Map(m => m.Mail).Name("mail");
Map(m => m.Website).Name("website");
}
}
Some more details in an answer here.
To skip the first line, just replace the line:
if (count == id)
with
if (count > 0 && count == id)
MORE THOUGHTS ON YOUR APPROACH
Because you used the ref keyword, each line you read will override the previous values you stored in the parameters. A better way to do this is to create a class to hold all the properties of interest. Then, for each line you read, package an instance of the class and add it to a list. You method signature (even the return type) will change eventually.
From your code, the class will look like this:
public class DataModel
{
public string Name { get; set; }
public string LastName { get; set; }
public string Phone{ get; set; }
public string Mail { get; set; }
public string Website{ get; set; }
}
Then your method will be like this:
public IList<DataModel> ReadEntrie(int id, string data_path)
{
int count = 0;
CreateConfigFile();
var fs = new FileStream(data_path, FileMode.Open);
var sr = new StreamReader(fs);
try
{
var list = new List<DataModel>();
string temp = "";
bool cond = true;
while (cond == true)
{
if ((temp = sr.ReadLine()) == null)
{
cond = false;
if (count == 0)
throw new Exception("Failed");
}
if (count > 0 && count == id)
{
string[] stringSplit = temp.Split(',');
var item = new DataModel();
item.Name = stringSplit[0].Trim('"');
item.LastName = stringSplit[1].Trim('"');
item.Phone = stringSplit[2].Trim('"');
item.Mail = stringSplit[3].Trim('"');
item.Website = stringSplit[4].Trim('"');
// add item to list
list.Add(item);
}
count++;
}
return list;
}
catch
{
throw; // or do whatever you wish
}
finally
{
sr.Close();
fs.Close();
}
}

Unable to serialize object into XML file

I'm trying to save a dictionary of Matrix into an Xml file.
My Matrix class attributes are :
public class Matrix
{
public int Lines { get; set; }
public int Columns { get; set; }
public double[,] Elements { get; set; }
public string name { get; set; }
}
After many attempts, I wrote this :
string fileName = dlg.FileName;
Stream writer = new FileStream(fileName,FileMode.Create);
foreach (KeyValuePair<String, Matrix> matrice in CalMat.Calculatrice.listMatrix)
{
XmlSerializer x = new XmlSerializer(matrice.GetType());
x.Serialize(writer, matrice);
}
writer.Close();
If i run this code with one matrix, the file is created, but i only have this sentence written :
<?xml version="1.0"?><KeyValuePairOfStringMatrix xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" /><?xml version="1.0"?>
I think my code is missing something but I don't know what. A write method, I guess.
Thank you for your time!
I don't think the default KeyValuePair is serializable,
try building your own KeyValuePair class:
[Serializable]
[XmlType(TypeName="MyTypeName")]
public struct KeyValuePair<T1, T2>
{
public T1 Key { get; set; }
public T2 Value { get; set; }
}
Using BinaryFormatter this is the code:
[Serializable] // mark with Serializable
public class Matrix
{
public Matrix(string name, int lines, int columns)
{
Name = name;
Lines = lines;
Columns = columns;
Elements = new double[Lines, Columns];
}
public int Lines { get; set; }
public int Columns { get; set; }
public double[,] Elements { get; set; }
public string Name { get; set; }
}
public static void Main()
{
var path = #"D:\serialize.data"; // use the path that you want
// this is an example collection
var listMatrix = new Dictionary<string, Matrix>();
listMatrix.Add("matrix_1", new Matrix("Matrix 1", 1, 2));
listMatrix.Add("matrix_2", new Matrix("Matrix 2", 2, 2));
// Serialization
var stream = new FileStream(path, FileMode.Create);
var binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(stream, listMatrix);
stream.Close();
// Deserialization
stream = new FileStream(path, FileMode.Open);
var result = (Dictionary<string, Matrix>)binaryFormatter.Deserialize(stream);
stream.Close();
}
Using XmlSerializer this is the code:
// I implement my custom KeyValuePair to serialize (because XmlSerializer can not serialize the .net KeyValuePair)
public struct CustomKeyValuePair<T1, T2>
{
public CustomKeyValuePair(T1 key, T2 value): this()
{
Key = key;
Value = value;
}
public T1 Key { get; set; }
public T2 Value { get; set; }
// here I specify how is the cast
public static explicit operator CustomKeyValuePair<T1, T2>(KeyValuePair<T1, T2> keyValuePair)
{
return new CustomKeyValuePair<T1, T2>(keyValuePair.Key, keyValuePair.Value);
}
}
// Matrix class used to Serialize with XmlSerailzer
public class Matrix
{
public Matrix() { } // need a default constructor
public Matrix(string name, int lines, int columns)
{
Name = name;
Lines = lines;
Columns = columns;
Elements = new double[Columns][];
for (int i = 0; i < Elements.Length; i++)
{
Elements[i] = new double[Columns];
}
}
public int Lines { get; set; }
public int Columns { get; set; }
public double[][] Elements { get; set; } // I use double[][] because XmlSerialzer can not serialize a two-dimensional array (double[,])
public string Name { get; set; }
}
public static void Main()
{
var path = #"D:\serialize.data"; // use the path that you want
// this is an example collection
var listMatrix = new Dictionary<string, Matrix>();
listMatrix.Add("matrix_1", new Matrix("Matrix 1", 1, 2));
listMatrix.Add("matrix_2", new Matrix("Matrix 2", 2, 2));
// Serialization
var stream = new FileStream(path, FileMode.Create);
var xmlSerializer = new XmlSerializer(typeof(CustomKeyValuePair<string, Matrix>[]));
var aux = listMatrix.Select(keyValuePair => (CustomKeyValuePair<string, Matrix>) keyValuePair).ToArray();
xmlSerializer.Serialize(stream, aux); // I serialize an array to make easy the deserailizer
stream.Close();
// Deserialization
stream = new FileStream(path, FileMode.Open);
var result = (CustomKeyValuePair<string, Matrix>[])xmlSerializer.Deserialize(stream);
stream.Close();
}

c# serialization ascii confusion

Here's the code.
[Serializable]
public class HostedGame
{
public int ID { get; set; }
public int UID { get; set; }
public String Name { get; set; }
public Boolean Available { get; set; }
public String Description { get; set; }
public List<int> Users { get; set; }
public int Port { get; set; }
public HostedGame(int uid, String name, String description, int port)
{
UID = uid;
Name = name;
Description = description;
Available = true;
Port = port;
Users = new List<int>();
}
public int CompareTo(Object obj)
{
int result = 1;
if(obj != null && obj is HostedGame)
{
HostedGame w = obj as HostedGame;
result = this.ID.CompareTo(w.ID);
}
return result;
}
static public int Compare(HostedGame x, HostedGame y)
{
int result = 1;
if(x != null && y != null)
{
result = x.CompareTo(y);
}
return result;
}
public static HostedGame DeSerialize(byte[] data)
{
MemoryStream ms = new MemoryStream(data);
BinaryFormatter bff = new BinaryFormatter();
return (HostedGame)bff.Deserialize(ms);
}
public static byte[] Serialize(HostedGame obj)
{
BinaryFormatter bff = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bff.Serialize(ms, obj);
return ms.ToArray();
}
}
The code bellow doesn't seem to work right:
HostedGame hs = new HostedGame(12,"Name", "Description", 8088);
String s = Encoding.ASCII.GetString(HostedGame.Serialize(hs));
HostedGame HACK = HostedGame.DeSerialize(Encoding.ASCII.GetBytes(s));
HACK.Port for some reason comes out being 7999?
When I just do this...
HostedGame HACK = HostedGame.DeSerialize(HostedGame.Serialize(hs));
It works fine.
So, what I'm asking is
Why am I getting a wrong value?
Is there a better way to convert the bytes to a string and back again?
You cannot use Encoding.ASCII.GetString to convert any byte array to a string. You are losing some data when you do this. Use Convert.ToBase64String instead. This one will make a string from any byte sequence without losing the data.
HostedGame hs = new HostedGame(12,"Name", "Description", 8088);
String s = Convert.ToBase64String(HostedGame.Serialize(hs));
HostedGame HACK= HostedGame.DeSerialize(Convert.FromBase64String(s));
Here is an example, that shows how using Encoding.ASCII loses the data.
var testBytes = new byte[] { 250, 251, 252 };
var text = Encoding.ASCII.GetString(testBytes);
var bytes = Encoding.ASCII.GetBytes(result); // will be 63, 63, 63
Binary serialization generates a byte array which is not (necessarily) a valid string in any encoding.
When you try to read it as ASCII text, the ASCII decoder will convert any invalid bytes (> 128) into ? characters.
Therefore, when you turn it back into ASCII bytes, you end up with a different set of bytes.
In short, don't treat binary data as ASCII, or as any other text encoding.
If you need to send binary data as plain text, use Base64 to safely convert it to text.

Categories