MINOR: standard: add function "encode_chunk"

This function has the same behavior as encode_string(), except it
takes a "struct chunk" instead of a "char *" on input.
This commit is contained in:
Thierry FOURNIER 2014-03-17 12:01:13 +01:00 committed by Willy Tarreau
parent 7cbc915a1d
commit e059ec9393
2 changed files with 40 additions and 0 deletions

View File

@ -29,6 +29,7 @@
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <common/chunk.h>
#include <common/config.h>
#include <eb32tree.h>
@ -282,6 +283,14 @@ char *encode_string(char *start, char *stop,
const char escape, const fd_set *map,
const char *string);
/*
* Same behavior, except that it encodes chunk <chunk> instead of a string.
*/
char *encode_chunk(char *start, char *stop,
const char escape, const fd_set *map,
const struct chunk *chunk);
/* Decode an URL-encoded string in-place. The resulting string might
* be shorter. If some forbidden characters are found, the conversion is
* aborted, the string is truncated before the issue and non-zero is returned,

View File

@ -21,6 +21,7 @@
#include <netinet/in.h>
#include <arpa/inet.h>
#include <common/chunk.h>
#include <common/config.h>
#include <common/standard.h>
#include <eb32tree.h>
@ -1045,6 +1046,36 @@ char *encode_string(char *start, char *stop,
return start;
}
/*
* Same behavior as encode_string() above, except that it encodes chunk
* <chunk> instead of a string.
*/
char *encode_chunk(char *start, char *stop,
const char escape, const fd_set *map,
const struct chunk *chunk)
{
char *str = chunk->str;
char *end = chunk->str + chunk->len;
if (start < stop) {
stop--; /* reserve one byte for the final '\0' */
while (start < stop && str < end) {
if (!FD_ISSET((unsigned char)(*str), map))
*start++ = *str;
else {
if (start + 3 >= stop)
break;
*start++ = escape;
*start++ = hextab[(*str >> 4) & 15];
*start++ = hextab[*str & 15];
}
str++;
}
*start = '\0';
}
return start;
}
/* Decode an URL-encoded string in-place. The resulting string might
* be shorter. If some forbidden characters are found, the conversion is
* aborted, the string is truncated before the issue and a negative value is