/*
 * Copyright (C) 1984-2026  Mark Nudelman
 *
 * You may distribute under the terms of either the GNU General Public
 * License or the Less License, as specified in the README file.
 *
 * For more information, see the README file.
 */


/*
 * Functions to define the character set
 * and do things specific to the character set.
 */

#include "less.h"
#if HAVE_LOCALE
#include <locale.h>
#include <ctype.h>
#include <langinfo.h>
#endif

#include "charset.h"
#include "xbuf.h"

#if MSDOS_COMPILER==WIN32C
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif

extern int bs_mode;

public int utf_mode = 0;

/*
 * Predefined character sets,
 * selected by the LESSCHARSET environment variable.
 */
struct charset {
	char *name;
	int *p_flag;
	char *desc;
} charsets[] = {
		{ "ascii",              NULL,       "8bcccbcc18b95.b" },
		{ "utf-8",              &utf_mode,  "8bcccbcc18b95.b126.bb" },
		{ "iso8859",            NULL,       "8bcccbcc18b95.33b." },
		{ "latin3",             NULL,       "8bcccbcc18b95.33b5.b8.b15.b4.b12.b18.b12.b." },
		{ "arabic",             NULL,       "8bcccbcc18b95.33b.3b.7b2.13b.3b.b26.5b19.b" },
		{ "greek",              NULL,       "8bcccbcc18b95.33b4.2b4.b3.b35.b44.b" },
		{ "greek2005",          NULL,       "8bcccbcc18b95.33b14.b35.b44.b" },
		{ "hebrew",             NULL,       "8bcccbcc18b95.33b.b29.32b28.2b2.b" },
		{ "koi8-r",             NULL,       "8bcccbcc18b95.b." },
		{ "KOI8-T",             NULL,       "8bcccbcc18b95.b8.b6.b8.b.b.5b7.3b4.b4.b3.b.b.3b." },
		{ "georgianps",         NULL,       "8bcccbcc18b95.3b11.4b12.2b." },
		{ "tcvn",               NULL,       "b..b...bcccbccbbb7.8b95.b48.5b." },
		{ "TIS-620",            NULL,       "8bcccbcc18b95.b.4b.11b7.8b." },
		{ "next",               NULL,       "8bcccbcc18b95.bb125.bb" },
		{ "dos",                NULL,       "8bcccbcc12bc5b95.b." },
		{ "windows-1251",       NULL,       "8bcccbcc12bc5b95.b24.b." },
		{ "windows-1252",       NULL,       "8bcccbcc12bc5b95.b.b11.b.2b12.b." },
		{ "windows-1255",       NULL,       "8bcccbcc12bc5b95.b.b8.b.5b9.b.4b." },
		{ "ebcdic",             NULL,       "5bc6bcc7bcc41b.9b7.9b5.b..8b6.10b6.b9.7b9.8b8.17b3.3b9.7b9.8b8.6b10.b.b.b." },
		{ "IBM-1047",           NULL,       "4cbcbc3b9cbccbccbb4c6bcc5b3cbbc4bc4bccbc191.b" },
		{ NULL, NULL, NULL }
};

/*
 * Support "locale charmap"/nl_langinfo(CODESET) values, as well as others.
 */
