14192
|
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
|
|
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)
|
|
36 {
|
|
37 if (*cursor <= buf + buflen - sizeof(*b)) {
|
|
38 *b = **(guint8 **) cursor;
|
|
39 *cursor += sizeof(*b);
|
|
40 return sizeof(*b);
|
|
41 } else {
|
|
42 return -1;
|
|
43 }
|
|
44 }
|
|
45
|
|
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)
|
|
49 {
|
|
50 if (*cursor <= buf + buflen - sizeof(*w)) {
|
|
51 *w = ntohs(**(guint16 **) cursor);
|
|
52 *cursor += sizeof(*w);
|
|
53 return sizeof(*w);
|
|
54 } else {
|
|
55 return -1;
|
|
56 }
|
|
57 }
|
|
58
|
|
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)
|
|
62 {
|
|
63 if (*cursor <= buf + buflen - sizeof(*dw)) {
|
|
64 *dw = ntohl(**(guint32 **) cursor);
|
|
65 *cursor += sizeof(*dw);
|
|
66 return sizeof(*dw);
|
|
67 } else {
|
|
68 return -1;
|
|
69 }
|
|
70 }
|
|
71
|
|
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) {
|
|
75 if (*cursor <= buf + buflen - datalen) {
|
|
76 g_memmove(data, *cursor, datalen);
|
|
77 *cursor += datalen;
|
|
78 return datalen;
|
|
79 } else {
|
|
80 return -1;
|
|
81 }
|
|
82 }
|
|
83
|
|
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)
|
|
87 {
|
|
88 if (*cursor <= buf + MAX_PACKET_SIZE - sizeof(guint8)) {
|
|
89 **(guint8 **) cursor = b;
|
|
90 *cursor += sizeof(guint8);
|
|
91 return sizeof(guint8);
|
|
92 } else {
|
|
93 return -1;
|
|
94 }
|
|
95 }
|
|
96
|
|
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)
|
|
100 {
|
|
101 if (*cursor <= buf + MAX_PACKET_SIZE - sizeof(guint16)) {
|
|
102 **(guint16 **) cursor = htons(w);
|
|
103 *cursor += sizeof(guint16);
|
|
104 return sizeof(guint16);
|
|
105 } else {
|
|
106 return -1;
|
|
107 }
|
|
108 }
|
|
109
|
|
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)
|
|
113 {
|
|
114 if (*cursor <= buf + MAX_PACKET_SIZE - sizeof(guint32)) {
|
|
115 **(guint32 **) cursor = htonl(dw);
|
|
116 *cursor += sizeof(guint32);
|
|
117 return sizeof(guint32);
|
|
118 } else {
|
|
119 return -1;
|
|
120 }
|
|
121 }
|
|
122
|
|
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 {
|
|
127 if (*cursor <= buf + MAX_PACKET_SIZE - datalen) {
|
|
128 g_memmove(*cursor, data, datalen);
|
|
129 *cursor += datalen;
|
|
130 return datalen;
|
|
131 } else {
|
|
132 return -1;
|
|
133 }
|
|
134 }
|