William Rosenbloom
William Rosenbloom

Reputation: 2626

What is the POSIX System Library for the LZW Algorithm Used by the Compress Utility

This has been an incredibly difficult question to Google. I am not looking for gzip or Zip or deflate. The algorithm I want to use is called "compress" but that does not mean I am trying to implement compression in general. I am looking for a specific algorithm.

I am looking for the adaptive Lempel-Ziv algorithm used by the compress command line tool in Unix-like systems. I am looking for the algorithm HTTP says you should use when you receive a Content-Encoding: compress header. It's the algorithm you see described when you type man compress in a POSIX shell, and in this Wikipedia article.

I understand this compression algorithm is very old and has been replaced by gzip, Zip, deflate, etc. for almost all practical purposes. But I am writing a server in C++ as a pet project and IANA specifies this Unix "compress" algorithm as one of the encodings every server should support.

The compress utility has been part of the Unix shell for a long time - since before POSIX - and I have trouble believing there's no standard C language implementation. I could use calls to system or exec to do the compression in the shell (creating another process...ugh) but that would be so much less efficient than just compiling the algorithm into my executable.

Is there a standard C implementation/library for this algorithm?

Upvotes: 3

Views: 1108

Answers (4)

puchu
puchu

Reputation: 3652

You can use lzws library. It has no legacy code and compatible with UNIX compress. Tested on GNU/Linux, OSX, Free BSD and Windows (MinGW). Has ruby bindings.

Upvotes: 0

Mark Adler
Mark Adler

Reputation: 112404

You can find an unlzw() function that I wrote here to decompress a Content-Encoding: compress transfer.

However, you do not have to support compress to be http-compliant. The compression method is a negotiation, and you can either not say that you accept compress (if you are the client) or not deliver compress when the client accepts it (if you are the server). It is not true that "every server should support" the compress encoding.

Upvotes: 1

Clifford
Clifford

Reputation: 93476

Certainly in Linux the system library libarchive supports LZW as used by compress.

The library has its own project page, and in that sense therefore is portable, although it is no doubt used on other, if not all POSIX systems. Try man libarchive perhaps?

Upvotes: 1

terence hill
terence hill

Reputation: 3454

I think I've found some good references:

First here is the Free BSD implementation of compress based on Lempel-Ziv: https://www.freebsd.org/security/advisories/FreeBSD-SA-11:04.compress.asc

Here and here a modified version of the Lempel-Ziv algorithm (among the authors you will find Spencer W. Thomas).

Newer implementation

DOS porting

Apple version based on FREE BSD.

hint: search "compress.c" quoted.

Upvotes: 1

Related Questions