Skip to content

Commit b33b630

Browse files
committed
small fixes in stream/stream high processing. data array helper was moved to main library
1 parent bb89dfb commit b33b630

File tree

15 files changed

+210
-35
lines changed

15 files changed

+210
-35
lines changed

Blazer.Exe/Program.cs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
using Force.Blazer.Algorithms;
99
using Force.Blazer.Exe.CommandLine;
10+
using Force.Blazer.Helpers;
1011

1112
namespace Force.Blazer.Exe
1213
{
@@ -176,11 +177,7 @@ private static int ProcessCompress(CommandLineParser<BlazerCommandLineOptions> o
176177
sourceData = tmpOutStream.ToArray();
177178
}
178179

179-
var encoder = compressionOptions.Encoder;
180-
encoder.Init(sourceData.Length);
181-
var res = encoder.Encode(sourceData, 0, sourceData.Length);
182-
outStream.Write(new[] { (byte)sourceData.Length, (byte)(sourceData.Length >> 8), (byte)(sourceData.Length >> 16), (byte)(sourceData.Length >> 24) }, 0, 4);
183-
outStream.Write(res.Buffer, res.Offset, res.Count);
180+
DataArrayCompressorHelper.CompressDataToArrayAndWriteToStream(sourceData, compressionOptions.Encoder, outStream);
184181
outStream.Close();
185182
}
186183
else
@@ -326,19 +323,17 @@ private static int ProcessDecompress(CommandLineParser<BlazerCommandLineOptions>
326323
var ms = new MemoryStream();
327324
inStreamSource.CopyTo(ms);
328325
var comprArray = ms.ToArray();
329-
var uncomprLength = comprArray[0] | (comprArray[1] << 8) | (comprArray[2] << 16) | (comprArray[3] << 24);
330-
var decoder = decOptions.Decoder;
331-
decoder.Init(uncomprLength);
332-
var decoded = decoder.Decode(comprArray, 4, comprArray.Length, true);
333-
outStream = new MemoryStream(decoded.Buffer, decoded.Offset, decoded.Count);
326+
outStream = DataArrayCompressorHelper.DecompressDataArrayToReadableStream(comprArray, decOptions.Decoder);
334327
}
335328
else
336329
{
337330
outBlazerStream = new BlazerOutputStream(inStreamSource, decOptions);
338331
outStream = outBlazerStream;
339332
}
340333

