//#include "Unix.h"
//#include <time.h>
#include "stdafx.h"   //MS Windows
#include "Strawberry.h"

#include "MainFrm.h"
#include "StrawberryDoc.h"

// Rado

unsigned char enc_map[] = {
/*   0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F    */
    'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
    'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
    'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
    'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
};

int dec_map[] = {
    -1, -1, -1, -1, -1, -1, -1, -1,  /* 0x00 - 0x07 */
    -1, -1, -1, -1, -1, -1, -1, -1,  /* 0x08 - 0x0F */
    -1, -1, -1, -1, -1, -1, -1, -1,  /* 0x10 - 0x17 */
    -1, -1, -1, -1, -1, -1, -1, -1,  /* 0x18 - 0x1F */
    -1, -1, -1, -1, -1, -1, -1, -1,  /* 0x20 - 0x27 */
    -1, -1, -1, 62, -1, -1, -1, 63,  /* 0x28 - 0x2F */
    52, 53, 54, 55, 56, 57, 58, 59,  /* 0x30 - 0x37 */
    60, 61, -1, -1, -1,  0, -1, -1,  /* 0x38 - 0x3F */
    -1,  0,  1,  2,  3,  4,  5,  6,  /* 0x40 - 0x47 */
     7,  8,  9, 10, 11, 12, 13, 14,  /* 0x48 - 0x4F */
    15, 16, 17, 18, 19, 20, 21, 22,  /* 0x50 - 0x57 */
    23, 24, 25, -1, -1, -1, -1, -1,  /* 0x58 - 0x5F */
    -1, 26, 27, 28, 29, 30, 31, 32,  /* 0x60 - 0x67 */
    33, 34, 35, 36, 37, 38, 39, 40,  /* 0x68 - 0x6F */
    41, 42, 43, 44, 45, 46, 47, 48,  /* 0x70 - 0x77 */
    49, 50, 51, -1, -1, -1, -1, -1   /* 0x78 - 0x7F */
};

inline DWORD make_call(DWORD Arguments, DWORD Function, DWORD Event, DWORD Arg1Tp, DWORD Arg1, DWORD Arg2Tp, DWORD Arg2, DWORD Arg3Tp, DWORD Arg3)
{
  if (Trace==2)
    return(0);
  status_fl&=M1^4;
  a1=buda_br;
  Buda1[buda_br++]=Function;  // ms_function
  Buda1[buda_br++]=Definition[Event];
  if (Arguments)
  {
    Buda1[buda_br]=Arg1Tp;
    Buda2[buda_br++]=Arg1;
    if (Arguments>1)
    {
      Buda1[buda_br]=Arg2Tp;
      Buda2[buda_br++]=Arg2;
      if (Arguments>2)
      {
        Buda1[buda_br]=Arg3Tp;
        Buda2[buda_br++]=Arg3;
      }
    }
  }
  int i=stack_br;
  Stack[stack_br]|=HALF;
  Ekler[++ekler_br]=4*Arguments+4;
  Stack[stack_br+3]=ekler_br;
  my_gl2=Arguments+1;
  DWORD j=call(2);
  if (j>1)
  {
    Wait_d=0;
    pMainFrame->PostMessage(WM_USER+1, j, 0); // OnEnd
  }
  else if (j==1)
  {
    for (DWORD m=i+3; m<stack_br; Stack[m+=4]|=HALF); // Cut
    call(1);
  }
  Stack[i]&=HALFM;
  status_fl|=4;
  if (Trace==1)
    MitiRedraw();
  return j;
}
unsigned char *sp_base64_enc(unsigned char *input, int ilen)
{
//    int ilen = strlen(input);
    int olen = ((ilen/3) + 1) * 4 + 1;
    unsigned char *result = (unsigned char *)malloc(olen);
    int i = 0, o = 0;
    int daipak = 1;

    if(result==NULL) {
      return NULL;
    }

    while(daipak) {
      unsigned char igroup[3];
      int n;

      igroup[0] = igroup[1] = igroup[2] = 0;
      for(n=0;n<3;n++) {
          if(i+n >= ilen) {
              daipak = 0;
              break;
          }
          igroup[n] = (unsigned char)input[i+n];
      }

      if(n>0) {
          result[o]   = enc_map[igroup[0]>>2];
          result[o+1] = enc_map[((igroup[0]&3)<<4)|(igroup[1]>>4)];
          result[o+2] = enc_map[((igroup[1]&0xF)<<2)|(igroup[2]>>6)];
          result[o+3] = enc_map[igroup[2]&0x3F];

          if(n<3) {
              result[o+3] = '=';
              if(n<2) {
                  result[o+2] = '=';
              }
          }

          o += 4;
          i += 3;
      }
    }

    result[o] = '\0';
    return result;
}

unsigned char *base64_dec(unsigned char *input, int ilen)
{
//    int ilen = strlen(input);
    int olen = (ilen/4)*3 + 1;
    unsigned char *result;
    int i = 0, o = 0;

    if(ilen % 4) {
        //fprintf(stderr,"base64_dec: Input incomplete (len = %d)\n", ilen);
        return NULL;
    }

    result = (unsigned char *)malloc(olen);
    if(result == NULL) {
        return NULL;
    }

    for(i = 0; i < ilen; i += 4) {
        int n;
        unsigned char a[4], b[4];

        for(n = 0; n < 4; n++) {
            int c = input[i+n];
            if((c >= 0x80) || ((c < 0x80) && dec_map[c] == -1)) {
                //fprintf(stderr, "Illegal character '%c' in input.\n", c);
		            free(result);
                return NULL;
            }
            a[n] = (unsigned char)c;
            b[n] = (unsigned char)dec_map[c];
        }

        result[o]   = (b[0] << 2) | (b[1] >> 4);
        result[o+1] = (b[1] << 4) | (b[2] >> 2);
        result[o+2] = (b[2] << 6) | (b[3]);

        o += ((a[2]=='=') ? 1 : (a[3]=='=') ? 2 : 3);
    }

    result[o] = '\0';
    return result;
}

int sp_fgets(char *dest, int len, FILE *in) {
    char c;
    int read = 0;

    while(1) {
      c = fgetc(in);
      if (feof(in))
          break;
      if (read >= len-1 || c == '\n')
          break;

      dest[read++] = c;
    }

    if (c == '\n' && read < len-1)
      dest[read++] = '\n';

    dest[read] = '\0';

    return read;
}

int sp_fgets_fixlen(char *dest, int len, FILE *in) {
    char c;
    int read = 0;

    while(len > 0) {
      c = fgetc(in);
      if (feof(in))
          break;

      dest[read++] = c;

      if (read >= len)
          break;
    }

    dest[read] = '\0';

    return read;
}

void sp_fprintf(FILE *out, char *src, int count) {
    for(int i = 0; i < count; i++) {
      fprintf(out, "%c", src[i]);
    }
}
// end Rado

