comparison src/protocols/qq/packet_parse.c @ 13870:983fd420e86b

[gaim-migrate @ 16340] Performed minor cleanup of the OpenQ codebase and patched it into the Gaim trunk as a prpl, providing basic QQ functionality. committer: Tailor Script <tailor@pidgin.im>
author Mark Huetsch <markhuetsch>
date Mon, 26 Jun 2006 02:58:54 +0000
parents
children ef8490f9e823
comparison
equal deleted inserted replaced
13869:5642f4658b59 13870:983fd420e86b
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 // START OF FILE
24 /*****************************************************************************/
25 #ifndef _WIN32
26 #include <arpa/inet.h>
27 #else
28 #include "win32dep.h"
29 #endif
30
31 #include <string.h>
32
33 #include "packet_parse.h"
34
35 /*****************************************************************************/
36 // read one byte from buf,
37 // return the number of bytes read if succeeds, otherwise return -1
38 gint read_packet_b(guint8 * buf, guint8 ** cursor, gint buflen, guint8 * b)
39 {
40 if (*cursor <= buf + buflen - sizeof(*b)) {
41 *b = **(guint8 **) cursor;
42 *cursor += sizeof(*b);
43 return sizeof(*b);
44 } else
45 return -1;
46 } // read_packet_b
47
48 /*****************************************************************************/
49 // read two bytes as "guint16" from buf,
50 // return the number of bytes read if succeeds, otherwise return -1
51 gint read_packet_w(guint8 * buf, guint8 ** cursor, gint buflen, guint16 * w)
52 {
53 if (*cursor <= buf + buflen - sizeof(*w)) {
54 *w = ntohs(**(guint16 **) cursor);
55 *cursor += sizeof(*w);
56 return sizeof(*w);
57 } else
58 return -1;
59 } // read_packet_w
60
61 /*****************************************************************************/
62 // read four bytes as "guint32" from buf,
63 // return the number of bytes read if succeeds, otherwise return -1
64 gint read_packet_dw(guint8 * buf, guint8 ** cursor, gint buflen, guint32 * dw)
65 {
66 if (*cursor <= buf + buflen - sizeof(*dw)) {
67 *dw = ntohl(**(guint32 **) cursor);
68 *cursor += sizeof(*dw);
69 return sizeof(*dw);
70 } else
71 return -1;
72 } // read_packet_dw
73
74 /*****************************************************************************/
75 // read datalen bytes from buf,
76 // return the number of bytes read if succeeds, otherwise return -1
77 gint read_packet_data(guint8 * buf, guint8 ** cursor, gint buflen, guint8 * data, gint datalen) {
78 if (*cursor <= buf + buflen - datalen) {
79 g_memmove(data, *cursor, datalen);
80 *cursor += datalen;
81 return datalen;
82 } else
83 return -1;
84 } // read_packet_data
85
86 /*****************************************************************************/
87 // pack one byte into buf
88 // return the number of bytes packed, otherwise return -1
89 gint create_packet_b(guint8 * buf, guint8 ** cursor, guint8 b)
90 {
91 if (*cursor <= buf + MAX_PACKET_SIZE - sizeof(guint8)) {
92 **(guint8 **) cursor = b;
93 *cursor += sizeof(guint8);
94 return sizeof(guint8);
95 } else
96 return -1;
97 } // create_packet_b
98
99 /*****************************************************************************/
100 // pack two bytes as "guint16" into buf
101 // return the number of bytes packed, otherwise return -1
102 gint create_packet_w(guint8 * buf, guint8 ** cursor, guint16 w)
103 {
104 if (*cursor <= buf + MAX_PACKET_SIZE - sizeof(guint16)) {
105 **(guint16 **) cursor = htons(w);
106 *cursor += sizeof(guint16);
107 return sizeof(guint16);
108 } else
109 return -1;
110 } // create_packet_w
111
112 /*****************************************************************************/
113 // pack four bytes as "guint32" into buf
114 // return the number of bytes packed, otherwise return -1
115 gint create_packet_dw(guint8 * buf, guint8 ** cursor, guint32 dw)
116 {
117 if (*cursor <= buf + MAX_PACKET_SIZE - sizeof(guint32)) {
118 **(guint32 **) cursor = htonl(dw);
119 *cursor += sizeof(guint32);
120 return sizeof(guint32);
121 } else
122 return -1;
123 } // create_packet_dw
124
125 /*****************************************************************************/
126 // pack datalen bytes into buf
127 // return the number of bytes packed, otherwise return -1
128 gint create_packet_data(guint8 * buf, guint8 ** cursor, guint8 * data, gint datalen) {
129 if (*cursor <= buf + MAX_PACKET_SIZE - datalen) {
130 g_memmove(*cursor, data, datalen);
131 *cursor += datalen;
132 return datalen;
133 } else
134 return -1;
135 } // create_packet_data
136
137 /*****************************************************************************/
138 // END OF FILE