-
Notifications
You must be signed in to change notification settings - Fork 0
/
html.c
141 lines (130 loc) · 3.64 KB
/
html.c
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
137
138
139
140
/*-
* Copyright (c) 2009 Thomas Hurst <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "html.h"
int
html_entities_max_entity_size ()
{
return MAX_ENTITY_SIZE;
}
char *
html_entities_ascii_buffer (len)
size_t len;
{
return malloc((len * html_entities_max_entity_size()) + 1);
}
int
html_entities_ascii (buf, len, escaped)
char *buf;
size_t len;
char *escaped;
{
unsigned int i,j = 0;
unsigned int *chunk;
static char *hex = "0123456789ABCDEF";
for (i = 0; i < len; i++)
{
// Checking longs is very fast, but only on data which contains
// long strings of alphabetic characters without numbers or spaces.
// Message-ID's usually contain numbers, with the occasional static
// string like "powerpost", which we can reduce to "powe" "rpos" "t"
while (len - i >= sizeof(unsigned int))
{
chunk = (unsigned int *)((char *)buf + i);
// null check
if ((*chunk - 0x01010101UL) & ~(*chunk) & 0x80808080UL) break;
if ((*chunk & 0xc0c0c0c0) == 0x40404040)
{
//memcpy(escaped + j, chunk, sizeof(int));
*(unsigned int *)(escaped + j) = *chunk;
j += sizeof(int);
i += sizeof(int);
}
else break;
}
if (i >= len) break;
unsigned char c = buf[i];
switch (c)
{
case '&':
memcpy(escaped + j, "&", 5);
j += 5;
break;
case '<':
memcpy(escaped + j, "<", 4);
j += 4;
break;
case '"':
memcpy(escaped + j, """, 6);
j += 6;
break;
case '\000':
goto fin;
default:
if (c <= 8 || c == 0xb || c == 0xc ||
(c >= 0xe && c <= 0x1f) || (c >= 0x7f && 0x84) ||
(c >= 0x86 && c <= 0x9f))
{
escaped[j++] = '&';
escaped[j++] = '#';
escaped[j++] = 'x';
// j += sprintf(..) reduces performance for some reason
// Also, shorter sprintf's are faster.
// sprintf(escaped + j, "%.2X", c);
// j += 2;
escaped[j++] = (hex[c >> 4 & 0x7f % 16]);
escaped[j++] = (hex[c & 0x7f % 16]);
escaped[j++] = ';';
}
else
escaped[j++] = c;
}
}
fin:
escaped[j] = '\000';
return j;
}
#ifdef BUILD_TEST
int
main ()
{
//char *buf = "foof$&b\"ar\"\001@moo<moo>";
//char *buf = "part1of201.i5WqnoDaVEHByaHWA&[email protected]";
char *buf = "part1of55.ghyV7aVQwwjNyrAVm4rg@pornk";
//char *buf = "\001&\"\xff\xf3<>";
size_t len = strlen(buf);
size_t elen;
char *e = html_entities_ascii_buffer(len);
printf("in: %s (%ld bytes)\n", buf, len);
elen = html_entities_ascii(buf, len, e);
printf("out: %s (%ld bytes)\n", e, elen);
int i;
for (i=0; i < 1000000; i++)
{
html_entities_ascii(buf, len, e);
}
free(e);
return 0;
}
#endif