| 1 |
/* |
| 2 |
Copyright (c) 2009 Peter "Corsix" Cawley |
| 3 |
|
| 4 |
Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 5 |
this software and associated documentation files (the "Software"), to deal in |
| 6 |
the Software without restriction, including without limitation the rights to |
| 7 |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 8 |
of the Software, and to permit persons to whom the Software is furnished to do |
| 9 |
so, subject to the following conditions: |
| 10 |
|
| 11 |
The above copyright notice and this permission notice shall be included in all |
| 12 |
copies or substantial portions of the Software. |
| 13 |
|
| 14 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 |
SOFTWARE. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "th.h" |
| 24 |
|
| 25 |
#include "config.h" |
| 26 |
|
| 27 |
#include <array> |
| 28 |
#include <stdexcept> |
| 29 |
|
| 30 |
#include "cp437_table.h" |
| 31 |
#include "cp936_table.h" |
| 32 |
#include "cpmik_table.h" |
| 33 |
|
| 34 |
link_list::link_list() = default; |
| 35 |
|
| 36 |
link_list::~link_list() { remove_from_list(); } |
| 37 |
|
| 38 |
void link_list::remove_from_list() { |
| 39 |
if (prev != nullptr) { |
| 40 |
prev->next = next; |
| 41 |
} |
| 42 |
if (next != nullptr) { |
| 43 |
next->prev = prev; |
| 44 |
next = nullptr; |
| 45 |
} |
| 46 |
prev = nullptr; |
| 47 |
} |
| 48 |
|
| 49 |
namespace { |
| 50 |
|
| 51 |
void utf8encode(uint8_t*& sOut, uint32_t iCodepoint) { |
| 52 |
if (iCodepoint <= 0x7F) { |
| 53 |
*sOut = static_cast<char>(iCodepoint); |
| 54 |
++sOut; |
| 55 |
} else if (iCodepoint <= 0x7FF) { |
| 56 |
uint8_t cSextet = iCodepoint & 0x3F; |
| 57 |
iCodepoint >>= 6; |
| 58 |
sOut[0] = static_cast<uint8_t>(0xC0 + iCodepoint); |
| 59 |
sOut[1] = static_cast<uint8_t>(0x80 + cSextet); |
| 60 |
sOut += 2; |
| 61 |
} else if (iCodepoint <= 0xFFFF) { |
| 62 |
uint8_t cSextet2 = iCodepoint & 0x3F; |
| 63 |
iCodepoint >>= 6; |
| 64 |
uint8_t cSextet1 = iCodepoint & 0x3F; |
| 65 |
iCodepoint >>= 6; |
| 66 |
sOut[0] = static_cast<uint8_t>(0xE0 + iCodepoint); |
| 67 |
sOut[1] = static_cast<uint8_t>(0x80 + cSextet1); |
| 68 |
sOut[2] = static_cast<uint8_t>(0x80 + cSextet2); |
| 69 |
sOut += 3; |
| 70 |
} else { |
| 71 |
uint8_t cSextet3 = iCodepoint & 0x3F; |
| 72 |
iCodepoint >>= 6; |
| 73 |
uint8_t cSextet2 = iCodepoint & 0x3F; |
| 74 |
iCodepoint >>= 6; |
| 75 |
uint8_t cSextet1 = iCodepoint & 0x3F; |
| 76 |
iCodepoint >>= 6; |
| 77 |
sOut[0] = static_cast<uint8_t>(0xF0 + iCodepoint); |
| 78 |
sOut[1] = static_cast<uint8_t>(0x80 + cSextet1); |
| 79 |
sOut[2] = static_cast<uint8_t>(0x80 + cSextet2); |
| 80 |
sOut[3] = static_cast<uint8_t>(0x80 + cSextet3); |
| 81 |
sOut += 4; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
void CopyStringCP437(const uint8_t*& sIn, uint8_t*& sOut) { |
| 86 |
uint8_t cChar; |
| 87 |
do { |
| 88 |
cChar = *sIn; |
| 89 |
++sIn; |
| 90 |
if (cChar < 0x80) { |
| 91 |
*sOut = cChar; |
| 92 |
++sOut; |
| 93 |
} else { |
| 94 |
utf8encode(sOut, cp437_to_unicode_table[cChar - 0x80]); |
| 95 |
} |
| 96 |
} while (cChar != 0); |
| 97 |
} |
| 98 |
|
| 99 |
void CopyStringMIK(const uint8_t*& sIn, uint8_t*& sOut) { |
| 100 |
uint8_t cChar; |
| 101 |
do { |
| 102 |
cChar = *sIn; |
| 103 |
++sIn; |
| 104 |
if (cChar < 0x80) { |
| 105 |
*sOut = cChar; |
| 106 |
++sOut; |
| 107 |
} else { |
| 108 |
utf8encode(sOut, cpmik_to_unicode_table[cChar - 0x80]); |
| 109 |
} |
| 110 |
} while (cChar != 0); |
| 111 |
} |
| 112 |
|
| 113 |
void CopyStringCP936(const uint8_t*& sIn, uint8_t*& sOut) { |
| 114 |
uint8_t cChar1, cChar2; |
| 115 |
do { |
| 116 |
cChar1 = *sIn; |
| 117 |
++sIn; |
| 118 |
if (cChar1 < 0x81 || cChar1 == 0xFF) { |
| 119 |
*sOut = cChar1; |
| 120 |
++sOut; |
| 121 |
} else { |
| 122 |
cChar2 = *sIn; |
| 123 |
++sIn; |
| 124 |
if (0x40 <= cChar2 && cChar2 <= 0xFE) { |
| 125 |
utf8encode(sOut, cp936_to_unicode_table[cChar1 - 0x81][cChar2 - 0x40]); |
| 126 |
// The Theme Hospital string tables seem to like following a |
| 127 |
// multibyte character with a superfluous space. |
| 128 |
cChar2 = *sIn; |
| 129 |
if (cChar2 == ' ') ++sIn; |
| 130 |
} else { |
| 131 |
*sOut = cChar1; |
| 132 |
++sOut; |
| 133 |
*sOut = cChar2; |
| 134 |
++sOut; |
| 135 |
} |
| 136 |
} |
| 137 |
} while (cChar1 != 0); |
| 138 |
} |
| 139 |
|
| 140 |
enum class encoding { cp437, cp936, mik }; |
| 141 |
|
| 142 |
encoding detect_encoding(const uint8_t* data, size_t length) { |
| 143 |
const uint8_t mik_prefix[] = {0x8C, 0xA5, 0xA4, 0xB1, 0xA5}; |
| 144 |
if (length >= sizeof(mik_prefix) && |
| 145 |
std::equal(mik_prefix, mik_prefix + sizeof(mik_prefix), data)) { |
| 146 |
// The MIK encoding is used in the Russian version of the game, and has a |
| 147 |
// fixed length of 0x1619D bytes. |
| 148 |
return encoding::mik; |
| 149 |
} |
| 150 |
|
| 151 |
// The range of bytes 0xB0 through 0xDF are box drawing characters in CP437 |
| 152 |
// which shouldn't occur much (if ever) in TH strings, whereas they are |
| 153 |
// commonly used in GB2312 encoding. We use 10% as a threshold. |
| 154 |
size_t iBCDCount = 0; |
| 155 |
for (size_t i = 0; i < length; ++i) { |
| 156 |
if (0xB0 <= data[i] && data[i] <= 0xDF) ++iBCDCount; |
| 157 |
} |
| 158 |
if (iBCDCount * 10 >= length) |
| 159 |
return encoding::cp936; |
| 160 |
else |
| 161 |
return encoding::cp437; |
| 162 |
} |
| 163 |
|
| 164 |
} // namespace |
| 165 |
|
| 166 |
// https://github.com/CorsixTH/theme-hospital-spec/blob/master/format-specification.md#strings |
| 167 |
th_string_list::th_string_list(const uint8_t* data, size_t length) { |
| 168 |
if (length < 2) throw std::invalid_argument("length must be 2 or larger"); |
| 169 |
|
| 170 |
size_t iSectionCount = bytes_to_uint16_le(data); |
| 171 |
size_t iHeaderLength = (iSectionCount + 1) * 2; |
| 172 |
|
| 173 |
if (length < iHeaderLength) |
| 174 |
throw std::invalid_argument("iDataLength must be larger than the header"); |
| 175 |
|
| 176 |
size_t iStringDataLength = length - iHeaderLength; |
| 177 |
const uint8_t* sStringData = data + iHeaderLength; |
| 178 |
const uint8_t* sDataEnd = sStringData + iStringDataLength; |
| 179 |
|
| 180 |
void (*fnCopyString)(const uint8_t*&, uint8_t*&); |
| 181 |
switch (detect_encoding(sStringData, iStringDataLength)) { |
| 182 |
case encoding::cp437: |
| 183 |
fnCopyString = CopyStringCP437; |
| 184 |
break; |
| 185 |
case encoding::cp936: |
| 186 |
fnCopyString = CopyStringCP936; |
| 187 |
break; |
| 188 |
case encoding::mik: |
| 189 |
fnCopyString = CopyStringMIK; |
| 190 |
} |
| 191 |
|
| 192 |
// String buffer sized to accept the largest possible reencoding of the |
| 193 |
// characters interpreted as CP936 or CP437 (2 bytes per character). |
| 194 |
string_buffer.resize(iStringDataLength * 2 + 2); |
| 195 |
|
| 196 |
uint8_t* sDataOut = string_buffer.data(); |
| 197 |
sections.resize(iSectionCount); |
| 198 |
for (size_t i = 0; i < iSectionCount; ++i) { |
| 199 |
// The section sizes start at offset 2 in the data, after the section count. |
| 200 |
// Each section size is 2 bytes. |
| 201 |
size_t sectionSizeOffset = (i + 1) * 2; |
| 202 |
size_t sectionSize = bytes_to_uint16_le(data + sectionSizeOffset); |
| 203 |
|
| 204 |
// Read the strings for the section. |
| 205 |
// |
| 206 |
// All of the strings get stored in string_buffer which sDataOut points |
| 207 |
// into. The sections vectors get filled with the pointers to the start of |
| 208 |
// each string in string_buffer. |
| 209 |
sections[i].reserve(sectionSize); |
| 210 |
for (size_t j = 0; j < sectionSize; ++j) { |
| 211 |
sections[i].push_back(reinterpret_cast<char*>(sDataOut)); |
| 212 |
if (sStringData != sDataEnd) { |
| 213 |
fnCopyString(sStringData, sDataOut); |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
// Terminate final string with nil character |
| 218 |
*sDataOut = 0; |
| 219 |
} |
| 220 |
|
| 221 |
size_t th_string_list::get_section_count() const { return sections.size(); } |
| 222 |
|
| 223 |
size_t th_string_list::get_section_size(size_t section) const { |
| 224 |
return section < sections.size() ? sections[section].size() : 0; |
| 225 |
} |
| 226 |
|
| 227 |
const char* th_string_list::get_string(size_t section, size_t index) const { |
| 228 |
if (index < get_section_size(section)) { |
| 229 |
return sections[section][index]; |
| 230 |
} |
| 231 |
return nullptr; |
| 232 |
} |