--- configure.~0	2007-12-29 20:49:57.000000000 +0200
+++ configure	2012-04-05 11:13:59.173489700 +0300
@@ -1324,7 +1324,7 @@
 
 XAR_MAJOR_VERSION="1"
 XAR_MINOR_VERSION="5.2"
-XAR_VERSION="${XAR_MAJOR_VERSION}.${XAR_MINOR_VERSION}"
+XAR_VERSION="${XAR_MAJOR_VERSION}.${XAR_MINOR_VERSION}p1 (MinGW32)"
 
 
 
@@ -3268,6 +3268,12 @@
 	abi="macho"
 	RPATH=""
 	;;
+  *-*-mingw*)
+	abi="pei-i386"
+	CFLAGS="$CFLAGS"
+	CPPFLAGS="$CPPFLAGS -I./include"
+	RPATH=""
+	;;
   *-*-freebsd*)
 	CFLAGS="$CFLAGS"
 	abi="elf"
@@ -6678,7 +6684,20 @@
 _ACEOF
 
 
-if test $ac_cv_sizeof_ino_t = "4"; then
+if test $ac_cv_sizeof_ino_t = "2"; then
+cat >>confdefs.h <<\_ACEOF
+#define INO_STRING PRId16
+_ACEOF
+
+cat >>confdefs.h <<\_ACEOF
+#define INO_HEXSTRING PRIx16
+_ACEOF
+
+cat >>confdefs.h <<\_ACEOF
+#define INO_CAST (int16_t)
+_ACEOF
+
+elif test $ac_cv_sizeof_ino_t = "4"; then
 cat >>confdefs.h <<\_ACEOF
 #define INO_STRING PRId32
 _ACEOF


--- include/xar.h.in~0	2007-12-29 20:49:57.000000000 +0200
+++ include/xar.h.in	2012-03-11 09:58:09.440265300 +0200
@@ -44,6 +44,31 @@
 #include <stdint.h>
 #include <sys/stat.h>
 
