Index: qemu/hw/hw.h
===================================================================
--- qemu.orig/hw/hw.h	2008-09-25 20:19:13.000000000 +0000
+++ qemu/hw/hw.h	2008-09-25 21:11:08.000000000 +0000
@@ -12,11 +12,27 @@
 void qemu_fclose(QEMUFile *f);
 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size);
 void qemu_put_byte(QEMUFile *f, int v);
+
+static inline void qemu_put_ubyte(QEMUFile *f, unsigned int v)
+{
+    qemu_put_byte(f, (int)v);
+}
+
+#define qemu_put_sbyte qemu_put_byte
+
 void qemu_put_be16(QEMUFile *f, unsigned int v);
 void qemu_put_be32(QEMUFile *f, unsigned int v);
 void qemu_put_be64(QEMUFile *f, uint64_t v);
 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size);
 int qemu_get_byte(QEMUFile *f);
+
+static inline unsigned int qemu_get_ubyte(QEMUFile *f)
+{
+    return (unsigned int)qemu_get_byte(f);
+}
+
+#define qemu_get_sbyte qemu_get_byte
+
 unsigned int qemu_get_be16(QEMUFile *f);
 unsigned int qemu_get_be32(QEMUFile *f);
 uint64_t qemu_get_be64(QEMUFile *f);
@@ -61,17 +77,106 @@
     *pv = qemu_get_byte(f);
 }
 
+// Signed versions for type safety
+static inline void qemu_put_sbuffer(QEMUFile *f, const int8_t *buf, int size)
+{
+    qemu_put_buffer(f, (const uint8_t *)buf, size);
+}
+
+static inline void qemu_put_sbe16(QEMUFile *f, int v)
+{
+    qemu_put_be16(f, (unsigned int)v);
+}
+
+static inline void qemu_put_sbe32(QEMUFile *f, int v)
+{
+    qemu_put_be32(f, (unsigned int)v);
+}
+
+static inline void qemu_put_sbe64(QEMUFile *f, int64_t v)
+{
+    qemu_put_be64(f, (uint64_t)v);
+}
+
+static inline size_t qemu_get_sbuffer(QEMUFile *f, int8_t *buf, int size)
+{
+    return qemu_get_buffer(f, (uint8_t *)buf, size);
+}
+
+static inline int qemu_get_sbe16(QEMUFile *f)
+{
+    return (int)qemu_get_be16(f);
+}
+
+static inline int qemu_get_sbe32(QEMUFile *f)
+{
+    return (int)qemu_get_be32(f);
+}
+
+static inline int64_t qemu_get_sbe64(QEMUFile *f)
+{
+    return (int64_t)qemu_get_be64(f);
+}
+
+static inline void qemu_put_s8s(QEMUFile *f, const int8_t *pv)
+{
+    qemu_put_8s(f, (const uint8_t *)pv);
+}
+
+static inline void qemu_put_sbe16s(QEMUFile *f, const int16_t *pv)
+{
+    qemu_put_be16s(f, (const uint16_t *)pv);
+}
+
+static inline void qemu_put_sbe32s(QEMUFile *f, const int32_t *pv)
+{
+    qemu_put_be32s(f, (const uint32_t *)pv);
+}
+
+static inline void qemu_put_sbe64s(QEMUFile *f, const int64_t *pv)
+{
+    qemu_put_be64s(f, (const uint64_t *)pv);
+}
+
+static inline void qemu_get_s8s(QEMUFile *f, int8_t *pv)
+{
+    qemu_get_8s(f, (uint8_t *)pv);
+}
+
+static inline void qemu_get_sbe16s(QEMUFile *f, int16_t *pv)
+{
+    qemu_get_be16s(f, (uint16_t *)pv);
+}
+
+static inline void qemu_get_sbe32s(QEMUFile *f, int32_t *pv)
+{
+    qemu_get_be32s(f, (uint32_t *)pv);
+}
+
+static inline void qemu_get_sbe64s(QEMUFile *f, int64_t *pv)
+{
+    qemu_get_be64s(f, (uint64_t *)pv);
+}
+
 #ifdef NEED_CPU_H
 #if TARGET_LONG_BITS == 64
 #define qemu_put_betl qemu_put_be64
 #define qemu_get_betl qemu_get_be64
 #define qemu_put_betls qemu_put_be64s
 #define qemu_get_betls qemu_get_be64s