WORD MAP_CP[CHAR_SETS][128] = {
// CP1251 Cyrillic (Windows)
{0x0402, 0x0403, 0x201A, 0x0453, 0x201E, 0x2026, 0x2020, 0x2021,
 0x20AC, 0x2030, 0x0409, 0x2039, 0x040A, 0x040C, 0x040B, 0x040F,
 0x0452, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
      0, 0x2122, 0x0459, 0x203A, 0x045A, 0x045C, 0x045B, 0x045F,
 0x00A0, 0x040E, 0x045E, 0x0408, 0x00A4, 0x0490, 0x00A6, 0x00A7,
 0x0401, 0x00A9, 0x0404, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x0407,
 0x00B0, 0x00B1, 0x0406, 0x0456, 0x0491, 0x00B5, 0x00B6, 0x00B7,
 0x0451, 0x2116, 0x0454, 0x00BB, 0x0458, 0x0405, 0x0455, 0x0457,
 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417,
 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F,
 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427,
 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F,
 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437,
 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F,
 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447,
 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F},

// CP1252 Western European (Windows)
{0x20AC,      0, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,
 0x02C6, 0x2030, 0x0160, 0x2039, 0x0152,      0, 0x017D,      0,
      0, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
 0x02DC, 0x2122, 0x0161, 0x203A, 0x0153,      0, 0x017E, 0x0178,
 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7,
 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,
 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7,
 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF,
 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x00C7,
 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF,
 0x00D0, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x00D7,
 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x00DD, 0x00DE, 0x00DF,
 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7,
 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,
 0x00F0, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7,
 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x00FD, 0x00FE, 0x00FF},

//CP1253 Greek (Windows)
{0x20AC,      0, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,
      0, 0x2030,      0, 0x2039,      0,      0,      0,      0,
      0, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
      0, 0x2122,      0, 0x203A,      0,      0,      0,      0,
 0x00A0, 0x0385, 0x0386, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7,
 0x00A8, 0x00A9,      0, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x2015,
 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x0384, 0x00B5, 0x00B6, 0x00B7,
 0x0388, 0x0389, 0x038A, 0x00BB, 0x038C, 0x00BD, 0x038E, 0x038F,
 0x0390, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397,
 0x0398, 0x0399, 0x039A, 0x039B, 0x039C, 0x039D, 0x039E, 0x039F,
 0x03A0, 0x03A1,      0, 0x03A3, 0x03A4, 0x03A5, 0x03A6, 0x03A7,
 0x03A8, 0x03A9, 0x03AA, 0x03AB, 0x03AC, 0x03AD, 0x03AE, 0x03AF,
 0x03B0, 0x03B1, 0x03B2, 0x03B3, 0x03B4, 0x03B5, 0x03B6, 0x03B7,
 0x03B8, 0x03B9, 0x03BA, 0x03BB, 0x03BC, 0x03BD, 0x03BE, 0x03BF,
 0x03C0, 0x03C1, 0x03C2, 0x03C3, 0x03C4, 0x03C5, 0x03C6, 0x03C7,
 0x03C8, 0x03C9, 0x03CA, 0x03CB, 0x03CC, 0x03CD, 0x03CE,      0},

//CP1254 Turkish (Windows)
{0x20AC,      0, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,
 0x02C6, 0x2030, 0x0160, 0x2039, 0x0152,      0,      0,      0,
      0, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
 0x02DC, 0x2122, 0x0161, 0x203A, 0x0153,      0,      0, 0x0178,
 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7,
 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,
 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7,
 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF,
 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x00C7,
 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF,
 0x011E, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x00D7,
 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x0130, 0x015E, 0x00DF,
 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7,
 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,
 0x011F, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7,
 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x0131, 0x015F, 0x00FF},

//CP1255 Hebrew (Windows)
{0x20AC,      0, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,
 0x02C6, 0x2030,      0, 0x2039,      0,      0,      0,      0,
      0, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
 0x02DC, 0x2122,      0, 0x203A,      0,      0,      0,      0,
 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x20AA, 0x00A5, 0x00A6, 0x00A7,
 0x00A8, 0x00A9, 0x00D7, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,
 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7,
 0x00B8, 0x00B9, 0x00F7, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF,
 0x05B0, 0x05B1, 0x05B2, 0x05B3, 0x05B4, 0x05B5, 0x05B6, 0x05B7,
 0x05B8, 0x05B9,      0, 0x05BB, 0x05BC, 0x05BD, 0x05BE, 0x05BF,
 0x05C0, 0x05C1, 0x05C2, 0x05C3, 0x05F0, 0x05F1, 0x05F2, 0x05F3,
 0x05F4,      0,      0,      0,      0,      0,      0,      0,
 0x05D0, 0x05D1, 0x05D2, 0x05D3, 0x05D4, 0x05D5, 0x05D6, 0x05D7,
 0x05D8, 0x05D9, 0x05DA, 0x05DB, 0x05DC, 0x05DD, 0x05DE, 0x05DF,
 0x05E0, 0x05E1, 0x05E2, 0x05E3, 0x05E4, 0x05E5, 0x05E6, 0x05E7,
 0x05E8, 0x05E9, 0x05EA,      0,      0, 0x200E, 0x200F,      0},

//CP1256 Arabic (Windows)
{0x20AC, 0x067E, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,
 0x02C6, 0x2030, 0x0679, 0x2039, 0x0152, 0x0686, 0x0698, 0x0688,
 0x06AF, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
 0x06A9, 0x2122, 0x0691, 0x203A, 0x0153, 0x200C, 0x200D, 0x06BA,
 0x00A0, 0x060C, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7,
 0x00A8, 0x00A9, 0x06BE, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,
 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7,
 0x00B8, 0x00B9, 0x061B, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x061F,
 0x06C1, 0x0621, 0x0622, 0x0623, 0x0624, 0x0625, 0x0626, 0x0627,
 0x0628, 0x0629, 0x062A, 0x062B, 0x062C, 0x062D, 0x062E, 0x062F,
 0x0630, 0x0631, 0x0632, 0x0633, 0x0634, 0x0635, 0x0636, 0x00D7,
 0x0637, 0x0638, 0x0639, 0x063A, 0x0640, 0x0641, 0x0642, 0x0643,
 0x00E0, 0x0644, 0x00E2, 0x0645, 0x0646, 0x0647, 0x0648, 0x00E7,
 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x0649, 0x064A, 0x00EE, 0x00EF,
 0x064B, 0x064C, 0x064D, 0x064E, 0x00F4, 0x064F, 0x0650, 0x00F7,
 0x0651, 0x00F9, 0x0652, 0x00FB, 0x00FC, 0x200E, 0x200F, 0x06D2},

//CP1257 Baltic (Windows)
{0x20AC,      0, 0x201A,      0, 0x201E, 0x2026, 0x2020, 0x2021,
      0, 0x2030,      0, 0x2039,      0, 0x00A8, 0x02C7, 0x00B8,
      0, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
      0, 0x2122,      0, 0x203A,      0, 0x00AF, 0x02DB,      0,
 0x00A0,      0, 0x00A2, 0x00A3, 0x00A4,      0, 0x00A6, 0x00A7,
 0x00D8, 0x00A9, 0x0156, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00C6,
 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7,
 0x00F8, 0x00B9, 0x0157, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00E6,
 0x0104, 0x012E, 0x0100, 0x0106, 0x00C4, 0x00C5, 0x0118, 0x0112,
 0x010C, 0x00C9, 0x0179, 0x0116, 0x0122, 0x0136, 0x012A, 0x013B,
 0x0160, 0x0143, 0x0145, 0x00D3, 0x014C, 0x00D5, 0x00D6, 0x00D7,
 0x0172, 0x0141, 0x015A, 0x016A, 0x00DC, 0x017B, 0x017D, 0x00DF,
 0x0105, 0x012F, 0x0101, 0x0107, 0x00E4, 0x00E5, 0x0119, 0x0113,
 0x010D, 0x00E9, 0x017A, 0x0117, 0x0123, 0x0137, 0x012B, 0x013C,
 0x0161, 0x0144, 0x0146, 0x00F3, 0x014D, 0x00F5, 0x00F6, 0x00F7,
 0x0173, 0x0142, 0x015B, 0x016B, 0x00FC, 0x017C, 0x017E, 0x02D9},

//CP1258 Vietnamese (Windows)
{0x20AC,      0, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,
 0x02C6, 0x2030,      0, 0x2039, 0x0152,      0,      0,      0,
      0, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
 0x02DC, 0x2122,      0, 0x203A, 0x0153,      0,      0, 0x0178,
 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7,
 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,
 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7,
 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF,
 0x00C0, 0x00C1, 0x00C2, 0x0102, 0x00C4, 0x00C5, 0x00C6, 0x00C7,
 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x0300, 0x00CD, 0x00CE, 0x00CF,
 0x0110, 0x00D1, 0x0309, 0x00D3, 0x00D4, 0x01A0, 0x00D6, 0x00D7,
 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x01AF, 0x0303, 0x00DF,
 0x00E0, 0x00E1, 0x00E2, 0x0103, 0x00E4, 0x00E5, 0x00E6, 0x00E7,
 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x0301, 0x00ED, 0x00EE, 0x00EF,
 0x0111, 0x00F1, 0x0323, 0x00F3, 0x00F4, 0x01A1, 0x00F6, 0x00F7,
 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x01B0, 0x20AB, 0x00FF},

//CP874 Thai (Windows)
{0x20AC,      0,      0,      0,      0, 0x2026,      0,      0,
      0,      0,      0,      0,      0,      0,      0,      0,
      0, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
      0,      0,      0,      0,      0,      0,      0,      0,
 0x00A0, 0x0E01, 0x0E02, 0x0E03, 0x0E04, 0x0E05, 0x0E06, 0x0E07,
 0x0E08, 0x0E09, 0x0E0A, 0x0E0B, 0x0E0C, 0x0E0D, 0x0E0E, 0x0E0F,
 0x0E10, 0x0E11, 0x0E12, 0x0E13, 0x0E14, 0x0E15, 0x0E16, 0x0E17,
 0x0E18, 0x0E19, 0x0E1A, 0x0E1B, 0x0E1C, 0x0E1D, 0x0E1E, 0x0E1F,
 0x0E20, 0x0E21, 0x0E22, 0x0E23, 0x0E24, 0x0E25, 0x0E26, 0x0E27,
 0x0E28, 0x0E29, 0x0E2A, 0x0E2B, 0x0E2C, 0x0E2D, 0x0E2E, 0x0E2F,
 0x0E30, 0x0E31, 0x0E32, 0x0E33, 0x0E34, 0x0E35, 0x0E36, 0x0E37,
 0x0E38, 0x0E39, 0x0E3A,      0,      0,      0,      0, 0x0E3F,
 0x0E40, 0x0E41, 0x0E42, 0x0E43, 0x0E44, 0x0E45, 0x0E46, 0x0E47,
 0x0E48, 0x0E49, 0x0E4A, 0x0E4B, 0x0E4C, 0x0E4D, 0x0E4E, 0x0E4F,
 0x0E50, 0x0E51, 0x0E52, 0x0E53, 0x0E54, 0x0E55, 0x0E56, 0x0E57,
 0x0E58, 0x0E59, 0x0E5A, 0x0E5B,      0,      0,      0,      0},
 
 //CP1250 Central European (Windows)
{0x20AC,      0, 0x201A,      0, 0x201E, 0x2026, 0x2020, 0x2021,
      0, 0x2030, 0x0160, 0x2039, 0x015A, 0x0164, 0x017D, 0x0179,
      0, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
      0, 0x2122, 0x0161, 0x203A, 0x015B, 0x0165, 0x017E, 0x017A,
 0x00A0, 0x02C7, 0x02D8, 0x0141, 0x00A4, 0x0104, 0x00A6, 0x00A7,
 0x00A8, 0x00A9, 0x015E, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x017B,
 0x00B0, 0x00B1, 0x02DB, 0x0142, 0x00B4, 0x00B5, 0x00B6, 0x00B7,
 0x00B8, 0x0105, 0x015F, 0x00BB, 0x013D, 0x02DD, 0x013E, 0x017C,
 0x0154, 0x00C1, 0x00C2, 0x0102, 0x00C4, 0x0139, 0x0106, 0x00C7,
 0x010C, 0x00C9, 0x0118, 0x00CB, 0x011A, 0x00CD, 0x00CE, 0x010E,
 0x0110, 0x0143, 0x0147, 0x00D3, 0x00D4, 0x0150, 0x00D6, 0x00D7,
 0x0158, 0x016E, 0x00DA, 0x0170, 0x00DC, 0x00DD, 0x0162, 0x00DF,
 0x0155, 0x00E1, 0x00E2, 0x0103, 0x00E4, 0x013A, 0x0107, 0x00E7,
 0x010D, 0x00E9, 0x0119, 0x00EB, 0x011B, 0x00ED, 0x00EE, 0x010F,
 0x0111, 0x0144, 0x0148, 0x00F3, 0x00F4, 0x0151, 0x00F6, 0x00F7,
 0x0159, 0x016F, 0x00FA, 0x0171, 0x00FC, 0x00FD, 0x0163, 0x02D9},
 
// Western European (ISO) iso_8859-1
{0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F,
 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7,
 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,
 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7,
 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF,
 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x00C7,
 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF,
 0x00D0, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x00D7,
 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x00DD, 0x00DE, 0x00DF,
 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7,
 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,
 0x00F0, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7,
 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x00FD, 0x00FE, 0x00FF},

// Central European (ISO)			iso_8859-2
{0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F,
 0x00A0, 0x0104, 0x02D8, 0x0141, 0x00A4, 0x013D, 0x015A, 0x00A7,
 0x00A8, 0x0160, 0x015E, 0x0164, 0x0179, 0x00AD, 0x017D, 0x017B,
 0x00B0, 0x0105, 0x02DB, 0x0142, 0x00B4, 0x013E, 0x015B, 0x02C7,
 0x00B8, 0x0161, 0x015F, 0x0165, 0x017A, 0x02DD, 0x017E, 0x017C,
 0x0154, 0x00C1, 0x00C2, 0x0102, 0x00C4, 0x0139, 0x0106, 0x00C7,
 0x010C, 0x00C9, 0x0118, 0x00CB, 0x011A, 0x00CD, 0x00CE, 0x010E,
 0x0110, 0x0143, 0x0147, 0x00D3, 0x00D4, 0x0150, 0x00D6, 0x00D7,
 0x0158, 0x016E, 0x00DA, 0x0170, 0x00DC, 0x00DD, 0x0162, 0x00DF,
 0x0155, 0x00E1, 0x00E2, 0x0103, 0x00E4, 0x013A, 0x0107, 0x00E7,
 0x010D, 0x00E9, 0x0119, 0x00EB, 0x011B, 0x00ED, 0x00EE, 0x010F,
 0x0111, 0x0144, 0x0148, 0x00F3, 0x00F4, 0x0151, 0x00F6, 0x00F7,
 0x0159, 0x016F, 0x00FA, 0x0171, 0x00FC, 0x00FD, 0x0163, 0x02D9},

// Latin 3 (ISO)				ISO_8859-3
{0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F,
 0x00A0, 0x0126, 0x02D8, 0x00A3, 0x00A4, 0x0124, 0x00A7, 0x00A8,
 0x0130, 0x015E, 0x011E, 0x0134, 0x00AD, 0x017B, 0x00B0, 0x0127,
 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x0125, 0x00B7, 0x00B8, 0x0131,
 0x015F, 0x011F, 0x0135, 0x00BD, 0x017C, 0x00C0, 0x00C1, 0x00C2,
 0x00C4, 0x010A, 0x0108, 0x00C7, 0x00C8, 0x00C9, 0x00CA, 0x00CB,
 0x00CC, 0x00CD, 0x00CE, 0x00CF, 0x00D1, 0x00D2, 0x00D3, 0x00D4,
 0x0120, 0x00D6, 0x00D7, 0x011C, 0x00D9, 0x00DA, 0x00DB, 0x00DC,
 0x016C, 0x015C, 0x00DF, 0x00E0, 0x00E1, 0x00E2, 0x00E4, 0x010B,
 0x0109, 0x00E7, 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED,
 0x00EE, 0x00EF, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x0121, 0x00F6,
 0x00F7, 0x011D, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x016D, 0x015D},

// Baltic (ISO)				ISO_8859-4
{0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F,
 0x00A0, 0x0104, 0x0138, 0x0156, 0x00A4, 0x0128, 0x013B, 0x00A7,
 0x00A8, 0x0160, 0x0112, 0x0122, 0x0166, 0x00AD, 0x017D, 0x00AF,
 0x00B0, 0x0105, 0x02DB, 0x0157, 0x00B4, 0x0129, 0x013C, 0x02C7,
 0x00B8, 0x0161, 0x0113, 0x0123, 0x0167, 0x014A, 0x017E, 0x014B,
 0x0100, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x012E,
 0x010C, 0x00C9, 0x0118, 0x00CB, 0x0116, 0x00CD, 0x00CE, 0x012A,
 0x0110, 0x0145, 0x014C, 0x0136, 0x00D4, 0x00D5, 0x00D6, 0x00D7,
 0x00D8, 0x0172, 0x00DA, 0x00DB, 0x00DC, 0x0168, 0x016A, 0x00DF,
 0x0101, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x012F,
 0x010D, 0x00E9, 0x0119, 0x00EB, 0x0117, 0x00ED, 0x00EE, 0x012B,
 0x0111, 0x0146, 0x014D, 0x0137, 0x00F4, 0x00F5, 0x00F6, 0x00F7,
 0x00F8, 0x0173, 0x00FA, 0x00FB, 0x00FC, 0x0169, 0x016B, 0x02D9},

// Cyrillic (ISO)				ISO_8859-5
{0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F,
 0x00A0, 0x0401, 0x0402, 0x0403, 0x0404, 0x0405, 0x0406, 0x0407,
 0x0408, 0x0409, 0x040A, 0x040B, 0x040C, 0x00AD, 0x040E, 0x040F,
 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417,
 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F,
 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427,
 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F,
 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437,
 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F,
 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447,
 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F,
 0x2116, 0x0451, 0x0452, 0x0453, 0x0454, 0x0455, 0x0456, 0x0457,
 0x0458, 0x0459, 0x045A, 0x045B, 0x045C, 0x00A7, 0x045E, 0x045F},

// Arabic (ASMO 708)			ISO_8859-6
{0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F,
 0x00A0,      0,      0,      0, 0x00A4,      0,      0,      0,
      0,      0,      0,      0, 0x060C, 0x00AD,      0,      0,
      0,      0,      0,      0,      0,      0,      0,      0,
      0,      0,      0, 0x061B,      0,      0,      0, 0x061F,
      0, 0x0621, 0x0622, 0x0623, 0x0624, 0x0625, 0x0626, 0x0627,
 0x0628, 0x0629, 0x062A, 0x062B, 0x062C, 0x062D, 0x062E, 0x062F,
 0x0630, 0x0631, 0x0632, 0x0633, 0x0634, 0x0635, 0x0636, 0x0637,
 0x0638, 0x0639, 0x063A,      0,      0,      0,      0,      0,
 0x0640, 0x0641, 0x0642, 0x0643, 0x0644, 0x0645, 0x0646, 0x0647,
 0x0648, 0x0649, 0x064A, 0x064B, 0x064C, 0x064D, 0x064E, 0x064F,
 0x0650, 0x0651, 0x0652,      0,      0,      0,      0,      0,
      0,      0,      0,      0,      0,      0,      0,      0},

// Greek (ISO)				ISO_8859-7
{0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F,
 0x00A0, 0x2018, 0x2019, 0x00A3,      0,      0, 0x00A6, 0x00A7,
 0x00A8, 0x00A9,      0, 0x00AB, 0x00AC, 0x00AD, 0x2015,      0,
 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x0384, 0x0385, 0x0386, 0x00B7,
 0x0388, 0x0389, 0x038A, 0x00BB, 0x038C, 0x00BD, 0x038E, 0x038F,
 0x0390, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397,
 0x0398, 0x0399, 0x039A, 0x039B, 0x039C, 0x039D, 0x039E, 0x039F,
 0x03A0, 0x03A1,      0, 0x03A3, 0x03A4, 0x03A5, 0x03A6, 0x03A7,
 0x03A8, 0x03A9, 0x03AA, 0x03AB, 0x03AC, 0x03AD, 0x03AE, 0x03AF,
 0x03B0, 0x03B1, 0x03B2, 0x03B3, 0x03B4, 0x03B5, 0x03B6, 0x03B7,
 0x03B8, 0x03B9, 0x03BA, 0x03BB, 0x03BC, 0x03BD, 0x03BE, 0x03BF,
 0x03C0, 0x03C1, 0x03C2, 0x03C3, 0x03C4, 0x03C5, 0x03C6, 0x03C7,
 0x03C8, 0x03C9, 0x03CA, 0x03CB, 0x03CC, 0x03CD, 0x03CE,      0},

// Hebrew (ISO-Visual) ISO-8859-8
{0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F,
 0x00A0,      0,  0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7,
 0x00A8, 0x00A9, 0x00D7, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,
 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7,
 0x00B8, 0x00B9, 0x00F7, 0x00BB, 0x00BC, 0x00BD, 0x00BE,      0,
      0,      0,      0,      0,      0,      0,      0,      0,
      0,      0,      0,      0,      0,      0,      0,      0,
      0,      0,      0,      0,      0,      0,      0,      0,
      0,      0,      0,      0,      0,      0,      0, 0x2017,
 0x05D0, 0x05D1, 0x05D2, 0x05D3, 0x05D4, 0x05D5, 0x05D6, 0x05D7,
 0x05D8, 0x05D9, 0x05DA, 0x05DB, 0x05DC, 0x05DD, 0x05DE, 0x05DF,
 0x05E0, 0x05E1, 0x05E2, 0x05E3, 0x05E4, 0x05E5, 0x05E6, 0x05E7,
 0x05E8, 0x05E9, 0x05EA,      0,      0, 0x200E, 0x200F,      0},

// Turkish (ISO)				ISO_8859-9
{0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F,
 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7,
 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,
 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7,
 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF,
 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x00C7,
 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF,
 0x011E, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x00D7,
 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x0130, 0x015E, 0x00DF,
 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7,
 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,
 0x011F, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7,
 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x0131, 0x015F, 0x00FF},

// Baltic (DOS)				cp775
{0x0106, 0x00fc, 0x00e9, 0x0101, 0x00e4, 0x0123, 0x00e5, 0x0107,
 0x0142, 0x0113, 0x0156, 0x0157, 0x012b, 0x0179, 0x00c4, 0x00c5,
 0x00c9, 0x00e6, 0x00c6, 0x014d, 0x00f6, 0x0122, 0x00a2, 0x015a,
 0x015b, 0x00d6, 0x00dc, 0x00f8, 0x00a3, 0x00d8, 0x00d7, 0x00a4,
 0x0100, 0x012a, 0x00f3, 0x017b, 0x017c, 0x017a, 0x201d, 0x00a6,
 0x00a9, 0x00ae, 0x00ac, 0x00bd, 0x00bc, 0x0141, 0x00ab, 0x00bb,
 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x0104, 0x010c, 0x0118,
 0x0116, 0x2563, 0x2551, 0x2557, 0x255d, 0x012e, 0x0160, 0x2510,
 0x2514, 0x2534, 0x252c, 0x251c, 0x2500, 0x253c, 0x0172, 0x016a,
 0x255a, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256c, 0x017d,
 0x0105, 0x010d, 0x0119, 0x0117, 0x012f, 0x0161, 0x0173, 0x016b,
 0x017e, 0x2518, 0x250c, 0x2588, 0x2584, 0x258c, 0x2590, 0x2580,
 0x00d3, 0x00df, 0x014c, 0x0143, 0x00f5, 0x00d5, 0x00b5, 0x0144,
 0x0136, 0x0137, 0x013b, 0x013c, 0x0146, 0x0112, 0x0145, 0x2019,
 0x00ad, 0x00b1, 0x201c, 0x00be, 0x00b6, 0x00a7, 0x00f7, 0x201e,
 0x00b0, 0x2219, 0x00b7, 0x00b9, 0x00b3, 0x00b2, 0x25a0, 0x00a0},

// Western European (DOS)			cp850
{0x00c7, 0x00fc, 0x00e9, 0x00e2, 0x00e4, 0x00e0, 0x00e5, 0x00e7,
 0x00ea, 0x00eb, 0x00e8, 0x00ef, 0x00ee, 0x00ec, 0x00c4, 0x00c5,
 0x00c9, 0x00e6, 0x00c6, 0x00f4, 0x00f6, 0x00f2, 0x00fb, 0x00f9,
 0x00ff, 0x00d6, 0x00dc, 0x00f8, 0x00a3, 0x00d8, 0x00d7, 0x0192,
 0x00e1, 0x00ed, 0x00f3, 0x00fa, 0x00f1, 0x00d1, 0x00aa, 0x00ba,
 0x00bf, 0x00ae, 0x00ac, 0x00bd, 0x00bc, 0x00a1, 0x00ab, 0x00bb,
 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x00c1, 0x00c2, 0x00c0,
 0x00a9, 0x2563, 0x2551, 0x2557, 0x255d, 0x00a2, 0x00a5, 0x2510,
 0x2514, 0x2534, 0x252c, 0x251c, 0x2500, 0x253c, 0x00e3, 0x00c3,
 0x255a, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256c, 0x00a4,
 0x00f0, 0x00d0, 0x00ca, 0x00cb, 0x00c8, 0x0131, 0x00cd, 0x00ce,
 0x00cf, 0x2518, 0x250c, 0x2588, 0x2584, 0x00a6, 0x00cc, 0x2580,
 0x00d3, 0x00df, 0x00d4, 0x00d2, 0x00f5, 0x00d5, 0x00b5, 0x00fe,
 0x00de, 0x00da, 0x00db, 0x00d9, 0x00fd, 0x00dd, 0x00af, 0x00b4,
 0x00ad, 0x00b1, 0x2017, 0x00be, 0x00b6, 0x00a7, 0x00f7, 0x00b8,
 0x00b0, 0x00a8, 0x00b7, 0x00b9, 0x00b3, 0x00b2, 0x25a0, 0x00a0},

// Central European (DOS)			cp852
{0x00c7, 0x00fc, 0x00e9, 0x00e2, 0x00e4, 0x016f, 0x0107, 0x00e7,
 0x0142, 0x00eb, 0x0150, 0x0151, 0x00ee, 0x0179, 0x00c4, 0x0106,
 0x00c9, 0x0139, 0x013a, 0x00f4, 0x00f6, 0x013d, 0x013e, 0x015a,
 0x015b, 0x00d6, 0x00dc, 0x0164, 0x0165, 0x0141, 0x00d7, 0x010d,
 0x00e1, 0x00ed, 0x00f3, 0x00fa, 0x0104, 0x0105, 0x017d, 0x017e,
 0x0118, 0x0119, 0x00ac, 0x017a, 0x010c, 0x015f, 0x00ab, 0x00bb,
 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x00c1, 0x00c2, 0x011a,
 0x015e, 0x2563, 0x2551, 0x2557, 0x255d, 0x017b, 0x017c, 0x2510,
 0x2514, 0x2534, 0x252c, 0x251c, 0x2500, 0x253c, 0x0102, 0x0103,
 0x255a, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256c, 0x00a4,
 0x0111, 0x0110, 0x010e, 0x00cb, 0x010f, 0x0147, 0x00cd, 0x00ce,
 0x011b, 0x2518, 0x250c, 0x2588, 0x2584, 0x0162, 0x016e, 0x2580,
 0x00d3, 0x00df, 0x00d4, 0x0143, 0x0144, 0x0148, 0x0160, 0x0161,
 0x0154, 0x00da, 0x0155, 0x0170, 0x00fd, 0x00dd, 0x0163, 0x00b4,
 0x00ad, 0x02dd, 0x02db, 0x02c7, 0x02d8, 0x00a7, 0x00f7, 0x00b8,
 0x00b0, 0x00a8, 0x02d9, 0x0171, 0x0158, 0x0159, 0x25a0, 0x00a0},

// Turkish (DOS)				cp857
{0x00c7, 0x00fc, 0x00e9, 0x00e2, 0x00e4, 0x00e0, 0x00e5, 0x00e7,
 0x00ea, 0x00eb, 0x00e8, 0x00ef, 0x00ee, 0x0131, 0x00c4, 0x00c5,
 0x00c9, 0x00e6, 0x00c6, 0x00f4, 0x00f6, 0x00f2, 0x00fb, 0x00f9,
 0x0130, 0x00d6, 0x00dc, 0x00f8, 0x00a3, 0x00d8, 0x015e, 0x015f,
 0x00e1, 0x00ed, 0x00f3, 0x00fa, 0x00f1, 0x00d1, 0x011e, 0x011f,
 0x00bf, 0x00ae, 0x00ac, 0x00bd, 0x00bc, 0x00a1, 0x00ab, 0x00bb,
 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x00c1, 0x00c2, 0x00c0,
 0x00a9, 0x2563, 0x2551, 0x2557, 0x255d, 0x00a2, 0x00a5, 0x2510,
 0x2514, 0x2534, 0x252c, 0x251c, 0x2500, 0x253c, 0x00e3, 0x00c3,
 0x255a, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256c, 0x00a4,
      0, 0x00ba, 0x00aa, 0x00ca, 0x00cb, 0x00c8, 0x00cd, 0x00ce,
 0x00cf, 0x2518, 0x250c, 0x2588, 0x2584, 0x00a6, 0x00cc, 0x2580,
 0x00d3,      0, 0x00df, 0x00d4, 0x00d2, 0x00f5, 0x00d5, 0x00b5,
 0x00d7, 0x00da, 0x00db, 0x00d9, 0x00ec, 0x00ff, 0x00af, 0x00b4,
 0x00ad, 0x00b1,      0, 0x00be, 0x00b6, 0x00a7, 0x00f7, 0x00b8,
 0x00b0, 0x00a8, 0x00b7, 0x00b9, 0x00b3, 0x00b2, 0x25a0, 0x00a0},

// Cyrillic (DOS)				cp866
{0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417,
 0x0418, 0x0419, 0x041a, 0x041b, 0x041c, 0x041d, 0x041e, 0x041f,
 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427,
 0x0428, 0x0429, 0x042a, 0x042b, 0x042c, 0x042d, 0x042e, 0x042f,
 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437,
 0x0438, 0x0439, 0x043a, 0x043b, 0x043c, 0x043d, 0x043e, 0x043f,
 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556,
 0x2555, 0x2563, 0x2551, 0x2557, 0x255d, 0x255c, 0x255b, 0x2510,
 0x2514, 0x2534, 0x252c, 0x251c, 0x2500, 0x253c, 0x255e, 0x255f,
 0x255a, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256c, 0x2567,
 0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256b,
 0x256a, 0x2518, 0x250c, 0x2588, 0x2584, 0x258c, 0x2590, 0x2580,
 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447,
 0x0448, 0x0449, 0x044a, 0x044b, 0x044c, 0x044d, 0x044e, 0x044f,
 0x0401, 0x0451, 0x0404, 0x0454, 0x0407, 0x0457, 0x040e, 0x045e,
 0x00b0, 0x2219, 0x00b7, 0x221a, 0x2116, 0x00a4, 0x25a0, 0x00a0},

// Greek, Modern (DOS)			cp869
{     0,      0,      0,      0,      0,      0, 0x0386,      0,
 0x00b7, 0x00ac, 0x00a6, 0x2018, 0x2019, 0x0388, 0x2015,      0,
      0, 0x0389, 0x038a, 0x03aa, 0x038c, 0x038e, 0x03ab, 0x00a9,
 0x038f, 0x00b2, 0x00b3, 0x03ac, 0x00a3, 0x03ad, 0x03ae, 0x03af,
 0x03ca, 0x0390, 0x03cc, 0x03cd, 0x0391, 0x0392, 0x0393, 0x0394,
 0x0395, 0x0396, 0x0397, 0x00bd, 0x0398, 0x0399, 0x00ab, 0x00bb,
 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x039a, 0x039b, 0x039c,
 0x039d, 0x2563, 0x2551, 0x2557, 0x255d, 0x039e, 0x039f, 0x2510,
 0x2514, 0x2534, 0x252c, 0x251c, 0x2500, 0x253c, 0x03a0, 0x03a1,
 0x255a, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256c, 0x03a3,
 0x03a4, 0x03a5, 0x03a6, 0x03a7, 0x03a8, 0x03a9, 0x03b1, 0x03b2,
 0x03b3, 0x2518, 0x250c, 0x2588, 0x2584, 0x03b4, 0x03b5, 0x2580,
 0x03b6, 0x03b7, 0x03b8, 0x03b9, 0x03ba, 0x03bb, 0x03bc, 0x03bd,
 0x03be, 0x03bf, 0x03c0, 0x03c1, 0x03c3, 0x03c2, 0x03c4, 0x0384,
 0x00ad, 0x00b1, 0x03c5, 0x03c6, 0x03c7, 0x00a7, 0x03c8, 0x0385,
 0x00b0, 0x00a8, 0x03c9, 0x03cb, 0x03b0, 0x03ce, 0x25a0, 0x00a0},

// Cyrillic (KOI8-R)			koi8r
{0x2500, 0x2502, 0x250C, 0x2510, 0x2514, 0x2518, 0x251C, 0x2524,
 0x252C, 0x2534, 0x253C, 0x2580, 0x2584, 0x2588, 0x258C, 0x2590,
 0x2591, 0x2592, 0x2593, 0x2320, 0x25A0, 0x2219, 0x221A, 0x2248,
 0x2264, 0x2265, 0x00A0, 0x2321, 0x00B0, 0x00B2, 0x00B7, 0x00F7,
 0x2550, 0x2551, 0x2552, 0x0451, 0x2553, 0x2554, 0x2555, 0x2556,
 0x2557, 0x2558, 0x2559, 0x255A, 0x255B, 0x255C, 0x255D, 0x255E,
 0x255F, 0x2560, 0x2561, 0x0401, 0x2562, 0x2563, 0x2564, 0x2565,
 0x2566, 0x2567, 0x2568, 0x2569, 0x256A, 0x256B, 0x256C, 0x00A9,
 0x044E, 0x0430, 0x0431, 0x0446, 0x0434, 0x0435, 0x0444, 0x0433,
 0x0445, 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E,
 0x043F, 0x044F, 0x0440, 0x0441, 0x0442, 0x0443, 0x0436, 0x0432,
 0x044C, 0x044B, 0x0437, 0x0448, 0x044D, 0x0449, 0x0447, 0x044A,
 0x042E, 0x0410, 0x0411, 0x0426, 0x0414, 0x0415, 0x0424, 0x0413,
 0x0425, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E,
 0x041F, 0x042F, 0x0420, 0x0421, 0x0422, 0x0423, 0x0416, 0x0412,
 0x042C, 0x042B, 0x0417, 0x0428, 0x042D, 0x0429, 0x0427, 0x042A}
};

