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
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* buffer.c - Generic data buffer, used by openssh, gnome-keyring
Copyright (C) 2007, 2012 Stefan Walter
Copyright (C) 2013 Red Hat Inc.
The Gnome Keyring Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The Gnome Keyring 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the Gnome Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
Author: Stef Walter <stef@thewalter.net>
*/
#include "config.h"
#include "buffer.h"
#include "debug.h"
#include <assert.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;
reserve = terminator + length + buffer->len;
if (reserve > buffer->size) {
/* Calculate a new length, minimize number of buffer allocations */
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);
}
|