3952
|
1 /*
|
13234
|
2 * Gaim's oscar protocol plugin
|
|
3 * This file is the legal property of its developers.
|
|
4 * Please see the AUTHORS file distributed alongside this file.
|
|
5 *
|
|
6 * This library is free software; you can redistribute it and/or
|
|
7 * modify it under the terms of the GNU Lesser General Public
|
|
8 * License as published by the Free Software Foundation; either
|
|
9 * version 2 of the License, or (at your option) any later version.
|
3952
|
10 *
|
13234
|
11 * This library is distributed in the hope that it will be useful,
|
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
14 * Lesser General Public License for more details.
|
|
15 *
|
|
16 * You should have received a copy of the GNU Lesser General Public
|
|
17 * License along with this library; if not, write to the Free Software
|
|
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
19 */
|
|
20
|
|
21 /*
|
3952
|
22 * This file contains all functions needed to use bstreams.
|
|
23 */
|
|
24
|
13234
|
25 #include "oscar.h"
|
3952
|
26
|
13239
|
27 faim_internal int aim_bstream_init(ByteStream *bs, guint8 *data, int len)
|
3952
|
28 {
|
10464
|
29
|
3952
|
30 if (!bs)
|
|
31 return -1;
|
|
32
|
|
33 bs->data = data;
|
|
34 bs->len = len;
|
|
35 bs->offset = 0;
|
|
36
|
|
37 return 0;
|
|
38 }
|
|
39
|
13239
|
40 faim_internal int aim_bstream_empty(ByteStream *bs)
|
3952
|
41 {
|
|
42 return bs->len - bs->offset;
|
|
43 }
|
|
44
|
13239
|
45 faim_internal int aim_bstream_curpos(ByteStream *bs)
|
3952
|
46 {
|
|
47 return bs->offset;
|
|
48 }
|
|
49
|
13239
|
50 faim_internal int aim_bstream_setpos(ByteStream *bs, unsigned int off)
|
3952
|
51 {
|
|
52
|
|
53 if (off > bs->len)
|
|
54 return -1;
|
|
55
|
|
56 bs->offset = off;
|
|
57
|
|
58 return off;
|
|
59 }
|
|
60
|
13239
|
61 faim_internal void aim_bstream_rewind(ByteStream *bs)
|
3952
|
62 {
|
|
63
|
|
64 aim_bstream_setpos(bs, 0);
|
|
65
|
|
66 return;
|
|
67 }
|
|
68
|
10595
|
69 /*
|
|
70 * N can be negative, which can be used for going backwards
|
|
71 * in a bstream. I'm not sure if libfaim actually does
|
|
72 * this anywhere...
|
|
73 */
|
13239
|
74 faim_internal int aim_bstream_advance(ByteStream *bs, int n)
|
3952
|
75 {
|
|
76
|
10595
|
77 if ((aim_bstream_curpos(bs) + n < 0) || (aim_bstream_empty(bs) < n))
|
3952
|
78 return 0; /* XXX throw an exception */
|
|
79
|
|
80 bs->offset += n;
|
|
81
|
|
82 return n;
|
|
83 }
|
|
84
|
13239
|
85 faim_internal guint8 aimbs_get8(ByteStream *bs)
|
3952
|
86 {
|
10464
|
87
|
3952
|
88 if (aim_bstream_empty(bs) < 1)
|
|
89 return 0; /* XXX throw an exception */
|
10464
|
90
|
3952
|
91 bs->offset++;
|
10464
|
92
|
3952
|
93 return aimutil_get8(bs->data + bs->offset - 1);
|
|
94 }
|
|
95
|
13239
|
96 faim_internal guint16 aimbs_get16(ByteStream *bs)
|
3952
|
97 {
|
10464
|
98
|
3952
|
99 if (aim_bstream_empty(bs) < 2)
|
|
100 return 0; /* XXX throw an exception */
|
10464
|
101
|
3952
|
102 bs->offset += 2;
|
10464
|
103
|
3952
|
104 return aimutil_get16(bs->data + bs->offset - 2);
|
|
105 }
|
|
106
|
13239
|
107 faim_internal guint32 aimbs_get32(ByteStream *bs)
|
3952
|
108 {
|
10464
|
109
|
3952
|
110 if (aim_bstream_empty(bs) < 4)
|
|
111 return 0; /* XXX throw an exception */
|
10464
|
112
|
3952
|
113 bs->offset += 4;
|
10464
|
114
|
3952
|
115 return aimutil_get32(bs->data + bs->offset - 4);
|
|
116 }
|
|
117
|
13239
|
118 faim_internal guint8 aimbs_getle8(ByteStream *bs)
|
3952
|
119 {
|
10464
|
120
|
3952
|
121 if (aim_bstream_empty(bs) < 1)
|
|
122 return 0; /* XXX throw an exception */
|
10464
|
123
|
3952
|
124 bs->offset++;
|
10464
|
125
|
3952
|
126 return aimutil_getle8(bs->data + bs->offset - 1);
|
|
127 }
|
|
128
|
13239
|
129 faim_internal guint16 aimbs_getle16(ByteStream *bs)
|
3952
|
130 {
|
10464
|
131
|
3952
|
132 if (aim_bstream_empty(bs) < 2)
|
|
133 return 0; /* XXX throw an exception */
|
10464
|
134
|
3952
|
135 bs->offset += 2;
|
10464
|
136
|
3952
|
137 return aimutil_getle16(bs->data + bs->offset - 2);
|
|
138 }
|
|
139
|
13239
|
140 faim_internal guint32 aimbs_getle32(ByteStream *bs)
|
3952
|
141 {
|
10464
|
142
|
3952
|
143 if (aim_bstream_empty(bs) < 4)
|
|
144 return 0; /* XXX throw an exception */
|
10464
|
145
|
3952
|
146 bs->offset += 4;
|
10464
|
147
|
3952
|
148 return aimutil_getle32(bs->data + bs->offset - 4);
|
|
149 }
|
|
150
|
13239
|
151 faim_internal int aimbs_getrawbuf(ByteStream *bs, guint8 *buf, int len)
|
10990
|
152 {
|
|
153
|
|
154 if (aim_bstream_empty(bs) < len)
|
|
155 return 0;
|
|
156
|
|
157 memcpy(buf, bs->data + bs->offset, len);
|
|
158 bs->offset += len;
|
|
159
|
|
160 return len;
|
|
161 }
|
|
162
|
13239
|
163 faim_internal guint8 *aimbs_getraw(ByteStream *bs, int len)
|
10990
|
164 {
|
13234
|
165 guint8 *ob;
|
10990
|
166
|
|
167 if (!(ob = malloc(len)))
|
|
168 return NULL;
|
|
169
|
|
170 if (aimbs_getrawbuf(bs, ob, len) < len) {
|
|
171 free(ob);
|
|
172 return NULL;
|
|
173 }
|
|
174
|
|
175 return ob;
|
|
176 }
|
|
177
|
13239
|
178 faim_internal char *aimbs_getstr(ByteStream *bs, int len)
|
10990
|
179 {
|
|
180 char *ob;
|
|
181
|
10993
|
182 if (!(ob = malloc(len + 1)))
|
10990
|
183 return NULL;
|
|
184
|
13234
|
185 if (aimbs_getrawbuf(bs, (guint8 *)ob, len) < len) {
|
10990
|
186 free(ob);
|
|
187 return NULL;
|
|
188 }
|
|
189
|
|
190 ob[len] = '\0';
|
|
191
|
|
192 return ob;
|
|
193 }
|
|
194
|
13239
|
195 faim_internal int aimbs_put8(ByteStream *bs, guint8 v)
|
3952
|
196 {
|
|
197
|
|
198 if (aim_bstream_empty(bs) < 1)
|
|
199 return 0; /* XXX throw an exception */
|
|
200
|
|
201 bs->offset += aimutil_put8(bs->data + bs->offset, v);
|
|
202
|
|
203 return 1;
|
|
204 }
|
|
205
|
13239
|
206 faim_internal int aimbs_put16(ByteStream *bs, guint16 v)
|
3952
|
207 {
|
|
208
|
|
209 if (aim_bstream_empty(bs) < 2)
|
|
210 return 0; /* XXX throw an exception */
|
|
211
|
|
212 bs->offset += aimutil_put16(bs->data + bs->offset, v);
|
|
213
|
|
214 return 2;
|
|
215 }
|
|
216
|
13239
|
217 faim_internal int aimbs_put32(ByteStream *bs, guint32 v)
|
3952
|
218 {
|
|
219
|
|
220 if (aim_bstream_empty(bs) < 4)
|
|
221 return 0; /* XXX throw an exception */
|
|
222
|
|
223 bs->offset += aimutil_put32(bs->data + bs->offset, v);
|
|
224
|
|
225 return 1;
|
|
226 }
|
|
227
|
13239
|
228 faim_internal int aimbs_putle8(ByteStream *bs, guint8 v)
|
3952
|
229 {
|
|
230
|
|
231 if (aim_bstream_empty(bs) < 1)
|
|
232 return 0; /* XXX throw an exception */
|
|
233
|
|
234 bs->offset += aimutil_putle8(bs->data + bs->offset, v);
|
|
235
|
|
236 return 1;
|
|
237 }
|
|
238
|
13239
|
239 faim_internal int aimbs_putle16(ByteStream *bs, guint16 v)
|
3952
|
240 {
|
|
241
|
|
242 if (aim_bstream_empty(bs) < 2)
|
|
243 return 0; /* XXX throw an exception */
|
|
244
|
|
245 bs->offset += aimutil_putle16(bs->data + bs->offset, v);
|
|
246
|
|
247 return 2;
|
|
248 }
|
|
249
|
13239
|
250 faim_internal int aimbs_putle32(ByteStream *bs, guint32 v)
|
3952
|
251 {
|
|
252
|
|
253 if (aim_bstream_empty(bs) < 4)
|
|
254 return 0; /* XXX throw an exception */
|
|
255
|
|
256 bs->offset += aimutil_putle32(bs->data + bs->offset, v);
|
|
257
|
|
258 return 1;
|
|
259 }
|
|
260
|
|
261
|
13239
|
262 faim_internal int aimbs_putraw(ByteStream *bs, const guint8 *v, int len)
|
3952
|
263 {
|
|
264
|
|
265 if (aim_bstream_empty(bs) < len)
|
|
266 return 0; /* XXX throw an exception */
|
|
267
|
|
268 memcpy(bs->data + bs->offset, v, len);
|
|
269 bs->offset += len;
|
|
270
|
|
271 return len;
|
|
272 }
|
|
273
|
13239
|
274 faim_internal int aimbs_putstr(ByteStream *bs, const char *str)
|
10990
|
275 {
|
13234
|
276 return aimbs_putraw(bs, (guint8 *)str, strlen(str));
|
10990
|
277 }
|
|
278
|
13239
|
279 faim_internal int aimbs_putbs(ByteStream *bs, ByteStream *srcbs, int len)
|
3952
|
280 {
|
|
281
|
|
282 if (aim_bstream_empty(srcbs) < len)
|
|
283 return 0; /* XXX throw exception (underrun) */
|
|
284
|
|
285 if (aim_bstream_empty(bs) < len)
|
|
286 return 0; /* XXX throw exception (overflow) */
|
|
287
|
|
288 memcpy(bs->data + bs->offset, srcbs->data + srcbs->offset, len);
|
|
289 bs->offset += len;
|
|
290 srcbs->offset += len;
|
|
291
|
|
292 return len;
|
|
293 }
|