Commit 9a609dc7 authored by Daniel Friesel's avatar Daniel Friesel
Browse files

this library implements inflate, not deflate

parent a288a4a4
Loading
Loading
Loading
Loading
+24 −19
Original line number Original line Diff line number Diff line
@@ -11,12 +11,17 @@ for speed. Right now, the implementation is naive, but usable. See below for
the current status and TODOs. Be aware that this library has not been
the current status and TODOs. Be aware that this library has not been
extensively tested yet.
extensively tested yet.


Note: This library *inflates* (i.e., decompresses) data. The source files and
API are named as such, as is the corresponding function in the original zlib
implementation. However, as the algorithm is called *deflate*, the project is
named zlib-*deflate*-nostdlib even though it does not support compression.

## Usage
## Usage


Embed `deflate.c` and `deflate.h` into your project. You can rename `deflate.c`
Embed `inflate.c` and `inflate.h` into your project. You can rename `inflate.c`
to `deflate.cc` and/or compile it with g++ instead of gcc, if you like. Use
to `inflate.cc` and/or compile it with g++ instead of gcc, if you like. Use
`deflate_zlib(input, input_len, output, output_len)` to decompress zlib data,
`inflate_zlib(input, input_len, output, output_len)` to decompress zlib data,
and `deflate(input, input_len, output, output_len)` to decompress deflate data
and `inflate(input, input_len, output, output_len)` to decompress deflate data
without zlib header.
without zlib header.


input and output must be `unsigned char *`, input\_len and output\_len are
input and output must be `unsigned char *`, input\_len and output\_len are
@@ -26,24 +31,24 @@ bytes written to `output`, or a negative value on error.
Example for zlib decompression (RFC 1950):
Example for zlib decompression (RFC 1950):


```
```
#include "deflate.h"
#include "inflate.h"


unsigned char deflate_input[] = { /* some compressed data, e.g.: */
unsigned char inflate_input[] = { /* some compressed data, e.g.: */
    120, 156, 243, 72, 205, 201, 201, 215, 81, 8, 207, 47, 202, 73, 177, 87,
    120, 156, 243, 72, 205, 201, 201, 215, 81, 8, 207, 47, 202, 73, 177, 87,
    240, 64, 226, 41, 2, 0, 128, 125, 9, 17
    240, 64, 226, 41, 2, 0, 128, 125, 9, 17
};
};


unsigned char deflate_output[128];
unsigned char inflate_output[128];


// within some function
// within some function
{
{
    int16_t out_bytes = deflate_zlib(deflate_input, sizeof(deflate_input),
    int16_t out_bytes = inflate_zlib(inflate_input, sizeof(inflate_input),
                                     deflate_output, sizeof(deflate_output));
                                     inflate_output, sizeof(inflate_output));
    if (out_bytes < 0) {
    if (out_bytes < 0) {
        // error
        // error
    } else {
    } else {
        // success. deflate_output contains "Hello, World? Hello, World!"
        // success. inflate_output contains "Hello, World? Hello, World!"
        // out_bytes contains the number of bytes written to deflate_output
        // out_bytes contains the number of bytes written to inflate_output
    }
    }
}
}


@@ -52,24 +57,24 @@ unsigned char deflate_output[128];
Decompressing deflate (RFC 1951) data works as follows:
Decompressing deflate (RFC 1951) data works as follows:


```
```
#include "deflate.h"
#include "inflate.h"


unsigned char deflate_input[] = { /* some compressed data, e.g.: */
unsigned char inflate_input[] = { /* some compressed data, e.g.: */
    243, 72, 205, 201, 201, 215, 81, 8, 207, 47, 202, 73, 177, 87,
    243, 72, 205, 201, 201, 215, 81, 8, 207, 47, 202, 73, 177, 87,
    240, 64, 226, 41, 2, 0
    240, 64, 226, 41, 2, 0
};
};


unsigned char deflate_output[128];
unsigned char inflate_output[128];


