Import buf stuff from tripe.
[u/mdw/catacomb] / buf.c
CommitLineData
6b80b6c4 1/* -*-c-*-
2 *
3 * $Id: buf.c,v 1.1 2003/10/11 21:02:33 mdw Exp $
4 *
5 * Buffer handling
6 *
7 * (c) 2001 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: buf.c,v $
33 * Revision 1.1 2003/10/11 21:02:33 mdw
34 * Import buf stuff from tripe.
35 *
36 * Revision 1.4 2001/06/19 22:09:54 mdw
37 * Expose interface, for use in the proxy.
38 *
39 * Revision 1.3 2001/03/03 12:06:48 mdw
40 * Use 16-bit lengths on MPs, since there's a packet limit of 64K anyway.
41 *
42 * Revision 1.2 2001/02/16 21:23:20 mdw
43 * Various minor changes. Check that MPs are in canonical form when
44 * loading.
45 *
46 * Revision 1.1 2001/02/03 20:26:37 mdw
47 * Initial checkin.
48 *
49 */
50
51/*----- Header files ------------------------------------------------------*/
52
53#include <string.h>
54
55#include <catacomb/mp.h>
56
57#include "buf.h"
58
59/*----- Main code ---------------------------------------------------------*/
60
61/* --- @buf_init@ --- *
62 *
63 * Arguments: @buf *b@ = pointer to a buffer block
64 * @void *p@ = pointer to a buffer
65 * @size_t sz@ = size of the buffer
66 *
67 * Returns: ---
68 *
69 * Use: Initializes the buffer block appropriately.
70 */
71
72void buf_init(buf *b, void *p, size_t sz)
73{
74 b->base = b->p = p;
75 b->limit = b->p + sz;
76 b->f = 0;
77}
78
79/* --- @buf_break@ --- *
80 *
81 * Arguments: @buf *b@ = pointer to a buffer block
82 *
83 * Returns: Some negative value.
84 *
85 * Use: Marks a buffer as broken.
86 */
87
88int buf_break(buf *b) { b->f |= BF_BROKEN; return (-1); }
89
90/* --- @buf_flip@ --- *
91 *
92 * Arguments: @buf *b@ = pointer to a buffer block
93 *
94 * Returns: ---
95 *
96 * Use: Flips a buffer so that if you've just been writing to it,
97 * you can now read from the bit you've written.
98 */
99
100void buf_flip(buf *b)
101{
102 b->limit = b->p;
103 b->p = b->base;
104}
105
106/* --- @buf_ensure@ --- *
107 *
108 * Arguments: @buf *b@ = pointer to a buffer block
109 * @size_t sz@ = size of data wanted
110 *
111 * Returns: Zero if it worked, nonzero if there wasn't enough space.
112 *
113 * Use: Ensures that there are @sz@ bytes still in the buffer.
114 */
115
116int buf_ensure(buf *b, size_t sz) { return (BENSURE(b, sz)); }
117
118/* --- @buf_get@ --- *
119 *
120 * Arguments: @buf *b@ = pointer to a buffer block
121 * @size_t sz@ = size of the buffer
122 *
123 * Returns: Pointer to the place in the buffer.
124 *
125 * Use: Reserves a space in the buffer of the requested size, and
126 * returns its start address.
127 */
128
129void *buf_get(buf *b, size_t sz)
130{
131 void *p;
132 if (BENSURE(b, sz))
133 return (0);
134 p = BCUR(b);
135 BSTEP(b, sz);
136 return (p);
137}
138
139/* --- @buf_put@ --- *
140 *
141 * Arguments: @buf *b@ = pointer to a buffer block
142 * @const void *p@ = pointer to a buffer
143 * @size_t sz@ = size of the buffer
144 *
145 * Returns: Zero if it worked, nonzero if there wasn't enough space.
146 *
147 * Use: Fetches data from some place and puts it in the buffer
148 */
149
150int buf_put(buf *b, const void *p, size_t sz)
151{
152 if (BENSURE(b, sz))
153 return (-1);
154 memcpy(BCUR(b), p, sz);
155 BSTEP(b, sz);
156 return (0);
157}
158
159/* --- @buf_getbyte@ --- *
160 *
161 * Arguments: @buf *b@ = pointer to a buffer block
162 *
163 * Returns: A byte, or less than zero if there wasn't a byte there.
164 *
165 * Use: Gets a single byte from a buffer.
166 */
167
168int buf_getbyte(buf *b)
169{
170 if (BENSURE(b, 1))
171 return (-1);
172 return (*b->p++);
173}
174
175/* --- @buf_putbyte@ --- *
176 *
177 * Arguments: @buf *b@ = pointer to a buffer block
178 * @int ch@ = byte to write
179 *
180 * Returns: Zero if OK, nonzero if there wasn't enough space.
181 *
182 * Use: Puts a single byte in a buffer.
183 */
184
185int buf_putbyte(buf *b, int ch)
186{
187 if (BENSURE(b, 1))
188 return (-1);
189 *b->p++ = ch;
190 return (0);
191}
192
193/* --- @buf_getu16@ --- *
194 *
195 * Arguments: @buf *b@ = pointer to a buffer block
196 * @uint16 *w@ = where to put the word
197 *
198 * Returns: Zero if OK, or nonzero if there wasn't a word there.
199 *
200 * Use: Gets a 16-bit word from a buffer.
201 */
202
203int buf_getu16(buf *b, uint16 *w)
204{
205 if (BENSURE(b, 2))
206 return (-1);
207 *w = LOAD16(b->p);
208 BSTEP(b, 2);
209 return (0);
210}
211
212/* --- @buf_putu16@ --- *
213 *
214 * Arguments: @buf *b@ = pointer to a buffer block
215 * @uint16 w@ = word to write
216 *
217 * Returns: Zero if OK, nonzero if there wasn't enough space.
218 *
219 * Use: Puts a 16-but word in a buffer.
220 */
221
222int buf_putu16(buf *b, uint16 w)
223{
224 if (BENSURE(b, 2))
225 return (-1);
226 STORE16(b->p, w);
227 BSTEP(b, 2);
228 return (0);
229}
230
231/* --- @buf_getu32@ --- *
232 *
233 * Arguments: @buf *b@ = pointer to a buffer block
234 * @uint32 *w@ = where to put the word
235 *
236 * Returns: Zero if OK, or nonzero if there wasn't a word there.
237 *
238 * Use: Gets a 32-bit word from a buffer.
239 */
240
241int buf_getu32(buf *b, uint32 *w)
242{
243 if (BENSURE(b, 4))
244 return (-1);
245 *w = LOAD32(b->p);
246 BSTEP(b, 4);
247 return (0);
248}
249
250/* --- @buf_putu32@ --- *
251 *
252 * Arguments: @buf *b@ = pointer to a buffer block
253 * @uint32 w@ = word to write
254 *
255 * Returns: Zero if OK, nonzero if there wasn't enough space.
256 *
257 * Use: Puts a 32-but word in a buffer.
258 */
259
260int buf_putu32(buf *b, uint32 w)
261{
262 if (BENSURE(b, 4))
263 return (-1);
264 STORE32(b->p, w);
265 BSTEP(b, 4);
266 return (0);
267}
268
269/* --- @buf_getmp@ --- *
270 *
271 * Arguments: @buf *b@ = pointer to a buffer block
272 *
273 * Returns: A multiprecision integer, or null if there wasn't one there.
274 *
275 * Use: Gets a multiprecision integer from a buffer.
276 */
277
278mp *buf_getmp(buf *b)
279{
280 uint16 sz;
281 mp *m;
282 if (buf_getu16(b, &sz) || buf_ensure(b, sz))
283 return (0);
284 m = mp_loadb(MP_NEW, BCUR(b), sz);
285 if (mp_octets(m) != sz) {
286 mp_drop(m);
287 return (0);
288 }
289 BSTEP(b, sz);
290 return (m);
291}
292
293/* --- @buf_putmp@ --- *
294 *
295 * Arguments: @buf *b@ = pointer to a buffer block
296 * @mp *m@ = a multiprecision integer
297 *
298 * Returns: Zero if it worked, nonzero if there wasn't enough space.
299 *
300 * Use: Puts a multiprecision integer to a buffer.
301 */
302
303int buf_putmp(buf *b, mp *m)
304{
305 size_t sz = mp_octets(m);
306 assert(sz < MASK16);
307 if (buf_putu16(b, sz) || buf_ensure(b, sz))
308 return (-1);
309 mp_storeb(m, BCUR(b), sz);
310 BSTEP(b, sz);
311 return (0);
312}
313
314/*----- That's all, folks -------------------------------------------------*/