summaryrefslogtreecommitdiff
path: root/common/buffer.h
blob: d9f94a4ef8f0d5f6b4807bbcd0efad2a5825bb93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * Copyright (c) 2004, Stefan Walter
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *     * Redistributions of source code must retain the above
 *       copyright notice, this list of conditions and the
 *       following disclaimer.
 *     * Redistributions in binary form must reproduce the
 *       above copyright notice, this list of conditions and
 *       the following disclaimer in the documentation and/or
 *       other materials provided with the distribution.
 *     * The names of contributors to this software may not be
 *       used to endorse or promote products derived from this
 *       software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */

#ifndef _BUFFER_H_
#define _BUFFER_H_

/* -----------------------------------------------------------------------
 * Memory Buffers
 */

struct ha_buffer_internal;

/* A buffer which owns memory */
typedef struct ha_buffer
{
    struct ha_buffer_internal* _ft;
    struct ha_buffer_internal* _dt;
    char* _pp;
    char* _rp;
}
ha_buffer_t;

/* Initializes a buffer */
void ha_bufinit(ha_buffer_t* buf);

/* Frees all memory associated with a buffer */
void ha_buffree(ha_buffer_t* buf);

/* Resets a buffer for later reuse */
void ha_bufreset(ha_buffer_t* buf);

#define ha_buflen(buf)  \
    ((buf)->_rp - (buf)->_pp)

#define ha_bufchar(buf)  \
    ((!ha_buferr(buf) && ha_buflen(buf) > 0) ? *((buf)->_pp) : '\0' )

#define ha_bufdata(buf)  \
    ((buf)->_pp)

#define ha_buferr(buf)   \
    ((buf)->_dt == NULL)

/* Buffer input functions ------------------------------------------------ */

/* Read a line from an input handle */
int ha_bufreadline(int fd, ha_buffer_t* buf);

/* Parse the current line */
char* ha_bufparseline(ha_buffer_t* buf, int trim);

/* Parse a word from the current block */
char* ha_bufparseword(ha_buffer_t* buf, const char* delims);

#define ha_bufskip(buf)  \
    ((buf)->_pp = (buf)->_rp)

#define ha_bufeat(buf)   \
    ((!ha_buferr(buf) && ha_buflen(buf) > 0) ? ++((buf)->_pp) : (buf)->_pp)

/* Buffer output functions ----------------------------------------------- */

/* Adds multiple strings together */
char* ha_bufmcat(ha_buffer_t* buf, ...);

/* Copies a string to the buffer */
char* ha_bufcpy(ha_buffer_t* buf, const char* src);

/* Copies a portion of a string to the buffer */
char* ha_bufncpy(ha_buffer_t* buf, const char* src, size_t len);

/* Opens up the end of the current block so it can be joined by more data */
#define ha_bufjoin(buf) \
    ((buf)->_rp && ((buf)->_rp != (buf)->_pp) ? (buf)->_rp-- : (buf)->_rp)

#define ha_bufcat ha_bufcpy

/* Buffer allocation functions ------------------------------------------- */

/* Memory allocation */
void* ha_bufmalloc(ha_buffer_t* buf, size_t bytes);

void* ha_bufmemdup(ha_buffer_t* buf, const void* src, size_t bytes);

/* Buffer Encoding Functions --------------------------------------------- */

/* Encode an array of bytes in base 64 */
char* ha_bufenc64(ha_buffer_t* buf, const void* src, size_t bytes);

/* Decode an array of bytes from base 64 */
void* ha_bufdec64(ha_buffer_t* buf, const char* src, size_t* bytes);

/* Encode an array of bytes in hex */
char* ha_bufenchex(ha_buffer_t* buf, const void* src, size_t bytes);

/* Decode an array of bytes in hex */
void* ha_bufdechex(ha_buffer_t* buf, const char* src, size_t* bytes);

#endif /* _BUFFER_H_ */