]> git.lizzy.rs Git - dragonfireclient.git/blob - src/util/string.cpp
Fix compile under MingW
[dragonfireclient.git] / src / util / string.cpp
1 /*
2 Minetest-c55
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
25 // Get an sha-1 hash of the player's name combined with
26 // the password entered. That's what the server uses as
27 // their password. (Exception : if the password field is
28 // blank, we send a blank password - this is for backwards
29 // compatibility with password-less players).
30 std::string translatePassword(std::string playername, std::wstring password)
31 {
32         if(password.length() == 0)
33                 return "";
34
35         std::string slt = playername + wide_to_narrow(password);
36         SHA1 sha1;
37         sha1.addBytes(slt.c_str(), slt.length());
38         unsigned char *digest = sha1.getDigest();
39         std::string pwd = base64_encode(digest, 20);
40         free(digest);
41         return pwd;
42 }
43
44 size_t curl_write_data(char *ptr, size_t size, size_t nmemb, void *userdata) {
45     std::ostringstream *stream = (std::ostringstream*)userdata;
46     size_t count = size * nmemb;
47     stream->write(ptr, count);
48     return count;
49 }
50
51 char *mystrtok_r(char *s, const char *sep, char **lasts) {
52         char *t;
53         int delim_reached;
54
55         if (!s)
56                 s = *lasts;
57
58         while (*s && strchr(sep, *s))
59                 s++;
60
61         if (!*s)
62                 return NULL;
63
64         delim_reached = 0;
65         t = s;
66         while (*t) {
67                 if (strchr(sep, *t)) {
68                         *t = '\0';
69                         delim_reached = 1;
70                 } else if (delim_reached) {
71                         *lasts = t;
72                         return s;
73                 }
74                 t++;
75         }
76         
77         *lasts = t;
78         return s;
79 }