// within some function
// within some function
{
{
    int16_t out_bytes = deflate(deflate_input, sizeof(deflate_input),
    int16_t out_bytes = inflate(inflate_input, sizeof(inflate_input),
                                deflate_output, sizeof(deflate_output));
                                inflate_output, sizeof(inflate_output));
    if (out_bytes < 0) {
    if (out_bytes < 0) {
        // error
        // error
    } else {
    } else {
        // success. deflate_output contains "Hello, World? Hello, World!"
        // success. inflate_output contains "Hello, World? Hello, World!"
        // out_bytes contains the number of bytes written to deflate_output
        // out_bytes contains the number of bytes written to inflate_output
    }
    }
}
}


@@ -78,7 +83,7 @@ unsigned char deflate_output[128];
## Compilation flags
## Compilation flags


Compile with `-DDEFLATE_CHECKSUM` to enable verification of the zlib ADLER32
Compile with `-DDEFLATE_CHECKSUM` to enable verification of the zlib ADLER32
checksum in `deflate_zlib`.
checksum in `inflate_zlib`.


## Compliance
## Compliance


+4 −4
Original line number Original line Diff line number Diff line
@@ -6,7 +6,7 @@
 * SPDX-License-Identifier: BSD-2-Clause
 * SPDX-License-Identifier: BSD-2-Clause
 */
 */


#include "lib/deflate.h"
#include "lib/inflate.h"


/*
/*
 * The compressed (inflated) input data.
 * The compressed (inflated) input data.
@@ -370,7 +370,7 @@ static int8_t deflate_dynamic_huffman()
			       deflate_lld_lengths + hlit, hdist);
			       deflate_lld_lengths + hlit, hdist);
}
}


int16_t deflate(unsigned char *input_buf, uint16_t input_len,
int16_t inflate(unsigned char *input_buf, uint16_t input_len,
		unsigned char *output_buf, uint16_t output_len)
		unsigned char *output_buf, uint16_t output_len)
{
{
	//uint8_t is_final = input_buf[0] & 0x01;
	//uint8_t is_final = input_buf[0] & 0x01;
@@ -405,7 +405,7 @@ int16_t deflate(unsigned char *input_buf, uint16_t input_len,
	return deflate_output_now - output_buf;
	return deflate_output_now - output_buf;
}
}


int16_t deflate_zlib(unsigned char *input_buf, uint16_t input_len,
int16_t inflate_zlib(unsigned char *input_buf, uint16_t input_len,
		     unsigned char *output_buf, uint16_t output_len)
		     unsigned char *output_buf, uint16_t output_len)
{
{
	if (input_len < 4) {
	if (input_len < 4) {
@@ -427,7 +427,7 @@ int16_t deflate_zlib(unsigned char *input_buf, uint16_t input_len,
	}
	}


	int16_t ret =
	int16_t ret =
	    deflate(input_buf + 2, input_len - 2, output_buf, output_len);
	    inflate(input_buf + 2, input_len - 2, output_buf, output_len);


#ifdef DEFLATE_CHECKSUM
#ifdef DEFLATE_CHECKSUM
	if (ret >= 0) {
	if (ret >= 0) {
+2 −2
Original line number Original line Diff line number Diff line
@@ -17,7 +17,7 @@
#define DEFLATE_ERR_FCHECK (-7)
#define DEFLATE_ERR_FCHECK (-7)
#define DEFLATE_ERR_NLEN (-8)
#define DEFLATE_ERR_NLEN (-8)


int16_t deflate(unsigned char *input_buf, uint16_t input_len,
int16_t inflate(unsigned char *input_buf, uint16_t input_len,
		unsigned char *output_buf, uint16_t output_len);
		unsigned char *output_buf, uint16_t output_len);
int16_t deflate_zlib(unsigned char *input_buf, uint16_t input_len,
int16_t inflate_zlib(unsigned char *input_buf, uint16_t input_len,
		     unsigned char *output_buf, uint16_t output_len);
		     unsigned char *output_buf, uint16_t output_len);