changeset 31043:5981b4116b52

Add some buffer reading/writing functions. I don't know if these are really the best implementations, but we'll see how they work out with each use.
author Elliott Sales de Andrade <qulogic@pidgin.im>
date Fri, 17 Dec 2010 07:12:21 +0000
parents 09c46c8f2f8f
children 7b771e6f1142
files libpurple/protocols/msn/msnutils.c libpurple/protocols/msn/msnutils.h
diffstat 2 files changed, 222 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/protocols/msn/msnutils.c	Thu Dec 16 22:27:43 2010 +0000
+++ b/libpurple/protocols/msn/msnutils.c	Fri Dec 17 07:12:21 2010 +0000
@@ -617,3 +617,87 @@
 	output[32] = '\0';
 }
 
+guint8
+msn_read8(const char *buf)
+{
+	return (guint8)buf[0];
+}
+
+guint16
+msn_read16le(const char *buf)
+{
+	return GUINT16_FROM_LE(*(guint16 *)buf);
+}
+
+guint16
+msn_read16be(const char *buf)
+{
+	return GUINT16_FROM_BE(*(guint16 *)buf);
+}
+
+guint32
+msn_read32le(const char *buf)
+{
+	return GUINT32_FROM_LE(*(guint32 *)buf);
+}
+
+guint32
+msn_read32be(const char *buf)
+{
+	return GUINT32_FROM_BE(*(guint32 *)buf);
+}
+
+guint64
+msn_read64le(const char *buf)
+{
+	return GUINT64_FROM_LE(*(guint64 *)buf);
+}
+
+guint64
+msn_read64be(const char *buf)
+{
+	return GUINT64_FROM_BE(*(guint64 *)buf);
+}
+
+void
+msn_write8(char *buf, guint8 data)
+{
+	*(guint8 *)buf = data;
+}
+
+void
+msn_write16le(char *buf, guint16 data)
+{
+	*(guint16 *)buf = GUINT16_TO_LE(data);
+}
+
+void
+msn_write16be(char *buf, guint16 data)
+{
+	*(guint16 *)buf = GUINT16_TO_BE(data);
+}
+
+void
+msn_write32le(char *buf, guint32 data)
+{
+	*(guint32 *)buf = GUINT32_TO_LE(data);
+}
+
+void
+msn_write32be(char *buf, guint32 data)
+{
+	*(guint32 *)buf = GUINT32_TO_BE(data);
+}
+
+void
+msn_write64le(char *buf, guint64 data)
+{
+	*(guint64 *)buf = GUINT64_TO_LE(data);
+}
+
+void
+msn_write64be(char *buf, guint64 data)
+{
+	*(guint64 *)buf = GUINT64_TO_BE(data);
+}
+
--- a/libpurple/protocols/msn/msnutils.h	Thu Dec 16 22:27:43 2010 +0000
+++ b/libpurple/protocols/msn/msnutils.h	Fri Dec 17 07:12:21 2010 +0000
@@ -94,4 +94,142 @@
  */
 void msn_handle_chl(char *input, char *output);
 
+/**
+ * Read a byte from a buffer
+ *
+ * @param buf Pointer to buffer.
+ *
+ * @return 8-bit byte
+ */
+guint8 msn_read8(const char *buf);
+
+/**
+ * Read a little-endian short from a buffer
+ *
+ * @param buf Pointer to buffer.
+ *
+ * @return 16-bit short
+ */
+guint16 msn_read16le(const char *buf);
+
+/**
+ * Read a big-endian short from a buffer
+ *
+ * @param buf Pointer to buffer.
+ *
+ * @return 16-bit short
+ */
+guint16 msn_read16be(const char *buf);
+
+/**
+ * Read a little-endian int from a buffer
+ *
+ * @param buf Pointer to buffer.
+ *
+ * @return 32-bit int
+ */
+guint32 msn_read32le(const char *buf);
+
+/**
+ * Read a big-endian int from a buffer
+ *
+ * @param buf Pointer to buffer.
+ *
+ * @return 32-bit int
+ */
+guint32 msn_read32be(const char *buf);
+
+/**
+ * Read a little-endian long from a buffer
+ *
+ * @param buf Pointer to buffer.
+ *
+ * @return 64-bit long
+ */
+guint64 msn_read64le(const char *buf);
+
+/**
+ * Read a big-endian long from a buffer
+ *
+ * @param buf Pointer to buffer.
+ *
+ * @return 64-bit long
+ */
+guint64 msn_read64be(const char *buf);
+
+/**
+ * Write a byte to a buffer
+ *
+ * @param buf  Pointer to buffer.
+ * @param data 8-bit byte.
+ */
+void msn_write8(char *buf, guint8 data);
+
+/**
+ * Write a little-endian short to a buffer
+ *
+ * @param buf  Pointer to buffer.
+ * @param data short.
+ */
+void msn_write16le(char *buf, guint16 data);
+
+/**
+ * Write a big-endian short to a buffer
+ *
+ * @param buf  Pointer to buffer.
+ * @param data short.
+ */
+void msn_write16be(char *buf, guint16 data);
+
+/**
+ * Write a little-endian int to a buffer
+ *
+ * @param buf  Pointer to buffer.
+ * @param data int.
+ */
+void msn_write32le(char *buf, guint32 data);
+
+/**
+ * Write a big-endian int to a buffer
+ *
+ * @param buf  Pointer to buffer.
+ * @param data int.
+ */
+void msn_write32be(char *buf, guint32 data);
+
+/**
+ * Write a little-endian long to a buffer
+ *
+ * @param buf  Pointer to buffer.
+ * @param data long.
+ */
+void msn_write64le(char *buf, guint64 data);
+
+/**
+ * Write a big-endian long to a buffer
+ *
+ * @param buf  Pointer to buffer.
+ * @param data short
+ */
+void msn_write64be(char *buf, guint64 data);
+
+/**
+ * Same as above, but these increment the buf pointer.
+ */
+#define msn_pop8(buf)    msn_read8((buf+=1)-1)
+#define msn_pop16le(buf) msn_read16le((buf+=2)-2)
+#define msn_pop16be(buf) msn_read16be((buf+=2)-2)
+#define msn_pop32le(buf) msn_read32le((buf+=4)-4)
+#define msn_pop32be(buf) msn_read32be((buf+=4)-4)
+#define msn_pop64le(buf) msn_read64le((buf+=8)-8)
+#define msn_pop64be(buf) msn_read64be((buf+=8)-8)
+#define msn_push8(buf, data)    msn_write8(buf, data),    buf+=1
+#define msn_push16le(buf, data) msn_write16le(buf, data), buf+=2
+#define msn_push16be(buf, data) msn_write16be(buf, data), buf+=2
+#define msn_push32le(buf, data) msn_write32le(buf, data), buf+=4
+#define msn_push32be(buf, data) msn_write32be(buf, data), buf+=4
+#define msn_push64le(buf, data) msn_write64le(buf, data), buf+=8
+#define msn_push64be(buf, data) msn_write64be(buf, data), buf+=8
+
 #endif /* MSN_UTILS_H */
+