ScummVM API documentation
script_patches.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef SCI_ENGINE_SCRIPT_PATCHES_H
23 #define SCI_ENGINE_SCRIPT_PATCHES_H
24 
25 #include "sci/sci.h"
26 
27 namespace Sci {
28 
29 // Please do not use the #defines, that are called SIG_CODE_* / PATCH_CODE_* inside signature/patch-tables
30 #define SIG_END 0xFFFF
31 #define SIG_MISMATCH 0xFFFE
32 #define SIG_COMMANDMASK 0xF000
33 #define SIG_VALUEMASK 0x0FFF
34 #define SIG_BYTEMASK 0x00FF
35 #define SIG_MAGICDWORD 0xF000
36 #define SIG_CODE_ADDTOOFFSET 0xE000
37 #define SIG_ADDTOOFFSET(_offset_) SIG_CODE_ADDTOOFFSET | (_offset_)
38 #define SIG_CODE_SELECTOR16 0x9000
39 #define SIG_SELECTOR16(_selectorID_) SIG_CODE_SELECTOR16 | SELECTOR_##_selectorID_
40 #define SIG_CODE_SELECTOR8 0x8000
41 #define SIG_SELECTOR8(_selectorID_) SIG_CODE_SELECTOR8 | SELECTOR_##_selectorID_
42 #define SIG_CODE_UINT16 0x1000
43 #define SIG_UINT16(_value_) SIG_CODE_UINT16 | ((_value_) & 0xFF), ((_value_) >> 8)
44 #define SIG_CODE_BYTE 0x0000
45 
46 #define PATCH_END SIG_END
47 #define PATCH_COMMANDMASK SIG_COMMANDMASK
48 #define PATCH_VALUEMASK SIG_VALUEMASK
49 #define PATCH_BYTEMASK SIG_BYTEMASK
50 #define PATCH_CODE_ADDTOOFFSET SIG_CODE_ADDTOOFFSET
51 #define PATCH_ADDTOOFFSET(_offset_) SIG_CODE_ADDTOOFFSET | (_offset_)
52 #define PATCH_CODE_GETORIGINALBYTES 0xB000
53 #define PATCH_GETORIGINALBYTES(_offset_, _length_) PATCH_CODE_GETORIGINALBYTES | (_offset_), (uint16)(_length_)
54 #define PATCH_CODE_GETORIGINALBYTE 0xC000
55 #define PATCH_GETORIGINALBYTE(_offset_) PATCH_CODE_GETORIGINALBYTE | (_offset_), 0
56 #define PATCH_GETORIGINALBYTEADJUST(_offset_, _adjustValue_) PATCH_CODE_GETORIGINALBYTE | (_offset_), (uint16)(_adjustValue_)
57 #define PATCH_CODE_GETORIGINALUINT16 0xD000
58 #define PATCH_GETORIGINALUINT16(_offset_) PATCH_CODE_GETORIGINALUINT16 | (_offset_), 0
59 #define PATCH_GETORIGINALUINT16ADJUST(_offset_, _adjustValue_) PATCH_CODE_GETORIGINALUINT16 | (_offset_), (uint16)(_adjustValue_)
60 #define PATCH_CODE_SELECTOR16 SIG_CODE_SELECTOR16
61 #define PATCH_SELECTOR16(_selectorID_) SIG_CODE_SELECTOR16 | SELECTOR_##_selectorID_
62 #define PATCH_CODE_SELECTOR8 SIG_CODE_SELECTOR8
63 #define PATCH_SELECTOR8(_selectorID_) SIG_CODE_SELECTOR8 | SELECTOR_##_selectorID_
64 #define PATCH_CODE_UINT16 SIG_CODE_UINT16
65 #define PATCH_UINT16(_value_) SIG_CODE_UINT16 | ((_value_) & 0xFF), ((_value_) >> 8)
66 #define PATCH_CODE_BYTE SIG_CODE_BYTE
67 
68 // defines maximum scratch area for getting original bytes from unpatched script data
69 #define PATCH_VALUELIMIT 4096
70 
72  bool defaultActive;
73  uint16 scriptNr;
74  const char *description;
75  int16 applyCount;
76  const uint16 *signatureData;
77  const uint16 *patchData;
78 };
79 
80 #define SCI_SIGNATUREENTRY_TERMINATOR { false, 0, NULL, 0, NULL, NULL }
81 
83  bool active;
84  uint32 magicDWord;
85  int magicOffset;
86 };
87 
92 public:
93  ScriptPatcher();
94  ~ScriptPatcher();
95 
96  // Calculates the magic DWord for fast search and verifies signature/patch data
97  // Returns the magic DWord in platform-specific byte-order. This is done on purpose for performance.
98  void calculateMagicDWordAndVerify(const char *signatureDescription, const uint16 *signatureData, bool magicDWordIncluded, uint32 &calculatedMagicDWord, int &calculatedMagicDWordOffset);
99 
100  // Called when a script is loaded to check for signature matches and apply patches in such cases
101  void processScript(uint16 scriptNr, SciSpan<byte> scriptData);
102 
103  // Verifies, if a given signature matches the given script data (pointed to by additional byte offset)
104  bool verifySignature(uint32 byteOffset, const uint16 *signatureData, const char *signatureDescription, const SciSpan<const byte> &scriptData);
105 
106  // searches for a given signature inside script data
107  // returns -1 in case it was not found or an offset to the matching data
108  int32 findSignature(uint32 magicDWord, int magicOffset, const uint16 *signatureData, const char *patchDescription, const SciSpan<const byte> &scriptData);
109 
110 private:
111  // Initializes a patch table and creates run time information for it (for enabling/disabling), also calculates magic DWORD)
112  void initSignature(const SciScriptPatcherEntry *patchTable);
113 
114  // Enables a patch inside the patch table (used for optional patches like CD+Text support for KQ6 & LB2)
115  void enablePatch(const SciScriptPatcherEntry *patchTable, const char *searchDescription);
116 
117  // Searches for a given signature entry inside script data
118  // returns -1 in case it was not found or an offset to the matching data
119  int32 findSignature(const SciScriptPatcherEntry *patchEntry, const SciScriptPatcherRuntimeEntry *runtimeEntry, const SciSpan<const byte> &scriptData);
120 
121  // Applies a patch to a given script + offset (overwrites parts)
122  void applyPatch(const SciScriptPatcherEntry *patchEntry, SciSpan<byte> scriptData, int32 signatureOffset);
123 
124  Selector *_selectorIdTable;
125  SciScriptPatcherRuntimeEntry *_runtimeTable;
126  bool _isMacSci11;
127 };
128 
129 } // End of namespace Sci
130 
131 #endif // SCI_ENGINE_WORKAROUNDS_H
Definition: script_patches.h:82
Definition: console.h:28
Definition: script_patches.h:91
Definition: script_patches.h:71