#include <stdio.h>
int main(int argc, char **argv)
{
  FILE *f = fopen("OFF.LR1", "rb");
  unsigned char trans[16][256];
  int startletter = 'A'-1;
  int c;
  int i;
  int len;
  int cypher;
  int pos;
  int freq[256];
  unsigned char word[10];
  word[pos = 0] = '\0';
  for (len - 0; len < 16; len++) {
    for (i = 0; i < 256; i++) {freq[i] = 0; trans[len][i] = '?';}
  }
  trans[0][0xb7] = 'A';
  trans[0][0xbe] = 'B';
  trans[1][0x53] = 'R';
  trans[1][0x61] = 'S';
  trans[1][0x6e] = 'L';
  trans[1][0x72] = 'H';
  trans[1][0x7b] = 'A';
  trans[2][0x05] = 'I';
  trans[2][0x06] = 'I';
  trans[2][0x08] = 'F';
  trans[2][0x0c] = 'M';
  trans[2][0x0d] = 'L';
  trans[2][0x0f] = 'E';
  trans[2][0x13] = 'P';
  trans[2][0x18] = 'N';
  trans[2][0x1b] = 'K';
  trans[2][0x1d] = 'S';
  trans[2][0x27] = 'S';
  trans[2][0x37] = 'S';
  trans[2][0x3b] = 'C';
  trans[2][0xf3] = 'V';
  trans[2][0xec] = 'G';
  trans[2][0xed] = 'R';
  trans[2][0xde] = 'D';
  trans[3][0x71] = 'W';
  trans[3][0x7f] = 'V';
  trans[3][0x87] = 'G';
  trans[3][0x8e] = 'O';
  trans[3][0x9c] = 'H';
  trans[3][0xa0] = 'N';
  trans[3][0xa1] = 'O';
  trans[3][0xb2] = 'I';
  trans[3][0xb5] = 'I';
  trans[3][0xbf] = 'T';
  trans[3][0xc8] = 'D';
  trans[3][0xc9] = 'P';
  trans[3][0xca] = 'A';
  trans[3][0xcb] = 'D';
  trans[3][0xd4] = 'U';
  trans[3][0xe9] = 'K';
  trans[3][0xea] = 'A';
  trans[3][0xf2] = 'I';
  trans[3][0xf5] = 'E';
  trans[4][0x08] = 'O';
  trans[4][0x10] = 'A';
  trans[4][0x2c] = 'G';
  trans[4][0x3f] = 'N';
  trans[4][0x42] = 'H';
  trans[4][0x50] = 'G';
  trans[4][0x54] = 'C';
  trans[4][0x5a] = 'E';
  trans[4][0x60] = 'O';
  trans[4][0x65] = 'S';
  trans[4][0x7e] = 'S';
  trans[4][0x81] = 'S';
  trans[4][0x88] = 'S';
  trans[4][0x93] = 'M';
  trans[4][0xa4] = 'S';
  trans[4][0xa5] = 'R';
  trans[4][0xac] = 'D';
  trans[5][0x19] = 'E';
  trans[5][0x1f] = 'N';
  trans[5][0xa6] = 'L';
  trans[5][0xa9] = 'R';
  trans[5][0xc4] = 'E';
  trans[5][0xec] = 'A';
  trans[5][0xf0] = 'E';
  trans[5][0xf3] = 'H';
  trans[5][0xf9] = 'R';
  trans[6][0x46] = 'V';
  trans[6][0x55] = 'F';
  trans[6][0x5b] = 'K';
  trans[6][0x76] = 'L';
  trans[6][0x9e] = 'L';
  trans[6][0xa8] = 'S';
  trans[6][0xab] = 'E';
  trans[6][0xb6] = 'I';
  trans[6][0xbf] = 'M';
  trans[6][0xc0] = 'E';
  trans[6][0xd4] = 'S';
  trans[6][0xd8] = 'S';
  trans[7][0x0a] = 'S';
  trans[7][0x3a] = 'S';
  trans[7][0x57] = 'E';
  trans[7][0x5e] = 'S';
  trans[7][0x55] = 'N';
  trans[7][0x73] = 'R';
  trans[7][0x7a] = 'D';
  trans[7][0xd9] = 'E';
  trans[8][0x0f] = 'G';
  trans[8][0x99] = 'S';
  trans[8][0xea] = 'N';
  trans[9][0x9f] = 'T';
  trans[10][0x55] = 'S';

#ifdef SIMPLE
  trans[0][0xb7] = 'A';
  trans[0][0xbe] = 'B';
  trans[1][0x9b] = 'R';
  trans[1][0xa9] = 'S';
  trans[1][0xb6] = 'L';
  trans[1][0xba] = 'H';
  trans[1][0xbc] = 'A';
  trans[2][0x89] = 'D';
  trans[2][0x90] = 'V';
  trans[2][0x91] = 'I';
  trans[2][0x96] = 'I';
  trans[2][0x97] = 'G';
  trans[2][0x98] = 'R';
  trans[2][0x9b] = 'E';
  trans[2][0xad] = 'S';
  trans[2][0xb3] = 'S';
  trans[2][0xbe] = 'C';
  trans[3][0x90] = 'W';
  trans[3][0x97] = 'G';
  trans[3][0x98] = 'o'; /* also 'N' */
  trans[3][0x9e] = 'V';
  trans[3][0xac] = 'I';
  trans[3][0xad] = 'H';
  trans[3][0xb6] = 'D';
  trans[4][0x8d] = 'A';
  trans[4][0x93] = 'O';
  trans[4][0x9a] = 'G';
  trans[4][0xac] = 's'; /* also 'G' */
  trans[4][0xb7] = 'H';
  trans[5][0x94] = 'R';
  trans[5][0x93] = 'E';
  trans[5][0x99] = 'L';
  trans[5][0xac] = 'H';
  trans[6][0x9a] = 'V';
  trans[6][0xa9] = 'F';
  trans[6][0xac] = 'l'; /* and 'K'; */
  trans[7][0x8c] = 'E';
  trans[7][0xa8] = 'S';
  trans[7][0xbd] = 'S';
  trans[8][0xb8] = 'S';
#endif

  for (;;) {
    c = fgetc(f);
    if (c == EOF) break;
    c &= 255;
    freq[c] += 1;
    if (c == 1) {
      /* special marker */
      /* 01 d1 d1 d1 d1 be 02 */
      fprintf(stdout, "[%02x]", c);
      for (i = 0; i < 6; i++) {
        c = fgetc(f);
        fprintf(stdout, "[%02x]", c);
      }
      if (c != 2) {
        fprintf(stdout, "Assumption failed\n"); exit(1);
      }
      pos = 0; word[pos] = '\0';
      startletter += 1;
      fprintf(stdout, "\n******** NEXT LETTER %c ********\n", startletter);
    } else if (c <= 15) {
      /* trigger previous word */
      /* first two bytes are some sort of code? */

      fprintf(stdout, "%c", startletter);
      cypher = 0;
      for (i = 0; i < pos; i++) {
        cypher = (word[i] + cypher + i) & 255;
        fprintf(stdout, "%c", trans[i][cypher]);
        /*if (trans[i][cypher] == '?')*/ fprintf(stdout, "[%0d:%02x,%02x]", i, cypher, word[i]);
      }
      fprintf(stdout, " (then truncate to %d)\n", c-2);

      if (strlen(word) < c-2) {
        fprintf(stdout, "$$ Can't truncate - len is already %d\n", strlen(word));
      }

      pos = c-2;
      word[pos] = '\0'; /* and truncate to new size */
    } else if ((c >= 133) && (c < 160)) {
      /* normal letter */
      word[pos++] = c; word[pos] = '\0';
    } else if ((c >= 165) && (c <= 209)) {
      /* normal letter */
      word[pos++] = c; word[pos] = '\0';

#ifdef NEVER
      /* implicit end of word but can still be appended to */
      word[pos++] = c; word[pos] = '\0';
      if (pos >= 2) {
        for (i = 2; i < pos; i++) {
          if (trans[pos][word[i]] == '?') {
            fprintf(stdout, "[%02x]", word[i]);
	  } else {
            fprintf(stdout, "%c", trans[pos][word[i]]);
	  }
        }
      } else {
        /* what is this ? */
        fprintf(stdout, "@@@ %s\n", word);
      }
      fprintf(stdout, "\n");
#endif


    } else {
      fprintf(stdout, "UNREC %d\n", c);
      word[pos++] = c; word[pos] = '\0';
    }

  }
exit (0);
  for (i = 0; i < 256; i++) {
    if (freq[i] != 0) fprintf(stdout, "%d %d\n", freq[i], i);
  }
}
/*
AA[b7]
aa
AA[b7]H[ba]
aa    h
AA[b7]H[ba]E[9b]D[b6]
aa    h    e    d
AA[b7]H[ba]I[91]N[98]G[ac]
aa    h    i    n    g
AA[b7]H[ba]S[b3]
aa    h    s
AA[b7]D[b6]
aa    l
AA[b7]D[b6]I[96]G[ac]
aa    l    i    i
AA[b7]D[b6]I[96]G[ac]G[ac]
aa    l    i    i    s
AA[b7]D[b6]n[ad]
aa    l    s
AA[b7]E[9b]B[89]V[9e]A[8d]R[94]G[ac]
aa    r    d    v    a    r    k
AA[b7]E[9b]B[89]V[9e]A[8d]R[94]G[ac]S[a8]
aa    r    d    v    a    r    k    s
AA[b7]E[9b]B[89]T[90]W[93]S[99]b[a9]
aa    r    d    w    o    l    f
AA[b7]E[9b]B[89]T[90]W[93]S[99]L[9a]N[8c]p[b8]
aa    r    d    w    o    l    v    e    s
AA[b7]E[9b]P[97]n[ad]
aa    r    g    h
AA[b7]E[9b]N[98]P[97]A[b7]
aa    r    r    g    h
AA[b7]E[9b]N[98]P[97]A[b7]G[ac]
aa    r    r    g    g    h
AA[b7]b[a9]
aa    s
AA[b7]b[a9]T[90]N[98]L[9a]W[93]G[ac]
aa    s    v    o    g    e    l
AA[b7]b[a9]T[90]N[98]L[9a]W[93]G[ac]k[bd]
aa    s    v    o    g    e    l    s
Ac[be]
ab
Ac[be]h[bc]
ab    a
Ac[be]h[bc]c[be]G[ac]
ab    a    c    a
Ac[be]h[bc]c[be]G[ac]D[b6]
ab    a    c    a    s
Ac[be]h[bc]c[be]G[ac]
ab    a    c    i
Ac[be]h[bc]c[be]a[ab]
ab    a    c    k

 */
