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:
parent
7cbc915a1d
commit
e059ec9393
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue