chiark / gitweb /
Don't link the client against Catacomb.
[tripe] / buf.c
CommitLineData
410c8acf 1/* -*-c-*-
2 *
cfc354fd 3 * $Id: buf.c,v 1.2 2001/02/16 21:23:20 mdw Exp $
410c8acf 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 $
cfc354fd 32 * Revision 1.2 2001/02/16 21:23:20 mdw
33 * Various minor changes. Check that MPs are in canonical form when
34 * loading.
35 *
410c8acf 36 * Revision 1.1 2001/02/03 20:26:37 mdw
37 * Initial checkin.
38 *
39 */
40
41/*----- Header files ------------------------------------------------------*/
42
43#include "tripe.h"
44
45/*----- Main code ---------------------------------------------------------*/
46
47/* --- @buf_init@ --- *
48 *
49 * Arguments: @buf *b@ = pointer to a buffer block
50 * @void *p@ = pointer to a buffer
51 * @size_t sz@ = size of the buffer
52 *
53 * Returns: ---
54 *
55 * Use: Initializes the buffer block appropriately.
56 */
57
58void buf_init(buf *b, void *p, size_t sz)
59{
60 b->base = b->p = p;
61 b->limit = b->p + sz;
62 b->f = 0;
63}
64
65/* --- @buf_break@ --- *
66 *
67 * Arguments: @buf *b@ = pointer to a buffer block
68 *
69 * Returns: Some negative value.
70 *
71 * Use: Marks a buffer as broken.
72 */
73
74int buf_break(buf *b) { b->f |= BF_BROKEN; return (-1); }
75
cfc354fd 76/* --- @buf_flip@ --- *
77 *
78 * Arguments: @buf *b@ = pointer to a buffer block
79 *
80 * Returns: ---
81 *
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.
84 */
85
86void buf_flip(buf *b)
87{
88 b->limit = b->p;
89 b->p = b->base;
90}
91
410c8acf 92/* --- @buf_ensure@ --- *
93 *
94 * Arguments: @buf *b@ = pointer to a buffer block
95 * @size_t sz@ = size of data wanted
96 *
97 * Returns: Zero if it worked, nonzero if there wasn't enough space.
98 *
99 * Use: Ensures that there are @sz@ bytes still in the buffer.
100 */
101
102int buf_ensure(buf *b, size_t sz) { return (BENSURE(b, sz)); }
103
104/* --- @buf_get@ --- *
105 *
106 * Arguments: @buf *b@ = pointer to a buffer block
410c8acf 107 * @size_t sz@ = size of the buffer
108 *
cfc354fd 109 * Returns: Pointer to the place in the buffer.
410c8acf 110 *
cfc354fd 111 * Use: Reserves a space in the buffer of the requested size, and
112 * returns its start address.
410c8acf 113 */
114
cfc354fd 115void *buf_get(buf *b, size_t sz)
410c8acf 116{
cfc354fd 117 void *p;
410c8acf 118 if (BENSURE(b, sz))
cfc354fd 119 return (0);
120 p = BCUR(b);
410c8acf 121 BSTEP(b, sz);
cfc354fd 122 return (p);
410c8acf 123}
124
125/* --- @buf_put@ --- *
126 *
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
130 *
131 * Returns: Zero if it worked, nonzero if there wasn't enough space.
132 *
133 * Use: Fetches data from some place and puts it in the buffer
134 */
135
136int buf_put(buf *b, const void *p, size_t sz)
137{
138 if (BENSURE(b, sz))
139 return (-1);
140 memcpy(BCUR(b), p, sz);
141 BSTEP(b, sz);
142 return (0);
143}
144
145/* --- @buf_getbyte@ --- *
146 *
147 * Arguments: @buf *b@ = pointer to a buffer block
148 *
149 * Returns: A byte, or less than zero if there wasn't a byte there.
150 *
151 * Use: Gets a single byte from a buffer.
152 */
153
154int buf_getbyte(buf *b)
155{
156 if (BENSURE(b, 1))
157 return (-1);
158 return (*b->p++);
159}
160
161/* --- @buf_putbyte@ --- *
162 *
163 * Arguments: @buf *b@ = pointer to a buffer block
164 * @int ch@ = byte to write
165 *
166 * Returns: Zero if OK, nonzero if there wasn't enough space.
167 *
168 * Use: Puts a single byte in a buffer.
169 */
170
171int buf_putbyte(buf *b, int ch)
172{
173 if (BENSURE(b, 1))
174 return (-1);
175 *b->p++ = ch;
176 return (0);
177}
178
179/* --- @buf_getword@ --- *
180 *
181 * Arguments: @buf *b@ = pointer to a buffer block
182 * @uint32 *w@ = where to put the word
183 *
184 * Returns: Zero if OK, or nonzero if there wasn't a word there.
185 *
186 * Use: Gets a 32-bit word from a buffer.
187 */
188
189int buf_getword(buf *b, uint32 *w)
190{
191 if (BENSURE(b, 4))
192 return (-1);
193 *w = LOAD32(b->p);
194 BSTEP(b, 4);
195 return (0);
196}
197
198/* --- @buf_putword@ --- *
199 *
200 * Arguments: @buf *b@ = pointer to a buffer block
201 * @uint32 w@ = word to write
202 *
203 * Returns: Zero if OK, nonzero if there wasn't enough space.
204 *
205 * Use: Puts a 32-but word in a buffer.
206 */
207
208int buf_putword(buf *b, uint32 w)
209{
210 if (BENSURE(b, 4))
211 return (-1);
212 STORE32(b->p, w);
213 BSTEP(b, 4);
214 return (0);
215}
216
217/* --- @buf_getmp@ --- *
218 *
219 * Arguments: @buf *b@ = pointer to a buffer block
220 *
221 * Returns: A multiprecision integer, or null if there wasn't one there.
222 *
223 * Use: Gets a multiprecision integer from a buffer.
224 */
225
cfc354fd 226mp *buf_getmp(buf *b)
410c8acf 227{
228 uint32 sz;
cfc354fd 229 mp *m;
410c8acf 230 if (buf_getword(b, &sz) || buf_ensure(b, sz))
231 return (0);
cfc354fd 232 m = mp_loadb(MP_NEW, BCUR(b), sz);
233 if (mp_octets(m) != sz) {
234 mp_drop(m);
235 return (0);
236 }
410c8acf 237 BSTEP(b, sz);
cfc354fd 238 return (m);
410c8acf 239}
240
241/* --- @buf_putmp@ --- *
242 *
243 * Arguments: @buf *b@ = pointer to a buffer block
244 * @mp *m@ = a multiprecision integer
245 *
246 * Returns: Zero if it worked, nonzero if there wasn't enough space.
247 *
248 * Use: Puts a multiprecision integer to a buffer.
249 */
250
251int buf_putmp(buf *b, mp *m)
252{
253 size_t sz = mp_octets(m);
254 if (buf_putword(b, sz) || buf_ensure(b, sz))
255 return (-1);
256 mp_storeb(m, BCUR(b), sz);
257 BSTEP(b, sz);
258 return (0);
259}
260
261/*----- That's all, folks -------------------------------------------------*/