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