| 1 | // Copyright (c) 2014-2022, The Monero Project |
| 2 | // |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without modification, are |
| 6 | // permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 9 | // conditions and the following disclaimer. |
| 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 12 | // of conditions and the following disclaimer in the documentation and/or other |
| 13 | // materials provided with the distribution. |
| 14 | // |
| 15 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 16 | // used to endorse or promote products derived from this software without specific |
| 17 | // prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 27 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <ctype.h> |
| 32 | #include <string> |
| 33 | #include <map> |
| 34 | #include "file_io_utils.h" |
| 35 | #include "common/i18n.h" |
| 36 | #include "translation_files.h" |
| 37 | |
| 38 | #include <boost/system/error_code.hpp> |
| 39 | #include <boost/filesystem.hpp> |
| 40 | #include <algorithm> |
| 41 | |
| 42 | #undef MONERO_DEFAULT_LOG_CATEGORY |
| 43 | #define MONERO_DEFAULT_LOG_CATEGORY "i18n" |
| 44 | |
| 45 | #define MAX_LANGUAGE_SIZE 16 |
| 46 | |
| 47 | static const unsigned char qm_magic[16] = {0x3c, 0xb8, 0x64, 0x18, 0xca, 0xef, 0x9c, 0x95, 0xcd, 0x21, 0x1c, 0xbf, 0x60, 0xa1, 0xbd, 0xdd}; |
| 48 | |
| 49 | static std::map<std::string,std::string> i18n_entries; |
| 50 | |
| 51 | /* Logging isn't initialized yet when this is run */ |
| 52 | /* add std::flush, because std::endl doesn't seem to flush, contrary to expected */ |
| 53 | // #define i18n_log(x) do { std::cout << __FILE__ << ":" << __LINE__ << ": " << x << std::endl; std::cout << std::flush; } while(0) |
| 54 | #define i18n_log(x) ((void)0) |
| 55 | |
| 56 | std::string i18n_get_language() |
| 57 | { |
| 58 | const char *e; |
| 59 | |
| 60 | e = getenv("LANG"); |
| 61 | i18n_log("LANG=" << e); |
| 62 | if (!e || !*e) { |
| 63 | e = getenv("LC_ALL"); |
| 64 | i18n_log("LC_ALL=" << e); |
| 65 | } |
| 66 | if (!e || !*e) |
| 67 | e = "en"; |
| 68 | |
| 69 | std::string language = e; |
| 70 | language = language.substr(0, language.find(".")); |
| 71 | language = language.substr(0, language.find("@")); |
| 72 | |
| 73 | // check valid values |
| 74 | for (char c: language) |
| 75 | if (!strchr("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-.@", c)) |
| 76 | return "en"; |
| 77 | |
| 78 | std::transform(language.begin(), language.end(), language.begin(), tolower); |
| 79 | if (language.size() > MAX_LANGUAGE_SIZE) |
| 80 | { |
| 81 | i18n_log("Language from LANG/LC_ALL suspiciously long, defaulting to en"); |
| 82 | return "en"; |
| 83 | } |
| 84 | return language; |
| 85 | } |
| 86 | |
| 87 | static uint32_t be32(const unsigned char *data) |
| 88 | { |
| 89 | return (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]; |
| 90 | } |
| 91 | |
| 92 | static std::string utf16(const unsigned char *data, uint32_t len) |
| 93 | { |
| 94 | std::string s; |
| 95 | while (len >= 2) { |
| 96 | uint32_t code = (data[0] << 8) | data[1]; |
| 97 | data += 2; |
| 98 | len -= 2; |
| 99 | if (code >= 0xd800 && code <= 0xdbfff && len >= 2) { |
| 100 | uint32_t next = (data[0] << 8) | data[1]; |
| 101 | if (next >= 0xdc00 && next <= 0xdfff) { |
| 102 | code = (code << 10) + next - 0x35dfc00; |
| 103 | data += 2; |
| 104 | len -= 2; |
| 105 | } |
| 106 | } |
| 107 | if (code <= 0x7f) { |
| 108 | s += (char)code; |
| 109 | } |
| 110 | else if (code <= 0x7ff) { |
| 111 | s += 0xc0 | (code >> 6); |
| 112 | s += 0x80 | (code & 0x3f); |
| 113 | } |
| 114 | else if (code <= 0xffff) { |
| 115 | s += 0xe0 | (code >> 12); |
| 116 | s += 0x80 | ((code >> 6) & 0x3f); |
| 117 | s += 0x80 | (code & 0x3f); |
| 118 | } |
| 119 | else { |
| 120 | s += 0xf0 | (code >> 18); |
| 121 | s += 0x80 | ((code >> 12) & 0x3f); |
| 122 | s += 0x80 | ((code >> 6) & 0x3f); |
| 123 | s += 0x80 | (code & 0x3f); |
| 124 | } |
| 125 | } |
| 126 | return s; |
| 127 | } |
| 128 | |
| 129 | static std::string utf8(const unsigned char *data, uint32_t len) |
| 130 | { |
| 131 | /* assume well formedness */ |
| 132 | return std::string((const char *)data,len); |
| 133 | } |
| 134 | |
| 135 | int i18n_set_language(const char *directory, const char *base, std::string language) |
| 136 | { |
| 137 | std::string filename, contents; |
| 138 | const unsigned char *data; |
| 139 | size_t datalen; |
| 140 | size_t idx; |
| 141 | unsigned char chunk_type; |
| 142 | uint32_t chunk_size; |
| 143 | uint32_t num_messages = (uint32_t)-1; |
| 144 | uint32_t messages_idx = (uint32_t)-1; |
| 145 | uint32_t offsets_idx = (uint32_t)-1; |
| 146 | std::string translation, source, context; |
| 147 | |
| 148 | i18n_log("i18n_set_language(" << directory << "," << base << ")"); |
| 149 | if (!directory || !base) |
| 150 | return -1; |
| 151 | |
| 152 | if (language.empty()) |
| 153 | language = i18n_get_language(); |
| 154 | filename = std::string(directory) + "/" + base + "_" + language + ".qm"; |
| 155 | i18n_log("Loading translations for language " << language); |
| 156 | |
| 157 | boost::system::error_code ignored_ec; |
| 158 | if (boost::filesystem::exists(filename, ignored_ec)) { |
| 159 | if (!epee::file_io_utils::load_file_to_string(filename, contents)) { |
| 160 | i18n_log("Failed to load translations file: " << filename); |
| 161 | return -1; |
| 162 | } |
| 163 | } else { |
| 164 | i18n_log("Translations file not found: " << filename); |
| 165 | filename = std::string(base) + "_" + language + ".qm"; |
| 166 | if (!find_embedded_file(filename, contents)) { |
| 167 | i18n_log("Embedded translations file not found: " << filename); |
| 168 | const char *underscore = strchr(language.c_str(), '_'); |
| 169 | if (underscore) { |
| 170 | std::string fallback_language = std::string(language, 0, underscore - language.c_str()); |
| 171 | filename = std::string(directory) + "/" + base + "_" + fallback_language + ".qm"; |
| 172 | i18n_log("Loading translations for language " << fallback_language); |
| 173 | if (boost::filesystem::exists(filename, ignored_ec)) { |
| 174 | if (!epee::file_io_utils::load_file_to_string(filename, contents)) { |
| 175 | i18n_log("Failed to load translations file: " << filename); |
| 176 | return -1; |
| 177 | } |
| 178 | } else { |
| 179 | i18n_log("Translations file not found: " << filename); |
| 180 | filename = std::string(base) + "_" + fallback_language + ".qm"; |
| 181 | if (!find_embedded_file(filename, contents)) { |
| 182 | i18n_log("Embedded translations file not found: " << filename); |
| 183 | return -1; |
| 184 | } |
| 185 | } |
| 186 | } else { |
| 187 | return -1; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | data = (const unsigned char*)contents.c_str(); |
| 193 | datalen = contents.size(); |
| 194 | idx = 0; |
| 195 | i18n_log("Translations file size: " << datalen); |
| 196 | |
| 197 | /* Format of the QM file (AFAICT): |
| 198 | * 16 bytes magic |
| 199 | * chunk list: N instances of chunks: |
| 200 | * 1 byte: chunk type (0x42: offsets, 0x69: messages) |
| 201 | * 4 bytes: chunk length, big endian |
| 202 | * D bytes: "chunk length" bytes of data |
| 203 | * |
| 204 | * 0x42 chunk: N instances of subchunks: |
| 205 | * 1 byte: subchunk type |
| 206 | * 0x01: end, no data |
| 207 | * 0x02: unsupported |
| 208 | * 0x03: translation |
| 209 | * 4 bytes: string length, big endian |
| 210 | * N bytes: string data, UTF-16 (or UCS2-BE ?) |
| 211 | * 0x04: unsupported |
| 212 | * 0x05: obsolete, unsupported |
| 213 | * 0x06: source text |
| 214 | * 0x07: context |
| 215 | * 0x08: obsolete, unsupported |
| 216 | * other: unsupported |
| 217 | * 4 bytes: subchunk length, big endian - except for 0x01, which has none |
| 218 | * S bytes: "chunk length" bytes of data |
| 219 | * 0x69 chunk: |
| 220 | * string data indexed by the 0x42 chunk data |
| 221 | */ |
| 222 | if (datalen < sizeof(qm_magic) || memcmp(data, qm_magic, sizeof(qm_magic))) { |
| 223 | i18n_log("Bad translations file format: " << filename); |
| 224 | return -1; |
| 225 | } |
| 226 | idx += sizeof(qm_magic); |
| 227 | |
| 228 | while (idx < datalen) { |
| 229 | if (idx + 5 > datalen) { |
| 230 | i18n_log("Bad translations file format: " << filename); |
| 231 | return -1; |
| 232 | } |
| 233 | chunk_type = data[idx++]; |
| 234 | chunk_size = be32(data+idx); |
| 235 | idx += 4; |
| 236 | |
| 237 | i18n_log("Found " << chunk_type << " of " << chunk_size << " bytes"); |
| 238 | if (chunk_size >= datalen || idx > datalen - chunk_size) { |
| 239 | i18n_log("Bad translations file format: " << filename); |
| 240 | return -1; |
| 241 | } |
| 242 | |
| 243 | switch (chunk_type) { |
| 244 | case 0x42: |
| 245 | i18n_log("Found offsets at " << idx); |
| 246 | /* two 32 bit integers, and possible padding */ |
| 247 | offsets_idx = idx; |
| 248 | num_messages = chunk_size / 8; |
| 249 | break; |
| 250 | case 0x69: |
| 251 | i18n_log("Found messages at " << idx); |
| 252 | messages_idx = idx; |
| 253 | break; |
| 254 | default: |
| 255 | i18n_log("Found unsupported chunk type: " << chunk_type); |
| 256 | break; |
| 257 | } |
| 258 | |
| 259 | idx += chunk_size; |
| 260 | } |
| 261 | |
| 262 | if (offsets_idx == (uint32_t)-1) { |
| 263 | i18n_log("No offsets chunk found"); |
| 264 | return -1; |
| 265 | } |
| 266 | if (messages_idx == (uint32_t)-1) { |
| 267 | i18n_log("No messages chunk found"); |
| 268 | return -1; |
| 269 | } |
| 270 | |
| 271 | for (uint32_t m = 0; m < num_messages; ++m) { |
| 272 | be32(data+offsets_idx+m*8); // unused |
| 273 | idx = be32(data+offsets_idx+m*8+4); |
| 274 | idx += messages_idx; |
| 275 | |
| 276 | if (idx > datalen || idx + 1 > datalen) { |
| 277 | i18n_log("Bad translations file format: " << filename); |
| 278 | return -1; |
| 279 | } |
| 280 | |
| 281 | while (1) { |
| 282 | if (idx + 5 > datalen) { |
| 283 | i18n_log("Bad translations file format: " << filename); |
| 284 | return -1; |
| 285 | } |
| 286 | chunk_type = data[idx++]; |
| 287 | chunk_size = 0; |
| 288 | if (chunk_type == 0x01) { |
| 289 | i18n_entries[context + std::string("",1) + source] = translation; |
| 290 | context = std::string(); |
| 291 | source = std::string(); |
| 292 | translation = std::string(); |
| 293 | break; |
| 294 | } |
| 295 | |
| 296 | chunk_size = be32(data+idx); |
| 297 | idx += 4; |
| 298 | i18n_log("Found " << chunk_type << " of " << chunk_size << " bytes"); |
| 299 | if (chunk_size >= datalen || idx > datalen - chunk_size) { |
| 300 | i18n_log("Bad translations file format: " << filename); |
| 301 | return -1; |
| 302 | } |
| 303 | switch (chunk_type) { |
| 304 | case 0x03: // translation, UTF-16 |
| 305 | translation = utf16(data+idx, chunk_size); |
| 306 | i18n_log("Found translation: " << translation); |
| 307 | break; |
| 308 | case 0x06: // source, UTF-8 |
| 309 | source = utf8(data+idx, chunk_size); |
| 310 | i18n_log("Found source: " << source); |
| 311 | break; |
| 312 | case 0x07: // context, UTF-8 |
| 313 | context = utf8(data+idx, chunk_size); |
| 314 | i18n_log("Found context: " << context); |
| 315 | break; |
| 316 | } |
| 317 | idx += chunk_size; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | /* The entries is constant by that time */ |
| 325 | const char *i18n_translate(const char *s, const std::string &context) |
| 326 | { |
| 327 | const std::string key = context + std::string("", 1) + s; |
| 328 | std::map<std::string,std::string>::const_iterator i = i18n_entries.find(key); |
| 329 | if (i == i18n_entries.end()) |
| 330 | return s; |
| 331 | return (*i).second.c_str(); |
| 332 | } |
| 333 | |
| 334 |