Add find_surface_nodes_in_area LUA call which permit to only get the nodes which...
[oweals/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 "base64.h"
26 #include "hex.h"
27 #include "sha1.h"
28 #include "../porting.h"
29
30 #include <algorithm>
31 #include <sstream>
32 #include <iomanip>
33 #include <map>
34
35 #if defined(_WIN32)
36 #include <windows.h>  // MultiByteToWideChar
37 #endif
38
39 static bool parseHexColorString(const std::string &value, video::SColor &color);
40 static bool parseNamedColorString(const std::string &value, video::SColor &color);
41
42 #ifdef __ANDROID__
43
44 const wchar_t* wide_chars =
45         L" !\"#$%&'()*+,-./0123456789:;<=>?@"
46         L"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`"
47         L"abcdefghijklmnopqrstuvwxyz{|}~";
48
49 int wctomb(char *s, wchar_t wc)
50 {
51         for (unsigned int j = 0; j < (sizeof(wide_chars)/sizeof(wchar_t));j++) {
52                 if (wc == wide_chars[j]) {
53                         *s = (char) (j+32);
54                         return 1;
55                 }
56                 else if (wc == L'\n') {
57                         *s = '\n';
58                         return 1;
59                 }
60         }
61         return -1;
62 }
63
64 int mbtowc(wchar_t *pwc, const char *s, size_t n)
65 {
66         if (s == NULL || *s == '\0')
67                 return -1;
68
69         const wchar_t *tmp = narrow_to_wide_c(s);
70         bool success = tmp[0] != '\0';
71
72         if (success)
73                 *pwc = tmp[0];
74
75         delete tmp;
76
77         return success ? 1 : -1;
78 }
79
80 // You must free the returned string!
81 const wchar_t *narrow_to_wide_c(const char *mbs)
82 {
83         size_t mbl = strlen(mbs);
84         wchar_t *wcs = new wchar_t[mbl + 1];
85
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];
90                 }
91                 //handle newline
92                 else if (mbs[i] == '\n') {
93                         wcs[i] = L'\n';
94                 }
95         }
96
97         return wcs;
98 }
99
100 #else
101
102 // You must free the returned string!
103 const wchar_t *narrow_to_wide_c(const char *mbs)
104 {
105         wchar_t *wcs = NULL;
106 #if defined(_WIN32)
107         int nResult = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) mbs, -1, 0, 0);
108         if (nResult == 0) {
109                 errorstream << "gettext: MultiByteToWideChar returned null" << std::endl;
110         } else {
111                 wcs = new wchar_t[nResult];
112                 MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) mbs, -1, (WCHAR *) wcs, nResult);
113         }
114 #else
115         size_t wcl = mbstowcs(NULL, mbs, 0);
116         if (wcl == (size_t) -1)
117                 return NULL;
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
121         wcs[l] = '\0';
122 #endif
123
124         return wcs;
125 }
126
127 #endif
128
129 std::wstring narrow_to_wide(const std::string& mbs)
130 {
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>";
136         wcs[l] = 0;
137         return *wcs;
138 }
139
140 #ifdef __ANDROID__
141 std::string wide_to_narrow(const std::wstring& wcs) {
142         size_t mbl = wcs.size()*4;
143
144         std::string retval = "";
145         for (unsigned int i = 0; i < wcs.size(); i++) {
146                 wchar_t char1 = (wchar_t) wcs[i];
147
148                 if (char1 == L'\n') {
149                         retval += '\n';
150                         continue;
151                 }
152
153                 for (unsigned int j = 0; j < wcslen(wide_chars);j++) {
154                         wchar_t char2 = (wchar_t) wide_chars[j];
155
156                         if (char1 == char2) {
157                                 char toadd = (j+32);
158                                 retval += toadd;
159                                 break;
160                         }
161                 }
162         }
163
164         return retval;
165 }
166 #else
167 std::string wide_to_narrow(const std::wstring& wcs)
168 {
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!";
174         }
175         else
176                 mbs[l] = 0;
177         return *mbs;
178 }
179
180 #endif
181
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)
188 {
189         if(password.length() == 0)
190                 return "";
191
192         std::string slt = playername + wide_to_narrow(password);
193         SHA1 sha1;
194         sha1.addBytes(slt.c_str(), slt.length());
195         unsigned char *digest = sha1.getDigest();
196         std::string pwd = base64_encode(digest, 20);
197         free(digest);
198         return pwd;
199 }
200
201 std::string urlencode(std::string str)
202 {
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 == '~')
210                         oss << c;
211                 else
212                         oss << "%"
213                                 << url_hex_chars[(c & 0xf0) >> 4]
214                                 << url_hex_chars[c & 0x0f];
215         }
216         return oss.str();
217 }
218
219 std::string urldecode(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                 }
231                 else
232                         oss << str[i];
233         }
234         return oss.str();
235 }
236
237 u32 readFlagString(std::string str, const FlagDesc *flagdesc, u32 *flagmask)
238 {
239         u32 result = 0, mask = 0;
240         char *s = &str[0];
241         char *flagstr, *strpos = NULL;
242
243         while ((flagstr = strtok_r(s, ",", &strpos))) {
244                 s = NULL;
245
246                 while (*flagstr == ' ' || *flagstr == '\t')
247                         flagstr++;
248
249                 bool flagset = true;
250                 if (!strncasecmp(flagstr, "no", 2)) {
251                         flagset = false;
252                         flagstr += 2;
253                 }
254
255                 for (int i = 0; flagdesc[i].name; i++) {
256                         if (!strcasecmp(flagstr, flagdesc[i].name)) {
257                                 mask |= flagdesc[i].flag;
258                                 if (flagset)
259                                         result |= flagdesc[i].flag;
260                                 break;
261                         }
262                 }
263         }
264
265         if (flagmask)
266                 *flagmask = mask;
267
268         return result;
269 }
270
271 std::string writeFlagString(u32 flags, const FlagDesc *flagdesc, u32 flagmask)
272 {
273         std::string result;
274
275         for (int i = 0; flagdesc[i].name; i++) {
276                 if (flagmask & flagdesc[i].flag) {
277                         if (!(flags & flagdesc[i].flag))
278                                 result += "no";
279
280                         result += flagdesc[i].name;
281                         result += ", ";
282                 }
283         }
284
285         size_t len = result.length();
286         if (len >= 2)
287                 result.erase(len - 2, 2);
288
289         return result;
290 }
291
292 size_t mystrlcpy(char *dst, const char *src, size_t size)
293 {
294         size_t srclen  = strlen(src) + 1;
295         size_t copylen = MYMIN(srclen, size);
296
297         if (copylen > 0) {
298                 memcpy(dst, src, copylen);
299                 dst[copylen - 1] = '\0';
300         }
301
302         return srclen;
303 }
304
305 char *mystrtok_r(char *s, const char *sep, char **lasts)
306 {
307         char *t;
308
309         if (!s)
310                 s = *lasts;
311
312         while (*s && strchr(sep, *s))
313                 s++;
314
315         if (!*s)
316                 return NULL;
317
318         t = s;
319         while (*t) {
320                 if (strchr(sep, *t)) {
321                         *t++ = '\0';
322                         break;
323                 }
324                 t++;
325         }
326         
327         *lasts = t;
328         return s;
329 }
330
331 u64 read_seed(const char *str)
332 {
333         char *endptr;
334         u64 num;
335         
336         if (str[0] == '0' && str[1] == 'x')
337                 num = strtoull(str, &endptr, 16);
338         else
339                 num = strtoull(str, &endptr, 10);
340                 
341         if (*endptr)
342                 num = murmur_hash_64_ua(str, (int)strlen(str), 0x1337);
343                 
344         return num;
345 }
346
347 bool parseColorString(const std::string &value, video::SColor &color, bool quiet)
348 {
349         bool success;
350
351         if (value[0] == '#')
352                 success = parseHexColorString(value, color);
353         else
354                 success = parseNamedColorString(value, color);
355
356         if (!success && !quiet)
357                 errorstream << "Invalid color: \"" << value << "\"" << std::endl;
358
359         return success;
360 }
361
362 static bool parseHexColorString(const std::string &value, video::SColor &color)
363 {
364         unsigned char components[] = { 0x00, 0x00, 0x00, 0xff }; // R,G,B,A
365
366         if (value[0] != '#')
367                 return false;
368
369         size_t len = value.size();
370         bool short_form;
371
372         if (len == 9 || len == 7) // #RRGGBBAA or #RRGGBB
373                 short_form = false;
374         else if (len == 5 || len == 4) // #RGBA or #RGB
375                 short_form = true;
376         else
377                 return false;
378
379         bool success = true;
380
381         for (size_t pos = 1, cc = 0; pos < len; pos++, cc++) {
382                 assert(cc < sizeof components / sizeof components[0]);
383                 if (short_form) {
384                         unsigned char d;
385                         if (!hex_digit_decode(value[pos], d)) {
386                                 success = false;
387                                 break;
388                         }
389                         components[cc] = (d & 0xf) << 4 | (d & 0xf);
390                 } else {
391                         unsigned char d1, d2;
392                         if (!hex_digit_decode(value[pos], d1) ||
393                                         !hex_digit_decode(value[pos+1], d2)) {
394                                 success = false;
395                                 break;
396                         }
397                         components[cc] = (d1 & 0xf) << 4 | (d2 & 0xf);
398                         pos++;  // skip the second digit -- it's already used
399                 }
400         }
401
402         if (success) {
403                 color.setRed(components[0]);
404                 color.setGreen(components[1]);
405                 color.setBlue(components[2]);
406                 color.setAlpha(components[3]);
407         }
408
409         return success;
410 }
411
412 struct ColorContainer {
413         ColorContainer();
414         std::map<const std::string, u32> colors;
415 };
416
417 ColorContainer::ColorContainer()
418 {
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;
559
560 }
561
562 static const ColorContainer named_colors;
563
564 static bool parseNamedColorString(const std::string &value, video::SColor &color)
565 {
566         std::string color_name;
567         std::string alpha_string;
568
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)
573          */
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);
578         } else {
579                 color_name = value;
580         }
581
582         color_name = lowercase(value);
583
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())
587                 return false;
588
589         u32 color_temp = it->second;
590
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
594          *
595          * For named colors the supplied alpha string (representing a hex value)
596          * must be exactly two digits. For example:  colorname#08
597          */
598         if (!alpha_string.empty()) {
599                 if (alpha_string.length() != 2)
600                         return false;
601
602                 unsigned char d1, d2;
603                 if (!hex_digit_decode(alpha_string.at(0), d1)
604                                 || !hex_digit_decode(alpha_string.at(1), d2))
605                         return false;
606                 color_temp |= ((d1 & 0xf) << 4 | (d2 & 0xf)) << 24;
607         } else {
608                 color_temp |= 0xff << 24;  // Fully opaque
609         }
610
611         color = video::SColor(color_temp);
612
613         return true;
614 }
615
616 void str_replace(std::string &str, char from, char to)
617 {
618         std::replace(str.begin(), str.end(), from, to);
619 }
620