#include <stdio.h>
#include <ctype.h>
#include <string.h>

#include "b64.h"
#include "globals.h"
#include "xemoticon.h"
#include "decode.h"
#include "emottab.h"

char dozwolony(char trescw)
{
    if (!((trescw >= 'a' && trescw <= 'z') ||
	  (trescw >= 'A' && trescw <= 'Z') ||
	  (trescw >= '0' && trescw <= '9') ||
	  (trescw == '(') ||
	  (trescw == '=') ||
	  (trescw == ')') ||
	  (trescw == '!') ||
	  (trescw == '@') ||
	  (trescw == '#') ||
	  (trescw == '%') ||
	  (trescw == '*') ||
	  (trescw == ',') ||
	  (trescw == '.') ||
	  (trescw == ' ') ||
	  (trescw == '-') || (trescw == ';') || (trescw == 39) || (trescw == '~') || (trescw == ':') || (trescw == '"') || (trescw == '?') || (trescw == '/')
	))			//dozwolone: "!@#%(),.;'~:"?/"
	trescw = '_';
    return trescw;
}

char warthex(char *i)
{
    int valu = 0;
    if (i[1] >= '0' && i[1] <= '9')
	valu += (i[1] - 48) * 16;
    else
	valu += (toupper(i[1]) - 55) * 16;
    if (i[2] >= '0' && i[2] <= '9')
	valu += (i[2] - 48);
    else
	valu += (toupper(i[2]) - 55);
    return (char) valu;
}

/*
dekodowanie wap->ekg
*/