+#define qemu_put_sbetl qemu_put_sbe64
+#define qemu_get_sbetl qemu_get_sbe64
+#define qemu_put_sbetls qemu_put_sbe64s
+#define qemu_get_sbetls qemu_get_sbe64s
 #else
 #define qemu_put_betl qemu_put_be32
 #define qemu_get_betl qemu_get_be32
 #define qemu_put_betls qemu_put_be32s
 #define qemu_get_betls qemu_get_be32s
+#define qemu_put_sbetl qemu_put_sbe32
+#define qemu_get_sbetl qemu_get_sbe32
+#define qemu_put_sbetls qemu_put_sbe32s
+#define qemu_get_sbetls qemu_get_sbe32s
 #endif
 #endif
 
Index: qemu/hw/esp.c
===================================================================
--- qemu.orig/hw/esp.c	2008-09-25 20:19:14.000000000 +0000
+++ qemu/hw/esp.c	2008-09-25 20:25:12.000000000 +0000
@@ -578,7 +578,7 @@
 
     qemu_put_buffer(f, s->rregs, ESP_REGS);
     qemu_put_buffer(f, s->wregs, ESP_REGS);
-    qemu_put_be32s(f, (uint32_t *)&s->ti_size);
+    qemu_put_sbe32s(f, &s->ti_size);
     qemu_put_be32s(f, &s->ti_rptr);
     qemu_put_be32s(f, &s->ti_wptr);
     qemu_put_buffer(f, s->ti_buf, TI_BUFSZ);
@@ -600,7 +600,7 @@
 
     qemu_get_buffer(f, s->rregs, ESP_REGS);
     qemu_get_buffer(f, s->wregs, ESP_REGS);
-    qemu_get_be32s(f, (uint32_t *)&s->ti_size);
+    qemu_get_sbe32s(f, &s->ti_size);
     qemu_get_be32s(f, &s->ti_rptr);
     qemu_get_be32s(f, &s->ti_wptr);
     qemu_get_buffer(f, s->ti_buf, TI_BUFSZ);
Index: qemu/hw/pcnet.c
===================================================================
--- qemu.orig/hw/pcnet.c	2008-09-25 20:19:14.000000000 +0000
+++ qemu/hw/pcnet.c	2008-09-25 20:25:12.000000000 +0000
@@ -1877,9 +1877,9 @@
     if (s->pci_dev)
         pci_device_save(s->pci_dev, f);
 
-    qemu_put_be32(f, s->rap);
-    qemu_put_be32(f, s->isr);
-    qemu_put_be32(f, s->lnkst);
+    qemu_put_sbe32(f, s->rap);
+    qemu_put_sbe32(f, s->isr);
+    qemu_put_sbe32(f, s->lnkst);
     qemu_put_be32s(f, &s->rdra);
     qemu_put_be32s(f, &s->tdra);
     qemu_put_buffer(f, s->prom, 16);
@@ -1888,10 +1888,10 @@
     for (i = 0; i < 32; i++)
         qemu_put_be16s(f, &s->bcr[i]);
     qemu_put_be64s(f, &s->timer);
-    qemu_put_be32(f, s->xmit_pos);
-    qemu_put_be32(f, s->recv_pos);
+    qemu_put_sbe32(f, s->xmit_pos);
+    qemu_put_sbe32(f, s->recv_pos);
     qemu_put_buffer(f, s->buffer, 4096);
-    qemu_put_be32(f, s->tx_busy);
+    qemu_put_sbe32(f, s->tx_busy);
     qemu_put_timer(f, s->poll_timer);
 }
 
@@ -1909,9 +1909,9 @@
             return ret;
     }
 
-    qemu_get_be32s(f, (uint32_t*)&s->rap);
-    qemu_get_be32s(f, (uint32_t*)&s->isr);
-    qemu_get_be32s(f, (uint32_t*)&s->lnkst);
+    qemu_get_sbe32s(f, &s->rap);
+    qemu_get_sbe32s(f, &s->isr);
+    qemu_get_sbe32s(f, &s->lnkst);
     qemu_get_be32s(f, &s->rdra);
     qemu_get_be32s(f, &s->tdra);
     qemu_get_buffer(f, s->prom, 16);
@@ -1920,10 +1920,10 @@
     for (i = 0; i < 32; i++)
         qemu_get_be16s(f, &s->bcr[i]);
     qemu_get_be64s(f, &s->timer);
-    qemu_get_be32s(f, (uint32_t*)&s->xmit_pos);
-    qemu_get_be32s(f, (uint32_t*)&s->recv_pos);
+    qemu_get_sbe32s(f, &s->xmit_pos);
+    qemu_get_sbe32s(f, &s->recv_pos);
     qemu_get_buffer(f, s->buffer, 4096);
