Skip to content

Commit 2929d8e

Browse files
committed
version bumped to 0.8.0 (we're going to final), added more tests, created nuget package
1 parent f89f20a commit 2929d8e

File tree

15 files changed

+236
-11
lines changed

15 files changed

+236
-11
lines changed

Blazer.Native/build.cmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe /t:Rebuild /p:Config
33
xcopy /y Release\Blazer.Native.x86.dll ..\Blazer.Native.Build\
44
xcopy /y Release\Blazer.Native.x64.dll ..\Blazer.Native.Build\
55

6-
del /s /q %temp%\Blazer.Net.0.2.0.3
6+
del /s /q %temp%\Blazer.Net.0.8.0.6

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
<Compile Include="IntegrityTests.cs" />
5151
<Compile Include="OptionsTests.cs" />
5252
<Compile Include="Properties\AssemblyInfo.cs" />
53+
<Compile Include="StreamEncoderTests.cs" />
5354
</ItemGroup>
5455
<ItemGroup>
5556
<None Include="packages.config" />

Blazer.Net.Tests/IntegrityHelper.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.IO;
33

44
using Force.Blazer;
5+
using Force.Blazer.Algorithms;
56

67
using NUnit.Framework;
78

@@ -36,5 +37,13 @@ public static byte[] DecompressData(byte[] inData, Func<Stream, Stream> decoderC
3637
output.Close();
3738
return ms2.ToArray();
3839
}
40+
41+
public static int StreamEncoderCheckCompressDecompress(byte[] inData)
42+
{
43+
var compressed = StreamEncoder.CompressData(inData);
44+
var decompressed = StreamDecoder.DecompressData(compressed);
45+
CollectionAssert.AreEqual(inData, decompressed);
46+
return compressed.Length;
47+
}
3948
}
4049
}

Blazer.Net.Tests/IntegrityTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void Invalid_Header_Should_Throw_Errors()
6161

6262
// invalid version
6363
compressed[3]++;
64-
Assert.That(Assert.Throws<InvalidOperationException>(() => IntegrityHelper.DecompressData(compressed)).Message, Is.EqualTo("Stream was created in new version of Blazer library"));
64+
Assert.That(Assert.Throws<InvalidOperationException>(() => IntegrityHelper.DecompressData(compressed)).Message, Is.EqualTo("Stream was created in newer version of Blazer library"));
6565
compressed[3]--;
6666

