#include "stdafx.h"
#include <stdlib.h>
#include <string.h>
#include "win32lacks.h"

# ifndef zz
#  define zz 127
# endif
static char convchart[256] = {
	zz,zz,zz,zz, zz,zz,zz,zz, zz,zz,zz,zz, zz,zz,zz,zz,// 16  - 1
	zz,zz,zz,zz, zz,zz,zz,zz, zz,zz,zz,zz, zz,zz,zz,zz,// 32  - 2
	zz,zz,zz,zz, zz,zz,zz,zz, zz,zz,zz,zz, zz,zz,zz,zz,// 48  - 3
	zz,zz,zz,zz, zz,zz,zz,zz, zz,zz,zz,zz, zz,zz,zz,zz,// 64  - 4

	zz,zz,zz,zz, zz,zz,zz,zz, zz,zz,zz,zz, zz,zz,zz,zz,// 80  - 5
	zz,zz,zz,zz, zz,zz,zz,zz, zz,zz,zz,zz, zz,zz,zz,zz,// 96  - 6
	zz,65,66,67, 68,69,70,71, 72,73,74,75, 76,77,78,79,// 112 - 7
	80,81,82,83, 84,85,86,87, 88,89,90,zz, zz,zz,zz,zz,// 128 - 8

	zz,zz,zz,zz, zz,zz,zz,zz, zz,zz,zz,zz, zz,zz,zz,zz,// 144 - 9
	zz,zz,zz,zz, zz,zz,zz,zz, zz,zz,zz,zz, zz,zz,zz,zz,// 160 - 10 
	zz,zz,zz,zz, zz,zz,zz,zz, zz,zz,zz,zz, zz,zz,zz,zz,// 176 - 11
	zz,zz,zz,zz, zz,zz,zz,zz, zz,zz,zz,zz, zz,zz,zz,zz,// 192 - 12

	zz,zz,zz,zz, zz,zz,zz,zz, zz,zz,zz,zz, zz,zz,zz,zz,// 208 - 13
	zz,zz,zz,zz, zz,zz,zz,zz, zz,zz,zz,zz, zz,zz,zz,zz,// 224 - 14
	zz,zz,zz,zz, zz,zz,zz,zz, zz,zz,zz,zz, zz,zz,zz,zz,// 240 - 15
	zz,zz,zz,zz, zz,zz,zz,zz, zz,zz,zz,zz, zz,zz,zz,zz,// 256 - 16
};

int win32_strncasecmp (const char *sa, const char *sb, size_t n) {
	unsigned int i, ret;
	char *tsa, *tsb;

	if (!sa) return -1;
	if (!sb) return 1;
	tsa=_strdup (sa);
	tsb=_strdup (sb);

	for (i=0; i < strlen (tsa); i++)
		if (tsa[i] >= 97 && tsa[i] <= 122)
			tsa[i]=convchart[tsa[i]];

	for (i=0; i < strlen (tsb); i++)
		if (tsb[i] >= 97 && tsb[i] <= 122)
			tsb[i]=convchart[tsb[i]];

	ret=strncmp (tsa, tsb, n);
	free (tsa);
	free (tsb);
	return ret;
}