struct cs_alias {
	char *name;
	char *oname;
} cs_aliases[] = {
	{ "UTF-8",              "utf-8" },
	{ "utf8",               "utf-8" },
	{ "UTF8",               "utf-8" },
	{ "ANSI_X3.4-1968",     "ascii" },
	{ "US-ASCII",           "ascii" },
	{ "latin1",             "iso8859" },
	{ "ISO-8859-1",         "iso8859" },
	{ "latin9",             "iso8859" },
	{ "ISO-8859-15",        "iso8859" },
	{ "latin2",             "iso8859" },
	{ "ISO-8859-2",         "iso8859" },
	{ "ISO-8859-3",         "latin3" },
	{ "latin4",             "iso8859" },
	{ "ISO-8859-4",         "iso8859" },
	{ "cyrillic",           "iso8859" },
	{ "ISO-8859-5",         "iso8859" },
	{ "ISO-8859-6",         "arabic" },
	{ "ISO-8859-7",         "greek" },
	{ "IBM9005",            "greek2005" },
	{ "ISO-8859-8",         "hebrew" },
	{ "latin5",             "iso8859" },
	{ "ISO-8859-9",         "iso8859" },
	{ "latin6",             "iso8859" },
	{ "ISO-8859-10",        "iso8859" },
	{ "latin7",             "iso8859" },
	{ "ISO-8859-13",        "iso8859" },
	{ "latin8",             "iso8859" },
	{ "ISO-8859-14",        "iso8859" },
	{ "latin10",            "iso8859" },
	{ "ISO-8859-16",        "iso8859" },
	{ "IBM437",             "dos" },
	{ "EBCDIC-US",          "ebcdic" },
	{ "IBM1047",            "IBM-1047" },
	{ "KOI8-R",             "koi8-r" },
	{ "KOI8-U",             "koi8-r" },
	{ "GEORGIAN-PS",        "georgianps" },
	{ "TCVN5712-1",         "tcvn" },
	{ "NEXTSTEP",           "next" },
	{ "windows",            "windows-1252" }, /* backward compatibility */
	{ "CP1251",             "windows-1251" },
	{ "CP1252",             "windows-1252" },
	{ "CP1255",             "windows-1255" },
	{ NULL, NULL }
};

#define IS_BINARY_CHAR  01
#define IS_CONTROL_CHAR 02

static char chardef[256];
static constant char *binfmt = NULL;
static constant char *utfbinfmt = NULL;
public int binattr = AT_STANDOUT|AT_COLOR_BIN;

static struct xbuffer user_wide_array;
static struct xbuffer user_ubin_array;
static struct xbuffer user_compose_array;
static struct xbuffer user_prt_array;
static struct xbuffer user_omit_array;
static struct wchar_range_table user_wide_table;
static struct wchar_range_table user_ubin_table;
static struct wchar_range_table user_compose_table;
static struct wchar_range_table user_prt_table;
static struct wchar_range_table user_omit_table;

/*
 * Set a wchar_range_table to the table in an xbuffer.
 */
static void wchar_range_table_set(struct wchar_range_table *tbl, struct xbuffer *arr)
{
	tbl->table = (struct wchar_range *) arr->data;
	tbl->count = (unsigned int) (arr->end / sizeof(struct wchar_range));
}

/*
 * Skip over a "U" or "U+" prefix before a hex codepoint.
 */
static constant char * skip_uprefix(constant char *s)
{
	if (*s == 'U' || *s == 'u')
		if (*++s == '+') ++s;
	return s;
}

/*
 * Parse a dash-separated range of hex values.
 */
static void wchar_range_get(constant char **ss, struct wchar_range *range)
{
	constant char *s = skip_uprefix(*ss);
	range->first = lstrtoulc(s, &s, 16);
	if (s[0] == '-')
	{
		s = skip_uprefix(&s[1]);
		range->last = lstrtoulc(s, &s, 16);
	} else 
	{
		range->last = range->first;
	}
	*ss = s;
}

/*
 * Parse the LESSUTFCHARDEF variable.
 */
