Skip to content

Commit

Permalink
Back to .Net 2.0 (We don't need Linq atm)
Browse files Browse the repository at this point in the history
  • Loading branch information
worstenbrood committed Dec 22, 2015
1 parent 0033ea3 commit 75369c9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
8 changes: 6 additions & 2 deletions HuaweiUpdateLibrary/Core/EntryType.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
namespace HuaweiUpdateLibrary.Core
using System;

namespace HuaweiUpdateLibrary.Core
{
[Flags]
internal enum EntryType
{
Checksum,
Signature
Signature,
Normal
}
}
23 changes: 13 additions & 10 deletions HuaweiUpdateLibrary/Core/UpdateFile.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Collections;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using HuaweiUpdateLibrary.Streams;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.OpenSsl;
Expand Down Expand Up @@ -190,6 +188,8 @@ public void Extract(UpdateEntry entry, Stream output, bool checksum = true)
/// <param name="stream"><see cref="Stream"/> to input data</param>
public void Add(UpdateEntry entry, Stream stream)
{
entry.Type = EntryType.Normal;

// Set size
entry.FileSize = (uint) stream.Length;

Expand Down Expand Up @@ -277,7 +277,9 @@ public void Add(UpdateEntry entry, string fileName)
}
}

private void ProcessData(int blockSize, Action<byte[], int> action)
private delegate void Action<in T, in TU>(T t, TU tu);

private void ProcessData(int blockSize, EntryType entryType, Action<byte[], int> action)
{
// Allocate buffer
var buffer = new byte[blockSize];
Expand All @@ -286,7 +288,7 @@ private void ProcessData(int blockSize, Action<byte[], int> action)
using (var stream = new FileStream(_fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
// Skip checksum and Signature
foreach (var entry in Entries.Where(e => e.Type != EntryType.Checksum && e.Type != EntryType.Signature))
foreach (var entry in Entries.FindAll(e => (entryType & e.Type) == e.Type))
{
// Seek to filedata
stream.Seek(entry.DataOffset, SeekOrigin.Begin);
Expand Down Expand Up @@ -317,13 +319,13 @@ public void AddChecksum(UpdateEntry entry, int blockSize = CrcBlockSize)
entry.Type = EntryType.Checksum;

// Result checksum list
var result = new List<ushort>();
var result = new List<byte>();

// Process data
ProcessData(CrcBlockSize, (bytes, i) => result.Add(Utilities.Crc.ComputeSum(bytes, 0, i)));
ProcessData(blockSize, EntryType.Normal, (bytes, i) => result.AddRange(Utilities.Crc.ComputeHash(bytes, 0, i)));

// Add entry
using (var stream = new MemoryStream(result.SelectMany(BitConverter.GetBytes).ToArray()))
using (var stream = new MemoryStream(result.ToArray()))
{
Add(entry, stream);
}
Expand All @@ -335,7 +337,8 @@ public void AddChecksum(UpdateEntry entry, int blockSize = CrcBlockSize)
/// <param name="entry"><see cref="UpdateEntry"/></param>
/// <param name="algorithm">Algorithm to use</param>
/// <param name="keyfile">Key file</param>
public void AddSignature(UpdateEntry entry, string algorithm, string keyfile)
/// <param name="blockSize">Block size</param>
public void AddSignature(UpdateEntry entry, string algorithm, string keyfile, int blockSize = CrcBlockSize)
{
// TODO: Remove already existing ?

Expand All @@ -354,7 +357,7 @@ public void AddSignature(UpdateEntry entry, string algorithm, string keyfile)
}

// TODO: this is just an assumption, maybe we do need to sign the headers/checksumtables and crc entry
ProcessData(CrcBlockSize, (bytes, i) => signer.BlockUpdate(bytes, 0, i));
ProcessData(blockSize, EntryType.Normal, (bytes, i) => signer.BlockUpdate(bytes, 0, i));

// Add entry
using (var stream = new MemoryStream(signer.GenerateSignature()))
Expand Down
2 changes: 1 addition & 1 deletion HuaweiUpdateLibrary/HuaweiUpdateLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>HuaweiUpdateLibrary</RootNamespace>
<AssemblyName>HuaweiUpdateLibrary</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
Expand Down
12 changes: 7 additions & 5 deletions Test/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;
using HuaweiUpdateLibrary.Algorithms;
using HuaweiUpdateLibrary.Core;

Expand All @@ -14,7 +12,7 @@ public static void UpdateCrc16Test()
var crc = new UpdateCrc16();

var stest = "test";
var sum1 = BitConverter.ToUInt16(crc.ComputeHash(Encoding.ASCII.GetBytes(stest)), 0);
var sum1 = crc.ComputeSum(Encoding.ASCII.GetBytes(stest));

var btest1 = Encoding.ASCII.GetBytes("te");
var btest2 = Encoding.ASCII.GetBytes("st");
Expand All @@ -39,8 +37,12 @@ static void Main(string[] args)
e.HardwareId = "HW8x50";
e.FileType = "IMAGE";
u.Add(e, @"c:\temp\v1.14.zip");
var v = UpdateFile.Open(@"c:\temp\test.app");
v.Extract(v[0], "c:\\temp\\test.zip");
var crc = UpdateEntry.Create();
crc.FileSequence = 0xFFFFFFFF;
crc.FileType = "CRC";
crc.HardwareId = "HUAWEI";
u.AddChecksum(crc);

}
}
}

0 comments on commit 75369c9

Please sign in to comment.