]> git.lizzy.rs Git - dragonfireclient.git/blob - src/util/string.cpp
Revert "Fix settings to honor numeric conversion errors"
[dragonfireclient.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) {
112         u32 result = 0;
113         char *s = &str[0];
114         char *flagstr, *strpos = NULL;
115         
116         while ((flagstr = strtok_r(s, ",", &strpos))) {
117                 s = NULL;
118                 
119                 while (*flagstr == ' ' || *flagstr == '\t')
120                         flagstr++;
121                 
122                 for (int i = 0; flagdesc[i].name; i++) {
123                         if (!strcasecmp(flagstr, flagdesc[i].name)) {
124                                 result |= flagdesc[i].flag;
125                                 break;
126                         }
127                 }
128         }
129         
130         return result;
131 }
132
133 std::string writeFlagString(u32 flags, FlagDesc *flagdesc) {
134         std::string result;
135         
136         for (int i = 0; flagdesc[i].name; i++) {
137                 if (flags & flagdesc[i].flag) {
138                         result += flagdesc[i].name;
139                         result += ", ";
140                 }
141         }
142         
143         size_t len = result.length();
144         if (len >= 2)
145                 result.erase(len - 2, 2);
146         
147         return result;
148 }
149
150 char *mystrtok_r(char *s, const char *sep, char **lasts) {
151         char *t;
152
153         if (!s)
154                 s = *lasts;
155
156         while (*s && strchr(sep, *s))
157                 s++;
158
159         if (!*s)
160                 return NULL;
161
162         t = s;
163         while (*t) {
164                 if (strchr(sep, *t)) {
165                         *t++ = '\0';
166                         break;
167                 }
168                 t++;
169         }
170         
171         *lasts = t;
172         return s;
173 }
174
175 u64 read_seed(const char *str) {
176         char *endptr;
177         u64 num;
178         
179         if (str[0] == '0' && str[1] == 'x')
180                 num = strtoull(str, &endptr, 16);
181         else
182                 num = strtoull(str, &endptr, 10);
183                 
184         if (*endptr)
185                 num = murmur_hash_64_ua(str, (int)strlen(str), 0x1337);
186                 
187         return num;
188 }