int decode(char *liniap, int max, int nopl)
{
    int k = 0, i;
    char linia[1000], liniaw[1001];
    if (strlen(liniap) > 1000)
	return 0;
    for (i = 0; i < strlen(liniap); i++) {
	if (liniap[i] == '+')
	    linia[k++] = ' ';
	else {
	    if (liniap[i] != '%') {
		linia[k++] = liniap[i];
	    }
	    else {
		char oc = warthex(liniap + i);
		if (oc != 0)
		    linia[k++] = oc;
		i += 2;
	    }
	}
    }
    linia[k] = 0;
    snprintf(liniaw, sizeof(liniaw), "%.1000s", linia);

/*
dekodowanie polskich znakow wap->ekg (utf-8 -> iso-8859-2)
zgodnie z tabela z nokii 7650 (tnx sybir)
¡ =  %01%04 ± =  %01%05 Æ =  %01%06 æ =  %01%07 Ê =  %01%18 ê =  %01%19
£ =  %01%41 ³ =  %01%42 Ñ =  %01%43 ñ =  %01%44 Ó =  %D3    ó =  %F3
¦ =  %01%5A ¶ =  %01%5B ¯ =  %01%7B ¿ =  %01%7C ¬ =  %01%79 ¼ =  %01%7A
*/

/*
tabelka dziwnych kodow (nokie w plusie, a mo¿e i inne) (tnx Michal)
(tak koduje jabber, wiêc te kody nie s± a¿ tak dziwne, ponoæ to te¿
jest utf-8, ciekawe tylko które utf-8 jest ok, tamto wy¿ej jest chocia¿
zgodne ze stron± ogonkow± ;)
0xC484 - ¡ 0xC485 - ± 0xC486 - Æ 0xC487 - æ 0xC498 - Ê 0xC499 - ê 0xC581 - £
0xC582 - ³ 0xC583 - Ñ 0xC584 - ñ 0xC393 - Ó 0xC3B3 - ó 0xC59A - ¦ 0xC59B - ¶
0xC5BB - ¯ 0xC5BC - ¿ 0xC5B9 - ¬ 0xC5BA - ¼
*/

    k = 0;
    for (i = 0; i < strlen(liniaw); i++) {

// <> do ekg leci:
	if (liniaw[i] == (char) 0x3c)
	    linia[k++] = '<';
	else if (liniaw[i] == (char) 0x3e)
	    linia[k++] = '>';
	else
// normalne o i O
	if (liniaw[i] == (char) 0xd3)
	    linia[k++] = (nopl) ? 'O' : 'Ó';
	else if (liniaw[i] == (char) 0xf3)
	    linia[k++] = (nopl) ? 'o' : 'ó';
	else
// dziwna tabelka
	if (liniaw[i] == (char) 0xc3) {
	    if (i < (strlen(liniaw) - 1))
		i++;
	    if (liniaw[i] == (char) 0x93)
		linia[k++] = (nopl) ? 'O' : 'Ó';
	    else if (liniaw[i] == (char) 0xB3)
		linia[k++] = (nopl) ? 'o' : 'ó';
	    else
		linia[k++] = '_';
	}
	else if (liniaw[i] == (char) 0xc4) {
	    if (i < (strlen(liniaw) - 1))
		i++;
	    if (liniaw[i] == (char) 0x84)
		linia[k++] = (nopl) ? 'A' : '¡';
	    else if (liniaw[i] == (char) 0x85)
		linia[k++] = (nopl) ? 'a' : '±';
	    else if (liniaw[i] == (char) 0x86)
		linia[k++] = (nopl) ? 'C' : 'Æ';
	    else if (liniaw[i] == (char) 0x87)
		linia[k++] = (nopl) ? 'c' : 'æ';
	    else if (liniaw[i] == (char) 0x98)
		linia[k++] = (nopl) ? 'E' : 'Ê';
	    else if (liniaw[i] == (char) 0x99)
		linia[k++] = (nopl) ? 'e' : 'ê';
	    else
		linia[k++] = '_';
	}
	else if (liniaw[i] == (char) 0xc5) {
	    if (i < (strlen(liniaw) - 1))
		i++;
	    if (liniaw[i] == (char) 0x81)
		linia[k++] = (nopl) ? 'L' : '£';
	    else if (liniaw[i] == (char) 0x82)
		linia[k++] = (nopl) ? 'l' : '³';
	    else if (liniaw[i] == (char) 0x83)
		linia[k++] = (nopl) ? 'N' : 'Ñ';
	    else if (liniaw[i] == (char) 0x84)
		linia[k++] = (nopl) ? 'n' : 'ñ';
	    else if (liniaw[i] == (char) 0x9A)
		linia[k++] = (nopl) ? 'S' : '¦';
	    else if (liniaw[i] == (char) 0x9B)
		linia[k++] = (nopl) ? 's' : '¶';
	    else if (liniaw[i] == (char) 0xBB)
		linia[k++] = (nopl) ? 'Z' : '¯';
	    else if (liniaw[i] == (char) 0xBC)
		linia[k++] = (nopl) ? 'z' : '¿';
	    else if (liniaw[i] == (char) 0xB9)
		linia[k++] = (nopl) ? 'Z' : '¬';
	    else if (liniaw[i] == (char) 0xBA)
		linia[k++] = (nopl) ? 'z' : '¼';
	    else
		linia[k++] = '_';
	}
	else
// unicode-utf-8
	if (liniaw[i] == (char) 1) {
	    if (i < (strlen(liniaw) - 1))
		i++;
	    if (liniaw[i] == (char) 0x04)
		linia[k++] = (nopl) ? 'A' : '¡';
	    else if (liniaw[i] == (char) 0x05)
		linia[k++] = (nopl) ? 'a' : '±';
	    else if (liniaw[i] == (char) 0x06)
		linia[k++] = (nopl) ? 'C' : 'Æ';
	    else if (liniaw[i] == (char) 0x07)
		linia[k++] = (nopl) ? 'c' : 'æ';
	    else if (liniaw[i] == (char) 0x18)
		linia[k++] = (nopl) ? 'E' : 'Ê';
	    else if (liniaw[i] == (char) 0x19)
		linia[k++] = (nopl) ? 'E' : 'ê';
	    else if (liniaw[i] == (char) 0x41)
		linia[k++] = (nopl) ? 'L' : '£';
	    else if (liniaw[i] == (char) 0x42)
		linia[k++] = (nopl) ? 'l' : '³';
	    else if (liniaw[i] == (char) 0x43)
		linia[k++] = (nopl) ? 'N' : 'Ñ';
	    else if (liniaw[i] == (char) 0x44)
		linia[k++] = (nopl) ? 'n' : 'ñ';
	    else if (liniaw[i] == (char) 0x5a)
		linia[k++] = (nopl) ? 'S' : '¦';
	    else if (liniaw[i] == (char) 0x5b)
		linia[k++] = (nopl) ? 's' : '¶';
	    else if (liniaw[i] == (char) 0x7b)
		linia[k++] = (nopl) ? 'Z' : '¯';
	    else if (liniaw[i] == (char) 0x7c)
		linia[k++] = (nopl) ? 'z' : '¿';
	    else if (liniaw[i] == (char) 0x79)
		linia[k++] = (nopl) ? 'Z' : '¬';
	    else if (liniaw[i] == (char) 0x7a)
		linia[k++] = (nopl) ? 'z' : '¼';
	    else
		linia[k++] = '_';
	}
	else
// przepu¶æ tez iso
	if (liniaw[i] == (char) 0xa1)
	    linia[k++] = (nopl) ? 'A' : '¡';
	else if (liniaw[i] == (char) 0xb1)
	    linia[k++] = (nopl) ? 'a' : '±';
	else if (liniaw[i] == (char) 0xc6)
	    linia[k++] = (nopl) ? 'C' : 'Æ';
	else if (liniaw[i] == (char) 0xe6)
	    linia[k++] = (nopl) ? 'c' : 'æ';
	else if (liniaw[i] == (char) 0xca)
	    linia[k++] = (nopl) ? 'E' : 'Ê';
	else if (liniaw[i] == (char) 0xea)
	    linia[k++] = (nopl) ? 'E' : 'ê';
	else if (liniaw[i] == (char) 0xa3)
	    linia[k++] = (nopl) ? 'L' : '£';
	else if (liniaw[i] == (char) 0xb3)
	    linia[k++] = (nopl) ? 'l' : '³';
	else if (liniaw[i] == (char) 0xd1)
	    linia[k++] = (nopl) ? 'N' : 'Ñ';
	else if (liniaw[i] == (char) 0xf1)
	    linia[k++] = (nopl) ? 'n' : 'ñ';
	else if (liniaw[i] == (char) 0xa6)
	    linia[k++] = (nopl) ? 'S' : '¦';
	else if (liniaw[i] == (char) 0xb6)
	    linia[k++] = (nopl) ? 's' : '¶';
	else if (liniaw[i] == (char) 0xaf)
	    linia[k++] = (nopl) ? 'Z' : '¯';
	else if (liniaw[i] == (char) 0xbf)
	    linia[k++] = (nopl) ? 'z' : '¿';
	else if (liniaw[i] == (char) 0xac)
	    linia[k++] = (nopl) ? 'Z' : '¬';
	else if (liniaw[i] == (char) 0xbc)
	    linia[k++] = (nopl) ? 'z' : '¼';
	else
	    linia[k++] = dozwolony(liniaw[i]);
    }
    linia[k] = 0;

    snprintf(liniap, max - 1, "%s", linia);
    return 0;
}

