2
0
Fork 0

add encoding functions

personal/stbuehler/wip
Thomas Porzelt 2009-03-05 00:19:55 +01:00
parent d5ec193b04
commit a888c34dbb
4 changed files with 126 additions and 0 deletions

View File

@ -51,6 +51,7 @@
#include <lighttpd/filter_chunked.h>
#include <lighttpd/collect.h>
#include <lighttpd/network.h>
#include <lighttpd/encoding.h>
#include <lighttpd/utils.h>
#define SERVER_VERSION ((guint) 0x01FF0000)

View File

@ -0,0 +1,17 @@
#ifndef _LIGHTTPD_ENCODING_H_
#define _LIGHTTPD_ENCODING_H_
#ifndef _LIGHTTPD_BASE_H_
#error Please include <lighttpd/base.h> instead of this file
#endif
typedef enum {
ENCODING_HEX, /* a => 61 */
ENCODING_HTML /* HTML special chars. & => &amp; e.g. */
} encoding_t;
/* encodes special characters in a string and returns the new string */
GString *string_encode(const gchar *str, encoding_t encoding);
#endif

107
src/encoding.c Normal file
View File

@ -0,0 +1,107 @@
#include <lighttpd/base.h>
static const gchar hex_chars[] = "0123456789abcdef";
/* HEX */
static const gchar encode_map_hex[] = {
/*
0 1 2 3 4 5 6 7 8 9 A B C D E F
*/
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00 - 0F */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 - 1F */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 20 - 2F */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 30 - 3F */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 40 - 40 */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 50 - 50 */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 60 - 60 */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70 - 70 */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 80 - 8F */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 90 - 9F */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* A0 - AF */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* B0 - BF */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* C0 - CF */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* D0 - DF */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* E0 - EF */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* F0 - FF */
};
/* HTML */
static const gchar encode_map_html[] = {
/*
0 1 2 3 4 5 6 7 8 9 A B C D E F
*/
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00 - 0F control chars */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 - 1F */
0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 20 - 2F & */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, /* 30 - 3F < > */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 - 40 */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 50 - 50 */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 60 */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, /* 70 - 70 DEL */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 80 - 8F */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 90 - 9F */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* A0 - AF */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* B0 - BF */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* C0 - CF */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* D0 - DF */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* E0 - EF */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* F0 - FF */
};
GString *string_encode(const gchar *str, encoding_t encoding) {
/* replace html chars with &#xHH; */
GString *result;
guchar *c;
guchar *pos;
gsize new_len = 0;
guint encoded_len = 0;
const gchar *map = NULL;
switch (encoding) {
case ENCODING_HTML:
map = encode_map_html;
encoded_len = 6;
break;
case ENCODING_HEX:
map = encode_map_hex;
encoded_len = 2;
break;
}
/* check how many chars need to be encoded */
for (c = (guchar*)str; *c != '\0'; c++) {
if (map[*c])
new_len += encoded_len;
else
new_len++;
}
result = g_string_sized_new(new_len);
for (c = (guchar*)str, pos = (guchar*)result->str; *c != '\0'; c++) {
if (map[*c]) {
/* char needs to be encoded */
switch (encoding) {
case ENCODING_HTML:
/* &#xHH */
*pos++ = '&';
*pos++ = '#';
*pos++ = 'x';
*pos++ = hex_chars[((*c) >> 4) & 0x0F];
*pos++ = hex_chars[(*c) & 0x0F];
*pos++ = ';';
break;
case ENCODING_HEX:
*pos++ = hex_chars[((*c) >> 4) & 0x0F];
*pos++ = hex_chars[(*c) & 0x0F];
}
} else {
/* no encoding needed */
*pos++ = *c;
}
}
*pos = '\0';
return result;
}

View File

@ -26,6 +26,7 @@ common_src = '''
condition_parsers.rl
config_parser.rl
connection.c
encoding.c
environment.c
filter_chunked.c
http_headers.c