comparison libpurple/protocols/qq/packet_parse.c @ 23050:51dbe83ebbd3

patch-04-tcp-pending
author SHiNE CsyFeK <csyfek@gmail.com>
date Tue, 24 Jun 2008 12:22:40 +0000
parents 190bc4ecf6c3
children 55f986ccbb6a
comparison
equal deleted inserted replaced
23049:190bc4ecf6c3 23050:51dbe83ebbd3
41 #define PARSER_DEBUG 41 #define PARSER_DEBUG
42 #endif 42 #endif
43 43
44 /* read one byte from buf, 44 /* read one byte from buf,
45 * return the number of bytes read if succeeds, otherwise return -1 */ 45 * return the number of bytes read if succeeds, otherwise return -1 */
46 /*
47 gint read_packet_b(guint8 *buf, guint8 **cursor, gint buflen, guint8 *b)
48 {
49 guint8 *b_ship = NULL;
50 #ifdef PARSER_DEBUG
51 purple_debug(PURPLE_DEBUG_INFO, "QQ_DEBUGGER",
52 "[read_b] buf addr: 0x%x\n", (gpointer)buf);
53 #endif
54 if (*cursor <= buf + buflen - sizeof(*b)) {
55 #ifdef PARSER_DEBUG
56 purple_debug(PURPLE_DEBUG_INFO, "QQ_DEBUGGER",
57 "[read_b] *cursor addr: 0x%x, buf expected addr: 0x%x\n",
58 (gpointer)*cursor, (gpointer)(buf + buflen - sizeof(*b)));
59 #endif
60 b_ship = g_new0(guint8, sizeof(guint8));
61 g_memmove(b_ship, *cursor, sizeof(guint8));
62 *b = *b_ship;
63 #ifdef PARSER_DEBUG
64 purple_debug(PURPLE_DEBUG_INFO, "QQ_DEBUGGER",
65 "[read_b] data: 0x%02x->0x%02x\n",
66 **(guint8 **)cursor, *b);
67 #endif
68 *cursor += sizeof(*b);
69 // free
70 g_free(b_ship);
71 b_ship = NULL;
72
73 return sizeof(*b);
74 } else {
75 return -1;
76 }
77 }
78 */
79 gint qq_get8(guint8 *b, guint8 *buf) 46 gint qq_get8(guint8 *b, guint8 *buf)
80 { 47 {
81 guint8 b_dest; 48 guint8 b_dest;
82 memcpy(&b_dest, buf, sizeof(b_dest)); 49 memcpy(&b_dest, buf, sizeof(b_dest));
83 *b = b_dest; 50 *b = b_dest;
84 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][get8] buf %lu\n", (void *)buf); 51 #ifdef PARSER_DEBUG
85 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][get8] b_dest 0x%2x, *b 0x%02x\n", b_dest, *b); 52 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][get8] buf %lu\n", (void *)buf);
53 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][get8] b_dest 0x%2x, *b 0x%02x\n", b_dest, *b);
54 #endif
86 return sizeof(b_dest); 55 return sizeof(b_dest);
87 } 56 }
88 57
89 58
90 /* read two bytes as "guint16" from buf, 59 /* read two bytes as "guint16" from buf,
91 * return the number of bytes read if succeeds, otherwise return -1 */ 60 * return the number of bytes read if succeeds, otherwise return -1 */
92 /*
93 gint read_packet_w(guint8 *buf, guint8 **cursor, gint buflen, guint16 *w)
94 {
95 guint8 *w_ship = NULL;
96 guint16 w_dest;
97 #ifdef PARSER_DEBUG
98 purple_debug(PURPLE_DEBUG_INFO, "QQ_DEBUGGER",
99 "[read_w] buf addr: 0x%x\n", (gpointer)buf);
100 #endif
101 if (*cursor <= buf + buflen - sizeof(*w)) {
102 #ifdef PARSER_DEBUG
103 purple_debug(PURPLE_DEBUG_INFO, "QQ_DEBUGGER",
104 "[read_w] *cursor addr: 0x%x, buf expected addr: 0x%x\n",
105 (gpointer)*cursor, (gpointer)(buf + buflen - sizeof(*w)));
106 #endif
107 // type should match memory buffer
108 w_ship = (guint8 *)g_new0(guint16, 1);
109 // copy bytes into temporary buffer
110 g_memmove(w_ship, *cursor, sizeof(guint16));
111 // type convert and assign value
112 w_dest = *(guint16 *)w_ship;
113 // ntohs
114 *w = g_ntohs(w_dest);
115 #ifdef PARSER_DEBUG
116 purple_debug(PURPLE_DEBUG_INFO, "QQ_DEBUGGER",
117 "[read_w] data: 0x%04x->0x%04x-g_ntohs->0x%04x\n",
118 **(guint16 **)cursor, w_dest, *w);
119 #endif
120 // *cursor goes on
121 *cursor += sizeof(*w);
122
123 // free mem
124 g_free(w_ship);
125 w_ship = NULL;
126
127 return sizeof(*w);
128 } else {
129 return -1;
130 }
131 }
132 */
133 gint qq_get16(guint16 *w, guint8 *buf) 61 gint qq_get16(guint16 *w, guint8 *buf)
134 { 62 {
135 guint16 w_dest; 63 guint16 w_dest;
136 memcpy(&w_dest, buf, sizeof(w_dest)); 64 memcpy(&w_dest, buf, sizeof(w_dest));
137 *w = g_ntohs(w_dest); 65 *w = g_ntohs(w_dest);
138 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][get16] buf %lu\n", (void *)buf); 66 #ifdef PARSER_DEBUG
139 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][get16] w_dest 0x%04x, *w 0x%04x\n", w_dest, *w); 67 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][get16] buf %lu\n", (void *)buf);
68 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][get16] w_dest 0x%04x, *w 0x%04x\n", w_dest, *w);
69 #endif
140 return sizeof(w_dest); 70 return sizeof(w_dest);
141 } 71 }
142 72
143 73
144 /* read four bytes as "guint32" from buf, 74 /* read four bytes as "guint32" from buf,
145 * return the number of bytes read if succeeds, otherwise return -1 */ 75 * return the number of bytes read if succeeds, otherwise return -1 */
146 /*
147 gint read_packet_dw(guint8 *buf, guint8 **cursor, gint buflen, guint32 *dw)
148 {
149 guint8 *dw_ship = NULL;
150 guint32 dw_dest;
151 #ifdef PARSER_DEBUG
152 purple_debug(PURPLE_DEBUG_INFO, "QQ_DEBUGGER",
153 "[read_dw] buf addr: 0x%x\n", (gpointer)buf);
154 #endif
155 if (*cursor <= buf + buflen - sizeof(*dw)) {
156 #ifdef PARSER_DEBUG
157 purple_debug(PURPLE_DEBUG_INFO, "QQ_DEBUGGER",
158 "[read_dw] *cursor addr: 0x%x, buf expected addr: 0x%x\n",
159 (gpointer)*cursor, (gpointer)(buf + buflen - sizeof(*dw)));
160 #endif
161 dw_ship = (guint8 *)g_new0(guint32, 1);
162 g_memmove(dw_ship, *cursor, sizeof(guint32));
163 dw_dest = *(guint32 *)dw_ship;
164 *dw = g_ntohl(dw_dest);
165 #ifdef PARSER_DEBUG
166 purple_debug(PURPLE_DEBUG_INFO, "QQ_DEBUGGER",
167 "[read_dw] data: 0x%08x->0x%08x-g_ntohl->0x%08x\n",
168 **(guint32 **)cursor, dw_dest, *dw);
169 #endif
170 *cursor += sizeof(*dw);
171
172 g_free(dw_ship);
173 dw_ship = NULL;
174
175 return sizeof(*dw);
176 } else {
177 return -1;
178 }
179 }
180 */
181 gint qq_get32(guint32 *dw, guint8 *buf) 76 gint qq_get32(guint32 *dw, guint8 *buf)
182 { 77 {
183 guint32 dw_dest; 78 guint32 dw_dest;
184 memcpy(&dw_dest, buf, sizeof(dw_dest)); 79 memcpy(&dw_dest, buf, sizeof(dw_dest));
185 *dw = g_ntohl(dw_dest); 80 *dw = g_ntohl(dw_dest);
186 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][get32] buf %lu\n", (void *)buf); 81 #ifdef PARSER_DEBUG
187 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][get32] dw_dest 0x%08x, *dw 0x%08x\n", dw_dest, *dw); 82 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][get32] buf %lu\n", (void *)buf);
83 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][get32] dw_dest 0x%08x, *dw 0x%08x\n", dw_dest, *dw);
84 #endif
188 return sizeof(dw_dest); 85 return sizeof(dw_dest);
189 } 86 }
190 87
191 88
192 /* read datalen bytes from buf, 89 /* read datalen bytes from buf,
193 * return the number of bytes read if succeeds, otherwise return -1 */ 90 * return the number of bytes read if succeeds, otherwise return -1 */
194 /*
195 gint read_packet_data(guint8 *buf, guint8 **cursor, gint buflen, guint8 *data, gint datalen) {
196 #ifdef PARSER_DEBUG
197 purple_debug(PURPLE_DEBUG_INFO, "QQ_DEBUGGER",
198 "[read_data] buf addr: 0x%x\n", (gpointer)buf);
199 #endif
200 if (*cursor <= buf + buflen - datalen) {
201 #ifdef PARSER_DEBUG
202 purple_debug(PURPLE_DEBUG_INFO, "QQ_DEBUGGER",
203 "[read_data] *cursor addr: 0x%x, buf expected addr: 0x%x\n",
204 (gpointer)*cursor, (gpointer)(buf + buflen - datalen));
205 #endif
206 g_memmove(data, *cursor, datalen);
207 *cursor += datalen;
208 return datalen;
209 } else {
210 return -1;
211 }
212 }
213 */
214 gint qq_getdata(guint8 *data, gint datalen, guint8 *buf) 91 gint qq_getdata(guint8 *data, gint datalen, guint8 *buf)
215 { 92 {
216 memcpy(data, buf, datalen); 93 memcpy(data, buf, datalen);
217 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][getdata] buf %lu\n", (void *)buf); 94 #ifdef PARSER_DEBUG
95 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][getdata] buf %lu\n", (void *)buf);
96 #endif
218 return datalen; 97 return datalen;
219 } 98 }
220 99
221 100
222 /* read four bytes as "time_t" from buf, 101 /* read four bytes as "time_t" from buf,
223 * return the number of bytes read if succeeds, otherwise return -1 102 * return the number of bytes read if succeeds, otherwise return -1
224 * This function is a wrapper around read_packet_dw() to avoid casting. */ 103 * This function is a wrapper around read_packet_dw() to avoid casting. */
225 /*
226 gint read_packet_time(guint8 *buf, guint8 **cursor, gint buflen, time_t *t)
227 {
228 guint32 time;
229 gint ret = read_packet_dw(buf, cursor, buflen, &time);
230 if (ret != -1 ) {
231 *t = time;
232 }
233 return ret;
234 }
235 */
236 gint qq_getime(time_t *t, guint8 *buf) 104 gint qq_getime(time_t *t, guint8 *buf)
237 { 105 {
238 guint32 dw_dest; 106 guint32 dw_dest;
239 memcpy(&dw_dest, buf, sizeof(dw_dest)); 107 memcpy(&dw_dest, buf, sizeof(dw_dest));
240 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][getime] buf %lu\n", (void *)buf); 108 #ifdef PARSER_DEBUG
241 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][getime] dw_dest before 0x%08x\n", dw_dest); 109 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][getime] buf %lu\n", (void *)buf);
110 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][getime] dw_dest before 0x%08x\n", dw_dest);
111 #endif
242 dw_dest = g_ntohl(dw_dest); 112 dw_dest = g_ntohl(dw_dest);
243 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][getime] dw_dest after 0x%08x\n", dw_dest); 113 #ifdef PARSER_DEBUG
114 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][getime] dw_dest after 0x%08x\n", dw_dest);
115 #endif
244 memcpy(t, &dw_dest, sizeof(dw_dest)); 116 memcpy(t, &dw_dest, sizeof(dw_dest));
245 return sizeof(dw_dest); 117 return sizeof(dw_dest);
246 } 118 }
247 119
248 /*------------------------------------------------PUT------------------------------------------------*/ 120 /*------------------------------------------------PUT------------------------------------------------*/
249 /* pack one byte into buf 121 /* pack one byte into buf
250 * return the number of bytes packed, otherwise return -1 */ 122 * return the number of bytes packed, otherwise return -1 */
251 /*
252 gint create_packet_b(guint8 *buf, guint8 **cursor, guint8 b)
253 {
254 guint8 b_dest;
255 #ifdef PARSER_DEBUG
256 // show me the address!
257 purple_debug(PURPLE_DEBUG_INFO, "QQ_DEBUGGER",
258 "[create_b] buf addr: 0x%x\n", (gpointer)buf);
259 #endif
260 // using gpointer is more safe, s3e, 20070704
261 if ((gpointer)*cursor <= (gpointer)(buf + MAX_PACKET_SIZE - sizeof(guint8))) {
262 #ifdef PARSER_DEBUG
263 purple_debug(PURPLE_DEBUG_INFO, "QQ_DEBUGGER",
264 "[create_b] *cursor addr: 0x%x, buf expected addr: 0x%x\n",
265 (gpointer)*cursor,
266 (gpointer)(buf + MAX_PACKET_SIZE - sizeof(guint8)));
267 #endif
268 b_dest = b;
269 g_memmove(*cursor, &b_dest, sizeof(guint8));
270 #ifdef PARSER_DEBUG
271 // show data
272 purple_debug(PURPLE_DEBUG_INFO, "QQ_DEBUGGER",
273 "[create_b] data: 0x%02x->0x%02x\n", b, **(guint8 **)cursor);
274 #endif
275 *cursor += sizeof(guint8);
276 return sizeof(guint8);
277 } else {
278 return -1;
279 }
280 }
281 */
282 gint qq_put8(guint8 *buf, guint8 b) 123 gint qq_put8(guint8 *buf, guint8 b)
283 { 124 {
284 memcpy(buf, &b, sizeof(b)); 125 memcpy(buf, &b, sizeof(b));
285 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][put8] buf %lu\n", (void *)buf); 126 #ifdef PARSER_DEBUG
286 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][put8] b 0x%02x\n", b); 127 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][put8] buf %lu\n", (void *)buf);
128 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][put8] b 0x%02x\n", b);
129 #endif
287 return sizeof(b); 130 return sizeof(b);
288 } 131 }
289 132
290 133
291 /* pack two bytes as "guint16" into buf 134 /* pack two bytes as "guint16" into buf
292 * return the number of bytes packed, otherwise return -1 */ 135 * return the number of bytes packed, otherwise return -1 */
293 /*
294 gint create_packet_w(guint8 *buf, guint8 **cursor, guint16 w)
295 {
296 guint16 w_dest;
297 guint8 *w_ship = NULL;
298 #ifdef PARSER_DEBUG
299 purple_debug(PURPLE_DEBUG_INFO, "QQ_DEBUGGER",
300 "[create_w] buf addr: 0x%x\n", (gpointer)buf);
301 #endif
302 if ((gpointer)*cursor <= (gpointer)(buf + MAX_PACKET_SIZE - sizeof(guint16))) {
303 #ifdef PARSER_DEBUG
304 purple_debug(PURPLE_DEBUG_INFO, "QQ_DEBUGGER",
305 "[create_w] *cursor addr: 0x%x, buf expected addr: 0x%x\n",
306 (gpointer)*cursor,
307 (gpointer)(buf + MAX_PACKET_SIZE - sizeof(guint16)));
308 #endif
309 // obscure bugs found by ccpaging, patches from him.
310 // similar bugs have been fixed, s3e, 20070710
311 w_dest = g_htons(w);
312 w_ship = (guint8 *)&w_dest;
313 g_memmove(*cursor, w_ship, sizeof(guint16));
314 #ifdef PARSER_DEBUG
315 purple_debug(PURPLE_DEBUG_INFO, "QQ_DEBUGGER",
316 "[create_w] data: 0x%04x-g_htons->0x%04x->0x%04x\n",
317 w, w_dest, **(guint16 **)cursor);
318 #endif
319 *cursor += sizeof(guint16);
320 return sizeof(guint16);
321 } else {
322 return -1;
323 }
324 }
325 */
326 gint qq_put16(guint8 *buf, guint16 w) 136 gint qq_put16(guint8 *buf, guint16 w)
327 { 137 {
328 guint16 w_porter; 138 guint16 w_porter;
329 w_porter = g_htons(w); 139 w_porter = g_htons(w);
330 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][put16] buf %lu\n", (void *)buf); 140 #ifdef PARSER_DEBUG
331 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][put16] w 0x%04x, w_porter 0x%04x\n", w, w_porter); 141 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][put16] buf %lu\n", (void *)buf);
142 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][put16] w 0x%04x, w_porter 0x%04x\n", w, w_porter);
143 #endif
332 memcpy(buf, &w_porter, sizeof(w_porter)); 144 memcpy(buf, &w_porter, sizeof(w_porter));
333 return sizeof(w_porter); 145 return sizeof(w_porter);
334 } 146 }
335 147
336 148
337 /* pack four bytes as "guint32" into buf 149 /* pack four bytes as "guint32" into buf
338 * return the number of bytes packed, otherwise return -1 */ 150 * return the number of bytes packed, otherwise return -1 */
339 /*
340 gint create_packet_dw(guint8 *buf, guint8 **cursor, guint32 dw)
341 {
342 guint32 dw_dest;
343 guint8 *dw_ship = NULL;
344 #ifdef PARSER_DEBUG
345 purple_debug(PURPLE_DEBUG_INFO, "QQ_DEBUGGER", "[create_dw] buf addr: 0x%x\n", (gpointer)buf);
346 #endif
347 if ((gpointer)*cursor <= (gpointer)(buf + MAX_PACKET_SIZE - sizeof(guint32))) {
348 #ifdef PARSER_DEBUG
349 purple_debug(PURPLE_DEBUG_INFO, "QQ_DEBUGGER",
350 "[create_dw] *cursor addr: 0x%x, buf expected addr: 0x%x\n",
351 (gpointer)*cursor,
352 (gpointer)(buf + MAX_PACKET_SIZE -sizeof(guint32)));
353 #endif
354 dw_dest = g_htonl(dw);
355 dw_ship = (guint8 *)&dw_dest;
356 g_memmove(*cursor, dw_ship, sizeof(guint32));
357 #ifdef PARSER_DEBUG
358 purple_debug(PURPLE_DEBUG_INFO, "QQ_DEBUGGER",
359 "[create_dw] data: 0x%08x-g_htonl->0x%08x->0x%08x\n",
360 dw, dw_dest, **(guint32 **)cursor);
361 #endif
362 *cursor += sizeof(guint32);
363 return sizeof(guint32);
364 } else {
365 return -1;
366 }
367 }
368 */
369 gint qq_put32(guint8 *buf, guint32 dw) 151 gint qq_put32(guint8 *buf, guint32 dw)
370 { 152 {
371 guint32 dw_porter; 153 guint32 dw_porter;
372 dw_porter = g_htonl(dw); 154 dw_porter = g_htonl(dw);
373 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][put32] buf %lu\n", (void *)buf); 155 #ifdef PARSER_DEBUG
374 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][put32] dw 0x%08x, dw_porter 0x%08x\n", dw, dw_porter); 156 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][put32] buf %lu\n", (void *)buf);
157 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][put32] dw 0x%08x, dw_porter 0x%08x\n", dw, dw_porter);
158 #endif
375 memcpy(buf, &dw_porter, sizeof(dw_porter)); 159 memcpy(buf, &dw_porter, sizeof(dw_porter));
376 return sizeof(dw_porter); 160 return sizeof(dw_porter);
377 } 161 }
378 162
379 163
380 /* pack datalen bytes into buf 164 /* pack datalen bytes into buf
381 * return the number of bytes packed, otherwise return -1 */ 165 * return the number of bytes packed, otherwise return -1 */
382 /* 166 gint qq_putdata(guint8 *buf, const guint8 *data, const int datalen)
383 gint create_packet_data(guint8 *buf, guint8 **cursor, guint8 *data, gint datalen)
384 {
385 #ifdef PARSER_DEBUG
386 purple_debug(PURPLE_DEBUG_INFO, "QQ_DEBUGGER",
387 "[create_data] buf addr: 0x%x\n", (gpointer)buf);
388 #endif
389 if ((gpointer)*cursor <= (gpointer)(buf + MAX_PACKET_SIZE - datalen)) {
390 #ifdef PARSER_DEBUG
391 purple_debug(PURPLE_DEBUG_INFO, "QQ_DEBUGGER",
392 "[create_data] *cursor addr: 0x%x, buf expected addr: 0x%x\n",
393 (gpointer)*cursor,
394 (gpointer)(buf + MAX_PACKET_SIZE - datalen));
395 #endif
396 g_memmove(*cursor, data, datalen);
397 *cursor += datalen;
398 return datalen;
399 } else {
400 return -1;
401 }
402 }
403 */
404 gint qq_putdata(guint8 *buf, guint8 *data, const int datalen)
405 { 167 {
406 memcpy(buf, data, datalen); 168 memcpy(buf, data, datalen);
169 #ifdef PARSER_DEBUG
407 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][putdata] buf %lu\n", (void *)buf); 170 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[DBG][putdata] buf %lu\n", (void *)buf);
171 #endif
408 return datalen; 172 return datalen;
409 } 173 }
410
411