summaryrefslogtreecommitdiff
path: root/src/buffer.c
diff options
context:
space:
mode:
authorArno Töll <arno@debian.org>2012-11-21 23:04:05 +0100
committerArno Töll <arno@debian.org>2012-11-21 23:04:05 +0100
commite787ca6640e45e5c28913d149b0ecd9810930b8a (patch)
tree0864b0dfde2872cf629fc0402f9dd2b8ef6c5f9c /src/buffer.c
parent6c8b72678daa33f7e9df2a97d6404380e1b2e8ca (diff)
downloadlighttpd-e787ca6640e45e5c28913d149b0ecd9810930b8a.tar.gz
Imported Upstream version 1.4.8upstream/1.4.8
Diffstat (limited to 'src/buffer.c')
-rw-r--r--src/buffer.c351
1 files changed, 135 insertions, 216 deletions
diff --git a/src/buffer.c b/src/buffer.c
index 43fb71c..cd4a72a 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -1,5 +1,3 @@
-#include "buffer.h"
-
#include <stdlib.h>
#include <string.h>
@@ -7,25 +5,27 @@
#include <assert.h>
#include <ctype.h>
+#include "buffer.h"
+
static const char hex_chars[] = "0123456789abcdef";
/**
- * init the buffer
- *
+ * init the buffer
+ *
*/
buffer* buffer_init(void) {
buffer *b;
-
+
b = malloc(sizeof(*b));
assert(b);
-
+
b->ptr = NULL;
b->size = 0;
b->used = 0;
-
+
return b;
}
@@ -36,8 +36,8 @@ buffer *buffer_init_buffer(buffer *src) {
}
/**
- * free the buffer
- *
+ * free the buffer
+ *
*/
void buffer_free(buffer *b) {
@@ -49,41 +49,39 @@ void buffer_free(buffer *b) {
void buffer_reset(buffer *b) {
if (!b) return;
-
+
/* limit don't reuse buffer larger than ... bytes */
if (b->size > BUFFER_MAX_REUSE_SIZE) {
free(b->ptr);
b->ptr = NULL;
b->size = 0;
- } else if (b->size) {
- b->ptr[0] = '\0';
}
-
+
b->used = 0;
}
/**
- *
- * allocate (if neccessary) enough space for 'size' bytes and
+ *
+ * allocate (if neccessary) enough space for 'size' bytes and
* set the 'used' counter to 0
- *
+ *
*/
#define BUFFER_PIECE_SIZE 64
int buffer_prepare_copy(buffer *b, size_t size) {
if (!b) return -1;
-
- if ((0 == b->size) ||
+
+ if ((0 == b->size) ||
(size > b->size)) {
if (b->size) free(b->ptr);
-
+
b->size = size;
-
+
/* always allocate a multiply of BUFFER_PIECE_SIZE */
b->size += BUFFER_PIECE_SIZE - (b->size % BUFFER_PIECE_SIZE);
-
+
b->ptr = malloc(b->size);
assert(b->ptr);
}
@@ -92,30 +90,30 @@ int buffer_prepare_copy(buffer *b, size_t size) {
}
/**
- *
+ *
* increase the internal buffer (if neccessary) to append another 'size' byte
* ->used isn't changed
- *
+ *
*/
int buffer_prepare_append(buffer *b, size_t size) {
if (!b) return -1;
-
+
if (0 == b->size) {
b->size = size;
-
+
/* always allocate a multiply of BUFFER_PIECE_SIZE */
b->size += BUFFER_PIECE_SIZE - (b->size % BUFFER_PIECE_SIZE);
-
+
b->ptr = malloc(b->size);
b->used = 0;
assert(b->ptr);
} else if (b->used + size > b->size) {
b->size += size;
-
+
/* always allocate a multiply of BUFFER_PIECE_SIZE */
b->size += BUFFER_PIECE_SIZE - (b->size % BUFFER_PIECE_SIZE);
-
+
b->ptr = realloc(b->ptr, b->size);
assert(b->ptr);
}
@@ -124,7 +122,7 @@ int buffer_prepare_append(buffer *b, size_t size) {
int buffer_copy_string(buffer *b, const char *s) {
size_t s_len;
-
+
if (!s || !b) return -1;
s_len = strlen(s) + 1;
@@ -138,28 +136,28 @@ int buffer_copy_string(buffer *b, const char *s) {
int buffer_copy_string_len(buffer *b, const char *s, size_t s_len) {
if (!s || !b) return -1;
-#if 0
- /* removed optimization as we have to keep the empty string
+#if 0
+ /* removed optimization as we have to keep the empty string
* in some cases for the config handling
- *
+ *
* url.access-deny = ( "" )
*/
if (s_len == 0) return 0;
-#endif
+#endif
buffer_prepare_copy(b, s_len + 1);
-
+
memcpy(b->ptr, s, s_len);
b->ptr[s_len] = '\0';
b->used = s_len + 1;
-
+
return 0;
}
int buffer_copy_string_buffer(buffer *b, const buffer *src) {
if (!src) return -1;
-
+
if (src->used == 0) {
- buffer_reset(b);
+ b->used = 0;
return 0;
}
return buffer_copy_string_len(b, src->ptr, src->used - 1);
@@ -187,7 +185,6 @@ int buffer_append_string_rfill(buffer *b, const char *s, size_t maxlen) {
if (!s || !b) return -1;
s_len = strlen(s);
- if (s_len > maxlen) s_len = maxlen;
buffer_prepare_append(b, maxlen + 1);
if (b->used == 0)
b->used++;
@@ -204,10 +201,10 @@ int buffer_append_string_rfill(buffer *b, const char *s, size_t maxlen) {
/**
* append a string to the end of the buffer
- *
- * the resulting buffer is terminated with a '\0'
+ *
+ * the resulting buffer is terminated with a '\0'
* s is treated as a un-terminated string (a \0 is handled a normal character)
- *
+ *
* @param b a buffer
* @param s the string
* @param s_len size of the string (without the terminating \0)
@@ -231,7 +228,7 @@ int buffer_append_string_len(buffer *b, const char *s, size_t s_len) {
int buffer_append_string_buffer(buffer *b, const buffer *src) {
if (!src) return -1;
if (src->used == 0) return 0;
-
+
return buffer_append_string_len(b, src->ptr, src->used - 1);
}
@@ -248,9 +245,9 @@ int buffer_append_memory(buffer *b, const char *s, size_t s_len) {
int buffer_copy_memory(buffer *b, const char *s, size_t s_len) {
if (!s || !b) return -1;
-
+
b->used = 0;
-
+
return buffer_append_memory(b, s, s_len);
}
@@ -284,7 +281,7 @@ int buffer_append_long_hex(buffer *b, unsigned long value) {
return 0;
}
-int LI_ltostr(char *buf, long val) {
+int ltostr(char *buf, long val) {
char swap;
char *end;
int len = 1;
@@ -323,7 +320,7 @@ int buffer_append_long(buffer *b, long val) {
if (b->used == 0)
b->used++;
- b->used += LI_ltostr(b->ptr + (b->used - 1), val);
+ b->used += ltostr(b->ptr + (b->used - 1), val);
return 0;
}
@@ -405,46 +402,46 @@ char hex2int(unsigned char hex) {
/**
- * init the buffer
- *
+ * init the buffer
+ *
*/
buffer_array* buffer_array_init(void) {
buffer_array *b;
-
+
b = malloc(sizeof(*b));
-
+
assert(b);
b->ptr = NULL;
b->size = 0;
b->used = 0;
-
+
return b;
}
void buffer_array_reset(buffer_array *b) {
size_t i;
-
+
if (!b) return;
-
+
/* if they are too large, reduce them */
for (i = 0; i < b->used; i++) {
buffer_reset(b->ptr[i]);
}
-
+
b->used = 0;
}
/**
- * free the buffer_array
- *
+ * free the buffer_array
+ *
*/
void buffer_array_free(buffer_array *b) {
size_t i;
if (!b) return;
-
+
for (i = 0; i < b->size; i++) {
if (b->ptr[i]) buffer_free(b->ptr[i]);
}
@@ -454,7 +451,7 @@ void buffer_array_free(buffer_array *b) {
buffer *buffer_array_append_get_buffer(buffer_array *b) {
size_t i;
-
+
if (b->size == 0) {
b->size = 16;
b->ptr = malloc(sizeof(*b->ptr) * b->size);
@@ -470,13 +467,13 @@ buffer *buffer_array_append_get_buffer(buffer_array *b) {
b->ptr[i] = NULL;
}
}
-
+
if (b->ptr[b->used] == NULL) {
b->ptr[b->used] = buffer_init();
}
-
+
b->ptr[b->used]->used = 0;
-
+
return b->ptr[b->used++];
}
@@ -485,34 +482,33 @@ char * buffer_search_string_len(buffer *b, const char *needle, size_t len) {
size_t i;
if (len == 0) return NULL;
if (needle == NULL) return NULL;
-
+
if (b->used < len) return NULL;
-
+
for(i = 0; i < b->used - len; i++) {
if (0 == memcmp(b->ptr + i, needle, len)) {
return b->ptr + i;
}
}
-
+
return NULL;
}
buffer *buffer_init_string(const char *str) {
buffer *b = buffer_init();
-
+
buffer_copy_string(b, str);
-
+
return b;
}
int buffer_is_empty(buffer *b) {
- if (!b) return 1;
return (b->used == 0);
}
/**
* check if two buffer contain the same data
- *
+ *
* HISTORY: this function was pretty much optimized, but didn't handled
* alignment properly.
*/
@@ -526,121 +522,115 @@ int buffer_is_equal(buffer *a, buffer *b) {
int buffer_is_equal_string(buffer *a, const char *s, size_t b_len) {
buffer b;
-
+
b.ptr = (char *)s;
b.used = b_len + 1;
-
+
return buffer_is_equal(a, &b);
}
/* simple-assumption:
- *
+ *
* most parts are equal and doing a case conversion needs time
- *
+ *
*/
int buffer_caseless_compare(const char *a, size_t a_len, const char *b, size_t b_len) {
size_t ndx = 0, max_ndx;
size_t *al, *bl;
size_t mask = sizeof(*al) - 1;
-
+
al = (size_t *)a;
bl = (size_t *)b;
-
+
/* is the alignment correct ? */
if ( ((size_t)al & mask) == 0 &&
((size_t)bl & mask) == 0 ) {
-
+
max_ndx = ((a_len < b_len) ? a_len : b_len) & ~mask;
-
+
for (; ndx < max_ndx; ndx += sizeof(*al)) {
if (*al != *bl) break;
al++; bl++;
-
+
}
-
+
}
-
+
a = (char *)al;
b = (char *)bl;
-
+
max_ndx = ((a_len < b_len) ? a_len : b_len);
-
+
for (; ndx < max_ndx; ndx++) {
- int a1 = *a++, b1 = *b++;
-
+ char a1 = *a++, b1 = *b++;
+
if (a1 != b1) {
- /* always lowercase for transitive results */
- if (a1 >= 'A' && a1 <= 'Z') a1 |= 32;
- if (b1 >= 'A' && b1 <= 'Z') b1 |= 32;
-
+ if ((a1 >= 'A' && a1 <= 'Z') && (b1 >= 'a' && b1 <= 'z'))
+ a1 |= 32;
+ else if ((a1 >= 'a' && a1 <= 'z') && (b1 >= 'A' && b1 <= 'Z'))
+ b1 |= 32;
if ((a1 - b1) != 0) return (a1 - b1);
+
}
}
-
- /* all chars are the same, and the length match too
- *
- * they are the same */
- if (a_len == b_len) return 0;
-
- /* if a is shorter then b, then b is larger */
- return (a_len - b_len);
+
+ return 0;
}
/**
* check if the rightmost bytes of the string are equal.
- *
- *
+ *
+ *
*/
int buffer_is_equal_right_len(buffer *b1, buffer *b2, size_t len) {
/* no, len -> equal */
if (len == 0) return 1;
-
+
/* len > 0, but empty buffers -> not equal */
if (b1->used == 0 || b2->used == 0) return 0;
-
+
/* buffers too small -> not equal */
if (b1->used - 1 < len || b1->used - 1 < len) return 0;
-
- if (0 == strncmp(b1->ptr + b1->used - 1 - len,
+
+ if (0 == strncmp(b1->ptr + b1->used - 1 - len,
b2->ptr + b2->used - 1 - len, len)) {
return 1;
}
-
+
return 0;
}
int buffer_copy_string_hex(buffer *b, const char *in, size_t in_len) {
size_t i;
-
+
/* BO protection */
if (in_len * 2 < in_len) return -1;
-
+
buffer_prepare_copy(b, in_len * 2 + 1);
-
+
for (i = 0; i < in_len; i++) {
b->ptr[b->used++] = hex_chars[(in[i] >> 4) & 0x0F];
b->ptr[b->used++] = hex_chars[in[i] & 0x0F];
}
b->ptr[b->used++] = '\0';
-
+
return 0;
}
-/* everything except: ! ( ) * - . 0-9 A-Z _ a-z */
const char encoded_chars_rel_uri_part[] = {
/*
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, /* 10 - 1F */
1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, /* 20 - 2F space " # $ % & ' + , / */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, /* 30 - 3F : ; < = > ? */
- 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 - 4F @ */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, /* 50 - 5F [ \ ] ^ */
- 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 6F ` */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, /* 70 - 7F { | } ~ DEL */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, /* 30 - 3F : ; = ? @ < > */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 - 4F */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 50 - 5F */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 6F */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, /* 70 - 7F 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 */
@@ -651,19 +641,18 @@ const char encoded_chars_rel_uri_part[] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* F0 - FF */
};
-/* everything except: ! ( ) * - . / 0-9 A-Z _ a-z */
const char encoded_chars_rel_uri[] = {
/*
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, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, /* 20 - 2F space " # $ % & ' + , */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, /* 30 - 3F : ; < = > ? */
- 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 - 4F @ */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, /* 50 - 5F [ \ ] ^ */
- 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 6F ` */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, /* 70 - 7F { | } ~ DEL */
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 - 1F */
+ 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, /* 20 - 2F space " # $ % & ' + , / */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, /* 30 - 3F : ; = ? @ < > */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 - 4F */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 50 - 5F */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 6F */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, /* 70 - 7F 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 */
@@ -679,7 +668,7 @@ const char encoded_chars_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 */
+ 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 - 4F */
@@ -696,39 +685,17 @@ const char encoded_chars_html[] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* F0 - FF */
};
-const char encoded_chars_minimal_xml[] = {
- /*
- 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 - 4F */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 50 - 5F */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 6F */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, /* 70 - 7F DEL */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 - 8F */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 90 - 9F */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* A0 - AF */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0 - BF */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* C0 - CF */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* D0 - DF */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* E0 - EF */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* F0 - FF */
-};
-
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, /* 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 */
@@ -740,41 +707,18 @@ const char encoded_chars_hex[] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* F0 - FF */
};
-const char encoded_chars_http_header[] = {
- /*
- 0 1 2 3 4 5 6 7 8 9 A B C D E F
- */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, /* 00 - 0F */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 - 1F */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 20 - 2F */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 30 - 3F */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 - 4F */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 50 - 5F */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 6F */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 70 - 7F */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 - 8F */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 90 - 9F */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* A0 - AF */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0 - BF */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* C0 - CF */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* D0 - DF */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* E0 - EF */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* F0 - FF */
-};
-
-
int buffer_append_string_encoded(buffer *b, const char *s, size_t s_len, buffer_encoding_t encoding) {
unsigned char *ds, *d;
size_t d_len, ndx;
const char *map = NULL;
-
+
if (!s || !b) return -1;
-
+
if (b->ptr[b->used - 1] != '\0') {
SEGFAULT();
}
-
+
if (s_len == 0) return 0;
switch(encoding) {
@@ -787,21 +731,15 @@ int buffer_append_string_encoded(buffer *b, const char *s, size_t s_len, buffer_
case ENCODING_HTML:
map = encoded_chars_html;
break;
- 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;
case ENCODING_UNSET:
break;
}
assert(map != NULL);
-
+
/* count to-be-encoded-characters */
for (ds = (unsigned char *)s, d_len = 0, ndx = 0; ndx < s_len; ds++, ndx++) {
if (map[*ds]) {
@@ -811,10 +749,8 @@ int buffer_append_string_encoded(buffer *b, const char *s, size_t s_len, buffer_
d_len += 3;
break;
case ENCODING_HTML:
- case ENCODING_MINIMAL_XML:
d_len += 6;
break;
- case ENCODING_HTTP_HEADER:
case ENCODING_HEX:
d_len += 2;
break;
@@ -825,9 +761,9 @@ int buffer_append_string_encoded(buffer *b, const char *s, size_t s_len, buffer_
d_len ++;
}
}
-
+
buffer_prepare_append(b, d_len);
-
+
for (ds = (unsigned char *)s, d = (unsigned char *)b->ptr + b->used - 1, d_len = 0, ndx = 0; ndx < s_len; ds++, ndx++) {
if (map[*ds]) {
switch(encoding) {
@@ -838,7 +774,6 @@ int buffer_append_string_encoded(buffer *b, const char *s, size_t s_len, buffer_
d[d_len++] = hex_chars[(*ds) & 0x0F];
break;
case ENCODING_HTML:
- case ENCODING_MINIMAL_XML:
d[d_len++] = '&';
d[d_len++] = '#';
d[d_len++] = 'x';
@@ -850,10 +785,6 @@ int buffer_append_string_encoded(buffer *b, const char *s, size_t s_len, buffer_
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';
- break;
case ENCODING_UNSET:
break;
}
@@ -862,9 +793,9 @@ int buffer_append_string_encoded(buffer *b, const char *s, size_t s_len, buffer_
}
}
- /* terminate buffer and calculate new length */
+ /* terminate buffer and calculate new length */
b->ptr[b->used + d_len - 1] = '\0';
-
+
b->used += d_len;
return 0;
@@ -896,10 +827,10 @@ static int buffer_urldecode_internal(buffer *url, int is_query) {
low = hex2int(*(src + 2));
if (low != 0xFF) {
high = (high << 4) | low;
-
- /* map control-characters out */
+
+ /* map control-characters out */
if (high < 32 || high == 127) high = '_';
-
+
*dst = high;
src += 2;
}
@@ -956,18 +887,6 @@ int buffer_path_simplify(buffer *dest, buffer *src)
start = dest->ptr;
out = dest->ptr;
slash = dest->ptr;
-
-
-#if defined(__WIN32) || defined(__CYGWIN__)
- /* cygwin is treating \ and / the same, so we have to that too
- */
-
- for (walk = src->ptr; *walk; walk++) {
- if (*walk == '\\') *walk = '/';
- }
- walk = src->ptr;
-#endif
-
while (*walk == ' ') {
walk++;
}
@@ -1033,7 +952,7 @@ int light_isdigit(int c) {
int light_isxdigit(int c) {
if (light_isdigit(c)) return 1;
-
+
c |= 32;
return (c >= 'a' && c <= 'f');
}
@@ -1049,29 +968,29 @@ int light_isalnum(int c) {
int buffer_to_lower(buffer *b) {
char *c;
-
+
if (b->used == 0) return 0;
-
+
for (c = b->ptr; *c; c++) {
if (*c >= 'A' && *c <= 'Z') {
*c |= 32;
}
}
-
+
return 0;
}
int buffer_to_upper(buffer *b) {
char *c;
-
+
if (b->used == 0) return 0;
-
+
for (c = b->ptr; *c; c++) {
if (*c >= 'a' && *c <= 'z') {
*c &= ~32;
}
}
-
+
return 0;
}