Reduce gettext wide/narrow and string/char* conversions
[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 "../sha1.h"
26 #include "../base64.h"
27 #include "../hex.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 const wchar_t* wide_chars =
44         L" !\"#$%&'()*+,-./0123456789:;<=>?@"
45         L"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`"
46         L"abcdefghijklmnopqrstuvwxyz{|}~";
47
48 int wctomb(char *s, wchar_t wc)
49 {
50         for (unsigned int j = 0; j < (sizeof(wide_chars)/sizeof(wchar_t));j++) {
51                 if (wc == wide_chars[j]) {
52                         *s = (char) (j+32);
53                         return 1;
54                 }
55                 else if (wc == L'\n') {
56                         *s = '\n';
57                         return 1;
58                 }
59         }
60         return -1;
61 }
62
63 int mbtowc(wchar_t *pwc, const char *s, size_t n)
64 {
65         wchar_t *intermediate = narrow_to_wide(s);
66
67         if (intermediate.length() > 0) {
68                 *pwc = intermediate[0];
69                 return 1;
70         } else {
71                 return -1;
72         }
73 }
74 // You must free the returned string!
75 const wchar_t *narrow_to_wide_c(const char *mbs)
76 {
77         size_t mbl = strlen(mbs);
78         wchar_t wcs = new wchar_t[mbl + 1];
79
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];
84                 }
85                 //handle newline
86                 else if (mbs[i] == '\n') {
87                         wcs[i] = L'\n';
88                 }
89         }
90
91         return wcs;
92 }
93
94 #else
95
96 // You must free the returned string!
97 const wchar_t *narrow_to_wide_c(const char *mbs)
98 {
99         wchar_t *wcs = NULL;
100 #if defined(_WIN32)
101         int wcl = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) mbs, -1, NULL, 0);
102         if (!wcl)
103                 return NULL;
104         wcs = new wchar_t[wcl];
105         MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) mbs, -1, (WCHAR *) wcs, wcl);
106 #else
107         size_t wcl = mbstowcs(NULL, mbs, 0);
108         if (wcl == (size_t) -1)
109                 return NULL;
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
113         wcs[l] = '\0';
114 #endif
115
116         return wcs;
117 }
118
119 #endif
120
121 std::wstring narrow_to_wide(const std::string& mbs)
122 {
123         const wchar_t *wcs = narrow_to_wide_c(mbs.c_str());
124         if (!wcs)
125                 return L"<invalid multibyte string>";
126         std::wstring wstr(wcs);
127         delete [] wcs;
128         return wstr;
129 }
130
131 #ifdef __ANDROID__
132 std::string wide_to_narrow(const std::wstring& wcs) {
133         size_t mbl = wcs.size()*4;
134
135         std::string retval = "";
136         for (unsigned int i = 0; i < wcs.size(); i++) {
137                 wchar_t char1 = (wchar_t) wcs[i];
138
139                 if (char1 == L'\n') {
140                         retval += '\n';
141                         continue;
142                 }
143
144                 for (unsigned int j = 0; j < wcslen(wide_chars);j++) {
145                         wchar_t char2 = (wchar_t) wide_chars[j];
146
147                         if (char1 == char2) {
148                                 char toadd = (j+32);
149                                 retval += toadd;
150                                 break;
151                         }
152                 }
153         }
154
155         return retval;
156 }
157 #else
158 std::string wide_to_narrow(const std::wstring& wcs)
159 {
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!";
165         }
166         else
167                 mbs[l] = 0;
168         return *mbs;
169 }
170
171 #endif
172
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)
179 {
180         if(password.length() == 0)
181                 return "";
182
183         std::string slt = playername + wide_to_narrow(password);
184         SHA1 sha1;
185         sha1.addBytes(slt.c_str(), slt.length());
186         unsigned char *digest = sha1.getDigest();
187         std::string pwd = base64_encode(digest, 20);
188         free(digest);
189         return pwd;
190 }
191
192 std::string urlencode(std::string str)
193 {
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 == '~')
201                         oss << c;
202                 else
203                         oss << "%"
204                                 << url_hex_chars[(c & 0xf0) >> 4]
205                                 << url_hex_chars[c & 0x0f];
206         }
207         return oss.str();
208 }
209
210 std::string urldecode(std::string str)
211 {
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;
216                 if (str[i] == '%' &&
217                                 hex_digit_decode(str[i+1], highvalue) &&
218                                 hex_digit_decode(str[i+2], lowvalue)) {
219                         oss << (char) ((highvalue << 4) | lowvalue);
220                         i += 2;
221                 }
222                 else
223                         oss << str[i];
224         }
225         return oss.str();
226 }
227
228 u32 readFlagString(std::string str, const FlagDesc *flagdesc, u32 *flagmask)
229 {
230         u32 result = 0, mask = 0;
231         char *s = &str[0];
232         char *flagstr, *strpos = NULL;
233
234         while ((flagstr = strtok_r(s, ",", &strpos))) {
235                 s = NULL;
236
237                 while (*flagstr == ' ' || *flagstr == '\t')
238                         flagstr++;
239
240                 bool flagset = true;
241                 if (!strncasecmp(flagstr, "no", 2)) {
242                         flagset = false;
243                         flagstr += 2;
244                 }
245
246                 for (int i = 0; flagdesc[i].name; i++) {
247                         if (!strcasecmp(flagstr, flagdesc[i].name)) {
248                                 mask |= flagdesc[i].flag;
249                                 if (flagset)
250                                         result |= flagdesc[i].flag;
251                                 break;
252                         }
253                 }
254         }
255
256         if (flagmask)
257                 *flagmask = mask;
258
259         return result;
260 }
261
262 std::string writeFlagString(u32 flags, const FlagDesc *flagdesc, u32 flagmask)
263 {
264         std::string result;
265
266         for (int i = 0; flagdesc[i].name; i++) {
267                 if (flagmask & flagdesc[i].flag) {
268                         if (!(flags & flagdesc[i].flag))
269                                 result += "no";
270
271                         result += flagdesc[i].name;
272                         result += ", ";
273                 }
274         }
275
276         size_t len = result.length();
277         if (len >= 2)
278                 result.erase(len - 2, 2);
279
280         return result;
281 }
282
283 size_t mystrlcpy(char *dst, const char *src, size_t size)
284 {
285         size_t srclen  = strlen(src) + 1;
286         size_t copylen = MYMIN(srclen, size);
287
288         if (copylen > 0) {
289                 memcpy(dst, src, copylen);
290                 dst[copylen - 1] = '\0';
291         }
292
293         return srclen;
294 }
295
296 char *mystrtok_r(char *s, const char *sep, char **lasts)
297 {
298         char *t;
299
300         if (!s)
301                 s = *lasts;
302
303         while (*s && strchr(sep, *s))
304                 s++;
305
306         if (!*s)
307                 return NULL;
308
309         t = s;
310         while (*t) {
311                 if (strchr(sep, *t)) {
312                         *t++ = '\0';
313                         break;
314                 }
315                 t++;
316         }
317         
318         *lasts = t;
319         return s;
320 }
321
322 u64 read_seed(const char *str)
323 {
324         char *endptr;
325         u64 num;
326         
327         if (str[0] == '0' && str[1] == 'x')
328                 num = strtoull(str, &endptr, 16);
329         else
330                 num = strtoull(str, &endptr, 10);
331                 
332         if (*endptr)
333                 num = murmur_hash_64_ua(str, (int)strlen(str), 0x1337);
334                 
335         return num;
336 }
337
338 bool parseColorString(const std::string &value, video::SColor &color, bool quiet)
339 {
340         bool success;
341
342         if (value[0] == '#')
343                 success = parseHexColorString(value, color);
344         else
345                 success = parseNamedColorString(value, color);
346
347         if (!success && !quiet)
348                 errorstream << "Invalid color: \"" << value << "\"" << std::endl;
349
350         return success;
351 }
352
353 static bool parseHexColorString(const std::string &value, video::SColor &color)
354 {
355         unsigned char components[] = { 0x00, 0x00, 0x00, 0xff }; // R,G,B,A
356
357         if (value[0] != '#')
358                 return false;
359
360         size_t len = value.size();
361         bool short_form;
362
363         if (len == 9 || len == 7) // #RRGGBBAA or #RRGGBB
364                 short_form = false;
365         else if (len == 5 || len == 4) // #RGBA or #RGB
366                 short_form = true;
367         else
368                 return false;
369
370         bool success = true;
371
372         for (size_t pos = 1, cc = 0; pos < len; pos++, cc++) {
373                 assert(cc < sizeof components / sizeof components[0]);
374                 if (short_form) {
375                         unsigned char d;
376                         if (!hex_digit_decode(value[pos], d)) {
377                                 success = false;
378                                 break;
379                         }
380                         components[cc] = (d & 0xf) << 4 | (d & 0xf);
381                 } else {
382                         unsigned char d1, d2;
383                         if (!hex_digit_decode(value[pos], d1) ||
384                                         !hex_digit_decode(value[pos+1], d2)) {
385                                 success = false;
386                                 break;
387                         }
388                         components[cc] = (d1 & 0xf) << 4 | (d2 & 0xf);
389                         pos++;  // skip the second digit -- it's already used
390                 }
391         }
392
393         if (success) {
394                 color.setRed(components[0]);
395                 color.setGreen(components[1]);
396                 color.setBlue(components[2]);
397                 color.setAlpha(components[3]);
398         }
399
400         return success;
401 }
402
403 struct ColorContainer {
404         ColorContainer();
405         std::map<const std::string, u32> colors;
406 };
407
408 ColorContainer::ColorContainer()
409 {
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;
550
551 }
552
553 static const ColorContainer named_colors;
554
555 static bool parseNamedColorString(const std::string &value, video::SColor &color)
556 {
557         std::string color_name;
558         std::string alpha_string;
559
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)
564          */
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);
569         } else {
570                 color_name = value;
571         }
572
573         color_name = lowercase(value);
574
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())
578                 return false;
579
580         u32 color_temp = it->second;
581
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
585          *
586          * For named colors the supplied alpha string (representing a hex value)
587          * must be exactly two digits. For example:  colorname#08
588          */
589         if (!alpha_string.empty()) {
590                 if (alpha_string.length() != 2)
591                         return false;
592
593                 unsigned char d1, d2;
594                 if (!hex_digit_decode(alpha_string.at(0), d1)
595                                 || !hex_digit_decode(alpha_string.at(1), d2))
596                         return false;
597                 color_temp |= ((d1 & 0xf) << 4 | (d2 & 0xf)) << 24;
598         } else {
599                 color_temp |= 0xff << 24;  // Fully opaque
600         }
601
602         color = video::SColor(color_temp);
603
604         return true;
605 }
606
607 void str_replace(std::string &str, char from, char to)
608 {
609         std::replace(str.begin(), str.end(), from, to);
610 }
611