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.
28 #include "../base64.h"
30 #include "../porting.h"
32 std::wstring narrow_to_wide(const std::string& mbs)
34 size_t wcl = mbs.size();
35 Buffer<wchar_t> wcs(wcl+1);
36 size_t l = mbstowcs(*wcs, mbs.c_str(), wcl);
38 return L"<invalid multibyte string>";
43 std::string wide_to_narrow(const std::wstring& wcs)
45 size_t mbl = wcs.size()*4;
46 SharedBuffer<char> mbs(mbl+1);
47 size_t l = wcstombs(*mbs, wcs.c_str(), mbl);
48 if(l == (size_t)(-1)) {
49 return "Character conversion failed!";
56 // Get an sha-1 hash of the player's name combined with
57 // the password entered. That's what the server uses as
58 // their password. (Exception : if the password field is
59 // blank, we send a blank password - this is for backwards
60 // compatibility with password-less players).
61 std::string translatePassword(std::string playername, std::wstring password)
63 if(password.length() == 0)
66 std::string slt = playername + wide_to_narrow(password);
68 sha1.addBytes(slt.c_str(), slt.length());
69 unsigned char *digest = sha1.getDigest();
70 std::string pwd = base64_encode(digest, 20);
75 std::string urlencode(std::string str)
77 // Encodes non-unreserved URI characters by a percent sign
78 // followed by two hex digits. See RFC 3986, section 2.3.
79 static const char url_hex_chars[] = "0123456789ABCDEF";
80 std::ostringstream oss(std::ios::binary);
81 for (u32 i = 0; i < str.size(); i++) {
82 unsigned char c = str[i];
83 if (isalnum(c) || c == '-' || c == '.' || c == '_' || c == '~')
87 << url_hex_chars[(c & 0xf0) >> 4]
88 << url_hex_chars[c & 0x0f];
93 std::string urldecode(std::string str)
95 // Inverse of urlencode
96 std::ostringstream oss(std::ios::binary);
97 for (u32 i = 0; i < str.size(); i++) {
98 unsigned char highvalue, lowvalue;
100 hex_digit_decode(str[i+1], highvalue) &&
101 hex_digit_decode(str[i+2], lowvalue)) {
102 oss << (char) ((highvalue << 4) | lowvalue);
111 u32 readFlagString(std::string str, FlagDesc *flagdesc) {
114 char *flagstr, *strpos = NULL;
116 while ((flagstr = strtok_r(s, ",", &strpos))) {
119 while (*flagstr == ' ' || *flagstr == '\t')
122 for (int i = 0; flagdesc[i].name; i++) {
123 if (!strcasecmp(flagstr, flagdesc[i].name)) {
124 result |= flagdesc[i].flag;
133 std::string writeFlagString(u32 flags, FlagDesc *flagdesc) {
136 for (int i = 0; flagdesc[i].name; i++) {
137 if (flags & flagdesc[i].flag) {
138 result += flagdesc[i].name;
143 size_t len = result.length();
145 result.erase(len - 2, 2);
150 char *mystrtok_r(char *s, const char *sep, char **lasts) {
156 while (*s && strchr(sep, *s))
164 if (strchr(sep, *t)) {
175 u64 read_seed(const char *str) {
179 if (str[0] == '0' && str[1] == 'x')
180 num = strtoull(str, &endptr, 16);
182 num = strtoull(str, &endptr, 10);
185 num = murmur_hash_64_ua(str, (int)strlen(str), 0x1337);