3952
|
1 /*
|
|
2 * bstream.c
|
|
3 *
|
|
4 * This file contains all functions needed to use bstreams.
|
|
5 */
|
|
6
|
|
7 #define FAIM_INTERNAL
|
10464
|
8 #include <aim.h>
|
3952
|
9
|
|
10 faim_internal int aim_bstream_init(aim_bstream_t *bs, fu8_t *data, int len)
|
|
11 {
|
10464
|
12
|
3952
|
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
|
7631
|
33 faim_internal int aim_bstream_setpos(aim_bstream_t *bs, unsigned int off)
|
3952
|
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
|
10595
|
52 /*
|
|
53 * N can be negative, which can be used for going backwards
|
|
54 * in a bstream. I'm not sure if libfaim actually does
|
|
55 * this anywhere...
|
|
56 */
|
3952
|
57 faim_internal int aim_bstream_advance(aim_bstream_t *bs, int n)
|
|
58 {
|
|
59
|
10595
|
60 if ((aim_bstream_curpos(bs) + n < 0) || (aim_bstream_empty(bs) < n))
|
3952
|
61 return 0; /* XXX throw an exception */
|
|
62
|
|
63 bs->offset += n;
|
|
64
|
|
65 return n;
|
|
66 }
|
|
67
|
|
68 faim_internal fu8_t aimbs_get8(aim_bstream_t *bs)
|
|
69 {
|
10464
|
70
|
3952
|
71 if (aim_bstream_empty(bs) < 1)
|
|
72 return 0; /* XXX throw an exception */
|
10464
|
73
|
3952
|
74 bs->offset++;
|
10464
|
75
|
3952
|
76 return aimutil_get8(bs->data + bs->offset - 1);
|
|
77 }
|
|
78
|
|
79 faim_internal fu16_t aimbs_get16(aim_bstream_t *bs)
|
|
80 {
|
10464
|
81
|
3952
|
82 if (aim_bstream_empty(bs) < 2)
|
|
83 return 0; /* XXX throw an exception */
|
10464
|
84
|
3952
|
85 bs->offset += 2;
|
10464
|
86
|
3952
|
87 return aimutil_get16(bs->data + bs->offset - 2);
|
|
88 }
|
|
89
|
|
90 faim_internal fu32_t aimbs_get32(aim_bstream_t *bs)
|
|
91 {
|
10464
|
92
|
3952
|
93 if (aim_bstream_empty(bs) < 4)
|
|
94 return 0; /* XXX throw an exception */
|
10464
|
95
|
3952
|
96 bs->offset += 4;
|
10464
|
97
|
3952
|
98 return aimutil_get32(bs->data + bs->offset - 4);
|
|
99 }
|
|
100
|
|
101 faim_internal fu8_t aimbs_getle8(aim_bstream_t *bs)
|
|
102 {
|
10464
|
103
|
3952
|
104 if (aim_bstream_empty(bs) < 1)
|
|
105 return 0; /* XXX throw an exception */
|
10464
|
106
|
3952
|
107 bs->offset++;
|
10464
|
108
|
3952
|
109 return aimutil_getle8(bs->data + bs->offset - 1);
|
|
110 }
|
|
111
|
|
112 faim_internal fu16_t aimbs_getle16(aim_bstream_t *bs)
|
|
113 {
|
10464
|
114
|
3952
|
115 if (aim_bstream_empty(bs) < 2)
|
|
116 return 0; /* XXX throw an exception */
|
10464
|
117
|
3952
|
118 bs->offset += 2;
|
10464
|
119
|
3952
|
120 return aimutil_getle16(bs->data + bs->offset - 2);
|
|
121 }
|
|
122
|
|
123 faim_internal fu32_t aimbs_getle32(aim_bstream_t *bs)
|
|
124 {
|
10464
|
125
|
3952
|
126 if (aim_bstream_empty(bs) < 4)
|
|
127 return 0; /* XXX throw an exception */
|
10464
|
128
|
3952
|
129 bs->offset += 4;
|
10464
|
130
|
3952
|
131 return aimutil_getle32(bs->data + bs->offset - 4);
|
|
132 }
|
|
133
|
10990
|
134 faim_internal int aimbs_getrawbuf(aim_bstream_t *bs, fu8_t *buf, int len)
|
|
135 {
|
|
136
|
|
137 if (aim_bstream_empty(bs) < len)
|
|
138 return 0;
|
|
139
|
|
140 memcpy(buf, bs->data + bs->offset, len);
|
|
141 bs->offset += len;
|
|
142
|
|
143 return len;
|
|
144 }
|
|
145
|
|
146 faim_internal fu8_t *aimbs_getraw(aim_bstream_t *bs, int len)
|
|
147 {
|
|
148 fu8_t *ob;
|
|
149
|
|
150 if (!(ob = malloc(len)))
|
|
151 return NULL;
|
|
152
|
|
153 if (aimbs_getrawbuf(bs, ob, len) < len) {
|
|
154 free(ob);
|
|
155 return NULL;
|
|
156 }
|
|
157
|
|
158 return ob;
|
|
159 }
|
|
160
|
|
161 faim_internal char *aimbs_getstr(aim_bstream_t *bs, int len)
|
|
162 {
|
|
163 char *ob;
|
|
164
|
10993
|
165 if (!(ob = malloc(len + 1)))
|
10990
|
166 return NULL;
|
|
167
|
10993
|
168 if (aimbs_getrawbuf(bs, (fu8_t *)ob, len) < len) {
|
10990
|
169 free(ob);
|
|
170 return NULL;
|
|
171 }
|
|
172
|
|
173 ob[len] = '\0';
|
|
174
|
|
175 return ob;
|
|
176 }
|
|
177
|
3952
|
178 faim_internal int aimbs_put8(aim_bstream_t *bs, fu8_t v)
|
|
179 {
|
|
180
|
|
181 if (aim_bstream_empty(bs) < 1)
|
|
182 return 0; /* XXX throw an exception */
|
|
183
|
|
184 bs->offset += aimutil_put8(bs->data + bs->offset, v);
|
|
185
|
|
186 return 1;
|
|
187 }
|
|
188
|
|
189 faim_internal int aimbs_put16(aim_bstream_t *bs, fu16_t v)
|
|
190 {
|
|
191
|
|
192 if (aim_bstream_empty(bs) < 2)
|
|
193 return 0; /* XXX throw an exception */
|
|
194
|
|
195 bs->offset += aimutil_put16(bs->data + bs->offset, v);
|
|
196
|
|
197 return 2;
|
|
198 }
|
|
199
|
|
200 faim_internal int aimbs_put32(aim_bstream_t *bs, fu32_t v)
|
|
201 {
|
|
202
|
|
203 if (aim_bstream_empty(bs) < 4)
|
|
204 return 0; /* XXX throw an exception */
|
|
205
|
|
206 bs->offset += aimutil_put32(bs->data + bs->offset, v);
|
|
207
|
|
208 return 1;
|
|
209 }
|
|
210
|
|
211 faim_internal int aimbs_putle8(aim_bstream_t *bs, fu8_t v)
|
|
212 {
|
|
213
|
|
214 if (aim_bstream_empty(bs) < 1)
|
|
215 return 0; /* XXX throw an exception */
|
|
216
|
|
217 bs->offset += aimutil_putle8(bs->data + bs->offset, v);
|
|
218
|
|
219 return 1;
|
|
220 }
|
|
221
|
|
222 faim_internal int aimbs_putle16(aim_bstream_t *bs, fu16_t v)
|
|
223 {
|
|
224
|
|
225 if (aim_bstream_empty(bs) < 2)
|
|
226 return 0; /* XXX throw an exception */
|
|
227
|
|
228 bs->offset += aimutil_putle16(bs->data + bs->offset, v);
|
|
229
|
|
230 return 2;
|
|
231 }
|
|
232
|
|
233 faim_internal int aimbs_putle32(aim_bstream_t *bs, fu32_t v)
|
|
234 {
|
|
235
|
|
236 if (aim_bstream_empty(bs) < 4)
|
|
237 return 0; /* XXX throw an exception */
|
|
238
|
|
239 bs->offset += aimutil_putle32(bs->data + bs->offset, v);
|
|
240
|
|
241 return 1;
|
|
242 }
|
|
243
|
|
244
|
|
245 faim_internal int aimbs_putraw(aim_bstream_t *bs, const fu8_t *v, int len)
|
|
246 {
|
|
247
|
|
248 if (aim_bstream_empty(bs) < len)
|
|
249 return 0; /* XXX throw an exception */
|
|
250
|
|
251 memcpy(bs->data + bs->offset, v, len);
|
|
252 bs->offset += len;
|
|
253
|
|
254 return len;
|
|
255 }
|
|
256
|
10990
|
257 faim_internal int aimbs_putstr(aim_bstream_t *bs, const char *str)
|
|
258 {
|
10993
|
259 return aimbs_putraw(bs, (fu8_t *)str, strlen(str));
|
10990
|
260 }
|
|
261
|
3952
|
262 faim_internal int aimbs_putbs(aim_bstream_t *bs, aim_bstream_t *srcbs, int len)
|
|
263 {
|
|
264
|
|
265 if (aim_bstream_empty(srcbs) < len)
|
|
266 return 0; /* XXX throw exception (underrun) */
|
|
267
|
|
268 if (aim_bstream_empty(bs) < len)
|
|
269 return 0; /* XXX throw exception (overflow) */
|
|
270
|
|
271 memcpy(bs->data + bs->offset, srcbs->data + srcbs->offset, len);
|
|
272 bs->offset += len;
|
|
273 srcbs->offset += len;
|
|
274
|
|
275 return len;
|
|
276 }
|