[862371b] | 1 | /* |
---|
| 2 | * bstream.c |
---|
| 3 | * |
---|
| 4 | * This file contains all functions needed to use bstreams. |
---|
| 5 | */ |
---|
| 6 | |
---|
| 7 | #define FAIM_INTERNAL |
---|
| 8 | #include <aim.h> |
---|
| 9 | |
---|
| 10 | faim_internal int aim_bstream_init(aim_bstream_t *bs, fu8_t *data, int len) |
---|
| 11 | { |
---|
| 12 | |
---|
| 13 | if (!bs) |
---|
| 14 | return -1; |
---|
| 15 | |
---|
| 16 | bs->data = data; |
---|
| 17 | bs->len = len; |
---|
| 18 | bs->offset = 0; |
---|
| 19 | |
---|
| 20 | return 0; |
---|
| 21 | } |
---|
| 22 | |
---|
| 23 | faim_internal int aim_bstream_empty(aim_bstream_t *bs) |
---|
| 24 | { |
---|
| 25 | return bs->len - bs->offset; |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | faim_internal int aim_bstream_curpos(aim_bstream_t *bs) |
---|
| 29 | { |
---|
| 30 | return bs->offset; |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | faim_internal int aim_bstream_setpos(aim_bstream_t *bs, int off) |
---|
| 34 | { |
---|
| 35 | |
---|
| 36 | if (off > bs->len) |
---|
| 37 | return -1; |
---|
| 38 | |
---|
| 39 | bs->offset = off; |
---|
| 40 | |
---|
| 41 | return off; |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | faim_internal void aim_bstream_rewind(aim_bstream_t *bs) |
---|
| 45 | { |
---|
| 46 | |
---|
| 47 | aim_bstream_setpos(bs, 0); |
---|
| 48 | |
---|
| 49 | return; |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | faim_internal int aim_bstream_advance(aim_bstream_t *bs, int n) |
---|
| 53 | { |
---|
| 54 | |
---|
| 55 | if (aim_bstream_empty(bs) < n) |
---|
| 56 | return 0; /* XXX throw an exception */ |
---|
| 57 | |
---|
| 58 | bs->offset += n; |
---|
| 59 | |
---|
| 60 | return n; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | faim_internal fu8_t aimbs_get8(aim_bstream_t *bs) |
---|
| 64 | { |
---|
| 65 | |
---|
| 66 | if (aim_bstream_empty(bs) < 1) |
---|
| 67 | return 0; /* XXX throw an exception */ |
---|
| 68 | |
---|
| 69 | bs->offset++; |
---|
| 70 | |
---|
| 71 | return aimutil_get8(bs->data + bs->offset - 1); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | faim_internal fu16_t aimbs_get16(aim_bstream_t *bs) |
---|
| 75 | { |
---|
| 76 | |
---|
| 77 | if (aim_bstream_empty(bs) < 2) |
---|
| 78 | return 0; /* XXX throw an exception */ |
---|
| 79 | |
---|
| 80 | bs->offset += 2; |
---|
| 81 | |
---|
| 82 | return aimutil_get16(bs->data + bs->offset - 2); |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | faim_internal fu32_t aimbs_get32(aim_bstream_t *bs) |
---|
| 86 | { |
---|
| 87 | |
---|
| 88 | if (aim_bstream_empty(bs) < 4) |
---|
| 89 | return 0; /* XXX throw an exception */ |
---|
| 90 | |
---|
| 91 | bs->offset += 4; |
---|
| 92 | |
---|
| 93 | return aimutil_get32(bs->data + bs->offset - 4); |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | faim_internal fu8_t aimbs_getle8(aim_bstream_t *bs) |
---|
| 97 | { |
---|
| 98 | |
---|
| 99 | if (aim_bstream_empty(bs) < 1) |
---|
| 100 | return 0; /* XXX throw an exception */ |
---|
| 101 | |
---|
| 102 | bs->offset++; |
---|
| 103 | |
---|
| 104 | return aimutil_getle8(bs->data + bs->offset - 1); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | faim_internal fu16_t aimbs_getle16(aim_bstream_t *bs) |
---|
| 108 | { |
---|
| 109 | |
---|
| 110 | if (aim_bstream_empty(bs) < 2) |
---|
| 111 | return 0; /* XXX throw an exception */ |
---|
| 112 | |
---|
| 113 | bs->offset += 2; |
---|
| 114 | |
---|
| 115 | return aimutil_getle16(bs->data + bs->offset - 2); |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | faim_internal fu32_t aimbs_getle32(aim_bstream_t *bs) |
---|
| 119 | { |
---|
| 120 | |
---|
| 121 | if (aim_bstream_empty(bs) < 4) |
---|
| 122 | return 0; /* XXX throw an exception */ |
---|
| 123 | |
---|
| 124 | bs->offset += 4; |
---|
| 125 | |
---|
| 126 | return aimutil_getle32(bs->data + bs->offset - 4); |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | faim_internal int aimbs_put8(aim_bstream_t *bs, fu8_t v) |
---|
| 130 | { |
---|
| 131 | |
---|
| 132 | if (aim_bstream_empty(bs) < 1) |
---|
| 133 | return 0; /* XXX throw an exception */ |
---|
| 134 | |
---|
| 135 | bs->offset += aimutil_put8(bs->data + bs->offset, v); |
---|
| 136 | |
---|
| 137 | return 1; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | faim_internal int aimbs_put16(aim_bstream_t *bs, fu16_t v) |
---|
| 141 | { |
---|
| 142 | |
---|
| 143 | if (aim_bstream_empty(bs) < 2) |
---|
| 144 | return 0; /* XXX throw an exception */ |
---|
| 145 | |
---|
| 146 | bs->offset += aimutil_put16(bs->data + bs->offset, v); |
---|
| 147 | |
---|
| 148 | return 2; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | faim_internal int aimbs_put32(aim_bstream_t *bs, fu32_t v) |
---|
| 152 | { |
---|
| 153 | |
---|
| 154 | if (aim_bstream_empty(bs) < 4) |
---|
| 155 | return 0; /* XXX throw an exception */ |
---|
| 156 | |
---|
| 157 | bs->offset += aimutil_put32(bs->data + bs->offset, v); |
---|
| 158 | |
---|
| 159 | return 1; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | faim_internal int aimbs_putle8(aim_bstream_t *bs, fu8_t v) |
---|
| 163 | { |
---|
| 164 | |
---|
| 165 | if (aim_bstream_empty(bs) < 1) |
---|
| 166 | return 0; /* XXX throw an exception */ |
---|
| 167 | |
---|
| 168 | bs->offset += aimutil_putle8(bs->data + bs->offset, v); |
---|
| 169 | |
---|
| 170 | return 1; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | faim_internal int aimbs_putle16(aim_bstream_t *bs, fu16_t v) |
---|
| 174 | { |
---|
| 175 | |
---|
| 176 | if (aim_bstream_empty(bs) < 2) |
---|
| 177 | return 0; /* XXX throw an exception */ |
---|
| 178 | |
---|
| 179 | bs->offset += aimutil_putle16(bs->data + bs->offset, v); |
---|
| 180 | |
---|
| 181 | return 2; |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | faim_internal int aimbs_putle32(aim_bstream_t *bs, fu32_t v) |
---|
| 185 | { |
---|
| 186 | |
---|
| 187 | if (aim_bstream_empty(bs) < 4) |
---|
| 188 | return 0; /* XXX throw an exception */ |
---|
| 189 | |
---|
| 190 | bs->offset += aimutil_putle32(bs->data + bs->offset, v); |
---|
| 191 | |
---|
| 192 | return 1; |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | faim_internal int aimbs_getrawbuf(aim_bstream_t *bs, fu8_t *buf, int len) |
---|
| 196 | { |
---|
| 197 | |
---|
| 198 | if (aim_bstream_empty(bs) < len) |
---|
| 199 | return 0; |
---|
| 200 | |
---|
| 201 | memcpy(buf, bs->data + bs->offset, len); |
---|
| 202 | bs->offset += len; |
---|
| 203 | |
---|
| 204 | return len; |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | faim_internal fu8_t *aimbs_getraw(aim_bstream_t *bs, int len) |
---|
| 208 | { |
---|
| 209 | fu8_t *ob; |
---|
| 210 | |
---|
| 211 | if (!(ob = malloc(len))) |
---|
| 212 | return NULL; |
---|
| 213 | |
---|
| 214 | if (aimbs_getrawbuf(bs, ob, len) < len) { |
---|
| 215 | free(ob); |
---|
| 216 | return NULL; |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | return ob; |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | faim_internal char *aimbs_getstr(aim_bstream_t *bs, int len) |
---|
| 223 | { |
---|
| 224 | char *ob; |
---|
| 225 | |
---|
| 226 | if (!(ob = malloc(len+1))) |
---|
| 227 | return NULL; |
---|
| 228 | |
---|
| 229 | if (aimbs_getrawbuf(bs, ob, len) < len) { |
---|
| 230 | free(ob); |
---|
| 231 | return NULL; |
---|
| 232 | } |
---|
[e374dee] | 233 | |
---|
[862371b] | 234 | ob[len] = '\0'; |
---|
| 235 | |
---|
| 236 | return ob; |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | faim_internal int aimbs_putraw(aim_bstream_t *bs, const fu8_t *v, int len) |
---|
| 240 | { |
---|
| 241 | |
---|
| 242 | if (aim_bstream_empty(bs) < len) |
---|
| 243 | return 0; /* XXX throw an exception */ |
---|
| 244 | |
---|
| 245 | memcpy(bs->data + bs->offset, v, len); |
---|
| 246 | bs->offset += len; |
---|
| 247 | |
---|
| 248 | return len; |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | faim_internal int aimbs_putbs(aim_bstream_t *bs, aim_bstream_t *srcbs, int len) |
---|
| 252 | { |
---|
| 253 | |
---|
| 254 | if (aim_bstream_empty(srcbs) < len) |
---|
| 255 | return 0; /* XXX throw exception (underrun) */ |
---|
| 256 | |
---|
| 257 | if (aim_bstream_empty(bs) < len) |
---|
| 258 | return 0; /* XXX throw exception (overflow) */ |
---|
| 259 | |
---|
| 260 | memcpy(bs->data + bs->offset, srcbs->data + srcbs->offset, len); |
---|
| 261 | bs->offset += len; |
---|
| 262 | srcbs->offset += len; |
---|
| 263 | |
---|
| 264 | return len; |
---|
| 265 | } |
---|