char Char_Sets[CHAR_SETS+1][32]=
{
    "Unicode (UTF-8)",
    "Cyrillic (Windows-1251)",
    "Western European (Windows-1252)",
    "Greek (Windows-1253)",
    "Turkish (Windows-1254)",
    "Hebrew (Windows-1255)",
    "Arabic (Windows-1256)",
    "Baltic (Windows-1257)",
    "Vietnamese (Windows-1258)",
    "Thai (Windows-874)",
    "Central European (Windows-1250)",
    "Western European (ISO)",
    "Central European (ISO)",
    "Latin 3 (ISO)",
    "Baltic (ISO)",
    "Cyrillic (ISO)",
    "Arabic (ASMO 708)",
    "Greek (ISO)",
    "Hebrew (ISO-Visual)",
    "Turkish (ISO)",
    "Baltic (DOS)",
    "Western European (DOS)",
    "Central European (DOS)",
    "Turkish (DOS)",
    "Cyrillic (DOS)",
    "Greek, Modern (DOS)",
    "Cyrillic (KOI8-R)", 
};

char Char_Sets_Short[CHAR_SETS+1][13]=
{
    "UTF-8",
    "Windows-1251",
    "Windows-1252",
    "Windows-1253",
    "Windows-1254",
    "Windows-1255",
    "Windows-1256",
    "Windows-1257",
    "Windows-1258",
    "Windows-874",
    "Windows-1250",
    "ISO_8859-1",
    "ISO_8859-2",
    "ISO_8859-3",
    "ISO_8859-4",
    "ISO_8859-5",
    "ISO_8859-6",
    "ISO_8859-7",
    "ISO-8859-8",
    "ISO_8859-9",
    "cp775",
    "cp850",
    "cp852",
    "cp857",
    "cp866",
    "cp869",
    "koi8r", 
};

