3 * $Id: buf.c,v 1.3 2001/03/03 12:06:48 mdw Exp $
7 * (c) 2001 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Trivial IP Encryption (TrIPE).
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.
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.
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.
29 /*----- Revision history --------------------------------------------------*
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.
35 * Revision 1.2 2001/02/16 21:23:20 mdw
36 * Various minor changes. Check that MPs are in canonical form when
39 * Revision 1.1 2001/02/03 20:26:37 mdw
44 /*----- Header files ------------------------------------------------------*/
48 /*----- Main code ---------------------------------------------------------*/
50 /* --- @buf_init@ --- *
52 * Arguments: @buf *b@ = pointer to a buffer block
53 * @void *p@ = pointer to a buffer
54 * @size_t sz@ = size of the buffer
58 * Use: Initializes the buffer block appropriately.
61 void buf_init(buf *b, void *p, size_t sz)
68 /* --- @buf_break@ --- *
70 * Arguments: @buf *b@ = pointer to a buffer block
72 * Returns: Some negative value.
74 * Use: Marks a buffer as broken.
77 int buf_break(buf *b) { b->f |= BF_BROKEN; return (-1); }
79 /* --- @buf_flip@ --- *
81 * Arguments: @buf *b@ = pointer to a buffer block
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.
95 /* --- @buf_ensure@ --- *
97 * Arguments: @buf *b@ = pointer to a buffer block
98 * @size_t sz@ = size of data wanted
100 * Returns: Zero if it worked, nonzero if there wasn't enough space.
102 * Use: Ensures that there are @sz@ bytes still in the buffer.
105 int buf_ensure(buf *b, size_t sz) { return (BENSURE(b, sz)); }
107 /* --- @buf_get@ --- *
109 * Arguments: @buf *b@ = pointer to a buffer block
110 * @size_t sz@ = size of the buffer
112 * Returns: Pointer to the place in the buffer.
114 * Use: Reserves a space in the buffer of the requested size, and
115 * returns its start address.
118 void *buf_get(buf *b, size_t sz)
128 /* --- @buf_put@ --- *
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
134 * Returns: Zero if it worked, nonzero if there wasn't enough space.
136 * Use: Fetches data from some place and puts it in the buffer
139 int buf_put(buf *b, const void *p, size_t sz)
143 memcpy(BCUR(b), p, sz);
148 /* --- @buf_getbyte@ --- *
150 * Arguments: @buf *b@ = pointer to a buffer block
152 * Returns: A byte, or less than zero if there wasn't a byte there.
154 * Use: Gets a single byte from a buffer.
157 int buf_getbyte(buf *b)
164 /* --- @buf_putbyte@ --- *
166 * Arguments: @buf *b@ = pointer to a buffer block
167 * @int ch@ = byte to write
169 * Returns: Zero if OK, nonzero if there wasn't enough space.
171 * Use: Puts a single byte in a buffer.
174 int buf_putbyte(buf *b, int ch)
182 /* --- @buf_getu16@ --- *
184 * Arguments: @buf *b@ = pointer to a buffer block
185 * @uint16 *w@ = where to put the word
187 * Returns: Zero if OK, or nonzero if there wasn't a word there.
189 * Use: Gets a 16-bit word from a buffer.
192 int buf_getu16(buf *b, uint16 *w)
201 /* --- @buf_putu16@ --- *
203 * Arguments: @buf *b@ = pointer to a buffer block
204 * @uint16 w@ = word to write
206 * Returns: Zero if OK, nonzero if there wasn't enough space.
208 * Use: Puts a 16-but word in a buffer.
211 int buf_putu16(buf *b, uint16 w)
220 /* --- @buf_getu32@ --- *
222 * Arguments: @buf *b@ = pointer to a buffer block
223 * @uint32 *w@ = where to put the word
225 * Returns: Zero if OK, or nonzero if there wasn't a word there.
227 * Use: Gets a 32-bit word from a buffer.
230 int buf_getu32(buf *b, uint32 *w)
239 /* --- @buf_putu32@ --- *
241 * Arguments: @buf *b@ = pointer to a buffer block
242 * @uint32 w@ = word to write
244 * Returns: Zero if OK, nonzero if there wasn't enough space.
246 * Use: Puts a 32-but word in a buffer.
249 int buf_putu32(buf *b, uint32 w)
258 /* --- @buf_getmp@ --- *
260 * Arguments: @buf *b@ = pointer to a buffer block
262 * Returns: A multiprecision integer, or null if there wasn't one there.
264 * Use: Gets a multiprecision integer from a buffer.
267 mp *buf_getmp(buf *b)
271 if (buf_getu16(b, &sz) || buf_ensure(b, sz))
273 m = mp_loadb(MP_NEW, BCUR(b), sz);
274 if (mp_octets(m) != sz) {
282 /* --- @buf_putmp@ --- *
284 * Arguments: @buf *b@ = pointer to a buffer block
285 * @mp *m@ = a multiprecision integer
287 * Returns: Zero if it worked, nonzero if there wasn't enough space.
289 * Use: Puts a multiprecision integer to a buffer.
292 int buf_putmp(buf *b, mp *m)
294 size_t sz = mp_octets(m);
296 if (buf_putu16(b, sz) || buf_ensure(b, sz))
298 mp_storeb(m, BCUR(b), sz);
303 /*----- That's all, folks -------------------------------------------------*/