summaryrefslogtreecommitdiff
path: root/src/utils/buffer.c
blob: 21c47fc165e39c739d2d63b381803a69f3da8230 (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
/*
 * This file is part of LibParserUtils.
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 * Copyright 2008 John-Mark Bell <jmb@netsurf-browser.org>
 */

#include <string.h>

#include <parserutils/utils/buffer.h>

#define DEFAULT_SIZE (4096)

/**
 * Create a memory buffer
 *
 * \param alloc  Memory (de)allocation function
 * \param pw     Pointer to client-specific private data
 * \return Pointer to memory buffer, or NULL on memory exhaustion
 */
parserutils_buffer *parserutils_buffer_create(parserutils_alloc alloc, void *pw)
{
	parserutils_buffer *buffer = 
			alloc(NULL, sizeof(parserutils_buffer), pw);

	if (buffer == NULL)
		return NULL;

	buffer->data = alloc(NULL, DEFAULT_SIZE, pw);
	if (buffer->data == NULL) {
		alloc(buffer, 0, pw);
		return NULL;
	}

	buffer->length = 0;
	buffer->allocated = DEFAULT_SIZE;

	buffer->alloc = alloc;
	buffer->pw = pw;

	return buffer;
}

/**
 * Destroy a memory buffer
 *
 * \param buffer  The buffer to destroy
 */
void parserutils_buffer_destroy(parserutils_buffer *buffer)
{
	if (buffer == NULL)
		return;

	buffer->alloc(buffer->data, 0, buffer->pw);
	buffer->alloc(buffer, 0, buffer->pw);
}

/**
 * Append data to a memory buffer
 *
 * \param buffer  The buffer to append to
 * \param data    The data to append
 * \param len     The length, in bytes, of the data to append
 * \return PARSERUTILS_OK on success, appropriate error otherwise.
 */
parserutils_error parserutils_buffer_append(parserutils_buffer *buffer, 
		const uint8_t *data, size_t len)
{
	while (len >= buffer->allocated - buffer->length) {
		parserutils_error error = parserutils_buffer_grow(buffer);
		if (error != PARSERUTILS_OK)
			return error;
	}

	memcpy(buffer->data + buffer->length, data, len);

	buffer->length += len;

	return PARSERUTILS_OK;
}

/**
 * Insert data into a memory buffer
 *
 * \param buffer  The buffer to insert into
 * \param offset  The offset into the buffer to insert at
 * \param data    The data to insert
 * \param len     The length, in bytes, of the data to insert
 * \return PARSERUTILS_OK on success, appropriate error otherwise
 */
parserutils_error parserutils_buffer_insert(parserutils_buffer *buffer, 
		size_t offset, const uint8_t *data, size_t len)
{
	if (offset > buffer->length)
		return PARSERUTILS_BADPARM;

	if (offset == buffer->length)
		return parserutils_buffer_append(buffer, data, len);

	while (len >= buffer->allocated - buffer->length) {
		parserutils_error error = parserutils_buffer_grow(buffer);
		if (error != PARSERUTILS_OK)
			return error;
	}

	memmove(buffer->data + buffer->length + len, 
			buffer->data + offset, buffer->length - offset);

	memcpy(buffer->data + offset, data, len);

	buffer->length += len;

	return PARSERUTILS_OK;
}

/**
 * Discard a section of a memory buffer
 *
 * \param buffer  The buffer to discard data from
 * \param offset  The offset into the buffer of the start of the section
 * \param len     The number of bytes to discard
 * \return PARSERUTILS_OK on success, appropriate error otherwise.
 */
parserutils_error parserutils_buffer_discard(parserutils_buffer *buffer, 
		size_t offset, size_t len)
{
	if (offset >= buffer->length || offset + len > buffer->length)
		return PARSERUTILS_BADPARM;

	memmove(buffer->data + offset, buffer->data + offset + len, 
			buffer->length - len);

	buffer->length -= len;

	return PARSERUTILS_OK;
}

/**
 * Extend the amount of space allocated for a memory buffer
 *
 * \param buffer  The buffer to extend
 * \return PARSERUTILS_OK on success, appropriate error otherwise.
 */
parserutils_error parserutils_buffer_grow(parserutils_buffer *buffer)
{
	uint8_t *temp = buffer->alloc(buffer->data, 
			buffer->allocated * 2, buffer->pw);
	if (temp == NULL)
		return PARSERUTILS_NOMEM;

	buffer->data = temp;
	buffer->allocated *= 2;

	return PARSERUTILS_OK;
}