README revision 1.1.1.2
11.1SchristosPuff -- A Simple Inflate 21.1Schristos3 Mar 2003 31.1SchristosMark Adler 41.1Schristosmadler@alumni.caltech.edu 51.1Schristos 61.1SchristosWhat this is -- 71.1Schristos 81.1Schristospuff.c provides the routine puff() to decompress the deflate data format. It 91.1Schristosdoes so more slowly than zlib, but the code is about one-fifth the size of the 101.1Schristosinflate code in zlib, and written to be very easy to read. 111.1Schristos 121.1SchristosWhy I wrote this -- 131.1Schristos 141.1Schristospuff.c was written to document the deflate format unambiguously, by virtue of 151.1Schristosbeing working C code. It is meant to supplement RFC 1951, which formally 161.1Schristosdescribes the deflate format. I have received many questions on details of the 171.1Schristosdeflate format, and I hope that reading this code will answer those questions. 181.1Schristospuff.c is heavily commented with details of the deflate format, especially 191.1Schristosthose little nooks and cranies of the format that might not be obvious from a 201.1Schristosspecification. 211.1Schristos 221.1Schristospuff.c may also be useful in applications where code size or memory usage is a 231.1Schristosvery limited resource, and speed is not as important. 241.1Schristos 251.1SchristosHow to use it -- 261.1Schristos 271.1SchristosWell, most likely you should just be reading puff.c and using zlib for actual 281.1Schristosapplications, but if you must ... 291.1Schristos 301.1SchristosInclude puff.h in your code, which provides this prototype: 311.1Schristos 321.1Schristosint puff(unsigned char *dest, /* pointer to destination pointer */ 331.1Schristos unsigned long *destlen, /* amount of output space */ 341.1Schristos unsigned char *source, /* pointer to source data pointer */ 351.1Schristos unsigned long *sourcelen); /* amount of input available */ 361.1Schristos 371.1SchristosThen you can call puff() to decompress a deflate stream that is in memory in 381.1Schristosits entirety at source, to a sufficiently sized block of memory for the 391.1Schristosdecompressed data at dest. puff() is the only external symbol in puff.c The 401.1Schristosonly C library functions that puff.c needs are setjmp() and longjmp(), which 411.1.1.2Schristosare used to simplify error checking in the code to improve readability. puff.c 421.1Schristosdoes no memory allocation, and uses less than 2K bytes off of the stack. 431.1Schristos 441.1SchristosIf destlen is not enough space for the uncompressed data, then inflate will 451.1Schristosreturn an error without writing more than destlen bytes. Note that this means 461.1Schristosthat in order to decompress the deflate data successfully, you need to know 471.1Schristosthe size of the uncompressed data ahead of time. 481.1Schristos 491.1SchristosIf needed, puff() can determine the size of the uncompressed data with no 501.1Schristosoutput space. This is done by passing dest equal to (unsigned char *)0. Then 511.1Schristosthe initial value of *destlen is ignored and *destlen is set to the length of 521.1Schristosthe uncompressed data. So if the size of the uncompressed data is not known, 531.1Schristosthen two passes of puff() can be used--first to determine the size, and second 541.1Schristosto do the actual inflation after allocating the appropriate memory. Not 551.1Schristospretty, but it works. (This is one of the reasons you should be using zlib.) 561.1Schristos 571.1SchristosThe deflate format is self-terminating. If the deflate stream does not end 581.1Schristosin *sourcelen bytes, puff() will return an error without reading at or past 591.1Schristosendsource. 601.1Schristos 611.1SchristosOn return, *sourcelen is updated to the amount of input data consumed, and 621.1Schristos*destlen is updated to the size of the uncompressed data. See the comments 631.1Schristosin puff.c for the possible return codes for puff(). 64