static void ichardef_utf(constant char *s)
{
	xbuf_init(&user_wide_array);
	xbuf_init(&user_ubin_array);
	xbuf_init(&user_compose_array);
	xbuf_init(&user_prt_array);
	xbuf_init(&user_omit_array);

	if (s != NULL)
	{
		while (s[0] != '\0')
		{
			struct wchar_range range;
			wchar_range_get(&s, &range);
			if (range.last == 0)
			{
				error(LM(invalid_hex_number_in_LESSUTFCHARDEF), NULL_PARG);
				quit(QUIT_ERROR);
			}
			if (*s++ != ':')
			{
				error(LM(missing_colon_in_LESSUTFCHARDEF), NULL_PARG);
				quit(QUIT_ERROR);
			}
			switch (*s++)
			{
			case 'b':
				xbuf_add_data(&user_ubin_array, &range, sizeof(range));
				break;
			case 'c':
				xbuf_add_data(&user_compose_array, &range, sizeof(range));
				break;
			case 'd':
				xbuf_add_data(&user_omit_array, &range, sizeof(range));
				break;
			case 'w':
				xbuf_add_data(&user_wide_array, &range, sizeof(range));
				xbuf_add_data(&user_prt_array, &range, sizeof(range));
				break;
			case 'p': case '.':
				xbuf_add_data(&user_prt_array, &range, sizeof(range));
				break;
			case '\0':
				s--;
				break;
			default:
				/* Ignore unknown character attribute. */
				break;
			}
			if (s[0] == ',') ++s;
		}
	}
	wchar_range_table_set(&user_wide_table, &user_wide_array);
	wchar_range_table_set(&user_ubin_table, &user_ubin_array);
	wchar_range_table_set(&user_compose_table, &user_compose_array);
	wchar_range_table_set(&user_prt_table, &user_prt_array);
	wchar_range_table_set(&user_omit_table, &user_omit_array);
}

/*
 * Define a charset, given a description string.
 * The string consists of 256 letters,
 * one for each character in the charset.
 * If the string is shorter than 256 letters, missing letters
 * are taken to be identical to the last one.
 * A decimal number followed by a letter is taken to be a 
 * repetition of the letter.
 *
 * Each letter is one of:
 *      . normal character
 *      b binary character
 *      c control character
 */
static void ichardef(constant char *s)
{
	char *cp;
	int n;
	char v;

	n = 0;
	v = 0;
	cp = chardef;
	while (*s != '\0')
	{
		switch (*s++)
		{
		case '.':
			v = 0;
			break;
		case 'c':
			v = IS_CONTROL_CHAR;
			break;
		case 'b':
			v = IS_BINARY_CHAR|IS_CONTROL_CHAR;
			break;

		case '0': case '1': case '2': case '3': case '4':
		case '5': case '6': case '7': case '8': case '9':
			if (ckd_mul(&n, n, 10) || ckd_add(&n, n, s[-1] - '0'))
				goto invalid_chardef;
			continue;

		default:
		invalid_chardef:
			error(LM(invalid_chardef), NULL_PARG);
			quit(QUIT_ERROR);
			/*NOTREACHED*/
		}

		do
		{
			if (cp >= chardef + sizeof(chardef))
			{
				error(LM(chardef_longer_than_256), NULL_PARG);
				quit(QUIT_ERROR);
				/*NOTREACHED*/
			}
			*cp++ = v;
		} while (--n > 0);
		n = 0;
	}

	while (cp < chardef + sizeof(chardef))
		*cp++ = v;
}

/*
 * Define a charset, given a charset name.
 * The valid charset names are listed in the "charsets" array.
 */
static int icharset(constant char *name, lbool no_error)
{
	struct charset *p;
	struct cs_alias *a;

	if (name == NULL || *name == '\0')
		return (0);

	/* First see if the name is an alias. */
	for (a = cs_aliases;  a->name != NULL;  a++)
	{
		if (strcmp(name, a->name) == 0)
		{
			name = a->oname;
			break;
		}
	}

	for (p = charsets;  p->name != NULL;  p++)
	{
		if (strcmp(name, p->name) == 0)
		{
			ichardef(p->desc);
			if (p->p_flag != NULL)
			{
#if MSDOS_COMPILER==WIN32C
				*(p->p_flag) = 1 + (GetConsoleOutputCP() != CP_UTF8);
#else
				*(p->p_flag) = 1;
#endif
			}
			return (1);
		}
	}

	if (!no_error) {
		error(LM(invalid_charset_name), NULL_PARG);
		quit(QUIT_ERROR);
	}
	return (0);
}

