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