]> git.lizzy.rs Git - minetest.git/blob - src/util/string.cpp
afe40610c02f27617136ccb66b14ca8bad405905
[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 std::wstring narrow_to_wide(const std::string& mbs)
33 {
34         size_t wcl = mbs.size();
35         Buffer<wchar_t> wcs(wcl+1);
36         size_t l = mbstowcs(*wcs, mbs.c_str(), wcl);
37         if(l == (size_t)(-1))
38                 return L"<invalid multibyte string>";
39         wcs[l] = 0;
40         return *wcs;
41 }
42
43 std::string wide_to_narrow(const std::wstring& wcs)
44 {
45         size_t mbl = wcs.size()*4;
46         SharedBuffer<char> mbs(mbl+1);
47         size_t l = wcstombs(*mbs, wcs.c_str(), mbl);
48         if(l == (size_t)(-1)) {
49                 return "Character conversion failed!";
50         }
51         else
52                 mbs[l] = 0;
53         return *mbs;
54 }
55
56 // Get an sha-1 hash of the player's name combined with
57 // the password entered. That's what the server uses as
58 // their password. (Exception : if the password field is
59 // blank, we send a blank password - this is for backwards
60 // compatibility with password-less players).
61 std::string translatePassword(std::string playername, std::wstring password)
62 {
63         if(password.length() == 0)
64                 return "";
65
66         std::string slt = playername + wide_to_narrow(password);
67         SHA1 sha1;
68         sha1.addBytes(slt.c_str(), slt.length());
69         unsigned char *digest = sha1.getDigest();
70         std::string pwd = base64_encode(digest, 20);
71         free(digest);
72         return pwd;
73 }
74
75 std::string urlencode(std::string str)
76 {
77         // Encodes non-unreserved URI characters by a percent sign
78         // followed by two hex digits. See RFC 3986, section 2.3.
79         static const char url_hex_chars[] = "0123456789ABCDEF";
80         std::ostringstream oss(std::ios::binary);
81         for (u32 i = 0; i < str.size(); i++) {
82                 unsigned char c = str[i];
83                 if (isalnum(c) || c == '-' || c == '.' || c == '_' || c == '~')
84                         oss << c;
85                 else
86                         oss << "%"
87                                 << url_hex_chars[(c & 0xf0) >> 4]
88                                 << url_hex_chars[c & 0x0f];
89         }
90         return oss.str();
91 }
92
93 std::string urldecode(std::string str)
94 {
95         // Inverse of urlencode
96         std::ostringstream oss(std::ios::binary);
97         for (u32 i = 0; i < str.size(); i++) {
98                 unsigned char highvalue, lowvalue;
99                 if (str[i] == '%' &&
100                                 hex_digit_decode(str[i+1], highvalue) &&
101                                 hex_digit_decode(str[i+2], lowvalue)) {
102                         oss << (char) ((highvalue << 4) | lowvalue);
103                         i += 2;
104                 }
105                 else
106                         oss << str[i];
107         }
108         return oss.str();
109 }
110
111 u32 readFlagString(std::string str, FlagDesc *flagdesc, u32 *flagmask)
112 {
113         u32 result = 0, mask = 0;
114         char *s = &str[0];
115         char *flagstr, *strpos = NULL;
116
117         while ((flagstr = strtok_r(s, ",", &strpos))) {
118                 s = NULL;
119
120                 while (*flagstr == ' ' || *flagstr == '\t')
121                         flagstr++;
122
123                 bool flagset = true;
124                 if (!strncasecmp(flagstr, "no", 2)) {
125                         flagset = false;
126                         flagstr += 2;
127                 }
128
129                 for (int i = 0; flagdesc[i].name; i++) {
130                         if (!strcasecmp(flagstr, flagdesc[i].name)) {
131                                 mask |= flagdesc[i].flag;
132                                 if (flagset)
133                                         result |= flagdesc[i].flag;
134                                 break;
135                         }
136                 }
137         }
138
139         if (flagmask)
140                 *flagmask = mask;
141
142         return result;
143 }
144
145 std::string writeFlagString(u32 flags, FlagDesc *flagdesc, u32 flagmask)
146 {
147         std::string result;
148
149         for (int i = 0; flagdesc[i].name; i++) {
150                 if (flagmask & flagdesc[i].flag) {
151                         if (!(flags & flagdesc[i].flag))
152                                 result += "no";
153
154                         result += flagdesc[i].name;
155                         result += ", ";
156                 }
157         }
158
159         size_t len = result.length();
160         if (len >= 2)
161                 result.erase(len - 2, 2);
162
163         return result;
164 }
165
166 char *mystrtok_r(char *s, const char *sep, char **lasts)
167 {
168         char *t;
169
170         if (!s)
171                 s = *lasts;
172
173         while (*s && strchr(sep, *s))
174                 s++;
175
176         if (!*s)
177                 return NULL;
178
179         t = s;
180         while (*t) {
181                 if (strchr(sep, *t)) {
182                         *t++ = '\0';
183                         break;
184                 }
185                 t++;
186         }
187         
188         *lasts = t;
189         return s;
190 }
191
192 u64 read_seed(const char *str)
193 {
194         char *endptr;
195         u64 num;
196         
197         if (str[0] == '0' && str[1] == 'x')
198                 num = strtoull(str, &endptr, 16);
199         else
200                 num = strtoull(str, &endptr, 10);
201                 
202         if (*endptr)
203                 num = murmur_hash_64_ua(str, (int)strlen(str), 0x1337);
204                 
205         return num;
206 }