Click here to Skip to main content
Licence 
First Posted 3 Nov 2006
Views 73,121
Bookmarked 51 times

Pure C# MiniLZO port

By | 22 Dec 2006 | Article
Fast stream compression using a ported minilzo for .NET.

Introduction

I was recently in a position where I needed to find a fast streamable compression solution.  The great source by Markus Oberhumer provided the answer. After using the C code for a while, I realized C#/.NET lacked a similar library, short of one I found which was commercial.

As a result, I decided to attempt a minimalistic port of MiniLZO compression and decompression into pure C#. At this point, the code uses unsafe/fixed blocks to best mimic the original minilzo implementation. A pure managed solution may be soon to follow (at the cost of speed).

The code has not been unit tested 100%, but all initial tests have been successful. If anyone finds any bugs, please report them so I may fix it. I also have not done any profiling of the speed yet, if anyone feels upto the task of comparing it against the C implementation.

Use of the code is relatively simple, here is a quick example.

Compression

byte[] original = File.ReadAllBytes("...");
byte[] destination;
MiniLZO.Compress(original, out destination);

Decompression

byte[] destination = ...; /* Resulting buffer from above */
byte[] original = new byte[x];
MiniLZO.Decompress(destination, original);

Note that with compression, the destination buffer will be assigned and trimmed to the correct size. With decompression, the "original" buffer must be preallocated with the correct original size. Also note that my implementation removed all use of goto from the original source, and replaces certain uses with OverflowExceptions.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Astaelan

Web Developer

Canada Canada

Member

Short and simple, I'm a self contracted programmer, my strongest programming skills are in C/C++ and C#/.NET. I have a nack for porting C algorithms to C#.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionCan u port lzo1z to .net Pinmemberyuvraj_oak23:01 1 Jul '11  
GeneralDynamic sizes PinmemberHenryChilvers13:26 20 Sep '10  
GeneralBuffer overflow help :( Pinmemberzeeh39:49 15 Jul '10  
GeneralRe: Buffer overflow help :( Pinmemberzeeh37:07 16 Jul '10  
QuestionUsage in closed source customer project PinmemberKay Herzam22:01 12 Jul '07  
AnswerRe: Usage in closed source customer project PinmemberAstaelan6:11 13 Jul '07  
Generalexample with memory stream, chunk-by-chunk Pinmemberdncpax21:19 11 Jul '07  
GeneralRe: example with memory stream, chunk-by-chunk PinmemberAstaelan6:07 13 Jul '07  
GeneralSource Code PinmemberTit Copernicus3:10 5 May '07  
GeneralRe: Source Code PinmemberAstaelan8:24 5 May '07  
GeneralHere is how to make it work on Pocket PC Windows Mobile Pinmemberrkm1:56 10 Jan '07  
GeneralDoesn't work on 64 bit Pinmemberbimal200617:38 4 Jan '07  
first of all Thanks for converting lzo in to C#. You library is working perfectly on 32 bit but the same code compiled on 64 bit gives the following error
 
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
 
And this happens inside the compress method at the following line(pos = ip - (ip - dict[index]))
I have extracted the code part including the line gives the error.
 
for (; ; )
{
offset = 0;
index = D_INDEX1(ip);
pos = ip - (ip - dict[index]);
if (pos < input || (offset = (uint)(ip - pos)) <= 0 || offset > M4_MAX_OFFSET)
literal = true;
else if (offset <= M2_MAX_OFFSET || pos[3] == ip[3])
{
}
else
{
index = D_INDEX2(index);
pos = ip - (ip - dict[index]);
if (pos < input || (offset = (uint)(ip - pos)) <= 0 || offset > M4_MAX_OFFSET)
literal = true;
 

 
It would be really great if you can make it 64 bit compatible as well.
Thanks.
 
Bimal
GeneralRe: Doesn't work on 64 bit PinmemberAstaelan18:15 4 Jan '07  
GeneralRe: Doesn't work on 64 bit Pinmembertedsmorris8:39 24 Oct '07  
GeneralRe: Doesn't work on 64 bit PinmemberAVEbrahimi21:21 17 Jun '09  
GeneralThanks PinmemberBehind The Scene12:46 22 Dec '06  
GeneralRe: Thanks PinmemberAstaelan3:47 23 Dec '06  
GeneralTiny bug PinmemberSimmoTech19:14 22 Nov '06  
GeneralRe: Tiny bug PinmemberAstaelan9:19 22 Dec '06  
GeneralSuggestion PinmemberOskar Austegard3:04 9 Nov '06  
GeneralRe: Suggestion PinmemberAstaelan22:54 13 Nov '06  
GeneralAnother such library... Pinmemberomaurice15:06 6 Nov '06  
GeneralRe: Another such library... PinmemberAstaelan17:04 6 Nov '06  
GeneralRe: Another such library... PinmemberBehind The Scene12:40 22 Dec '06  
GeneralRe: Another such library... Pinmemberomaurice12:43 22 Dec '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 22 Dec 2006
Article Copyright 2006 by Astaelan
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid