1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "houdini.h"
/**
* & --> &
* < --> <
* > --> >
* " --> "
* ' --> '
*/
static const char *LOOKUP_CODES[] = {
"", /* reserved: use literal single character */
"", /* unused */
"", /* reserved: 2 character UTF-8 */
"", /* reserved: 3 character UTF-8 */
"", /* reserved: 4 character UTF-8 */
"?", /* invalid UTF-8 character */
""",
"&",
"'",
"<",
">"
};
static const char CODE_INVALID = 5;
static const char XML_LOOKUP_TABLE[] = {
/* ASCII: 0xxxxxxx */
5, 5, 5, 5, 5, 5, 5, 5, 5, 0, 0, 5, 5, 0, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
0, 0, 6, 0, 0, 0, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0,10, 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,
/* Invalid UTF-8 char start: 10xxxxxx */
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
/* Multibyte UTF-8 */
/* 2 bytes: 110xxxxx */
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
/* 3 bytes: 1110xxxx */
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
/* 4 bytes: 11110xxx */
4, 4, 4, 4, 4, 4, 4, 4,
/* Invalid UTF-8: 11111xxx */
5, 5, 5, 5, 5, 5, 5, 5,
};
int
houdini_escape_xml(gh_buf *ob, const uint8_t *src, size_t size)
{
size_t i = 0;
unsigned char code = 0;
gh_buf_grow(ob, HOUDINI_ESCAPED_SIZE(size));
while (i < size) {
size_t start, end;
start = end = i;
while (i < size) {
unsigned int byte;
byte = src[i++];
code = XML_LOOKUP_TABLE[byte];
if (!code) {
/* single character used literally */
} else if (code >= CODE_INVALID) {
break; /* insert lookup code string */
} else if (code > size - end) {
code = CODE_INVALID; /* truncated UTF-8 character */
break;
} else {
unsigned int chr = byte & (0xff >> code);
while (--code) {
byte = src[i++];
if ((byte & 0xc0) != 0x80) {
code = CODE_INVALID;
break;
}
chr = (chr << 6) + (byte & 0x3f);
}
switch (i - end) {
case 2:
if (chr < 0x80)
code = CODE_INVALID;
break;
case 3:
if (chr < 0x800 ||
(chr > 0xd7ff && chr < 0xe000) ||
chr > 0xfffd)
code = CODE_INVALID;
break;
case 4:
if (chr < 0x10000 || chr > 0x10ffff)
code = CODE_INVALID;
break;
default:
break;
}
if (code == CODE_INVALID)
break;
}
end = i;
}
if (end > start)
gh_buf_put(ob, src + start, end - start);
/* escaping */
if (end >= size)
break;
gh_buf_puts(ob, LOOKUP_CODES[code]);
}
return 1;
}
|