portability fixes

diff --git a/libasn1fix/asn1fix.c b/libasn1fix/asn1fix.c
index f120853..22cd367 100644
--- a/libasn1fix/asn1fix.c
+++ b/libasn1fix/asn1fix.c
@@ -1,10 +1,5 @@
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-#include <stdarg.h>
-
-#include "asn1fix.h"
 #include "asn1fix_internal.h"
+#include "asn1fix.h"
 
 /* Print everything to stderr */
 static void _default_error_logger(int _severity, const char *fmt, ...);
diff --git a/libasn1fix/asn1fix_constraint.c b/libasn1fix/asn1fix_constraint.c
index 8c6c73c..9a32b49 100644
--- a/libasn1fix/asn1fix_constraint.c
+++ b/libasn1fix/asn1fix_constraint.c
@@ -1,6 +1,6 @@
-#include <asn1fix_internal.h>
-#include <asn1fix_constraint.h>
-#include <asn1fix_crange.h>
+#include "asn1fix_internal.h"
+#include "asn1fix_constraint.h"
+#include "asn1fix_crange.h"
 
 static void _remove_exceptions(arg_t *arg, asn1p_constraint_t *ct);
 static int _constraint_value_resolve(arg_t *arg, asn1p_module_t *mod, asn1p_value_t **value);
diff --git a/libasn1fix/asn1fix_constraint_compat.c b/libasn1fix/asn1fix_constraint_compat.c
index effa0ad..5eb93f0 100644
--- a/libasn1fix/asn1fix_constraint_compat.c
+++ b/libasn1fix/asn1fix_constraint_compat.c
@@ -1,5 +1,5 @@
-#include <asn1fix_internal.h>
-#include <asn1fix_crange.h>
+#include "asn1fix_internal.h"
+#include "asn1fix_crange.h"
 
 /*
  * Check that a specific constraint is compatible
diff --git a/libasn1fix/asn1fix_crange.c b/libasn1fix/asn1fix_crange.c
index 974aa21..1e6a0d0 100644
--- a/libasn1fix/asn1fix_crange.c
+++ b/libasn1fix/asn1fix_crange.c
@@ -1,6 +1,6 @@
-#include <asn1fix_internal.h>
-#include <asn1fix_constraint.h>
-#include <asn1fix_crange.h>
+#include "asn1fix_internal.h"
+#include "asn1fix_constraint.h"
+#include "asn1fix_crange.h"
 
 #undef	FATAL
 #define	FATAL(fmt, args...)	do {			\
diff --git a/libasn1fix/asn1fix_cstring.c b/libasn1fix/asn1fix_cstring.c
index b8a3883..9cbb42c 100644
--- a/libasn1fix/asn1fix_cstring.c
+++ b/libasn1fix/asn1fix_cstring.c
@@ -2,7 +2,7 @@
 
 struct _cstring_pattern {
 	char *start;
-	int length;
+	size_t length;
 };
 static int _asn1f_cstring_find_line_pattern(char *s, struct _cstring_pattern *);
 
@@ -55,7 +55,7 @@
 		case '\0':
 		default:
 			if(newline_found) {
-				cp->length = s - cp->start;
+				cp->length = (size_t)(s - cp->start);
 				return 1;
 			}
 
diff --git a/libasn1fix/asn1fix_export.h b/libasn1fix/asn1fix_export.h
index 5879b93..99d80b7 100644
--- a/libasn1fix/asn1fix_export.h
+++ b/libasn1fix/asn1fix_export.h
@@ -5,7 +5,7 @@
 #ifndef	_ASN1FIX_EXPORT_H_
 #define	_ASN1FIX_EXPORT_H_
 
-#include <asn1fix_tags.h>
+#include "asn1fix_tags.h"
 
 /*
  * Create a human-readable representation of a reference and value.
diff --git a/libasn1fix/asn1fix_internal.h b/libasn1fix/asn1fix_internal.h
index 040750d..6b15778 100644
--- a/libasn1fix/asn1fix_internal.h
+++ b/libasn1fix/asn1fix_internal.h
@@ -10,13 +10,29 @@
  */
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdarg.h>
 #include <string.h>
 #include <ctype.h>		/* isupper() */
 #include <errno.h>
 #include <assert.h>
 
+#ifdef	HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
 #include <asn1parser.h>		/* Our lovely ASN.1 parser module */
