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