comparison libpurple/protocols/oscar/bstream.c @ 30417:9d028dbd76f4

Change the "length of bstream" data type to be a gsize, since it represents the size of a buffer. There's no reason this should ever be negative. It probably shouldn't be more than 32 bits, either, but gsize still seems more appropriate to me
author Mark Doliner <mark@kingant.net>
date Sun, 15 Aug 2010 08:42:30 +0000
parents ed520e6e972b
children
comparison
equal deleted inserted replaced
30416:6ee64d62c43c 30417:9d028dbd76f4
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 int byte_stream_new(ByteStream *bs, guint32 len) 27 int byte_stream_new(ByteStream *bs, size_t len)
28 { 28 {
29 if (bs == NULL) 29 if (bs == NULL)
30 return -1; 30 return -1;
31 31
32 return byte_stream_init(bs, g_malloc(len), len); 32 return byte_stream_init(bs, g_malloc(len), len);
33 } 33 }
34 34
35 int byte_stream_init(ByteStream *bs, guint8 *data, int len) 35 int byte_stream_init(ByteStream *bs, guint8 *data, size_t len)
36 { 36 {
37 if (bs == NULL) 37 if (bs == NULL)
38 return -1; 38 return -1;
39 39
40 bs->data = data; 40 bs->data = data;
57 int byte_stream_curpos(ByteStream *bs) 57 int byte_stream_curpos(ByteStream *bs)
58 { 58 {
59 return bs->offset; 59 return bs->offset;
60 } 60 }
61 61
62 int byte_stream_setpos(ByteStream *bs, unsigned int off) 62 int byte_stream_setpos(ByteStream *bs, size_t off)
63 { 63 {
64 g_return_val_if_fail(off <= bs->len, -1); 64 g_return_val_if_fail(off <= bs->len, -1);
65 65
66 bs->offset = off; 66 bs->offset = off;
67 return off; 67 return off;
131 131
132 bs->offset += 4; 132 bs->offset += 4;
133 return aimutil_getle32(bs->data + bs->offset - 4); 133 return aimutil_getle32(bs->data + bs->offset - 4);
134 } 134 }
135 135
136 static void byte_stream_getrawbuf_nocheck(ByteStream *bs, guint8 *buf, int len) 136 static void byte_stream_getrawbuf_nocheck(ByteStream *bs, guint8 *buf, size_t len)
137 { 137 {
138 memcpy(buf, bs->data + bs->offset, len); 138 memcpy(buf, bs->data + bs->offset, len);
139 bs->offset += len; 139 bs->offset += len;
140 } 140 }
141 141
142 int byte_stream_getrawbuf(ByteStream *bs, guint8 *buf, int len) 142 int byte_stream_getrawbuf(ByteStream *bs, guint8 *buf, size_t len)
143 { 143 {
144 g_return_val_if_fail(byte_stream_bytes_left(bs) >= len, 0); 144 g_return_val_if_fail(byte_stream_bytes_left(bs) >= len, 0);
145 145
146 byte_stream_getrawbuf_nocheck(bs, buf, len); 146 byte_stream_getrawbuf_nocheck(bs, buf, len);
147 return len; 147 return len;
148 } 148 }
149 149
150 guint8 *byte_stream_getraw(ByteStream *bs, int len) 150 guint8 *byte_stream_getraw(ByteStream *bs, size_t len)
151 { 151 {
152 guint8 *ob; 152 guint8 *ob;
153 153
154 g_return_val_if_fail(byte_stream_bytes_left(bs) >= len, NULL); 154 g_return_val_if_fail(byte_stream_bytes_left(bs) >= len, NULL);
155 155
156 ob = g_malloc(len); 156 ob = g_malloc(len);
157 byte_stream_getrawbuf_nocheck(bs, ob, len); 157 byte_stream_getrawbuf_nocheck(bs, ob, len);
158 return ob; 158 return ob;
159 } 159 }
160 160
161 char *byte_stream_getstr(ByteStream *bs, int len) 161 char *byte_stream_getstr(ByteStream *bs, size_t len)
162 { 162 {
163 char *ob; 163 char *ob;
164 164
165 g_return_val_if_fail(byte_stream_bytes_left(bs) >= len, NULL); 165 g_return_val_if_fail(byte_stream_bytes_left(bs) >= len, NULL);
166 166
217 bs->offset += aimutil_putle32(bs->data + bs->offset, v); 217 bs->offset += aimutil_putle32(bs->data + bs->offset, v);
218 return 1; 218 return 1;
219 } 219 }
220 220
221 221
222 int byte_stream_putraw(ByteStream *bs, const guint8 *v, int len) 222 int byte_stream_putraw(ByteStream *bs, const guint8 *v, size_t len)
223 { 223 {
224 g_return_val_if_fail(byte_stream_bytes_left(bs) >= len, 0); 224 g_return_val_if_fail(byte_stream_bytes_left(bs) >= len, 0);
225 225
226 memcpy(bs->data + bs->offset, v, len); 226 memcpy(bs->data + bs->offset, v, len);
227 bs->offset += len; 227 bs->offset += len;
231 int byte_stream_putstr(ByteStream *bs, const char *str) 231 int byte_stream_putstr(ByteStream *bs, const char *str)
232 { 232 {
233 return byte_stream_putraw(bs, (guint8 *)str, strlen(str)); 233 return byte_stream_putraw(bs, (guint8 *)str, strlen(str));
234 } 234 }
235 235
236 int byte_stream_putbs(ByteStream *bs, ByteStream *srcbs, int len) 236 int byte_stream_putbs(ByteStream *bs, ByteStream *srcbs, size_t len)
237 { 237 {
238 g_return_val_if_fail(byte_stream_bytes_left(srcbs) >= len, 0); 238 g_return_val_if_fail(byte_stream_bytes_left(srcbs) >= len, 0);
239 g_return_val_if_fail(byte_stream_bytes_left(bs) >= len, 0); 239 g_return_val_if_fail(byte_stream_bytes_left(bs) >= len, 0);
240 240
241 memcpy(bs->data + bs->offset, srcbs->data + srcbs->offset, len); 241 memcpy(bs->data + bs->offset, srcbs->data + srcbs->offset, len);