Sha256: d7b724186c9c879cd3c878d54b58caa9632b4db1041e68f7a6d7ca4b80f77d05

Contents?: true

Size: 1.84 KB

Versions: 52

Compression:

Stored size: 1.84 KB

Contents

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

#include "houdini.h"

static const char JS_ESCAPE[] = {
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
	0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};

int
houdini_escape_js(gh_buf *ob, const uint8_t *src, size_t size)
{
	size_t  i = 0, org, ch;

	while (i < size) {
		org = i;
		while (i < size && JS_ESCAPE[src[i]] == 0)
			i++;

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

				gh_buf_grow(ob, HOUDINI_ESCAPED_SIZE(size));
			}

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

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

		ch = src[i];
		
		switch (ch) {
		case '/':
			/*
			 * Escape only if preceded by a lt
			 */
			if (i && src[i - 1] == '<')
				gh_buf_putc(ob, '\\');

			gh_buf_putc(ob, ch);
			break;

		case '\r':
			/*
			 * Escape as \n, and skip the next \n if it's there
			 */
			if (i + 1 < size && src[i + 1] == '\n') i++;

		case '\n':
			/*
			 * Escape actually as '\','n', not as '\', '\n'
			 */
			ch = 'n';

		default:
			/*
			 * Normal escaping
			 */
			gh_buf_putc(ob, '\\');
			gh_buf_putc(ob, ch);
			break;
		}

		i++;
	}

	return 1;
}

Version data entries

52 entries across 52 versions & 4 rubygems

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