Commit | Line | Data |
---|---|---|
1479465f GJ |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
104 | varbuf_end_str(struct varbuf *v) | |
105 | { | |
106 | varbuf_grow(v, 1); | |
107 | v->buf[v->used] = '\0'; | |
108 | } | |
109 | ||
110 | const char * | |
111 | varbuf_get_str(struct varbuf *v) | |
112 | { | |
113 | varbuf_end_str(v); | |
114 | ||
115 | return v->buf; | |
116 | } | |
117 | ||
118 | void | |
119 | varbuf_init(struct varbuf *v, size_t size) | |
120 | { | |
121 | v->used = 0; | |
122 | v->size = size; | |
123 | if (size) | |
124 | v->buf = m_malloc(size); | |
125 | else | |
126 | v->buf = NULL; | |
127 | } | |
128 | ||
129 | void | |
130 | varbuf_reset(struct varbuf *v) | |
131 | { | |
132 | v->used= 0; | |
133 | } | |
134 | ||
135 | void | |
136 | varbuf_grow(struct varbuf *v, size_t need_size) | |
137 | { | |
138 | /* Make sure the varbuf is in a sane state. */ | |
139 | if (v->size < v->used) | |
140 | internerr("varbuf used(%zu) > size(%zu)", v->used, v->size); | |
141 | ||
142 | /* Check if we already have enough room. */ | |
143 | if ((v->size - v->used) >= need_size) | |
144 | return; | |
145 | ||
146 | v->size = (v->size + need_size) * 2; | |
147 | v->buf = m_realloc(v->buf, v->size); | |
148 | } | |
149 | ||
150 | void | |
151 | varbuf_trunc(struct varbuf *v, size_t used_size) | |
152 | { | |
153 | /* Make sure the caller does not claim more than available. */ | |
154 | if (v->size < used_size) | |
155 | internerr("varbuf new_used(%zu) > size(%zu)", used_size, v->size); | |
156 | ||
157 | v->used = used_size; | |
158 | } | |
159 | ||
160 | void | |
161 | varbuf_snapshot(struct varbuf *v, struct varbuf_state *vs) | |
162 | { | |
163 | vs->used = v->used; | |
164 | } | |
165 | ||
166 | void | |
167 | varbuf_rollback(struct varbuf *v, struct varbuf_state *vs) | |
168 | { | |
169 | varbuf_trunc(v, vs->used); | |
170 | } | |
171 | ||
172 | char * | |
173 | varbuf_detach(struct varbuf *v) | |
174 | { | |
175 | char *buf = v->buf; | |
176 | ||
177 | v->buf = NULL; | |
178 | v->size = 0; | |
179 | v->used = 0; | |
180 | ||
181 | return buf; | |
182 | } | |
183 | ||
184 | void | |
185 | varbuf_destroy(struct varbuf *v) | |
186 | { | |
187 | free(v->buf); v->buf=NULL; v->size=0; v->used=0; | |
188 | } |