1*212397c6SchristosPuff -- A Simple Inflate 2*212397c6Schristos3 Mar 2003 3*212397c6SchristosMark Adler 4*212397c6Schristosmadler@alumni.caltech.edu 5*212397c6Schristos 6*212397c6SchristosWhat this is -- 7*212397c6Schristos 8*212397c6Schristospuff.c provides the routine puff() to decompress the deflate data format. It 9*212397c6Schristosdoes so more slowly than zlib, but the code is about one-fifth the size of the 10*212397c6Schristosinflate code in zlib, and written to be very easy to read. 11*212397c6Schristos 12*212397c6SchristosWhy I wrote this -- 13*212397c6Schristos 14*212397c6Schristospuff.c was written to document the deflate format unambiguously, by virtue of 15*212397c6Schristosbeing working C code. It is meant to supplement RFC 1951, which formally 16*212397c6Schristosdescribes the deflate format. I have received many questions on details of the 17*212397c6Schristosdeflate format, and I hope that reading this code will answer those questions. 18*212397c6Schristospuff.c is heavily commented with details of the deflate format, especially 19*212397c6Schristosthose little nooks and cranies of the format that might not be obvious from a 20*212397c6Schristosspecification. 21*212397c6Schristos 22*212397c6Schristospuff.c may also be useful in applications where code size or memory usage is a 23*212397c6Schristosvery limited resource, and speed is not as important. 24*212397c6Schristos 25*212397c6SchristosHow to use it -- 26*212397c6Schristos 27*212397c6SchristosWell, most likely you should just be reading puff.c and using zlib for actual 28*212397c6Schristosapplications, but if you must ... 29*212397c6Schristos 30*212397c6SchristosInclude puff.h in your code, which provides this prototype: 31*212397c6Schristos 32*212397c6Schristosint puff(unsigned char *dest, /* pointer to destination pointer */ 33*212397c6Schristos unsigned long *destlen, /* amount of output space */ 34*212397c6Schristos unsigned char *source, /* pointer to source data pointer */ 35*212397c6Schristos unsigned long *sourcelen); /* amount of input available */ 36*212397c6Schristos 37*212397c6SchristosThen you can call puff() to decompress a deflate stream that is in memory in 38*212397c6Schristosits entirety at source, to a sufficiently sized block of memory for the 39*212397c6Schristosdecompressed data at dest. puff() is the only external symbol in puff.c The 40*212397c6Schristosonly C library functions that puff.c needs are setjmp() and longjmp(), which 41*212397c6Schristosare used to simplify error checking in the code to improve readabilty. puff.c 42*212397c6Schristosdoes no memory allocation, and uses less than 2K bytes off of the stack. 43*212397c6Schristos 44*212397c6SchristosIf destlen is not enough space for the uncompressed data, then inflate will 45*212397c6Schristosreturn an error without writing more than destlen bytes. Note that this means 46*212397c6Schristosthat in order to decompress the deflate data successfully, you need to know 47*212397c6Schristosthe size of the uncompressed data ahead of time. 48*212397c6Schristos 49*212397c6SchristosIf needed, puff() can determine the size of the uncompressed data with no 50*212397c6Schristosoutput space. This is done by passing dest equal to (unsigned char *)0. Then 51*212397c6Schristosthe initial value of *destlen is ignored and *destlen is set to the length of 52*212397c6Schristosthe uncompressed data. So if the size of the uncompressed data is not known, 53*212397c6Schristosthen two passes of puff() can be used--first to determine the size, and second 54*212397c6Schristosto do the actual inflation after allocating the appropriate memory. Not 55*212397c6Schristospretty, but it works. (This is one of the reasons you should be using zlib.) 56*212397c6Schristos 57*212397c6SchristosThe deflate format is self-terminating. If the deflate stream does not end 58*212397c6Schristosin *sourcelen bytes, puff() will return an error without reading at or past 59*212397c6Schristosendsource. 60*212397c6Schristos 61*212397c6SchristosOn return, *sourcelen is updated to the amount of input data consumed, and 62*212397c6Schristos*destlen is updated to the size of the uncompressed data. See the comments 63*212397c6Schristosin puff.c for the possible return codes for puff(). 64