comparison src/protocols/oscar/bstream.c @ 13239:f260d319bbbc

[gaim-migrate @ 15605] Renaming a bunch of structs and typedefs to use the same naming scheme as the rest of Gaim committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Sun, 12 Feb 2006 16:02:05 +0000
parents f2431a7e33aa
children 6519aeb66b31
comparison
equal deleted inserted replaced
13238:1e855032d7bc 13239:f260d319bbbc
22 * This file contains all functions needed to use bstreams. 22 * This file contains all functions needed to use bstreams.
23 */ 23 */
24 24
25 #include "oscar.h" 25 #include "oscar.h"
26 26
27 faim_internal int aim_bstream_init(aim_bstream_t *bs, guint8 *data, int len) 27 faim_internal int aim_bstream_init(ByteStream *bs, guint8 *data, int len)
28 { 28 {
29 29
30 if (!bs) 30 if (!bs)
31 return -1; 31 return -1;
32 32
35 bs->offset = 0; 35 bs->offset = 0;
36 36
37 return 0; 37 return 0;
38 } 38 }
39 39
40 faim_internal int aim_bstream_empty(aim_bstream_t *bs) 40 faim_internal int aim_bstream_empty(ByteStream *bs)
41 { 41 {
42 return bs->len - bs->offset; 42 return bs->len - bs->offset;
43 } 43 }
44 44
45 faim_internal int aim_bstream_curpos(aim_bstream_t *bs) 45 faim_internal int aim_bstream_curpos(ByteStream *bs)
46 { 46 {
47 return bs->offset; 47 return bs->offset;
48 } 48 }
49 49
50 faim_internal int aim_bstream_setpos(aim_bstream_t *bs, unsigned int off) 50 faim_internal int aim_bstream_setpos(ByteStream *bs, unsigned int off)
51 { 51 {
52 52
53 if (off > bs->len) 53 if (off > bs->len)
54 return -1; 54 return -1;
55 55
56 bs->offset = off; 56 bs->offset = off;
57 57
58 return off; 58 return off;
59 } 59 }
60 60
61 faim_internal void aim_bstream_rewind(aim_bstream_t *bs) 61 faim_internal void aim_bstream_rewind(ByteStream *bs)
62 { 62 {
63 63
64 aim_bstream_setpos(bs, 0); 64 aim_bstream_setpos(bs, 0);
65 65
66 return; 66 return;
69 /* 69 /*
70 * N can be negative, which can be used for going backwards 70 * N can be negative, which can be used for going backwards
71 * in a bstream. I'm not sure if libfaim actually does 71 * in a bstream. I'm not sure if libfaim actually does
72 * this anywhere... 72 * this anywhere...
73 */ 73 */
74 faim_internal int aim_bstream_advance(aim_bstream_t *bs, int n) 74 faim_internal int aim_bstream_advance(ByteStream *bs, int n)
75 { 75 {
76 76
77 if ((aim_bstream_curpos(bs) + n < 0) || (aim_bstream_empty(bs) < n)) 77 if ((aim_bstream_curpos(bs) + n < 0) || (aim_bstream_empty(bs) < n))
78 return 0; /* XXX throw an exception */ 78 return 0; /* XXX throw an exception */
79 79
80 bs->offset += n; 80 bs->offset += n;
81 81
82 return n; 82 return n;
83 } 83 }
84 84
85 faim_internal guint8 aimbs_get8(aim_bstream_t *bs) 85 faim_internal guint8 aimbs_get8(ByteStream *bs)
86 { 86 {
87 87
88 if (aim_bstream_empty(bs) < 1) 88 if (aim_bstream_empty(bs) < 1)
89 return 0; /* XXX throw an exception */ 89 return 0; /* XXX throw an exception */
90 90
91 bs->offset++; 91 bs->offset++;
92 92
93 return aimutil_get8(bs->data + bs->offset - 1); 93 return aimutil_get8(bs->data + bs->offset - 1);
94 } 94 }
95 95
96 faim_internal guint16 aimbs_get16(aim_bstream_t *bs) 96 faim_internal guint16 aimbs_get16(ByteStream *bs)
97 { 97 {
98 98
99 if (aim_bstream_empty(bs) < 2) 99 if (aim_bstream_empty(bs) < 2)
100 return 0; /* XXX throw an exception */ 100 return 0; /* XXX throw an exception */
101 101
102 bs->offset += 2; 102 bs->offset += 2;
103 103
104 return aimutil_get16(bs->data + bs->offset - 2); 104 return aimutil_get16(bs->data + bs->offset - 2);
105 } 105 }
106 106
107 faim_internal guint32 aimbs_get32(aim_bstream_t *bs) 107 faim_internal guint32 aimbs_get32(ByteStream *bs)
108 { 108 {
109 109
110 if (aim_bstream_empty(bs) < 4) 110 if (aim_bstream_empty(bs) < 4)
111 return 0; /* XXX throw an exception */ 111 return 0; /* XXX throw an exception */
112 112
113 bs->offset += 4; 113 bs->offset += 4;
114 114
115 return aimutil_get32(bs->data + bs->offset - 4); 115 return aimutil_get32(bs->data + bs->offset - 4);
116 } 116 }
117 117
118 faim_internal guint8 aimbs_getle8(aim_bstream_t *bs) 118 faim_internal guint8 aimbs_getle8(ByteStream *bs)
119 { 119 {
120 120
121 if (aim_bstream_empty(bs) < 1) 121 if (aim_bstream_empty(bs) < 1)
122 return 0; /* XXX throw an exception */ 122 return 0; /* XXX throw an exception */
123 123
124 bs->offset++; 124 bs->offset++;
125 125
126 return aimutil_getle8(bs->data + bs->offset - 1); 126 return aimutil_getle8(bs->data + bs->offset - 1);
127 } 127 }
128 128
129 faim_internal guint16 aimbs_getle16(aim_bstream_t *bs) 129 faim_internal guint16 aimbs_getle16(ByteStream *bs)
130 { 130 {
131 131
132 if (aim_bstream_empty(bs) < 2) 132 if (aim_bstream_empty(bs) < 2)
133 return 0; /* XXX throw an exception */ 133 return 0; /* XXX throw an exception */
134 134
135 bs->offset += 2; 135 bs->offset += 2;
136 136
137 return aimutil_getle16(bs->data + bs->offset - 2); 137 return aimutil_getle16(bs->data + bs->offset - 2);
138 } 138 }
139 139
140 faim_internal guint32 aimbs_getle32(aim_bstream_t *bs) 140 faim_internal guint32 aimbs_getle32(ByteStream *bs)
141 { 141 {
142 142
143 if (aim_bstream_empty(bs) < 4) 143 if (aim_bstream_empty(bs) < 4)
144 return 0; /* XXX throw an exception */ 144 return 0; /* XXX throw an exception */
145 145
146 bs->offset += 4; 146 bs->offset += 4;
147 147
148 return aimutil_getle32(bs->data + bs->offset - 4); 148 return aimutil_getle32(bs->data + bs->offset - 4);
149 } 149 }
150 150
151 faim_internal int aimbs_getrawbuf(aim_bstream_t *bs, guint8 *buf, int len) 151 faim_internal int aimbs_getrawbuf(ByteStream *bs, guint8 *buf, int len)
152 { 152 {
153 153
154 if (aim_bstream_empty(bs) < len) 154 if (aim_bstream_empty(bs) < len)
155 return 0; 155 return 0;
156 156
158 bs->offset += len; 158 bs->offset += len;
159 159
160 return len; 160 return len;
161 } 161 }
162 162
163 faim_internal guint8 *aimbs_getraw(aim_bstream_t *bs, int len) 163 faim_internal guint8 *aimbs_getraw(ByteStream *bs, int len)
164 { 164 {
165 guint8 *ob; 165 guint8 *ob;
166 166
167 if (!(ob = malloc(len))) 167 if (!(ob = malloc(len)))
168 return NULL; 168 return NULL;
173 } 173 }
174 174
175 return ob; 175 return ob;
176 } 176 }
177 177
178 faim_internal char *aimbs_getstr(aim_bstream_t *bs, int len) 178 faim_internal char *aimbs_getstr(ByteStream *bs, int len)
179 { 179 {
180 char *ob; 180 char *ob;
181 181
182 if (!(ob = malloc(len + 1))) 182 if (!(ob = malloc(len + 1)))
183 return NULL; 183 return NULL;
190 ob[len] = '\0'; 190 ob[len] = '\0';
191 191
192 return ob; 192 return ob;
193 } 193 }
194 194
195 faim_internal int aimbs_put8(aim_bstream_t *bs, guint8 v) 195 faim_internal int aimbs_put8(ByteStream *bs, guint8 v)
196 { 196 {
197 197
198 if (aim_bstream_empty(bs) < 1) 198 if (aim_bstream_empty(bs) < 1)
199 return 0; /* XXX throw an exception */ 199 return 0; /* XXX throw an exception */
200 200
201 bs->offset += aimutil_put8(bs->data + bs->offset, v); 201 bs->offset += aimutil_put8(bs->data + bs->offset, v);
202 202
203 return 1; 203 return 1;
204 } 204 }
205 205
206 faim_internal int aimbs_put16(aim_bstream_t *bs, guint16 v) 206 faim_internal int aimbs_put16(ByteStream *bs, guint16 v)
207 { 207 {
208 208
209 if (aim_bstream_empty(bs) < 2) 209 if (aim_bstream_empty(bs) < 2)
210 return 0; /* XXX throw an exception */ 210 return 0; /* XXX throw an exception */
211 211
212 bs->offset += aimutil_put16(bs->data + bs->offset, v); 212 bs->offset += aimutil_put16(bs->data + bs->offset, v);
213 213
214 return 2; 214 return 2;
215 } 215 }
216 216
217 faim_internal int aimbs_put32(aim_bstream_t *bs, guint32 v) 217 faim_internal int aimbs_put32(ByteStream *bs, guint32 v)
218 { 218 {
219 219
220 if (aim_bstream_empty(bs) < 4) 220 if (aim_bstream_empty(bs) < 4)
221 return 0; /* XXX throw an exception */ 221 return 0; /* XXX throw an exception */
222 222
223 bs->offset += aimutil_put32(bs->data + bs->offset, v); 223 bs->offset += aimutil_put32(bs->data + bs->offset, v);
224 224
225 return 1; 225 return 1;
226 } 226 }
227 227
228 faim_internal int aimbs_putle8(aim_bstream_t *bs, guint8 v) 228 faim_internal int aimbs_putle8(ByteStream *bs, guint8 v)
229 { 229 {
230 230
231 if (aim_bstream_empty(bs) < 1) 231 if (aim_bstream_empty(bs) < 1)
232 return 0; /* XXX throw an exception */ 232 return 0; /* XXX throw an exception */
233 233
234 bs->offset += aimutil_putle8(bs->data + bs->offset, v); 234 bs->offset += aimutil_putle8(bs->data + bs->offset, v);
235 235
236 return 1; 236 return 1;
237 } 237 }
238 238
239 faim_internal int aimbs_putle16(aim_bstream_t *bs, guint16 v) 239 faim_internal int aimbs_putle16(ByteStream *bs, guint16 v)
240 { 240 {
241 241
242 if (aim_bstream_empty(bs) < 2) 242 if (aim_bstream_empty(bs) < 2)
243 return 0; /* XXX throw an exception */ 243 return 0; /* XXX throw an exception */
244 244
245 bs->offset += aimutil_putle16(bs->data + bs->offset, v); 245 bs->offset += aimutil_putle16(bs->data + bs->offset, v);
246 246
247 return 2; 247 return 2;
248 } 248 }
249 249
250 faim_internal int aimbs_putle32(aim_bstream_t *bs, guint32 v) 250 faim_internal int aimbs_putle32(ByteStream *bs, guint32 v)
251 { 251 {
252 252
253 if (aim_bstream_empty(bs) < 4) 253 if (aim_bstream_empty(bs) < 4)
254 return 0; /* XXX throw an exception */ 254 return 0; /* XXX throw an exception */
255 255
257 257
258 return 1; 258 return 1;
259 } 259 }
260 260
261 261
262 faim_internal int aimbs_putraw(aim_bstream_t *bs, const guint8 *v, int len) 262 faim_internal int aimbs_putraw(ByteStream *bs, const guint8 *v, int len)
263 { 263 {
264 264
265 if (aim_bstream_empty(bs) < len) 265 if (aim_bstream_empty(bs) < len)
266 return 0; /* XXX throw an exception */ 266 return 0; /* XXX throw an exception */
267 267
269 bs->offset += len; 269 bs->offset += len;
270 270
271 return len; 271 return len;
272 } 272 }
273 273
274 faim_internal int aimbs_putstr(aim_bstream_t *bs, const char *str) 274 faim_internal int aimbs_putstr(ByteStream *bs, const char *str)
275 { 275 {
276 return aimbs_putraw(bs, (guint8 *)str, strlen(str)); 276 return aimbs_putraw(bs, (guint8 *)str, strlen(str));
277 } 277 }
278 278
279 faim_internal int aimbs_putbs(aim_bstream_t *bs, aim_bstream_t *srcbs, int len) 279 faim_internal int aimbs_putbs(ByteStream *bs, ByteStream *srcbs, int len)
280 { 280 {
281 281
282 if (aim_bstream_empty(srcbs) < len) 282 if (aim_bstream_empty(srcbs) < len)
283 return 0; /* XXX throw exception (underrun) */ 283 return 0; /* XXX throw exception (underrun) */
284 284