int readUTF8(const unsigned char *src, DWORD *code)
{
  int i, j;
  static int bits[] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02};

  unsigned char ch = src[0];
  // on error return 0 and code==0, but code==0 can be with return 1

  for(i = 0; i < 7; i++)
    if (!(ch & bits[i]))
      break;

  switch (i)
  {
   case 0:
    *code = ch;
    return 1;
   case 2:
    if ((src[1] & 0xC0) != 0x80)
    {
      *code = 0;
      return 0;
    }
    *code = ((((int)ch) & 0x1F) << 6)+(src[1] & 0x3F);
    return 2;
   case 3:
    if ((src[1] & 0xC0) != 0x80 || (src[2] & 0xC0) != 0x80)
    {
      *code = 0;
      return 0;
    }
    *code = ((((int)ch) & 0xF) << 12)+(((int)(src[1]) & 0x3F) << 6)+(src[2] & 0x3F);
    return 3;
   case 4:
   case 5:
   case 6:
    ch = ch << (i+1);
    ch = ch >> (i+1);

    *code |= (((int)ch) << (6*(i-1)));

    for(j = i-1; j > 0; j--)
    {
      ch = src[j];
      if ((ch & 0xC0) != 0x80)
      {
        *code = 0;
        return 0;
      }
      ch = ch & 0x3F; // 00xx xxxx
      *code |= (((int)ch) << (6* (i - j - 1)));
    }
    return i;
   default:
    *code = 0;
    return 0;
  }
}

int unicodeToUTF8(unsigned code, unsigned char *result)
{
  unsigned bytes;
  unsigned i;
  static unsigned range[] = {0x7F, 0x7FF, 0xFFFF, 0x1FFFFF, 0x03FFFFFF, 0x7FFFFFFF};
  static unsigned startByte[] = {0xF0, 0xF8, 0xFC};

  //if (code < 0)  // case 7

  for(i = 0; i < 6; i++)
    if (code <= range[i])
      break;
  bytes = i + 1;

  switch (bytes)
  {
   case 1:
    result[0] = code;
    break;
   case 2:
    result[0] = 0xC0 | (code >> 6);
    result[1] = 0x80 | (code & 0x3F);
    break;
   case 3:
    result[0] = 0xE0 | (code >> 12);
    result[1] = 0x80 | ((code >> 6) & 0x3F);
    result[2] = 0x80 | (code & 0x3F);
    break;
   case 4:
   case 5:
   case 6:
    result[0] = startByte[bytes - 4] | (code >> (6 * (bytes-1)));
    for(i = bytes-1; i > 0; i--)
    {
      result[i] = 0x80 | (code & 0x3F);
      code = code >> 6;
    }
    break;
   case 7:
    bytes=0;
  }
  result[bytes] = 0;
  return bytes;
}

int char_set(DWORD what, DWORD from, DWORD to) // what >= 128
{
  int i;
  
  if (to>0)
  {
    if (from>0 && (what=MAP_CP[from-1][what-128])==0)
      return 1;
    switch (to)
    {
     case 1: // CP_1251
      if(what>=0x410 && what<=0x44F)
        return what - 0x350; // -0x410+0xC0
      for(i = 0; i < 0x40; i++) // (the rest)
      {
        if (MAP_CP[0][i] == what)
          return 0x80 + i;
      }
      return 0;
     case 2: // CP_1252
      if(what>=0xA0 && what<=0xFF)
        return what;
      for(i = 0; i < 0x20; i++) // (the rest)
      {
        if (MAP_CP[1][i] == what)
          return 0x80 + i;
      }
      return 0;
     case 3: // Greek
      if(what>0x387 && what<0x3cf && what!=0x38b && what!=0x38d && what!=0x3a2)
        return what - 0x2d0; // -0x388+0xB8
      for(i = 0; i < 0x3e; i++) // (the rest)
      {
        if (MAP_CP[2][i] == what)
          return 0x80 + i;
      }
      return 0;
     case 5: // Hebrew
      if(what>=0x5b0 && what<=0x5ea && ((what<=0x5c3 && what!=0x5ba) || what>=0x5d0))
        return what - 0x4f0; // -0x5b0+0xC0
      if(what>=0x5f0 && what<=0x5f4)
        return what - 0x51c; // -0x5f0+0xD4
      if(what==0x200e || what==0x200f)
        return what - 0x1f11; //  - 0x200e+0xFD
      for(i = 0; i < 0x40; i++) // (the rest)
      {
        if (MAP_CP[4][i] == what)
          return 0x80 + i;
      }
      return 0;
     case 9: // Thai
      if(what>0xe00 && what<0xe5c && (what<0xe3b || what>0xe3e))
        return what - 0xd60; // -0xE01+0xA1
      for(i = 0; i < 0x21; i++) // (the rest)
      {
        if (MAP_CP[8][i] == what)
          return 0x80 + i;
      }
      return 0;
     default:
      for(i = 0; i < 0x80; i++) // (the rest)
      {
        if (MAP_CP[to-1][i] == what)
          return 0x80 + i;
      }
      return 0;
    }
  }
  else if (from>0)
    return MAP_CP[from-1][what-128];
  else
    return what;
}

DWORD ChangeCharset(DWORD ch_set1, DWORD ch_set2, unsigned char *from, unsigned char *to, DWORD *Bad, DWORD dies, DWORD LimitTo)
// Bad 1 - not in ch_set2, 2 - not in ch_set1, 4 - bad unicode, 8 unicode bigger than 2 bytes 
{
  DWORD i, j, code, read;
  
  *Bad=0;
	if(!from) //plamen!  - may be smth better?
	  return 0;
  if ((ch_set1&63)>0)
  {
    if (ch_set1==ch_set2)
    {
the_same:
      for (i=0; from[i] && i<LimitTo; to[i]=from[i], i++);
      to[i]=0;
    }
    else if (ch_set2==64)
    {
      for (i=j=0; from[j] && i<LimitTo; i+=2, j++)
      {
        if (from[j]<128)
        {
          to[i]=from[j];
          to[i+1]=0;
        }
        else
        {
          code=MAP_CP[ch_set1-1][from[j]-128];
          if (code)
          {
            to[i]=(BYTE)(code&0xff);
            to[i+1]=(BYTE)((code&0xff00)/0x100);
          }
          else // error
          {
            *Bad|=2;
            to[i]=(BYTE)dies; //'#'
            to[i+1]=0;
          }
        }
      }  
      to[i]=0; to[i+1]=0;
    }
    else
    {
      for (i=j=0; from[j] && i<LimitTo; j++)
      {
        if (from[j]<128)
          to[i++]=from[j];
        else
        {
          if (ch_set2==0)
          {
            if ((code=MAP_CP[ch_set1-1][from[j]-128])==0)
            {
              to[i++]=(BYTE)dies; //'#'
              *Bad|=2;
            }
            else
              i+=unicodeToUTF8(code, to + i);
          }
          else if ((code=char_set(from[j], ch_set1, ch_set2))<2)
          {
            to[i++]=(BYTE)dies; //'#'
            *Bad|=code+1;
          }
          else
            to[i++]=(BYTE)code;
        }
      }  
      to[i]=0;
    }
  }
  else if (ch_set1==64)
  {
    WORD *Str=(WORD*)from;
    
    for (i=j=0; Str[j] && i<LimitTo; j++)
    {
      if (Str[j]<128)
        to[i++]=(BYTE)(Str[j]);
      else
      {
        if (ch_set2==0)
          i+=unicodeToUTF8(Str[j], to + i);
        else if ((code=char_set(Str[j], 0, ch_set2))==0)
        {
          to[i++]=(BYTE)dies; //'#'
          *Bad|=1;
        }
        else
          to[i++]=(BYTE)code;
      }
    }  
    to[i]=0;
  }
  else // ch_set1==0
  {
    if ((ch_set2&63)>0)
    {
      for (i=j=0; from[j] && i<LimitTo; i++)
      {
        if (from[j]<128)
          to[i]=from[j++];
        else
        {
          j+=read=readUTF8(from + j, &code);
          if (read==0 || (code=char_set(code, 0, ch_set2))==0) // error
          {
            if (read==0)
            {
              j++;
              *Bad|=4;
            }
            else
              *Bad|=1;
            to[i]=(BYTE)dies; //'#'
          }
          else
            to[i]= (BYTE)code;
        }
      }
      to[i]=0;
    }
    else if (ch_set2==64)
    {
      for (i=j=0; from[j] && i<LimitTo; i+=2)
      {
        if (from[j]<128)
        {
          to[i]=from[j++];
          to[i+1]=0;
        }
        else
        {
          j+=readUTF8(from + j, &code);
          if (code && code<0x10000)
          {
            to[i]=(BYTE)(code&0xff);
            to[i+1]=(BYTE)((code&0xff00)/0x100);
          }
          else // error
          {
            if (code==0)
            {
              j++;
              *Bad|=4;
            }
            else
              *Bad|=8;
            to[i]=(BYTE)dies; //'#'
            to[i+1]=0;
          }
        }
      }  
      to[i]=0; to[i+1]=0;
    }
    else // from 0 to 0
      goto the_same;
  }
  return i;
}

