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.
30 #include "../base64.h"
32 #include "../porting.h"
34 static bool parseHexColorString(const std::string &value, video::SColor &color);
35 static bool parseNamedColorString(const std::string &value, video::SColor &color);
38 const wchar_t* wide_chars =
39 L" !\"#$%&'()*+,-./0123456789:;<=>?@"
40 L"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`"
41 L"abcdefghijklmnopqrstuvwxyz{|}~";
43 int wctomb(char *s, wchar_t wc)
45 for (unsigned int j = 0; j < (sizeof(wide_chars)/sizeof(wchar_t));j++) {
46 if (wc == wide_chars[j]) {
50 else if (wc == L'\n') {
58 int mbtowc(wchar_t *pwc, const char *s, size_t n)
60 std::wstring intermediate = narrow_to_wide(s);
62 if (intermediate.length() > 0) {
63 *pwc = intermediate[0];
71 std::wstring narrow_to_wide(const std::string& mbs) {
72 size_t wcl = mbs.size();
74 std::wstring retval = L"";
76 for (unsigned int i = 0; i < wcl; i++) {
77 if (((unsigned char) mbs[i] >31) &&
78 ((unsigned char) mbs[i] < 127)) {
80 retval += wide_chars[(unsigned char) mbs[i] -32];
83 else if (mbs[i] == '\n') {
92 std::wstring narrow_to_wide(const std::string& mbs)
94 size_t wcl = mbs.size();
95 Buffer<wchar_t> wcs(wcl+1);
96 size_t l = mbstowcs(*wcs, mbs.c_str(), wcl);
98 return L"<invalid multibyte string>";
106 std::string wide_to_narrow(const std::wstring& wcs) {
107 size_t mbl = wcs.size()*4;
109 std::string retval = "";
110 for (unsigned int i = 0; i < wcs.size(); i++) {
111 wchar_t char1 = (wchar_t) wcs[i];
113 if (char1 == L'\n') {
118 for (unsigned int j = 0; j < wcslen(wide_chars);j++) {
119 wchar_t char2 = (wchar_t) wide_chars[j];
121 if (char1 == char2) {
132 std::string wide_to_narrow(const std::wstring& wcs)
134 size_t mbl = wcs.size()*4;
135 SharedBuffer<char> mbs(mbl+1);
136 size_t l = wcstombs(*mbs, wcs.c_str(), mbl);
137 if(l == (size_t)(-1)) {
138 return "Character conversion failed!";
147 // Get an sha-1 hash of the player's name combined with
148 // the password entered. That's what the server uses as
149 // their password. (Exception : if the password field is
150 // blank, we send a blank password - this is for backwards
151 // compatibility with password-less players).
152 std::string translatePassword(std::string playername, std::wstring password)
154 if(password.length() == 0)
157 std::string slt = playername + wide_to_narrow(password);
159 sha1.addBytes(slt.c_str(), slt.length());
160 unsigned char *digest = sha1.getDigest();
161 std::string pwd = base64_encode(digest, 20);
166 std::string urlencode(std::string str)
168 // Encodes non-unreserved URI characters by a percent sign
169 // followed by two hex digits. See RFC 3986, section 2.3.
170 static const char url_hex_chars[] = "0123456789ABCDEF";
171 std::ostringstream oss(std::ios::binary);
172 for (u32 i = 0; i < str.size(); i++) {
173 unsigned char c = str[i];
174 if (isalnum(c) || c == '-' || c == '.' || c == '_' || c == '~')
178 << url_hex_chars[(c & 0xf0) >> 4]
179 << url_hex_chars[c & 0x0f];
184 std::string urldecode(std::string str)
186 // Inverse of urlencode
187 std::ostringstream oss(std::ios::binary);
188 for (u32 i = 0; i < str.size(); i++) {
189 unsigned char highvalue, lowvalue;
191 hex_digit_decode(str[i+1], highvalue) &&
192 hex_digit_decode(str[i+2], lowvalue)) {
193 oss << (char) ((highvalue << 4) | lowvalue);
202 u32 readFlagString(std::string str, const FlagDesc *flagdesc, u32 *flagmask)
204 u32 result = 0, mask = 0;
206 char *flagstr, *strpos = NULL;
208 while ((flagstr = strtok_r(s, ",", &strpos))) {
211 while (*flagstr == ' ' || *flagstr == '\t')
215 if (!strncasecmp(flagstr, "no", 2)) {
220 for (int i = 0; flagdesc[i].name; i++) {
221 if (!strcasecmp(flagstr, flagdesc[i].name)) {
222 mask |= flagdesc[i].flag;
224 result |= flagdesc[i].flag;
236 std::string writeFlagString(u32 flags, const FlagDesc *flagdesc, u32 flagmask)
240 for (int i = 0; flagdesc[i].name; i++) {
241 if (flagmask & flagdesc[i].flag) {
242 if (!(flags & flagdesc[i].flag))
245 result += flagdesc[i].name;
250 size_t len = result.length();
252 result.erase(len - 2, 2);
257 size_t mystrlcpy(char *dst, const char *src, size_t size)
259 size_t srclen = strlen(src) + 1;
260 size_t copylen = MYMIN(srclen, size);
263 memcpy(dst, src, copylen);
264 dst[copylen - 1] = '\0';
270 char *mystrtok_r(char *s, const char *sep, char **lasts)
277 while (*s && strchr(sep, *s))
285 if (strchr(sep, *t)) {
296 u64 read_seed(const char *str)
301 if (str[0] == '0' && str[1] == 'x')
302 num = strtoull(str, &endptr, 16);
304 num = strtoull(str, &endptr, 10);
307 num = murmur_hash_64_ua(str, (int)strlen(str), 0x1337);
312 bool parseColorString(const std::string &value, video::SColor &color, bool quiet)
317 success = parseHexColorString(value, color);
319 success = parseNamedColorString(value, color);
321 if (!success && !quiet)
322 errorstream << "Invalid color: \"" << value << "\"" << std::endl;
327 static bool parseHexColorString(const std::string &value, video::SColor &color)
329 unsigned char components[] = { 0x00, 0x00, 0x00, 0xff }; // R,G,B,A
334 size_t len = value.size();
337 if (len == 9 || len == 7) // #RRGGBBAA or #RRGGBB
339 else if (len == 5 || len == 4) // #RGBA or #RGB
346 for (size_t pos = 1, cc = 0; pos < len; pos++, cc++) {
347 assert(cc < sizeof components / sizeof components[0]);
350 if (!hex_digit_decode(value[pos], d)) {
354 components[cc] = (d & 0xf) << 4 | (d & 0xf);
356 unsigned char d1, d2;
357 if (!hex_digit_decode(value[pos], d1) ||
358 !hex_digit_decode(value[pos+1], d2)) {
362 components[cc] = (d1 & 0xf) << 4 | (d2 & 0xf);
363 pos++; // skip the second digit -- it's already used
368 color.setRed(components[0]);
369 color.setGreen(components[1]);
370 color.setBlue(components[2]);
371 color.setAlpha(components[3]);
377 struct ColorContainer {
379 std::map<const std::string, u32> colors;
382 ColorContainer::ColorContainer()
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["darkkhaki"] = 0xbdb76b;
411 colors["darkmagenta"] = 0x8b008b;
412 colors["darkolivegreen"] = 0x556b2f;
413 colors["darkorange"] = 0xff8c00;
414 colors["darkorchid"] = 0x9932cc;
415 colors["darkred"] = 0x8b0000;
416 colors["darksalmon"] = 0xe9967a;
417 colors["darkseagreen"] = 0x8fbc8f;
418 colors["darkslateblue"] = 0x483d8b;
419 colors["darkslategray"] = 0x2f4f4f;
420 colors["darkturquoise"] = 0x00ced1;
421 colors["darkviolet"] = 0x9400d3;
422 colors["deeppink"] = 0xff1493;
423 colors["deepskyblue"] = 0x00bfff;
424 colors["dimgray"] = 0x696969;
425 colors["dodgerblue"] = 0x1e90ff;
426 colors["firebrick"] = 0xb22222;
427 colors["floralwhite"] = 0xfffaf0;
428 colors["forestgreen"] = 0x228b22;
429 colors["fuchsia"] = 0xff00ff;
430 colors["gainsboro"] = 0xdcdcdc;
431 colors["ghostwhite"] = 0xf8f8ff;
432 colors["gold"] = 0xffd700;
433 colors["goldenrod"] = 0xdaa520;
434 colors["gray"] = 0x808080;
435 colors["green"] = 0x008000;
436 colors["greenyellow"] = 0xadff2f;
437 colors["honeydew"] = 0xf0fff0;
438 colors["hotpink"] = 0xff69b4;
439 colors["indianred "] = 0xcd5c5c;
440 colors["indigo "] = 0x4b0082;
441 colors["ivory"] = 0xfffff0;
442 colors["khaki"] = 0xf0e68c;
443 colors["lavender"] = 0xe6e6fa;
444 colors["lavenderblush"] = 0xfff0f5;
445 colors["lawngreen"] = 0x7cfc00;
446 colors["lemonchiffon"] = 0xfffacd;
447 colors["lightblue"] = 0xadd8e6;
448 colors["lightcoral"] = 0xf08080;
449 colors["lightcyan"] = 0xe0ffff;
450 colors["lightgoldenrodyellow"] = 0xfafad2;
451 colors["lightgray"] = 0xd3d3d3;
452 colors["lightgreen"] = 0x90ee90;
453 colors["lightpink"] = 0xffb6c1;
454 colors["lightsalmon"] = 0xffa07a;
455 colors["lightseagreen"] = 0x20b2aa;
456 colors["lightskyblue"] = 0x87cefa;
457 colors["lightslategray"] = 0x778899;
458 colors["lightsteelblue"] = 0xb0c4de;
459 colors["lightyellow"] = 0xffffe0;
460 colors["lime"] = 0x00ff00;
461 colors["limegreen"] = 0x32cd32;
462 colors["linen"] = 0xfaf0e6;
463 colors["magenta"] = 0xff00ff;
464 colors["maroon"] = 0x800000;
465 colors["mediumaquamarine"] = 0x66cdaa;
466 colors["mediumblue"] = 0x0000cd;
467 colors["mediumorchid"] = 0xba55d3;
468 colors["mediumpurple"] = 0x9370db;
469 colors["mediumseagreen"] = 0x3cb371;
470 colors["mediumslateblue"] = 0x7b68ee;
471 colors["mediumspringgreen"] = 0x00fa9a;
472 colors["mediumturquoise"] = 0x48d1cc;
473 colors["mediumvioletred"] = 0xc71585;
474 colors["midnightblue"] = 0x191970;
475 colors["mintcream"] = 0xf5fffa;
476 colors["mistyrose"] = 0xffe4e1;
477 colors["moccasin"] = 0xffe4b5;
478 colors["navajowhite"] = 0xffdead;
479 colors["navy"] = 0x000080;
480 colors["oldlace"] = 0xfdf5e6;
481 colors["olive"] = 0x808000;
482 colors["olivedrab"] = 0x6b8e23;
483 colors["orange"] = 0xffa500;
484 colors["orangered"] = 0xff4500;
485 colors["orchid"] = 0xda70d6;
486 colors["palegoldenrod"] = 0xeee8aa;
487 colors["palegreen"] = 0x98fb98;
488 colors["paleturquoise"] = 0xafeeee;
489 colors["palevioletred"] = 0xdb7093;
490 colors["papayawhip"] = 0xffefd5;
491 colors["peachpuff"] = 0xffdab9;
492 colors["peru"] = 0xcd853f;
493 colors["pink"] = 0xffc0cb;
494 colors["plum"] = 0xdda0dd;
495 colors["powderblue"] = 0xb0e0e6;
496 colors["purple"] = 0x800080;
497 colors["red"] = 0xff0000;
498 colors["rosybrown"] = 0xbc8f8f;
499 colors["royalblue"] = 0x4169e1;
500 colors["saddlebrown"] = 0x8b4513;
501 colors["salmon"] = 0xfa8072;
502 colors["sandybrown"] = 0xf4a460;
503 colors["seagreen"] = 0x2e8b57;
504 colors["seashell"] = 0xfff5ee;
505 colors["sienna"] = 0xa0522d;
506 colors["silver"] = 0xc0c0c0;
507 colors["skyblue"] = 0x87ceeb;
508 colors["slateblue"] = 0x6a5acd;
509 colors["slategray"] = 0x708090;
510 colors["snow"] = 0xfffafa;
511 colors["springgreen"] = 0x00ff7f;
512 colors["steelblue"] = 0x4682b4;
513 colors["tan"] = 0xd2b48c;
514 colors["teal"] = 0x008080;
515 colors["thistle"] = 0xd8bfd8;
516 colors["tomato"] = 0xff6347;
517 colors["turquoise"] = 0x40e0d0;
518 colors["violet"] = 0xee82ee;
519 colors["wheat"] = 0xf5deb3;
520 colors["white"] = 0xffffff;
521 colors["whitesmoke"] = 0xf5f5f5;
522 colors["yellow"] = 0xffff00;
523 colors["yellowgreen"] = 0x9acd32;
527 static const ColorContainer named_colors;
529 static bool parseNamedColorString(const std::string &value, video::SColor &color)
531 std::string color_name;
532 std::string alpha_string;
534 /* If the string has a # in it, assume this is the start of a specified
535 * alpha value (if it isn't the string is invalid and the error will be
536 * caught later on, either because the color name won't be found or the
537 * alpha value will fail conversion)
539 size_t alpha_pos = value.find('#');
540 if (alpha_pos != std::string::npos) {
541 color_name = value.substr(0, alpha_pos);
542 alpha_string = value.substr(alpha_pos + 1);
547 color_name = lowercase(value);
549 std::map<const std::string, unsigned>::const_iterator it;
550 it = named_colors.colors.find(color_name);
551 if (it == named_colors.colors.end())
554 u32 color_temp = it->second;
556 /* An empty string for alpha is ok (none of the color table entries
557 * have an alpha value either). Color strings without an alpha specified
558 * are interpreted as fully opaque
560 * For named colors the supplied alpha string (representing a hex value)
561 * must be exactly two digits. For example: colorname#08
563 if (!alpha_string.empty()) {
564 if (alpha_string.length() != 2)
567 unsigned char d1, d2;
568 if (!hex_digit_decode(alpha_string.at(0), d1)
569 || !hex_digit_decode(alpha_string.at(1), d2))
571 color_temp |= ((d1 & 0xf) << 4 | (d2 & 0xf)) << 24;
573 color_temp |= 0xff << 24; // Fully opaque
576 color = video::SColor(color_temp);