1 /*
2 * libdpkg - Debian packaging suite library routines
3 * varbuf.c - variable length expandable buffer handling
4 *
5 * Copyright © 1994,1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright © 2008-2015 Guillem Jover <guillem@debian.org>
7 *
8 * This is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <config.h>
23 #include <compat.h>
24
25 #include <string.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28
29 #include <dpkg/i18n.h>
30 #include <dpkg/dpkg.h>
31 #include <dpkg/dpkg-db.h>
32
33 void
varbuf_add_char(struct varbuf * v,int c)34 varbuf_add_char(struct varbuf *v, int c)
35 {
36 varbuf_grow(v, 1);
37 v->buf[v->used++]= c;
38 }
39
40 void
varbuf_dup_char(struct varbuf * v,int c,size_t n)41 varbuf_dup_char(struct varbuf *v, int c, size_t n)
42 {
43 varbuf_grow(v, n);
44 memset(v->buf + v->used, c, n);
45 v->used += n;
46 }
47
48 void
varbuf_map_char(struct varbuf * v,int c_src,int c_dst)49 varbuf_map_char(struct varbuf *v, int c_src, int c_dst)
50 {
51 size_t i;
52
53 for (i = 0; i < v->used; i++)
54 if (v->buf[i] == c_src)
55 v->buf[i] = c_dst;
56 }
57
58 int
varbuf_printf(struct varbuf * v,const char * fmt,...)59 varbuf_printf(struct varbuf *v, const char *fmt, ...)
60 {
61 int r;
62 va_list args;
63
64 va_start(args, fmt);
65 r = varbuf_vprintf(v, fmt, args);
66 va_end(args);
67
68 return r;
69 }
70
71 int
varbuf_vprintf(struct varbuf * v,const char * fmt,va_list args)72 varbuf_vprintf(struct varbuf *v, const char *fmt, va_list args)
73 {
74 va_list args_copy;
75 int needed, r;
76
77 va_copy(args_copy, args);
78 needed = vsnprintf(NULL, 0, fmt, args_copy);
79 va_end(args_copy);
80
81 if (needed < 0)
82 ohshite(_("error formatting string into varbuf variable"));
83
84 varbuf_grow(v, needed + 1);
85
86 r = vsnprintf(v->buf + v->used, needed + 1, fmt, args);
87 if (r < 0)
88 ohshite(_("error formatting string into varbuf variable"));
89
90 v->used += r;
91
92 return r;
93 }
94
95 void
varbuf_add_buf(struct varbuf * v,const void * s,size_t size)96 varbuf_add_buf(struct varbuf *v, const void *s, size_t size)
97 {
98 varbuf_grow(v, size);
99 memcpy(v->buf + v->used, s, size);
100 v->used += size;
101 }
102
103 void
varbuf_end_str(struct varbuf * v)104 varbuf_end_str(struct varbuf *v)
105 {
106 varbuf_grow(v, 1);
107 v->buf[v->used] = '\0';
108 }
109
110 const char *
varbuf_get_str(struct varbuf * v)111 varbuf_get_str(struct varbuf *v)
112 {
113 varbuf_end_str(v);
114
115 return v->buf;
116 }
117
118 struct varbuf *
varbuf_new(size_t size)119 varbuf_new(size_t size)
120 {
121 struct varbuf *v;
122
123 v = m_malloc(sizeof(*v));
124 varbuf_init(v, size);
125
126 return v;
127 }
128
129 void
varbuf_init(struct varbuf * v,size_t size)130 varbuf_init(struct varbuf *v, size_t size)
131 {
132 v->used = 0;
133 v->size = size;
134 if (size)
135 v->buf = m_malloc(size);
136 else
137 v->buf = NULL;
138 }
139
140 void
varbuf_reset(struct varbuf * v)141 varbuf_reset(struct varbuf *v)
142 {
143 v->used= 0;
144 }
145
146 void
varbuf_grow(struct varbuf * v,size_t need_size)147 varbuf_grow(struct varbuf *v, size_t need_size)
148 {
149 /* Make sure the varbuf is in a sane state. */
150 if (v->size < v->used)
151 internerr("varbuf used(%zu) > size(%zu)", v->used, v->size);
152
153 /* Check if we already have enough room. */
154 if ((v->size - v->used) >= need_size)
155 return;
156
157 v->size = (v->size + need_size) * 2;
158 v->buf = m_realloc(v->buf, v->size);
159 }
160
161 void
varbuf_trunc(struct varbuf * v,size_t used_size)162 varbuf_trunc(struct varbuf *v, size_t used_size)
163 {
164 /* Make sure the caller does not claim more than available. */
165 if (v->size < used_size)
166 internerr("varbuf new_used(%zu) > size(%zu)", used_size, v->size);
167
168 v->used = used_size;
169 }
170
171 void
varbuf_snapshot(struct varbuf * v,struct varbuf_state * vs)172 varbuf_snapshot(struct varbuf *v, struct varbuf_state *vs)
173 {
174 vs->used = v->used;
175 }
176
177 void
varbuf_rollback(struct varbuf * v,struct varbuf_state * vs)178 varbuf_rollback(struct varbuf *v, struct varbuf_state *vs)
179 {
180 varbuf_trunc(v, vs->used);
181 }
182
183 char *
varbuf_detach(struct varbuf * v)184 varbuf_detach(struct varbuf *v)
185 {
186 char *buf = v->buf;
187
188 v->buf = NULL;
189 v->size = 0;
190 v->used = 0;
191
192 return buf;
193 }
194
195 void
varbuf_destroy(struct varbuf * v)196 varbuf_destroy(struct varbuf *v)
197 {
198 free(v->buf); v->buf=NULL; v->size=0; v->used=0;
199 }
200
201 void
varbuf_free(struct varbuf * v)202 varbuf_free(struct varbuf *v)
203 {
204 free(v->buf);
205 free(v);
206 }
207