3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "../porting.h"
36 #include <windows.h> // MultiByteToWideChar
39 static bool parseHexColorString(const std::string &value, video::SColor &color);
40 static bool parseNamedColorString(const std::string &value, video::SColor &color);
44 const wchar_t* wide_chars =
45 L" !\"#$%&'()*+,-./0123456789:;<=>?@"
46 L"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`"
47 L"abcdefghijklmnopqrstuvwxyz{|}~";
49 int wctomb(char *s, wchar_t wc)
51 for (unsigned int j = 0; j < (sizeof(wide_chars)/sizeof(wchar_t));j++) {
52 if (wc == wide_chars[j]) {
56 else if (wc == L'\n') {
64 int mbtowc(wchar_t *pwc, const char *s, size_t n)
66 if (s == NULL || *s == '\0')
69 const wchar_t *tmp = narrow_to_wide_c(s);
70 bool success = tmp[0] != '\0';
77 return success ? 1 : -1;
80 // You must free the returned string!
81 const wchar_t *narrow_to_wide_c(const char *mbs)
83 size_t mbl = strlen(mbs);
84 wchar_t *wcs = new wchar_t[mbl + 1];
86 for (size_t i = 0; i < mbl; i++) {
87 if (((unsigned char) mbs[i] > 31) &&
88 ((unsigned char) mbs[i] < 127)) {
89 wcs[i] = wide_chars[(unsigned char) mbs[i] - 32];
92 else if (mbs[i] == '\n') {
102 // You must free the returned string!
103 const wchar_t *narrow_to_wide_c(const char *mbs)
107 int nResult = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) mbs, -1, 0, 0);
109 errorstream << "gettext: MultiByteToWideChar returned null" << std::endl;
111 wcs = new wchar_t[nResult];
112 MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) mbs, -1, (WCHAR *) wcs, nResult);
115 size_t wcl = mbstowcs(NULL, mbs, 0);
116 if (wcl == (size_t) -1)
118 wcs = new wchar_t[wcl + 1];
119 size_t l = mbstowcs(wcs, mbs, wcl);
120 assert(l != (size_t) -1); // Should never happen if the last call worked
129 std::wstring narrow_to_wide(const std::string& mbs)
131 size_t wcl = mbs.size();
132 Buffer<wchar_t> wcs(wcl + 1);
133 size_t l = mbstowcs(*wcs, mbs.c_str(), wcl);
134 if (l == (size_t)(-1))
135 return L"<invalid multibyte string>";
141 std::string wide_to_narrow(const std::wstring& wcs) {
142 size_t mbl = wcs.size()*4;
144 std::string retval = "";
145 for (unsigned int i = 0; i < wcs.size(); i++) {
146 wchar_t char1 = (wchar_t) wcs[i];
148 if (char1 == L'\n') {
153 for (unsigned int j = 0; j < wcslen(wide_chars);j++) {
154 wchar_t char2 = (wchar_t) wide_chars[j];
156 if (char1 == char2) {
167 std::string wide_to_narrow(const std::wstring& wcs)
169 size_t mbl = wcs.size()*4;
170 SharedBuffer<char> mbs(mbl+1);
171 size_t l = wcstombs(*mbs, wcs.c_str(), mbl);
172 if(l == (size_t)(-1)) {
173 return "Character conversion failed!";
182 // Get an sha-1 hash of the player's name combined with
183 // the password entered. That's what the server uses as
184 // their password. (Exception : if the password field is
185 // blank, we send a blank password - this is for backwards
186 // compatibility with password-less players).
187 std::string translatePassword(std::string playername, std::wstring password)
189 if(password.length() == 0)
192 std::string slt = playername + wide_to_narrow(password);
194 sha1.addBytes(slt.c_str(), slt.length());
195 unsigned char *digest = sha1.getDigest();
196 std::string pwd = base64_encode(digest, 20);
201 std::string urlencode(std::string str)
203 // Encodes non-unreserved URI characters by a percent sign
204 // followed by two hex digits. See RFC 3986, section 2.3.
205 static const char url_hex_chars[] = "0123456789ABCDEF";
206 std::ostringstream oss(std::ios::binary);
207 for (u32 i = 0; i < str.size(); i++) {
208 unsigned char c = str[i];
209 if (isalnum(c) || c == '-' || c == '.' || c == '_' || c == '~')
213 << url_hex_chars[(c & 0xf0) >> 4]
214 << url_hex_chars[c & 0x0f];
219 std::string urldecode(std::string str)
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;
226 hex_digit_decode(str[i+1], highvalue) &&
227 hex_digit_decode(str[i+2], lowvalue)) {
228 oss << (char) ((highvalue << 4) | lowvalue);
237 u32 readFlagString(std::string str, const FlagDesc *flagdesc, u32 *flagmask)
239 u32 result = 0, mask = 0;
241 char *flagstr, *strpos = NULL;
243 while ((flagstr = strtok_r(s, ",", &strpos))) {
246 while (*flagstr == ' ' || *flagstr == '\t')
250 if (!strncasecmp(flagstr, "no", 2)) {
255 for (int i = 0; flagdesc[i].name; i++) {
256 if (!strcasecmp(flagstr, flagdesc[i].name)) {
257 mask |= flagdesc[i].flag;
259 result |= flagdesc[i].flag;
271 std::string writeFlagString(u32 flags, const FlagDesc *flagdesc, u32 flagmask)
275 for (int i = 0; flagdesc[i].name; i++) {
276 if (flagmask & flagdesc[i].flag) {
277 if (!(flags & flagdesc[i].flag))
280 result += flagdesc[i].name;
285 size_t len = result.length();
287 result.erase(len - 2, 2);
292 size_t mystrlcpy(char *dst, const char *src, size_t size)
294 size_t srclen = strlen(src) + 1;
295 size_t copylen = MYMIN(srclen, size);
298 memcpy(dst, src, copylen);
299 dst[copylen - 1] = '\0';
305 char *mystrtok_r(char *s, const char *sep, char **lasts)
312 while (*s && strchr(sep, *s))
320 if (strchr(sep, *t)) {
331 u64 read_seed(const char *str)
336 if (str[0] == '0' && str[1] == 'x')
337 num = strtoull(str, &endptr, 16);
339 num = strtoull(str, &endptr, 10);
342 num = murmur_hash_64_ua(str, (int)strlen(str), 0x1337);
347 bool parseColorString(const std::string &value, video::SColor &color, bool quiet)
352 success = parseHexColorString(value, color);
354 success = parseNamedColorString(value, color);
356 if (!success && !quiet)
357 errorstream << "Invalid color: \"" << value << "\"" << std::endl;
362 static bool parseHexColorString(const std::string &value, video::SColor &color)
364 unsigned char components[] = { 0x00, 0x00, 0x00, 0xff }; // R,G,B,A
369 size_t len = value.size();
372 if (len == 9 || len == 7) // #RRGGBBAA or #RRGGBB
374 else if (len == 5 || len == 4) // #RGBA or #RGB
381 for (size_t pos = 1, cc = 0; pos < len; pos++, cc++) {
382 assert(cc < sizeof components / sizeof components[0]);
385 if (!hex_digit_decode(value[pos], d)) {
389 components[cc] = (d & 0xf) << 4 | (d & 0xf);
391 unsigned char d1, d2;
392 if (!hex_digit_decode(value[pos], d1) ||
393 !hex_digit_decode(value[pos+1], d2)) {
397 components[cc] = (d1 & 0xf) << 4 | (d2 & 0xf);
398 pos++; // skip the second digit -- it's already used
403 color.setRed(components[0]);
404 color.setGreen(components[1]);
405 color.setBlue(components[2]);
406 color.setAlpha(components[3]);
412 struct ColorContainer {
414 std::map<const std::string, u32> colors;
417 ColorContainer::ColorContainer()
419 colors["aliceblue"] = 0xf0f8ff;
420 colors["antiquewhite"] = 0xfaebd7;
421 colors["aqua"] = 0x00ffff;
422 colors["aquamarine"] = 0x7fffd4;
423 colors["azure"] = 0xf0ffff;
424 colors["beige"] = 0xf5f5dc;
425 colors["bisque"] = 0xffe4c4;
426 colors["black"] = 00000000;
427 colors["blanchedalmond"] = 0xffebcd;
428 colors["blue"] = 0x0000ff;
429 colors["blueviolet"] = 0x8a2be2;
430 colors["brown"] = 0xa52a2a;
431 colors["burlywood"] = 0xdeb887;
432 colors["cadetblue"] = 0x5f9ea0;
433 colors["chartreuse"] = 0x7fff00;
434 colors["chocolate"] = 0xd2691e;
435 colors["coral"] = 0xff7f50;
436 colors["cornflowerblue"] = 0x6495ed;
437 colors["cornsilk"] = 0xfff8dc;
438 colors["crimson"] = 0xdc143c;
439 colors["cyan"] = 0x00ffff;
440 colors["darkblue"] = 0x00008b;
441 colors["darkcyan"] = 0x008b8b;
442 colors["darkgoldenrod"] = 0xb8860b;
443 colors["darkgray"] = 0xa9a9a9;
444 colors["darkgreen"] = 0x006400;
445 colors["darkkhaki"] = 0xbdb76b;
446 colors["darkmagenta"] = 0x8b008b;
447 colors["darkolivegreen"] = 0x556b2f;
448 colors["darkorange"] = 0xff8c00;
449 colors["darkorchid"] = 0x9932cc;
450 colors["darkred"] = 0x8b0000;
451 colors["darksalmon"] = 0xe9967a;
452 colors["darkseagreen"] = 0x8fbc8f;
453 colors["darkslateblue"] = 0x483d8b;
454 colors["darkslategray"] = 0x2f4f4f;
455 colors["darkturquoise"] = 0x00ced1;
456 colors["darkviolet"] = 0x9400d3;
457 colors["deeppink"] = 0xff1493;
458 colors["deepskyblue"] = 0x00bfff;
459 colors["dimgray"] = 0x696969;
460 colors["dodgerblue"] = 0x1e90ff;
461 colors["firebrick"] = 0xb22222;
462 colors["floralwhite"] = 0xfffaf0;
463 colors["forestgreen"] = 0x228b22;
464 colors["fuchsia"] = 0xff00ff;
465 colors["gainsboro"] = 0xdcdcdc;
466 colors["ghostwhite"] = 0xf8f8ff;
467 colors["gold"] = 0xffd700;
468 colors["goldenrod"] = 0xdaa520;
469 colors["gray"] = 0x808080;
470 colors["green"] = 0x008000;
471 colors["greenyellow"] = 0xadff2f;
472 colors["honeydew"] = 0xf0fff0;
473 colors["hotpink"] = 0xff69b4;
474 colors["indianred "] = 0xcd5c5c;
475 colors["indigo "] = 0x4b0082;
476 colors["ivory"] = 0xfffff0;
477 colors["khaki"] = 0xf0e68c;
478 colors["lavender"] = 0xe6e6fa;
479 colors["lavenderblush"] = 0xfff0f5;
480 colors["lawngreen"] = 0x7cfc00;
481 colors["lemonchiffon"] = 0xfffacd;
482 colors["lightblue"] = 0xadd8e6;
483 colors["lightcoral"] = 0xf08080;
484 colors["lightcyan"] = 0xe0ffff;
485 colors["lightgoldenrodyellow"] = 0xfafad2;
486 colors["lightgray"] = 0xd3d3d3;
487 colors["lightgreen"] = 0x90ee90;
488 colors["lightpink"] = 0xffb6c1;
489 colors["lightsalmon"] = 0xffa07a;
490 colors["lightseagreen"] = 0x20b2aa;
491 colors["lightskyblue"] = 0x87cefa;
492 colors["lightslategray"] = 0x778899;
493 colors["lightsteelblue"] = 0xb0c4de;
494 colors["lightyellow"] = 0xffffe0;
495 colors["lime"] = 0x00ff00;
496 colors["limegreen"] = 0x32cd32;
497 colors["linen"] = 0xfaf0e6;
498 colors["magenta"] = 0xff00ff;
499 colors["maroon"] = 0x800000;
500 colors["mediumaquamarine"] = 0x66cdaa;
501 colors["mediumblue"] = 0x0000cd;
502 colors["mediumorchid"] = 0xba55d3;
503 colors["mediumpurple"] = 0x9370db;
504 colors["mediumseagreen"] = 0x3cb371;
505 colors["mediumslateblue"] = 0x7b68ee;
506 colors["mediumspringgreen"] = 0x00fa9a;
507 colors["mediumturquoise"] = 0x48d1cc;
508 colors["mediumvioletred"] = 0xc71585;
509 colors["midnightblue"] = 0x191970;
510 colors["mintcream"] = 0xf5fffa;
511 colors["mistyrose"] = 0xffe4e1;
512 colors["moccasin"] = 0xffe4b5;
513 colors["navajowhite"] = 0xffdead;
514 colors["navy"] = 0x000080;
515 colors["oldlace"] = 0xfdf5e6;
516 colors["olive"] = 0x808000;
517 colors["olivedrab"] = 0x6b8e23;
518 colors["orange"] = 0xffa500;
519 colors["orangered"] = 0xff4500;
520 colors["orchid"] = 0xda70d6;
521 colors["palegoldenrod"] = 0xeee8aa;
522 colors["palegreen"] = 0x98fb98;
523 colors["paleturquoise"] = 0xafeeee;
524 colors["palevioletred"] = 0xdb7093;
525 colors["papayawhip"] = 0xffefd5;
526 colors["peachpuff"] = 0xffdab9;
527 colors["peru"] = 0xcd853f;
528 colors["pink"] = 0xffc0cb;
529 colors["plum"] = 0xdda0dd;
530 colors["powderblue"] = 0xb0e0e6;
531 colors["purple"] = 0x800080;
532 colors["red"] = 0xff0000;
533 colors["rosybrown"] = 0xbc8f8f;
534 colors["royalblue"] = 0x4169e1;
535 colors["saddlebrown"] = 0x8b4513;
536 colors["salmon"] = 0xfa8072;
537 colors["sandybrown"] = 0xf4a460;
538 colors["seagreen"] = 0x2e8b57;
539 colors["seashell"] = 0xfff5ee;
540 colors["sienna"] = 0xa0522d;
541 colors["silver"] = 0xc0c0c0;
542 colors["skyblue"] = 0x87ceeb;
543 colors["slateblue"] = 0x6a5acd;
544 colors["slategray"] = 0x708090;
545 colors["snow"] = 0xfffafa;
546 colors["springgreen"] = 0x00ff7f;
547 colors["steelblue"] = 0x4682b4;
548 colors["tan"] = 0xd2b48c;
549 colors["teal"] = 0x008080;
550 colors["thistle"] = 0xd8bfd8;
551 colors["tomato"] = 0xff6347;
552 colors["turquoise"] = 0x40e0d0;
553 colors["violet"] = 0xee82ee;
554 colors["wheat"] = 0xf5deb3;
555 colors["white"] = 0xffffff;
556 colors["whitesmoke"] = 0xf5f5f5;
557 colors["yellow"] = 0xffff00;
558 colors["yellowgreen"] = 0x9acd32;
562 static const ColorContainer named_colors;
564 static bool parseNamedColorString(const std::string &value, video::SColor &color)
566 std::string color_name;
567 std::string alpha_string;
569 /* If the string has a # in it, assume this is the start of a specified
570 * alpha value (if it isn't the string is invalid and the error will be
571 * caught later on, either because the color name won't be found or the
572 * alpha value will fail conversion)
574 size_t alpha_pos = value.find('#');
575 if (alpha_pos != std::string::npos) {
576 color_name = value.substr(0, alpha_pos);
577 alpha_string = value.substr(alpha_pos + 1);
582 color_name = lowercase(value);
584 std::map<const std::string, unsigned>::const_iterator it;
585 it = named_colors.colors.find(color_name);
586 if (it == named_colors.colors.end())
589 u32 color_temp = it->second;
591 /* An empty string for alpha is ok (none of the color table entries
592 * have an alpha value either). Color strings without an alpha specified
593 * are interpreted as fully opaque
595 * For named colors the supplied alpha string (representing a hex value)
596 * must be exactly two digits. For example: colorname#08
598 if (!alpha_string.empty()) {
599 if (alpha_string.length() != 2)
602 unsigned char d1, d2;
603 if (!hex_digit_decode(alpha_string.at(0), d1)
604 || !hex_digit_decode(alpha_string.at(1), d2))
606 color_temp |= ((d1 & 0xf) << 4 | (d2 & 0xf)) << 24;
608 color_temp |= 0xff << 24; // Fully opaque
611 color = video::SColor(color_temp);
616 void str_replace(std::string &str, char from, char to)
618 std::replace(str.begin(), str.end(), from, to);