Skip to content

Commit

Permalink
initial support for zlib encoded blobs
Browse files Browse the repository at this point in the history
  • Loading branch information
intelliyole committed May 9, 2007
1 parent e359086 commit 390cd9a
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 1 deletion.
Binary file added References/ICSharpCode.SharpZipLib.dll
Binary file not shown.
18 changes: 18 additions & 0 deletions Structorian.Engine.Tests/LoadDataTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Diagnostics;
using System.IO;
using System.Text;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using NUnit.Framework;
using Structorian.Engine.Fields;

Expand Down Expand Up @@ -896,5 +898,21 @@ [Test] public void NegativeStrLen()
Assert.AreEqual(1, instance.Cells.Count);
Assert.IsTrue(instance.Cells [0].IsError());
}

[Test] public void ZipBlob()
{
MemoryStream sourceStream = new MemoryStream(new byte[] { 12, 34, 56, 78 });
MemoryStream compressedStream = new MemoryStream();
DeflaterOutputStream deflaterStream = new DeflaterOutputStream(compressedStream);
StreamUtils.Copy(sourceStream, deflaterStream, new byte[4096]);
deflaterStream.Finish();
Assert.AreEqual(12, compressedStream.Length);
compressedStream.Capacity = (int) compressedStream.Length;
StructInstance instance = PrepareInstance(
"struct A { blob q [len=FileSize, encoding=zlib]; }",
compressedStream.GetBuffer());
BlobCell cell = (BlobCell) instance.Cells[0];
Assert.AreEqual(4, cell.DataStream.Length);
}
}
}
4 changes: 4 additions & 0 deletions Structorian.Engine.Tests/Structorian.Engine.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="ICSharpCode.SharpZipLib, Version=0.85.1.271, Culture=neutral, PublicKeyToken=1b03e6acf1164f73, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\References\ICSharpCode.SharpZipLib.dll</HintPath>
</Reference>
<Reference Include="nunit.framework, Version=2.2.8.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\References\nunit.framework.dll</HintPath>
Expand Down
1 change: 1 addition & 0 deletions Structorian.Engine/AttributeRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ internal AttributeRegistry()
RegisterAttribute(typeof(AssertField), "expr", AttributeType.Expression);
RegisterAttribute(typeof(BitfieldField), "size", AttributeType.Expression);
RegisterAttribute(typeof(BlobField), "len", AttributeType.Expression);
RegisterAttribute(typeof(BlobField), "encoding", AttributeType.String);
RegisterAttribute(typeof(CalcField), "value", AttributeType.Expression);
RegisterAttribute(typeof(CaseField), "expr", AttributeType.Expression);
RegisterAttribute(typeof(CaseField), "default", AttributeType.Bool);
Expand Down
2 changes: 1 addition & 1 deletion Structorian.Engine/Fields/BlobCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public BlobCell(StructField def, byte[] data, int offset) : base(def, null, offs

public Stream DataStream
{
get { return _dataStream; }
get { return _dataStream; }
}
}
}
16 changes: 16 additions & 0 deletions Structorian.Engine/Fields/BlobField.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.IO;
using System.Text;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;

namespace Structorian.Engine.Fields
{
Expand All @@ -17,6 +19,20 @@ public override void LoadData(BinaryReader reader, StructInstance instance)
if (offset + len > reader.BaseStream.Length)
throw new LoadDataException("Blob size " + len + " exceeds stream length");
byte[] blobBytes = reader.ReadBytes(len);
string encoding = GetStringAttribute("encoding");
if (encoding == "zlib")
{
InflaterInputStream stream = new InflaterInputStream(new MemoryStream(blobBytes));
byte[] data = new byte[4096];
MemoryStream outStream = new MemoryStream();
int size;
while((size = stream.Read(data, 0, data.Length)) > 0)
{
outStream.Write(data, 0, size);
}
outStream.Capacity = (int) outStream.Length;
blobBytes = outStream.GetBuffer();
}
BlobCell cell = new BlobCell(this, blobBytes, offset);
instance.AddCell(cell, _hidden);
instance.RegisterCellSize(cell, len);
Expand Down
4 changes: 4 additions & 0 deletions Structorian.Engine/Structorian.Engine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="ICSharpCode.SharpZipLib, Version=0.85.1.271, Culture=neutral, PublicKeyToken=1b03e6acf1164f73, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\References\ICSharpCode.SharpZipLib.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
Expand Down

0 comments on commit 390cd9a

Please sign in to comment.