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