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"
33 const wchar_t* wide_chars =
34 L" !\"#$%&'()*+,-./0123456789:;<=>?@"
35 L"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`"
36 L"abcdefghijklmnopqrstuvwxyz{|}~";
38 int wctomb(char *s, wchar_t wc)
40 for (unsigned int j = 0; j < (sizeof(wide_chars)/sizeof(wchar_t));j++) {
41 if (wc == wide_chars[j]) {
45 else if (wc == L'\n') {
53 int mbtowc(wchar_t *pwc, const char *s, size_t n)
55 std::wstring intermediate = narrow_to_wide(s);
57 if (intermediate.length() > 0) {
58 *pwc = intermediate[0];
66 std::wstring narrow_to_wide(const std::string& mbs) {
67 size_t wcl = mbs.size();
69 std::wstring retval = L"";
71 for (unsigned int i = 0; i < wcl; i++) {
72 if (((unsigned char) mbs[i] >31) &&
73 ((unsigned char) mbs[i] < 127)) {
75 retval += wide_chars[(unsigned char) mbs[i] -32];
78 else if (mbs[i] == '\n') {
87 std::wstring narrow_to_wide(const std::string& mbs)
89 size_t wcl = mbs.size();
90 Buffer<wchar_t> wcs(wcl+1);
91 size_t l = mbstowcs(*wcs, mbs.c_str(), wcl);
93 return L"<invalid multibyte string>";
101 std::string wide_to_narrow(const std::wstring& wcs) {
102 size_t mbl = wcs.size()*4;
104 std::string retval = "";
105 for (unsigned int i = 0; i < wcs.size(); i++) {
106 wchar_t char1 = (wchar_t) wcs[i];
108 if (char1 == L'\n') {
113 for (unsigned int j = 0; j < wcslen(wide_chars);j++) {
114 wchar_t char2 = (wchar_t) wide_chars[j];
116 if (char1 == char2) {
127 std::string wide_to_narrow(const std::wstring& wcs)
129 size_t mbl = wcs.size()*4;
130 SharedBuffer<char> mbs(mbl+1);
131 size_t l = wcstombs(*mbs, wcs.c_str(), mbl);
132 if(l == (size_t)(-1)) {
133 return "Character conversion failed!";
142 // Get an sha-1 hash of the player's name combined with
143 // the password entered. That's what the server uses as
144 // their password. (Exception : if the password field is
145 // blank, we send a blank password - this is for backwards
146 // compatibility with password-less players).
147 std::string translatePassword(std::string playername, std::wstring password)
149 if(password.length() == 0)
152 std::string slt = playername + wide_to_narrow(password);
154 sha1.addBytes(slt.c_str(), slt.length());
155 unsigned char *digest = sha1.getDigest();
156 std::string pwd = base64_encode(digest, 20);
161 std::string urlencode(std::string str)
163 // Encodes non-unreserved URI characters by a percent sign
164 // followed by two hex digits. See RFC 3986, section 2.3.
165 static const char url_hex_chars[] = "0123456789ABCDEF";
166 std::ostringstream oss(std::ios::binary);
167 for (u32 i = 0; i < str.size(); i++) {
168 unsigned char c = str[i];
169 if (isalnum(c) || c == '-' || c == '.' || c == '_' || c == '~')
173 << url_hex_chars[(c & 0xf0) >> 4]
174 << url_hex_chars[c & 0x0f];
179 std::string urldecode(std::string str)
181 // Inverse of urlencode
182 std::ostringstream oss(std::ios::binary);
183 for (u32 i = 0; i < str.size(); i++) {
184 unsigned char highvalue, lowvalue;
186 hex_digit_decode(str[i+1], highvalue) &&
187 hex_digit_decode(str[i+2], lowvalue)) {
188 oss << (char) ((highvalue << 4) | lowvalue);
197 u32 readFlagString(std::string str, const FlagDesc *flagdesc, u32 *flagmask)
199 u32 result = 0, mask = 0;
201 char *flagstr, *strpos = NULL;
203 while ((flagstr = strtok_r(s, ",", &strpos))) {
206 while (*flagstr == ' ' || *flagstr == '\t')
210 if (!strncasecmp(flagstr, "no", 2)) {
215 for (int i = 0; flagdesc[i].name; i++) {
216 if (!strcasecmp(flagstr, flagdesc[i].name)) {
217 mask |= flagdesc[i].flag;
219 result |= flagdesc[i].flag;
231 std::string writeFlagString(u32 flags, const FlagDesc *flagdesc, u32 flagmask)
235 for (int i = 0; flagdesc[i].name; i++) {
236 if (flagmask & flagdesc[i].flag) {
237 if (!(flags & flagdesc[i].flag))
240 result += flagdesc[i].name;
245 size_t len = result.length();
247 result.erase(len - 2, 2);
252 size_t mystrlcpy(char *dst, const char *src, size_t size)
254 size_t srclen = strlen(src) + 1;
255 size_t copylen = MYMIN(srclen, size);
258 memcpy(dst, src, copylen);
259 dst[copylen - 1] = '\0';
265 char *mystrtok_r(char *s, const char *sep, char **lasts)
272 while (*s && strchr(sep, *s))
280 if (strchr(sep, *t)) {
291 u64 read_seed(const char *str)
296 if (str[0] == '0' && str[1] == 'x')
297 num = strtoull(str, &endptr, 16);
299 num = strtoull(str, &endptr, 10);
302 num = murmur_hash_64_ua(str, (int)strlen(str), 0x1337);