6
* Purple is the legal property of its developers, whose names are too numerous
7
* to list here. Please refer to the COPYRIGHT file distributed with this
10
* This program is free software; you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation; either version 2 of the License, or
13
* (at your option) any later version.
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
20
* You should have received a copy of the GNU General Public License
21
* along with this program; if not, write to the Free Software
22
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
27
#include "packet_parse.h"
30
/*------------------------------------------------PUT------------------------------------------------*/
33
* 1, in these functions, 'b' stands for byte, 'w' stands for word, 'dw' stands for double word.
34
* 2, we use '*cursor' and 'buf' as two addresses to calculate the length.
35
* 3, change 'undef' to 'define' to get more info about the packet parsing. */
39
/* read one byte from buf,
40
* return the number of bytes read if succeeds, otherwise return -1 */
41
gint qq_get8(guint8 *b, guint8 *buf)
44
memcpy(&b_dest, buf, sizeof(b_dest));
47
purple_debug_info("QQ", "[DBG][get8] buf %p\n", (void *)buf);
48
purple_debug_info("QQ", "[DBG][get8] b_dest 0x%2x, *b 0x%02x\n", b_dest, *b);
50
return sizeof(b_dest);
54
/* read two bytes as "guint16" from buf,
55
* return the number of bytes read if succeeds, otherwise return -1 */
56
gint qq_get16(guint16 *w, guint8 *buf)
59
memcpy(&w_dest, buf, sizeof(w_dest));
62
purple_debug_info("QQ", "[DBG][get16] buf %p\n", (void *)buf);
63
purple_debug_info("QQ", "[DBG][get16] w_dest 0x%04x, *w 0x%04x\n", w_dest, *w);
65
return sizeof(w_dest);
68
/* read four bytes as "guint32" from buf,
69
* return the number of bytes read if succeeds, otherwise return -1 */
70
gint qq_get32(guint32 *dw, guint8 *buf)
73
memcpy(&dw_dest, buf, sizeof(dw_dest));
74
*dw = g_ntohl(dw_dest);
76
purple_debug_info("QQ", "[DBG][get32] buf %p\n", (void *)buf);
77
purple_debug_info("QQ", "[DBG][get32] dw_dest 0x%08x, *dw 0x%08x\n", dw_dest, *dw);
79
return sizeof(dw_dest);
82
gint qq_getIP(struct in_addr *ip, guint8 *buf)
84
memcpy(ip, buf, sizeof(struct in_addr));
85
return sizeof(struct in_addr);
88
/* read datalen bytes from buf,
89
* return the number of bytes read if succeeds, otherwise return -1 */
90
gint qq_getdata(guint8 *data, gint datalen, guint8 *buf)
92
memcpy(data, buf, datalen);
94
purple_debug_info("QQ", "[DBG][getdata] buf %p\n", (void *)buf);
100
/* read four bytes as "time_t" from buf,
101
* return the number of bytes read if succeeds, otherwise return -1
102
* This function is a wrapper around read_packet_dw() to avoid casting. */
103
gint qq_getime(time_t *t, guint8 *buf)
106
memcpy(&dw_dest, buf, sizeof(dw_dest));
108
purple_debug_info("QQ", "[DBG][getime] buf %p\n", (void *)buf);
109
purple_debug_info("QQ", "[DBG][getime] dw_dest before 0x%08x\n", dw_dest);
111
dw_dest = g_ntohl(dw_dest);
113
purple_debug_info("QQ", "[DBG][getime] dw_dest after 0x%08x\n", dw_dest);
115
memcpy(t, &dw_dest, sizeof(dw_dest));
116
return sizeof(dw_dest);
119
/*------------------------------------------------PUT------------------------------------------------*/
120
/* pack one byte into buf
121
* return the number of bytes packed, otherwise return -1 */
122
gint qq_put8(guint8 *buf, guint8 b)
124
memcpy(buf, &b, sizeof(b));
126
purple_debug_info("QQ", "[DBG][put8] buf %p\n", (void *)buf);
127
purple_debug_info("QQ", "[DBG][put8] b 0x%02x\n", b);
133
/* pack two bytes as "guint16" into buf
134
* return the number of bytes packed, otherwise return -1 */
135
gint qq_put16(guint8 *buf, guint16 w)
138
w_porter = g_htons(w);
140
purple_debug_info("QQ", "[DBG][put16] buf %p\n", (void *)buf);
141
purple_debug_info("QQ", "[DBG][put16] w 0x%04x, w_porter 0x%04x\n", w, w_porter);
143
memcpy(buf, &w_porter, sizeof(w_porter));
144
return sizeof(w_porter);
148
/* pack four bytes as "guint32" into buf
149
* return the number of bytes packed, otherwise return -1 */
150
gint qq_put32(guint8 *buf, guint32 dw)
153
dw_porter = g_htonl(dw);
155
purple_debug_info("QQ", "[DBG][put32] buf %p\n", (void *)buf);
156
purple_debug_info("QQ", "[DBG][put32] dw 0x%08x, dw_porter 0x%08x\n", dw, dw_porter);
158
memcpy(buf, &dw_porter, sizeof(dw_porter));
159
return sizeof(dw_porter);
162
gint qq_putime(guint8 *buf, time_t *t)
164
guint32 dw, dw_porter;
165
memcpy(&dw, t, sizeof(dw));
166
dw_porter = g_htonl(dw);
168
purple_debug_info("QQ", "[DBG][put32] buf %p\n", (void *)buf);
169
purple_debug_info("QQ", "[DBG][put32] dw 0x%08x, dw_porter 0x%08x\n", dw, dw_porter);
171
memcpy(buf, &dw_porter, sizeof(dw_porter));
172
return sizeof(dw_porter);
175
gint qq_putIP(guint8* buf, struct in_addr *ip)
177
memcpy(buf, ip, sizeof(struct in_addr));
178
return sizeof(struct in_addr);
181
/* pack datalen bytes into buf
182
* return the number of bytes packed, otherwise return -1 */
183
gint qq_putdata(guint8 *buf, const guint8 *data, const int datalen)
185
memcpy(buf, data, datalen);
187
purple_debug_info("QQ", "[DBG][putdata] buf %p\n", (void *)buf);