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 #include <string.h>
|
|
24
|
|
25 #include "packet_parse.h"
|
|
26
|
|
27 /* read one byte from buf,
|
|
28 * return the number of bytes read if succeeds, otherwise return -1 */
|
|
29 gint read_packet_b(guint8 *buf, guint8 **cursor, gint buflen, guint8 *b)
|
|
30 {
|
|
31 if (*cursor <= buf + buflen - sizeof(*b)) {
|
|
32 *b = **(guint8 **) cursor;
|
|
33 *cursor += sizeof(*b);
|
|
34 return sizeof(*b);
|
|
35 } else {
|
|
36 return -1;
|
|
37 }
|
|
38 }
|
|
39
|
|
40 /* read two bytes as "guint16" from buf,
|
|
41 * return the number of bytes read if succeeds, otherwise return -1 */
|
|
42 gint read_packet_w(guint8 *buf, guint8 **cursor, gint buflen, guint16 *w)
|
|
43 {
|
|
44 if (*cursor <= buf + buflen - sizeof(*w)) {
|
14610
|
45 *w = g_ntohs(**(guint16 **) cursor);
|
14192
|
46 *cursor += sizeof(*w);
|
|
47 return sizeof(*w);
|
|
48 } else {
|
|
49 return -1;
|
|
50 }
|
|
51 }
|
|
52
|
|
53 /* read four bytes as "guint32" from buf,
|
|
54 * return the number of bytes read if succeeds, otherwise return -1 */
|
|
55 gint read_packet_dw(guint8 *buf, guint8 **cursor, gint buflen, guint32 *dw)
|
|
56 {
|
|
57 if (*cursor <= buf + buflen - sizeof(*dw)) {
|
14610
|
58 *dw = g_ntohl(**(guint32 **) cursor);
|
14192
|
59 *cursor += sizeof(*dw);
|
|
60 return sizeof(*dw);
|
|
61 } else {
|
|
62 return -1;
|
|
63 }
|
|
64 }
|
|
65
|
|
66 /* read datalen bytes from buf,
|
|
67 * return the number of bytes read if succeeds, otherwise return -1 */
|
|
68 gint read_packet_data(guint8 *buf, guint8 **cursor, gint buflen, guint8 *data, gint datalen) {
|
|
69 if (*cursor <= buf + buflen - datalen) {
|
|
70 g_memmove(data, *cursor, datalen);
|
|
71 *cursor += datalen;
|
|
72 return datalen;
|
|
73 } else {
|
|
74 return -1;
|
|
75 }
|
|
76 }
|
|
77
|
|
78 /* pack one byte into buf
|
|
79 * return the number of bytes packed, otherwise return -1 */
|
|
80 gint create_packet_b(guint8 *buf, guint8 **cursor, guint8 b)
|
|
81 {
|
|
82 if (*cursor <= buf + MAX_PACKET_SIZE - sizeof(guint8)) {
|
|
83 **(guint8 **) cursor = b;
|
|
84 *cursor += sizeof(guint8);
|
|
85 return sizeof(guint8);
|
|
86 } else {
|
|
87 return -1;
|
|
88 }
|
|
89 }
|
|
90
|
|
91 /* pack two bytes as "guint16" into buf
|
|
92 * return the number of bytes packed, otherwise return -1 */
|
|
93 gint create_packet_w(guint8 *buf, guint8 **cursor, guint16 w)
|
|
94 {
|
|
95 if (*cursor <= buf + MAX_PACKET_SIZE - sizeof(guint16)) {
|
14610
|
96 **(guint16 **) cursor = g_htons(w);
|
14192
|
97 *cursor += sizeof(guint16);
|
|
98 return sizeof(guint16);
|
|
99 } else {
|
|
100 return -1;
|
|
101 }
|
|
102 }
|
|
103
|
|
104 /* pack four bytes as "guint32" into buf
|
|
105 * return the number of bytes packed, otherwise return -1 */
|
|
106 gint create_packet_dw(guint8 *buf, guint8 **cursor, guint32 dw)
|
|
107 {
|
|
108 if (*cursor <= buf + MAX_PACKET_SIZE - sizeof(guint32)) {
|
14610
|
109 **(guint32 **) cursor = g_htonl(dw);
|
14192
|
110 *cursor += sizeof(guint32);
|
|
111 return sizeof(guint32);
|
|
112 } else {
|
|
113 return -1;
|
|
114 }
|
|
115 }
|
|
116
|
|
117 /* pack datalen bytes into buf
|
|
118 * return the number of bytes packed, otherwise return -1 */
|
|
119 gint create_packet_data(guint8 *buf, guint8 **cursor, guint8 *data, gint datalen)
|
|
120 {
|
|
121 if (*cursor <= buf + MAX_PACKET_SIZE - datalen) {
|
|
122 g_memmove(*cursor, data, datalen);
|
|
123 *cursor += datalen;
|
|
124 return datalen;
|
|
125 } else {
|
|
126 return -1;
|
|
127 }
|
|
128 }
|