/*
0xC484 - ¡ 0xC485 - ± 0xC486 - Æ 0xC487 - æ 0xC498 - Ê 0xC499 - ê 0xC581 - £
0xC582 - ³ 0xC583 - Ñ 0xC584 - ñ 0xC393 - Ó 0xC3B3 - ó 0xC59A - ¦ 0xC59B - ¶
0xC5BB - ¯ 0xC5BC - ¿ 0xC5B9 - ¬ 0xC5BA - ¼
*/

int iso2jabber(char *liniaw, int max)
{
    int i;
    char linia[2048];

    memset(&linia, 0, sizeof(linia));

    for (i = 0; i < strlen(liniaw); i++) {
	if (strlen(linia) < (max - 7)) {
	    if (liniaw[i] == (char) 0x26)
		snprintf(linia + strlen(linia), max - strlen(linia), "&#x26;");
	    else if (liniaw[i] == (char) 0x3c)
		snprintf(linia + strlen(linia), max - strlen(linia), "&#x3C;");
	    else if (liniaw[i] == (char) 0x3e)
		snprintf(linia + strlen(linia), max - strlen(linia), "&#x3E;");
	    else if (liniaw[i] == (char) 0x22)
		snprintf(linia + strlen(linia), max - strlen(linia), "&#x22;");
	    else if (liniaw[i] == (char) 0xd3)
		snprintf(linia + strlen(linia), max - strlen(linia), "%c%c", 0xC3, 0x93);
	    else if (liniaw[i] == (char) 0xf3)
		snprintf(linia + strlen(linia), max - strlen(linia), "%c%c", 0xC3, 0xB3);
	    else if (liniaw[i] == (char) 0xa1)
		snprintf(linia + strlen(linia), max - strlen(linia), "%c%c", 0xC4, 0x84);
	    else if (liniaw[i] == (char) 0xb1)
		snprintf(linia + strlen(linia), max - strlen(linia), "%c%c", 0xC4, 0x85);
	    else if (liniaw[i] == (char) 0xc6)
		snprintf(linia + strlen(linia), max - strlen(linia), "%c%c", 0xC4, 0x86);
	    else if (liniaw[i] == (char) 0xe6)
		snprintf(linia + strlen(linia), max - strlen(linia), "%c%c", 0xC4, 0x87);
	    else if (liniaw[i] == (char) 0xca)
		snprintf(linia + strlen(linia), max - strlen(linia), "%c%c", 0xC4, 0x98);
	    else if (liniaw[i] == (char) 0xea)
		snprintf(linia + strlen(linia), max - strlen(linia), "%c%c", 0xC4, 0x99);
	    else if (liniaw[i] == (char) 0xa3)
		snprintf(linia + strlen(linia), max - strlen(linia), "%c%c", 0xC5, 0x81);
	    else if (liniaw[i] == (char) 0xb3)
		snprintf(linia + strlen(linia), max - strlen(linia), "%c%c", 0xC5, 0x82);
	    else if (liniaw[i] == (char) 0xd1)
		snprintf(linia + strlen(linia), max - strlen(linia), "%c%c", 0xC5, 0x83);
	    else if (liniaw[i] == (char) 0xf1)
		snprintf(linia + strlen(linia), max - strlen(linia), "%c%c", 0xC5, 0x84);
	    else if (liniaw[i] == (char) 0xa6)
		snprintf(linia + strlen(linia), max - strlen(linia), "%c%c", 0xC5, 0x9A);
	    else if (liniaw[i] == (char) 0xb6)
		snprintf(linia + strlen(linia), max - strlen(linia), "%c%c", 0xC5, 0x9B);
	    else if (liniaw[i] == (char) 0xaf)
		snprintf(linia + strlen(linia), max - strlen(linia), "%c%c", 0xC5, 0xBB);
	    else if (liniaw[i] == (char) 0xbf)
		snprintf(linia + strlen(linia), max - strlen(linia), "%c%c", 0xC5, 0xBC);
	    else if (liniaw[i] == (char) 0xac)
		snprintf(linia + strlen(linia), max - strlen(linia), "%c%c", 0xC5, 0xB9);
	    else if (liniaw[i] == (char) 0xbc)
		snprintf(linia + strlen(linia), max - strlen(linia), "%c%c", 0xC5, 0xBA);
	    else
		snprintf(linia + strlen(linia), max - strlen(linia), "%c", dozwolony(liniaw[i]));
	}
    }

    snprintf(liniaw, max - 1, "%s", linia);
    return 0;
}

