3 * $Id: buf.c,v 1.1 2001/02/03 20:26:37 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.1 2001/02/03 20:26:37 mdw
37 /*----- Header files ------------------------------------------------------*/
41 /*----- Main code ---------------------------------------------------------*/
43 /* --- @buf_init@ --- *
45 * Arguments: @buf *b@ = pointer to a buffer block
46 * @void *p@ = pointer to a buffer
47 * @size_t sz@ = size of the buffer
51 * Use: Initializes the buffer block appropriately.
54 void buf_init(buf *b, void *p, size_t sz)
61 /* --- @buf_break@ --- *
63 * Arguments: @buf *b@ = pointer to a buffer block
65 * Returns: Some negative value.
67 * Use: Marks a buffer as broken.
70 int buf_break(buf *b) { b->f |= BF_BROKEN; return (-1); }
72 /* --- @buf_ensure@ --- *
74 * Arguments: @buf *b@ = pointer to a buffer block
75 * @size_t sz@ = size of data wanted
77 * Returns: Zero if it worked, nonzero if there wasn't enough space.
79 * Use: Ensures that there are @sz@ bytes still in the buffer.
82 int buf_ensure(buf *b, size_t sz) { return (BENSURE(b, sz)); }
84 /* --- @buf_get@ --- *
86 * Arguments: @buf *b@ = pointer to a buffer block
87 * @void *p@ = pointer to a buffer
88 * @size_t sz@ = size of the buffer
90 * Returns: Zero if it worked, nonzero if there wasn't enough data.
92 * Use: Fetches data from the buffer into some other place.
95 int buf_get(buf *b, void *p, size_t sz)
99 memcpy(p, BCUR(b), sz);
104 /* --- @buf_put@ --- *
106 * Arguments: @buf *b@ = pointer to a buffer block
107 * @const void *p@ = pointer to a buffer
108 * @size_t sz@ = size of the buffer
110 * Returns: Zero if it worked, nonzero if there wasn't enough space.
112 * Use: Fetches data from some place and puts it in the buffer
115 int buf_put(buf *b, const void *p, size_t sz)
119 memcpy(BCUR(b), p, sz);
124 /* --- @buf_getbyte@ --- *
126 * Arguments: @buf *b@ = pointer to a buffer block
128 * Returns: A byte, or less than zero if there wasn't a byte there.
130 * Use: Gets a single byte from a buffer.
133 int buf_getbyte(buf *b)
140 /* --- @buf_putbyte@ --- *
142 * Arguments: @buf *b@ = pointer to a buffer block
143 * @int ch@ = byte to write
145 * Returns: Zero if OK, nonzero if there wasn't enough space.
147 * Use: Puts a single byte in a buffer.
150 int buf_putbyte(buf *b, int ch)
158 /* --- @buf_getword@ --- *
160 * Arguments: @buf *b@ = pointer to a buffer block
161 * @uint32 *w@ = where to put the word
163 * Returns: Zero if OK, or nonzero if there wasn't a word there.
165 * Use: Gets a 32-bit word from a buffer.
168 int buf_getword(buf *b, uint32 *w)
177 /* --- @buf_putword@ --- *
179 * Arguments: @buf *b@ = pointer to a buffer block
180 * @uint32 w@ = word to write
182 * Returns: Zero if OK, nonzero if there wasn't enough space.
184 * Use: Puts a 32-but word in a buffer.
187 int buf_putword(buf *b, uint32 w)
196 /* --- @buf_getmp@ --- *
198 * Arguments: @buf *b@ = pointer to a buffer block
200 * Returns: A multiprecision integer, or null if there wasn't one there.
202 * Use: Gets a multiprecision integer from a buffer.
205 mp *buf_getmp(buf *b, mp *d)
208 if (buf_getword(b, &sz) || buf_ensure(b, sz))
210 d = mp_loadb(d, BCUR(b), sz);
215 /* --- @buf_putmp@ --- *
217 * Arguments: @buf *b@ = pointer to a buffer block
218 * @mp *m@ = a multiprecision integer
220 * Returns: Zero if it worked, nonzero if there wasn't enough space.
222 * Use: Puts a multiprecision integer to a buffer.
225 int buf_putmp(buf *b, mp *m)
227 size_t sz = mp_octets(m);
228 if (buf_putword(b, sz) || buf_ensure(b, sz))
230 mp_storeb(m, BCUR(b), sz);
235 /*----- That's all, folks -------------------------------------------------*/