changeset 31221:09f2a7d2ac99

Add SlpMessage parser and serializer functions to SlpMessage module.
author masca@cpw.pidgin.im
date Tue, 08 Jun 2010 21:21:49 +0000
parents 3ccddae82532
children 320c0852b5cc
files libpurple/protocols/msn/slpmsg.c libpurple/protocols/msn/slpmsg.h
diffstat 2 files changed, 88 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/protocols/msn/slpmsg.c	Tue Jun 08 21:08:13 2010 +0000
+++ b/libpurple/protocols/msn/slpmsg.c	Tue Jun 08 21:21:49 2010 +0000
@@ -56,6 +56,41 @@
 	return slpmsg;
 }
 
+MsnSlpMessage *msn_slpmsg_new_from_data(const char *data, size_t data_len)
+{
+	MsnSlpMessage *slpmsg;
+	MsnP2PHeader *header;
+	const char *tmp;
+	int body_len;
+
+	tmp = data;
+	slpmsg = msn_slpmsg_new(NULL);
+
+	if (data_len < sizeof(*header)) {
+		return NULL;
+	}
+
+	/* Extract the binary SLP header */
+	slpmsg->header = msn_p2p_header_from_wire((MsnP2PHeader*)tmp);
+
+	/* Extract the body */
+	body_len = data_len - (tmp - data);
+	/* msg->body_len = msg->msnslp_header.length; */
+
+	if (body_len > 0) {
+		slpmsg->size = body_len;
+		slpmsg->buffer = g_malloc(body_len);
+		memcpy(slpmsg->buffer, tmp, body_len);
+		tmp += body_len;
+	}
+
+	/* Extract the footer */
+	if (body_len >= 0) 
+		slpmsg->footer = msn_p2p_footer_from_wire((MsnP2PFooter*)tmp);
+
+	return slpmsg;
+}
+
 void
 msn_slpmsg_destroy(MsnSlpMessage *slpmsg)
 {
@@ -218,3 +253,36 @@
 
 	return slpmsg;
 }
+
+char *msn_slpmsg_serialize(MsnSlpMessage *slpmsg, size_t *ret_size)
+{
+	MsnP2PHeader *header;
+	MsnP2PFooter *footer;
+	char *base;
+	char *tmp;
+	size_t siz;
+
+	base = g_malloc(P2P_PACKET_HEADER_SIZE + slpmsg->size + sizeof(MsnP2PFooter));
+	tmp = base;
+
+	header = msn_p2p_header_to_wire(slpmsg->header);
+	footer = msn_p2p_footer_to_wire(slpmsg->footer);
+
+	siz = sizeof(MsnP2PHeader);
+	/* Copy header */
+	memcpy(tmp, (char*)header, siz);
+	tmp += siz;
+
+	/* Copy body */
+	memcpy(tmp, slpmsg->buffer, slpmsg->size);
+	tmp += slpmsg->size;
+
+	/* Copy footer */
+	siz = sizeof(MsnP2PFooter);
+	memcpy(tmp, (char*)footer, siz);
+	tmp += siz;
+
+	*ret_size = tmp - base;
+
+	return base;
+}
--- a/libpurple/protocols/msn/slpmsg.h	Tue Jun 08 21:08:13 2010 +0000
+++ b/libpurple/protocols/msn/slpmsg.h	Tue Jun 08 21:21:49 2010 +0000
@@ -94,6 +94,16 @@
 MsnSlpMessage *msn_slpmsg_new(MsnSlpLink *slplink);
 
 /**
+ * Creates a MsnSlpMessage without a MsnSlpLink by parsing the raw data.
+ *
+ * @param data 		The raw data with the slp message.
+ * @param data_len 	The len of the data
+ *
+ * @return The createed slp message.
+ */
+MsnSlpMessage *msn_slpmsg_new_from_data(const char *data, size_t data_len);
+
+/**
  * Destroys a slp message
  *
  * @param slpmsg The slp message to destory.
@@ -113,4 +123,14 @@
 
 void msn_slpmsg_show(MsnMessage *msg);
 
+/**
+ * Serialize the MsnSlpMessage in a way it can be used to be transmited
+ *
+ * @param slpmsg 	The MsnSlpMessage.
+ * @param ret_size 	The size of the buffer cointaining the message.
+ *
+ * @return a buffer with the serialized data.
+ */
+char *msn_slpmsg_serialize(MsnSlpMessage *slpmsg, size_t *ret_size);
+
 #endif /* _MSN_SLPMSG_H_ */