3 * $Id: buf.c,v 1.2 2001/02/16 21:23:20 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.2 2001/02/16 21:23:20 mdw
33 * Various minor changes. Check that MPs are in canonical form when
36 * Revision 1.1 2001/02/03 20:26:37 mdw
41 /*----- Header files ------------------------------------------------------*/
45 /*----- Main code ---------------------------------------------------------*/
47 /* --- @buf_init@ --- *
49 * Arguments: @buf *b@ = pointer to a buffer block
50 * @void *p@ = pointer to a buffer
51 * @size_t sz@ = size of the buffer
55 * Use: Initializes the buffer block appropriately.
58 void buf_init(buf *b, void *p, size_t sz)
65 /* --- @buf_break@ --- *
67 * Arguments: @buf *b@ = pointer to a buffer block
69 * Returns: Some negative value.
71 * Use: Marks a buffer as broken.
74 int buf_break(buf *b) { b->f |= BF_BROKEN; return (-1); }
76 /* --- @buf_flip@ --- *
78 * Arguments: @buf *b@ = pointer to a buffer block
82 * Use: Flips a buffer so that if you've just been writing to it,
83 * you can now read from the bit you've written.
92 /* --- @buf_ensure@ --- *
94 * Arguments: @buf *b@ = pointer to a buffer block
95 * @size_t sz@ = size of data wanted
97 * Returns: Zero if it worked, nonzero if there wasn't enough space.
99 * Use: Ensures that there are @sz@ bytes still in the buffer.
102 int buf_ensure(buf *b, size_t sz) { return (BENSURE(b, sz)); }
104 /* --- @buf_get@ --- *
106 * Arguments: @buf *b@ = pointer to a buffer block
107 * @size_t sz@ = size of the buffer
109 * Returns: Pointer to the place in the buffer.
111 * Use: Reserves a space in the buffer of the requested size, and
112 * returns its start address.
115 void *buf_get(buf *b, size_t sz)
125 /* --- @buf_put@ --- *
127 * Arguments: @buf *b@ = pointer to a buffer block
128 * @const void *p@ = pointer to a buffer
129 * @size_t sz@ = size of the buffer
131 * Returns: Zero if it worked, nonzero if there wasn't enough space.
133 * Use: Fetches data from some place and puts it in the buffer
136 int buf_put(buf *b, const void *p, size_t sz)
140 memcpy(BCUR(b), p, sz);
145 /* --- @buf_getbyte@ --- *
147 * Arguments: @buf *b@ = pointer to a buffer block
149 * Returns: A byte, or less than zero if there wasn't a byte there.
151 * Use: Gets a single byte from a buffer.
154 int buf_getbyte(buf *b)
161 /* --- @buf_putbyte@ --- *
163 * Arguments: @buf *b@ = pointer to a buffer block
164 * @int ch@ = byte to write
166 * Returns: Zero if OK, nonzero if there wasn't enough space.
168 * Use: Puts a single byte in a buffer.
171 int buf_putbyte(buf *b, int ch)
179 /* --- @buf_getword@ --- *
181 * Arguments: @buf *b@ = pointer to a buffer block
182 * @uint32 *w@ = where to put the word
184 * Returns: Zero if OK, or nonzero if there wasn't a word there.
186 * Use: Gets a 32-bit word from a buffer.
189 int buf_getword(buf *b, uint32 *w)
198 /* --- @buf_putword@ --- *
200 * Arguments: @buf *b@ = pointer to a buffer block
201 * @uint32 w@ = word to write
203 * Returns: Zero if OK, nonzero if there wasn't enough space.
205 * Use: Puts a 32-but word in a buffer.
208 int buf_putword(buf *b, uint32 w)
217 /* --- @buf_getmp@ --- *
219 * Arguments: @buf *b@ = pointer to a buffer block
221 * Returns: A multiprecision integer, or null if there wasn't one there.
223 * Use: Gets a multiprecision integer from a buffer.
226 mp *buf_getmp(buf *b)
230 if (buf_getword(b, &sz) || buf_ensure(b, sz))
232 m = mp_loadb(MP_NEW, BCUR(b), sz);
233 if (mp_octets(m) != sz) {
241 /* --- @buf_putmp@ --- *
243 * Arguments: @buf *b@ = pointer to a buffer block
244 * @mp *m@ = a multiprecision integer
246 * Returns: Zero if it worked, nonzero if there wasn't enough space.
248 * Use: Puts a multiprecision integer to a buffer.
251 int buf_putmp(buf *b, mp *m)
253 size_t sz = mp_octets(m);
254 if (buf_putword(b, sz) || buf_ensure(b, sz))
256 mp_storeb(m, BCUR(b), sz);
261 /*----- That's all, folks -------------------------------------------------*/