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