int decodeqp(char *liniap, int max)
{
    int k = 0, i;
    char linia[5000];
    if (strlen(liniap) > 5000)
	return 0;
    for (i = 0; i < strlen(liniap); i++) {
	if (liniap[i] != '=') {
	    linia[k++] = liniap[i];
	}
	else {
	    if (i != strlen(liniap) - 1) {
		linia[k++] = warthex(liniap + i);
		i += 2;
	    }
	}
    }
    linia[k] = 0;
    snprintf(liniap, max - 1, "%.*s", max - 1, linia);
    return 0;
}

int decodesubiect(char *liniap, int max)
{
    int k = 0, i, qp = 0;
    char linia[5000];
    if (strlen(liniap) > 5000)
	return 0;
    for (i = 0; i < strlen(liniap); i++) {
	if (liniap[i] == '?' && liniap[i + 1] == '=' && qp == 1) {
	    qp = 0;
	    i += 1;
	}
	else if (liniap[i] != '=') {
	    if (liniap[i] == '_' && qp == 1)
		linia[k++] = ' ';
	    else
		linia[k++] = liniap[i];
	}
	else if (strncasecmp(liniap + i, "=?ISO-8859-2?Q?", 15) == 0 || strncasecmp(liniap + i, "=?ISO-8859-1?Q?", 15) == 0) {
	    qp = 1;
	    i += 14;
	}
	else if (strncasecmp(liniap + i, "=?ISO-8859-2?B?", 15) == 0 ||
		 strncasecmp(liniap + i, "=?ISO-8859-1?B?", 15) == 0 || strncasecmp(liniap + i, "=?windows-1250?B?", 16) == 0) {
	    char *s, *r;
	    char pomb[1000], out[1000];
	    int w, j, len;

	    len = 15;
	    if (strncasecmp(liniap + i, "=?windows-1250?B?", 16) == 0)
		len = 16;

	    s = liniap + i + len;
	    r = (char *) strstr(s, "?=");

	    if (r) {
		snprintf(pomb, sizeof(pomb), "%.*s", r - s, s);
		w = b64_decode(pomb, out, sizeof(out));
		if (w > 0) {
		    for (j = 0; j < w; j++)
			linia[k++] = out[j];
		}
		i += (r - s) + len + 1;
	    }
	    else {
		linia[k++] = '=';
	    }
	}
	else if (qp != 1) {
	    linia[k++] = '=';
	}
	else {
	    linia[k++] = warthex(liniap + i);
	    i += 2;
	}
    }
    linia[k] = 0;
    snprintf(liniap, max - 1, "%.*s", max - 1, linia);
    return 0;
}

