]> git.lizzy.rs Git - minetest.git/blob - src/util/string.cpp
5ba97afd515ecb03656418f1cb651c6440766d2d
[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
24 #include <sstream>
25 #include <iomanip>
26
27 #include "../sha1.h"
28 #include "../base64.h"
29 #include "../hex.h"
30 #include "../porting.h"
31
32 #ifdef __ANDROID__
33 const wchar_t* wide_chars =
34         L" !\"#$%&'()*+,-./0123456789:;<=>?@"
35         L"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`"
36         L"abcdefghijklmnopqrstuvwxyz{|}~";
37
38 int wctomb(char *s, wchar_t wc)
39 {
40         for (unsigned int j = 0; j < (sizeof(wide_chars)/sizeof(wchar_t));j++) {
41                 if (wc == wide_chars[j]) {
42                         *s = (char) (j+32);
43                         return 1;
44                 }
45                 else if (wc == L'\n') {
46                         *s = '\n';
47                         return 1;
48                 }
49         }
50         return -1;
51 }
52
53 int mbtowc(wchar_t *pwc, const char *s, size_t n)
54 {
55         std::wstring intermediate = narrow_to_wide(s);
56
57         if (intermediate.length() > 0) {
58                 *pwc = intermediate[0];
59                 return 1;
60         }
61         else {
62                 return -1;
63         }
64 }
65
66 std::wstring narrow_to_wide(const std::string& mbs) {
67         size_t wcl = mbs.size();
68
69         std::wstring retval = L"";
70
71         for (unsigned int i = 0; i < wcl; i++) {
72                 if (((unsigned char) mbs[i] >31) &&
73                  ((unsigned char) mbs[i] < 127)) {
74
75                         retval += wide_chars[(unsigned char) mbs[i] -32];
76                 }
77                 //handle newline
78                 else if (mbs[i] == '\n') {
79                         retval += L'\n';
80                 }
81         }
82
83         return retval;
84 }
85 #else
86
87 std::wstring narrow_to_wide(const std::string& mbs)
88 {
89         size_t wcl = mbs.size();
90         Buffer<wchar_t> wcs(wcl+1);
91         size_t l = mbstowcs(*wcs, mbs.c_str(), wcl);
92         if(l == (size_t)(-1))
93                 return L"<invalid multibyte string>";
94         wcs[l] = 0;
95         return *wcs;
96 }
97
98 #endif
99
100 #ifdef __ANDROID__
101 std::string wide_to_narrow(const std::wstring& wcs) {
102         size_t mbl = wcs.size()*4;
103
104         std::string retval = "";
105         for (unsigned int i = 0; i < wcs.size(); i++) {
106                 wchar_t char1 = (wchar_t) wcs[i];
107
108                 if (char1 == L'\n') {
109                         retval += '\n';
110                         continue;
111                 }
112
113                 for (unsigned int j = 0; j < wcslen(wide_chars);j++) {
114                         wchar_t char2 = (wchar_t) wide_chars[j];
115
116                         if (char1 == char2) {
117                                 char toadd = (j+32);
118                                 retval += toadd;
119                                 break;
120                         }
121                 }
122         }
123
124         return retval;
125 }
126 #else
127 std::string wide_to_narrow(const std::wstring& wcs)
128 {
129         size_t mbl = wcs.size()*4;
130         SharedBuffer<char> mbs(mbl+1);
131         size_t l = wcstombs(*mbs, wcs.c_str(), mbl);
132         if(l == (size_t)(-1)) {
133                 return "Character conversion failed!";
134         }
135         else
136                 mbs[l] = 0;
137         return *mbs;
138 }
139
140 #endif
141
142 // Get an sha-1 hash of the player's name combined with
143 // the password entered. That's what the server uses as
144 // their password. (Exception : if the password field is
145 // blank, we send a blank password - this is for backwards
146 // compatibility with password-less players).
147 std::string translatePassword(std::string playername, std::wstring password)
148 {
149         if(password.length() == 0)
150                 return "";
151
152         std::string slt = playername + wide_to_narrow(password);
153         SHA1 sha1;
154         sha1.addBytes(slt.c_str(), slt.length());
155         unsigned char *digest = sha1.getDigest();
156         std::string pwd = base64_encode(digest, 20);
157         free(digest);
158         return pwd;
159 }
160
161 std::string urlencode(std::string str)
162 {
163         // Encodes non-unreserved URI characters by a percent sign
164         // followed by two hex digits. See RFC 3986, section 2.3.
165         static const char url_hex_chars[] = "0123456789ABCDEF";
166         std::ostringstream oss(std::ios::binary);
167         for (u32 i = 0; i < str.size(); i++) {
168                 unsigned char c = str[i];
169                 if (isalnum(c) || c == '-' || c == '.' || c == '_' || c == '~')
170                         oss << c;
171                 else
172                         oss << "%"
173                                 << url_hex_chars[(c & 0xf0) >> 4]
174                                 << url_hex_chars[c & 0x0f];
175         }
176         return oss.str();
177 }
178
179 std::string urldecode(std::string str)
180 {
181         // Inverse of urlencode
182         std::ostringstream oss(std::ios::binary);
183         for (u32 i = 0; i < str.size(); i++) {
184                 unsigned char highvalue, lowvalue;
185                 if (str[i] == '%' &&
186                                 hex_digit_decode(str[i+1], highvalue) &&
187                                 hex_digit_decode(str[i+2], lowvalue)) {
188                         oss << (char) ((highvalue << 4) | lowvalue);
189                         i += 2;
190                 }
191                 else
192                         oss << str[i];
193         }
194         return oss.str();
195 }
196
197 u32 readFlagString(std::string str, const FlagDesc *flagdesc, u32 *flagmask)
198 {
199         u32 result = 0, mask = 0;
200         char *s = &str[0];
201         char *flagstr, *strpos = NULL;
202
203         while ((flagstr = strtok_r(s, ",", &strpos))) {
204                 s = NULL;
205
206                 while (*flagstr == ' ' || *flagstr == '\t')
207                         flagstr++;
208
209                 bool flagset = true;
210                 if (!strncasecmp(flagstr, "no", 2)) {
211                         flagset = false;
212                         flagstr += 2;
213                 }
214
215                 for (int i = 0; flagdesc[i].name; i++) {
216                         if (!strcasecmp(flagstr, flagdesc[i].name)) {
217                                 mask |= flagdesc[i].flag;
218                                 if (flagset)
219                                         result |= flagdesc[i].flag;
220                                 break;
221                         }
222                 }
223         }
224
225         if (flagmask)
226                 *flagmask = mask;
227
228         return result;
229 }
230
231 std::string writeFlagString(u32 flags, const FlagDesc *flagdesc, u32 flagmask)
232 {
233         std::string result;
234
235         for (int i = 0; flagdesc[i].name; i++) {
236                 if (flagmask & flagdesc[i].flag) {
237                         if (!(flags & flagdesc[i].flag))
238                                 result += "no";
239
240                         result += flagdesc[i].name;
241                         result += ", ";
242                 }
243         }
244
245         size_t len = result.length();
246         if (len >= 2)
247                 result.erase(len - 2, 2);
248
249         return result;
250 }
251
252 size_t mystrlcpy(char *dst, const char *src, size_t size)
253 {
254         size_t srclen  = strlen(src) + 1;
255         size_t copylen = MYMIN(srclen, size);
256
257         if (copylen > 0) {
258                 memcpy(dst, src, copylen);
259                 dst[copylen - 1] = '\0';
260         }
261
262         return srclen;
263 }
264
265 char *mystrtok_r(char *s, const char *sep, char **lasts)
266 {
267         char *t;
268
269         if (!s)
270                 s = *lasts;
271
272         while (*s && strchr(sep, *s))
273                 s++;
274
275         if (!*s)
276                 return NULL;
277
278         t = s;
279         while (*t) {
280                 if (strchr(sep, *t)) {
281                         *t++ = '\0';
282                         break;
283                 }
284                 t++;
285         }
286         
287         *lasts = t;
288         return s;
289 }
290
291 u64 read_seed(const char *str)
292 {
293         char *endptr;
294         u64 num;
295         
296         if (str[0] == '0' && str[1] == 'x')
297                 num = strtoull(str, &endptr, 16);
298         else
299                 num = strtoull(str, &endptr, 10);
300                 
301         if (*endptr)
302                 num = murmur_hash_64_ua(str, (int)strlen(str), 0x1337);
303                 
304         return num;
305 }