An optimized C library for percent encoding/decoding text
This is a URI encoder/decoder written in C based on RFC3986. This module always encodes characters that are not unreserved. When decoding, invalid escape sequences are preserved.
The code is optimized for speed and has a reasonable test suite. It will also encode and decode null bytes in the middle of strings (assuming you calculated the string length correctly!).
#include <stdlib.h>
#include <string.h>
#include <uri_encode.h>
/* encode text */
const char* uri = "Some data!That Needs Encoding/";
size_t len = strlen(uri);
size_t b_len = uri_encode_buffer_size(uri, len);
char *buffer = (char*) calloc(b_len, sizeof(char));
assert(buffer != NULL);
buffer[0] = '\0';
uri_encode(uri, len, buffer, b_len);
/* decode text */
const char* encoded_uri = "Some%20data%21That%20Needs%20Encoding%2F";
size_t len2 = strlen(encoded_uri);
char *decoded_uri = (char*)calloc(len2 + 1, sizeof(char));
assert(decoded_uri != NULL);
decoded_uri[0] = '\0';
uri_decode(encoded_uri, decoded_uri, len2 + 1);
assert(strcmp(decoded_uri, uri) == 0);
free(decoded_uri);
free(buffer);
Builds, tests and installs a static library: liburi_encode.a
clone https://github.com/ssrlive/uri-encode-c.git uri
cd uri
make
make test
sudo make install
To install to a custom location, edit DESTDIR
and PREFIX
in Makefile
.
sudo make uninstall
- URI-Encode-XS is a Perl XS module that uses the same C code.
- My article about the C code: The road to a 55x speedup with XS
© 2016
0.04
See LICENSE