2012-08-24 17:22:53 +00:00
|
|
|
/*
|
|
|
|
* include/common/chunk.h
|
|
|
|
* Chunk management definitions, macros and inline functions.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2000-2012 Willy Tarreau - w@1wt.eu
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation, version 2.1
|
|
|
|
* exclusively.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _TYPES_CHUNK_H
|
|
|
|
#define _TYPES_CHUNK_H
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2018-07-13 09:56:34 +00:00
|
|
|
#include <common/buf.h>
|
2012-08-24 17:22:53 +00:00
|
|
|
#include <common/config.h>
|
2019-10-29 12:02:15 +00:00
|
|
|
#include <common/ist.h>
|
2017-02-08 10:06:11 +00:00
|
|
|
#include <common/memory.h>
|
2012-08-24 17:22:53 +00:00
|
|
|
|
|
|
|
|
2019-03-29 17:13:36 +00:00
|
|
|
extern struct pool_head *pool_head_trash;
|
2017-02-08 10:06:11 +00:00
|
|
|
|
2012-08-24 17:22:53 +00:00
|
|
|
/* function prototypes */
|
|
|
|
|
2018-07-13 09:56:34 +00:00
|
|
|
int chunk_printf(struct buffer *chk, const char *fmt, ...)
|
2012-08-24 17:22:53 +00:00
|
|
|
__attribute__ ((format(printf, 2, 3)));
|
|
|
|
|
2018-07-13 09:56:34 +00:00
|
|
|
int chunk_appendf(struct buffer *chk, const char *fmt, ...)
|
2012-10-29 15:14:26 +00:00
|
|
|
__attribute__ ((format(printf, 2, 3)));
|
|
|
|
|
2018-07-13 09:56:34 +00:00
|
|
|
int chunk_htmlencode(struct buffer *dst, struct buffer *src);
|
|
|
|
int chunk_asciiencode(struct buffer *dst, struct buffer *src, char qc);
|
|
|
|
int chunk_strcmp(const struct buffer *chk, const char *str);
|
|
|
|
int chunk_strcasecmp(const struct buffer *chk, const char *str);
|
|
|
|
struct buffer *get_trash_chunk(void);
|
|
|
|
struct buffer *alloc_trash_chunk(void);
|
2017-10-27 11:53:47 +00:00
|
|
|
int init_trash_buffers(int first);
|
2017-02-08 10:06:11 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* free a trash chunk allocated by alloc_trash_chunk(). NOP on NULL.
|
|
|
|
*/
|
2018-07-13 09:56:34 +00:00
|
|
|
static inline void free_trash_chunk(struct buffer *chunk)
|
2017-02-08 10:06:11 +00:00
|
|
|
{
|
2017-11-24 16:34:44 +00:00
|
|
|
pool_free(pool_head_trash, chunk);
|
2017-02-08 10:06:11 +00:00
|
|
|
}
|
|
|
|
|
2012-08-24 17:22:53 +00:00
|
|
|
|
2018-07-13 09:56:34 +00:00
|
|
|
static inline void chunk_reset(struct buffer *chk)
|
2012-10-29 12:23:11 +00:00
|
|
|
{
|
2018-07-13 08:54:26 +00:00
|
|
|
chk->data = 0;
|
2012-10-29 12:23:11 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 09:56:34 +00:00
|
|
|
static inline void chunk_init(struct buffer *chk, char *str, size_t size)
|
2012-08-24 17:22:53 +00:00
|
|
|
{
|
2018-07-13 09:56:34 +00:00
|
|
|
chk->area = str;
|
|
|
|
chk->head = 0;
|
|
|
|
chk->data = 0;
|
2012-08-24 17:22:53 +00:00
|
|
|
chk->size = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* report 0 in case of error, 1 if OK. */
|
2018-07-13 09:56:34 +00:00
|
|
|
static inline int chunk_initlen(struct buffer *chk, char *str, size_t size,
|
|
|
|
int len)
|
2012-08-24 17:22:53 +00:00
|
|
|
{
|
|
|
|
|
2016-02-25 15:15:19 +00:00
|
|
|
if (len < 0 || (size && len > size))
|
2012-08-24 17:22:53 +00:00
|
|
|
return 0;
|
|
|
|
|
2018-07-13 09:56:34 +00:00
|
|
|
chk->area = str;
|
|
|
|
chk->head = 0;
|
|
|
|
chk->data = len;
|
2012-08-24 17:22:53 +00:00
|
|
|
chk->size = size;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-01-06 19:45:03 +00:00
|
|
|
/* this is only for temporary manipulation, the chunk is read-only */
|
2018-07-13 09:56:34 +00:00
|
|
|
static inline void chunk_initstr(struct buffer *chk, const char *str)
|
2012-08-24 17:22:53 +00:00
|
|
|
{
|
2018-07-13 08:54:26 +00:00
|
|
|
chk->area = (char *)str;
|
2018-07-13 09:56:34 +00:00
|
|
|
chk->head = 0;
|
2018-07-13 08:54:26 +00:00
|
|
|
chk->data = strlen(str);
|
2012-08-24 17:22:53 +00:00
|
|
|
chk->size = 0; /* mark it read-only */
|
|
|
|
}
|
|
|
|
|
2018-10-08 05:34:25 +00:00
|
|
|
/* copies chunk <src> into <chk>. Returns 0 in case of failure. */
|
|
|
|
static inline int chunk_cpy(struct buffer *chk, const struct buffer *src)
|
|
|
|
{
|
2019-10-14 09:29:48 +00:00
|
|
|
if (unlikely(src->data > chk->size))
|
2018-10-08 05:34:25 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
chk->data = src->data;
|
|
|
|
memcpy(chk->area, src->area, src->data);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* appends chunk <src> after <chk>. Returns 0 in case of failure. */
|
|
|
|
static inline int chunk_cat(struct buffer *chk, const struct buffer *src)
|
|
|
|
{
|
2019-10-14 09:29:48 +00:00
|
|
|
if (unlikely(chk->data + src->data > chk->size))
|
2018-10-08 05:34:25 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
memcpy(chk->area + chk->data, src->area, src->data);
|
|
|
|
chk->data += src->data;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-10-29 12:02:15 +00:00
|
|
|
/* appends ist <src> after <chk>. Returns 0 in case of failure. */
|
|
|
|
static inline int chunk_istcat(struct buffer *chk, const struct ist src)
|
|
|
|
{
|
|
|
|
if (unlikely(chk->data + src.len > chk->size))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
memcpy(chk->area + chk->data, src.ptr, src.len);
|
|
|
|
chk->data += src.len;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-07-27 11:35:34 +00:00
|
|
|
/* copies memory area <src> into <chk> for <len> bytes. Returns 0 in
|
|
|
|
* case of failure. No trailing zero is added.
|
|
|
|
*/
|
2018-07-13 09:56:34 +00:00
|
|
|
static inline int chunk_memcpy(struct buffer *chk, const char *src,
|
|
|
|
size_t len)
|
2017-07-27 11:35:34 +00:00
|
|
|
{
|
2019-10-14 09:29:48 +00:00
|
|
|
if (unlikely(len > chk->size))
|
2017-07-27 11:35:34 +00:00
|
|
|
return 0;
|
|
|
|
|
2018-07-13 08:54:26 +00:00
|
|
|
chk->data = len;
|
|
|
|
memcpy(chk->area, src, len);
|
2017-07-27 11:35:34 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* appends memory area <src> after <chk> for <len> bytes. Returns 0 in
|
|
|
|
* case of failure. No trailing zero is added.
|
|
|
|
*/
|
2018-07-13 09:56:34 +00:00
|
|
|
static inline int chunk_memcat(struct buffer *chk, const char *src,
|
|
|
|
size_t len)
|
2017-07-27 11:35:34 +00:00
|
|
|
{
|
2019-10-14 09:29:48 +00:00
|
|
|
if (unlikely(chk->data + len > chk->size))
|
2017-07-27 11:35:34 +00:00
|
|
|
return 0;
|
|
|
|
|
2018-07-13 08:54:26 +00:00
|
|
|
memcpy(chk->area + chk->data, src, len);
|
|
|
|
chk->data += len;
|
2017-07-27 11:35:34 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-01-04 19:21:33 +00:00
|
|
|
/* copies str into <chk> followed by a trailing zero. Returns 0 in
|
|
|
|
* case of failure.
|
|
|
|
*/
|
2018-07-13 09:56:34 +00:00
|
|
|
static inline int chunk_strcpy(struct buffer *chk, const char *str)
|
2012-08-24 17:22:53 +00:00
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = strlen(str);
|
|
|
|
|
2016-01-04 19:21:33 +00:00
|
|
|
if (unlikely(len >= chk->size))
|
2012-08-24 17:22:53 +00:00
|
|
|
return 0;
|
|
|
|
|
2018-07-13 08:54:26 +00:00
|
|
|
chk->data = len;
|
|
|
|
memcpy(chk->area, str, len + 1);
|
2012-08-24 17:22:53 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-02-14 10:31:41 +00:00
|
|
|
/* copies at most <max> chars from str into <chk> followed by a trailing zero.
|
|
|
|
* Returns 0 in case of failure.
|
|
|
|
*/
|
|
|
|
static inline int chunk_strncpy(struct buffer *chk, const char *str, size_t max)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = strlen(str);
|
|
|
|
if (len > max)
|
|
|
|
len = max;
|
|
|
|
|
|
|
|
if (unlikely(len >= chk->size))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
memcpy(chk->area, str, len);
|
|
|
|
chk->area[len] = 0;
|
|
|
|
chk->data = len;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-01-04 19:13:55 +00:00
|
|
|
/* appends str after <chk> followed by a trailing zero. Returns 0 in
|
|
|
|
* case of failure.
|
|
|
|
*/
|
2018-07-13 09:56:34 +00:00
|
|
|
static inline int chunk_strcat(struct buffer *chk, const char *str)
|
2016-01-04 19:13:55 +00:00
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = strlen(str);
|
|
|
|
|
2018-08-22 02:59:48 +00:00
|
|
|
if (unlikely(chk->data + len >= chk->size))
|
2016-01-04 19:13:55 +00:00
|
|
|
return 0;
|
|
|
|
|
2018-07-13 08:54:26 +00:00
|
|
|
memcpy(chk->area + chk->data, str, len + 1);
|
|
|
|
chk->data += len;
|
2016-01-04 19:13:55 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-03-26 13:12:50 +00:00
|
|
|
/* appends <nb> characters from str after <chk>.
|
|
|
|
* Returns 0 in case of failure.
|
|
|
|
*/
|
2018-07-13 09:56:34 +00:00
|
|
|
static inline int chunk_strncat(struct buffer *chk, const char *str, int nb)
|
2016-03-26 13:12:50 +00:00
|
|
|
{
|
2018-08-22 02:59:48 +00:00
|
|
|
if (unlikely(chk->data + nb >= chk->size))
|
2016-03-26 13:12:50 +00:00
|
|
|
return 0;
|
|
|
|
|
2018-07-13 08:54:26 +00:00
|
|
|
memcpy(chk->area + chk->data, str, nb);
|
|
|
|
chk->data += nb;
|
2016-03-26 13:12:50 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-01-04 19:13:55 +00:00
|
|
|
/* Adds a trailing zero to the current chunk and returns the pointer to the
|
|
|
|
* following part. The purpose is to be able to use a chunk as a series of
|
2018-11-25 18:49:51 +00:00
|
|
|
* short independent strings with chunk_* functions, which do not need to be
|
2016-01-04 19:13:55 +00:00
|
|
|
* released. Returns NULL if no space is available to ensure that the new
|
|
|
|
* string will have its own trailing zero. For example :
|
|
|
|
* chunk_init(&trash);
|
|
|
|
* pid = chunk_newstr(&trash);
|
|
|
|
* chunk_appendf(&trash, "%d", getpid()));
|
|
|
|
* name = chunk_newstr(&trash);
|
|
|
|
* chunk_appendf(&trash, "%s", gethosname());
|
|
|
|
* printf("hostname=<%s>, pid=<%d>\n", name, pid);
|
|
|
|
*/
|
2018-07-13 09:56:34 +00:00
|
|
|
static inline char *chunk_newstr(struct buffer *chk)
|
2016-01-04 19:13:55 +00:00
|
|
|
{
|
2018-08-22 02:59:48 +00:00
|
|
|
if (chk->data + 1 >= chk->size)
|
2016-01-04 19:13:55 +00:00
|
|
|
return NULL;
|
|
|
|
|
2018-07-13 08:54:26 +00:00
|
|
|
chk->area[chk->data++] = 0;
|
|
|
|
return chk->area + chk->data;
|
2016-01-04 19:13:55 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 09:56:34 +00:00
|
|
|
static inline void chunk_drop(struct buffer *chk)
|
2012-08-24 17:22:53 +00:00
|
|
|
{
|
2018-07-13 08:54:26 +00:00
|
|
|
chk->area = NULL;
|
|
|
|
chk->data = -1;
|
2012-08-24 17:22:53 +00:00
|
|
|
chk->size = 0;
|
|
|
|
}
|
|
|
|
|
2018-07-13 09:56:34 +00:00
|
|
|
static inline void chunk_destroy(struct buffer *chk)
|
2012-08-24 17:22:53 +00:00
|
|
|
{
|
|
|
|
if (!chk->size)
|
|
|
|
return;
|
|
|
|
|
2018-07-13 08:54:26 +00:00
|
|
|
free(chk->area);
|
2012-10-29 12:23:11 +00:00
|
|
|
chunk_drop(chk);
|
2012-08-24 17:22:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* frees the destination chunk if already allocated, allocates a new string,
|
2016-01-04 19:36:59 +00:00
|
|
|
* and copies the source into it. The new chunk will have extra room for a
|
|
|
|
* trailing zero unless the source chunk was actually full. The pointer to
|
|
|
|
* the destination string is returned, or NULL if the allocation fails or if
|
|
|
|
* any pointer is NULL.
|
2012-08-24 17:22:53 +00:00
|
|
|
*/
|
2018-07-13 09:56:34 +00:00
|
|
|
static inline char *chunk_dup(struct buffer *dst, const struct buffer *src)
|
2012-08-24 17:22:53 +00:00
|
|
|
{
|
2018-08-22 02:59:48 +00:00
|
|
|
if (!dst || !src || !src->area)
|
2012-08-24 17:22:53 +00:00
|
|
|
return NULL;
|
2016-01-04 19:36:59 +00:00
|
|
|
|
|
|
|
if (dst->size)
|
2018-07-13 08:54:26 +00:00
|
|
|
free(dst->area);
|
2018-07-13 09:56:34 +00:00
|
|
|
dst->head = src->head;
|
2018-07-13 08:54:26 +00:00
|
|
|
dst->data = src->data;
|
|
|
|
dst->size = src->data;
|
2016-01-04 19:36:59 +00:00
|
|
|
if (dst->size < src->size || !src->size)
|
|
|
|
dst->size++;
|
|
|
|
|
2018-07-13 08:54:26 +00:00
|
|
|
dst->area = (char *)malloc(dst->size);
|
|
|
|
if (!dst->area) {
|
2018-07-13 09:56:34 +00:00
|
|
|
dst->head = 0;
|
2018-07-13 08:54:26 +00:00
|
|
|
dst->data = 0;
|
2016-03-23 17:50:57 +00:00
|
|
|
dst->size = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-07-13 08:54:26 +00:00
|
|
|
memcpy(dst->area, src->area, dst->data);
|
|
|
|
if (dst->data < dst->size)
|
|
|
|
dst->area[dst->data] = 0;
|
2016-01-04 19:36:59 +00:00
|
|
|
|
2018-07-13 08:54:26 +00:00
|
|
|
return dst->area;
|
2012-08-24 17:22:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* _TYPES_CHUNK_H */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* c-indent-level: 8
|
|
|
|
* c-basic-offset: 8
|
|
|
|
* End:
|
|
|
|
*/
|