Levi Dahlstrom
Levi Dahlstrom

Reputation: 3

ASC-GZIP decoding in Objective C

I've been scouring the internet for a library or piece of code that will help me decompress an XFDL file with this type of encoding:application/x-xfdl;content-encoding="asc-gzip". All of the previous XFDL files I've dealt with have had this type of encoding: application/vnd.xfdl;content-encoding="base64-gzip", and I've had no problem using Base64 and GZIP (both libraries on github written by nicklockwood). I've found code in Python that would decompress these types of files, located here:http://www.ourada.org/blog/archives/375, but I'm at a loss as to how I would implement this code into an Objective-C project.

Any help at all or nudges in the right direction would be greatly appreciated.

Thank you!

Upvotes: 0

Views: 372

Answers (1)

Mark Adler
Mark Adler

Reputation: 112284

The asc-gzip examples I have found are actually zlib, then base-64 encoded. It is broken into blocks, with each block preceded by the compressed and uncompressed length in big-endian order, two bytes for each.

So to decode:

  1. Base-64 decode the whole thing. Then on the decoded bytes:
  2. Compute the next compressed length from the first two bytes in big-endian order. Call it c.
  3. Compute the next uncompressed length from the next two bytes in big-endian order. Call it u.
  4. Use zlib to decode the zlib stream consisting of the next c bytes. Verify that zlib decoded exactly c bytes, and that the resulting uncompressed data is exactly u bytes long.
  5. Repeat starting at step 2 until the data is consumed. (It should all be exactly consumed.)

Interestingly, both "asc" and "gzip" are misnomers in the encoding name. asc really means base 64, and gzip really means zlib. Go figure.

Upvotes: 2

Related Questions