string.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "string.h"
  17. #include "pointer.h"
  18. #include "numeric.h"
  19. #include "log.h"
  20. #include "hex.h"
  21. #include "porting.h"
  22. #include "translation.h"
  23. #include <algorithm>
  24. #include <array>
  25. #include <sstream>
  26. #include <iomanip>
  27. #include <map>
  28. #ifndef _WIN32
  29. #include <iconv.h>
  30. #else
  31. #define _WIN32_WINNT 0x0501
  32. #include <windows.h>
  33. #endif
  34. #if defined(_ICONV_H_) && (defined(__FreeBSD__) || defined(__NetBSD__) || \
  35. defined(__OpenBSD__) || defined(__DragonFly__))
  36. #define BSD_ICONV_USED
  37. #endif
  38. static bool parseHexColorString(const std::string &value, video::SColor &color,
  39. unsigned char default_alpha = 0xff);
  40. static bool parseNamedColorString(const std::string &value, video::SColor &color);
  41. #ifndef _WIN32
  42. bool convert(const char *to, const char *from, char *outbuf,
  43. size_t outbuf_size, char *inbuf, size_t inbuf_size)
  44. {
  45. iconv_t cd = iconv_open(to, from);
  46. #ifdef BSD_ICONV_USED
  47. const char *inbuf_ptr = inbuf;
  48. #else
  49. char *inbuf_ptr = inbuf;
  50. #endif
  51. char *outbuf_ptr = outbuf;
  52. size_t *inbuf_left_ptr = &inbuf_size;
  53. size_t *outbuf_left_ptr = &outbuf_size;
  54. size_t old_size = inbuf_size;
  55. while (inbuf_size > 0) {
  56. iconv(cd, &inbuf_ptr, inbuf_left_ptr, &outbuf_ptr, outbuf_left_ptr);
  57. if (inbuf_size == old_size) {
  58. iconv_close(cd);
  59. return false;
  60. }
  61. old_size = inbuf_size;
  62. }
  63. iconv_close(cd);
  64. return true;
  65. }
  66. #ifdef __ANDROID__
  67. // Android need manual caring to support the full character set possible with wchar_t
  68. const char *DEFAULT_ENCODING = "UTF-32LE";
  69. #else
  70. const char *DEFAULT_ENCODING = "WCHAR_T";
  71. #endif
  72. std::wstring utf8_to_wide(const std::string &input)
  73. {
  74. size_t inbuf_size = input.length() + 1;
  75. // maximum possible size, every character is sizeof(wchar_t) bytes
  76. size_t outbuf_size = (input.length() + 1) * sizeof(wchar_t);
  77. char *inbuf = new char[inbuf_size];
  78. memcpy(inbuf, input.c_str(), inbuf_size);
  79. char *outbuf = new char[outbuf_size];
  80. memset(outbuf, 0, outbuf_size);
  81. #ifdef __ANDROID__
  82. // Android need manual caring to support the full character set possible with wchar_t
  83. SANITY_CHECK(sizeof(wchar_t) == 4);
  84. #endif
  85. if (!convert(DEFAULT_ENCODING, "UTF-8", outbuf, outbuf_size, inbuf, inbuf_size)) {
  86. infostream << "Couldn't convert UTF-8 string 0x" << hex_encode(input)
  87. << " into wstring" << std::endl;
  88. delete[] inbuf;
  89. delete[] outbuf;
  90. return L"<invalid UTF-8 string>";
  91. }
  92. std::wstring out((wchar_t *)outbuf);
  93. delete[] inbuf;
  94. delete[] outbuf;
  95. return out;
  96. }
  97. std::string wide_to_utf8(const std::wstring &input)
  98. {
  99. size_t inbuf_size = (input.length() + 1) * sizeof(wchar_t);
  100. // maximum possible size: utf-8 encodes codepoints using 1 up to 6 bytes
  101. size_t outbuf_size = (input.length() + 1) * 6;
  102. char *inbuf = new char[inbuf_size];
  103. memcpy(inbuf, input.c_str(), inbuf_size);
  104. char *outbuf = new char[outbuf_size];
  105. memset(outbuf, 0, outbuf_size);
  106. if (!convert("UTF-8", DEFAULT_ENCODING, outbuf, outbuf_size, inbuf, inbuf_size)) {
  107. infostream << "Couldn't convert wstring 0x" << hex_encode(inbuf, inbuf_size)
  108. << " into UTF-8 string" << std::endl;
  109. delete[] inbuf;
  110. delete[] outbuf;
  111. return "<invalid wstring>";
  112. }
  113. std::string out(outbuf);
  114. delete[] inbuf;
  115. delete[] outbuf;
  116. return out;
  117. }
  118. #else // _WIN32
  119. std::wstring utf8_to_wide(const std::string &input)
  120. {
  121. size_t outbuf_size = input.size() + 1;
  122. wchar_t *outbuf = new wchar_t[outbuf_size];
  123. memset(outbuf, 0, outbuf_size * sizeof(wchar_t));
  124. MultiByteToWideChar(CP_UTF8, 0, input.c_str(), input.size(),
  125. outbuf, outbuf_size);
  126. std::wstring out(outbuf);
  127. delete[] outbuf;
  128. return out;
  129. }
  130. std::string wide_to_utf8(const std::wstring &input)
  131. {
  132. size_t outbuf_size = (input.size() + 1) * 6;
  133. char *outbuf = new char[outbuf_size];
  134. memset(outbuf, 0, outbuf_size);
  135. WideCharToMultiByte(CP_UTF8, 0, input.c_str(), input.size(),
  136. outbuf, outbuf_size, NULL, NULL);
  137. std::string out(outbuf);
  138. delete[] outbuf;
  139. return out;
  140. }
  141. #endif // _WIN32
  142. // You must free the returned string!
  143. // The returned string is allocated using new
  144. wchar_t *utf8_to_wide_c(const char *str)
  145. {
  146. std::wstring ret = utf8_to_wide(std::string(str));
  147. size_t len = ret.length();
  148. wchar_t *ret_c = new wchar_t[len + 1];
  149. memset(ret_c, 0, (len + 1) * sizeof(wchar_t));
  150. memcpy(ret_c, ret.c_str(), len * sizeof(wchar_t));
  151. return ret_c;
  152. }
  153. // You must free the returned string!
  154. // The returned string is allocated using new
  155. wchar_t *narrow_to_wide_c(const char *str)
  156. {
  157. wchar_t *nstr = nullptr;
  158. #if defined(_WIN32)
  159. int nResult = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) str, -1, 0, 0);
  160. if (nResult == 0) {
  161. errorstream<<"gettext: MultiByteToWideChar returned null"<<std::endl;
  162. } else {
  163. nstr = new wchar_t[nResult];
  164. MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) str, -1, (WCHAR *) nstr, nResult);
  165. }
  166. #else
  167. size_t len = strlen(str);
  168. nstr = new wchar_t[len + 1];
  169. std::wstring intermediate = narrow_to_wide(str);
  170. memset(nstr, 0, (len + 1) * sizeof(wchar_t));
  171. memcpy(nstr, intermediate.c_str(), len * sizeof(wchar_t));
  172. #endif
  173. return nstr;
  174. }
  175. std::wstring narrow_to_wide(const std::string &mbs) {
  176. #ifdef __ANDROID__
  177. return utf8_to_wide(mbs);
  178. #else
  179. size_t wcl = mbs.size();
  180. Buffer<wchar_t> wcs(wcl + 1);
  181. size_t len = mbstowcs(*wcs, mbs.c_str(), wcl);
  182. if (len == (size_t)(-1))
  183. return L"<invalid multibyte string>";
  184. wcs[len] = 0;
  185. return *wcs;
  186. #endif
  187. }
  188. std::string wide_to_narrow(const std::wstring &wcs)
  189. {
  190. #ifdef __ANDROID__
  191. return wide_to_utf8(wcs);
  192. #else
  193. size_t mbl = wcs.size() * 4;
  194. SharedBuffer<char> mbs(mbl+1);
  195. size_t len = wcstombs(*mbs, wcs.c_str(), mbl);
  196. if (len == (size_t)(-1))
  197. return "Character conversion failed!";
  198. mbs[len] = 0;
  199. return *mbs;
  200. #endif
  201. }
  202. std::string urlencode(const std::string &str)
  203. {
  204. // Encodes non-unreserved URI characters by a percent sign
  205. // followed by two hex digits. See RFC 3986, section 2.3.
  206. static const char url_hex_chars[] = "0123456789ABCDEF";
  207. std::ostringstream oss(std::ios::binary);
  208. for (unsigned char c : str) {
  209. if (isalnum(c) || c == '-' || c == '.' || c == '_' || c == '~') {
  210. oss << c;
  211. } else {
  212. oss << "%"
  213. << url_hex_chars[(c & 0xf0) >> 4]
  214. << url_hex_chars[c & 0x0f];
  215. }
  216. }
  217. return oss.str();
  218. }
  219. std::string urldecode(const std::string &str)
  220. {
  221. // Inverse of urlencode
  222. std::ostringstream oss(std::ios::binary);
  223. for (u32 i = 0; i < str.size(); i++) {
  224. unsigned char highvalue, lowvalue;
  225. if (str[i] == '%' &&
  226. hex_digit_decode(str[i+1], highvalue) &&
  227. hex_digit_decode(str[i+2], lowvalue)) {
  228. oss << (char) ((highvalue << 4) | lowvalue);
  229. i += 2;
  230. } else {
  231. oss << str[i];
  232. }
  233. }
  234. return oss.str();
  235. }
  236. u32 readFlagString(std::string str, const FlagDesc *flagdesc, u32 *flagmask)
  237. {
  238. u32 result = 0;
  239. u32 mask = 0;
  240. char *s = &str[0];
  241. char *flagstr;
  242. char *strpos = nullptr;
  243. while ((flagstr = strtok_r(s, ",", &strpos))) {
  244. s = nullptr;
  245. while (*flagstr == ' ' || *flagstr == '\t')
  246. flagstr++;
  247. bool flagset = true;
  248. if (!strncasecmp(flagstr, "no", 2)) {
  249. flagset = false;
  250. flagstr += 2;
  251. }
  252. for (int i = 0; flagdesc[i].name; i++) {
  253. if (!strcasecmp(flagstr, flagdesc[i].name)) {
  254. mask |= flagdesc[i].flag;
  255. if (flagset)
  256. result |= flagdesc[i].flag;
  257. break;
  258. }
  259. }
  260. }
  261. if (flagmask)
  262. *flagmask = mask;
  263. return result;
  264. }
  265. std::string writeFlagString(u32 flags, const FlagDesc *flagdesc, u32 flagmask)
  266. {
  267. std::string result;
  268. for (int i = 0; flagdesc[i].name; i++) {
  269. if (flagmask & flagdesc[i].flag) {
  270. if (!(flags & flagdesc[i].flag))
  271. result += "no";
  272. result += flagdesc[i].name;
  273. result += ", ";
  274. }
  275. }
  276. size_t len = result.length();
  277. if (len >= 2)
  278. result.erase(len - 2, 2);
  279. return result;
  280. }
  281. size_t mystrlcpy(char *dst, const char *src, size_t size)
  282. {
  283. size_t srclen = strlen(src) + 1;
  284. size_t copylen = MYMIN(srclen, size);
  285. if (copylen > 0) {
  286. memcpy(dst, src, copylen);
  287. dst[copylen - 1] = '\0';
  288. }
  289. return srclen;
  290. }
  291. char *mystrtok_r(char *s, const char *sep, char **lasts)
  292. {
  293. char *t;
  294. if (!s)
  295. s = *lasts;
  296. while (*s && strchr(sep, *s))
  297. s++;
  298. if (!*s)
  299. return nullptr;
  300. t = s;
  301. while (*t) {
  302. if (strchr(sep, *t)) {
  303. *t++ = '\0';
  304. break;
  305. }
  306. t++;
  307. }
  308. *lasts = t;
  309. return s;
  310. }
  311. u64 read_seed(const char *str)
  312. {
  313. char *endptr;
  314. u64 num;
  315. if (str[0] == '0' && str[1] == 'x')
  316. num = strtoull(str, &endptr, 16);
  317. else
  318. num = strtoull(str, &endptr, 10);
  319. if (*endptr)
  320. num = murmur_hash_64_ua(str, (int)strlen(str), 0x1337);
  321. return num;
  322. }
  323. bool parseColorString(const std::string &value, video::SColor &color, bool quiet,
  324. unsigned char default_alpha)
  325. {
  326. bool success;
  327. if (value[0] == '#')
  328. success = parseHexColorString(value, color, default_alpha);
  329. else
  330. success = parseNamedColorString(value, color);
  331. if (!success && !quiet)
  332. errorstream << "Invalid color: \"" << value << "\"" << std::endl;
  333. return success;
  334. }
  335. static bool parseHexColorString(const std::string &value, video::SColor &color,
  336. unsigned char default_alpha)
  337. {
  338. unsigned char components[] = { 0x00, 0x00, 0x00, default_alpha }; // R,G,B,A
  339. if (value[0] != '#')
  340. return false;
  341. size_t len = value.size();
  342. bool short_form;
  343. if (len == 9 || len == 7) // #RRGGBBAA or #RRGGBB
  344. short_form = false;
  345. else if (len == 5 || len == 4) // #RGBA or #RGB
  346. short_form = true;
  347. else
  348. return false;
  349. bool success = true;
  350. for (size_t pos = 1, cc = 0; pos < len; pos++, cc++) {
  351. assert(cc < sizeof components / sizeof components[0]);
  352. if (short_form) {
  353. unsigned char d;
  354. if (!hex_digit_decode(value[pos], d)) {
  355. success = false;
  356. break;
  357. }
  358. components[cc] = (d & 0xf) << 4 | (d & 0xf);
  359. } else {
  360. unsigned char d1, d2;
  361. if (!hex_digit_decode(value[pos], d1) ||
  362. !hex_digit_decode(value[pos+1], d2)) {
  363. success = false;
  364. break;
  365. }
  366. components[cc] = (d1 & 0xf) << 4 | (d2 & 0xf);
  367. pos++; // skip the second digit -- it's already used
  368. }
  369. }
  370. if (success) {
  371. color.setRed(components[0]);
  372. color.setGreen(components[1]);
  373. color.setBlue(components[2]);
  374. color.setAlpha(components[3]);
  375. }
  376. return success;
  377. }
  378. struct ColorContainer {
  379. ColorContainer();
  380. std::map<const std::string, u32> colors;
  381. };
  382. ColorContainer::ColorContainer()
  383. {
  384. colors["aliceblue"] = 0xf0f8ff;
  385. colors["antiquewhite"] = 0xfaebd7;
  386. colors["aqua"] = 0x00ffff;
  387. colors["aquamarine"] = 0x7fffd4;
  388. colors["azure"] = 0xf0ffff;
  389. colors["beige"] = 0xf5f5dc;
  390. colors["bisque"] = 0xffe4c4;
  391. colors["black"] = 00000000;
  392. colors["blanchedalmond"] = 0xffebcd;
  393. colors["blue"] = 0x0000ff;
  394. colors["blueviolet"] = 0x8a2be2;
  395. colors["brown"] = 0xa52a2a;
  396. colors["burlywood"] = 0xdeb887;
  397. colors["cadetblue"] = 0x5f9ea0;
  398. colors["chartreuse"] = 0x7fff00;
  399. colors["chocolate"] = 0xd2691e;
  400. colors["coral"] = 0xff7f50;
  401. colors["cornflowerblue"] = 0x6495ed;
  402. colors["cornsilk"] = 0xfff8dc;
  403. colors["crimson"] = 0xdc143c;
  404. colors["cyan"] = 0x00ffff;
  405. colors["darkblue"] = 0x00008b;
  406. colors["darkcyan"] = 0x008b8b;
  407. colors["darkgoldenrod"] = 0xb8860b;
  408. colors["darkgray"] = 0xa9a9a9;
  409. colors["darkgreen"] = 0x006400;
  410. colors["darkgrey"] = 0xa9a9a9;
  411. colors["darkkhaki"] = 0xbdb76b;
  412. colors["darkmagenta"] = 0x8b008b;
  413. colors["darkolivegreen"] = 0x556b2f;
  414. colors["darkorange"] = 0xff8c00;
  415. colors["darkorchid"] = 0x9932cc;
  416. colors["darkred"] = 0x8b0000;
  417. colors["darksalmon"] = 0xe9967a;
  418. colors["darkseagreen"] = 0x8fbc8f;
  419. colors["darkslateblue"] = 0x483d8b;
  420. colors["darkslategray"] = 0x2f4f4f;
  421. colors["darkslategrey"] = 0x2f4f4f;
  422. colors["darkturquoise"] = 0x00ced1;
  423. colors["darkviolet"] = 0x9400d3;
  424. colors["deeppink"] = 0xff1493;
  425. colors["deepskyblue"] = 0x00bfff;
  426. colors["dimgray"] = 0x696969;
  427. colors["dimgrey"] = 0x696969;
  428. colors["dodgerblue"] = 0x1e90ff;
  429. colors["firebrick"] = 0xb22222;
  430. colors["floralwhite"] = 0xfffaf0;
  431. colors["forestgreen"] = 0x228b22;
  432. colors["fuchsia"] = 0xff00ff;
  433. colors["gainsboro"] = 0xdcdcdc;
  434. colors["ghostwhite"] = 0xf8f8ff;
  435. colors["gold"] = 0xffd700;
  436. colors["goldenrod"] = 0xdaa520;
  437. colors["gray"] = 0x808080;
  438. colors["green"] = 0x008000;
  439. colors["greenyellow"] = 0xadff2f;
  440. colors["grey"] = 0x808080;
  441. colors["honeydew"] = 0xf0fff0;
  442. colors["hotpink"] = 0xff69b4;
  443. colors["indianred"] = 0xcd5c5c;
  444. colors["indigo"] = 0x4b0082;
  445. colors["ivory"] = 0xfffff0;
  446. colors["khaki"] = 0xf0e68c;
  447. colors["lavender"] = 0xe6e6fa;
  448. colors["lavenderblush"] = 0xfff0f5;
  449. colors["lawngreen"] = 0x7cfc00;
  450. colors["lemonchiffon"] = 0xfffacd;
  451. colors["lightblue"] = 0xadd8e6;
  452. colors["lightcoral"] = 0xf08080;
  453. colors["lightcyan"] = 0xe0ffff;
  454. colors["lightgoldenrodyellow"] = 0xfafad2;
  455. colors["lightgray"] = 0xd3d3d3;
  456. colors["lightgreen"] = 0x90ee90;
  457. colors["lightgrey"] = 0xd3d3d3;
  458. colors["lightpink"] = 0xffb6c1;
  459. colors["lightsalmon"] = 0xffa07a;
  460. colors["lightseagreen"] = 0x20b2aa;
  461. colors["lightskyblue"] = 0x87cefa;
  462. colors["lightslategray"] = 0x778899;
  463. colors["lightslategrey"] = 0x778899;
  464. colors["lightsteelblue"] = 0xb0c4de;
  465. colors["lightyellow"] = 0xffffe0;
  466. colors["lime"] = 0x00ff00;
  467. colors["limegreen"] = 0x32cd32;
  468. colors["linen"] = 0xfaf0e6;
  469. colors["magenta"] = 0xff00ff;
  470. colors["maroon"] = 0x800000;
  471. colors["mediumaquamarine"] = 0x66cdaa;
  472. colors["mediumblue"] = 0x0000cd;
  473. colors["mediumorchid"] = 0xba55d3;
  474. colors["mediumpurple"] = 0x9370db;
  475. colors["mediumseagreen"] = 0x3cb371;
  476. colors["mediumslateblue"] = 0x7b68ee;
  477. colors["mediumspringgreen"] = 0x00fa9a;
  478. colors["mediumturquoise"] = 0x48d1cc;
  479. colors["mediumvioletred"] = 0xc71585;
  480. colors["midnightblue"] = 0x191970;
  481. colors["mintcream"] = 0xf5fffa;
  482. colors["mistyrose"] = 0xffe4e1;
  483. colors["moccasin"] = 0xffe4b5;
  484. colors["navajowhite"] = 0xffdead;
  485. colors["navy"] = 0x000080;
  486. colors["oldlace"] = 0xfdf5e6;
  487. colors["olive"] = 0x808000;
  488. colors["olivedrab"] = 0x6b8e23;
  489. colors["orange"] = 0xffa500;
  490. colors["orangered"] = 0xff4500;
  491. colors["orchid"] = 0xda70d6;
  492. colors["palegoldenrod"] = 0xeee8aa;
  493. colors["palegreen"] = 0x98fb98;
  494. colors["paleturquoise"] = 0xafeeee;
  495. colors["palevioletred"] = 0xdb7093;
  496. colors["papayawhip"] = 0xffefd5;
  497. colors["peachpuff"] = 0xffdab9;
  498. colors["peru"] = 0xcd853f;
  499. colors["pink"] = 0xffc0cb;
  500. colors["plum"] = 0xdda0dd;
  501. colors["powderblue"] = 0xb0e0e6;
  502. colors["purple"] = 0x800080;
  503. colors["red"] = 0xff0000;
  504. colors["rosybrown"] = 0xbc8f8f;
  505. colors["royalblue"] = 0x4169e1;
  506. colors["saddlebrown"] = 0x8b4513;
  507. colors["salmon"] = 0xfa8072;
  508. colors["sandybrown"] = 0xf4a460;
  509. colors["seagreen"] = 0x2e8b57;
  510. colors["seashell"] = 0xfff5ee;
  511. colors["sienna"] = 0xa0522d;
  512. colors["silver"] = 0xc0c0c0;
  513. colors["skyblue"] = 0x87ceeb;
  514. colors["slateblue"] = 0x6a5acd;
  515. colors["slategray"] = 0x708090;
  516. colors["slategrey"] = 0x708090;
  517. colors["snow"] = 0xfffafa;
  518. colors["springgreen"] = 0x00ff7f;
  519. colors["steelblue"] = 0x4682b4;
  520. colors["tan"] = 0xd2b48c;
  521. colors["teal"] = 0x008080;
  522. colors["thistle"] = 0xd8bfd8;
  523. colors["tomato"] = 0xff6347;
  524. colors["turquoise"] = 0x40e0d0;
  525. colors["violet"] = 0xee82ee;
  526. colors["wheat"] = 0xf5deb3;
  527. colors["white"] = 0xffffff;
  528. colors["whitesmoke"] = 0xf5f5f5;
  529. colors["yellow"] = 0xffff00;
  530. colors["yellowgreen"] = 0x9acd32;
  531. }
  532. static const ColorContainer named_colors;
  533. static bool parseNamedColorString(const std::string &value, video::SColor &color)
  534. {
  535. std::string color_name;
  536. std::string alpha_string;
  537. /* If the string has a # in it, assume this is the start of a specified
  538. * alpha value (if it isn't the string is invalid and the error will be
  539. * caught later on, either because the color name won't be found or the
  540. * alpha value will fail conversion)
  541. */
  542. size_t alpha_pos = value.find('#');
  543. if (alpha_pos != std::string::npos) {
  544. color_name = value.substr(0, alpha_pos);
  545. alpha_string = value.substr(alpha_pos + 1);
  546. } else {
  547. color_name = value;
  548. }
  549. color_name = lowercase(color_name);
  550. std::map<const std::string, unsigned>::const_iterator it;
  551. it = named_colors.colors.find(color_name);
  552. if (it == named_colors.colors.end())
  553. return false;
  554. u32 color_temp = it->second;
  555. /* An empty string for alpha is ok (none of the color table entries
  556. * have an alpha value either). Color strings without an alpha specified
  557. * are interpreted as fully opaque
  558. *
  559. * For named colors the supplied alpha string (representing a hex value)
  560. * must be exactly two digits. For example: colorname#08
  561. */
  562. if (!alpha_string.empty()) {
  563. if (alpha_string.length() != 2)
  564. return false;
  565. unsigned char d1, d2;
  566. if (!hex_digit_decode(alpha_string.at(0), d1)
  567. || !hex_digit_decode(alpha_string.at(1), d2))
  568. return false;
  569. color_temp |= ((d1 & 0xf) << 4 | (d2 & 0xf)) << 24;
  570. } else {
  571. color_temp |= 0xff << 24; // Fully opaque
  572. }
  573. color = video::SColor(color_temp);
  574. return true;
  575. }
  576. void str_replace(std::string &str, char from, char to)
  577. {
  578. std::replace(str.begin(), str.end(), from, to);
  579. }
  580. /* Translated strings have the following format:
  581. * \x1bT marks the beginning of a translated string
  582. * \x1bE marks its end
  583. *
  584. * \x1bF marks the beginning of an argument, and \x1bE its end.
  585. *
  586. * Arguments are *not* translated, as they may contain escape codes.
  587. * Thus, if you want a translated argument, it should be inside \x1bT/\x1bE tags as well.
  588. *
  589. * This representation is chosen so that clients ignoring escape codes will
  590. * see untranslated strings.
  591. *
  592. * For instance, suppose we have a string such as "@1 Wool" with the argument "White"
  593. * The string will be sent as "\x1bT\x1bF\x1bTWhite\x1bE\x1bE Wool\x1bE"
  594. * To translate this string, we extract what is inside \x1bT/\x1bE tags.
  595. * When we notice the \x1bF tag, we recursively extract what is there up to the \x1bE end tag,
  596. * translating it as well.
  597. * We get the argument "White", translated, and create a template string with "@1" instead of it.
  598. * We finally get the template "@1 Wool" that was used in the beginning, which we translate
  599. * before filling it again.
  600. */
  601. void translate_all(const std::wstring &s, size_t &i,
  602. Translations *translations, std::wstring &res);
  603. void translate_string(const std::wstring &s, Translations *translations,
  604. const std::wstring &textdomain, size_t &i, std::wstring &res)
  605. {
  606. std::wostringstream output;
  607. std::vector<std::wstring> args;
  608. int arg_number = 1;
  609. while (i < s.length()) {
  610. // Not an escape sequence: just add the character.
  611. if (s[i] != '\x1b') {
  612. output.put(s[i]);
  613. // The character is a literal '@'; add it twice
  614. // so that it is not mistaken for an argument.
  615. if (s[i] == L'@')
  616. output.put(L'@');
  617. ++i;
  618. continue;
  619. }
  620. // We have an escape sequence: locate it and its data
  621. // It is either a single character, or it begins with '('
  622. // and extends up to the following ')', with '\' as an escape character.
  623. ++i;
  624. size_t start_index = i;
  625. size_t length;
  626. if (i == s.length()) {
  627. length = 0;
  628. } else if (s[i] == L'(') {
  629. ++i;
  630. ++start_index;
  631. while (i < s.length() && s[i] != L')') {
  632. if (s[i] == L'\\')
  633. ++i;
  634. ++i;
  635. }
  636. length = i - start_index;
  637. ++i;
  638. if (i > s.length())
  639. i = s.length();
  640. } else {
  641. ++i;
  642. length = 1;
  643. }
  644. std::wstring escape_sequence(s, start_index, length);
  645. // The escape sequence is now reconstructed.
  646. std::vector<std::wstring> parts = split(escape_sequence, L'@');
  647. if (parts[0] == L"E") {
  648. // "End of translation" escape sequence. We are done locating the string to translate.
  649. break;
  650. } else if (parts[0] == L"F") {
  651. // "Start of argument" escape sequence.
  652. // Recursively translate the argument, and add it to the argument list.
  653. // Add an "@n" instead of the argument to the template to translate.
  654. if (arg_number >= 10) {
  655. errorstream << "Ignoring too many arguments to translation" << std::endl;
  656. std::wstring arg;
  657. translate_all(s, i, translations, arg);
  658. args.push_back(arg);
  659. continue;
  660. }
  661. output.put(L'@');
  662. output << arg_number;
  663. ++arg_number;
  664. std::wstring arg;
  665. translate_all(s, i, translations, arg);
  666. args.push_back(arg);
  667. } else {
  668. // This is an escape sequence *inside* the template string to translate itself.
  669. // This should not happen, show an error message.
  670. errorstream << "Ignoring escape sequence '" << wide_to_narrow(escape_sequence) << "' in translation" << std::endl;
  671. }
  672. }
  673. std::wstring toutput;
  674. // Translate the template.
  675. if (translations != nullptr)
  676. toutput = translations->getTranslation(
  677. textdomain, output.str());
  678. else
  679. toutput = output.str();
  680. // Put back the arguments in the translated template.
  681. std::wostringstream result;
  682. size_t j = 0;
  683. while (j < toutput.length()) {
  684. // Normal character, add it to output and continue.
  685. if (toutput[j] != L'@' || j == toutput.length() - 1) {
  686. result.put(toutput[j]);
  687. ++j;
  688. continue;
  689. }
  690. ++j;
  691. // Literal escape for '@'.
  692. if (toutput[j] == L'@') {
  693. result.put(L'@');
  694. ++j;
  695. continue;
  696. }
  697. // Here we have an argument; get its index and add the translated argument to the output.
  698. int arg_index = toutput[j] - L'1';
  699. ++j;
  700. if (0 <= arg_index && (size_t)arg_index < args.size()) {
  701. result << args[arg_index];
  702. } else {
  703. // This is not allowed: show an error message
  704. errorstream << "Ignoring out-of-bounds argument escape sequence in translation" << std::endl;
  705. }
  706. }
  707. res = result.str();
  708. }
  709. void translate_all(const std::wstring &s, size_t &i,
  710. Translations *translations, std::wstring &res)
  711. {
  712. std::wostringstream output;
  713. while (i < s.length()) {
  714. // Not an escape sequence: just add the character.
  715. if (s[i] != '\x1b') {
  716. output.put(s[i]);
  717. ++i;
  718. continue;
  719. }
  720. // We have an escape sequence: locate it and its data
  721. // It is either a single character, or it begins with '('
  722. // and extends up to the following ')', with '\' as an escape character.
  723. size_t escape_start = i;
  724. ++i;
  725. size_t start_index = i;
  726. size_t length;
  727. if (i == s.length()) {
  728. length = 0;
  729. } else if (s[i] == L'(') {
  730. ++i;
  731. ++start_index;
  732. while (i < s.length() && s[i] != L')') {
  733. if (s[i] == L'\\') {
  734. ++i;
  735. }
  736. ++i;
  737. }
  738. length = i - start_index;
  739. ++i;
  740. if (i > s.length())
  741. i = s.length();
  742. } else {
  743. ++i;
  744. length = 1;
  745. }
  746. std::wstring escape_sequence(s, start_index, length);
  747. // The escape sequence is now reconstructed.
  748. std::vector<std::wstring> parts = split(escape_sequence, L'@');
  749. if (parts[0] == L"E") {
  750. // "End of argument" escape sequence. Exit.
  751. break;
  752. } else if (parts[0] == L"T") {
  753. // Beginning of translated string.
  754. std::wstring textdomain;
  755. if (parts.size() > 1)
  756. textdomain = parts[1];
  757. std::wstring translated;
  758. translate_string(s, translations, textdomain, i, translated);
  759. output << translated;
  760. } else {
  761. // Another escape sequence, such as colors. Preserve it.
  762. output << std::wstring(s, escape_start, i - escape_start);
  763. }
  764. }
  765. res = output.str();
  766. }
  767. // Translate string server side
  768. std::wstring translate_string(const std::wstring &s, Translations *translations)
  769. {
  770. size_t i = 0;
  771. std::wstring res;
  772. translate_all(s, i, translations, res);
  773. return res;
  774. }
  775. // Translate string client side
  776. std::wstring translate_string(const std::wstring &s)
  777. {
  778. #ifdef SERVER
  779. return translate_string(s, nullptr);
  780. #else
  781. return translate_string(s, g_client_translations);
  782. #endif
  783. }
  784. static const std::array<std::wstring, 22> disallowed_dir_names = {
  785. // Problematic filenames from here:
  786. // https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file#file-and-directory-names
  787. L"CON",
  788. L"PRN",
  789. L"AUX",
  790. L"NUL",
  791. L"COM1",
  792. L"COM2",
  793. L"COM3",
  794. L"COM4",
  795. L"COM5",
  796. L"COM6",
  797. L"COM7",
  798. L"COM8",
  799. L"COM9",
  800. L"LPT1",
  801. L"LPT2",
  802. L"LPT3",
  803. L"LPT4",
  804. L"LPT5",
  805. L"LPT6",
  806. L"LPT7",
  807. L"LPT8",
  808. L"LPT9",
  809. };
  810. /**
  811. * List of characters that are blacklisted from created directories
  812. */
  813. static const std::wstring disallowed_path_chars = L"<>:\"/\\|?*.";
  814. /**
  815. * Sanitize the name of a new directory. This consists of two stages:
  816. * 1. Check for 'reserved filenames' that can't be used on some filesystems
  817. * and add a prefix to them
  818. * 2. Remove 'unsafe' characters from the name by replacing them with '_'
  819. */
  820. std::string sanitizeDirName(const std::string &str, const std::string &optional_prefix)
  821. {
  822. std::wstring safe_name = utf8_to_wide(str);
  823. for (std::wstring disallowed_name : disallowed_dir_names) {
  824. if (str_equal(safe_name, disallowed_name, true)) {
  825. safe_name = utf8_to_wide(optional_prefix) + safe_name;
  826. break;
  827. }
  828. }
  829. for (unsigned long i = 0; i < safe_name.length(); i++) {
  830. bool is_valid = true;
  831. // Unlikely, but control characters should always be blacklisted
  832. if (safe_name[i] < 32) {
  833. is_valid = false;
  834. } else if (safe_name[i] < 128) {
  835. is_valid = disallowed_path_chars.find_first_of(safe_name[i])
  836. == std::wstring::npos;
  837. }
  838. if (!is_valid)
  839. safe_name[i] = '_';
  840. }
  841. return wide_to_utf8(safe_name);
  842. }