3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
25 #include "../base64.h"
26 #include "../porting.h"
28 std::wstring narrow_to_wide(const std::string& mbs)
30 size_t wcl = mbs.size();
31 Buffer<wchar_t> wcs(wcl+1);
32 size_t l = mbstowcs(*wcs, mbs.c_str(), wcl);
34 return L"<invalid multibyte string>";
39 std::string wide_to_narrow(const std::wstring& wcs)
41 size_t mbl = wcs.size()*4;
42 SharedBuffer<char> mbs(mbl+1);
43 size_t l = wcstombs(*mbs, wcs.c_str(), mbl);
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)
58 if(password.length() == 0)
61 std::string slt = playername + wide_to_narrow(password);
63 sha1.addBytes(slt.c_str(), slt.length());
64 unsigned char *digest = sha1.getDigest();
65 std::string pwd = base64_encode(digest, 20);
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);
77 u32 readFlagString(std::string str, FlagDesc *flagdesc) {
80 char *flagstr, *strpos = NULL;
82 while ((flagstr = strtok_r(s, ",", &strpos))) {
85 while (*flagstr == ' ' || *flagstr == '\t')
88 for (int i = 0; flagdesc[i].name; i++) {
89 if (!strcasecmp(flagstr, flagdesc[i].name)) {
90 result |= flagdesc[i].flag;
99 std::string writeFlagString(u32 flags, FlagDesc *flagdesc) {
102 for (int i = 0; flagdesc[i].name; i++) {
103 if (flags & flagdesc[i].flag) {
104 result += flagdesc[i].name;
109 size_t len = result.length();
111 result.erase(len - 2, 2);
116 char *mystrtok_r(char *s, const char *sep, char **lasts) {
122 while (*s && strchr(sep, *s))
130 if (strchr(sep, *t)) {
141 u64 read_seed(const char *str) {
145 if (str[0] == '0' && str[1] == 'x')
146 num = strtoull(str, &endptr, 16);
148 num = strtoull(str, &endptr, 10);
151 num = murmur_hash_64_ua(str, (int)strlen(str), 0x1337);