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