-#include <asn1fix.h>
+#include "asn1fix.h"
+
+#ifdef	WIN32
+#define	EX_NOINPUT	66
+#define	EX_DATAERR	65
+#define	snprintf	_snprintf
+#define	strcasecmp	stricmp
+#endif
+
+#ifndef	ETOOMANYREFS
+#define	ETOOMANYREFS	144
+#endif
 
 /*
  * A definition of a function that will log error messages.
diff --git a/libasn1fix/asn1fix_misc.c b/libasn1fix/asn1fix_misc.c
index e9d156c..15a1686 100644
--- a/libasn1fix/asn1fix_misc.c
+++ b/libasn1fix/asn1fix_misc.c
@@ -19,11 +19,11 @@
 asn1f_printable_value(asn1p_value_t *v) {
 	static char buf[128];
 	static char *managedptr;
-	static int managedptr_len;
+	static size_t managedptr_len;
 	int ret;
 
 #define	ENSURE(len)	do {						\
-		int __len = (len);					\
+		size_t __len = (len);					\
 		if(__len >= managedptr_len) {				\
 			if(managedptr)					\
 				free(managedptr);			\
@@ -69,7 +69,7 @@
 		{
 			uint8_t *bitvector;
 			char *ptr;
-			int len;
+			size_t len;
 			int i;
 			/*
 			 * Compute number of bytes necessary
@@ -110,13 +110,13 @@
 			*ptr++ = '\'';
 			*ptr++ = (bits%8)?'B':'H';
 			*ptr++ = 'H';
-			assert((ptr - managedptr) == len);
+			assert(len == (size_t)(ptr - managedptr));
 			return managedptr;
 		}
 	case ATV_REFERENCED:
 		{
 			asn1p_ref_t *ref;
-			char reflen;
+			size_t reflen;
 			char *ptr;
 			int i;
 
@@ -141,7 +141,7 @@
 					*ptr++ = *nc;
 			}
 			*ptr++ = '\0';
-			assert(reflen == (ptr - managedptr));
+			assert(reflen == (size_t)(ptr - managedptr));
 			return managedptr;
 		}
 	case ATV_CHOICE_IDENTIFIER:
@@ -158,7 +158,7 @@
 
 			ret = snprintf(managedptr, managedptr_len + 1,
 				"%s: %s", cid, val);
-			assert(ret >= 0 && ret <= managedptr_len);
+			assert(ret >= 0 && (size_t)ret <= managedptr_len);
 			free(val);
 			return managedptr;
 		}
diff --git a/libasn1fix/check_fixer.c b/libasn1fix/check_fixer.c
index 7129a56..8808f28 100644
--- a/libasn1fix/check_fixer.c
+++ b/libasn1fix/check_fixer.c
@@ -1,15 +1,17 @@
 #undef	NDEBUG
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/types.h>
-#include <unistd.h>
+#include "asn1fix_internal.h"
+
+#ifdef	WIN32
+#include <io.h>
+#include <direct.h>
+#define	chdir _chdir
+#else
 #include <dirent.h>
-#include <errno.h>
 #include <sysexits.h>
+#endif
+#include <errno.h>
 
 #include "asn1fix.h"
-#include "asn1fix_internal.h"
 
 static int check(const char *fname,
 	enum asn1p_flags parser_flags,
@@ -19,12 +21,19 @@
 
 int
 main(int ac, char **av) {
+#ifdef	WIN32
+	intptr_t dir;
+	struct _finddata_t c_file;
+#else
 	struct dirent *dp;
 	DIR *dir;
+#endif
 	int failed = 0;
 	int completed = 0;
 	enum asn1p_flags parser_flags = A1P_NOFLAGS;
 	enum asn1f_flags fixer_flags  = A1F_NOFLAGS;
+	const char *filename;
+	int len;
 	int ret;
 
 	/*
@@ -44,8 +53,13 @@
 		fprintf(stderr, "Testing in ./tests...\n");
 		ret = chdir("../tests");
 		assert(ret == 0);
+#ifdef	WIN32
+		dir = _findfirst("*.asn1", &c_file);
+		assert(dir != -1L);
+#else
 		dir = opendir(".");
 		assert(dir);
+#endif	/* WIN32 */
 	} else {
 		dir = 0;
 	}
@@ -54,21 +68,31 @@
 	 * Scan every *.asn1 file and try to parse and fix it.
 	 */
 	if(dir) {
+#ifdef	WIN32
+		do {
+			filename = c_file.name;
+#else
 		while((dp = readdir(dir))) {
-			int len = strlen(dp->d_name);
-			if(len && strcmp(dp->d_name + len - 5, ".asn1") == 0) {
-				ret = check(dp->d_name,
-					parser_flags, fixer_flags);
+			filename = dp->d_name;
+#endif	/* WIN32 */
+			int len = strlen(filename);
+			if(len && strcmp(filename + len - 5, ".asn1") == 0) {
+				ret = check(filename, parser_flags,fixer_flags);
 				if(ret) {
-					fprintf(stderr,
-						"FAILED: %s\n",
-						dp->d_name);
+					fprintf(stderr, "FAILED: %s\n",
+						filename);
 					failed++;
 				}
 				completed++;
 			}
+#ifdef	WIN32
+		} while(_findnext(dir, &c_file) == 0);
+		_findclose(dir);
+#else
 		}
 		closedir(dir);
+#endif	/* WIN32 */
+
 
 		fprintf(stderr,
 			"Tests COMPLETED: %d\n"