Sha256: 82e53c59d9577dac6cb587a56516b523417efc4a4642168ab763b8f426f75565

Contents?: true

Size: 1.21 KB

Versions: 43

Compression:

Stored size: 1.21 KB

Contents

#include <assert.h>
#include <stdio.h>
#include <string.h>

#include "houdini.h"

#define hex2c(c) ((c | 32) % 39 - 9)

static int
unescape(gh_buf *ob, const uint8_t *src, size_t size, bool unescape_plus)
{
	size_t  i = 0, org;

	while (i < size) {
		org = i;
		while (i < size && src[i] != '%' && src[i] != '+')
			i++;

		if (likely(i > org)) {
			if (unlikely(org == 0)) {
				if (i >= size)
					return 0;

				gh_buf_grow(ob, HOUDINI_UNESCAPED_SIZE(size));
			}

			gh_buf_put(ob, src + org, i - org);
		}

		/* escaping */
		if (i >= size)
			break;

		if (src[i++] == '+') {
			gh_buf_putc(ob, unescape_plus ? ' ' : '+');
			continue;
		}

		if (i + 1 < size && _isxdigit(src[i]) && _isxdigit(src[i + 1])) {
			unsigned char new_char = (hex2c(src[i]) << 4) + hex2c(src[i + 1]);
			gh_buf_putc(ob, new_char);
			i += 2;
		} else {
			gh_buf_putc(ob, '%');
		}
	}

	return 1;
}

int
houdini_unescape_uri(gh_buf *ob, const uint8_t *src, size_t size)
{
	return unescape(ob, src, size, false);
}

int
houdini_unescape_uri_component(gh_buf *ob, const uint8_t *src, size_t size)
{
	return unescape(ob, src, size, false);
}

int
houdini_unescape_url(gh_buf *ob, const uint8_t *src, size_t size)
{
	return unescape(ob, src, size, true);
}

Version data entries

43 entries across 43 versions & 3 rubygems

Version Path
escape_utils-1.2.2 ext/escape_utils/houdini_uri_u.c
hamlit-2.2.4 ext/hamlit/houdini/houdini_uri_u.c
hamlit-2.5.0 ext/hamlit/houdini/houdini_uri_u.c
hamlit-2.4.2 ext/hamlit/houdini/houdini_uri_u.c
hamlit-2.4.1 ext/hamlit/houdini/houdini_uri_u.c
hamlit-2.4.0 ext/hamlit/houdini/houdini_uri_u.c
hamlit-2.3.1 ext/hamlit/houdini/houdini_uri_u.c
hamlit-2.3.0 ext/hamlit/houdini/houdini_uri_u.c
escape_utils-1.1.1 ext/escape_utils/houdini_uri_u.c
escape_utils-1.2.1 ext/escape_utils/houdini_uri_u.c
hamlit-2.2.3 ext/hamlit/houdini/houdini_uri_u.c
faml-0.8.1 vendor/houdini/houdini_uri_u.c
hamlit-2.2.2 ext/hamlit/houdini/houdini_uri_u.c
hamlit-2.2.1 ext/hamlit/houdini/houdini_uri_u.c
escape_utils-1.2.0 ext/escape_utils/houdini_uri_u.c
faml-0.8.0 vendor/houdini/houdini_uri_u.c
faml-0.7.3 vendor/houdini/houdini_uri_u.c
hamlit-2.2.0 ext/hamlit/houdini/houdini_uri_u.c
hamlit-2.1.2 ext/hamlit/houdini/houdini_uri_u.c
hamlit-2.1.1 ext/hamlit/houdini/houdini_uri_u.c