Bytecode. Command length built into messages.

   1 #include <stdio.h>
   2 #include <stdlib.h>
   3 #include <stdint.h>
   4 
   5 #include <deque>
   6 
   7 bool flag = false;
   8 
   9 void fread_or_die(void * a, size_t b, int c, FILE * d)
  10 {
  11     int got = fread(a, b, c, d);
  12     if(feof(d) || ferror(d) || got != c) flag = true;
  13 }
  14 
  15 int main(int argc, char ** argv)
  16 {
  17     if(argc < 2) return puts("Need argument"), 0;
  18     for(int arg = 1; arg < argc; arg++)
  19     {
  20         auto fs = fopen(argv[arg], "rb");
  21         
  22         fseek(fs, 0x114, SEEK_SET);
  23         
  24         while(!feof(fs) and !ferror(fs) and !flag)
  25         {
  26             uint16_t command;
  27             uint8_t ints;
  28             uint8_t other;
  29             fread_or_die(&command, 2, 1, fs);
  30             fread_or_die(&ints, 1, 1, fs);
  31             fread_or_die(&other, 1, 1, fs);
  32             uint8_t strings = other & 0xF;
  33             
  34             if(flag) continue;
  35             
  36             //printf("%04X %02X %02X\n", command, ints, other);
  37             
  38             fseek(fs, 4*ints, SEEK_CUR);
  39             
  40             for(int i = 0; i < strings; i++)
  41             {
  42                 while(true)
  43                 {
  44                     char c = fgetc(fs);  
  45                     if(c == 0x0D) continue;
  46                     if(command == 0x001C and i == 1)
  47                     {
  48                         if(c=='\n'); // don't retain line wraps
  49                         else if(c!=0) putc(c, stdout);
  50                         else putc('\n', stdout); // make newlines between messages
  51                     }
  52                     if(c==0) break;
  53                 }
  54             }
  55         }
  56         
  57         fclose(fs);
  58         flag = false;
  59     }
  60 }