+#include <fcntl.h>
+#ifndef O_BINARY
+#define O_BINARY  0
+#endif
+
+#ifdef __MINGW32__
+struct xar_stat {
+	unsigned st_dev;
+	long long st_ino;
+	mode_t st_mode;
+	short st_nlink;
+	uid_t st_uid;
+	gid_t st_gid;
+	dev_t st_rdev;
+	long long st_size;
+	time_t st_ctime;
+	time_t st_atime;
+	time_t st_mtime;
+	char st_uname[257];
+	char st_gname[257];
+};
+#else
+#define xar_stat stat
+#endif
+
 #pragma pack(4)
 
 struct xar_header {
@@ -118,7 +143,7 @@
 xar_file_t xar_add(xar_t x, const char *path);
 
 xar_file_t xar_add_frombuffer(xar_t x, xar_file_t parent, const char *name, char *buffer, size_t length);
-xar_file_t xar_add_folder(xar_t x, xar_file_t f, const char *name, struct stat *info);
+xar_file_t xar_add_folder(xar_t x, xar_file_t f, const char *name, struct xar_stat *info);
 xar_file_t xar_add_frompath(xar_t x, xar_file_t parent, const char *name, const char *realpath);
 
 xar_file_t xar_add_from_archive(xar_t x, xar_file_t parent, const char *name, xar_t sourcearchive, xar_file_t sourcefile);


--- lib/archive.c~0	2007-12-29 20:49:57.000000000 +0200
+++ lib/archive.c	2012-03-11 16:13:25.048120300 +0200
@@ -71,6 +71,9 @@
 #ifndef O_SHLOCK
 #define O_SHLOCK 0
 #endif
+#ifndef ENOTSUP
+#define ENOTSUP ENOSYS
+#endif
 
 #ifndef LONG_MAX
 #define LONG_MAX INT32_MAX
@@ -79,6 +82,14 @@
 #define LONG_MIN INT32_MIN
 #endif
 
+#ifndef S_ISLNK
+#ifdef __MINGW32__
+extern int lstat (const char *, struct xar_stat *);
+#else
+#define lstat stat
+#endif
+#endif
+
 static int32_t xar_unserialize(xar_t x);
 void xar_serialize(xar_t x, const char *file);
 
@@ -198,12 +209,15 @@ xar_t xar_open(const char *file, int32_t
 		 */
 		asprintf(&tmp4, "%s/xar.heap.XXXXXX", tmp3);
 		free(tmp1);
-		if( strcmp(file, "-") == 0 )
+		if( strcmp(file, "-") == 0 ) {
 			XAR(ret)->fd = 1;
-		else{
-			XAR(ret)->fd = open(file, O_WRONLY | O_CREAT | O_TRUNC | O_EXLOCK, 0644);
+#ifdef __MINGW32__
+			_setmode(XAR(ret)->fd, O_BINARY);
+#endif
+		} else {
+			XAR(ret)->fd = open(file, O_WRONLY | O_CREAT | O_TRUNC | O_EXLOCK | O_BINARY, 0644);
 			if( (-1 == XAR(ret)->fd ) && (ENOTSUP == errno) ){
-				XAR(ret)->fd = open(file, O_WRONLY | O_CREAT | O_TRUNC , 0644);				
+				XAR(ret)->fd = open(file, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);				
 			}
 		}
 		XAR(ret)->heap_fd = mkstemp(tmp4);
@@ -212,8 +226,12 @@ xar_t xar_open(const char *file, int32_t
 			free(XAR(ret));
 			return NULL;
 		}
+#ifdef __MINGW32__
+		XAR(ret)->heap_name = tmp4;
+#else
 		unlink(tmp4);
 		free(tmp4);
+#endif
 
 		deflateInit(&XAR(ret)->zs, Z_BEST_COMPRESSION);
 
@@ -236,13 +254,16 @@ xar_t xar_open(const char *file, int32_t
 		unsigned int tlen;
 		const EVP_MD *md;
 
-		if( strcmp(file, "-") == 0 )
+		if( strcmp(file, "-") == 0 ) {
 			XAR(ret)->fd = 0;
-		else{
-			XAR(ret)->fd = open(file, O_RDONLY | O_SHLOCK);
+#ifdef __MINGW32__
+			_setmode(XAR(ret)->fd, O_BINARY);
+#endif
+		} else {
+			XAR(ret)->fd = open(file, O_RDONLY | O_SHLOCK | O_BINARY);
 			
 			if( (-1 == XAR(ret)->fd ) && (ENOTSUP == errno) ){
-				XAR(ret)->fd = open(file, O_RDONLY);
+				XAR(ret)->fd = open(file, O_RDONLY | O_BINARY);
 			}
 
 		}
@@ -308,17 +329,21 @@ int xar_close(xar_t x) {
 	xar_attr_t a;
 	xar_file_t f;
 	int ret, retval = 0;
+	int fd, tocfd;
+	char *tmpser;
+#ifdef __MINGW32__
+	char *tmpser2;
+	int fd2;
+#endif
 
 	/* If we're creating an archive */
 	if( XAR(x)->heap_fd != -1 ) {
-		char *tmpser;
 		void *rbuf, *wbuf = NULL;
-		int fd, r, off, wbytes, rbytes;
+		int r, off, wbytes, rbytes;
 		long rsize, wsize;
 		z_stream zs;
 		uint64_t ungztoc, gztoc;
 		unsigned char chkstr[EVP_MAX_MD_SIZE];
-		int tocfd;
 		char timestr[128];
 		struct tm tmptm;
 		time_t t;
@@ -355,19 +380,25 @@ int xar_close(xar_t x) {
 		t = time(NULL);
 		gmtime_r(&t, &tmptm);
 		memset(timestr, 0, sizeof(timestr));
-		strftime(timestr, sizeof(timestr), "%FT%T", &tmptm);
+		strftime(timestr, sizeof(timestr), "%Y-%m-%dT%H:%M:%S", &tmptm);
 		xar_prop_set(XAR_FILE(x), "creation-time", timestr);
 
 		/* serialize the toc to a tmp file */
 		asprintf(&tmpser, "%s/xar.toc.XXXXXX", XAR(x)->dirname);
 		fd = mkstemp(tmpser);
 		xar_serialize(x, tmpser);
+#ifdef __MINGW32__
+		tmpser2 = tmpser;
+#else
 		unlink(tmpser);
 		free(tmpser);
+#endif
 		asprintf(&tmpser, "%s/xar.toc.XXXXXX", XAR(x)->dirname);
 		tocfd = mkstemp(tmpser);
+#ifndef __MINGW32__
 		unlink(tmpser);
 		free(tmpser);
+#endif
 		
 	
 		/* read the toc from the tmp file, compress it, and write it
@@ -398,6 +429,13 @@ int xar_close(xar_t x) {
 			r = read(fd, rbuf, rsize);
 			if( (r < 0) && (errno == EINTR) )
 				continue;
+			if( r < 0 ) {
+				xar_err_new(x);
+				xar_err_set_string(x, "Error closing xar archive (read from temporary TOC file)");
+				xar_err_set_errno(x, errno);
+				retval = -1;
+				goto CLOSEEND;
+			}
 			if( r == 0 )
 				break;
 	
@@ -430,7 +468,8 @@ int xar_close(xar_t x) {
 					continue;
 				if( r < 0 ) {
 					xar_err_new(x);
-					xar_err_set_string(x, "Error closing xar archive");
+					xar_err_set_string(x, "Error closing xar archive (write compressed TOC)");
+					xar_err_set_errno(x, errno);
 					retval = -1;
 					goto CLOSEEND;
 				}
@@ -470,6 +509,13 @@ int xar_close(xar_t x) {
 			r = read(tocfd, rbuf, rsize);
 			if( (r < 0) && (errno == EINTR) )
 				continue;
+			if( r < 0 ) {
+				xar_err_new(x);
+				xar_err_set_string(x, "Error closing xar archive (read compressed TOC)");
+				xar_err_set_errno(x, errno);
+				retval = -1;
+				goto CLOSEEND;
+			}
 			if( r == 0 )
 				break;
 
@@ -481,7 +527,8 @@ int xar_close(xar_t x) {
 					continue;
 				if( r < 0 ) {
 					xar_err_new(x);
-					xar_err_set_string(x, "Error closing xar archive");
+					xar_err_set_string(x, "Error closing xar archive (copy compressed TOC)");
+					xar_err_set_errno(x, errno);
 					retval = -1;
 					goto CLOSEEND;
 				}
@@ -602,9 +649,21 @@ CLOSE_BAIL:
 	xmlHashFree(XAR(x)->ino_hash, NULL);
 	xmlHashFree(XAR(x)->link_hash, NULL);
 	xmlHashFree(XAR(x)->csum_hash, NULL);
-	close(XAR(x)->fd);
-	if( XAR(x)->heap_fd >= 0 )
+	if( XAR(x)->fd > 1 )
+		close(XAR(x)->fd);
+	if( XAR(x)->heap_fd > 0 ) {
 		close(XAR(x)->heap_fd);
+#ifdef __MINGW32__
+		unlink(XAR(x)->heap_name);
+		free(XAR(x)->heap_name);
+		close(fd);
+		unlink(tmpser2);
+		free(tmpser2);
+		close(tocfd);
+		unlink(tmpser);
+		free(tmpser);
+#endif
+	}
 	free((char *)XAR(x)->filename);
 	free((char *)XAR(x)->dirname);
 	free(XAR(x)->readbuf);
@@ -669,7 +728,7 @@ int32_t xar_opt_set(xar_t x, const char 
  * f: parent node, possibly NULL
  * name: name of the node to add
  * realpath: real path to item, this is used if the item being archived is to be located at a different location in the tree
- * then it is on the real filesystem.
+ * than it is on the real filesystem.
  * Returns: newly allocated and populated node
  * Summary: helper function which adds a child of f and populates
  * its properties.  If f is NULL, the node will be added as a top
@@ -931,7 +990,7 @@ static xar_file_t xar_add_r(xar_t x, xar
 /* xar_add
  * x: archive to add the file to
  * path: path to file
- * Returns: allocated an populated xar_file_t representing the 
+ * Returns: allocated and populated xar_file_t representing the
  * specified file.
  * Summary: if a full path "foo/bar/blah" is specified, then any
  * directories not already existing in the archive will be added
@@ -950,6 +1009,12 @@ xar_file_t xar_add(xar_t x, const char *
 	if( path[0] == '/' ) {
 		XAR(x)->path_prefix = "/";
 		path++;
+#ifdef __MINGW32__
+	} else if( path[0] && path[1] == ':' && path[2] == '/') {
+		XAR(x)->path_prefix = strdup("x:/");
+		XAR(x)->path_prefix[0] = path[0];
+		path += 3;
+#endif
 	} else
 		XAR(x)->path_prefix = "";
 	return xar_add_r(x, NULL, path, "");
@@ -1014,13 +1079,13 @@ xar_file_t xar_add_frombuffer(xar_t x, x
 	return ret;
 }
 
-xar_file_t xar_add_folder(xar_t x, xar_file_t f, const char *name, struct stat *info)
+xar_file_t xar_add_folder(xar_t x, xar_file_t f, const char *name, struct xar_stat *info)
 {
 	xar_file_t ret;
 	char idstr[32];
 
 	if( info )
-		memcpy(&XAR(x)->sbcache,info,sizeof(struct stat));
+		memcpy(&XAR(x)->sbcache,info,sizeof(struct xar_stat));
 	
 	ret = xar_file_new(f);
 	if( !ret )


--- lib/archive.h~0	2007-12-29 20:49:57.000000000 +0200
+++ lib/archive.h	2012-03-11 09:57:18.487140300 +0200
@@ -67,6 +67,9 @@
 	int heap_fd;            /* fd for tmp heap archive, used in creation */
 	off_t heap_offset;      /* current offset within the heap */
 	off_t heap_len;         /* current length of the heap */
+#ifdef __MINGW32__
+	char *heap_name;	/* name of tmp heap archive */
+#endif
 	xar_header_t header;    /* header of the xar archive */
 	void *readbuf;          /* buffer for reading/writing compressed toc */
 	size_t readbuf_len;     /* length of readbuf */
@@ -86,7 +89,7 @@
 	EVP_MD_CTX toc_ctx;
 	int docksum;
 	int skipwarn;
-	struct stat sbcache;
+	struct xar_stat sbcache;
 };
 
 #define XAR(x) ((struct __xar_t *)(x))


--- lib/bzxar.c~0	2007-12-29 20:49:57.000000000 +0200
+++ lib/bzxar.c	2012-03-07 14:55:31.298192800 +0200
@@ -37,9 +37,6 @@
 
 
 #include "config.h"
-#ifndef HAVE_ASPRINTF
-#include "asprintf.h"
-#endif
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>


--- lib/darwinattr.c~0	2007-12-29 20:49:57.000000000 +0200
+++ lib/darwinattr.c	2012-03-07 14:56:02.032567800 +0200
@@ -36,6 +36,9 @@
 */
 
 #include "config.h"
+#ifndef HAVE_ASPRINTF
+#include "asprintf.h"
+#endif
 #include <stdio.h>
 #include <unistd.h>
 #include <string.h>
--- lib/data.c~0	2007-12-29 20:49:57.000000000 +0200
+++ lib/data.c	2012-03-08 16:33:56.979489000 +0200
@@ -157,7 +157,7 @@ int32_t xar_data_archive(xar_t x, xar_fi
 	}
 
 	if( 0 == len ){
-		context.fd = open(file, O_RDONLY);
+		context.fd = open(file, O_RDONLY | O_BINARY);
 		if( context.fd < 0 ) {
 			xar_err_new(x);
 			xar_err_set_file(x, f);
@@ -219,7 +219,7 @@ int32_t xar_data_extract(xar_t x, xar_fi
 		/* mode 600 since other modules may need to operate on the file
 		* prior to the real permissions being set.
 		*/
-		context.fd = open(file, O_RDWR|O_TRUNC|O_EXLOCK, 0600);
+		context.fd = open(file, O_RDWR|O_TRUNC|O_EXLOCK|O_BINARY, 0600);
 		if( context.fd < 0 ) {
 			xar_err_new(x);
 			xar_err_set_file(x, f);


--- lib/ext2.c~0	2007-12-29 20:49:57.000000000 +0200
+++ lib/ext2.c	2012-03-07 14:57:00.032567800 +0200
@@ -37,9 +37,11 @@
 */
 
 #include "config.h"
+#if defined(HAVE_EXT2FS_EXT2_FS_H) || defined(HAVE_LINUX_EXT2_FS_H)
 #ifndef HAVE_ASPRINTF
 #include "asprintf.h"
 #endif
+#endif
 #include <stdio.h>
 #include <unistd.h>
 #include "xar.h"


--- lib/signature.c~0	2007-12-29 20:49:57.000000000 +0200
+++ lib/signature.c	2012-03-07 15:00:09.720067800 +0200
@@ -381,7 +381,7 @@ int32_t xar_signature_serialize(xar_sign
 
 	/* <size> */
 	xmlTextWriterStartElementNS( writer, NULL, BAD_CAST("size"), NULL);
-	xmlTextWriterWriteFormatString(writer, "%ld", (XAR_SIGNATURE(sig)->len));
+	xmlTextWriterWriteFormatString(writer, "%ld", (long)(XAR_SIGNATURE(sig)->len));
 	xmlTextWriterEndElement(writer);	
 
 	/* <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#"> */


--- lib/stat.c~0	2007-12-29 20:49:57.000000000 +0200
+++ lib/stat.c	2012-04-05 10:25:35.094834700 +0300
@@ -79,17 +79,58 @@
 #define LLONG_MAX LONG_LONG_MAX
 #endif
 
+#ifdef __MINGW32__
+#define INO_T  __int64
+#undef INO_CAST
+#undef INO_STRING
+#undef INO_HEXSTRING
+#define INO_CAST      (__int64)
+#define INO_STRING    PRId64
+#define INO_HEXSTRING PRIx64
+#define mknod(F,M,D)  (-1)
+#define mkfifo(F,M)   (-1)
+#define mkdir(F,M)    _mkdir(F)
+extern int chown (const char *, unsigned, unsigned);
+#else
+#define INO_T ino_t
+#endif
+
+#ifndef S_IFLNK
+
+#define S_ISLNK(X) (0)
+
+int
+readlink (const char *fn, char *buf, size_t sz)
+{
+  errno = EINVAL;
+  return -1;
+}
+
+#ifndef __MINGW32__
+int
+symlink (const char *old, const char *new)
+{
+  return -1;
+}
+#endif
+
+#endif
+
 static struct {
 	const char *name;
 	mode_t type;
 } filetypes [] = {
 	{ "file", S_IFREG },
 	{ "directory", S_IFDIR },
+#ifdef S_IFLNK
 	{ "symlink", S_IFLNK },
+#endif
 	{ "fifo", S_IFIFO },
 	{ "character special", S_IFCHR },
 	{ "block special", S_IFBLK },
+#ifdef S_IFSOCK
 	{ "socket", S_IFSOCK },
+#endif
 #ifdef S_IFWHT
 	{ "whiteout", S_IFWHT },
 #endif
@@ -104,7 +145,7 @@ static const char * filetype_name (mode_
 	return ("unknown");
 }
 
-static xar_file_t xar_link_lookup(xar_t x, dev_t dev, ino_t ino, xar_file_t f) {
+static xar_file_t xar_link_lookup(xar_t x, dev_t dev, INO_T ino, xar_file_t f) {
 	char key[32];
 	xar_file_t ret;
 
@@ -279,7 +320,7 @@ static void x_addflag(xar_file_t f, cons
 }
 #endif
 
-static int32_t flags_archive(xar_file_t f, const struct stat *sb) {
+static int32_t flags_archive(xar_file_t f, const struct xar_stat *sb) {
 #ifdef HAVE_STRUCT_STAT_ST_FLAGS
 	if( !sb->st_flags )
 		return 0;
@@ -382,8 +423,10 @@ int32_t xar_flags_extract(xar_t x, xar_f
 
 int32_t xar_stat_archive(xar_t x, xar_file_t f, const char *file, const char *buffer, size_t len) {
 	char *tmpstr;
+#ifndef __MINGW32__
 	struct passwd *pw;
 	struct group *gr;
+#endif
 	char time[128];
 	struct tm t;
 	const char *type;
@@ -454,33 +497,41 @@ int32_t xar_stat_archive(xar_t x, xar_fi
 	xar_prop_set(f, "uid", tmpstr);
 	free(tmpstr);
 
+#ifdef __MINGW32__
+	xar_prop_set(f, "user", XAR(x)->sbcache.st_uname);
+#else
 	pw = getpwuid(XAR(x)->sbcache.st_uid);
 	if( pw )
 		xar_prop_set(f, "user", pw->pw_name);
+#endif
 
 	asprintf(&tmpstr, "%"PRIu64, (uint64_t)XAR(x)->sbcache.st_gid);
 	xar_prop_set(f, "gid", tmpstr);
 	free(tmpstr);
 
+#ifdef __MINGW32__
+	xar_prop_set(f, "group", XAR(x)->sbcache.st_gname);
+#else
 	gr = getgrgid(XAR(x)->sbcache.st_gid);
 	if( gr )
 		xar_prop_set(f, "group", gr->gr_name);
+#endif
 
 	gmtime_r(&XAR(x)->sbcache.st_atime, &t);
 	memset(time, 0, sizeof(time));
-	strftime(time, sizeof(time), "%FT%T", &t);
+	strftime(time, sizeof(time), "%Y-%m-%dT%H:%M:%S", &t);
 	strcat(time, "Z");
 	xar_prop_set(f, "atime", time);
 
 	gmtime_r(&XAR(x)->sbcache.st_mtime, &t);
 	memset(time, 0, sizeof(time));
-	strftime(time, sizeof(time), "%FT%T", &t);
+	strftime(time, sizeof(time), "%Y-%m-%dT%H:%M:%S", &t);
 	strcat(time, "Z");
 	xar_prop_set(f, "mtime", time);
 
 	gmtime_r(&XAR(x)->sbcache.st_ctime, &t);
 	memset(time, 0, sizeof(time));
-	strftime(time, sizeof(time), "%FT%T", &t);
+	strftime(time, sizeof(time), "%Y-%m-%dT%H:%M:%S", &t);
 	strcat(time, "Z");
 	xar_prop_set(f, "ctime", time);
 
@@ -529,6 +580,7 @@ int32_t xar_set_perm(xar_t x, xar_file_t
 			}
 		}
 	}
+	opt = xar_opt_get(x, XAR_OPT_OWNERSHIP);
 	if( opt && (strcmp(opt, XAR_OPT_VAL_NUMERIC) == 0) ) {
 		xar_prop_get(f, "uid", &opt);
 		if( opt ) {
@@ -609,13 +661,14 @@ int32_t xar_set_perm(xar_t x, xar_file_t
 			}
 	}
 
-	eacls(x, f, file);
+	if (xar_opt_get(x, XAR_OPT_OWNERSHIP))
+		eacls(x, f, file);
 
 	memset(tv, 0, sizeof(struct timeval) * 2);
 	xar_prop_get(f, "atime", &timestr);
 	if( timestr ) {
 		memset(&t, 0, sizeof(t));
-		strptime(timestr, "%FT%T", &t);
+		strptime(timestr, "%Y-%m-%dT%H:%M:%S", &t);
 		tv[ATIME].tv_sec = timegm(&t);
 	} else {
 		tv[ATIME].tv_sec = time(NULL);
@@ -624,7 +677,7 @@ int32_t xar_set_perm(xar_t x, xar_file_t
 	xar_prop_get(f, "mtime", &timestr);
 	if( timestr ) {
 		memset(&t, 0, sizeof(t));
-		strptime(timestr, "%FT%T", &t);
+		strptime(timestr, "%Y-%m-%dT%H:%M:%S", &t);
 		tv[MTIME].tv_sec = timegm(&t);
 	} else {
 		tv[MTIME].tv_sec = time(NULL);
@@ -776,7 +829,7 @@ int32_t xar_stat_extract(xar_t x, xar_fi
 
 CREATEFILE:
 	unlink(file);
-	fd = open(file, O_RDWR|O_CREAT|O_TRUNC, 0600);
+	fd = open(file, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0600);
 	if( fd > 0 )
 		close(fd);
 	return 0;


--- Makefile.in~0	2007-12-29 20:49:57.000000000 +0200
+++ Makefile.in	2012-03-07 14:32:13.600211400 +0200
@@ -25,10 +25,13 @@
 CPPFLAGS := @CPPFLAGS@
 CFLAGS := @CFLAGS@ 
 A_CFLAGS := $(CFLAGS) 
-S_CFLAGS := $(CFLAGS) -fPIC -DPIC
+S_CFLAGS := $(CFLAGS) -DPIC
 ifeq (macho, @abi@)
 S_CFLAGS += -dynamic
 endif
+ifneq (pei-i386, @abi@)
+S_CFLAGS += -fPIC
+endif
 LDFLAGS := @LDFLAGS@ 
 
 prefix := @PREFIX@
@@ -38,6 +41,8 @@
 includedir := @INCLUDEDIR@
 mandir := @MANDIR@
 
+EXEEXT := @EXEEXT@
+
 PREFIX := $(prefix)
 BINDIR := $(bindir)
 DATADIR := $(datadir)


--- src/xar.c~0	2007-12-29 20:49:57.000000000 +0200
+++ src/xar.c	2012-03-08 16:37:13.326686700 +0200
@@ -50,6 +50,9 @@
 #include <errno.h>
 #include "xar.h"
 #include "config.h"
+#ifndef HAVE_ASPRINTF
+#include "../lib/asprintf.h"
+#endif
 
 #define SYMBOLIC 1
 #define NUMERIC  2
@@ -464,7 +467,7 @@ static int dump_header(const char *filen
 	if(filename == NULL)
 		fd = 0;
 	else {
-		fd = open(filename, O_RDONLY);
+		fd = open(filename, O_RDONLY | O_BINARY);
 		if( fd < 0 ) {
 			perror("open");
 			exit(1);
@@ -476,7 +479,7 @@ static int dump_header(const char *filen
 		exit(1);
 	}
 
-	printf("magic:                  0x%x ", ntohl(xh.magic));
+	printf("magic:                  0x%x ", (uint32_t)ntohl(xh.magic));
 	if( ntohl(xh.magic) != XAR_HEADER_MAGIC )
 		printf("(BAD)\n");
 	else
@@ -485,7 +488,7 @@ static int dump_header(const char *filen
 	printf("version:                %d\n", ntohs(xh.version));
 	printf("Compressed TOC length:  %" PRId64 "\n", xar_ntoh64(xh.toc_length_compressed));
 	printf("Uncompressed TOC length: %" PRId64 "\n", xar_ntoh64(xh.toc_length_uncompressed));
-	printf("Checksum algorithm:     %d ", ntohl(xh.cksum_alg));
+	printf("Checksum algorithm:     %u ", (uint32_t)ntohl(xh.cksum_alg));
 	switch( ntohl(xh.cksum_alg) ) {
 	case XAR_CKSUM_NONE: printf("(none)\n");
 	                     break;
@@ -600,6 +603,9 @@ int main(int argc, char *argv[]) {
 	char **args;
 	const char *tocfile = NULL;
 	int arglen, i, err;
+#ifdef __MINGW32__
+	char *p;
+#endif
 	xar_t x;
 	int loptind = 0;
 	int required_dash_f = 0;  /* This release requires us to use -f */
@@ -793,8 +799,18 @@ int main(int argc, char *argv[]) {
 			arglen = argc - optind;
 			args = malloc(sizeof(char*) * (arglen+1));
 			memset(args, 0, sizeof(char*) * (arglen+1));
-			for( i = 0; i < arglen; i++ )
+			for( i = 0; i < arglen; i++ ) {
 				args[i] = strdup(argv[optind + i]);
+#ifdef __MINGW32__
+				/* Replace all backslashes with
+				   forward slashes, as the rest of the
+				   code relies on that.  */
+				for( p = args[i]; *p; p++ ) {
+					if (*p == '\\')
+						*p = '/';
+				}
+#endif
+			}
 
 			return archive(filename, arglen, args);
 		case 'd':


--- test/buffer.c~0	2007-12-29 20:49:57.000000000 +0200
+++ test/buffer.c	2012-03-11 09:34:20.165020700 +0200
@@ -15,7 +15,7 @@ int main(int argc, char *argv[])
 {
 	int fd;
 	unsigned char *buffer;
-	struct stat sb;
+	struct xar_stat sb;
 	ssize_t red;
 	xar_t x;
 	xar_file_t f, f2;
@@ -25,7 +25,7 @@ int main(int argc, char *argv[])
 		exit(1);
 	}
 
-	fd = open(argv[1], O_RDONLY);
+	fd = open(argv[1], O_RDONLY | O_BINARY);
 	if( fd < 0 ) {
 		fprintf(stderr, "Unable to open file %s\n", argv[1]);
 		exit(2);


--- test/validate.c~0	2007-12-29 20:49:57.000000000 +0200
+++ test/validate.c	2012-03-08 16:39:28.291117700 +0200
@@ -135,7 +135,7 @@ int main(int argc, char *argv[]) {
 		exit(1);
 	}
 
-	fd = open(file, O_RDONLY);
+	fd = open(file, O_RDONLY | O_RDONLY);
 	if( fd < 0 ) {
 		fprintf(stderr, "Error opening archive\n");
 		exit(1);


--- lib/Makefile.inc.in~0	2007-12-29 20:49:57.000000000 +0200
+++ lib/Makefile.inc.in	2012-04-05 09:08:36.330334700 +0300
@@ -18,6 +18,9 @@
 LIBXAR_SRCS := archive.c arcmod.c b64.c bzxar.c darwinattr.c data.c ea.c err.c
 LIBXAR_SRCS += ext2.c fbsdattr.c filetree.c io.c linuxattr.c hash.c signature.c stat.c
 LIBXAR_SRCS += subdoc.c util.c zxar.c script.c macho.c
+ifeq (pei-i386, @abi@)
+LIBXAR_SRCS += w32supp.c
+endif
 
 LIBXAR_SRCS := $(patsubst %, @srcroot@lib/%, $(LIBXAR_SRCS))
 
@@ -50,6 +53,14 @@
 LIBXAR_LNAME :=
 LIBXAR_L :=
 endif
+ifeq (pei-i386, @abi@)
+LIBRXAR_SNAME := librxar.dll.a
+LIBRXAR_LNAME :=
+LIBRXAR_L :=
+LIBXAR_SNAME := libxar.dll.a
+LIBXAR_LNAME :=
+LIBXAR_L :=
+endif
 LIBXAR_LA := @objroot@lib/$(LIBXAR_LANAME)
 LIBXAR_A := @objroot@lib/$(LIBXAR_ANAME)
 LIBRXAR_S := @objroot@lib/$(LIBRXAR_SNAME)
@@ -88,6 +99,12 @@
 	rm -f $(DESTDIR)$(LIBDIR)/$(LIBXAR_LNAME)
 	ln -s $(LIBXAR_SNAME) $(DESTDIR)$(LIBDIR)/$(LIBXAR_LNAME)
 endif
+ifeq (pei-i386, @abi@)
+	@INSTALL@ -d $(DESTDIR)$(BINDIR)
+ifeq (yes, @shared@)
+	@INSTALL@ -m 0755 @objroot@lib/libxar-@LIB_REV@.dll $(DESTDIR)$(BINDIR)
+endif
+endif
 endif
 ifeq (yes, @static@)
 	@INSTALL@ -m 0644 $(LIBXAR_A) $(DESTDIR)$(LIBDIR)
@@ -142,6 +159,9 @@
 ifeq (aout, @abi@)
 	$(CC) -shared -o $@ $+
 endif
+ifeq (pei-i386, @abi@)
+	$(CC) -shared -Wl,--out-implib,$@ -o @objroot@lib/librxar-@LIB_REV@.dll $+ @LIBS@
+endif
 ifneq ($(words "" $(LIBRXAR_L)), 1)
 	rm -f $(LIBRXAR_L)
 	ln -s $(LIBRXAR_SNAME) $(LIBRXAR_L)
@@ -158,6 +178,9 @@
 ifeq (aout, @abi@)
 	$(CC) -shared -o $@ $+
 endif
+ifeq (pei-i386, @abi@)
+	$(CC) -shared -Wl,--out-implib,$@ -o @objroot@lib/libxar-@LIB_REV@.dll $+ @LIBS@
+endif
 ifneq ($(words "" $(LIBXAR_L)), 1)
 	rm -f $(LIBXAR_L)
 	ln -s $(LIBXAR_SNAME) $(LIBXAR_L)
@@ -179,8 +202,10 @@
 $(LIBXAR_LA) : $(LIBXAR_LA).in
 	@mkdir -p @objroot@lib/.libs
 ifeq (yes, @shared@)
+ifneq ($(words "" $(LIBXAR_LNAME)), 1)
 	@ln -sf ../$(LIBXAR_LNAME) @objroot@lib/.libs/$(LIBXAR_LNAME)
 endif
+endif
 ifeq (yes, @static@)
 	@ln -sf ../$(LIBXAR_ANAME) @objroot@lib/.libs/$(LIBXAR_ANAME)
 endif


--- src/Makefile.inc.in~0	2007-12-29 20:49:57.000000000 +0200
+++ src/Makefile.inc.in	2012-03-08 14:30:23.548902700 +0200
@@ -5,21 +5,21 @@
 XAR_SRCS := $(patsubst %, @srcroot@src/%, $(XAR_SRCS))
 -include $(XAR_SRCS:@srcroot@%.c=@objroot@%.d)
 
-src_all : @objroot@src/xar @objroot@src/ixar
+src_all : @objroot@src/xar$(EXEEXT) @objroot@src/ixar$(EXEEXT)
 
-src_install : @objroot@src/ixar
+src_install : @objroot@src/ixar$(EXEEXT)
 	@INSTALL@ -d $(DESTDIR)$(BINDIR)
-	@INSTALL@ -m 0755 $< $(DESTDIR)$(BINDIR)/xar
+	@INSTALL@ -m 0755 $< $(DESTDIR)$(BINDIR)/xar$(EXEEXT)
 	@INSTALL@ -d $(DESTDIR)$(MANDIR)/man1
 	@INSTALL@ -m 0644 @srcroot@src/xar.1 $(DESTDIR)$(MANDIR)/man1
 
 src_uninstall :
-	rm -f $(DESTDIR)/$(BINDIR)/xar
+	rm -f $(DESTDIR)/$(BINDIR)/xar$(EXEEXT)
 	rm -f $(DESTDIR)/$(MANDIR)/man1/xar.1
 
 src_clean :
-	rm -f @objroot@src/xar
-	rm -f @objroot@src/ixar
+	rm -f @objroot@src/xar$(EXEEXT)
+	rm -f @objroot@src/ixar$(EXEEXT)
 	rm -f $(XAR_SRCS:@srcroot@%.c=@objroot@%.o)
 	rm -f $(XAR_SRCS:@srcroot@%.c=@objroot@%.d)
 
@@ -33,14 +33,19 @@
 LIBRXAR := $(LIBXAR_A)
 endif
 
+LOADLIBES :=
+ifeq (pei-i386, @abi@)
+LOADLIBES += -lregex
+endif
+
 # xar links against librxar, so that it can be run without first installing
 # libxar.
-@objroot@src/% : @objroot@src/%.o $(LIBRXAR)
+@objroot@src/%$(EXEEXT) : @objroot@src/%.o $(LIBRXAR)
 	@mkdir -p $(@D)
 ifneq ($(words "" @RPATH@), 1)
-	$(CC) $(CFLAGS) -o $@ $< @RPATH@@abs_objroot@lib $(LDFLAGS) $(LIBRXAR) @LIBS@
+	$(CC) $(CFLAGS) -o $@ $< @RPATH@@abs_objroot@lib $(LDFLAGS) $(LIBRXAR) @LIBS@ $(LOADLIBES)
 else
-	$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) $(LIBRXAR) @LIBS@
+	$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) $(LIBRXAR) @LIBS@ $(LOADLIBES)
 endif
 
 ifeq (yes, @static@)
@@ -51,12 +56,12 @@
 endif
 
 # ixar is the version of the xar binary that gets installed.
-@objroot@src/i% : @objroot@src/%.o $(LIBXAR)
+@objroot@src/i%$(EXEEXT) : @objroot@src/%.o $(LIBXAR)
 	@mkdir -p $(@D)
 ifneq ($(words "" @RPATH@), 1)
-	$(CC) $(CFLAGS) -o $@ $< @RPATH@$(LIBDIR) $(LDFLAGS) $(LIBXAR) @LIBS@
+	$(CC) $(CFLAGS) -o $@ $< @RPATH@$(LIBDIR) $(LDFLAGS) $(LIBXAR) @LIBS@ $(LOADLIBES)
 else
-	$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) $(LIBXAR) @LIBS@
+	$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) $(LIBXAR) @LIBS@ $(LOADLIBES)
 endif
 
 @objroot@src/%.o : @srcroot@src/%.c
