HPACK - HTTP/2 Compression

To build

make dist

To use

These examples use static linking:

HPACK integer encoding

#include <stdint.h>
#include "lib/hpack.h"

unsigned int i = 987; /* number to encode */
size_t  nbits = 6;    /* number of bits to occupy in the first byte */
uint8_t pbits = 0x40; /* initial value of the first byte */

#define BUFFER_SIZE 4321
uint8_t buffer[BUFFER_SIZE];
size_t  written;      /* will hold the number of bytes written */

written = hpack_encode_int(i, nbits, pbits, buffer, BUFFER_SIZE, NULL);
if (hpack_encoder_error) {
    fprintf(2, "Error encoding integer[%u]: %d\n", i, hpack_encoder_error);
} else {
    printf("Encoded integer[%u] in %d bytes\n", i, written);
}
#include <stdint.h>
#include "lib/hpack.h"

#define CODE_LENGTH 10
uint8_t code[CODE_LENGH] = "\xFF\xF2\x81\xC0\x80\x01XYZ";
size_t  read;         /* will hold the number of bytes read */
size_t  nbits = 6;    /* number of bits to occupy in the first byte */

unsigned int i;       /* number to encode */
uint8_t pbits;        /* will hold remainder of the first byte */

i = hpack_decode_int(code, CODE_LENGTH, &read, nbits, NULL, &pbits);
if (hpack_decoder_error) {
    fprintf(2, "Error decoding integer: %d\n", hpack_encoder_error);
} else {
    printf("Decoded integer[%u] from %d bytes, prefix was %02x\n", i, read, pbits);
}

Huffman string encoding

#include <stdint.h>
#include "lib/huffman.h"

#define STRING_LENGTH 16
uint8_t string[STRING_LENGH] = "www.example.com";

#define BUFFER_SIZE 4321
uint8_t buffer[BUFFER_SIZE];
size_t  written;      /* will hold the number of bytes written */

written = huffman_encode(string, STRING_LENGTH, NULL, buffer, BUFFER_SIZE, NULL);
if (huffman_encoder_error) {
    fprintf(2, "Error encoding string: %d\n", huffman_encoder_error);
} else {
    printf("Encoded %d-byte string in %d bytes\n", STRING_LENGTH, written);
}
#include <stdint.h>
#include "lib/huffman.h"

#define CODE_LENGTH 16
uint8_t code[CODE_LENGH] = "\xE7\xCF\x9B\xEB\xE8\x9B\x6F\xB1\x6F\xA9\xB6\xFFXYZ";
size_t  read;         /* will hold the number of bytes read */

#define BUFFER_SIZE 4321
uint8_t buffer[BUFFER_SIZE];
size_t  written;      /* will hold the number of bytes written */

written = huffman_decode(code, CODE_LENGTH, &read, buffer, BUFFER_SIZE, NULL);
if (huffman_decoder_error) {
    fprintf(2, "Error decoding string: %d\n", huffman_decoder_error);
} else {
    printf("Decoded %d-byte string from %d bytes\n", written, read);
}

Contributor Code of Conduct

This repository is subject to a Contributor Code of Conduct
adapted from the Contributor Covenant, version 1.4, available at
http://contributor-covenant.org/version/1/4

License

Copyright (c) 2014, Matthew Kerwin matthew@kerwin.net.au

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Copyright (c) 2014, Matthew Kerwin 

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Contributor Covenant Code of Conduct

Our Pledge

In the interest of fostering an open and welcoming environment, we as
contributors and maintainers pledge to making participation in our
project and our community a harassment-free experience for everyone,
regardless of age, body size, disability, ethnicity, gender identity
and expression, level of experience, nationality, personal appearance,
race, religion, or sexual identity and orientation.

Our Standards

Examples of behavior that contributes to creating a positive
environment include:

Examples of unacceptable behavior by participants include:

Our Responsibilities

Project maintainers are responsible for clarifying the standards of
acceptable behavior and are expected to take appropriate and fair
corrective action in response to any instances of unacceptable behavior.

Project maintainers have the right and responsibility to remove, edit,
or reject comments, commits, code, wiki edits, issues, and other
contributions that are not aligned to this Code of Conduct, or to ban
temporarily or permanently any contributor for other behaviors that they
deem inappropriate, threatening, offensive, or harmful.

Scope

This Code of Conduct applies both within project spaces and in public
spaces when an individual is representing the project or its community.
Examples of representing a project or community include using an
official project e-mail address, posting via an official social media
account, or acting as an appointed representative at an online or
offline event. Representation of a project may be further defined and
clarified by project maintainers.

Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may
be reported by contacting the project team at matthew@kerwin.net.au.
All complaints will be reviewed and investigated and will result in a
response that is deemed necessary and appropriate to the circumstances.
The project team is obligated to maintain confidentiality with regard to
the reporter of an incident. Further details of specific enforcement
policies may be posted separately.

Project maintainers who do not follow or enforce the Code of Conduct in
good faith may face temporary or permanent repercussions as determined
by other members of the project's leadership.

Attribution

This Code of Conduct is adapted from the Contributor Covenant,
version 1.4, available at http://contributor-covenant.org/version/1/4