From 6214255bbb56558f6062aa6c33ae31d32c38f3e5 Mon Sep 17 00:00:00 2001
From: Richard Nyberg <rnyberg@murmeldjur.se>
Date: Sun, 19 Feb 2006 13:04:18 +0000
Subject: [PATCH] Safer code for net_read32 and net_write32. It may have been
 possible for them to cause failure on some architectures because of unaligned
 fetch/write of integers.

---
 btpd/net.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/btpd/net.c b/btpd/net.c
index a1eac6b..0c63776 100644
--- a/btpd/net.c
+++ b/btpd/net.c
@@ -128,13 +128,19 @@ net_active(struct torrent *tp)
 void
 net_write32(void *buf, uint32_t num)
 {
-    *(uint32_t *)buf = htonl(num);
+    uint8_t *p = buf;
+    *p = (num >> 24) & 0xff;
+    *(p + 1) = (num >> 16) & 0xff;
+    *(p + 2) = (num >> 8) & 0xff;
+    *(p + 3) = num & 0xff;
 }
 
 uint32_t
 net_read32(const void *buf)
 {
-    return ntohl(*(uint32_t *)buf);
+    const uint8_t *p = buf;
+    return (uint32_t)*p << 24 | (uint32_t)*(p + 1) << 16
+        | (uint16_t)*(p + 2) << 8 | *(p + 3);
 }
 
 static unsigned long