// ekg ->wap
// dla poczty i dla msg
// przetwarza string liniap koduj±c lub usuwaj±c znaczki
// jak=1 jak=0 - normalny tekst
// jak=2 - kasuje rowniez zawartosc w tag-ah dla content: text/html, zaczyna od wyswietlania
// jak=3 - kasuje html, zaczyna od niewyswietlania
// jak=4 - dekoduje emoticony
// jak=5 - dekoduje emoticony i pozostawia widoczny tekst
// zwraca wskaznik kontynuacji html 1-wyswetlam 0-nie wyswietlam
// xemots=1 - emoticony rowniez zewnetrzne (gify)
// icgi=1 - emotikony maja inny link dla cgi
// nopl=1 - polskie na polskawe, 0 - polskie w utf-8, 2 - polskie w iso
// xe_count - ile emoticon do parsowania

int dozwolonys(char *liniap, int sizeofliniap, int jak, int xemots, int icgi, int nopl, int xe_count)
{
    int i, emot;
    int jx = 1;
    int iso = 0;
    char linia[4000];

    if (nopl == 2) {
	nopl = 0;
	iso = 1;
    }

    if (strlen(liniap) > 4000)
	return 0;
    linia[0] = 0;

    if (jak == 3) {
	jx = 0;
	jak = 2;
    };

// printf("aaaa: %s\n",liniap);

    for (i = 0; i < strlen(liniap); i++) {
	if (strlen(linia) < (sizeofliniap - 50)) {	//if(strlen(linia)<(sizeof(linia)-50))

	    emot = 0;

	    if ((jak == 4 || jak == 5) && xemots == 1 && xe_count > 0) {
		if (((liniap[i - 1] == ' ' || liniap[i - 1] == '>' || liniap[i] == '<' || liniap[i] == ':' || liniap[i] == ';') && i > 0) || i == 0) {
		    int ii;

		    for (ii = xe_count - 1; ii >= 0; ii--) {
			if (strncmp(xe_pars[ii], liniap + i, strlen(xe_pars[ii])) == 0 && !emot) {
			    snprintf(linia + strlen(linia),
				     sizeof(linia) - strlen(linia),
				     "<img src=\"%s%s\" alt=\"%s\"/>%s%s%s",
				     (icgi == 1) ? "/rtg/" : "?",
				     xe_gifs[ii], xe_gifs[ii], (jak == 5) ? "&#x3C;" : "", (jak == 5) ? xe_gifs[ii] : "", (jak == 5) ? "&#x3E;" : "");
			    i += strlen(xe_pars[ii]) - 1;
			    emot = 1;
			    ii = (-1);
			}
		    }
		}
	    }

	    if ((liniap[i] == '<' || liniap[i] == ';' || liniap[i] == ':')
		&& !emot) {
		if (jak == 4 || jak == 5) {
		    int ii;

		    for (ii = 0; ii < (EMOTIKON * 3); ii += 3) {
			if (strncmp(liniap + i, emoticons_tab[ii], strlen(emoticons_tab[ii])) == 0 && !emot && strlen(emoticons_tab[ii]) > 1) {
			    snprintf(linia + strlen(linia),
				     sizeof(linia) - strlen(linia),
				     "<img src=\"%s%s\" alt=\"%s\"/>%s%s%s",
				     (icgi == 1) ? "/rtg/" : "?",
				     emoticons_tab[ii + 1],
				     emoticons_tab[ii + 2], (jak == 5) ? "&#x3C;" : "", (jak == 5) ? emoticons_tab[ii + 2] : "", (jak == 5) ? "&#x3E;" : "");
			    i += strlen(emoticons_tab[ii]) - 1;
			    emot = 1;
			}
		    }
		}
	    }

	    if (!emot) {
		if (liniap[i] == '<') {
		    if (jak == 2)
			jx = 0;
		    else
			snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "&#x3C;");
		}
		else if (liniap[i] == '>') {
		    if (jak == 2)
			jx = 1;
		    else
			snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "&#x3E;");
		}
		else if (jx == 1) {
		    if (strncmp(liniap + i, "&nbsp;", 6) == 0) {
			snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), " ");
			i += 5;
		    }
		    else if (strncmp(liniap + i, "http://", 7) == 0) {
			snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "http://");
			i += 6;
		    }
		    else
