Change Minetest-c55 to Minetest
[oweals/minetest.git] / src / util / string.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2012 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
22 #include "../sha1.h"
23 #include "../base64.h"
24 #include "../porting.h"
25
26 // Get an sha-1 hash of the player's name combined with
27 // the password entered. That's what the server uses as
28 // their password. (Exception : if the password field is
29 // blank, we send a blank password - this is for backwards
30 // compatibility with password-less players).
31 std::string translatePassword(std::string playername, std::wstring password)
32 {
33         if(password.length() == 0)
34                 return "";
35
36         std::string slt = playername + wide_to_narrow(password);
37         SHA1 sha1;
38         sha1.addBytes(slt.c_str(), slt.length());
39         unsigned char *digest = sha1.getDigest();
40         std::string pwd = base64_encode(digest, 20);
41         free(digest);
42         return pwd;
43 }
44
45 size_t curl_write_data(char *ptr, size_t size, size_t nmemb, void *userdata) {
46     std::ostringstream *stream = (std::ostringstream*)userdata;
47     size_t count = size * nmemb;
48     stream->write(ptr, count);
49     return count;
50 }
51
52 u32 readFlagString(std::string str, FlagDesc *flagdesc) {
53         u32 result = 0;
54         char *s = &str[0];
55         char *flagstr, *strpos = NULL;
56         
57         while ((flagstr = strtok_r(s, ",", &strpos))) {
58                 s = NULL;
59                 
60                 while (*flagstr == ' ' || *flagstr == '\t')
61                         flagstr++;
62                 
63                 for (int i = 0; flagdesc[i].name; i++) {
64                         if (!strcasecmp(flagstr, flagdesc[i].name)) {
65                                 result |= flagdesc[i].flag;
66                                 break;
67                         }
68                 }
69         }
70         
71         return result;
72 }
73
74 std::string writeFlagString(u32 flags, FlagDesc *flagdesc) {
75         std::string result;
76         
77         for (int i = 0; flagdesc[i].name; i++) {
78                 if (flags & flagdesc[i].flag) {
79                         result += flagdesc[i].name;
80                         result += ", ";
81                 }
82         }
83         
84         size_t len = result.length();
85         if (len >= 2)
86                 result.erase(len - 2, 2);
87         
88         return result;
89 }
90
91 char *mystrtok_r(char *s, const char *sep, char **lasts) {
92         char *t;
93
94         if (!s)
95                 s = *lasts;
96
97         while (*s && strchr(sep, *s))
98                 s++;
99
100         if (!*s)
101                 return NULL;
102
103         t = s;
104         while (*t) {
105                 if (strchr(sep, *t)) {
106                         *t++ = '\0';
107                         break;
108                 }
109                 t++;
110         }
111         
112         *lasts = t;
113         return s;
114 }