00001
00023
00024
00025 #ifndef _WIN32
00026 #include <arpa/inet.h>
00027 #else
00028 #include "win32dep.h"
00029 #endif
00030
00031 #include <string.h>
00032
00033 #include "packet_parse.h"
00034
00035
00036
00037
00038 gint read_packet_b(guint8 * buf, guint8 ** cursor, gint buflen, guint8 * b)
00039 {
00040 if (*cursor <= buf + buflen - sizeof(*b)) {
00041 *b = **(guint8 **) cursor;
00042 *cursor += sizeof(*b);
00043 return sizeof(*b);
00044 } else
00045 return -1;
00046 }
00047
00048
00049
00050
00051 gint read_packet_w(guint8 * buf, guint8 ** cursor, gint buflen, guint16 * w)
00052 {
00053 if (*cursor <= buf + buflen - sizeof(*w)) {
00054 *w = ntohs(**(guint16 **) cursor);
00055 *cursor += sizeof(*w);
00056 return sizeof(*w);
00057 } else
00058 return -1;
00059 }
00060
00061
00062
00063
00064 gint read_packet_dw(guint8 * buf, guint8 ** cursor, gint buflen, guint32 * dw)
00065 {
00066 if (*cursor <= buf + buflen - sizeof(*dw)) {
00067 *dw = ntohl(**(guint32 **) cursor);
00068 *cursor += sizeof(*dw);
00069 return sizeof(*dw);
00070 } else
00071 return -1;
00072 }
00073
00074
00075
00076
00077 gint read_packet_data(guint8 * buf, guint8 ** cursor, gint buflen, guint8 * data, gint datalen) {
00078 if (*cursor <= buf + buflen - datalen) {
00079 g_memmove(data, *cursor, datalen);
00080 *cursor += datalen;
00081 return datalen;
00082 } else
00083 return -1;
00084 }
00085
00086
00087
00088
00089 gint create_packet_b(guint8 * buf, guint8 ** cursor, guint8 b)
00090 {
00091 if (*cursor <= buf + MAX_PACKET_SIZE - sizeof(guint8)) {
00092 **(guint8 **) cursor = b;
00093 *cursor += sizeof(guint8);
00094 return sizeof(guint8);
00095 } else
00096 return -1;
00097 }
00098
00099
00100
00101
00102 gint create_packet_w(guint8 * buf, guint8 ** cursor, guint16 w)
00103 {
00104 if (*cursor <= buf + MAX_PACKET_SIZE - sizeof(guint16)) {
00105 **(guint16 **) cursor = htons(w);
00106 *cursor += sizeof(guint16);
00107 return sizeof(guint16);
00108 } else
00109 return -1;
00110 }
00111
00112
00113
00114
00115 gint create_packet_dw(guint8 * buf, guint8 ** cursor, guint32 dw)
00116 {
00117 if (*cursor <= buf + MAX_PACKET_SIZE - sizeof(guint32)) {
00118 **(guint32 **) cursor = htonl(dw);
00119 *cursor += sizeof(guint32);
00120 return sizeof(guint32);
00121 } else
00122 return -1;
00123 }
00124
00125
00126
00127
00128 gint create_packet_data(guint8 * buf, guint8 ** cursor, guint8 * data, gint datalen) {
00129 if (*cursor <= buf + MAX_PACKET_SIZE - datalen) {
00130 g_memmove(*cursor, data, datalen);
00131 *cursor += datalen;
00132 return datalen;
00133 } else
00134 return -1;
00135 }
00136
00137
00138