// pliterki z jabbera
		    if (liniap[i] == (char) 0xc3) {
			if (i < (strlen(liniap) - 1))
			    i++;
			if (liniap[i] == (char) 0x93)
			    snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%s", (nopl) ? "O" : iso ? "Ó" : "&#211;");
			else if (liniap[i] == (char) 0xB3)
			    snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%s", (nopl) ? "o" : iso ? "ó" : "&#243;");
			else
			    snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "_");
		    }
		    else if (liniap[i] == (char) 0xc4) {
			if (i < (strlen(liniap) - 1))
			    i++;
			if (liniap[i] == (char) 0x84)
			    snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%s", (nopl) ? "A" : iso ? "¡" : "&#260;");
			else if (liniap[i] == (char) 0x85)
			    snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%s", (nopl) ? "a" : iso ? "±" : "&#261;");
			else if (liniap[i] == (char) 0x86)
			    snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%s", (nopl) ? "C" : iso ? "Æ" : "&#262;");
			else if (liniap[i] == (char) 0x87)
			    snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%s", (nopl) ? "c" : iso ? "æ" : "&#263;");
			else if (liniap[i] == (char) 0x98)
			    snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%s", (nopl) ? "E" : iso ? "Ê" : "&#280;");
			else if (liniap[i] == (char) 0x99)
			    snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%s", (nopl) ? "e" : iso ? "ê" : "&#281;");
			else
			    snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "_");
		    }
		    else if (liniap[i] == (char) 0xc5) {
			if (i < (strlen(liniap) - 1))
			    i++;
			if (liniap[i] == (char) 0x81)
			    snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%s", (nopl) ? "L" : iso ? "£" : "&#321;");
			else if (liniap[i] == (char) 0x82)
			    snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%s", (nopl) ? "l" : iso ? "³" : "&#322;");
			else if (liniap[i] == (char) 0x83)
			    snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%s", (nopl) ? "N" : iso ? "Ñ" : "&#323;");
			else if (liniap[i] == (char) 0x84)
			    snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%s", (nopl) ? "n" : iso ? "ñ" : "&#324;");
			else if (liniap[i] == (char) 0x9A)
			    snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%s", (nopl) ? "S" : iso ? "¦" : "&#346;");
			else if (liniap[i] == (char) 0x9B)
			    snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%s", (nopl) ? "s" : iso ? "¶" : "&#347;");
			else if (liniap[i] == (char) 0xBB)
			    snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%s", (nopl) ? "Z" : iso ? "¯" : "&#379;");
			else if (liniap[i] == (char) 0xBC)
			    snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%s", (nopl) ? "z" : iso ? "¿" : "&#380;");
			else if (liniap[i] == (char) 0xB9)
			    snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%s", (nopl) ? "Z" : iso ? "¬" : "&#377;");
			else if (liniap[i] == (char) 0xBA)
			    snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%s", (nopl) ? "z" : iso ? "¼" : "&#378;");
			else
			    snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "_");
		    }
		    else