-    qemu_get_be32s(f, (uint32_t*)&s->tx_busy);
+    qemu_get_sbe32s(f, &s->tx_busy);
     qemu_get_timer(f, s->poll_timer);
 
     return 0;
Index: qemu/hw/pxa2xx.c
===================================================================
--- qemu.orig/hw/pxa2xx.c	2008-09-25 20:19:14.000000000 +0000
+++ qemu/hw/pxa2xx.c	2008-09-25 20:25:12.000000000 +0000
@@ -1233,9 +1233,9 @@
     qemu_put_be32s(f, &s->last_rycr);
     qemu_put_be32s(f, &s->last_swcr);
     qemu_put_be32s(f, &s->last_rtcpicr);
-    qemu_put_be64s(f, (uint64_t *) &s->last_hz);
-    qemu_put_be64s(f, (uint64_t *) &s->last_sw);
-    qemu_put_be64s(f, (uint64_t *) &s->last_pi);
+    qemu_put_sbe64s(f, &s->last_hz);
+    qemu_put_sbe64s(f, &s->last_sw);
+    qemu_put_sbe64s(f, &s->last_pi);
 }
 
 static int pxa2xx_rtc_load(QEMUFile *f, void *opaque, int version_id)
@@ -1257,9 +1257,9 @@
     qemu_get_be32s(f, &s->last_rycr);
     qemu_get_be32s(f, &s->last_swcr);
     qemu_get_be32s(f, &s->last_rtcpicr);
-    qemu_get_be64s(f, (uint64_t *) &s->last_hz);
-    qemu_get_be64s(f, (uint64_t *) &s->last_sw);
-    qemu_get_be64s(f, (uint64_t *) &s->last_pi);
+    qemu_get_sbe64s(f, &s->last_hz);
+    qemu_get_sbe64s(f, &s->last_sw);
+    qemu_get_sbe64s(f, &s->last_pi);
 
     pxa2xx_rtc_alarm_update(s, s->rtsr);
 
Index: qemu/hw/pxa2xx_timer.c
===================================================================
--- qemu.orig/hw/pxa2xx_timer.c	2008-09-25 20:19:14.000000000 +0000
+++ qemu/hw/pxa2xx_timer.c	2008-09-25 20:25:12.000000000 +0000
@@ -384,8 +384,8 @@
         for (i = 0; i < 8; i ++) {
             qemu_put_be32s(f, &s->tm4[i].tm.value);
             qemu_put_be32(f, s->tm4[i].tm.level);
-            qemu_put_be32s(f, (uint32_t *) &s->tm4[i].oldclock);
-            qemu_put_be32s(f, (uint32_t *) &s->tm4[i].clock);
+            qemu_put_sbe32s(f, &s->tm4[i].oldclock);
+            qemu_put_sbe32s(f, &s->tm4[i].clock);
             qemu_put_be64s(f, &s->tm4[i].lastload);
             qemu_put_be32s(f, &s->tm4[i].freq);
             qemu_put_be32s(f, &s->tm4[i].control);
@@ -418,8 +418,8 @@
         for (i = 0; i < 8; i ++) {
             qemu_get_be32s(f, &s->tm4[i].tm.value);
             s->tm4[i].tm.level = qemu_get_be32(f);
-            qemu_get_be32s(f, (uint32_t *) &s->tm4[i].oldclock);
-            qemu_get_be32s(f, (uint32_t *) &s->tm4[i].clock);
+            qemu_get_sbe32s(f, &s->tm4[i].oldclock);
+            qemu_get_sbe32s(f, &s->tm4[i].clock);
             qemu_get_be64s(f, &s->tm4[i].lastload);
             qemu_get_be32s(f, &s->tm4[i].freq);
             qemu_get_be32s(f, &s->tm4[i].control);
Index: qemu/hw/tcx.c
===================================================================
--- qemu.orig/hw/tcx.c	2008-09-25 20:19:14.000000000 +0000
+++ qemu/hw/tcx.c	2008-09-25 20:25:12.000000000 +0000
@@ -372,9 +372,9 @@
 {
     TCXState *s = opaque;
 
-    qemu_put_be16s(f, (uint16_t *)&s->height);
-    qemu_put_be16s(f, (uint16_t *)&s->width);
-    qemu_put_be16s(f, (uint16_t *)&s->depth);
+    qemu_put_be16s(f, &s->height);
+    qemu_put_be16s(f, &s->width);
+    qemu_put_be16s(f, &s->depth);
     qemu_put_buffer(f, s->r, 256);
     qemu_put_buffer(f, s->g, 256);
     qemu_put_buffer(f, s->b, 256);
@@ -391,13 +391,13 @@
         return -EINVAL;
 
     if (version_id == 3) {
-        qemu_get_be32s(f, (uint32_t *)&dummy);
-        qemu_get_be32s(f, (uint32_t *)&dummy);
-        qemu_get_be32s(f, (uint32_t *)&dummy);
+        qemu_get_be32s(f, &dummy);
+        qemu_get_be32s(f, &dummy);
+        qemu_get_be32s(f, &dummy);
     }
-    qemu_get_be16s(f, (uint16_t *)&s->height);
-    qemu_get_be16s(f, (uint16_t *)&s->width);
-    qemu_get_be16s(f, (uint16_t *)&s->depth);
+    qemu_get_be16s(f, &s->height);
+    qemu_get_be16s(f, &s->width);
+    qemu_get_be16s(f, &s->depth);
     qemu_get_buffer(f, s->r, 256);
     qemu_get_buffer(f, s->g, 256);
     qemu_get_buffer(f, s->b, 256);
Index: qemu/hw/tmp105.c
===================================================================
--- qemu.orig/hw/tmp105.c	2008-09-25 20:19:14.000000000 +0000
+++ qemu/hw/tmp105.c	2008-09-25 20:25:12.000000000 +0000
@@ -185,9 +185,9 @@
 
     qemu_put_8s(f, &s->pointer);
     qemu_put_8s(f, &s->config);
-    qemu_put_be16s(f, (uint16_t *) &s->temperature);
-    qemu_put_be16s(f, (uint16_t *) &s->limit[0]);
-    qemu_put_be16s(f, (uint16_t *) &s->limit[1]);
+    qemu_put_sbe16s(f, &s->temperature);
+    qemu_put_sbe16s(f, &s->limit[0]);
+    qemu_put_sbe16s(f, &s->limit[1]);
     qemu_put_byte(f, s->alarm);
     s->faults = tmp105_faultq[(s->config >> 3) & 3];		/* F */
 
@@ -204,9 +204,9 @@
 
     qemu_get_8s(f, &s->pointer);
     qemu_get_8s(f, &s->config);
-    qemu_get_be16s(f, (uint16_t *) &s->temperature);
-    qemu_get_be16s(f, (uint16_t *) &s->limit[0]);
-    qemu_get_be16s(f, (uint16_t *) &s->limit[1]);
+    qemu_get_sbe16s(f, &s->temperature);
+    qemu_get_sbe16s(f, &s->limit[0]);
+    qemu_get_sbe16s(f, &s->limit[1]);
     s->alarm = qemu_get_byte(f);
 
     tmp105_interrupt_update(s);
Index: qemu/hw/tsc210x.c
===================================================================
--- qemu.orig/hw/tsc210x.c	2008-09-25 20:19:14.000000000 +0000
+++ qemu/hw/tsc210x.c	2008-09-25 20:25:12.000000000 +0000
@@ -1046,8 +1046,8 @@
     qemu_put_be16s(f, &s->pll[0]);
     qemu_put_be16s(f, &s->pll[1]);
     qemu_put_be16s(f, &s->volume);
-    qemu_put_be64(f, (uint64_t) (s->volume_change - now));
-    qemu_put_be64(f, (uint64_t) (s->powerdown - now));
+    qemu_put_sbe64(f, (s->volume_change - now));
+    qemu_put_sbe64(f, (s->powerdown - now));
     qemu_put_byte(f, s->softstep);
     qemu_put_be16s(f, &s->dac_power);
 
@@ -1092,8 +1092,8 @@
     qemu_get_be16s(f, &s->pll[0]);
     qemu_get_be16s(f, &s->pll[1]);
     qemu_get_be16s(f, &s->volume);
-    s->volume_change = (int64_t) qemu_get_be64(f) + now;
-    s->powerdown = (int64_t) qemu_get_be64(f) + now;
+    s->volume_change = qemu_get_sbe64(f) + now;
+    s->powerdown = qemu_get_sbe64(f) + now;
     s->softstep = qemu_get_byte(f);
     qemu_get_be16s(f, &s->dac_power);
 
Index: qemu/hw/e1000.c
===================================================================
--- qemu.orig/hw/e1000.c	2008-09-25 20:19:14.000000000 +0000
+++ qemu/hw/e1000.c	2008-09-25 20:25:12.000000000 +0000
@@ -102,8 +102,8 @@
         uint32_t paylen;
         uint16_t tso_frames;
         char tse;
-        char ip;
-        char tcp;
+        int8_t ip;
+        int8_t tcp;
         char cptse;     // current packet tse bit
     } tx;
 
@@ -824,8 +824,8 @@
     qemu_put_be16s(f, &s->tx.size);
     qemu_put_be16s(f, &s->tx.tso_frames);
     qemu_put_8s(f, &s->tx.sum_needed);
-    qemu_put_8s(f, &s->tx.ip);
-    qemu_put_8s(f, &s->tx.tcp);
+    qemu_put_s8s(f, &s->tx.ip);
+    qemu_put_s8s(f, &s->tx.tcp);
     qemu_put_buffer(f, s->tx.header, sizeof s->tx.header);
     qemu_put_buffer(f, s->tx.data, sizeof s->tx.data);
     for (i = 0; i < 64; i++)
@@ -849,7 +849,7 @@
     if ((ret = pci_device_load(&s->dev, f)) < 0)
         return ret;
     if (version_id == 1)
-        qemu_get_be32s(f, &i); /* once some unused instance id */
+        qemu_get_sbe32s(f, &i); /* once some unused instance id */
     qemu_get_be32s(f, &s->mmio_base);
     qemu_get_be32s(f, &s->rxbuf_size);
     qemu_get_be32s(f, &s->rxbuf_min_shift);
@@ -870,8 +870,8 @@
     qemu_get_be16s(f, &s->tx.size);
     qemu_get_be16s(f, &s->tx.tso_frames);
     qemu_get_8s(f, &s->tx.sum_needed);
-    qemu_get_8s(f, &s->tx.ip);
-    qemu_get_8s(f, &s->tx.tcp);
+    qemu_get_s8s(f, &s->tx.ip);
+    qemu_get_s8s(f, &s->tx.tcp);
     qemu_get_buffer(f, s->tx.header, sizeof s->tx.header);
     qemu_get_buffer(f, s->tx.data, sizeof s->tx.data);
     for (i = 0; i < 64; i++)
Index: qemu/hw/ptimer.c
===================================================================
--- qemu.orig/hw/ptimer.c	2008-09-25 20:19:14.000000000 +0000
+++ qemu/hw/ptimer.c	2008-09-25 20:25:12.000000000 +0000
@@ -165,9 +165,9 @@
     qemu_put_be64s(f, &s->limit);
     qemu_put_be64s(f, &s->delta);
     qemu_put_be32s(f, &s->period_frac);
-    qemu_put_be64s(f, &s->period);
-    qemu_put_be64s(f, &s->last_event);
-    qemu_put_be64s(f, &s->next_event);
+    qemu_put_sbe64s(f, &s->period);
+    qemu_put_sbe64s(f, &s->last_event);
+    qemu_put_sbe64s(f, &s->next_event);
     qemu_put_timer(f, s->timer);
 }
 
@@ -177,9 +177,9 @@
     qemu_get_be64s(f, &s->limit);
     qemu_get_be64s(f, &s->delta);
     qemu_get_be32s(f, &s->period_frac);
-    qemu_get_be64s(f, &s->period);
-    qemu_get_be64s(f, &s->last_event);
-    qemu_get_be64s(f, &s->next_event);
+    qemu_get_sbe64s(f, &s->period);
+    qemu_get_sbe64s(f, &s->last_event);
+    qemu_get_sbe64s(f, &s->next_event);
     qemu_get_timer(f, s->timer);
 }
 
Index: qemu/target-i386/machine.c
===================================================================
--- qemu.orig/target-i386/machine.c	2008-09-25 20:23:55.000000000 +0000
+++ qemu/target-i386/machine.c	2008-09-25 20:25:12.000000000 +0000
@@ -102,7 +102,7 @@
 
     /* MMU */
     a20_mask = (int32_t) env->a20_mask;
-    qemu_put_be32s(f, &a20_mask);
+    qemu_put_sbe32s(f, &a20_mask);
 
     /* XMM */
     qemu_put_be32s(f, &env->mxcsr);
@@ -261,7 +261,7 @@
         qemu_get_betls(f, &env->dr[i]);
 
     /* MMU */
-    qemu_get_be32s(f, &a20_mask);
+    qemu_get_sbe32s(f, &a20_mask);
     env->a20_mask = a20_mask;
 
     qemu_get_be32s(f, &env->mxcsr);