341-
if (opt.NoFileName && outBlazerStream.HaveMultipleFiles)
334+
hasMultipleFiles = outBlazerStream != null && outBlazerStream.HaveMultipleFiles;
335+
336+
if (opt.NoFileName && hasMultipleFiles)
342337
{
343338
Console.Error.WriteLine("Cannot decompress without filename when archive contains multiple files");
344339
return 1;
@@ -354,11 +349,9 @@ private static int ProcessDecompress(CommandLineParser<BlazerCommandLineOptions>
354349
}
355350

356351
// nothing to do
357-
if (!outBlazerStream.HaveMultipleFiles && !customOutFileNames.Any(y => y.IsMatch(fileName)))
352+
if (!hasMultipleFiles && !customOutFileNames.Any(y => y.IsMatch(fileName)))
358353
return 0;
359354

360-
hasMultipleFiles = outBlazerStream.HaveMultipleFiles;
361-
362355
if (opt.Stdout) outFile[0] = Console.OpenStandardOutput();
363356
if (!hasMultipleFiles)
364357
decOptions.FileInfoCallback(new BlazerFileInfo { FileName = fileName, CreationTimeUtc = DateTime.UtcNow, LastWriteTimeUtc = DateTime.UtcNow });

Blazer.Exe/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
// You can specify all the values or you can default the Build and Revision Numbers
3232
// by using the '*' as shown below:
3333
// [assembly: AssemblyVersion("1.0.*")]
34-
[assembly: AssemblyVersion("0.9.0.0")]
35-
[assembly: AssemblyFileVersion("0.9.3.11")]
34+
[assembly: AssemblyVersion("0.10.0.0")]
35+
[assembly: AssemblyFileVersion("0.10.0.13")]
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

Blazer.Native/BlazerStream.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ extern "C" __declspec(dllexport) __int32 blazer_stream_decompress_block(unsigned
338338
}
339339

340340
unsigned char* maxOutLength = bufferOut + litCnt + seqCnt;
341-
if (maxOutLength >= bufferOutEnd)
341+
if (maxOutLength > bufferOutEnd)
342342
return -1;
343343

344344
if (bufferIn + litCnt > bufferInEnd)

Blazer.Net.Tests/Blazer.Net.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
<ItemGroup>
5555
<Compile Include="BigDataTests.cs" />
5656
<Compile Include="Crc32Tests.cs" />
57+
<Compile Include="DataArrayTests.cs" />
5758
<Compile Include="EncryptionTests.cs" />
5859
<Compile Include="IntegrityHelper.cs" />
5960
<Compile Include="IntegrityTests.cs" />

Blazer.Net.Tests/DataArrayTests.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using System;
2+
3+
using Force.Blazer;
4+
using Force.Blazer.Algorithms;
5+
using Force.Blazer.Helpers;
6+
using Force.Blazer.Native;
7+
8+
using NUnit.Framework;
9+
10+
namespace Blazer.Net.Tests
11+
{
12+
[TestFixture]
13+
public class DataArrayTests
14+
{
15+
[Test]
16+
public void Stream_Encode_Decode_Should_Not_Resize_Array()
17+
{
18+
var bufferIn = new byte[] { 1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4 };
19+
var compr = StreamEncoder.CompressData(bufferIn);
20+
var bufferOut = new byte[bufferIn.Length];
21+
var cnt = StreamDecoder.DecompressBlockExternal(compr, 0, compr.Length, ref bufferOut, 0, bufferOut.Length, false);
22+
Assert.That(cnt, Is.EqualTo(bufferIn.Length));
23+
CollectionAssert.AreEqual(bufferIn, bufferOut);
24+
}
25+
26+
[Test]
27+
[TestCase(typeof(BlockEncoder), typeof(BlockDecoder))]
28+
[TestCase(typeof(BlockEncoderNative), typeof(BlockDecoderNative))]
29+
public void Block_Encode_Decode_Should_Not_Resize_Array(Type encoderType, Type decoderType)
30+
{
31+
// ensuring native is inited
32+
NativeHelper.SetNativeImplementation(true);
33+
var bufferIn = new byte[] { 1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4 };
34+
var encoder = (BlockEncoder)Activator.CreateInstance(encoderType);
35+
encoder.Init(bufferIn.Length);
36+
var compr = new byte[bufferIn.Length];
37+
var comprCnt = encoder.CompressBlock(bufferIn, 0, bufferIn.Length, compr, 0, true);
38+
Console.WriteLine(comprCnt);
39+
40+
var bufferOut = new byte[bufferIn.Length];
41+
var decoder = (BlockDecoder)Activator.CreateInstance(decoderType);
42+
decoder.Init(bufferIn.Length);
43+
var cnt = decoder.DecompressBlock(compr, 0, comprCnt, bufferOut, 0, bufferOut.Length, true);
44+
Assert.That(cnt, Is.EqualTo(bufferIn.Length));
45+
CollectionAssert.AreEqual(bufferIn, bufferOut);
46+
}
47+
48+
[Test]
49+
[TestCase(typeof(StreamEncoder), typeof(StreamDecoder))]
50+
[TestCase(typeof(StreamEncoderNative), typeof(StreamDecoderNative))]
51+
[TestCase(typeof(StreamEncoderHigh), typeof(StreamDecoder))]
52+
public void Stream_Encode_Decode_Should_Not_Resize_Array(Type encoderType, Type decoderType)
53+
{
54+
// ensuring native is inited
55+
NativeHelper.SetNativeImplementation(true);
56+
var bufferIn = new byte[] { 1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4 };
57+
var encoder = (StreamEncoder)Activator.CreateInstance(encoderType);
58+
encoder.Init(bufferIn.Length);
59+
var compr = new byte[bufferIn.Length];
60+
var comprCnt = encoder.CompressBlock(bufferIn, 0, bufferIn.Length, 0, compr, 0);
61+
Console.WriteLine(comprCnt);
62+
63+
var bufferOut = new byte[bufferIn.Length];
64+
var decoder = (StreamDecoder)Activator.CreateInstance(decoderType);
65+
decoder.Init(bufferIn.Length);
66+
var cnt = decoder.DecompressBlock(compr, 0, comprCnt, bufferOut, 0, bufferOut.Length);
67+
Assert.That(cnt, Is.EqualTo(bufferIn.Length));
68+
CollectionAssert.AreEqual(bufferIn, bufferOut);
69+
}
70+
71+
[Test]
72+
[TestCase(BlazerAlgorithm.NoCompress)]
73+
[TestCase(BlazerAlgorithm.Block)]
74+
[TestCase(BlazerAlgorithm.Stream)]
75+
public void DataArrayCompressionHelper_Should_Encode_Decode(BlazerAlgorithm algorithm)
76+
{
77+
var bufferIn = new byte[] { 1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4 };
78+
var arr = DataArrayCompressorHelper.CompressDataToArray(bufferIn, EncoderDecoderFactory.GetEncoder(algorithm));
79+
var bufferOut = DataArrayCompressorHelper.DecompressDataArray(arr, EncoderDecoderFactory.GetDecoder(algorithm));
80+
CollectionAssert.AreEqual(bufferIn, bufferOut);
81+
}
82+
}
83+
}

Blazer.Net.Tests/EncryptionTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ public void Compare_Random_And_CryptoRandom_Peformance()
176176
Console.WriteLine(total / sw.Elapsed.TotalSeconds);
177177
}
178178