// iso -> utf-8 polskawe lub bez zmian
		    if (liniap[i] == '[')
			snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "&#x5B;");
		    else if (liniap[i] == ']')
			snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "&#x5D;");
		    else if (liniap[i] == '&')
			snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "&#x26;");
		    else if (liniap[i] == '=')
			snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "&#x3D;");
		    else if (liniap[i] == '*')
			snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "&#x2A;");
		    else if (liniap[i] == '±')
			snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%s", (nopl) ? "a" : iso ? "±" : "&#261;");
		    else if (liniap[i] == 'æ')
			snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%s", (nopl) ? "c" : iso ? "æ" : "&#263;");
		    else if (liniap[i] == 'ê')
			snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%s", (nopl) ? "e" : iso ? "ê" : "&#281;");
		    else if (liniap[i] == '³')
			snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%s", (nopl) ? "l" : iso ? "³" : "&#322;");
		    else if (liniap[i] == 'ñ')
			snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%s", (nopl) ? "n" : iso ? "ñ" : "&#324;");
		    else if (liniap[i] == 'ó')
			snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%s", (nopl) ? "o" : iso ? "ó" : "&#243;");
		    else if (liniap[i] == '¶')
			snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%s", (nopl) ? "s" : iso ? "¶" : "&#347;");
		    else if (liniap[i] == '¼')
			snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%s", (nopl) ? "z" : iso ? "¼" : "&#378;");
		    else if (liniap[i] == '¿')
			snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%s", (nopl) ? "z" : iso ? "¿" : "&#380;");
		    else if (liniap[i] == '¡')
			snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%s", (nopl) ? "A" : iso ? "¡" : "&#260;");
		    else if (liniap[i] == 'Æ')
			snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%s", (nopl) ? "C" : iso ? "Æ" : "&#262;");
		    else if (liniap[i] == 'Ê')
			snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%s", (nopl) ? "E" : iso ? "Ê" : "&#280;");
		    else if (liniap[i] == '£')
			snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%s", (nopl) ? "L" : iso ? "£" : "&#321;");
		    else if (liniap[i] == 'Ñ')
			snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%s", (nopl) ? "N" : iso ? "Ñ" : "&#323;");
		    else if (liniap[i] == 'Ó')
			snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%s", (nopl) ? "O" : iso ? "Ó" : "&#211;");
		    else if (liniap[i] == '¦')
			snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%s", (nopl) ? "S" : iso ? "¦" : "&#346;");
		    else if (liniap[i] == '¬')
			snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%s", (nopl) ? "Z" : iso ? "¬" : "&#377;");
		    else if (liniap[i] == '¯')
			snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%s", (nopl) ? "Z" : iso ? "¯" : "&#379;");
		    else
			snprintf(linia + strlen(linia), sizeof(linia) - strlen(linia), "%c", dozwolony(liniap[i]));
		}
	    }
	}
    }
// printf("bbb: %s\n",linia);
    snprintf(liniap, sizeofliniap, "%s", linia);
    return jx;
}