For Primary by SkyFish, which uses xor-table encryption:

   1 #include <stdio.h>
   2 #include <stdlib.h>
   3 #include <stdint.h>
   4 
   5 #include <string>
   6 
   7 bool flag = false;
   8 
   9 unsigned char key2 = 0x6E;
  10 
  11 unsigned char key[] = 
  12 {
  13 0xEF,0xC3,0x2B,0xED,0x9B,0x93,0xC6,0xEE,0xCF,0xE1,0xAD,0xDF,0x7D,0xCB,0x42,0xCF,
  14 0x44,0x88,0xDF,0xBA,0x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31,0xDF,0x09,0x1A,0x57,0x69,0xD9,0xF3,0xFC,0x6A,0x7D,0xDA,
  41 0x28,0xD5,0x69,0x97,0x7C,0xB7,0x3A,0x4B,0x02,0x8C,0x0F,0x49,0xA0,0x2E,0xBC,0x74,
  42 0x26,0xEB,0x9A,0x94,0x11,0xAA,0xA4,0x0D,0xEB,0x33,0xD4,0xAE,0xB0,0x51,0x54,0xE1,
  43 0x46,0x4F,0xA1,0xE9,0xF9,0xAC,0x2E,0x08,0xDC,0x46,0x10,0x4B,0x91,0xBA,0xD6,0x4A,
  44 0x4A,0x13,0x4B,0xC8,0xA8,0xD3,0x59,0x62,0x0A,0xE0,0xCF,0x6D,0x85,0xD0,0xE5,0x91,
  45 0x1B,0xD9,0x1F,0xFA,0xD8,0xF4,0x13,0x1C,0x52,0x05,0x44,0xF0,0x2C,0xA3,0x72,0x7B,
  46 0x34,0x91,0x00,0x3E,0x98,0x87,0xA3,0xB2,0x18,0xEB,0x25,0xA3,0xED,0x04,0xC8,0x65,
  47 0x28,0xCA,0xE6,0xFE,0x0D,0x2A,0x68,0x7C,0x6F,0x1F,0xC6,0x95,0x2F,0x09,0x11,0xD9,
  48 0xB7,0xD6,0xB4,0x37,0x0F,0x67,0xD1,0x37,0x07,0x40,0xAD,0xA6,0x00,0x60,0x7F,0x65,
  49 0x64,0x7B,0x30,0xA7,0xA5,0xC4,0x28,0x67,0x80,0x70,0x9A,0xC6,0x9C,0x7B,0xE3,0x97,
  50 0xCC,0xC8,0x87,0x5A,0x6A,0x2C,0x2A,0x71,0x0B,0x3A,0xE4,0x84,0x4B,0xEA,0x6B,0xE8,
  51 0x2E,0x00,0x11,0x66,0xB1,0xD1,0x88,0x4F,0x12,0xCF,0x8D,0x50,0x67,0x7E,0xD2,0xC8,
  52 0x7E,0x74,0x14,0x82,0x33,0x7A,0x27,0x41,0xAB,0x21,0x7A,0x42,0x3A,0x82,0x2A,0xFD,
  53 0x4E,0x79,0x8A,0x8F,0x14,0xE6,0xF9,0x8B,0x49,0x78,0x7A,0xD7,0xB3,0x83,0x48,0x15,
  54 0xB5,0xC7,0xD2,0xB9,0xAA,0xAD,0xB0,0x5E,0xC8,0xE1,0x51,0x56,0x46,0xA4,0xD5,0xFB,
  55 0x79,0x9C,0xC6,0x01,0x05,0x93,0x2A,0x40,0x34,0x66,0x7E,0xA5,0x36,0x94,0xD5,0x57,
  56 0xF5,0x41,0x89,0xD1,0xF9,0x33,0xDB,0xEF,0x5C,0x1C,0x2A,0x72,0x6D,0xBC,0x6B,0xFB,
  57 0x3A,0xB5,0xF6,0x70,0xAE,0x99,0x09,0xF8,0x59,0xB6,0x96,0x39,0x5C,0xC4,0xEC,0x81,
  58 0x58,0x94,0x09,0x0F,0x77,0xF3,0x8B,0x2D,0xBC,0xFF,0x83,0x05,0x72,0x7D,0x8A,0x43,
  59 0x6C,0x58,0x18,0xBF,0xD0,0x97,0x5E,0xF7,0xCB,0xAB,0xA9,0x4C,0x6D,0xF9,0x51,0x00,
  60 0x60,0x77,0xA2,0x61,0xFA,0xBF,0x56,0xDC,0x3F,0x62,0x2E,0x18,0x5D,0x3B,0xC0,0xB6,
  61 0xCC,0xF6,0x01,0xB2,0x34,0x12,0x45,0x9F,0xD7,0xFB,0xDF,0xD7,0xA1,0x17,0xDF,0xF1,
  62 0x60,0xC1,0x17,0x02,0xD3,0x63,0xEC,0xAE,0x2A,0x8A,0x81,0x4F,0x85,0x0B,0x5F,0x45,
  63 0x79,0x24,0xC6,0xCB,0x1A,0xF1,0xDA,0x42,0x3E,0xFA,0xC2,0x0D,0x54,0x1C,0x39,0x4A,
  64 0xFD,0x99,0x40,0xF1,0xCA,0x2F,0xCB,0xF4,0xA5,0xB6,0x93,0x90,0x8A,0xCD,0x1E,0x5C,
  65 0x1A,0x02,0x58,0x3F,0x66,0x4A,0x3F,0x3F,0xF7,0x70,0xEA,0xBA,0x95,0x7C,0x43,0x7C,
  66 0x9F,0x26,0xF0,0x0A,0x2B,0xFC,0xB9,0xEA,0x66,0x84,0x24,0x96,0x50,0x65,0xC1,0xF2,
  67 0x4F,0x46,0x3B,0x5C,0xE8,0xE9,0xFF,0x92,0x8B,0xB0,0x62,0x09,0xC6,0x6F,0xE9,0x50,
  68 0x60,0x88,0x51,0xD0,0x01,0x5C,0xAF,0x6F,0x28,0xFC,0x63,0x9E,0x3C,0x0F,0x63,0x65,
  69 0x21,0xEF,0x23,0xFB,0xBF,0x9E,0x87,0x91,0xE9,0x20,0x5E,0x3D,0x42,0x4F,0x43,0x94,
  70 0x38,0xFD,0xC1,0xC8,0x1E,0xBF,0x13,0x4E,0xD5,0x05,0x71,0x59,0x75,0x9F,0x18,0x6F,
  71 0x42,0xCA,0x1B,0xE3,0xA5,0xA4,0x29,0x2F,0x83,0xBE,0xB6,0x4E,0xCC,0x15,0xDF,0xE9,
  72 0x12,0x2C,0x1B,0x2E,0xCD,0xF8,0x74,0xC8,0xDF,0x8B,0x17,0x50,0x9C,0x61,0xA6,0x95,
  73 0xB6,0xFC,0xC5,0x9B,0x56,0x02,0x1B,0xF9,0xD1,0x53,0x4A,0xE7,0x2F,0xD7,0x21,0xFC,
  74 0x58,0x5E,0x81,0xB4,0xA3,0xD3,0x4D,0x9B,0xD3,0xC1,0xA6,0x96,0xDB,0xB8,0xB5,0x5D,
  75 0x37,0x7A,0xA4,0x36,0xF7,0xE0,0x2C,0x25,0x57,0x6E,0x27,0x9D,0xCF,0xB4,0x2E,0x46,
  76 0x7D,0xF1,0x7A,0x2C,0xD1,0x05,0x9B,0x5A,0x27,0x3C,0x18,0x56,0x29,0x1D,0x54,0x6D
  77 };
  78 
  79 void decrypt(unsigned char * buffer, size_t len)
  80 {
  81     for(size_t counter = 0; counter < len and counter < 0xFFC0; counter++)
  82         buffer[counter] ^= key[counter%0x400] ^ key2;
  83 }
  84 
  85 int main(int argc, char ** argv)
  86 {
  87     if(argc < 2) return puts("Need argument"), 0;
  88     for(int arg = 1; arg < argc; arg++)
  89     {
  90         auto fs = fopen(argv[arg], "rb");
  91         
  92         fseek(fs, 0, SEEK_END);
  93         auto end = ftell(fs);
  94         fseek(fs, 0, SEEK_SET);
  95         
  96         auto buffer = (unsigned char *)malloc(end);
  97         fread(buffer, 1, end, fs);
  98         
  99         fclose(fs);
 100         
 101         if(end < 0x10)
 102             continue;
 103         
 104         decrypt(buffer+0x10, end-0x10);
 105         
 106         auto copy = fopen((std::string(argv[arg])+".bin").data(), "wb");
 107         fwrite(buffer, 1, end, copy);
 108         fclose(copy);
 109         
 110         size_t cursor = 0x114;
 111         
 112         while(!feof(fs) and !ferror(fs) and !flag)
 113         {
 114             uint16_t command;
 115             uint8_t ints;
 116             uint8_t other;
 117             if(cursor + 4 > end)
 118                 break;
 119             
 120             command  = buffer[cursor++];
 121             command |= (unsigned int)(buffer[cursor++])<<8;
 122             
 123             ints  = buffer[cursor++];
 124             other = buffer[cursor++];
 125             uint8_t strings = other & 0xF;
 126             
 127             bool dialogue = false;
 128             if(command == 0x001C)
 129             {
 130                 uint32_t firstint = 0;
 131                 firstint |= (unsigned int)(buffer[cursor++])<<24;
 132                 firstint >>= 8;
 133                 firstint |= (unsigned int)(buffer[cursor++])<<24;
 134                 firstint >>= 8;
 135                 firstint |= (unsigned int)(buffer[cursor++])<<24;
 136                 firstint >>= 8;
 137                 firstint |= (unsigned int)(buffer[cursor++])<<24;
 138                 
 139                 dialogue = firstint != 0x00000001;
 140                 
 141                 cursor += 4*(ints-1);
 142             }
 143             else
 144                 cursor += 4*ints;
 145             
 146             if(dialogue)
 147             {
 148                 putc('\x81', stdout);
 149                 putc('\x75', stdout);
 150             }
 151             for(int i = 0; i < strings; i++)
 152             {
 153                 if(cursor > end)
 154                     break;
 155                 while(true)
 156                 {
 157                     char c = buffer[cursor++];
 158                     if(c == 0x0D) continue;
 159                     if(command == 0x001C and i == 1)
 160                     {
 161                         if(c=='\n'); // don't retain line wraps
 162                         else if(c!=0) putc(c, stdout);
 163                         else if(!dialogue) putc('\n', stdout); // make newlines between messages
 164                     }
 165                     if(c==0) break;
 166                     if(cursor > end)
 167                         break;
 168                 }
 169             }
 170             if(dialogue)
 171             {
 172                 putc('\x81', stdout);
 173                 putc('\x76', stdout);
 174                 putc('\n', stdout);
 175             }
 176         }
 177         
 178         fclose(fs);
 179         flag = false;
 180     }
 181 }

ExHIBIT (last edited 2018-07-22 13:25:01 by weh)