summaryrefslogtreecommitdiff
path: root/common/buffer.c
blob: f2e2cb886cf6f6c23263aaf48fa0e2af40ac960b (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * Copyright (C) 2007, 2012 Stefan Walter
 * Copyright (C) 2013 Red Hat Inc.
 *
 * 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.
 *
 * Author: Stef Walter <stef@thewalter.net>
 */

#include "config.h"

#include "buffer.h"
#include "debug.h"

#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

static bool
buffer_realloc (p11_buffer *buffer,
                size_t size)
{
	void *data;

	/* Memory owned elsewhere can't be reallocated */
	return_val_if_fail (buffer->frealloc != NULL, false);

	/* Reallocate built in buffer using allocator */
	data = (buffer->frealloc) (buffer->data, size);
	if (!data && size > 0) {
		p11_buffer_fail (buffer);
		return_val_if_reached (false);
	}

	buffer->data = data;
	buffer->size = size;
	return true;
}

bool
p11_buffer_init (p11_buffer *buffer,
                 size_t reserve)
{
	p11_buffer_init_full (buffer, NULL, 0, 0, realloc, free);
	return buffer_realloc (buffer, reserve);
}

bool
p11_buffer_init_null (p11_buffer *buffer,
                      size_t reserve)
{
	p11_buffer_init_full (buffer, NULL, 0, P11_BUFFER_NULL, realloc, free);
	return buffer_realloc (buffer, reserve);
}

void
p11_buffer_init_full (p11_buffer *buffer,
                      void *data,
                      size_t len,
                      int flags,
                      void * (* frealloc) (void *, size_t),
                      void (* ffree) (void *))
{
	memset (buffer, 0, sizeof (*buffer));

	buffer->data = data;
	buffer->len = len;
	buffer->size = len;
	buffer->flags = flags;
	buffer->frealloc = frealloc;
	buffer->ffree = ffree;

	return_if_fail (!(flags & P11_BUFFER_FAILED));
}

void
p11_buffer_uninit (p11_buffer *buffer)
{
	return_if_fail (buffer != NULL);

	if (buffer->ffree && buffer->data)
		(buffer->ffree) (buffer->data);
	memset (buffer, 0, sizeof (*buffer));
}

void *
p11_buffer_steal (p11_buffer *buffer,
                  size_t *length)
{
	void *data;

	return_val_if_fail (p11_buffer_ok (buffer), NULL);

	if (length)
		*length = buffer->len;
	data = buffer->data;

	buffer->data = NULL;
	buffer->size = 0;
	buffer->len = 0;
	return data;
}

bool
p11_buffer_reset (p11_buffer *buffer,
                  size_t reserve)
{
	buffer->flags &= ~P11_BUFFER_FAILED;
	buffer->len = 0;

	if (reserve < buffer->size)
		return true;
	return buffer_realloc (buffer, reserve);
}

void *
p11_buffer_append (p11_buffer *buffer,
                   size_t length)
{
	unsigned char *data;
	size_t terminator;
	size_t newlen;
	size_t reserve;

	return_val_if_fail (p11_buffer_ok (buffer), NULL);

	terminator = (buffer->flags & P11_BUFFER_NULL) ? 1 : 0;

	/* Check for unlikely and unrecoverable integer overflow */
	return_val_if_fail (SIZE_MAX - (terminator + length) > buffer->len, NULL);

	reserve = terminator + length + buffer->len;

	if (reserve > buffer->size) {

		/* Calculate a new length, minimize number of buffer allocations */
		return_val_if_fail (buffer->size < SIZE_MAX / 2, NULL);
		newlen = buffer->size * 2;
		if (!newlen)
			newlen = 16;
		if (reserve > newlen)
			newlen = reserve;

		if (!buffer_realloc (buffer, newlen))
			return_val_if_reached (NULL);
	}

	data = buffer->data;
	data += buffer->len;
	buffer->len += length;
	if (terminator)
		data[length] = '\0';
	return data;
}

void
p11_buffer_add (p11_buffer *buffer,
                const void *data,
                ssize_t length)
{
	void *at;

	if (length < 0)
		length = strlen (data);

	at = p11_buffer_append (buffer, length);
	return_if_fail (at != NULL);
	memcpy (at, data, length);
}