13870
|
1 /**
|
|
2 * The QQ2003C protocol plugin
|
|
3 *
|
|
4 * for gaim
|
|
5 *
|
|
6 * Copyright (C) 2004 Puzzlebird
|
|
7 *
|
|
8 * This program is free software; you can redistribute it and/or modify
|
|
9 * it under the terms of the GNU General Public License as published by
|
|
10 * the Free Software Foundation; either version 2 of the License, or
|
|
11 * (at your option) any later version.
|
|
12 *
|
|
13 * This program is distributed in the hope that it will be useful,
|
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16 * GNU General Public License for more details.
|
|
17 *
|
|
18 * You should have received a copy of the GNU General Public License
|
|
19 * along with this program; if not, write to the Free Software
|
|
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
21 */
|
|
22
|
|
23 #ifndef _WIN32
|
|
24 #include <arpa/inet.h>
|
|
25 #else
|
|
26 #include "win32dep.h"
|
|
27 #endif
|
|
28
|
|
29 #include <string.h>
|
|
30
|
|
31 #include "packet_parse.h"
|
|
32
|
14021
|
33 /* read one byte from buf,
|
|
34 * return the number of bytes read if succeeds, otherwise return -1 */
|
|
35 gint read_packet_b(guint8 *buf, guint8 **cursor, gint buflen, guint8 *b)
|
13870
|
36 {
|
|
37 if (*cursor <= buf + buflen - sizeof(*b)) {
|
|
38 *b = **(guint8 **) cursor;
|
|
39 *cursor += sizeof(*b);
|
|
40 return sizeof(*b);
|
14021
|
41 } else {
|
13870
|
42 return -1;
|
14021
|
43 }
|
|
44 }
|
13870
|
45
|
14021
|
46 /* read two bytes as "guint16" from buf,
|
|
47 * return the number of bytes read if succeeds, otherwise return -1 */
|
|
48 gint read_packet_w(guint8 *buf, guint8 **cursor, gint buflen, guint16 *w)
|
13870
|
49 {
|
|
50 if (*cursor <= buf + buflen - sizeof(*w)) {
|
|
51 *w = ntohs(**(guint16 **) cursor);
|
|
52 *cursor += sizeof(*w);
|
|
53 return sizeof(*w);
|
14021
|
54 } else {
|
13870
|
55 return -1;
|
14021
|
56 }
|
|
57 }
|
13870
|
58
|
14021
|
59 /* read four bytes as "guint32" from buf,
|
|
60 * return the number of bytes read if succeeds, otherwise return -1 */
|
|
61 gint read_packet_dw(guint8 *buf, guint8 **cursor, gint buflen, guint32 *dw)
|
13870
|
62 {
|
|
63 if (*cursor <= buf + buflen - sizeof(*dw)) {
|
|
64 *dw = ntohl(**(guint32 **) cursor);
|
|
65 *cursor += sizeof(*dw);
|
|
66 return sizeof(*dw);
|
14021
|
67 } else {
|
13870
|
68 return -1;
|
14021
|
69 }
|
|
70 }
|
13870
|
71
|
14021
|
72 /* read datalen bytes from buf,
|
|
73 * return the number of bytes read if succeeds, otherwise return -1 */
|
|
74 gint read_packet_data(guint8 *buf, guint8 **cursor, gint buflen, guint8 *data, gint datalen) {
|
13870
|
75 if (*cursor <= buf + buflen - datalen) {
|
|
76 g_memmove(data, *cursor, datalen);
|
|
77 *cursor += datalen;
|
|
78 return datalen;
|
14021
|
79 } else {
|
13870
|
80 return -1;
|
14021
|
81 }
|
|
82 }
|
13870
|
83
|
14021
|
84 /* pack one byte into buf
|
|
85 * return the number of bytes packed, otherwise return -1 */
|
|
86 gint create_packet_b(guint8 *buf, guint8 **cursor, guint8 b)
|
13870
|
87 {
|
|
88 if (*cursor <= buf + MAX_PACKET_SIZE - sizeof(guint8)) {
|
|
89 **(guint8 **) cursor = b;
|
|
90 *cursor += sizeof(guint8);
|
|
91 return sizeof(guint8);
|
14021
|
92 } else {
|
13870
|
93 return -1;
|
14021
|
94 }
|
|
95 }
|
13870
|
96
|
14021
|
97 /* pack two bytes as "guint16" into buf
|
|
98 * return the number of bytes packed, otherwise return -1 */
|
|
99 gint create_packet_w(guint8 *buf, guint8 **cursor, guint16 w)
|
13870
|
100 {
|
|
101 if (*cursor <= buf + MAX_PACKET_SIZE - sizeof(guint16)) {
|
|
102 **(guint16 **) cursor = htons(w);
|
|
103 *cursor += sizeof(guint16);
|
|
104 return sizeof(guint16);
|
14021
|
105 } else {
|
13870
|
106 return -1;
|
14021
|
107 }
|
|
108 }
|
13870
|
109
|
14021
|
110 /* pack four bytes as "guint32" into buf
|
|
111 * return the number of bytes packed, otherwise return -1 */
|
|
112 gint create_packet_dw(guint8 *buf, guint8 **cursor, guint32 dw)
|
13870
|
113 {
|
|
114 if (*cursor <= buf + MAX_PACKET_SIZE - sizeof(guint32)) {
|
|
115 **(guint32 **) cursor = htonl(dw);
|
|
116 *cursor += sizeof(guint32);
|
|
117 return sizeof(guint32);
|
14021
|
118 } else {
|
13870
|
119 return -1;
|
14021
|
120 }
|
|
121 }
|
13870
|
122
|
14021
|
123 /* pack datalen bytes into buf
|
|
124 * return the number of bytes packed, otherwise return -1 */
|
|
125 gint create_packet_data(guint8 *buf, guint8 **cursor, guint8 *data, gint datalen)
|
|
126 {
|
13870
|
127 if (*cursor <= buf + MAX_PACKET_SIZE - datalen) {
|
|
128 g_memmove(*cursor, data, datalen);
|
|
129 *cursor += datalen;
|
|
130 return datalen;
|
14021
|
131 } else {
|
13870
|
132 return -1;
|
14021
|
133 }
|
|
134 }
|