Click here to Skip to main content
Click here to Skip to main content

Pure C# MiniLZO port

, 22 Dec 2006
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

Astaelan
Web Developer
Canada Canada
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#.

Comments and Discussions

 
QuestionAccessViolation on 64-bit Pinmemberzzattack26-Aug-13 16:25 
QuestionCan u port lzo1z to .net Pinmemberyuvraj_oak1-Jul-11 23:01 
GeneralDynamic sizes PinmemberHenryChilvers20-Sep-10 13:26 
GeneralBuffer overflow help :( Pinmemberzeeh315-Jul-10 9:49 
GeneralRe: Buffer overflow help :( Pinmemberzeeh316-Jul-10 7:07 
GeneralRe: Buffer overflow help :( PinmemberMember 1020339811-Aug-13 20:15 
QuestionUsage in closed source customer project PinmemberKay Herzam12-Jul-07 22:01 
AnswerRe: Usage in closed source customer project PinmemberAstaelan13-Jul-07 6:11 
Generalexample with memory stream, chunk-by-chunk Pinmemberdncpax211-Jul-07 1:19 
GeneralRe: example with memory stream, chunk-by-chunk PinmemberAstaelan13-Jul-07 6:07 
GeneralSource Code PinmemberTit Copernicus5-May-07 3:10 
GeneralRe: Source Code PinmemberAstaelan5-May-07 8:24 
GeneralHere is how to make it work on Pocket PC Windows Mobile Pinmemberrkm10-Jan-07 1:56 
GeneralDoesn't work on 64 bit Pinmemberbimal20064-Jan-07 17:38 
GeneralRe: Doesn't work on 64 bit PinmemberAstaelan4-Jan-07 18:15 
GeneralRe: Doesn't work on 64 bit Pinmembertedsmorris24-Oct-07 8:39 
GeneralRe: Doesn't work on 64 bit PinmemberAVEbrahimi17-Jun-09 21:21 
GeneralThanks PinmemberBehind The Scene22-Dec-06 12:46 
GeneralRe: Thanks PinmemberAstaelan23-Dec-06 3:47 
GeneralTiny bug PinmemberSimmoTech22-Nov-06 19:14 
GeneralRe: Tiny bug PinmemberAstaelan22-Dec-06 9:19 
GeneralSuggestion PinmemberOskar Austegard9-Nov-06 3:04 
GeneralRe: Suggestion PinmemberAstaelan13-Nov-06 22:54 
GeneralAnother such library... Pinmemberomaurice6-Nov-06 15:06 
GeneralRe: Another such library... PinmemberAstaelan6-Nov-06 17:04 
GeneralRe: Another such library... PinmemberBehind The Scene22-Dec-06 12:40 
GeneralRe: Another such library... Pinmemberomaurice22-Dec-06 12:43 
GeneralRe: Another such library... PinmemberBehind The Scene22-Dec-06 12:51 
GeneralRe: Another such library... Pinmemberomaurice22-Dec-06 13:00 
GeneralRe: Another such library... PinmemberAstaelan23-Dec-06 4:10 
GeneralRe: Another such library... Pinmemberomaurice23-Dec-06 4:32 
GeneralRe: Another such library... PinmemberAstaelan23-Dec-06 4:46 
"In some of the compression cases LZF is faster than LZO"
 
In some cases, perhaps. All EXE's? I doubt it. I think when you're specifically testing against 1 file, any algorithm can become biased. I could write an algorithm that compresses winword even faster and better, but it would only compress winword well and maybe a few select other exe's.
 
The point I'm making is you can't assess your "better algorithm" based on 1 test, unless you intend your LibLZF to be used only for 1 specific use.
 
The design of minilzo, more importantly my port of it, was to provide fast streaming compression of basically ALL data and still get a good 40% reduction on average. Do I care that I can't compress an already compressed file? Not really, because if I was using minilzo or LZF, I wouldn't be compressing the data in either case, it's a waste of a lot of CPU cycles on data that, as shown by LZF, gains less than a 1% saving for probably a significant amount of time compressing.
 
Anyway, beyond the point that LZO makes a better overall compression solution for streaming, the true reason I felt the need to reply again was your final comment about QuickLZ not being "worth th effort". I don't know about you, but in every case it's almost twice as fast as the second fastest alternative for compression, and comes out leaps and bounds ahead of LZO or LZF in ALL cases. The compression ratio's tend to be a little worse, but I'd say that given a month or two to refine the original code, this could replace everything we use now for streaming compression.
 
Anyway, I didn't mean to be rude, LZF obviously has it's uses which I would place somewhere between LZO and ZLIB in that it's a good large binary file compressor, but I couldn't stand by and have you say LZF is better, because for general purpose uses (streaming packet data that isn't compressed), LZO will come out ahead more often than not.
 
Anyway, I think I'm going to look more into QuickLZ, it has my curiousity, and it may put both our LZO and LZF products to shame Smile | :)
GeneralRe: Another such library... PinmemberWorldNomad23-Dec-06 3:32 
GeneralRe: Another such library... Pinmemberomaurice23-Dec-06 4:00 
Generalwhy not adding the original size to the header of the buffer PinmemberUnruled Boy5-Nov-06 15:19 
GeneralRe: why not adding the original size to the header of the buffer PinmemberAstaelan6-Nov-06 17:01 
GeneralRe: why not adding the original size to the header of the buffer PinmemberSimmoTech6-Nov-06 20:44 
GeneralRe: why not adding the original size to the header of the buffer PinmemberAstaelan6-Nov-06 21:12 
GeneralImpressive, but... Pinmemberwoudwijk4-Nov-06 1:11 
GeneralRe: Impressive, but... PinmemberAstaelan4-Nov-06 10:29 
GeneralAgreement PinmemberBehind The Scene22-Dec-06 12:44 

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.

| Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 22 Dec 2006
Article Copyright 2006 by Astaelan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid