#! /bin/sh -e
## 334016_broken_bswab_inc_macros.dpatch by Piotr Krysiuk <piotras@gmail.com>
##
## All lines beginning with `## DP:' are a description of the patch.
## DP: Fix broken bswab*_inc macros.

if [ $# -ne 1 ]; then
    echo >&2 "`basename $0`: script expects -patch|-unpatch as argument"
    exit 1
fi
case "$1" in
    -patch) patch -f --no-backup-if-mismatch -p1 < $0;;
    -unpatch) patch -f --no-backup-if-mismatch -R -p1 < $0;;
    *)
        echo >&2 "`basename $0`: script expects -patch|-unpatch as argument"
        exit 1;;
esac

exit 0

diff -x debian -ruN old/hfsplus-1.0.4/libhfsp/src/btree.c hfsplus-1.0.4/libhfsp/src/btree.c
--- old/hfsplus-1.0.4/libhfsp/src/btree.c	2002-03-05 19:50:28.000000000 +0000
+++ hfsplus-1.0.4/libhfsp/src/btree.c	2006-09-13 20:52:15.000000000 +0000
@@ -95,8 +95,9 @@
     head->btree_type    = bswabU8_inc(p);
     head->reserved2	= bswabU8_inc(p);
     head->attributes    = bswabU32_inc(p);
-    for (i=0; i < 16; i++)
+    for (i=0; i < 16; i++) {
 	head->reserved3[i] = bswabU32_inc(p);
+    }
     return p;
 }
 
@@ -121,8 +122,9 @@
     bstoreU8_inc (p, head->btree_type);
     bstoreU8_inc (p, head->reserved2);
     bstoreU32_inc(p, head->attributes);
-    for (i=0; i < 16; i++)
+    for (i=0; i < 16; i++) {
 	bstoreU32_inc(p, head->reserved3[i]);
+    }
     return p;
 }
 
diff -x debian -ruN old/hfsplus-1.0.4/libhfsp/src/record.c hfsplus-1.0.4/libhfsp/src/record.c
--- old/hfsplus-1.0.4/libhfsp/src/record.c	2002-03-17 17:20:01.000000000 +0000
+++ hfsplus-1.0.4/libhfsp/src/record.c	2006-09-13 20:52:15.000000000 +0000
@@ -60,8 +60,9 @@
     key->parent_cnid		    = bswabU32_inc(p);
     key->name.strlen = len	    = bswabU16_inc(p);
     cp = key->name.name;
-    for (i=0; i < len; i++, cp++)
+    for (i=0; i < len; i++, cp++) {
 	*cp			    = bswabU16_inc(p);
+    }
 	/* check if keylenght was correct */
     if (key_length != ((char*) p) - ((char*) check))
 	 HFSP_ERROR(EINVAL, "Invalid key length in record_readkey");
@@ -93,8 +94,9 @@
     bstoreU16_inc(p, key_length);
     bstoreU32_inc(p, key->parent_cnid);
     bstoreU16_inc(p, len);
-    for (i=0; i < len; i++, cp++)
+    for (i=0; i < len; i++, cp++) {
 	bstoreU16_inc(p, *cp);
+    }
     return p;	
   fail:
     return NULL;
@@ -477,8 +479,9 @@
     cp = entry->nodeName.name;
     if (len > 255)
 	HFSP_ERROR(-1, "Invalid key length in record_thread");
-    for (i=0; i < len; i++, cp++)
+    for (i=0; i < len; i++, cp++) {
 	*cp			 = bswabU16_inc(p);
+    }
     return p;
   fail:
     return NULL;
@@ -497,8 +500,9 @@
     cp = entry->nodeName.name;
     if (len > 255)
 	HFSP_ERROR(-1, "Invalid key length in record_thread");
-    for (i=0; i < len; i++, cp++)
+    for (i=0; i < len; i++, cp++) {
 	bstoreU16_inc(p, *cp);
+    }
     return p;
   fail:
     return NULL;
diff -x debian -ruN old/hfsplus-1.0.4/libhfsp/src/swab.h hfsplus-1.0.4/libhfsp/src/swab.h
--- old/hfsplus-1.0.4/libhfsp/src/swab.h	2002-03-05 19:50:29.000000000 +0000
+++ hfsplus-1.0.4/libhfsp/src/swab.h	2006-09-13 20:52:15.000000000 +0000
@@ -48,7 +48,7 @@
 
 #define bswabU16(val) val
 
-#define bswabU16_inc(ptr) ptr = (UInt16*)ptr + 1
+#define bswabU16_inc(ptr) *((UInt16*) (ptr)); ptr = (UInt16*)ptr + 1
 /* Only available as a GCC extension, but required on sparc due to
    alignment issues in some of the on-disk structs */
 #if defined(__GNUC__) && defined(__sparc__)
@@ -65,8 +65,8 @@
 		val += (c[i] << (8*(7-i++)));	\
 	val;})
 #else
-#define bswabU32_inc(ptr) ptr = (UInt32*)ptr + 1
-#define bswabU64_inc(ptr) ptr = (UInt64*)ptr + 1
+#define bswabU32_inc(ptr) *((UInt32*) (ptr)); ptr = (UInt32*)ptr + 1
+#define bswabU64_inc(ptr) *((UInt64*) (ptr)); ptr = (UInt64*)ptr + 1
 #endif
 
 #define bstoreU16_inc(ptr, val) *((UInt16*) (ptr)) = val; ptr = (UInt16*)ptr + 1

