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.
26 #include "../base64.h"
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);
43 const wchar_t* wide_chars =
44 L" !\"#$%&'()*+,-./0123456789:;<=>?@"
45 L"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`"
46 L"abcdefghijklmnopqrstuvwxyz{|}~";
48 int wctomb(char *s, wchar_t wc)
50 for (unsigned int j = 0; j < (sizeof(wide_chars)/sizeof(wchar_t));j++) {
51 if (wc == wide_chars[j]) {
55 else if (wc == L'\n') {
63 int mbtowc(wchar_t *pwc, const char *s, size_t n)
65 wchar_t *intermediate = narrow_to_wide(s);
67 if (intermediate.length() > 0) {
68 *pwc = intermediate[0];
74 // You must free the returned string!
75 const wchar_t *narrow_to_wide_c(const char *mbs)
77 size_t mbl = strlen(mbs);
78 wchar_t wcs = new wchar_t[mbl + 1];
80 for (size_t i = 0; i < mbl; i++) {
81 if (((unsigned char) mbs[i] > 31) &&
82 ((unsigned char) mbs[i] < 127)) {
83 wcs[i] = wide_chars[(unsigned char) mbs[i] - 32];
86 else if (mbs[i] == '\n') {
96 // You must free the returned string!
97 const wchar_t *narrow_to_wide_c(const char *mbs)
101 int wcl = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) mbs, -1, NULL, 0);
104 wcs = new wchar_t[wcl];
105 MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) mbs, -1, (WCHAR *) wcs, wcl);
107 size_t wcl = mbstowcs(NULL, mbs, 0);
108 if (wcl == (size_t) -1)
110 wcs = new wchar_t[wcl + 1];
111 size_t l = mbstowcs(wcs, mbs, wcl);
112 assert(l != (size_t) -1); // Should never happen if the last call worked
121 std::wstring narrow_to_wide(const std::string& mbs)
123 const wchar_t *wcs = narrow_to_wide_c(mbs.c_str());
125 return L"<invalid multibyte string>";
126 std::wstring wstr(wcs);
132 std::string wide_to_narrow(const std::wstring& wcs) {
133 size_t mbl = wcs.size()*4;
135 std::string retval = "";
136 for (unsigned int i = 0; i < wcs.size(); i++) {
137 wchar_t char1 = (wchar_t) wcs[i];
139 if (char1 == L'\n') {
144 for (unsigned int j = 0; j < wcslen(wide_chars);j++) {
145 wchar_t char2 = (wchar_t) wide_chars[j];
147 if (char1 == char2) {
158 std::string wide_to_narrow(const std::wstring& wcs)
160 size_t mbl = wcs.size()*4;
161 SharedBuffer<char> mbs(mbl+1);
162 size_t l = wcstombs(*mbs, wcs.c_str(), mbl);
163 if(l == (size_t)(-1)) {
164 return "Character conversion failed!";
173 // Get an sha-1 hash of the player's name combined with
174 // the password entered. That's what the server uses as
175 // their password. (Exception : if the password field is
176 // blank, we send a blank password - this is for backwards
177 // compatibility with password-less players).
178 std::string translatePassword(std::string playername, std::wstring password)
180 if(password.length() == 0)
183 std::string slt = playername + wide_to_narrow(password);
185 sha1.addBytes(slt.c_str(), slt.length());
186 unsigned char *digest = sha1.getDigest();
187 std::string pwd = base64_encode(digest, 20);
192 std::string urlencode(std::string str)
194 // Encodes non-unreserved URI characters by a percent sign
195 // followed by two hex digits. See RFC 3986, section 2.3.
196 static const char url_hex_chars[] = "0123456789ABCDEF";
197 std::ostringstream oss(std::ios::binary);
198 for (u32 i = 0; i < str.size(); i++) {
199 unsigned char c = str[i];
200 if (isalnum(c) || c == '-' || c == '.' || c == '_' || c == '~')
204 << url_hex_chars[(c & 0xf0) >> 4]
205 << url_hex_chars[c & 0x0f];
210 std::string urldecode(std::string str)
212 // Inverse of urlencode
213 std::ostringstream oss(std::ios::binary);
214 for (u32 i = 0; i < str.size(); i++) {
215 unsigned char highvalue, lowvalue;
217 hex_digit_decode(str[i+1], highvalue) &&
218 hex_digit_decode(str[i+2], lowvalue)) {
219 oss << (char) ((highvalue << 4) | lowvalue);
228 u32 readFlagString(std::string str, const FlagDesc *flagdesc, u32 *flagmask)
230 u32 result = 0, mask = 0;
232 char *flagstr, *strpos = NULL;
234 while ((flagstr = strtok_r(s, ",", &strpos))) {
237 while (*flagstr == ' ' || *flagstr == '\t')
241 if (!strncasecmp(flagstr, "no", 2)) {
246 for (int i = 0; flagdesc[i].name; i++) {
247 if (!strcasecmp(flagstr, flagdesc[i].name)) {
248 mask |= flagdesc[i].flag;
250 result |= flagdesc[i].flag;
262 std::string writeFlagString(u32 flags, const FlagDesc *flagdesc, u32 flagmask)
266 for (int i = 0; flagdesc[i].name; i++) {
267 if (flagmask & flagdesc[i].flag) {
268 if (!(flags & flagdesc[i].flag))
271 result += flagdesc[i].name;
276 size_t len = result.length();
278 result.erase(len - 2, 2);
283 size_t mystrlcpy(char *dst, const char *src, size_t size)
285 size_t srclen = strlen(src) + 1;
286 size_t copylen = MYMIN(srclen, size);
289 memcpy(dst, src, copylen);
290 dst[copylen - 1] = '\0';
296 char *mystrtok_r(char *s, const char *sep, char **lasts)
303 while (*s && strchr(sep, *s))
311 if (strchr(sep, *t)) {
322 u64 read_seed(const char *str)
327 if (str[0] == '0' && str[1] == 'x')
328 num = strtoull(str, &endptr, 16);
330 num = strtoull(str, &endptr, 10);
333 num = murmur_hash_64_ua(str, (int)strlen(str), 0x1337);
338 bool parseColorString(const std::string &value, video::SColor &color, bool quiet)
343 success = parseHexColorString(value, color);
345 success = parseNamedColorString(value, color);
347 if (!success && !quiet)
348 errorstream << "Invalid color: \"" << value << "\"" << std::endl;
353 static bool parseHexColorString(const std::string &value, video::SColor &color)
355 unsigned char components[] = { 0x00, 0x00, 0x00, 0xff }; // R,G,B,A
360 size_t len = value.size();
363 if (len == 9 || len == 7) // #RRGGBBAA or #RRGGBB
365 else if (len == 5 || len == 4) // #RGBA or #RGB
372 for (size_t pos = 1, cc = 0; pos < len; pos++, cc++) {
373 assert(cc < sizeof components / sizeof components[0]);
376 if (!hex_digit_decode(value[pos], d)) {
380 components[cc] = (d & 0xf) << 4 | (d & 0xf);
382 unsigned char d1, d2;
383 if (!hex_digit_decode(value[pos], d1) ||
384 !hex_digit_decode(value[pos+1], d2)) {
388 components[cc] = (d1 & 0xf) << 4 | (d2 & 0xf);
389 pos++; // skip the second digit -- it's already used
394 color.setRed(components[0]);
395 color.setGreen(components[1]);
396 color.setBlue(components[2]);
397 color.setAlpha(components[3]);
403 struct ColorContainer {
405 std::map<const std::string, u32> colors;
408 ColorContainer::ColorContainer()
410 colors["aliceblue"] = 0xf0f8ff;
411 colors["antiquewhite"] = 0xfaebd7;
412 colors["aqua"] = 0x00ffff;
413 colors["aquamarine"] = 0x7fffd4;
414 colors["azure"] = 0xf0ffff;
415 colors["beige"] = 0xf5f5dc;
416 colors["bisque"] = 0xffe4c4;
417 colors["black"] = 00000000;
418 colors["blanchedalmond"] = 0xffebcd;
419 colors["blue"] = 0x0000ff;
420 colors["blueviolet"] = 0x8a2be2;
421 colors["brown"] = 0xa52a2a;
422 colors["burlywood"] = 0xdeb887;
423 colors["cadetblue"] = 0x5f9ea0;
424 colors["chartreuse"] = 0x7fff00;
425 colors["chocolate"] = 0xd2691e;
426 colors["coral"] = 0xff7f50;
427 colors["cornflowerblue"] = 0x6495ed;
428 colors["cornsilk"] = 0xfff8dc;
429 colors["crimson"] = 0xdc143c;
430 colors["cyan"] = 0x00ffff;
431 colors["darkblue"] = 0x00008b;
432 colors["darkcyan"] = 0x008b8b;
433 colors["darkgoldenrod"] = 0xb8860b;
434 colors["darkgray"] = 0xa9a9a9;
435 colors["darkgreen"] = 0x006400;
436 colors["darkkhaki"] = 0xbdb76b;
437 colors["darkmagenta"] = 0x8b008b;
438 colors["darkolivegreen"] = 0x556b2f;
439 colors["darkorange"] = 0xff8c00;
440 colors["darkorchid"] = 0x9932cc;
441 colors["darkred"] = 0x8b0000;
442 colors["darksalmon"] = 0xe9967a;
443 colors["darkseagreen"] = 0x8fbc8f;
444 colors["darkslateblue"] = 0x483d8b;
445 colors["darkslategray"] = 0x2f4f4f;
446 colors["darkturquoise"] = 0x00ced1;
447 colors["darkviolet"] = 0x9400d3;
448 colors["deeppink"] = 0xff1493;
449 colors["deepskyblue"] = 0x00bfff;
450 colors["dimgray"] = 0x696969;
451 colors["dodgerblue"] = 0x1e90ff;
452 colors["firebrick"] = 0xb22222;
453 colors["floralwhite"] = 0xfffaf0;
454 colors["forestgreen"] = 0x228b22;
455 colors["fuchsia"] = 0xff00ff;
456 colors["gainsboro"] = 0xdcdcdc;
457 colors["ghostwhite"] = 0xf8f8ff;
458 colors["gold"] = 0xffd700;
459 colors["goldenrod"] = 0xdaa520;
460 colors["gray"] = 0x808080;
461 colors["green"] = 0x008000;
462 colors["greenyellow"] = 0xadff2f;
463 colors["honeydew"] = 0xf0fff0;
464 colors["hotpink"] = 0xff69b4;
465 colors["indianred "] = 0xcd5c5c;
466 colors["indigo "] = 0x4b0082;
467 colors["ivory"] = 0xfffff0;
468 colors["khaki"] = 0xf0e68c;
469 colors["lavender"] = 0xe6e6fa;
470 colors["lavenderblush"] = 0xfff0f5;
471 colors["lawngreen"] = 0x7cfc00;
472 colors["lemonchiffon"] = 0xfffacd;
473 colors["lightblue"] = 0xadd8e6;
474 colors["lightcoral"] = 0xf08080;
475 colors["lightcyan"] = 0xe0ffff;
476 colors["lightgoldenrodyellow"] = 0xfafad2;
477 colors["lightgray"] = 0xd3d3d3;
478 colors["lightgreen"] = 0x90ee90;
479 colors["lightpink"] = 0xffb6c1;
480 colors["lightsalmon"] = 0xffa07a;
481 colors["lightseagreen"] = 0x20b2aa;
482 colors["lightskyblue"] = 0x87cefa;
483 colors["lightslategray"] = 0x778899;
484 colors["lightsteelblue"] = 0xb0c4de;
485 colors["lightyellow"] = 0xffffe0;
486 colors["lime"] = 0x00ff00;
487 colors["limegreen"] = 0x32cd32;
488 colors["linen"] = 0xfaf0e6;
489 colors["magenta"] = 0xff00ff;
490 colors["maroon"] = 0x800000;
491 colors["mediumaquamarine"] = 0x66cdaa;
492 colors["mediumblue"] = 0x0000cd;
493 colors["mediumorchid"] = 0xba55d3;
494 colors["mediumpurple"] = 0x9370db;
495 colors["mediumseagreen"] = 0x3cb371;
496 colors["mediumslateblue"] = 0x7b68ee;
497 colors["mediumspringgreen"] = 0x00fa9a;
498 colors["mediumturquoise"] = 0x48d1cc;
499 colors["mediumvioletred"] = 0xc71585;
500 colors["midnightblue"] = 0x191970;
501 colors["mintcream"] = 0xf5fffa;
502 colors["mistyrose"] = 0xffe4e1;
503 colors["moccasin"] = 0xffe4b5;
504 colors["navajowhite"] = 0xffdead;
505 colors["navy"] = 0x000080;
506 colors["oldlace"] = 0xfdf5e6;
507 colors["olive"] = 0x808000;
508 colors["olivedrab"] = 0x6b8e23;
509 colors["orange"] = 0xffa500;
510 colors["orangered"] = 0xff4500;
511 colors["orchid"] = 0xda70d6;
512 colors["palegoldenrod"] = 0xeee8aa;
513 colors["palegreen"] = 0x98fb98;
514 colors["paleturquoise"] = 0xafeeee;
515 colors["palevioletred"] = 0xdb7093;
516 colors["papayawhip"] = 0xffefd5;
517 colors["peachpuff"] = 0xffdab9;
518 colors["peru"] = 0xcd853f;
519 colors["pink"] = 0xffc0cb;
520 colors["plum"] = 0xdda0dd;
521 colors["powderblue"] = 0xb0e0e6;
522 colors["purple"] = 0x800080;
523 colors["red"] = 0xff0000;
524 colors["rosybrown"] = 0xbc8f8f;
525 colors["royalblue"] = 0x4169e1;
526 colors["saddlebrown"] = 0x8b4513;
527 colors["salmon"] = 0xfa8072;
528 colors["sandybrown"] = 0xf4a460;
529 colors["seagreen"] = 0x2e8b57;
530 colors["seashell"] = 0xfff5ee;
531 colors["sienna"] = 0xa0522d;
532 colors["silver"] = 0xc0c0c0;
533 colors["skyblue"] = 0x87ceeb;
534 colors["slateblue"] = 0x6a5acd;
535 colors["slategray"] = 0x708090;
536 colors["snow"] = 0xfffafa;
537 colors["springgreen"] = 0x00ff7f;
538 colors["steelblue"] = 0x4682b4;
539 colors["tan"] = 0xd2b48c;
540 colors["teal"] = 0x008080;
541 colors["thistle"] = 0xd8bfd8;
542 colors["tomato"] = 0xff6347;
543 colors["turquoise"] = 0x40e0d0;
544 colors["violet"] = 0xee82ee;
545 colors["wheat"] = 0xf5deb3;
546 colors["white"] = 0xffffff;
547 colors["whitesmoke"] = 0xf5f5f5;
548 colors["yellow"] = 0xffff00;
549 colors["yellowgreen"] = 0x9acd32;
553 static const ColorContainer named_colors;
555 static bool parseNamedColorString(const std::string &value, video::SColor &color)
557 std::string color_name;
558 std::string alpha_string;
560 /* If the string has a # in it, assume this is the start of a specified
561 * alpha value (if it isn't the string is invalid and the error will be
562 * caught later on, either because the color name won't be found or the
563 * alpha value will fail conversion)
565 size_t alpha_pos = value.find('#');
566 if (alpha_pos != std::string::npos) {
567 color_name = value.substr(0, alpha_pos);
568 alpha_string = value.substr(alpha_pos + 1);
573 color_name = lowercase(value);
575 std::map<const std::string, unsigned>::const_iterator it;
576 it = named_colors.colors.find(color_name);
577 if (it == named_colors.colors.end())
580 u32 color_temp = it->second;
582 /* An empty string for alpha is ok (none of the color table entries
583 * have an alpha value either). Color strings without an alpha specified
584 * are interpreted as fully opaque
586 * For named colors the supplied alpha string (representing a hex value)
587 * must be exactly two digits. For example: colorname#08
589 if (!alpha_string.empty()) {
590 if (alpha_string.length() != 2)
593 unsigned char d1, d2;
594 if (!hex_digit_decode(alpha_string.at(0), d1)
595 || !hex_digit_decode(alpha_string.at(1), d2))
597 color_temp |= ((d1 & 0xf) << 4 | (d2 & 0xf)) << 24;
599 color_temp |= 0xff << 24; // Fully opaque
602 color = video::SColor(color_temp);
607 void str_replace(std::string &str, char from, char to)
609 std::replace(str.begin(), str.end(), from, to);