6767
// invalid flags
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using System;
2+
using System.Linq;
3+
using System.Text;
4+
5+
using NUnit.Framework;
6+
7+
namespace Blazer.Net.Tests
8+
{
9+
[TestFixture]
10+
public class StreamEncoderTests
11+
{
12+
[Test]
13+
public void Test_AAAAAAAA()
14+
{
15+
var buf = new byte[] { 1, 1, 1, 1, 1, 1, 1, 1 };
16+
var len = IntegrityHelper.StreamEncoderCheckCompressDecompress(buf);
17+
// 1 times 1, then 7 times ref -1
18+
Assert.That(len, Is.EqualTo(3));
19+
}
20+
21+
[Test]
22+
public void Test_AAAAAAAABBBBBBBB()
23+
{
24+
var buf = new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2 };
25+
var len = IntegrityHelper.StreamEncoderCheckCompressDecompress(buf);
26+
Assert.That(len, Is.LessThanOrEqualTo(16));
27+
}
28+
29+
[Test]
30+
public void Test_1000A()
31+
{
32+
// byte 0: short backref, 1 lit, 15 repetitions
33+
// byte 1: backref 0 + 1
34+
// byte 2: 254 repetitions + 2 byte
35+
// byte 3-4: 1d7 repetitions. 471 + 256 + 253 + 15 + 4 = 999
36+
// 1F-00-FE-01-D7-01 (6)
37+
var buf = Enumerable.Range(0, 1000).Select(x => (byte)1).ToArray();
38+
var len = IntegrityHelper.StreamEncoderCheckCompressDecompress(buf);
39+
// 1 times 1, then 7 times ref -1
40+
Assert.That(len, Is.EqualTo(6));
41+
}
42+
43+
[Test]
44+
public void Test_ByteBorder_Repetitions()
45+
{
46+
for (var i = 250; i < 300; i++)
47+
{
48+
var buf = Enumerable.Range(0, i).Select(x => (byte)1).ToArray();
49+
IntegrityHelper.StreamEncoderCheckCompressDecompress(buf);
50+
}
51+
52+
for (var i = 65700; i < 65800; i++)
53+
{
54+
var buf = Enumerable.Range(0, i).Select(x => (byte)1).ToArray();
55+
IntegrityHelper.StreamEncoderCheckCompressDecompress(buf);
56+
}
57+
}
58+
59+
[Test]
60+
public void Test_HabaxHabax()
61+
{
62+
var buf = new byte[] { 2, 1, 3, 1, 4, 2, 1, 3, 1, 4 };
63+
var len = IntegrityHelper.StreamEncoderCheckCompressDecompress(buf);
64+
Assert.That(len, Is.LessThan(8));
65+
}
66+
67+
[Test]
68+
public void Test_Cycle_20000_SemiRandom()
69+
{
70+
var r = new Random(12346);
71+
var buf = Enumerable.Range(0, 20000).Select(x => (byte)r.Next(2)).ToArray();
72+
var len = IntegrityHelper.StreamEncoderCheckCompressDecompress(buf);
73+
// 1 times 1, then 7 times ref -1
74+
Assert.That(len, Is.LessThan(20000));
75+
}
76+
77+
[Test]
78+
public void Test_Cycle_120000_SemiRandom()
79+
{
80+
var r = new Random(12346);
81+
var buf = Enumerable.Range(0, 120000).Select(x => (byte)r.Next(2)).ToArray();
82+
var len = IntegrityHelper.StreamEncoderCheckCompressDecompress(buf);
83+
// 1 times 1, then 7 times ref -1
84+
Assert.That(len, Is.LessThan(120000));
85+
}
86+
87+
[Test]
88+
public void Test_Text()
89+
{
90+
var buf = Encoding.GetEncoding(1251).GetBytes(@"
91+
Когда то давно Московское метро замышлялось как гигантское бомбоубежище, способное спасти десятки тысяч жизней. Мир стоял на пороге гибели, но тогда ее удалось отсрочить. Дорога, по которой идет человечество, вьется, как спираль, и однажды оно снова окажется на краю пропасти. Когда мир будет рушиться, метро окажется последним пристанищем человека перед тем, как он канет в ничто.
92+
— Кто это там? Эй, Артем! Глянь ка!
93+
Артем нехотя поднялся со своего места у костра и, перетягивая со спины на грудь автомат, двинулся во тьму. Стоя на самом краю освещенного пространства, он демонстративно, как можно громче и внушительней, щелкнул затвором и хрипло крикнул: — Стоять! Пароль!");
94+
IntegrityHelper.StreamEncoderCheckCompressDecompress(buf);
95+
}
96+
}
97+
}

Blazer.Net/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
_releases

Blazer.Net/Algorithms/StreamDecoder.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,21 @@ public BlazerAlgorithm GetAlgorithmId()
6767
protected virtual int DecompressBlock(
6868
byte[] bufferIn, int bufferInOffset, int bufferInLength, byte[] bufferOut, int bufferOutOffset, int bufferOutLength)
6969
{
70-
return DecompressBlockExternal(bufferIn, bufferInOffset, bufferInLength, bufferOut, bufferOutOffset, bufferOutLength);
70+
return DecompressBlockExternal(bufferIn, bufferInOffset, bufferInLength, ref bufferOut, bufferOutOffset, bufferOutLength, false);
71+
}
72+
73+
/// <summary>
74+
/// Decompresses independent block of data
75+
/// </summary>
76+
/// <param name="bufferIn">In buffer</param>
77+
/// <returns>Decompressed data</returns>
78+
public static byte[] DecompressData(
79+
byte[] bufferIn)
80+
{
81+
var outBuffer = new byte[bufferIn.Length * 2];
82+
var count = DecompressBlockExternal(bufferIn, 0, bufferIn.Length, ref outBuffer, 0, outBuffer.Length, true);
83+
Array.Resize(ref outBuffer, count);
84+
return outBuffer;
7185
}
7286

