Skip to content

Commit d89fbcd

Browse files
committed
Added example and binaries and more overloads.
1 parent a9289d8 commit d89fbcd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+25424
-12
lines changed

Example/Example.csproj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="..\Hexa.NET.ZLib\Hexa.NET.ZLib.csproj" />
13+
</ItemGroup>
14+
15+
</Project>

Example/Program.cs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// See https://aka.ms/new-console-template for more information
2+
using Hexa.NET.ZLib;
3+
using System.Text;
4+
5+
unsafe
6+
{
7+
string original = "Hello, this is an example of zlib compression with a C wrapper!";
8+
byte[] inputData = Encoding.UTF8.GetBytes(original);
9+
uint inputLen = (uint)inputData.Length;
10+
11+
byte[] compressedData = new byte[1024];
12+
uint compressedSize;
13+
{
14+
ZStream stream;
15+
int initResult = ZLib.DeflateInit(&stream, ZLib.Z_DEFAULT_COMPRESSION, ZLib.ZLIB_VERSION, sizeof(ZStream));
16+
if (initResult != 0)
17+
{
18+
Console.WriteLine("DeflateInit failed!");
19+
return;
20+
}
21+
22+
// Set up the ZStream for compression
23+
fixed (byte* inputPtr = inputData)
24+
fixed (byte* compressedPtr = compressedData)
25+
{
26+
stream.NextIn = inputPtr;
27+
stream.AvailIn = inputLen;
28+
stream.NextOut = compressedPtr;
29+
stream.AvailOut = (uint)compressedData.Length;
30+
31+
// Perform compression
32+
int deflateResult = ZLib.Deflate(&stream, ZLib.Z_FINISH);
33+
34+
if (deflateResult != ZLib.Z_STREAM_END)
35+
{
36+
Console.WriteLine("Deflate failed!");
37+
return;
38+
}
39+
40+
// Get the compressed length
41+
uint compressedLength = stream.TotalOut;
42+
43+
// Output the compressed data
44+
Console.WriteLine("Compressed data:");
45+
for (int i = 0; i < compressedLength; i++)
46+
{
47+
Console.Write(compressedData[i] + " ");
48+
}
49+
Console.WriteLine();
50+
}
51+
52+
ZLib.DeflateEnd(&stream);
53+
54+
compressedSize = stream.TotalOut;
55+
}
56+
57+
byte[] buffer = new byte[1024];
58+
59+
{
60+
ZStream stream;
61+
int initResult = ZLib.InflateInit(&stream, ZLib.ZLIB_VERSION, sizeof(ZStream));
62+
if (initResult != 0)
63+
{
64+
Console.WriteLine("InflateInit failed!");
65+
return;
66+
}
67+
68+
// Set up the ZStream for decompression
69+
fixed (byte* outputPtr = buffer)
70+
fixed (byte* compressedPtr = compressedData)
71+
{
72+
stream.NextIn = compressedPtr;
73+
stream.AvailIn = compressedSize;
74+
stream.NextOut = outputPtr;
75+
stream.AvailOut = (uint)buffer.Length;
76+
77+
// Perform decompression
78+
int deflateResult = ZLib.Inflate(&stream, ZLib.Z_FINISH);
79+
80+
if (deflateResult != ZLib.Z_STREAM_END)
81+
{
82+
Console.WriteLine("Inflate failed!");
83+
return;
84+
}
85+
86+
// Get the decompression length
87+
uint decompressedLength = stream.TotalOut;
88+
89+
Console.WriteLine("Decompressed data:");
90+
Console.WriteLine(Encoding.UTF8.GetString(outputPtr, (int)decompressedLength));
91+
}
92+
93+
ZLib.InflateEnd(&stream);
94+
}
95+
}
96+

Generator/Generator.csproj

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,42 @@
1515
<None Update="generator.json">
1616
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
1717
</None>
18+
<None Update="include\crc32.h">
19+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
20+
</None>
21+
<None Update="include\deflate.h">
22+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
23+
</None>
24+
<None Update="include\gzguts.h">
25+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
26+
</None>
27+
<None Update="include\inffast.h">
28+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
29+
</None>
30+
<None Update="include\inffixed.h">
31+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
32+
</None>
33+
<None Update="include\inflate.h">
34+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
35+
</None>
36+
<None Update="include\inftrees.h">
37+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
38+
</None>
39+
<None Update="include\trees.h">
40+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
41+
</None>
1842
<None Update="include\zconf.h">
1943
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
2044
</None>
2145
<None Update="include\zlib.h">
2246
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
2347
</None>
48+
<None Update="include\main.h">
49+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
50+
</None>
51+
<None Update="include\zutil.h">
52+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
53+
</None>
2454
</ItemGroup>
2555

2656
</Project>

Generator/Program.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
// See https://aka.ms/new-console-template for more information
22
using HexaGen;
3+
using HexaGen.Core.Mapping;
34
using HexaGen.Patching;
45

5-
List<string> headers = ["include/zconf.h", "include/zlib.h"];
6-
76
BatchGenerator batch = new();
87
batch
98
.Setup<CsCodeGenerator>("generator.json")
9+
.AlterConfig(c =>
10+
{
11+
EnumMapping mapping = new("inflate_mode", null, null);
12+
mapping.ItemMappings.Add(new("COPY_", "Copy_", null, null));
13+
mapping.ItemMappings.Add(new("LEN_", "Len_", null, null));
14+
c.EnumMappings.Add(mapping);
15+
})
1016
.AddPrePatch(new NamingPatch(["Gz", "Zlib"], NamingPatchOptions.None))
11-
.Generate(headers, "../../../../Hexa.NET.ZLib/Generated");
17+
.Generate("include/main.h", "../../../../Hexa.NET.ZLib/Generated", [.. Directory.GetFiles("include")]);

Generator/generator.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
"GenerateDelegates": true,
1111
"GenerateMetadata": false,
1212
"GenerateExtensions": true,
13+
"UseCustomContext": true,
1314
"DelegatesAsVoidPointer": true,
1415
"BoolType": 0,
15-
"IgnoredConstants": [ "PNG_HEADER_VERSION_STRING" ],
1616
"IgnoredFunctions": [ "gzvprintf" ],
1717
"IgnoredTypedefs": [
1818
"gz_headerp",
@@ -44,6 +44,7 @@
4444
"z_size_t": "nint",
4545
"uLongf": "uint",
4646

47-
"Bytef": "byte"
47+
"Bytef": "byte",
48+
"internal_state": "DeflateState"
4849
}
4950
}

0 commit comments

Comments
 (0)