#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 == 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;
  sprintf (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;

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

  sprintf (liniaw, "%.*s", max - 1, 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;
  sprintf (liniap, "%.*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;
  sprintf (liniap, "%.*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
// 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;
  char linia[4000];

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