]> git.lizzy.rs Git - minetest.git/blob - src/util/string.cpp
Accept hexadecimal and string values for seeds
[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 "../sha1.h"
25 #include "../base64.h"
26 #include "../porting.h"
27
28 std::wstring narrow_to_wide(const std::string& mbs)
29 {
30         size_t wcl = mbs.size();
31         Buffer<wchar_t> wcs(wcl+1);
32         size_t l = mbstowcs(*wcs, mbs.c_str(), wcl);
33         if(l == (size_t)(-1))
34                 return L"<invalid multibyte string>";
35         wcs[l] = 0;
36         return *wcs;
37 }
38
39 std::string wide_to_narrow(const std::wstring& wcs)
40 {
41         size_t mbl = wcs.size()*4;
42         SharedBuffer<char> mbs(mbl+1);
43         size_t l = wcstombs(*mbs, wcs.c_str(), mbl);
44         if(l == (size_t)(-1))
45                 mbs[0] = 0;
46         else
47                 mbs[l] = 0;
48         return *mbs;
49 }
50
51 // Get an sha-1 hash of the player's name combined with
52 // the password entered. That's what the server uses as
53 // their password. (Exception : if the password field is
54 // blank, we send a blank password - this is for backwards
55 // compatibility with password-less players).
56 std::string translatePassword(std::string playername, std::wstring password)
57 {
58         if(password.length() == 0)
59                 return "";
60
61         std::string slt = playername + wide_to_narrow(password);
62         SHA1 sha1;
63         sha1.addBytes(slt.c_str(), slt.length());
64         unsigned char *digest = sha1.getDigest();
65         std::string pwd = base64_encode(digest, 20);
66         free(digest);
67         return pwd;
68 }
69
70 size_t curl_write_data(char *ptr, size_t size, size_t nmemb, void *userdata) {
71     std::ostringstream *stream = (std::ostringstream*)userdata;
72     size_t count = size * nmemb;
73     stream->write(ptr, count);
74     return count;
75 }
76
77 u32 readFlagString(std::string str, FlagDesc *flagdesc) {
78         u32 result = 0;
79         char *s = &str[0];
80         char *flagstr, *strpos = NULL;
81         
82         while ((flagstr = strtok_r(s, ",", &strpos))) {
83                 s = NULL;
84                 
85                 while (*flagstr == ' ' || *flagstr == '\t')
86                         flagstr++;
87                 
88                 for (int i = 0; flagdesc[i].name; i++) {
89                         if (!strcasecmp(flagstr, flagdesc[i].name)) {
90                                 result |= flagdesc[i].flag;
91                                 break;
92                         }
93                 }
94         }
95         
96         return result;
97 }
98
99 std::string writeFlagString(u32 flags, FlagDesc *flagdesc) {
100         std::string result;
101         
102         for (int i = 0; flagdesc[i].name; i++) {
103                 if (flags & flagdesc[i].flag) {
104                         result += flagdesc[i].name;
105                         result += ", ";
106                 }
107         }
108         
109         size_t len = result.length();
110         if (len >= 2)
111                 result.erase(len - 2, 2);
112         
113         return result;
114 }
115
116 char *mystrtok_r(char *s, const char *sep, char **lasts) {
117         char *t;
118
119         if (!s)
120                 s = *lasts;
121
122         while (*s && strchr(sep, *s))
123                 s++;
124
125         if (!*s)
126                 return NULL;
127
128         t = s;
129         while (*t) {
130                 if (strchr(sep, *t)) {
131                         *t++ = '\0';
132                         break;
133                 }
134                 t++;
135         }
136         
137         *lasts = t;
138         return s;
139 }
140
141 u64 read_seed(const char *str) {
142         char *endptr;
143         u64 num;
144         
145         if (str[0] == '0' && str[1] == 'x')
146                 num = strtoull(str, &endptr, 16);
147         else
148                 num = strtoull(str, &endptr, 10);
149                 
150         if (*endptr)
151                 num = murmur_hash_64_ua(str, (int)strlen(str), 0x1337);
152                 
153         return num;
154 }