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
|
13592
|
27 int byte_stream_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
|
13592
|
40 int byte_stream_empty(ByteStream *bs)
|
3952
|
41 {
|
|
42 return bs->len - bs->offset;
|
|
43 }
|
|
44
|
13592
|
45 int byte_stream_curpos(ByteStream *bs)
|
3952
|
46 {
|
|
47 return bs->offset;
|
|
48 }
|
|
49
|
13592
|
50 int byte_stream_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
|
13592
|
61 void byte_stream_rewind(ByteStream *bs)
|
3952
|
62 {
|
|
63
|
13592
|
64 byte_stream_setpos(bs, 0);
|
3952
|
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 */
|
13592
|
74 int byte_stream_advance(ByteStream *bs, int n)
|
3952
|
75 {
|
|
76
|
13592
|
77 if ((byte_stream_curpos(bs) + n < 0) || (byte_stream_empty(bs) < n))
|
3952
|
78 return 0; /* XXX throw an exception */
|
|
79
|
|
80 bs->offset += n;
|
|
81
|
|
82 return n;
|
|
83 }
|
|
84
|
13592
|
85 guint8 byte_stream_get8(ByteStream *bs)
|
3952
|
86 {
|
10464
|
87
|
13592
|
88 if (byte_stream_empty(bs) < 1)
|
3952
|
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
|
13592
|
96 guint16 byte_stream_get16(ByteStream *bs)
|
3952
|
97 {
|
10464
|
98
|
13592
|
99 if (byte_stream_empty(bs) < 2)
|
3952
|
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
|
13592
|
107 guint32 byte_stream_get32(ByteStream *bs)
|
3952
|
108 {
|
10464
|
109
|
13592
|
110 if (byte_stream_empty(bs) < 4)
|
3952
|
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
|
13592
|
118 guint8 byte_stream_getle8(ByteStream *bs)
|
3952
|
119 {
|
10464
|
120
|
13592
|
121 if (byte_stream_empty(bs) < 1)
|
3952
|
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
|
13592
|
129 guint16 byte_stream_getle16(ByteStream *bs)
|
3952
|
130 {
|
10464
|
131
|
13592
|
132 if (byte_stream_empty(bs) < 2)
|
3952
|
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
|
13592
|
140 guint32 byte_stream_getle32(ByteStream *bs)
|
3952
|
141 {
|
10464
|
142
|
13592
|
143 if (byte_stream_empty(bs) < 4)
|
3952
|
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
|
13592
|
151 int byte_stream_getrawbuf(ByteStream *bs, guint8 *buf, int len)
|
10990
|
152 {
|
|
153
|
13592
|
154 if (byte_stream_empty(bs) < len)
|
10990
|
155 return 0;
|
|
156
|
|
157 memcpy(buf, bs->data + bs->offset, len);
|
|
158 bs->offset += len;
|
|
159
|
|
160 return len;
|
|
161 }
|
|
162
|
13592
|
163 guint8 *byte_stream_getraw(ByteStream *bs, int len)
|
10990
|
164 {
|
13234
|
165 guint8 *ob;
|
10990
|
166
|
13592
|
167 ob = malloc(len);
|
10990
|
168
|
13592
|
169 if (byte_stream_getrawbuf(bs, ob, len) < len) {
|
10990
|
170 free(ob);
|
|
171 return NULL;
|
|
172 }
|
|
173
|
|
174 return ob;
|
|
175 }
|
|
176
|
13592
|
177 char *byte_stream_getstr(ByteStream *bs, int len)
|
10990
|
178 {
|
|
179 char *ob;
|
|
180
|
13592
|
181 ob = malloc(len + 1);
|
10990
|
182
|
13592
|
183 if (byte_stream_getrawbuf(bs, (guint8 *)ob, len) < len) {
|
10990
|
184 free(ob);
|
|
185 return NULL;
|
|
186 }
|
|
187
|
|
188 ob[len] = '\0';
|
|
189
|
|
190 return ob;
|
|
191 }
|
|
192
|
13592
|
193 int byte_stream_put8(ByteStream *bs, guint8 v)
|
3952
|
194 {
|
|
195
|
13592
|
196 if (byte_stream_empty(bs) < 1)
|
3952
|
197 return 0; /* XXX throw an exception */
|
|
198
|
|
199 bs->offset += aimutil_put8(bs->data + bs->offset, v);
|
|
200
|
|
201 return 1;
|
|
202 }
|
|
203
|
13592
|
204 int byte_stream_put16(ByteStream *bs, guint16 v)
|
3952
|
205 {
|
|
206
|
13592
|
207 if (byte_stream_empty(bs) < 2)
|
3952
|
208 return 0; /* XXX throw an exception */
|
|
209
|
|
210 bs->offset += aimutil_put16(bs->data + bs->offset, v);
|
|
211
|
|
212 return 2;
|
|
213 }
|
|
214
|
13592
|
215 int byte_stream_put32(ByteStream *bs, guint32 v)
|
3952
|
216 {
|
|
217
|
13592
|
218 if (byte_stream_empty(bs) < 4)
|
3952
|
219 return 0; /* XXX throw an exception */
|
|
220
|
|
221 bs->offset += aimutil_put32(bs->data + bs->offset, v);
|
|
222
|
|
223 return 1;
|
|
224 }
|
|
225
|
13592
|
226 int byte_stream_putle8(ByteStream *bs, guint8 v)
|
3952
|
227 {
|
|
228
|
13592
|
229 if (byte_stream_empty(bs) < 1)
|
3952
|
230 return 0; /* XXX throw an exception */
|
|
231
|
|
232 bs->offset += aimutil_putle8(bs->data + bs->offset, v);
|
|
233
|
|
234 return 1;
|
|
235 }
|
|
236
|
13592
|
237 int byte_stream_putle16(ByteStream *bs, guint16 v)
|
3952
|
238 {
|
|
239
|
13592
|
240 if (byte_stream_empty(bs) < 2)
|
3952
|
241 return 0; /* XXX throw an exception */
|
|
242
|
|
243 bs->offset += aimutil_putle16(bs->data + bs->offset, v);
|
|
244
|
|
245 return 2;
|
|
246 }
|
|
247
|
13592
|
248 int byte_stream_putle32(ByteStream *bs, guint32 v)
|
3952
|
249 {
|
|
250
|
13592
|
251 if (byte_stream_empty(bs) < 4)
|
3952
|
252 return 0; /* XXX throw an exception */
|
|
253
|
|
254 bs->offset += aimutil_putle32(bs->data + bs->offset, v);
|
|
255
|
|
256 return 1;
|
|
257 }
|
|
258
|
|
259
|
13592
|
260 int byte_stream_putraw(ByteStream *bs, const guint8 *v, int len)
|
3952
|
261 {
|
|
262
|
13592
|
263 if (byte_stream_empty(bs) < len)
|
3952
|
264 return 0; /* XXX throw an exception */
|
|
265
|
|
266 memcpy(bs->data + bs->offset, v, len);
|
|
267 bs->offset += len;
|
|
268
|
|
269 return len;
|
|
270 }
|
|
271
|
13592
|
272 int byte_stream_putstr(ByteStream *bs, const char *str)
|
10990
|
273 {
|
13592
|
274 return byte_stream_putraw(bs, (guint8 *)str, strlen(str));
|
10990
|
275 }
|
|
276
|
13592
|
277 int byte_stream_putbs(ByteStream *bs, ByteStream *srcbs, int len)
|
3952
|
278 {
|
|
279
|
13592
|
280 if (byte_stream_empty(srcbs) < len)
|
3952
|
281 return 0; /* XXX throw exception (underrun) */
|
|
282
|
13592
|
283 if (byte_stream_empty(bs) < len)
|
3952
|
284 return 0; /* XXX throw exception (overflow) */
|
|
285
|
|
286 memcpy(bs->data + bs->offset, srcbs->data + srcbs->offset, len);
|
|
287 bs->offset += len;
|
|
288 srcbs->offset += len;
|
|
289
|
|
290 return len;
|
|
291 }
|