3 * $Id: buf.c,v 1.4 2004/04/08 01:36:15 mdw Exp $
7 * (c) 2001 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Header files ------------------------------------------------------*/
38 /*----- Main code ---------------------------------------------------------*/
40 /* --- @buf_init@ --- *
42 * Arguments: @buf *b@ = pointer to a buffer block
43 * @void *p@ = pointer to a buffer
44 * @size_t sz@ = size of the buffer
48 * Use: Initializes the buffer block appropriately.
51 void buf_init(buf
*b
, void *p
, size_t sz
)
58 /* --- @buf_break@ --- *
60 * Arguments: @buf *b@ = pointer to a buffer block
62 * Returns: Some negative value.
64 * Use: Marks a buffer as broken.
67 int buf_break(buf
*b
) { b
->f
|= BF_BROKEN
; return (-1); }
69 /* --- @buf_flip@ --- *
71 * Arguments: @buf *b@ = pointer to a buffer block
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.
85 /* --- @buf_ensure@ --- *
87 * Arguments: @buf *b@ = pointer to a buffer block
88 * @size_t sz@ = size of data wanted
90 * Returns: Zero if it worked, nonzero if there wasn't enough space.
92 * Use: Ensures that there are @sz@ bytes still in the buffer.
95 int buf_ensure(buf
*b
, size_t sz
) { return (BENSURE(b
, sz
)); }
97 /* --- @buf_get@ --- *
99 * Arguments: @buf *b@ = pointer to a buffer block
100 * @size_t sz@ = size of the buffer
102 * Returns: Pointer to the place in the buffer.
104 * Use: Reserves a space in the buffer of the requested size, and
105 * returns its start address.
108 void *buf_get(buf
*b
, size_t sz
)
118 /* --- @buf_put@ --- *
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
124 * Returns: Zero if it worked, nonzero if there wasn't enough space.
126 * Use: Fetches data from some place and puts it in the buffer
129 int buf_put(buf
*b
, const void *p
, size_t sz
)
133 memcpy(BCUR(b
), p
, sz
);
138 /* --- @buf_getbyte@ --- *
140 * Arguments: @buf *b@ = pointer to a buffer block
142 * Returns: A byte, or less than zero if there wasn't a byte there.
144 * Use: Gets a single byte from a buffer.
147 int buf_getbyte(buf
*b
)
154 /* --- @buf_putbyte@ --- *
156 * Arguments: @buf *b@ = pointer to a buffer block
157 * @int ch@ = byte to write
159 * Returns: Zero if OK, nonzero if there wasn't enough space.
161 * Use: Puts a single byte in a buffer.
164 int buf_putbyte(buf
*b
, int ch
)
172 /* --- @buf_getu16@ --- *
174 * Arguments: @buf *b@ = pointer to a buffer block
175 * @uint16 *w@ = where to put the word
177 * Returns: Zero if OK, or nonzero if there wasn't a word there.
179 * Use: Gets a 16-bit word from a buffer.
182 int buf_getu16(buf
*b
, uint16
*w
)
191 /* --- @buf_putu16@ --- *
193 * Arguments: @buf *b@ = pointer to a buffer block
194 * @uint16 w@ = word to write
196 * Returns: Zero if OK, nonzero if there wasn't enough space.
198 * Use: Puts a 16-but word in a buffer.
201 int buf_putu16(buf
*b
, uint16 w
)
210 /* --- @buf_getu32@ --- *
212 * Arguments: @buf *b@ = pointer to a buffer block
213 * @uint32 *w@ = where to put the word
215 * Returns: Zero if OK, or nonzero if there wasn't a word there.
217 * Use: Gets a 32-bit word from a buffer.
220 int buf_getu32(buf
*b
, uint32
*w
)
229 /* --- @buf_putu32@ --- *
231 * Arguments: @buf *b@ = pointer to a buffer block
232 * @uint32 w@ = word to write
234 * Returns: Zero if OK, nonzero if there wasn't enough space.
236 * Use: Puts a 32-but word in a buffer.
239 int buf_putu32(buf
*b
, uint32 w
)
248 /* --- @buf_getmp@ --- *
250 * Arguments: @buf *b@ = pointer to a buffer block
252 * Returns: A multiprecision integer, or null if there wasn't one there.
254 * Use: Gets a multiprecision integer from a buffer.
257 mp
*buf_getmp(buf
*b
)
262 if (buf_getu16(b
, &sz
) || buf_ensure(b
, sz
))
264 m
= mp_loadb(MP_NEW
, BCUR(b
), sz
);
266 if (n
!= sz
&& n
!= 0 && sz
!= 1) {
274 /* --- @buf_putmp@ --- *
276 * Arguments: @buf *b@ = pointer to a buffer block
277 * @mp *m@ = a multiprecision integer
279 * Returns: Zero if it worked, nonzero if there wasn't enough space.
281 * Use: Puts a multiprecision integer to a buffer.
284 int buf_putmp(buf
*b
, mp
*m
)
286 size_t sz
= mp_octets(m
);
289 if (buf_putu16(b
, sz
) || buf_ensure(b
, sz
))
291 mp_storeb(m
, BCUR(b
), sz
);
296 /* --- @buf_getec@ --- *
298 * Arguments: @buf *b@ = pointer to a buffer block
299 * @ec *p@ = where to put the point
301 * Returns: Zero if it worked, nonzero if it failed.
303 * Use: Gets a multiprecision integer from a buffer. The point must
307 int buf_getec(buf
*b
, ec
*p
)
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);
316 EC_DESTROY(p
); p
->x
= x
; p
->y
= y
; p
->z
= 0;
320 /* --- @buf_putec@ --- *
322 * Arguments: @buf *b@ = pointer to a buffer block
323 * @ec *p@ = an elliptic curve point
325 * Returns: Zero if it worked, nonzero if there wasn't enough space.
327 * Use: Puts an elliptic curve point to a buffer.
330 int buf_putec(buf
*b
, ec
*p
)
332 if (EC_ATINF(p
)) return (buf_putu16(b
, 0));
333 if (buf_putmp(b
, p
->x
) || buf_putmp(b
, p
->y
)) return (-1);
337 /*----- That's all, folks -------------------------------------------------*/