]> git.lizzy.rs Git - minetest.git/blob - src/util/string.cpp
Added names colours and refactored parseColorString()
[minetest.git] / src / util / string.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "string.h"
21 #include "pointer.h"
22 #include "numeric.h"
23 #include "log.h"
24
25 #include <sstream>
26 #include <iomanip>
27 #include <map>
28
29 #include "../sha1.h"
30 #include "../base64.h"
31 #include "../hex.h"
32 #include "../porting.h"
33
34 static bool parseHexColorString(const std::string &value, video::SColor &color);
35 static bool parseNamedColorString(const std::string &value, video::SColor &color);
36
37 #ifdef __ANDROID__
38 const wchar_t* wide_chars =
39         L" !\"#$%&'()*+,-./0123456789:;<=>?@"
40         L"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`"
41         L"abcdefghijklmnopqrstuvwxyz{|}~";
42
43 int wctomb(char *s, wchar_t wc)
44 {
45         for (unsigned int j = 0; j < (sizeof(wide_chars)/sizeof(wchar_t));j++) {
46                 if (wc == wide_chars[j]) {
47                         *s = (char) (j+32);
48                         return 1;
49                 }
50                 else if (wc == L'\n') {
51                         *s = '\n';
52                         return 1;
53                 }
54         }
55         return -1;
56 }
57
58 int mbtowc(wchar_t *pwc, const char *s, size_t n)
59 {
60         std::wstring intermediate = narrow_to_wide(s);
61
62         if (intermediate.length() > 0) {
63                 *pwc = intermediate[0];
64                 return 1;
65         }
66         else {
67                 return -1;
68         }
69 }
70
71 std::wstring narrow_to_wide(const std::string& mbs) {
72         size_t wcl = mbs.size();
73
74         std::wstring retval = L"";
75
76         for (unsigned int i = 0; i < wcl; i++) {
77                 if (((unsigned char) mbs[i] >31) &&
78                  ((unsigned char) mbs[i] < 127)) {
79
80                         retval += wide_chars[(unsigned char) mbs[i] -32];
81                 }
82                 //handle newline
83                 else if (mbs[i] == '\n') {
84                         retval += L'\n';
85                 }
86         }
87
88         return retval;
89 }
90 #else
91
92 std::wstring narrow_to_wide(const std::string& mbs)
93 {
94         size_t wcl = mbs.size();
95         Buffer<wchar_t> wcs(wcl+1);
96         size_t l = mbstowcs(*wcs, mbs.c_str(), wcl);
97         if(l == (size_t)(-1))
98                 return L"<invalid multibyte string>";
99         wcs[l] = 0;
100         return *wcs;
101 }
102
103 #endif
104
105 #ifdef __ANDROID__
106 std::string wide_to_narrow(const std::wstring& wcs) {
107         size_t mbl = wcs.size()*4;
108
109         std::string retval = "";
110         for (unsigned int i = 0; i < wcs.size(); i++) {
111                 wchar_t char1 = (wchar_t) wcs[i];
112
113                 if (char1 == L'\n') {
114                         retval += '\n';
115                         continue;
116                 }
117
118                 for (unsigned int j = 0; j < wcslen(wide_chars);j++) {
119                         wchar_t char2 = (wchar_t) wide_chars[j];
120
121                         if (char1 == char2) {
122                                 char toadd = (j+32);
123                                 retval += toadd;
124                                 break;
125                         }
126                 }
127         }
128
129         return retval;
130 }
131 #else
132 std::string wide_to_narrow(const std::wstring& wcs)
133 {
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!";
139         }
140         else
141                 mbs[l] = 0;
142         return *mbs;
143 }
144
145 #endif
146
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)
153 {
154         if(password.length() == 0)
155                 return "";
156
157         std::string slt = playername + wide_to_narrow(password);
158         SHA1 sha1;
159         sha1.addBytes(slt.c_str(), slt.length());
160         unsigned char *digest = sha1.getDigest();
161         std::string pwd = base64_encode(digest, 20);
162         free(digest);
163         return pwd;
164 }
165
166 std::string urlencode(std::string str)
167 {
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 == '~')
175                         oss << c;
176                 else
177                         oss << "%"
178                                 << url_hex_chars[(c & 0xf0) >> 4]
179                                 << url_hex_chars[c & 0x0f];
180         }
181         return oss.str();
182 }
183
184 std::string urldecode(std::string str)
185 {
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;
190                 if (str[i] == '%' &&
191                                 hex_digit_decode(str[i+1], highvalue) &&
192                                 hex_digit_decode(str[i+2], lowvalue)) {
193                         oss << (char) ((highvalue << 4) | lowvalue);
194                         i += 2;
195                 }
196                 else
197                         oss << str[i];
198         }
199         return oss.str();
200 }
201
202 u32 readFlagString(std::string str, const FlagDesc *flagdesc, u32 *flagmask)
203 {
204         u32 result = 0, mask = 0;
205         char *s = &str[0];
206         char *flagstr, *strpos = NULL;
207
208         while ((flagstr = strtok_r(s, ",", &strpos))) {
209                 s = NULL;
210
211                 while (*flagstr == ' ' || *flagstr == '\t')
212                         flagstr++;
213
214                 bool flagset = true;
215                 if (!strncasecmp(flagstr, "no", 2)) {
216                         flagset = false;
217                         flagstr += 2;
218                 }
219
220                 for (int i = 0; flagdesc[i].name; i++) {
221                         if (!strcasecmp(flagstr, flagdesc[i].name)) {
222                                 mask |= flagdesc[i].flag;
223                                 if (flagset)
224                                         result |= flagdesc[i].flag;
225                                 break;
226                         }
227                 }
228         }
229
230         if (flagmask)
231                 *flagmask = mask;
232
233         return result;
234 }
235
236 std::string writeFlagString(u32 flags, const FlagDesc *flagdesc, u32 flagmask)
237 {
238         std::string result;
239
240         for (int i = 0; flagdesc[i].name; i++) {
241                 if (flagmask & flagdesc[i].flag) {
242                         if (!(flags & flagdesc[i].flag))
243                                 result += "no";
244
245                         result += flagdesc[i].name;
246                         result += ", ";
247                 }
248         }
249
250         size_t len = result.length();
251         if (len >= 2)
252                 result.erase(len - 2, 2);
253
254         return result;
255 }
256
257 size_t mystrlcpy(char *dst, const char *src, size_t size)
258 {
259         size_t srclen  = strlen(src) + 1;
260         size_t copylen = MYMIN(srclen, size);
261
262         if (copylen > 0) {
263                 memcpy(dst, src, copylen);
264                 dst[copylen - 1] = '\0';
265         }
266
267         return srclen;
268 }
269
270 char *mystrtok_r(char *s, const char *sep, char **lasts)
271 {
272         char *t;
273
274         if (!s)
275                 s = *lasts;
276
277         while (*s && strchr(sep, *s))
278                 s++;
279
280         if (!*s)
281                 return NULL;
282
283         t = s;
284         while (*t) {
285                 if (strchr(sep, *t)) {
286                         *t++ = '\0';
287                         break;
288                 }
289                 t++;
290         }
291         
292         *lasts = t;
293         return s;
294 }
295
296 u64 read_seed(const char *str)
297 {
298         char *endptr;
299         u64 num;
300         
301         if (str[0] == '0' && str[1] == 'x')
302                 num = strtoull(str, &endptr, 16);
303         else
304                 num = strtoull(str, &endptr, 10);
305                 
306         if (*endptr)
307                 num = murmur_hash_64_ua(str, (int)strlen(str), 0x1337);
308                 
309         return num;
310 }
311
312 bool parseColorString(const std::string &value, video::SColor &color, bool quiet)
313 {
314         bool success;
315
316         if (value[0] == '#')
317                 success = parseHexColorString(value, color);
318         else
319                 success = parseNamedColorString(value, color);
320
321         if (!success && !quiet)
322                 errorstream << "Invalid color: \"" << value << "\"" << std::endl;
323
324         return success;
325 }
326
327 static bool parseHexColorString(const std::string &value, video::SColor &color)
328 {
329         unsigned char components[] = { 0x00, 0x00, 0x00, 0xff }; // R,G,B,A
330
331         if (value[0] != '#')
332                 return false;
333
334         size_t len = value.size();
335         bool short_form;
336
337         if (len == 9 || len == 7) // #RRGGBBAA or #RRGGBB
338                 short_form = false;
339         else if (len == 5 || len == 4) // #RGBA or #RGB
340                 short_form = true;
341         else
342                 return false;
343
344         bool success = true;
345
346         for (size_t pos = 1, cc = 0; pos < len; pos++, cc++) {
347                 assert(cc < sizeof components / sizeof components[0]);
348                 if (short_form) {
349                         unsigned char d;
350                         if (!hex_digit_decode(value[pos], d)) {
351                                 success = false;
352                                 break;
353                         }
354                         components[cc] = (d & 0xf) << 4 | (d & 0xf);
355                 } else {
356                         unsigned char d1, d2;
357                         if (!hex_digit_decode(value[pos], d1) ||
358                                         !hex_digit_decode(value[pos+1], d2)) {
359                                 success = false;
360                                 break;
361                         }
362                         components[cc] = (d1 & 0xf) << 4 | (d2 & 0xf);
363                         pos++;  // skip the second digit -- it's already used
364                 }
365         }
366
367         if (success) {
368                 color.setRed(components[0]);
369                 color.setGreen(components[1]);
370                 color.setBlue(components[2]);
371                 color.setAlpha(components[3]);
372         }
373
374         return success;
375 }
376
377 struct ColorContainer {
378         ColorContainer();
379         std::map<const std::string, u32> colors;
380 };
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["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;
524
525 }
526
527 static const ColorContainer named_colors;
528
529 static bool parseNamedColorString(const std::string &value, video::SColor &color)
530 {
531         std::string color_name;
532         std::string alpha_string;
533
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)
538          */
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);
543         } else {
544                 color_name = value;
545         }
546
547         color_name = lowercase(value);
548
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())
552                 return false;
553
554         u32 color_temp = it->second;
555
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
559          *
560          * For named colors the supplied alpha string (representing a hex value)
561          * must be exactly two digits. For example:  colorname#08
562          */
563         if (!alpha_string.empty()) {
564                 if (alpha_string.length() != 2)
565                         return false;
566
567                 unsigned char d1, d2;
568                 if (!hex_digit_decode(alpha_string.at(0), d1)
569                                 || !hex_digit_decode(alpha_string.at(1), d2))
570                         return false;
571                 color_temp |= ((d1 & 0xf) << 4 | (d2 & 0xf)) << 24;
572         } else {
573                 color_temp |= 0xff << 24;  // Fully opaque
574         }
575
576         color = video::SColor(color_temp);
577
578         return true;
579 }