Browse Source

[core] buffer_append_string_encoded_hex_lc()

special-purpose routine is more efficient than what was previously in
buffer_append_string_encoded() with ENCODING_HEX
personal/stbuehler/fix-fdevent
Glenn Strauss 4 years ago
parent
commit
936db51f05
  1. 53
      src/buffer.c
  2. 4
      src/buffer.h
  3. 2
      src/mod_usertrack.c

53
src/buffer.c

@ -8,6 +8,8 @@
#include <time.h> /* strftime() */
static const char hex_chars[] = "0123456789abcdef";
static const char hex_chars_lc[] = "0123456789abcdef";
static const char hex_chars_uc[] = "0123456789ABCDEF";
/**
* init the buffer
@ -483,6 +485,27 @@ void buffer_substr_replace (buffer * const b, const size_t offset,
}
void buffer_append_string_encoded_hex_lc(buffer *b, const char *s, size_t len) {
unsigned char * const p =
(unsigned char*) buffer_string_prepare_append(b, len*2);
buffer_commit(b, len*2); /* fill below */
for (size_t i = 0; i < len; ++i) {
p[(i<<1)] = hex_chars_lc[(s[i] >> 4) & 0x0F];
p[(i<<1)+1] = hex_chars_lc[(s[i]) & 0x0F];
}
}
void buffer_append_string_encoded_hex_uc(buffer *b, const char *s, size_t len) {
unsigned char * const p =
(unsigned char*) buffer_string_prepare_append(b, len*2);
buffer_commit(b, len*2); /* fill below */
for (size_t i = 0; i < len; ++i) {
p[(i<<1)] = hex_chars_uc[(s[i] >> 4) & 0x0F];
p[(i<<1)+1] = hex_chars_uc[(s[i]) & 0x0F];
}
}
/* everything except: ! ( ) * - . 0-9 A-Z _ a-z */
static const char encoded_chars_rel_uri_part[] = {
/*
@ -573,28 +596,6 @@ static const char encoded_chars_minimal_xml[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* F0 - FF */
};
static const char encoded_chars_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 control chars */
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 - 4F */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 50 - 5F */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 60 - 6F */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70 - 7F */
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 */
};
static const char encoded_chars_http_header[] = {
/*
0 1 2 3 4 5 6 7 8 9 A B C D E F
@ -642,9 +643,6 @@ void buffer_append_string_encoded(buffer *b, const char *s, size_t s_len, buffer
case ENCODING_MINIMAL_XML:
map = encoded_chars_minimal_xml;
break;
case ENCODING_HEX:
map = encoded_chars_hex;
break;
case ENCODING_HTTP_HEADER:
map = encoded_chars_http_header;
break;
@ -665,7 +663,6 @@ void buffer_append_string_encoded(buffer *b, const char *s, size_t s_len, buffer
d_len += 6;
break;
case ENCODING_HTTP_HEADER:
case ENCODING_HEX:
d_len += 2;
break;
}
@ -696,10 +693,6 @@ void buffer_append_string_encoded(buffer *b, const char *s, size_t s_len, buffer
d[d_len++] = hex_chars[(*ds) & 0x0F];
d[d_len++] = ';';
break;
case ENCODING_HEX:
d[d_len++] = hex_chars[((*ds) >> 4) & 0x0F];
d[d_len++] = hex_chars[(*ds) & 0x0F];
break;
case ENCODING_HTTP_HEADER:
d[d_len++] = *ds;
d[d_len++] = '\t';

4
src/buffer.h

@ -106,12 +106,14 @@ int buffer_caseless_compare(const char *a, size_t a_len, const char *b, size_t b
void buffer_substr_replace (buffer *b, size_t offset, size_t len, const buffer *replace);
void buffer_append_string_encoded_hex_lc(buffer *b, const char *s, size_t len);
void buffer_append_string_encoded_hex_uc(buffer *b, const char *s, size_t len);
typedef enum {
ENCODING_REL_URI, /* for coding a rel-uri (/with space/and%percent) nicely as part of a href */
ENCODING_REL_URI_PART, /* same as ENC_REL_URL plus coding / too as %2F */
ENCODING_HTML, /* & becomes &amp; and so on */
ENCODING_MINIMAL_XML, /* minimal encoding for xml */
ENCODING_HEX, /* encode string as hex */
ENCODING_HTTP_HEADER /* encode \n with \t\n */
} buffer_encoding_t;

2
src/mod_usertrack.c

@ -241,7 +241,7 @@ URIHANDLER_FUNC(mod_usertrack_uri_handler) {
li_MD5_Final(h, &Md5Ctx);
buffer_append_string_encoded(ds->value, (char *)h, 16, ENCODING_HEX);
buffer_append_string_encoded_hex_lc(ds->value, (char *)h, 16);
/* usertrack.cookie-attrs, if set, replaces all other attrs */
if (!buffer_string_is_empty(p->conf.cookie_attrs)) {

Loading…
Cancel
Save