#if HAVE_LOCALE
/*
 * Define a charset, given a locale name.
 */
static void ilocale(void)
{
	size_t c;

	for (c = 0;  c < sizeof(chardef);  c++)
	{
		if (isprint(c))
			chardef[c] = 0;
		else if (iscntrl(c))
			chardef[c] = IS_CONTROL_CHAR;
		else
			chardef[c] = IS_BINARY_CHAR|IS_CONTROL_CHAR;
	}
}
#endif

/*
 * Define the printing format for control (or binary utf) chars.
 */
public void setfmt(constant char *s, constant char **fmtvarptr, int *attrptr, constant char *default_fmt, lbool for_printf)
{
	if (s == NULL || *s == '\0')
		s = default_fmt;
	else if (for_printf &&
	    ((*s == '*' && (s[1] == '\0' || s[2] == '\0' || strchr(s + 2, 'n'))) ||
	     (*s != '*' && strchr(s, 'n'))))
		/* %n is evil */
		s = default_fmt;

	/*
	 * Select the attributes if it starts with "*".
	 */
	if (*s == '*' && s[1] != '\0')
	{
		switch (s[1])
		{
		case 'd':  *attrptr = AT_BOLD;      break;
		case 'k':  *attrptr = AT_BLINK;     break;
		case 's':  *attrptr = AT_STANDOUT;  break;
		case 'u':  *attrptr = AT_UNDERLINE; break;
		default:   *attrptr = AT_NORMAL;    break;
		}
		s += 2;
	}
	*fmtvarptr = s;
}

/*
 *
 */
static void set_charset(void)
{
	constant char *s;

	ichardef_utf(lgetenv("LESSUTFCHARDEF"));

	/*
	 * See if environment variable LESSCHARSET is defined.
	 */
	s = lgetenv("LESSCHARSET");
	if (icharset(s, FALSE))
		return;

	/*
	 * LESSCHARSET is not defined: try LESSCHARDEF.
	 */
	s = lgetenv("LESSCHARDEF");
	if (!isnullenv(s))
	{
		ichardef(s);
		return;
	}

#if HAVE_LOCALE
#ifdef CODESET
	/*
	 * Try using the codeset name as the charset name.
	 */
#if LESSTEST
	/*
	 * Don't check nl_langinfo in lesstest mode; charset should come
	 * only from environment variables, not from the system locale.
	 */
	if (0) /* {{ unfortunately it's too early to use is_lesstest }} */
#endif
	{
		s = nl_langinfo(CODESET);
		if (icharset(s, TRUE))
			return;
	}
#endif
#endif

	/*
	 * Check whether LC_ALL, LC_CTYPE or LANG look like UTF-8 is used.
	 */
	if ((s = lgetenv("LC_ALL")) != NULL ||
	    (s = lgetenv("LC_CTYPE")) != NULL ||
	    (s = lgetenv("LANG")) != NULL)
	{
		if (   strstr(s, "UTF-8") != NULL || strstr(s, "utf-8") != NULL
		    || strstr(s, "UTF8")  != NULL || strstr(s, "utf8")  != NULL)
			if (icharset("utf-8", TRUE))
				return;
	}

#if HAVE_LOCALE
	/*
	 * Get character definitions from locale functions,
	 * rather than from predefined charset entry.
	 */
	ilocale();
#else
#if MSDOS_COMPILER
#if MSDOS_COMPILER==WIN32C
	(void) icharset("utf-8", TRUE);
#else
	(void) icharset("dos", TRUE);
#endif
#else
	(void) icharset("utf-8", TRUE);
#endif
#endif
}

/*
 * Initialize charset data structures.
 */
public void init_charset(void)
{
	constant char *s;

#if HAVE_LOCALE
	setlocale(LC_ALL, "");
#endif

	set_charset();

	s = lgetenv("LESSBINFMT");
	setfmt(s, &binfmt, &binattr, "*s<%02X>", TRUE);
	
	s = lgetenv("LESSUTFBINFMT");
	setfmt(s, &utfbinfmt, &binattr, "<U+%04lX>", TRUE);
}

/*
 * Is a given character a "binary" character?
 */
public lbool binary_char(LWCHAR c)
{
	if (utf_mode)
		return (is_ubin_char(c));
	if (c >= sizeof(chardef))
		return TRUE;
	return ((chardef[c] & IS_BINARY_CHAR) != 0);
}

/*
 * Is a given character a "control" character?
 */
public lbool control_char(LWCHAR c)
{
	if (c >= sizeof(chardef))
		return TRUE;
	return (chardef[c] & IS_CONTROL_CHAR);
}

/*
 * Return the printable form of a character.
 * For example, in the "ascii" charset '\3' is printed as "^C".
 */
public constant char * prchar(LWCHAR c)
{
	/* {{ Fixed buffer size means LESSBINFMT etc can be truncated. }} */
	static char buf[MAX_PRCHAR_LEN+1];

	c = (unsigned char) c; /* {{ parameter is interpreted as a byte }} */
	if ((c < 128 || !utf_mode) && !control_char(c))
		SNPRINTF1(buf, sizeof(buf), "%c", (int) c);
	else if (c == ESC)
		strcpy(buf, "ESC");
#if IS_EBCDIC_HOST
	else if (!binary_char(c) && c < 64)
		SNPRINTF1(buf, sizeof(buf), "^%c",
		/*
		 * This array roughly inverts CONTROL() #defined in less.h,
		 * and should be kept in sync with CONTROL() and IBM-1047.
		 */
		"@ABC.I.?...KLMNO"
		"PQRS.JH.XY.."
		"\\]^_"
		"......W[.....EFG"
		"..V....D....TU.Z"[c]);
#else
	else if (c < 128 && !control_char(c ^ 0100))
		SNPRINTF1(buf, sizeof(buf), "^%c", (int) (c ^ 0100));
#endif
	else
		SNPRINTF1(buf, sizeof(buf), binfmt, c);
	return (buf);
}

/*
 * Return the printable form of a UTF-8 character.
 */
public constant char * prutfchar(LWCHAR ch)
{
	static char buf[MAX_PRCHAR_LEN+1];

	if (ch == ESC)
		strcpy(buf, "ESC");
	else if (ch < 128 && control_char(ch))
	{
		if (!control_char(ch ^ 0100))
			SNPRINTF1(buf, sizeof(buf), "^%c", ((char) ch) ^ 0100);
		else
			SNPRINTF1(buf, sizeof(buf), binfmt, (char) ch);
	} else if (is_ubin_char(ch) || is_omit_char(ch))
	{
		SNPRINTF1(buf, sizeof(buf), utfbinfmt, ch);
	} else
	{
		char *p = buf;
		if (ch >= 0x80000000)
			ch = 0xFFFD; /* REPLACEMENT CHARACTER */
		put_wchar(&p, ch);
		*p = '\0';
	}
	return (buf);
}

/*
 * Get the length of a UTF-8 character in bytes.
 */
public int utf_len(char ach)
{
	unsigned char ch = (unsigned char) ach;
	if (IS_ASCII_OCTET(ch))
		return 1;
	if (IS_UTF8_LEAD2(ch))
		return 2;
	if (IS_UTF8_LEAD3(ch))
		return 3;
	if (IS_UTF8_LEAD4(ch))
		return 4;
#if 0
	if (IS_UTF8_LEAD5(ch))
		return 5;
	if (IS_UTF8_LEAD6(ch))
		return 6;
#endif
	/* Invalid UTF-8 encoding. */
	return 1;
}

/*
 * Does the parameter point to the lead byte of a well-formed UTF-8 character?
 */
