| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: buf.c,v 1.2 2003/11/10 22:18:30 mdw Exp $ |
| 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 | |
| 30 | /*----- Revision history --------------------------------------------------* |
| 31 | * |
| 32 | * $Log: buf.c,v $ |
| 33 | * Revision 1.2 2003/11/10 22:18:30 mdw |
| 34 | * Build fixes. |
| 35 | * |
| 36 | * Revision 1.1 2003/10/11 21:02:33 mdw |
| 37 | * Import buf stuff from tripe. |
| 38 | * |
| 39 | * Revision 1.4 2001/06/19 22:09:54 mdw |
| 40 | * Expose interface, for use in the proxy. |
| 41 | * |
| 42 | * Revision 1.3 2001/03/03 12:06:48 mdw |
| 43 | * Use 16-bit lengths on MPs, since there's a packet limit of 64K anyway. |
| 44 | * |
| 45 | * Revision 1.2 2001/02/16 21:23:20 mdw |
| 46 | * Various minor changes. Check that MPs are in canonical form when |
| 47 | * loading. |
| 48 | * |
| 49 | * Revision 1.1 2001/02/03 20:26:37 mdw |
| 50 | * Initial checkin. |
| 51 | * |
| 52 | */ |
| 53 | |
| 54 | /*----- Header files ------------------------------------------------------*/ |
| 55 | |
| 56 | #include <string.h> |
| 57 | |
| 58 | #include "mp.h" |
| 59 | #include "buf.h" |
| 60 | |
| 61 | /*----- Main code ---------------------------------------------------------*/ |
| 62 | |
| 63 | /* --- @buf_init@ --- * |
| 64 | * |
| 65 | * Arguments: @buf *b@ = pointer to a buffer block |
| 66 | * @void *p@ = pointer to a buffer |
| 67 | * @size_t sz@ = size of the buffer |
| 68 | * |
| 69 | * Returns: --- |
| 70 | * |
| 71 | * Use: Initializes the buffer block appropriately. |
| 72 | */ |
| 73 | |
| 74 | void buf_init(buf *b, void *p, size_t sz) |
| 75 | { |
| 76 | b->base = b->p = p; |
| 77 | b->limit = b->p + sz; |
| 78 | b->f = 0; |
| 79 | } |
| 80 | |
| 81 | /* --- @buf_break@ --- * |
| 82 | * |
| 83 | * Arguments: @buf *b@ = pointer to a buffer block |
| 84 | * |
| 85 | * Returns: Some negative value. |
| 86 | * |
| 87 | * Use: Marks a buffer as broken. |
| 88 | */ |
| 89 | |
| 90 | int buf_break(buf *b) { b->f |= BF_BROKEN; return (-1); } |
| 91 | |
| 92 | /* --- @buf_flip@ --- * |
| 93 | * |
| 94 | * Arguments: @buf *b@ = pointer to a buffer block |
| 95 | * |
| 96 | * Returns: --- |
| 97 | * |
| 98 | * Use: Flips a buffer so that if you've just been writing to it, |
| 99 | * you can now read from the bit you've written. |
| 100 | */ |
| 101 | |
| 102 | void buf_flip(buf *b) |
| 103 | { |
| 104 | b->limit = b->p; |
| 105 | b->p = b->base; |
| 106 | } |
| 107 | |
| 108 | /* --- @buf_ensure@ --- * |
| 109 | * |
| 110 | * Arguments: @buf *b@ = pointer to a buffer block |
| 111 | * @size_t sz@ = size of data wanted |
| 112 | * |
| 113 | * Returns: Zero if it worked, nonzero if there wasn't enough space. |
| 114 | * |
| 115 | * Use: Ensures that there are @sz@ bytes still in the buffer. |
| 116 | */ |
| 117 | |
| 118 | int buf_ensure(buf *b, size_t sz) { return (BENSURE(b, sz)); } |
| 119 | |
| 120 | /* --- @buf_get@ --- * |
| 121 | * |
| 122 | * Arguments: @buf *b@ = pointer to a buffer block |
| 123 | * @size_t sz@ = size of the buffer |
| 124 | * |
| 125 | * Returns: Pointer to the place in the buffer. |
| 126 | * |
| 127 | * Use: Reserves a space in the buffer of the requested size, and |
| 128 | * returns its start address. |
| 129 | */ |
| 130 | |
| 131 | void *buf_get(buf *b, size_t sz) |
| 132 | { |
| 133 | void *p; |
| 134 | if (BENSURE(b, sz)) |
| 135 | return (0); |
| 136 | p = BCUR(b); |
| 137 | BSTEP(b, sz); |
| 138 | return (p); |
| 139 | } |
| 140 | |
| 141 | /* --- @buf_put@ --- * |
| 142 | * |
| 143 | * Arguments: @buf *b@ = pointer to a buffer block |
| 144 | * @const void *p@ = pointer to a buffer |
| 145 | * @size_t sz@ = size of the buffer |
| 146 | * |
| 147 | * Returns: Zero if it worked, nonzero if there wasn't enough space. |
| 148 | * |
| 149 | * Use: Fetches data from some place and puts it in the buffer |
| 150 | */ |
| 151 | |
| 152 | int buf_put(buf *b, const void *p, size_t sz) |
| 153 | { |
| 154 | if (BENSURE(b, sz)) |
| 155 | return (-1); |
| 156 | memcpy(BCUR(b), p, sz); |
| 157 | BSTEP(b, sz); |
| 158 | return (0); |
| 159 | } |
| 160 | |
| 161 | /* --- @buf_getbyte@ --- * |
| 162 | * |
| 163 | * Arguments: @buf *b@ = pointer to a buffer block |
| 164 | * |
| 165 | * Returns: A byte, or less than zero if there wasn't a byte there. |
| 166 | * |
| 167 | * Use: Gets a single byte from a buffer. |
| 168 | */ |
| 169 | |
| 170 | int buf_getbyte(buf *b) |
| 171 | { |
| 172 | if (BENSURE(b, 1)) |
| 173 | return (-1); |
| 174 | return (*b->p++); |
| 175 | } |
| 176 | |
| 177 | /* --- @buf_putbyte@ --- * |
| 178 | * |
| 179 | * Arguments: @buf *b@ = pointer to a buffer block |
| 180 | * @int ch@ = byte to write |
| 181 | * |
| 182 | * Returns: Zero if OK, nonzero if there wasn't enough space. |
| 183 | * |
| 184 | * Use: Puts a single byte in a buffer. |
| 185 | */ |
| 186 | |
| 187 | int buf_putbyte(buf *b, int ch) |
| 188 | { |
| 189 | if (BENSURE(b, 1)) |
| 190 | return (-1); |
| 191 | *b->p++ = ch; |
| 192 | return (0); |
| 193 | } |
| 194 | |
| 195 | /* --- @buf_getu16@ --- * |
| 196 | * |
| 197 | * Arguments: @buf *b@ = pointer to a buffer block |
| 198 | * @uint16 *w@ = where to put the word |
| 199 | * |
| 200 | * Returns: Zero if OK, or nonzero if there wasn't a word there. |
| 201 | * |
| 202 | * Use: Gets a 16-bit word from a buffer. |
| 203 | */ |
| 204 | |
| 205 | int buf_getu16(buf *b, uint16 *w) |
| 206 | { |
| 207 | if (BENSURE(b, 2)) |
| 208 | return (-1); |
| 209 | *w = LOAD16(b->p); |
| 210 | BSTEP(b, 2); |
| 211 | return (0); |
| 212 | } |
| 213 | |
| 214 | /* --- @buf_putu16@ --- * |
| 215 | * |
| 216 | * Arguments: @buf *b@ = pointer to a buffer block |
| 217 | * @uint16 w@ = word to write |
| 218 | * |
| 219 | * Returns: Zero if OK, nonzero if there wasn't enough space. |
| 220 | * |
| 221 | * Use: Puts a 16-but word in a buffer. |
| 222 | */ |
| 223 | |
| 224 | int buf_putu16(buf *b, uint16 w) |
| 225 | { |
| 226 | if (BENSURE(b, 2)) |
| 227 | return (-1); |
| 228 | STORE16(b->p, w); |
| 229 | BSTEP(b, 2); |
| 230 | return (0); |
| 231 | } |
| 232 | |
| 233 | /* --- @buf_getu32@ --- * |
| 234 | * |
| 235 | * Arguments: @buf *b@ = pointer to a buffer block |
| 236 | * @uint32 *w@ = where to put the word |
| 237 | * |
| 238 | * Returns: Zero if OK, or nonzero if there wasn't a word there. |
| 239 | * |
| 240 | * Use: Gets a 32-bit word from a buffer. |
| 241 | */ |
| 242 | |
| 243 | int buf_getu32(buf *b, uint32 *w) |
| 244 | { |
| 245 | if (BENSURE(b, 4)) |
| 246 | return (-1); |
| 247 | *w = LOAD32(b->p); |
| 248 | BSTEP(b, 4); |
| 249 | return (0); |
| 250 | } |
| 251 | |
| 252 | /* --- @buf_putu32@ --- * |
| 253 | * |
| 254 | * Arguments: @buf *b@ = pointer to a buffer block |
| 255 | * @uint32 w@ = word to write |
| 256 | * |
| 257 | * Returns: Zero if OK, nonzero if there wasn't enough space. |
| 258 | * |
| 259 | * Use: Puts a 32-but word in a buffer. |
| 260 | */ |
| 261 | |
| 262 | int buf_putu32(buf *b, uint32 w) |
| 263 | { |
| 264 | if (BENSURE(b, 4)) |
| 265 | return (-1); |
| 266 | STORE32(b->p, w); |
| 267 | BSTEP(b, 4); |
| 268 | return (0); |
| 269 | } |
| 270 | |
| 271 | /* --- @buf_getmp@ --- * |
| 272 | * |
| 273 | * Arguments: @buf *b@ = pointer to a buffer block |
| 274 | * |
| 275 | * Returns: A multiprecision integer, or null if there wasn't one there. |
| 276 | * |
| 277 | * Use: Gets a multiprecision integer from a buffer. |
| 278 | */ |
| 279 | |
| 280 | mp *buf_getmp(buf *b) |
| 281 | { |
| 282 | uint16 sz; |
| 283 | mp *m; |
| 284 | if (buf_getu16(b, &sz) || buf_ensure(b, sz)) |
| 285 | return (0); |
| 286 | m = mp_loadb(MP_NEW, BCUR(b), sz); |
| 287 | if (mp_octets(m) != sz) { |
| 288 | mp_drop(m); |
| 289 | return (0); |
| 290 | } |
| 291 | BSTEP(b, sz); |
| 292 | return (m); |
| 293 | } |
| 294 | |
| 295 | /* --- @buf_putmp@ --- * |
| 296 | * |
| 297 | * Arguments: @buf *b@ = pointer to a buffer block |
| 298 | * @mp *m@ = a multiprecision integer |
| 299 | * |
| 300 | * Returns: Zero if it worked, nonzero if there wasn't enough space. |
| 301 | * |
| 302 | * Use: Puts a multiprecision integer to a buffer. |
| 303 | */ |
| 304 | |
| 305 | int buf_putmp(buf *b, mp *m) |
| 306 | { |
| 307 | size_t sz = mp_octets(m); |
| 308 | assert(sz < MASK16); |
| 309 | if (buf_putu16(b, sz) || buf_ensure(b, sz)) |
| 310 | return (-1); |
| 311 | mp_storeb(m, BCUR(b), sz); |
| 312 | BSTEP(b, sz); |
| 313 | return (0); |
| 314 | } |
| 315 | |
| 316 | /*----- That's all, folks -------------------------------------------------*/ |