unsigned correct_D_field(void)
{
  DWORD i, j;

  j=DEF_F+NUMB_FLAGS+1024;
  MitiUnlock();
  if (j<=LAST_FLAG && (hTemp=GlobalReAlloc(hD_field, 4*j, GMEM_MOVEABLE)))
  {
    hD_field=hTemp;
    MitiLock();
    for (i=DEF_F+NUMB_FLAGS; i<j; D_field[i++]=M1);
    DEF_F=j-NUMB_FLAGS;
    return(1);
  }
  MitiLock();
  MitiText("System Error: Too many definitions! Memory problems.\r\n", &cf_red);
  return(0);
}
// HALF heads. QUATER atoms,
// OBSEG1 built_in, OBSEG2 short built_in
// OBSEG3 functions, OBSEG4 constants
// OBSEG5 object name, OBSEG6 operator
void put_in_f(DWORD what, DWORD where, DWORD at, DWORD flag) // what must be !=M1
{
  DWORD i, j, h;

  if (Pointers[where] & (MASKA_P^M1))
  {
    if (D_field[(Pointers[where] & MASKA3)+at]==M1)
    {
      D_field[(Pointers[where] & MASKA3)+at]=what;
      Pointers[where]|=flag;
    }
    else // premesti
    {
      DWORD DF_min=0, DF_min_flag=HALF, new_at;
      // kyde?
      for (; (Pointers[where]&DF_min_flag)==0; DF_min_flag=DF_min_flag>>1, DF_min++);
loop: while (D_field[df_pointer]!=M1)
        if (df_pointer++ >= DEF_F)
          correct_D_field();
      new_at=df_pointer-min(DF_min,at);
      if (D_field[new_at+at]!=M1)
      {
        df_pointer++;
        goto loop;
      }
      for (i=DF_min, j=DF_min_flag; i<NUMB_FLAGS; j=j>>1, i++)
        if ((Pointers[where]&j)!=0 && D_field[new_at+i]!=M1)
        {
          df_pointer++;
          goto loop;
        }
      // mesti
      for (i=DF_min, j=DF_min_flag; i<NUMB_FLAGS; j=j>>1, i++)
        if (Pointers[where]&j)
        {
          D_field[new_at+i]=D_field[(Pointers[where] & MASKA3)+i];
          D_field[(Pointers[where] & MASKA3)+i]=M1;
        }
      if (df_min_p>(Pointers[where] & MASKA3)+DF_min)
        df_min_p=(Pointers[where] & MASKA3)+DF_min;
      D_field[new_at+at]=what;
      Pointers[where]=(Pointers[where] & (MASKA_P^M1)) | flag | new_at;
    }
  }
  else
  {
    for (h=df_min_p; D_field[h]!=M1 && (h<DEF_F || correct_D_field()); h++);
    Pointers[where]=flag | (h-at);
    D_field[h]=what;
    if ((df_min_p=h+1)>df_pointer)
      df_pointer=df_min_p;
  }
}

#if !CONSOLE
static DWORD CALLBACK StreamInCallbackEd(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
  int i=0;
  char** ppText = (char**) dwCookie;
  char* pText = *ppText;
  for(; i<cb && pText[i]; pbBuff[i]=pText[i], i++);
  *ppText+=i;
  *pcb = i;
  return 0;
}