public lbool is_utf8_well_formed(constant char *ssa, int slen)
{
	int i;
	int len;
	constant unsigned char *ss = (constant unsigned char *) ssa;
	unsigned char s0 = ss[0];

	if (IS_UTF8_LEAD5(s0) || IS_UTF8_LEAD6(s0) || IS_UTF8_INVALID(s0))
		return (FALSE); /* invalid lead byte */

	len = utf_len(s0);
	if (len > slen)
		return (FALSE); /* UTF-8 sequence doesn't fit in buffer */
	if (len == 1)
		return (TRUE);
	if (len == 2)
	{
		if (s0 < 0xC2)
			return (FALSE); /* overlong (fits in 1 byte) */
	} else
	{
		/* mask = byte with len high-order bits set to 1, others 0 */
		unsigned char mask = (unsigned char) (~((1 << (8-len)) - 1));
		if (s0 == mask && (ss[1] & mask) == 0x80)
			return (FALSE); /* overlong (fits in fewer bytes) */
	}

	for (i = 1;  i < len;  i++)
		if (!IS_UTF8_TRAIL(ss[i]))
			return (FALSE); /* not a continuation byte */
	return (TRUE);
}

/*
 * Skip bytes until a UTF-8 lead byte (11xxxxxx) or ASCII byte (0xxxxxxx) is found.
 */
public void utf_skip_to_lead(constant char **pp, constant char *limit)
{
	do {
		++(*pp);
	} while (*pp < limit && !IS_UTF8_LEAD((*pp)[0] & 0377) && !IS_ASCII_OCTET((*pp)[0]));
}

/*
 * Get the value of a UTF-8 character.
 */