7387
/// <summary>
@@ -79,8 +93,9 @@ protected virtual int DecompressBlock(
7993
/// <param name="bufferOut">Out buffer, should be enough size</param>
8094
/// <param name="bufferOutOffset">Out buffer offset</param>
8195
/// <param name="bufferOutLength">Out buffer maximum right offset (offset + count)</param>
96+
/// <param name="resizeOutBufferIfNeeded">Resize out buffer if smaller than required</param>
8297
/// <returns>Bytes count of decompressed data</returns>
83-
public static int DecompressBlockExternal(byte[] bufferIn, int bufferInOffset, int bufferInLength, byte[] bufferOut, int bufferOutOffset, int bufferOutLength)
98+
public static int DecompressBlockExternal(byte[] bufferIn, int bufferInOffset, int bufferInLength, ref byte[] bufferOut, int bufferOutOffset, int bufferOutLength, bool resizeOutBufferIfNeeded)
8499
{
85100
var idxIn = bufferInOffset;
86101
var idxOut = bufferOutOffset;
@@ -140,7 +155,13 @@ public static int DecompressBlockExternal(byte[] bufferIn, int bufferInOffset, i
140155
var maxOutLength = idxOut + litCnt + seqCnt;
141156
if (maxOutLength >= bufferOutLength)
142157
{
143-
throw new IndexOutOfRangeException("Invalid stream structure");
158+
if (resizeOutBufferIfNeeded)
159+
{
160+
bufferOutLength = Math.Max(bufferOut.Length * 2, maxOutLength);
161+
Array.Resize(ref bufferOut, bufferOutLength);
162+
}
163+
else
164+
throw new IndexOutOfRangeException("Invalid stream structure");
144165
}
145166

146167
if (litCnt >= 8)

Blazer.Net/Algorithms/StreamEncoder.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,20 @@ protected virtual int CompressBlock(
128128
return CompressBlockExternal(bufferIn, bufferInOffset, bufferInLength, bufferInShift, bufferOut, bufferOutOffset, _hashArr);
129129
}
130130

131+
/// <summary>
132+
/// Compresses independent block of data
133+
/// </summary>
134+
/// <param name="bufferIn">In buffer</param>
135+
/// <returns>Compressed array</returns>
136+
public static byte[] CompressData(byte[] bufferIn)
137+
{
138+
var hashArr = new int[HASH_TABLE_LEN + 1];
139+
var outBuffer = new byte[bufferIn.Length + (bufferIn.Length >> 8) + 3];
140+
var cnt = CompressBlockExternal(bufferIn, 0, bufferIn.Length, 0, outBuffer, 0, hashArr);
141+
Array.Resize(ref outBuffer, cnt);
142+
return outBuffer;
143+
}
144+
131145
/// <summary>
132146
/// Compresses block of data, can be used independently for byte arrays
133147
/// </summary>

Blazer.Net/Blazer.Net.nuspec

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0"?>
2+
<package >
3+
<metadata>
4+
<id>Blazer.Net</id>
5+
<title>Blazer.Net</title>
6+
<version>0.8.0</version>
7+
<authors>force</authors>
8+
<owners>force</owners>
9+
<licenseUrl>https://github.com/force-net/blazer/blob/develop/LICENSE</licenseUrl>
10+
<projectUrl>https://github.com/force-net/blazer</projectUrl>
11+
<!--iconUrl>http://ICON_URL_HERE_OR_DELETE_THIS_LINE</iconUrl-->
12+
<requireLicenseAcceptance>false</requireLicenseAcceptance>
13+
<description>
14+
Blazer is high-performance, low compression rate archiver. Main usage for working with streams but can be used as general archiver.
15+
Compression rate is comparable (slightly better) than LZ4 and Snappy and compression speed is faster than GZip.
16+
</description>
17+
<releaseNotes>
18+
First public release
19+
</releaseNotes>
20+
<copyright>Copyright by Force 2016</copyright>
21+
<tags>.NET fast compression archive</tags>
22+
</metadata>
23+
<files><file src="bin\Release\Blazer.Net.*" target="lib\net40" /></files>
24+
</package>

Blazer.Net/BlazerInputStream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public BlazerInputStream(Stream innerStream, BlazerCompressionOptions options)
183183
_header = new byte[]
184184
{
185185
(byte)'b', (byte)'L', (byte)'z',
186-
0x00, // version of file structure
186+
0x01, // version of file structure
187187
(byte)((((uint)flags) & 0xff) | ((uint)_encoderAlgorithmId << 4)),
188188
(byte)(((uint)flags >> 8) & 0xff),
189189
(byte)(((uint)flags >> 16) & 0xff),

0 commit comments

Comments
 (0)