1 // -*- Mode: C++; tab-width:2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi:tw=80:et:ts=2:sts=2
3 //
4 // -----------------------------------------------------------------------
5 //
6 // This file is part of libreallive, a dependency of RLVM.
7 //
8 // -----------------------------------------------------------------------
9 //
10 // Copyright (c) 2006, 2007 Peter Jolly
11 //
12 // Permission is hereby granted, free of charge, to any person
13 // obtaining a copy of this software and associated documentation
14 // files (the "Software"), to deal in the Software without
15 // restriction, including without limitation the rights to use, copy,
16 // modify, merge, publish, distribute, sublicense, and/or sell copies
17 // of the Software, and to permit persons to whom the Software is
18 // furnished to do so, subject to the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 // BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 // SOFTWARE.
31 //
32 // -----------------------------------------------------------------------
33 
34 #include "libreallive/compression.h"
35 
36 #include <string>
37 
38 namespace libreallive {
39 namespace compression {
40 
41 /* RealLive uses a rather basic XOR encryption scheme, to which this
42  * is the key. */
43 static char xor_mask[256] = {
44     0x8b, 0xe5, 0x5d, 0xc3, 0xa1, 0xe0, 0x30, 0x44, 0x00, 0x85, 0xc0, 0x74,
45     0x09, 0x5f, 0x5e, 0x33, 0xc0, 0x5b, 0x8b, 0xe5, 0x5d, 0xc3, 0x8b, 0x45,
46     0x0c, 0x85, 0xc0, 0x75, 0x14, 0x8b, 0x55, 0xec, 0x83, 0xc2, 0x20, 0x52,
47     0x6a, 0x00, 0xe8, 0xf5, 0x28, 0x01, 0x00, 0x83, 0xc4, 0x08, 0x89, 0x45,
48     0x0c, 0x8b, 0x45, 0xe4, 0x6a, 0x00, 0x6a, 0x00, 0x50, 0x53, 0xff, 0x15,
49     0x34, 0xb1, 0x43, 0x00, 0x8b, 0x45, 0x10, 0x85, 0xc0, 0x74, 0x05, 0x8b,
50     0x4d, 0xec, 0x89, 0x08, 0x8a, 0x45, 0xf0, 0x84, 0xc0, 0x75, 0x78, 0xa1,
51     0xe0, 0x30, 0x44, 0x00, 0x8b, 0x7d, 0xe8, 0x8b, 0x75, 0x0c, 0x85, 0xc0,
52     0x75, 0x44, 0x8b, 0x1d, 0xd0, 0xb0, 0x43, 0x00, 0x85, 0xff, 0x76, 0x37,
53     0x81, 0xff, 0x00, 0x00, 0x04, 0x00, 0x6a, 0x00, 0x76, 0x43, 0x8b, 0x45,
54     0xf8, 0x8d, 0x55, 0xfc, 0x52, 0x68, 0x00, 0x00, 0x04, 0x00, 0x56, 0x50,
55     0xff, 0x15, 0x2c, 0xb1, 0x43, 0x00, 0x6a, 0x05, 0xff, 0xd3, 0xa1, 0xe0,
56     0x30, 0x44, 0x00, 0x81, 0xef, 0x00, 0x00, 0x04, 0x00, 0x81, 0xc6, 0x00,
57     0x00, 0x04, 0x00, 0x85, 0xc0, 0x74, 0xc5, 0x8b, 0x5d, 0xf8, 0x53, 0xe8,
58     0xf4, 0xfb, 0xff, 0xff, 0x8b, 0x45, 0x0c, 0x83, 0xc4, 0x04, 0x5f, 0x5e,
59     0x5b, 0x8b, 0xe5, 0x5d, 0xc3, 0x8b, 0x55, 0xf8, 0x8d, 0x4d, 0xfc, 0x51,
60     0x57, 0x56, 0x52, 0xff, 0x15, 0x2c, 0xb1, 0x43, 0x00, 0xeb, 0xd8, 0x8b,
61     0x45, 0xe8, 0x83, 0xc0, 0x20, 0x50, 0x6a, 0x00, 0xe8, 0x47, 0x28, 0x01,
62     0x00, 0x8b, 0x7d, 0xe8, 0x89, 0x45, 0xf4, 0x8b, 0xf0, 0xa1, 0xe0, 0x30,
63     0x44, 0x00, 0x83, 0xc4, 0x08, 0x85, 0xc0, 0x75, 0x56, 0x8b, 0x1d, 0xd0,
64     0xb0, 0x43, 0x00, 0x85, 0xff, 0x76, 0x49, 0x81, 0xff, 0x00, 0x00, 0x04,
65     0x00, 0x6a, 0x00, 0x76};
66 
67 /* In some new titles, a second round of XORing is performed on a block of
68  * uncompressed bytecode. The keys appear to be on a game-by-game basis. */
69 const XorKey little_busters_xor_mask[] = {
70     {{0xa8, 0x28, 0xfd, 0x66, 0xa0, 0x23, 0x77, 0x69, 0xf9, 0x45, 0xf8, 0x2c,
71       0x7c, 0x00, 0xad, 0xf4},
72      256, 257},
73     {{0x0}, -1, -1}};
74 
75 const XorKey little_busters_ex_xor_mask[] = {
76     {{0xa8, 0x28, 0xfd, 0x71, 0xb4, 0x23, 0x64, 0x15, 0x96, 0x48, 0x8a, 0x43,
77       0x62, 0x0e, 0xad, 0xf0},
78      256, 128},
79     {{0xde, 0xd9, 0x4a, 0x18, 0xaf, 0x23, 0x1d, 0x9a, 0xac, 0x23, 0x25, 0x48,
80       0xd8, 0xd4, 0x8f, 0xa7},
81      384, 128},
82     {{0xde, 0xf1, 0xb7, 0x69, 0x1b, 0x00, 0x79, 0x8f, 0x3a, 0x6b, 0xaf, 0x0b,
83       0xba, 0xda, 0x22, 0x57},
84      512, 16},
85     {{0x76, 0xf1, 0xb7, 0x69, 0x1b, 0x00, 0x79, 0x8f, 0x3a, 0x6b, 0xaf, 0x0b,
86       0xba, 0xda, 0x22, 0x57},
87      528, 113},
88     {{0x0}, -1, -1}};
89 
90 const XorKey clannad_full_voice_xor_mask[] = {
91     {{0xAF, 0x2F, 0xFB, 0x6B, 0xAF, 0x30, 0x77, 0x17, 0x87, 0x48, 0xFE, 0x2C,
92       0x68, 0x1A, 0xB9, 0xF0},
93      256, 257},
94     {{0x0}, -1, -1}};
95 
96 const XorKey snow_standard_edition_xor_mask[] = {
97     {{0xe4, 0xab, 0xa2, 0xc9, 0xec, 0x39, 0x36, 0x62, 0xc9, 0x03, 0xba, 0x6d,
98       0x2e, 0x9c, 0xf2, 0x64},
99      256, 257},
100     {{0x0}, -1, -1}};
101 
102 const XorKey kud_wafter_xor_mask[] = {
103     {{0x67, 0x1c, 0x21, 0xbe, 0x6f, 0xef, 0xb5, 0x16, 0x4a, 0x82, 0x39, 0x2b,
104       0xad, 0x3a, 0x71, 0x3f},
105      256, 257},
106     {{0x0}, -1, -1}};
107 
108 const XorKey kud_wafter_all_ages_xor_mask[] = {
109     {{0xaf, 0x3f, 0xe6, 0x63, 0xad, 0x3a, 0x69, 0x18, 0x85, 0x45, 0xe5, 0x40,
110       0x1e, 0x7e, 0xb9, 0xe0},
111      256, 257},
112     {{0x0}, -1, -1}};
113 
114 
115 // -----------------------------------------------------------------------
116 
117 // Decompress an archived file.
Decompress(const char * src,size_t src_len,char * dst,size_t dst_len,const XorKey * per_game_xor_key)118 void Decompress(const char* src,
119                 size_t src_len,
120                 char* dst,
121                 size_t dst_len,
122                 const XorKey* per_game_xor_key) {
123   int bit = 1;
124   const char* srcend = src + src_len;
125   char* dststart = dst;
126   char* dstend = dst + dst_len;
127   src += 8;
128   unsigned char mask = 8;
129   char flag = *src++ ^ xor_mask[mask++];
130   while (src < srcend && dst < dstend) {
131     if (bit == 256) {
132       bit = 1;
133       flag = *src++ ^ xor_mask[mask++];
134     }
135     if (flag & bit) {
136       *dst++ = *src++ ^ xor_mask[mask++];
137     } else {
138       char* repeat;
139       int count = *src++ ^ xor_mask[mask++];
140       count += (*src++ ^ xor_mask[mask++]) << 8;
141       repeat = dst - ((count >> 4) - 1) - 1;
142       count = (count & 0x0f) + 2;
143       if (repeat < dststart || repeat >= dst)
144         throw Error("corrupt data");
145       for (int i = 0; i < count; i++)
146         *dst++ = *repeat++;
147     }
148     bit <<= 1;
149   }
150 
151   if (per_game_xor_key) {
152     for (; per_game_xor_key->xor_offset != -1; per_game_xor_key++) {
153       dst = dststart + per_game_xor_key->xor_offset;
154       for (int i = 0; i < per_game_xor_key->xor_length && dst < dstend; ++i) {
155         *dst++ ^= per_game_xor_key->xor_key[i % 16];
156       }
157     }
158   }
159 }
160 
161 }  // namespace compression
162 }  // namespace libreallive
163