Skip to content

Commit

Permalink
Add remaining Houdini files (differs from Sundown bundled version, bu…
Browse files Browse the repository at this point in the history
…t there isn't any change to the API, only a new method added)
  • Loading branch information
mildsunrise committed Sep 11, 2012
1 parent 75da699 commit f767ec5
Show file tree
Hide file tree
Showing 13 changed files with 1,358 additions and 52 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Expand Up @@ -4,3 +4,6 @@
[submodule "v8u"]
path = v8u
url = https://github.com/jmendeth/v8u.git
[submodule "houdini"]
path = houdini
url = https://github.com/vmg/houdini.git
1 change: 1 addition & 0 deletions houdini
Submodule houdini added at 14c4eb
85 changes: 44 additions & 41 deletions src/buffer.c
Expand Up @@ -22,7 +22,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

/* MSVC compat */
#if defined(_MSC_VER)
Expand All @@ -35,7 +34,6 @@ int
bufprefix(const struct buf *buf, const char *prefix)
{
size_t i;
assert(buf && buf->unit);

for (i = 0; i < buf->size; ++i) {
if (prefix[i] == 0)
Expand All @@ -54,10 +52,7 @@ bufgrow(struct buf *buf, size_t neosz)
{
size_t neoasz;
void *neodata;

assert(buf && buf->unit);

if (neosz > BUFFER_MAX_ALLOC_SIZE)
if (!buf || !buf->unit || neosz > BUFFER_MAX_ALLOC_SIZE)
return BUF_ENOMEM;

if (buf->asize >= neosz)
Expand Down Expand Up @@ -96,7 +91,8 @@ bufnew(size_t unit)
const char *
bufcstr(struct buf *buf)
{
assert(buf && buf->unit);
if (!buf || !buf->unit)
return NULL;

if (buf->size < buf->asize && buf->data[buf->size] == 0)
return (char *)buf->data;
Expand All @@ -114,47 +110,20 @@ void
bufprintf(struct buf *buf, const char *fmt, ...)
{
va_list ap;
int n;

assert(buf && buf->unit);

if (buf->size >= buf->asize && bufgrow(buf, buf->size + 1) < 0)
if (!buf || !buf->unit)
return;

va_start(ap, fmt);
n = _buf_vsnprintf((char *)buf->data + buf->size, buf->asize - buf->size, fmt, ap);
vbufprintf(buf, fmt, ap);
va_end(ap);

if (n < 0) {
#ifdef _MSC_VER
va_start(ap, fmt);
n = _vscprintf(fmt, ap);
va_end(ap);
#else
return;
#endif
}

if ((size_t)n >= buf->asize - buf->size) {
if (bufgrow(buf, buf->size + n + 1) < 0)
return;

va_start(ap, fmt);
n = _buf_vsnprintf((char *)buf->data + buf->size, buf->asize - buf->size, fmt, ap);
va_end(ap);
}

if (n < 0)
return;

buf->size += n;
}

/* bufput: appends raw data to a buffer */
void
bufput(struct buf *buf, const void *data, size_t len)
{
assert(buf && buf->unit);
if (!buf)
return;

if (buf->size + len > buf->asize && bufgrow(buf, buf->size + len) < 0)
return;
Expand All @@ -175,7 +144,8 @@ bufputs(struct buf *buf, const char *str)
void
bufputc(struct buf *buf, int c)
{
assert(buf && buf->unit);
if (!buf)
return;

if (buf->size + 1 > buf->asize && bufgrow(buf, buf->size + 1) < 0)
return;
Expand Down Expand Up @@ -212,7 +182,8 @@ bufreset(struct buf *buf)
void
bufslurp(struct buf *buf, size_t len)
{
assert(buf && buf->unit);
if (!buf || !buf->unit || len <= 0)
return;

if (len >= buf->size) {
buf->size = 0;
Expand All @@ -223,3 +194,35 @@ bufslurp(struct buf *buf, size_t len)
memmove(buf->data, buf->data + len, buf->size);
}

/* vbufprintf: stdarg variant of formatted printing into a buffer */
void
vbufprintf(struct buf *buf, const char *fmt, va_list ap)
{
int n;

if (buf == 0 || (buf->size >= buf->asize && bufgrow(buf, buf->size + 1)) < 0)
return;

n = _buf_vsnprintf((char *)buf->data + buf->size, buf->asize - buf->size, fmt, ap);

if (n < 0) {
#ifdef _MSC_VER
n = _vscprintf(fmt, ap);
#else
return;
#endif
}

if ((size_t)n >= buf->asize - buf->size) {
if (bufgrow(buf, buf->size + n + 1) < 0)
return;

n = _buf_vsnprintf((char *)buf->data + buf->size, buf->asize - buf->size, fmt, ap);
}

if (n < 0)
return;

buf->size += n;
}

15 changes: 9 additions & 6 deletions src/buffer.h
Expand Up @@ -15,17 +15,17 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#ifndef BUFFER_H__
#define BUFFER_H__

#include <stddef.h>
#include <stdarg.h>
#include <stdint.h>
#ifndef __GEN_BUFFER_H__
#define __GEN_BUFFER_H__

#ifdef __cplusplus
extern "C" {
#endif

#include <stddef.h>
#include <stdarg.h>
#include <stdint.h>

#if defined(_MSC_VER)
#define __attribute__(x)
#define inline
Expand Down Expand Up @@ -89,6 +89,9 @@ void bufslurp(struct buf *, size_t);
/* bufprintf: formatted printing to a buffer */
void bufprintf(struct buf *, const char *, ...) __attribute__ ((format (printf, 2, 3)));

/* vbufprintf: stdarg variant of formatted printing into a buffer */
void vbufprintf(struct buf *, const char * , va_list);

#ifdef __cplusplus
}
#endif
Expand Down
8 changes: 4 additions & 4 deletions src/houdini.h
@@ -1,12 +1,12 @@
#ifndef HOUDINI_H__
#define HOUDINI_H__

#include "buffer.h"
#ifndef __HOUDINI_H__
#define __HOUDINI_H__

#ifdef __cplusplus
extern "C" {
#endif

#include "buffer.h"

#ifdef HOUDINI_USE_LOCALE
# define _isxdigit(c) isxdigit(c)
# define _isdigit(c) isdigit(c)
Expand Down
2 changes: 1 addition & 1 deletion src/houdini_html_e.c
Expand Up @@ -49,7 +49,7 @@ static const char *HTML_ESCAPES[] = {
void
houdini_escape_html0(struct buf *ob, const uint8_t *src, size_t size, int secure)
{
size_t i = 0, org, esc = 0;
size_t i = 0, org, esc = 0;

bufgrow(ob, ESCAPE_GROW_FACTOR(size));

Expand Down
116 changes: 116 additions & 0 deletions src/houdini_html_u.c
@@ -0,0 +1,116 @@
#include <assert.h>
#include <stdio.h>
#include <string.h>

#include "houdini.h"
#include "html_unescape.h"

#define UNESCAPE_GROW_FACTOR(x) (x) /* unescaping shouldn't grow our buffer */

static inline void
bufput_utf8(struct buf *ob, int c)
{
unsigned char unichar[4];

if (c < 0x80) {
bufputc(ob, c);
}
else if (c < 0x800) {
unichar[0] = 192 + (c / 64);
unichar[1] = 128 + (c % 64);
bufput(ob, unichar, 2);
}
else if (c - 0xd800u < 0x800) {
bufputc(ob, '?');
}
else if (c < 0x10000) {
unichar[0] = 224 + (c / 4096);
unichar[1] = 128 + (c / 64) % 64;
unichar[2] = 128 + (c % 64);
bufput(ob, unichar, 3);
}
else if (c < 0x110000) {
unichar[0] = 240 + (c / 262144);
unichar[1] = 128 + (c / 4096) % 64;
unichar[2] = 128 + (c / 64) % 64;
unichar[3] = 128 + (c % 64);
bufput(ob, unichar, 4);
}
else {
bufputc(ob, '?');
}
}

static size_t
unescape_ent(struct buf *ob, const uint8_t *src, size_t size)
{
size_t i = 0;

if (size > 3 && src[0] == '#') {
int codepoint = 0;

if (_isdigit(src[1])) {
for (i = 1; i < size && _isdigit(src[i]); ++i)
codepoint = (codepoint * 10) + (src[i] - '0');
}

else if (src[1] == 'x' || src[1] == 'X') {
for (i = 2; i < size && _isxdigit(src[i]); ++i)
codepoint = (codepoint * 16) + ((src[i] | 32) % 39 - 9);
}

if (i < size && src[i] == ';') {
bufput_utf8(ob, codepoint);
return i + 1;
}
}

else {
if (size > MAX_WORD_LENGTH)
size = MAX_WORD_LENGTH;

for (i = MIN_WORD_LENGTH; i < size; ++i) {
if (src[i] == ' ')
break;

if (src[i] == ';') {
const struct html_ent *entity = find_entity((char *)src, i);

if (entity != NULL) {
bufput(ob, entity->utf8, entity->utf8_len);
return i + 1;
}

break;
}
}
}

bufputc(ob, '&');
return 0;
}

void
houdini_unescape_html(struct buf *ob, const uint8_t *src, size_t size)
{
size_t i = 0, org;

bufgrow(ob, UNESCAPE_GROW_FACTOR(size));

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

if (i > org)
bufput(ob, src + org, i - org);

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

i++;
i += unescape_ent(ob, src + i, size - i);
}
}

0 comments on commit f767ec5

Please sign in to comment.