public LWCHAR get_wchar(constant char *sp)
{
	constant unsigned char *p = (constant unsigned char *) sp;
	switch (utf_len(sp[0]))
	{
	case 1:
	default:
		/* 0xxxxxxx */
		return (LWCHAR)
			(p[0] & 0xFF);
	case 2:
		/* 110xxxxx 10xxxxxx */
		return (LWCHAR) (
			((p[0] & 0x1F) << 6) |
			(p[1] & 0x3F));
	case 3:
		/* 1110xxxx 10xxxxxx 10xxxxxx */
		return (LWCHAR) (
			((p[0] & 0x0F) << 12) |
			((p[1] & 0x3F) << 6) |
			(p[2] & 0x3F));
	case 4:
		/* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
		return (LWCHAR) (
			((p[0] & 0x07) << 18) |
			((p[1] & 0x3F) << 12) | 
			((p[2] & 0x3F) << 6) | 
			(p[3] & 0x3F));
#if 0
	case 5:
		/* 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
		return (LWCHAR) (
			((p[0] & 0x03) << 24) |
			((p[1] & 0x3F) << 18) | 
			((p[2] & 0x3F) << 12) | 
			((p[3] & 0x3F) << 6) | 
			(p[4] & 0x3F));
	case 6:
		/* 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
		return (LWCHAR) (
			((p[0] & 0x01) << 30) |
			((p[1] & 0x3F) << 24) | 
			((p[2] & 0x3F) << 18) | 
			((p[3] & 0x3F) << 12) | 
			((p[4] & 0x3F) << 6) | 
			(p[5] & 0x3F));
#endif
	}
}

/*
 * Store a character into a UTF-8 string.
 */
public void put_wchar(mutable char **pp, LWCHAR ch)
{
	if (!utf_mode || ch < 0x80) 
	{
		/* 0xxxxxxx */
		*(*pp)++ = (char) ch;
	} else if (ch < 0x800)
	{
		/* 110xxxxx 10xxxxxx */
		*(*pp)++ = (char) (0xC0 | ((ch >> 6) & 0x1F));
		*(*pp)++ = (char) (0x80 | (ch & 0x3F));
	} else if (ch < 0x10000)
	{
		/* 1110xxxx 10xxxxxx 10xxxxxx */
		*(*pp)++ = (char) (0xE0 | ((ch >> 12) & 0x0F));
		*(*pp)++ = (char) (0x80 | ((ch >> 6) & 0x3F));
		*(*pp)++ = (char) (0x80 | (ch & 0x3F));
	} else if (ch < 0x200000)
	{
		/* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
		*(*pp)++ = (char) (0xF0 | ((ch >> 18) & 0x07));
		*(*pp)++ = (char) (0x80 | ((ch >> 12) & 0x3F));
		*(*pp)++ = (char) (0x80 | ((ch >> 6) & 0x3F));
		*(*pp)++ = (char) (0x80 | (ch & 0x3F));
#if 0
	} else if (ch < 0x4000000)
	{
		/* 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
		*(*pp)++ = (char) (0xF0 | ((ch >> 24) & 0x03));
		*(*pp)++ = (char) (0x80 | ((ch >> 18) & 0x3F));
		*(*pp)++ = (char) (0x80 | ((ch >> 12) & 0x3F));
		*(*pp)++ = (char) (0x80 | ((ch >> 6) & 0x3F));
		*(*pp)++ = (char) (0x80 | (ch & 0x3F));
	} else 
	{
		/* 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
		*(*pp)++ = (char) (0xF0 | ((ch >> 30) & 0x01));
		*(*pp)++ = (char) (0x80 | ((ch >> 24) & 0x3F));
		*(*pp)++ = (char) (0x80 | ((ch >> 18) & 0x3F));
		*(*pp)++ = (char) (0x80 | ((ch >> 12) & 0x3F));
		*(*pp)++ = (char) (0x80 | ((ch >> 6) & 0x3F));
		*(*pp)++ = (char) (0x80 | (ch & 0x3F));
#endif
	}
}

/*
 * Step forward (dir > 0) or backward (dir < 0) one character in a string.
 * If dir > 0, limit is end of string; if dir < 0, limit is start of string.
 */
public LWCHAR step_charc(constant char **pp, signed int dir, constant char *limit)
{
	LWCHAR ch;
	int len;
	constant char *p = *pp;

	if (!utf_mode)
	{
		/* It's easy if chars are one byte. */
		if (dir > 0)
			ch = (LWCHAR) (unsigned char) ((p < limit) ? *p++ : 0);
		else
			ch = (LWCHAR) (unsigned char) ((p > limit) ? *--p : 0);
	} else if (dir > 0)
	{
		if (p >= limit)
			ch = 0;
		else 
		{
			len = utf_len(*p);
			if (p + len > limit || !is_utf8_well_formed(p, len))
			{
				/* Invalid UTF-8 sequence; just take the next byte as a character. */
				ch = (LWCHAR) (unsigned char) *p++;
			} else
			{
				ch = get_wchar(p);
				p += len;
			}
		}
	} else
	{
		while (p > limit && IS_UTF8_TRAIL(p[-1]))
			p--;
		if (p <= limit)
			ch = 0;
		else
		{
			len = utf_len(*--p);
			if (p + len != *pp || !is_utf8_well_formed(p, len))
			{
				/* Invalid UTF-8 sequence; just take the next byte as a character. */
				p = *pp - 1;
				ch = (LWCHAR) (unsigned char) *p;
			} else
			{
				ch = get_wchar(p);
			}
		}
	}
	*pp = p;
	return ch;
}

public LWCHAR step_char(char **pp, signed int dir, constant char *limit)
{
	constant char *p = (constant char *) *pp;
	LWCHAR ch = step_charc(&p, dir, limit);
	*pp = (char *) p;
	return ch;
}

/*
 * Unicode characters data
 * Actual data is in the generated *.uni files.
 */

#define DECLARE_RANGE_TABLE_START(name) \
	static struct wchar_range name##_array[] = {
#define DECLARE_RANGE_TABLE_END(name) \
	}; struct wchar_range_table name##_table = { name##_array, countof(name##_array) };

DECLARE_RANGE_TABLE_START(compose)
#include "compose.uni"
DECLARE_RANGE_TABLE_END(compose)

DECLARE_RANGE_TABLE_START(ubin)
#include "ubin.uni"
DECLARE_RANGE_TABLE_END(ubin)

DECLARE_RANGE_TABLE_START(wide)
#include "wide.uni"
DECLARE_RANGE_TABLE_END(wide)

DECLARE_RANGE_TABLE_START(fmt)
#include "fmt.uni"
DECLARE_RANGE_TABLE_END(fmt)

DECLARE_RANGE_TABLE_START(omit)
#include "omit.uni"
DECLARE_RANGE_TABLE_END(omit)

/* comb_table is special pairs, not ranges. */
static struct wchar_range comb_table[] = {
	{0x0644,0x0622}, {0x0644,0x0623}, {0x0644,0x0625}, {0x0644,0x0627},
};


static lbool is_in_table(LWCHAR ch, struct wchar_range_table *table)
{
	unsigned int hi;
	unsigned int lo;

	/* Binary search in the table. */
	if (table->table == NULL || table->count == 0 || ch < table->table[0].first)
		return FALSE;
	lo = 0;
	hi = table->count - 1;
	while (lo <= hi)
	{
		unsigned int mid = (lo + hi) / 2;
		if (ch > table->table[mid].last)
			lo = mid + 1;
		else if (ch < table->table[mid].first)
			hi = mid - 1;
		else
			return TRUE;
	}
	return FALSE;
}

/*
 * Is a character in none of a set of specified user tables?
 */
static lbool not_user_defined(LWCHAR ch, struct wchar_range_table *tbl1, struct wchar_range_table *tbl2, struct wchar_range_table *tbl3)
{
	if (is_in_table(ch, tbl1)) return FALSE;
	if (is_in_table(ch, tbl2)) return FALSE;
	if (is_in_table(ch, tbl3)) return FALSE;
	return TRUE;
}

/*
 * Is a character a UTF-8 composing character?
 * If a composing character follows any char, the two combine into one glyph.
 */
public lbool is_composing_char(LWCHAR ch)
{
	if (is_in_table(ch, &user_prt_table)) return FALSE;
	return is_in_table(ch, &user_compose_table) ||
	       (is_in_table(ch, &compose_table) ||
	       (bs_mode != BS_CONTROL && is_in_table(ch, &fmt_table) &&
            not_user_defined(ch, &user_prt_table, &user_ubin_table, &user_omit_table)));
}

/*
 * Should this UTF-8 character be treated as binary?
 */
public lbool is_ubin_char(LWCHAR ch)
{
	if (is_in_table(ch, &user_prt_table)) return FALSE;
	if (ch > MAX_UNICODE) return TRUE;
	return is_in_table(ch, &user_ubin_table) ||
	       (is_in_table(ch, &ubin_table) ||
	       (bs_mode == BS_CONTROL && is_in_table(ch, &fmt_table) &&
            not_user_defined(ch, &user_prt_table, &user_compose_table, &user_omit_table)));
}

/*
 * Is this a double width UTF-8 character?
 */
public lbool is_wide_char(LWCHAR ch)
{
	return is_in_table(ch, &user_wide_table) ||
	       (is_in_table(ch, &wide_table) &&
            not_user_defined(ch, &user_compose_table, &user_ubin_table, &user_omit_table));
}

/*
 * Is this an omittable character?
 */
public lbool is_omit_char(LWCHAR ch)
{
	return is_in_table(ch, &user_omit_table) ||
	       (is_in_table(ch, &omit_table) &&
            not_user_defined(ch, &user_prt_table, &user_compose_table, &user_ubin_table));
}

/*
 * Is a character a UTF-8 combining character?
 * A combining char acts like an ordinary char, but if it follows
 * a specific char (not any char), the two combine into one glyph.
 */
public lbool is_combining_char(LWCHAR ch1, LWCHAR ch2)
{
	/* The table is small; use linear search. */
	int i;
	for (i = 0;  i < countof(comb_table);  i++)
	{
		if (ch1 == comb_table[i].first &&
		    ch2 == comb_table[i].last)
			return TRUE;
	}
	return FALSE;
}