void MitiText(CStringA Text, CHARFORMAT* cf)
{
  //if (status_fl&64) // Run Directly
	//	return;
  //MessageBoxA(NUL, "Test.", "Run Directly", MB_OK | MB_ICONSTOP);
  EDITSTREAM es;
  DWORD pText= (DWORD)(LPCSTR)Text;
	if( ! ::IsWindow(pMainFrame->m_hWnd) )  //plamen check if sending mesages on close
		return;
  if (pOutput==NULL)
    pMainFrame->SendMessage(WM_USER+2, 0, 0);
  es.dwCookie = (DWORD)&pText;
  es.pfnCallback = StreamInCallbackEd; 
  pOutput->SetSel(-1,-1);
  if (cf) // we don't need to check this but for Plamen
    pOutput->SetSelectionCharFormat(*cf);
  pOutput->StreamIn((CP_UTF8 << 16) | SF_USECODEPAGE | SF_TEXT | SFF_SELECTION, es); // 
}
void MitiText2(CStringA Text, int Type)
{
  if (Type==0)
    MitiText(Text, pFontMask);
#else // !CONSOLE
void MitiText(char *Text)
{
#if PROF==0
  printf("%s",Text);
#endif
}
void MitiText2(char *Text, int Type)
{
  if (Type==0)
    printf("%s",Text);
#endif // !CONSOLE
  else
  {
    int i, j=sv_StStore;

    if ((sv_StStore+=strlen(Text))>=STRING_STORE && correct_STR_ST() )
      MessageBoxA(NUL, "The string store is full.", "No Space", MB_OK | MB_ICONSTOP);
    for(i=0; Text[i]; StringStore[j++]=Text[i++]);
  }    
}

BOOL MitiGlobalAlloc(void)
{
    int i, j;
#if !CONSOLE
    hbrGreen = CreateSolidBrush(GetSysColor(COLOR_WINDOW));
    hMyRedPen = CreatePen(PS_INSIDEFRAME, 3, RGB(255, 0, 0));
    hMyBluePen = CreatePen(PS_INSIDEFRAME, 3, RGB(0, 0, 255));
    hSavePen=NULL;
    hSaveBrush=NULL;
    hSaveFont=NULL;
    cf_blue.cbSize=cf_red.cbSize=cf_black.cbSize=cf_green.cbSize=sizeof(CHARFORMAT); //?
    cf_blue.dwMask=cf_red.dwMask=cf_black.dwMask=cf_green.dwMask=
      CFM_COLOR | CFM_FACE | CFM_SIZE | CFM_OFFSET | CFM_BOLD | CFM_ITALIC | CFM_STRIKEOUT | CFM_UNDERLINE;
    cf_blue.crTextColor=RGB(0,0,255); //?
    cf_red.crTextColor=RGB(255,0,0);
    cf_black.crTextColor=RGB(0,0,0);
    cf_green.crTextColor=RGB(0,128,0);
    my_strcpy(cf_blue.szFaceName, DefaultProject.m_lf.lfFaceName);
    my_strcpy(cf_red.szFaceName, DefaultProject.m_lf.lfFaceName);
    my_strcpy(cf_black.szFaceName, DefaultProject.m_lf.lfFaceName);
    my_strcpy(cf_green.szFaceName, DefaultProject.m_lf.lfFaceName);
    cf_blue.yHeight=cf_red.yHeight=cf_black.yHeight=cf_green.yHeight=-DefaultProject.m_lf.lfHeight*15;
    if (cf_blue.yHeight <190) cf_blue.yHeight=cf_red.yHeight=cf_black.yHeight=cf_green.yHeight=190; //plamen test
    cf_blue.yOffset=cf_red.yOffset=cf_black.yOffset=cf_green.yOffset=0;
    cf_blue.dwEffects=cf_red.dwEffects=cf_black.dwEffects=cf_green.dwEffects=0;

    SYSTEMTIME LokTime;   // Random init
    GetLocalTime(&LokTime);
    srand(LokTime.wHour + LokTime.wMinute + LokTime.wSecond + LokTime.wMilliseconds);
#else
    long t;
    time( &t);
    srand(t);
#endif    
    for(i=0; i<126; i++)  // Key map init
    {
       for(j=0; key_map[j]!=i && j<255; j++);
       key_oposit[i]=(BYTE)j;
    }

    hFunctors=GlobalAlloc(GMEM_MOVEABLE, 4*FUNCTOR_L);
    hProgram=GlobalAlloc(GMEM_MOVEABLE, 4*(PROGRAM_L+8)); // 8 is the reserve but 2 is enoght
    hProgram2=GlobalAlloc(GMEM_MOVEABLE, 4*(PROGRAM_L+8));
    hProgTail=GlobalAlloc(GMEM_MOVEABLE, 4*(PROGRAM_L+8));
    hPointers=GlobalAlloc(GMEM_MOVEABLE, 4*(MEMORY_c256+MEMORY_V)); // zero GHND in ReAlloc
    hProg_Obj=GlobalAlloc(GMEM_MOVEABLE, 4*(PROGRAM_L+8));
    hAtoms=GlobalAlloc(GMEM_MOVEABLE, 4*(PROGRAM_L+8));
    hProg_fild=GlobalAlloc(GMEM_MOVEABLE, 4*(PROGRAM_F+8)); // 8 is the reserve but 3 or 4 is enoght
    hD_field=GlobalAlloc(GMEM_MOVEABLE, 4*(DEF_F+NUMB_FLAGS)); // NUMB_FLAGS is the reserve
    hF_field=GlobalAlloc(GMEM_MOVEABLE, 4*(FUNC_F+MAX_ARGUMENTS));
    hReadyLists=GlobalAlloc(GMEM_MOVEABLE, 4*READY_LISTS);
    hStringStore=GlobalAlloc(GMEM_MOVEABLE, STRING_STORE+L_BUFER+2); // L_BUFER is the reserve
    hNewObj=GlobalAlloc(GMEM_MOVEABLE, 4*(NEW_OBJ+8)); // 8 is the reserve but 2 is enoght
    hNewObj1=GlobalAlloc(GMEM_MOVEABLE, (NEW_OBJ+8));
    hNewObj2=GlobalAlloc(GMEM_MOVEABLE, 2*(NEW_OBJ+8));
    hStack=GlobalAlloc(GMEM_MOVEABLE, 4*(STACK2+8));
    hDurty=GlobalAlloc(GMEM_MOVEABLE, 4*MAX_VARS);
    hBuda1=GlobalAlloc(GMEM_MOVEABLE, 4*(BUDA3_UP+BUDA3+MAX_VARS));
    hBuda2=GlobalAlloc(GMEM_MOVEABLE, 4*(BUDA3_UP+BUDA3+MAX_VARS));
    hLink=GlobalAlloc(GMEM_MOVEABLE, 4*(BUDA3_UP+BUDA_S2+MAX_VARS)); // MAX_VARS is the reserve
    hUfil_c1=GlobalAlloc(GMEM_MOVEABLE, 4*(MEMORY_C+MEMORY_V));
    hUfil_c2=GlobalAlloc(GMEM_MOVEABLE, 4*(MEMORY_C+MEMORY_V));
    hUfil_vs=GlobalAlloc(GMEM_MOVEABLE, 4*MEMORY_V);
    hSalam=GlobalAlloc(GMEM_MOVEABLE, 4*(SALAM+2*BUFFERS));// place for 8 + 2*(br_buf1-buf1_min)  
    hEkler=GlobalAlloc(GMEM_MOVEABLE, 4*(EKLER+8)); // 8 is the reserve but 3 is enoght
    hBlocks=GlobalAlloc(GMEM_MOVEABLE, 4*(BLOCKS+TT_START+MAX_VARS));
  
    hBlocks_val=GlobalAlloc(GMEM_MOVEABLE, 4*(BLOCKS+TT_START+MAX_VARS));
    hBlocks_next=GlobalAlloc(GMEM_MOVEABLE, 4*(BLOCKS+TT_START+MAX_VARS));
    hChfil_v=GlobalAlloc(GMEM_MOVEABLE, 2*MEMORY_V);
    hSubsti=GlobalAlloc(GMEM_MOVEABLE, 2*(MEMORY_S+MAX_VARS)); // MAX_VARS is the reserve
    hSubstiT=GlobalAlloc(GMEM_MOVEABLE, 2*(MEMORY_STemp+1024));
    hCh_tree=GlobalAlloc(GHND, 2*MEMORY_tr);  // zero  ?!
    hObjectStore=GlobalAlloc(GMEM_MOVEABLE, 4*(OBJ_STORE+512));
    hMtree=GlobalAlloc(GMEM_MOVEABLE, 16*MEMORY_tr); // 4*4

    hChfil_c=GlobalAlloc(GMEM_MOVEABLE, MEMORY_C+MEMORY_V);

    hOper=GlobalAlloc(GMEM_MOVEABLE, 4*BUFFERS);
    hOpPr=GlobalAlloc(GMEM_MOVEABLE, 4*BUFFERS);
    hOpType=GlobalAlloc(GMEM_MOVEABLE, 4*BUFFERS);
    hOpMin=GlobalAlloc(GMEM_MOVEABLE, 4*BUFFERS);
    Hbuf_sub=GlobalAlloc(GMEM_MOVEABLE, 4*BUFFERS);
    hArray=GlobalAlloc(GMEM_MOVEABLE, 8*BUFFERS); // *2
    hArType=GlobalAlloc(GMEM_MOVEABLE, 8*BUFFERS); //
    hArPos=GlobalAlloc(GMEM_MOVEABLE, 8*BUFFERS); // *2
    hAddInfo=GlobalAlloc(GMEM_MOVEABLE, 4*BUFFERS);
    Hatom_buf=GlobalAlloc(GMEM_MOVEABLE, L_BUFER+2);
    Hvar_field=GlobalAlloc(GMEM_MOVEABLE, L_BUFER+2);
    hReadStack=GlobalAlloc(GMEM_MOVEABLE, 4*BUFFERS);
    hLeft=GlobalAlloc(GMEM_MOVEABLE, 4*BUFFERS);
    hRight=GlobalAlloc(GMEM_MOVEABLE, 4*BUFFERS);
    Hbuf1=GlobalAlloc(GMEM_MOVEABLE, 4*BUFFERS);
    Hbuf2=GlobalAlloc(GMEM_MOVEABLE, 4*BUFFERS);
    Hbufu1=GlobalAlloc(GMEM_MOVEABLE, 4*BUFFERS);
    Hbufu2=GlobalAlloc(GMEM_MOVEABLE, 4*BUFFERS);
    Hbufu3=GlobalAlloc(GMEM_MOVEABLE, 4*BUFFERS);
    Hbufu4=GlobalAlloc(GMEM_MOVEABLE, 4*BUFFERS);
    Hbufu5=GlobalAlloc(GMEM_MOVEABLE, 4*BUFFERS);
    Hbufu6=GlobalAlloc(GMEM_MOVEABLE, 4*BUFFERS);
    Hbufu7=GlobalAlloc(GMEM_MOVEABLE, 4*BUFFERS);
    Hst_buf=GlobalAlloc(GMEM_MOVEABLE, 4*BUFFERS);
    Hbuf_3=GlobalAlloc(GMEM_MOVEABLE, 4*BUFFERS);  // bufu is up
    Hbuf_4=GlobalAlloc(GMEM_MOVEABLE, 4*BUFFERS);
    Hbuf_5=GlobalAlloc(GMEM_MOVEABLE, 4*BUFFERS);
    Hbuf_6=GlobalAlloc(GMEM_MOVEABLE, 4*BUFFERS);
    Hbuf_7=GlobalAlloc(GMEM_MOVEABLE, 4*BUFFERS);
    Hbuf_8=GlobalAlloc(GMEM_MOVEABLE, 16*BUFFERS);
    Hlist=GlobalAlloc(GMEM_MOVEABLE, 4*BUFFERS);
    Hglobal=GlobalAlloc(GMEM_MOVEABLE, 4*GLOBAL_max);
    Hglobal2=GlobalAlloc(GMEM_MOVEABLE, 4*GLOBAL_max);
    Hst_buf_num=GlobalAlloc(GMEM_MOVEABLE, 4*BUFFERS);

    if (!(hMtree && hObjectStore && hCh_tree && hSubstiT
         && hSubsti && hChfil_v && hChfil_c && hUfil_vs && hBlocks && hBlocks_val && hBlocks_next
          && hUfil_c2 && hUfil_c1 && hLink && hBuda2 && hBuda1
           && hStack && hDurty && hNewObj2 && hNewObj1 && hNewObj && hStringStore
            && hFunctors && hProg_fild && hD_field && hF_field && hReadyLists && hAtoms && hProg_Obj 
             && hProgram && hProgram2 && hOper && hOpPr && hOpType && hOpMin
               && Hbuf_sub && hArray && hArType && hArPos && hAddInfo && Hatom_buf
                 && Hvar_field && hReadStack && hLeft && hRight && Hbuf1 && Hbuf2 && hProgTail
                 && hPointers && Hbufu1 && Hbufu2 && Hbufu3 && Hbufu4 && Hbufu5
                  && Hbufu6 && Hbufu7 && Hst_buf && Hbuf_3 && Hbuf_4 && Hbuf_5 && Hbuf_6 
                   && Hbuf_7 && Hbuf_8 && Hlist && Hglobal && Hglobal2 && Hst_buf_num && hSalam && hEkler))
    {
       MessageBoxA(NUL, "    Can not allocate enough memory. Close some applications and try agein.",
        "Strawberry Prolog", MB_OK | MB_ICONSTOP);
      return FALSE;
    }
    MitiLock();
    for (i=BUDA3+MAX_VARS; i; Buda1[--i]=TT_VAR); // clean var
    for (i= -BUDA3_UP; i; Link[i]=i, i++);        // init negative link
    for (i=BLOCKS+TT_START+MAX_VARS; i; Blocks_val[--i]=SM1); // clean blocks
    ObjectStore[0]=0;
    for(i=0; i<512; StringStore[i]=i/2, StringStore[i+1]=0, i+=2);
    //for(i=0; i<256; StringStore[2*i]=i, StringStore[2*i+1]=0, i++);
    // Bug in Microsoft optimizations !!! global and full
    //  sprintf((LPSTR)string2, "StringStore[0, 1, 2]=%d, %d, %d", StringStore[0], StringStore[1], StringStore[2]);
    //  MessageBoxA(NUL, (LPSTR)string2, "Strawberry Prolog", MB_OK | MB_ICONSTOP);        
    Stack[0]=HALF;                     /* Tati */
    Stack[1]=Stack[3]=Stack[4]=0;
    Stack[5]=1;   // Atoms[0]
    Stack[2]=13;  // Atoms[0]+12
    for(i=4*FIRST_ROWS; i; Link[--i]=0);  // 16 for sub ? what I mean?
    Link[4]=1;
    Link[8]=2;
    Link[12]=3;
    Link[16]=4;
    Link[20]=5;
    Link[24]=6;
    Link[28]=7;
    Link[32]=8;
    Link[36]=9;
    Ekler[0]=0;
    for(i=FIRST_ROWS; i--; ProgTail[i]=Prog_Obj[i]=M1);
    return TRUE;
}

void MitiLock(void)
{
#if TR
  if (GlobalFlags(hFunctors)==GMEM_INVALID_HANDLE
   || GlobalFlags(hProgram)==GMEM_INVALID_HANDLE
   || GlobalFlags(hProgram2)==GMEM_INVALID_HANDLE
   || GlobalFlags(hProgTail)==GMEM_INVALID_HANDLE
   || GlobalFlags(hPointers)==GMEM_INVALID_HANDLE)
    MitiText("Invalid Handle 1\r\n", &cf_red);
  if (GlobalFlags(hProg_Obj)==GMEM_INVALID_HANDLE
   || GlobalFlags(hAtoms)==GMEM_INVALID_HANDLE
   || GlobalFlags(hProg_fild)==GMEM_INVALID_HANDLE
   || GlobalFlags(hD_field)==GMEM_INVALID_HANDLE
   || GlobalFlags(hF_field)==GMEM_INVALID_HANDLE
   || GlobalFlags(hReadyLists)==GMEM_INVALID_HANDLE)
    MitiText("Invalid Handle 2\r\n", &cf_red);
  if (GlobalFlags(hStringStore)==GMEM_INVALID_HANDLE
   || GlobalFlags(hNewObj)==GMEM_INVALID_HANDLE
   || GlobalFlags(hNewObj1)==GMEM_INVALID_HANDLE
   || GlobalFlags(hNewObj2)==GMEM_INVALID_HANDLE
   || GlobalFlags(hDurty)==GMEM_INVALID_HANDLE)
    MitiText("Invalid Handle 3\r\n", &cf_red);
  if (GlobalFlags(hStack)==GMEM_INVALID_HANDLE
   || GlobalFlags(hBuda1)==GMEM_INVALID_HANDLE
   || GlobalFlags(hBuda2)==GMEM_INVALID_HANDLE
   || GlobalFlags(hLink)==GMEM_INVALID_HANDLE)
    MitiText("Invalid Handle 4\r\n", &cf_red);
  if (GlobalFlags(hUfil_c1)==GMEM_INVALID_HANDLE
   || GlobalFlags(hUfil_c2)==GMEM_INVALID_HANDLE
   || GlobalFlags(hUfil_vs)==GMEM_INVALID_HANDLE
   || GlobalFlags(hBlocks)==GMEM_INVALID_HANDLE
   || GlobalFlags(hBlocks_val)==GMEM_INVALID_HANDLE
   || GlobalFlags(hBlocks_next)==GMEM_INVALID_HANDLE)
    MitiText("Invalid Handle 5\r\n", &cf_red);
  if (GlobalFlags(hSalam)==GMEM_INVALID_HANDLE
   || GlobalFlags(hEkler)==GMEM_INVALID_HANDLE
   || GlobalFlags(hChfil_c)==GMEM_INVALID_HANDLE
   || GlobalFlags(hChfil_v)==GMEM_INVALID_HANDLE
   || GlobalFlags(hSubsti)==GMEM_INVALID_HANDLE)
    MitiText("Invalid Handle 6\r\n", &cf_red);
  if (GlobalFlags(hSubstiT)==GMEM_INVALID_HANDLE
   || GlobalFlags(hCh_tree)==GMEM_INVALID_HANDLE
   || GlobalFlags(hObjectStore)==GMEM_INVALID_HANDLE
   || GlobalFlags(hMtree)==GMEM_INVALID_HANDLE)
    MitiText("Invalid Handle 7\r\n", &cf_red);
  if (GlobalFlags(hOper)==GMEM_INVALID_HANDLE
   || GlobalFlags(hOpPr)==GMEM_INVALID_HANDLE
   || GlobalFlags(hOpType)==GMEM_INVALID_HANDLE
   || GlobalFlags(hOpMin)==GMEM_INVALID_HANDLE
   || GlobalFlags(Hbuf_sub)==GMEM_INVALID_HANDLE)
    MitiText("Invalid Handle 8\r\n", &cf_red);
  if (GlobalFlags(hArray)==GMEM_INVALID_HANDLE
   || GlobalFlags(hArType)==GMEM_INVALID_HANDLE
   || GlobalFlags(hArPos)==GMEM_INVALID_HANDLE
   || GlobalFlags(hAddInfo)==GMEM_INVALID_HANDLE
   || GlobalFlags(Hatom_buf)==GMEM_INVALID_HANDLE)
    MitiText("Invalid Handle 9\r\n", &cf_red);
  if (GlobalFlags(Hvar_field)==GMEM_INVALID_HANDLE
   || GlobalFlags(hReadStack)==GMEM_INVALID_HANDLE
   || GlobalFlags(hLeft)==GMEM_INVALID_HANDLE
   || GlobalFlags(hRight)==GMEM_INVALID_HANDLE
   || GlobalFlags(Hbuf1)==GMEM_INVALID_HANDLE)
    MitiText("Invalid Handle 10\r\n", &cf_red);
  if (GlobalFlags(Hbuf2)==GMEM_INVALID_HANDLE
   || GlobalFlags(Hbufu1)==GMEM_INVALID_HANDLE
   || GlobalFlags(Hbufu2)==GMEM_INVALID_HANDLE
   || GlobalFlags(Hbufu3)==GMEM_INVALID_HANDLE
   || GlobalFlags(Hbufu4)==GMEM_INVALID_HANDLE)
    MitiText("Invalid Handle 11\r\n", &cf_red);
  if (GlobalFlags(Hbufu5)==GMEM_INVALID_HANDLE
   || GlobalFlags(Hbufu6)==GMEM_INVALID_HANDLE
   || GlobalFlags(Hbufu7)==GMEM_INVALID_HANDLE
   || GlobalFlags(Hst_buf)==GMEM_INVALID_HANDLE
   || GlobalFlags(Hbuf_3)==GMEM_INVALID_HANDLE)
    MitiText("Invalid Handle 12\r\n", &cf_red);
  if (GlobalFlags(Hbuf_4)==GMEM_INVALID_HANDLE
   || GlobalFlags(Hbuf_5)==GMEM_INVALID_HANDLE
   || GlobalFlags(Hbuf_6)==GMEM_INVALID_HANDLE
   || GlobalFlags(Hbuf_7)==GMEM_INVALID_HANDLE
   || GlobalFlags(Hbuf_8)==GMEM_INVALID_HANDLE)
    MitiText("Invalid Handle 13\r\n", &cf_red);
  if (GlobalFlags(Hlist)==GMEM_INVALID_HANDLE
   || GlobalFlags(Hglobal)==GMEM_INVALID_HANDLE
   || GlobalFlags(Hglobal2)==GMEM_INVALID_HANDLE
   || GlobalFlags(Hst_buf_num)==GMEM_INVALID_HANDLE)
    MitiText("Invalid Handle 14\r\n", &cf_red);
#endif
  Functors=(LPDWORD)GlobalLock(hFunctors);
  Program=(LPDWORD)GlobalLock(hProgram);
  Program2=(LPDWORD)GlobalLock(hProgram2);
  ProgTail=(LPDWORD)GlobalLock(hProgTail);
  Pointers=(LPDWORD)GlobalLock(hPointers);
  Prog_Obj=(LPDWORD)GlobalLock(hProg_Obj);
  Atoms=(LPDWORD)GlobalLock(hAtoms);
  Prog_fild=(LPDWORD)GlobalLock(hProg_fild);
  D_field=(LPDWORD)GlobalLock(hD_field);
  F_field=(LPDWORD)GlobalLock(hF_field);
  ReadyLists=(LPDWORD)GlobalLock(hReadyLists);
  StringStore=(BYTE *)GlobalLock(hStringStore);
  NewObj=(LPDWORD)GlobalLock(hNewObj);
  NewObj1=(BYTE *)GlobalLock(hNewObj1);
  NewObj2=(LPWORD)GlobalLock(hNewObj2);
  Durty=(LPDWORD)GlobalLock(hDurty);    
  Stack=(LPDWORD)GlobalLock(hStack);
  Buda1=(LPDWORD)GlobalLock(hBuda1)+BUDA3_UP;
  Buda2=(LPDWORD)GlobalLock(hBuda2)+BUDA3_UP;
  Link=(LPDWORD)GlobalLock(hLink)+BUDA3_UP;
  Ufil_c1=(LPDWORD)GlobalLock(hUfil_c1);
  Ufil_c2=(LPDWORD)GlobalLock(hUfil_c2);
  Ufil_vs=(LPDWORD)GlobalLock(hUfil_vs);
  Salam=(LPDWORD)GlobalLock(hSalam);
  Ekler=(LPDWORD)GlobalLock(hEkler);
  ObjectStore=(LPDWORD)GlobalLock(hObjectStore);
  Blocks=(LPDWORD)GlobalLock(hBlocks);

  Blocks_val=(LPWORD)GlobalLock(hBlocks_val);
  Blocks_next=(LPWORD)GlobalLock(hBlocks_next);
  Chfil_v=(LPWORD)GlobalLock(hChfil_v);
  Substi=(LPWORD)GlobalLock(hSubsti);
  SubstiT=(LPWORD)GlobalLock(hSubstiT);
  Ch_tree=(LPWORD)GlobalLock(hCh_tree);
  Mtree=(unsigned4 *)GlobalLock(hMtree);
  
  Chfil_c=(BYTE *)GlobalLock(hChfil_c);

  Oper=(LPDWORD)GlobalLock(hOper);
  OpPr=(LPDWORD)GlobalLock(hOpPr);
  OpType=(LPDWORD)GlobalLock(hOpType);
  OpMin= (LPDWORD)GlobalLock(hOpMin);
  buf_sub=(LPDWORD)GlobalLock(Hbuf_sub);
  Array=(LPDWORD)GlobalLock(hArray);
  ArType=(LPDWORD)GlobalLock(hArType);
  ArPos=(LPDWORD)GlobalLock(hArPos);
  AddInfo=(LPDWORD)GlobalLock(hAddInfo);
  atom_buf=(BYTE *)GlobalLock(Hatom_buf);
  var_field=(BYTE *)GlobalLock(Hvar_field);
  ReadStack=(LPDWORD)GlobalLock(hReadStack);
  Left=(LPDWORD)GlobalLock(hLeft);
  Right=(LPDWORD)GlobalLock(hRight);
  buf1=(LPDWORD)GlobalLock(Hbuf1);
  buf2=(LPDWORD)GlobalLock(Hbuf2);
  bufu1=(LPDWORD)GlobalLock(Hbufu1);
  bufu2=(LPDWORD)GlobalLock(Hbufu2);
  bufu3=(LPDWORD)GlobalLock(Hbufu3);
  bufu4=(LPDWORD)GlobalLock(Hbufu4);
  bufu5=(LPDWORD)GlobalLock(Hbufu5);
  bufu6=(LPDWORD)GlobalLock(Hbufu6);
  bufu7=(LPDWORD)GlobalLock(Hbufu7);
  st_buf=(LPDWORD)GlobalLock(Hst_buf);
  buf_3=(LPDWORD)GlobalLock(Hbuf_3);
  buf_4=(LPDWORD)GlobalLock(Hbuf_4);
  buf_5=(LPDWORD)GlobalLock(Hbuf_5);
  buf_6=(LPDWORD)GlobalLock(Hbuf_6);
  buf_7=(LPDWORD)GlobalLock(Hbuf_7);
  buf_8=(LPDWORD)GlobalLock(Hbuf_8);
  list=(LPDWORD)GlobalLock(Hlist);
  global=(LPDWORD)GlobalLock(Hglobal);
  global2=(LPDWORD)GlobalLock(Hglobal2);
  st_buf_num=(LPDWORD)GlobalLock(Hst_buf_num);

#if TR
  if (!(Mtree && ObjectStore && Ch_tree && SubstiT
           && Substi && Chfil_v && Chfil_c && Ufil_vs && Blocks && Ufil_c2
            && Ufil_c1 && Link && Buda2 && Buda1
             && Stack && Durty && NewObj2 && NewObj1 && NewObj && StringStore
              && Prog_fild && Atoms && Prog_Obj 
               && Program && Program2 && Oper && OpPr && OpType && OpMin
                && buf_sub && Array && ArType && ArPos && AddInfo && atom_buf 
                 && var_field && ReadStack && Left && Right && buf1 && buf2 && ProgTail
                  && Pointers && bufu1 && bufu2 && bufu3 && bufu4 && bufu5
                   && bufu6 && bufu7 && st_buf && buf_3 && buf_4 && buf_5 && buf_6 
                    && buf_7 && buf_8 && list && global && st_buf_num && Salam && Ekler))
    MessageBoxA(NUL, "    Memory problem! Close some applications and try agein.",
      "Strawberry Prolog", MB_OK | MB_ICONSTOP);
#endif
}
void MitiUnlock(void)
{
#if !CONSOLE
  GlobalUnlock(hFunctors);
  GlobalUnlock(hProgram);
  GlobalUnlock(hProgram2);
  GlobalUnlock(hProgTail);
  GlobalUnlock(hPointers);
  GlobalUnlock(hProg_Obj);
  GlobalUnlock(hAtoms);
  GlobalUnlock(hProg_fild);
  GlobalUnlock(hD_field);
  GlobalUnlock(hF_field);
  GlobalUnlock(hReadyLists);
  GlobalUnlock(hStringStore);
  GlobalUnlock(hNewObj);
  GlobalUnlock(hNewObj1);
  GlobalUnlock(hNewObj2);
  GlobalUnlock(hStack);
  GlobalUnlock(hDurty);    
  GlobalUnlock(hBuda1);
  GlobalUnlock(hBuda2);
  GlobalUnlock(hLink);
  GlobalUnlock(hUfil_c1);
  GlobalUnlock(hUfil_c2);
  GlobalUnlock(hUfil_vs);
  GlobalUnlock(hSalam);
  GlobalUnlock(hEkler);
  GlobalUnlock(hBlocks);
  
  GlobalUnlock(hBlocks_val);
  GlobalUnlock(hBlocks_next);
  GlobalUnlock(hChfil_v);
  GlobalUnlock(hSubsti);
  GlobalUnlock(hSubstiT);
  GlobalUnlock(hCh_tree);
  GlobalUnlock(hObjectStore);
  GlobalUnlock(hMtree);

  GlobalUnlock(hChfil_c);

  GlobalUnlock(hOper);
  GlobalUnlock(hOpPr);
  GlobalUnlock(hOpType);
  GlobalUnlock(hOpMin);
  GlobalUnlock(Hbuf_sub);
  GlobalUnlock(hArray);
  GlobalUnlock(hArType);
  GlobalUnlock(hArPos);
  GlobalUnlock(hAddInfo);
  GlobalUnlock(Hatom_buf);
  GlobalUnlock(Hvar_field);
  GlobalUnlock(hReadStack);
  GlobalUnlock(hLeft);
  GlobalUnlock(hRight);
  GlobalUnlock(Hbuf1);
  GlobalUnlock(Hbuf2);
  GlobalUnlock(Hbufu1);
  GlobalUnlock(Hbufu2);
  GlobalUnlock(Hbufu3);
  GlobalUnlock(Hbufu4);
  GlobalUnlock(Hbufu5);
  GlobalUnlock(Hbufu6);
  GlobalUnlock(Hbufu7);
  GlobalUnlock(Hst_buf);
  GlobalUnlock(Hbuf_3);
  GlobalUnlock(Hbuf_4);
  GlobalUnlock(Hbuf_5);
  GlobalUnlock(Hbuf_6);
  GlobalUnlock(Hbuf_7);
  GlobalUnlock(Hbuf_8);
  GlobalUnlock(Hlist);
  GlobalUnlock(Hglobal);
  GlobalUnlock(Hglobal2);
  GlobalUnlock(Hst_buf_num);
#endif
}
unsigned correct_list(DWORD obj_num, DWORD new_size)
{
  DWORD j;

  if (new_size==0)
    j=ObjectStore[obj_num+3]+LISTS_INCREASE;
  else
    j=LISTS_INCREASE*(1+new_size/LISTS_INCREASE);
  MitiUnlock();
  GlobalUnlock((MY_HANDLE)ObjectStore[obj_num+5]);
  if (hTemp=GlobalReAlloc((MY_HANDLE)ObjectStore[obj_num+5], 4*j, GMEM_MOVEABLE))
  {
    ObjectStore[obj_num+5]=(DWORD)hTemp;
    ObjectStore[obj_num+4]=(DWORD)(LPDWORD)GlobalLock((MY_HANDLE)ObjectStore[obj_num+5]);
    MitiLock();
    ObjectStore[obj_num+3]=j;
    return(0);
  }
  ObjectStore[obj_num+4]=(DWORD)(LPDWORD)GlobalLock((MY_HANDLE)ObjectStore[obj_num+5]);
  MitiLock();
  MitiText("System Error: Too many arrays! Memory problem.\r\n", &cf_red);
  return(1);
}
unsigned correct_F_field(void)
{
  DWORD j;

  j=FUNC_F+1024+MAX_ARGUMENTS;
  MitiUnlock();
  if (j<=LAST_FLAG_F && (hTemp=GlobalReAlloc(hF_field, 4*j, GMEM_MOVEABLE)))
  {
    hF_field=hTemp;
    MitiLock();
    FUNC_F=j-MAX_ARGUMENTS;
    return(0);
  }
  MitiLock();
  MitiText("System Error: Too many definitions! Memory problem.\r\n", &cf_red);
  return(1);
}
unsigned correct_ReadyLists(DWORD ncArgs)
{
  DWORD i, j;

  for (j=READY_LISTS+128; j<=ncArgs; j+=128);
  MitiUnlock();
  if (hTemp=GlobalReAlloc(hReadyLists, 4*j, GMEM_MOVEABLE))
  {
    hReadyLists=hTemp;
    MitiLock();
    for (i=READY_LISTS; i<j; ReadyLists[i]=creat_p(2, TT_VAR, ReadyLists[i-1], M1 ,1), i++);
    READY_LISTS=j;
    return(0);
  }
  MitiLock();
  MitiText("System Error: Too many Ready Lists! Memory problems.\r\n", &cf_red);
  return(1);
}
unsigned correct_MEM_S(void)
{
  DWORD j;

  j=MEMORY_S+MAX_VARS+2048;
  MitiUnlock();
  if (hTemp=GlobalReAlloc(hSubsti, 2*j, GMEM_MOVEABLE))
  {
    hSubsti=hTemp;
    MEMORY_S=j-MAX_VARS;
    MitiLock();
    return(0);
  }
  MitiLock();
  return(1);
}
unsigned correct_MEM_STemp(int Add)
{
  DWORD j;

  for (j=MEMORY_STemp+1024; j<sv_subT+Add; j+=1024);
  j+=1024;
  MitiUnlock();
  if (hTemp=GlobalReAlloc(hSubstiT, 2*j, GMEM_MOVEABLE))
  {
    hSubstiT=hTemp;
    MEMORY_STemp=j-1024;
    MitiLock();
    return(0);
  }
  MitiLock();
  return(1);
}
unsigned correct_BLOCKS(void)
{
  DWORD j, i;

  j=BLOCKS+TT_START+MAX_VARS+2048;
  MitiUnlock();
  if (hTemp=GlobalReAlloc(hBlocks, 4*j, GMEM_MOVEABLE))
  {
    hBlocks=hTemp;
    if (hTemp=GlobalReAlloc(hBlocks_val, 2*j, GMEM_MOVEABLE))
    {
      hBlocks_val=hTemp;
      if (hTemp=GlobalReAlloc(hBlocks_next, 2*j, GMEM_MOVEABLE))
      {
        hBlocks_next=hTemp;
        MitiLock();
        for (i=BLOCKS+TT_START+MAX_VARS; i<j; Blocks_val[i++]=SM1);
        BLOCKS=j-TT_START-MAX_VARS;
        return(0);
      }
    }
  }
  MitiLock();
  return(1);
}
unsigned correct_PROG_F(void)
{
  DWORD j;

  j=PROGRAM_F+8+3072;
  MitiUnlock();
  if (hTemp=GlobalReAlloc(hProg_fild, 4*j, GMEM_MOVEABLE))
  {
    hProg_fild=hTemp;
    PROGRAM_F=j-8;
    MitiLock();
    return(0);
  }
  MitiLock();
  return(1);
}
unsigned correct_SALAM(void)
{
  DWORD j;

  j=SALAM+2*BUFFERS+256;
  MitiUnlock();
  if (hTemp=GlobalReAlloc(hSalam, 4*j, GMEM_MOVEABLE))
  {
    hSalam=hTemp;
    SALAM=j-2*BUFFERS;
    MitiLock();
    return(0);
  }
  MitiLock();
  return(1);
}
unsigned correct_EKLER(void)
{
  DWORD j;

  j=EKLER+8+2048;
  MitiUnlock();
  if (hTemp=GlobalReAlloc(hEkler, 4*j, GMEM_MOVEABLE))
  {
    hEkler=hTemp;
    EKLER=j-8;
    MitiLock();
    return(0);
  }
  MitiLock();
  return(1);
}
unsigned correct_OBJ_ST(void)
{
  DWORD j;

  j=OBJ_STORE+(512+1024);
  MitiUnlock();
  if (hTemp=GlobalReAlloc(hObjectStore, 4*j, GMEM_MOVEABLE))
  {
    hObjectStore=hTemp;
    OBJ_STORE=j-512;
    MitiLock();
    return(0);
  }
  MitiLock();
  return(1);
}
unsigned correct_STR_ST(void)
{
  DWORD j;

  for (j=STRING_STORE+8192; j<sv_StStore; j+=8192);
  j+=L_BUFER+2;
  MitiUnlock();
  if (hTemp=GlobalReAlloc(hStringStore, j, GMEM_MOVEABLE))
  {
    hStringStore=hTemp;
    STRING_STORE=j-L_BUFER-2;
    MitiLock();
    return(0);
  }
  MitiLock();
  return(1);
}
unsigned correct_GLOB_max(void)
{
  DWORD j;

  j=GLOBAL_max+256;
  MitiUnlock();
  if (hTemp=GlobalReAlloc(Hglobal, 4*j, GMEM_MOVEABLE))
  {
    Hglobal=hTemp;
    if (hTemp=GlobalReAlloc(Hglobal2, 4*j, GMEM_MOVEABLE))
    {
      Hglobal2=hTemp;
      GLOBAL_max=j;
      MitiLock();
      return(0);
    }
  }
  MitiLock();
  return(1);
}
unsigned correct_FUNCTOR(void)
{
  DWORD j;

  j=FUNCTOR_L+128;
  MitiUnlock();
  if (hTemp=GlobalReAlloc(hFunctors, 4*j, GMEM_MOVEABLE))
  {
    hFunctors=hTemp;
    FUNCTOR_L=j;
    MitiLock();
    return(0);
  }
  MitiLock();
  return(1);
}
unsigned correct_Stack(void)
{
  DWORD STACK_max=(pInfo2->m_Settings&32)?DefaultProject.STACK_max:pInfo2->STACK_max;
  if (pInfo2->m_Settings&1) //Prolongated Stack
  {
    DWORD j;

    if (STACK2>=STACK_max-8)
    {
      //MessageBeep(MB_ICONQUESTION); why this is not making sound?
      sprintf(string2, "The stack size is %d.\nThe maximum is reached. Do you want to increase the stack anyway?", (STACK2+8)/4);
      if (MessageBoxA( pMainFrame->m_hWnd, string2, "Strawberry Prolog (Stack)", MB_YESNO | MB_ICONQUESTION)!=IDYES)
        return(1);
      j=max(STACK2+4008, 2*(STACK2+8));
    }
    else if (STACK2>=STACK_max-4008)
      j=STACK_max;
    else
      j=STACK2+4008;
    MitiUnlock();
    if (hTemp=GlobalReAlloc(hStack, 4*j, GMEM_MOVEABLE) ) // ?!? GMEM_NODISCARD
    {
      hStack=hTemp;
      MitiLock();
      STACK2=j-8;
      return (0);
    }
    MitiLock();
    return (1);
  }
  else  
    return (1);
}
unsigned correct_LINK(void)
{
  DWORD j;
  DWORD BUDA_S_max=(pInfo2->m_Settings&32)?DefaultProject.BUDA_S_max:pInfo2->BUDA_S_max;
  if (BUDA_S2+MAX_VARS>=BUDA_S_max)
  {
    //MessageBeep(MB_ICONQUESTION);
    //sprintf(string2, "The Linking Memory is %d.\nThe maximum is reached. Do you want to increase it anyway?", (BUDA_S2+MAX_VARS)/256);
    //if (MessageBoxA( pMainFrame->m_hWnd, string2, "Strawberry Prolog (Linking Memory)", MB_YESNO | MB_ICONQUESTION)!=IDYES)
    //  return(1);
    j=max(BUDA_S2+MAX_VARS+8192, 2*(BUDA_S2+MAX_VARS));
  }
  else if (BUDA_S2>=BUDA_S_max-MAX_VARS-8192)
    j=BUDA_S_max;
  else
    j=BUDA_S2+MAX_VARS+8192;
  MitiUnlock();
  if (hTemp=GlobalReAlloc(hLink, 4*(j+BUDA3_UP), GMEM_MOVEABLE))
  {
    hLink=hTemp;
    BUDA_S2=j-MAX_VARS;
    MitiLock();
    return(0);
  }
  MitiLock();
  return(1);
}
unsigned correct_BUDA(void)
{
  DWORD i, j;
  DWORD BUDA_max=(pInfo2->m_Settings&32)?DefaultProject.BUDA_max:pInfo2->BUDA_max;
  if (BUDA3+MAX_VARS>=BUDA_max)
  {
    //MessageBeep(MB_ICONQUESTION);
    //sprintf(string2, "The number of variables is %d K.\nThe maximum is reached. Do you want to increase it anyway?", BUDA3/1024);
    //if (MessageBoxA( pMainFrame->m_hWnd, string2, "Strawberry Prolog (Variables)", MB_YESNO | MB_ICONQUESTION)!=IDYES)
    //  return(1);
    j=max(BUDA3+MAX_VARS+4096, 2*BUDA3+MAX_VARS);
  }
  else if (BUDA3>=BUDA_max-MAX_VARS-4096)
    j=BUDA_max;
  else
    j=BUDA3+MAX_VARS+4096;
  MitiUnlock();
  if (hTemp=GlobalReAlloc(hBuda1, 4*(j+BUDA3_UP), GMEM_MOVEABLE))
  {
    hBuda1=hTemp;
    if (hTemp=GlobalReAlloc(hBuda2, 4*(j+BUDA3_UP), GMEM_MOVEABLE))
    {
      hBuda2=hTemp;
      MitiLock();
      for (i=BUDA3+MAX_VARS; i<j; Buda1[i++]=TT_VAR);
      BUDA3=j-MAX_VARS;
      return(0);
    }
  }
  MitiLock();
  return(1);
}
unsigned correct_BUDA_UP(void) // !!! list_br == ?
{
  int i, j1, j2;

  j1=BUDA3+MAX_VARS+4096;
  j2=BUDA_S2+MAX_VARS+4096;
  MitiUnlock();
  if (hTemp=GlobalReAlloc(hBuda1, 4*(j1+BUDA3_UP), GMEM_MOVEABLE))
  {
    hBuda1=hTemp;
    if (hTemp=GlobalReAlloc(hBuda2, 4*(j1+BUDA3_UP), GMEM_MOVEABLE))
    {
      hBuda2=hTemp;
      if (hTemp=GlobalReAlloc(hLink, 4*(j2+BUDA3_UP), GMEM_MOVEABLE))
      {
        hLink=hTemp;
        MitiLock();
        for (i=BUDA3+MAX_VARS; i<j1; Buda1[i++]=TT_VAR); // clean the new part
        for (i=buda_br+list_br; i> -buda_br_up; i--, Buda1[i+4096]=Buda1[i], Buda2[i+4096]=Buda2[i]); // move all up
        for (i=sv_subv; i> -buda_br_up; i--, Link[i+4096]=Link[i]); // move all up
        Link+=4096;
        for (i= -BUDA3_UP-4096; i< -BUDA3_UP; Link[i]=i, i++); // init the new part
        BUDA3_UP+=4096;
        Buda1+=4096;
        Buda2+=4096;
        return(0);
      }
    }
  }
  MitiLock();
  return(1);
}
unsigned correct_MEM_tr(void)
{
  DWORD j;

  j=MEMORY_tr+4096;
  MitiUnlock();
  if (hTemp=GlobalReAlloc(hMtree, 16*j, GMEM_MOVEABLE))
  {
    hMtree=hTemp;
    if (hTemp=GlobalReAlloc(hCh_tree, 2*j, GMEM_MOVEABLE))
    {
      hCh_tree=hTemp;
      MitiLock();
      MEMORY_tr=j;
      return(0);
    }
  }
#if !CONSOLE
  DWORD theerr = GetLastError();
  FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL,theerr,0,string3,L_BUFER,NULL);
  MitiText(make_utf8(string3));
#endif
  MitiLock();
  return(1);
}
unsigned correct_NEW_OBJ(void)
{
  DWORD j;

  j=NEW_OBJ+8+1024;
  MitiUnlock();
  if (hTemp=GlobalReAlloc(hNewObj, 4*j, GMEM_MOVEABLE))
  {
    hNewObj=hTemp;
    if (hTemp=GlobalReAlloc(hNewObj1, j, GMEM_MOVEABLE))
    {
      hNewObj1=hTemp;
      if (hTemp=GlobalReAlloc(hNewObj2, 2*j, GMEM_MOVEABLE))
      {
        hNewObj2=hTemp;
        MitiLock();
        NEW_OBJ=j-8;
        return(0);
      }
    }
  }
  MitiLock();
  return(1);
}
unsigned correct_PROG_L(void)
{
  DWORD j;

  for (j=PROGRAM_L+8+512; j<program_br+8; j+=512); 
  MitiUnlock();
  if (hTemp=GlobalReAlloc(hProgram, 4*j, GMEM_MOVEABLE))
  {
    hProgram=hTemp;
    if (hTemp=GlobalReAlloc(hProgram2, 4*j, GMEM_MOVEABLE))
    {
      hProgram2=hTemp;
      if (hTemp=GlobalReAlloc(hProgTail, 4*j, GMEM_MOVEABLE))
      {
        hProgTail=hTemp;
        if (hTemp=GlobalReAlloc(hProg_Obj, 4*j, GMEM_MOVEABLE))
        {
          hProg_Obj=hTemp;
          if (hTemp=GlobalReAlloc(hAtoms, 4*j, GMEM_MOVEABLE))
          {
            hAtoms=hTemp;
            MitiLock();
            PROGRAM_L=j-8;
            return(0);
          }
        }
      }
    }
  }
  MitiLock();
  return(1);
}
unsigned correct_MEM_V(void)
{
  DWORD i, j;

  j=MEMORY_V+1024;
  MitiUnlock();
  if (hTemp=GlobalReAlloc(hPointers, 4*(MEMORY_c256+j), GMEM_MOVEABLE))
  {
    hPointers=hTemp;
    if (hTemp=GlobalReAlloc(hUfil_c1, 4*(MEMORY_C+j), GMEM_MOVEABLE))
    {
      hUfil_c1=hTemp;
      if (hTemp=GlobalReAlloc(hUfil_c2, 4*(MEMORY_C+j), GMEM_MOVEABLE))
      {
        hUfil_c2=hTemp;
        if (hTemp=GlobalReAlloc(hUfil_vs, 4*j, GMEM_MOVEABLE))
        {
          hUfil_vs=hTemp;
          if (hTemp=GlobalReAlloc(hChfil_c, MEMORY_C+j, GMEM_MOVEABLE))
          {
            hChfil_c=hTemp;
            if (hTemp=GlobalReAlloc(hChfil_v, 2*j, GMEM_MOVEABLE))
            {
              hChfil_v=hTemp;
              MitiLock();
              for (i=MEMORY_c256+MEMORY_V; i<MEMORY_c256+j; Pointers[i++]=0);
              MEMORY_V=j;
              return(0);
            }
          }
        }
      }
    }
  }
  MitiLock();
  return(1);
}
unsigned correct_MEM_C(void)
{
  DWORD i, j, t, k;

#if TR
  MitiText("correct_MEM_C\r\n");
#endif      
  j=MEMORY_c256+INCREASE_C;
  MitiUnlock();
  if (hTemp=GlobalReAlloc(hPointers, 4*(j+MEMORY_V), GMEM_MOVEABLE))
  {
    hPointers=hTemp;
    if (hTemp=GlobalReAlloc(hUfil_c1, 4*(j-TT_START+MEMORY_V), GMEM_MOVEABLE))
    {
      hUfil_c1=hTemp;
      if (hTemp=GlobalReAlloc(hUfil_c2, 4*(j-TT_START+MEMORY_V), GMEM_MOVEABLE))
      {
        hUfil_c2=hTemp;
        if (hTemp=GlobalReAlloc(hChfil_c, j-TT_START+MEMORY_V, GMEM_MOVEABLE))
        {
          hChfil_c=hTemp;
          MitiLock();
          if (status_fl&256)
            return(0);
          for (i=MEMORY_C+sv_v; i-->MEMORY_C; )
          {
            Chfil_c[i+INCREASE_C]=Chfil_c[i];
            Ufil_c1[i+INCREASE_C]=(Ufil_c1[i]>=MEMORY_c256)?(Ufil_c1[i]+INCREASE_C):Ufil_c1[i];
            Ufil_c2[i+INCREASE_C]=(Ufil_c2[i]>=MEMORY_c256)?(Ufil_c2[i]+INCREASE_C):Ufil_c2[i];
            Pointers[i+TT_START+INCREASE_C]=Pointers[i+TT_START];
          }
          for (i=MEMORY_c256; i<j; Pointers[i++]=0);
          for (i=max(MEMORY_c256+MEMORY_V,j+sv_v); i<j+MEMORY_V; Pointers[i++]=0); // Zeroinit
          for (i=min( max(buda_br, a1)+MAX_VARS, BUDA3); i--; ) // ? start from?
            if (Buda1[i]>=MEMORY_c256)
              Buda1[i]+=INCREASE_C;
          for (i=sv_tr; i--; )
          {
            for (k=0; k<4; k++)
            {
              if ((Mtree[i][k]&MASKA2)<MASKA && (Mtree[i][k]&MASKA2)>=MEMORY_C)
                Mtree[i][k]+=INCREASE_C;
            }
          }
          for (i=sv_blocks+TT_START+MAX_VARS; --i; );
          {
            if (Blocks_val[i]<SM2 && (Blocks[i]&MASKA2)<MASKA && (Blocks[i]&MASKA2)>=MEMORY_C)
              Blocks[i]+=INCREASE_C;
          }
          if (status_fl&2) // running
          {
            for (i=0; i<program_br1; i++)
              if (!(Program[i]&HALF))
                for (t=Atoms[i]+1; t--; )
                  if (Prog_fild[Program[i]+2*t]>=MEMORY_c256)
                    Prog_fild[Program[i]+2*t]+=INCREASE_C;
            for (i=program_br2; i<program_br; i++)
              if (!(Program[i]&HALF))
                for (t=Atoms[i]+1; t--; )
                  if (Prog_fild[Program[i]+2*t]>=MEMORY_c256)
                    Prog_fild[Program[i]+2*t]+=INCREASE_C;
          }
          else
          {
            for (i=0; i<program_br1; i++)
              if (Program[i]>=MEMORY_c256)
                Program[i]+=INCREASE_C;
            for (i=program_br2; i<program_br; i++)
              if (Program[i]>=MEMORY_c256)
                Program[i]+=INCREASE_C;
          }
          MEMORY_c256=j;
          MEMORY_C=j-TT_START;
          return(0);
        }
      }
    }
  }
  MitiLock();
  return(1);
}