179-
[Test]
179+
// for future
180+
/*[Test]
180181
public void Check_Encrypt_Peformance()
181182
{
182183
var b = new byte[65530];
@@ -191,7 +192,7 @@ public void Check_Encrypt_Peformance()
191192
192193
Console.WriteLine(total / 1048576.0 / sw.Elapsed.TotalSeconds);
193194
sw.Restart();
194-
}
195+
}*/
195196

196197
[Test]
197198
public void Duplicate_Data_Blocks_Should_Be_Catched_on_Decryption()

Blazer.Net/Algorithms/BufferInfo.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,24 @@ public BufferInfo(byte[] buffer, int offset, int length)
4141
Length = length;
4242
}
4343

44+
/// <summary>
45+
/// Extracts body to separate byte array
46+
/// </summary>
47+
/// <returns>new array</returns>
4448
public byte[] ExtractToSeparateArray()
4549
{
46-
var res = new byte[Length];
47-
System.Buffer.BlockCopy(Buffer, Offset, res, 0, Count);
50+
return ExtractToSeparateArray(0);
51+
}
52+
53+
/// <summary>
54+
/// Extracts body to separate byte array
55+
/// </summary>
56+
/// <param name="offset">additional offset for new array</param>
57+
/// <returns>new array</returns>
58+
public byte[] ExtractToSeparateArray(int offset)
59+
{
60+
var res = new byte[Count + offset];
61+
System.Buffer.BlockCopy(Buffer, Offset, res, offset, Count);
4862
return res;
4963
}
5064
}

Blazer.Net/Algorithms/StreamDecoder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public static int DecompressBlockExternal(byte[] bufferIn, int bufferInOffset, i
153153
}
154154

155155
var maxOutLength = idxOut + litCnt + seqCnt;
156-
if (maxOutLength >= bufferOutLength)
156+
if (maxOutLength > bufferOutLength)
157157
{
158158
if (resizeOutBufferIfNeeded)
159159
{

0 commit comments

Comments
 (0)