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 = L" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
35 int wctomb(char *s, wchar_t wc)
37 for (unsigned int j = 0; j < (sizeof(wide_chars)/sizeof(wchar_t));j++) {
38 if (wc == wide_chars[j]) {
42 else if (wc == L'\n') {
50 int mbtowc(wchar_t *pwc, const char *s, size_t n)
52 std::wstring intermediate = narrow_to_wide(s);
54 if (intermediate.length() > 0) {
55 *pwc = intermediate[0];
63 std::wstring narrow_to_wide(const std::string& mbs) {
64 size_t wcl = mbs.size();
66 std::wstring retval = L"";
68 for (unsigned int i = 0; i < wcl; i++) {
69 if (((unsigned char) mbs[i] >31) &&
70 ((unsigned char) mbs[i] < 127)) {
72 retval += wide_chars[(unsigned char) mbs[i] -32];
75 else if (mbs[i] == '\n') {
84 std::wstring narrow_to_wide(const std::string& mbs)
86 size_t wcl = mbs.size();
87 Buffer<wchar_t> wcs(wcl+1);
88 size_t l = mbstowcs(*wcs, mbs.c_str(), wcl);
90 return L"<invalid multibyte string>";
98 std::string wide_to_narrow(const std::wstring& wcs) {
99 size_t mbl = wcs.size()*4;
101 std::string retval = "";
102 for (unsigned int i = 0; i < wcs.size(); i++) {
103 wchar_t char1 = (wchar_t) wcs[i];
105 if (char1 == L'\n') {
110 for (unsigned int j = 0; j < wcslen(wide_chars);j++) {
111 wchar_t char2 = (wchar_t) wide_chars[j];
113 if (char1 == char2) {
124 std::string wide_to_narrow(const std::wstring& wcs)
126 size_t mbl = wcs.size()*4;
127 SharedBuffer<char> mbs(mbl+1);
128 size_t l = wcstombs(*mbs, wcs.c_str(), mbl);
129 if(l == (size_t)(-1)) {
130 return "Character conversion failed!";
139 // Get an sha-1 hash of the player's name combined with
140 // the password entered. That's what the server uses as
141 // their password. (Exception : if the password field is
142 // blank, we send a blank password - this is for backwards
143 // compatibility with password-less players).
144 std::string translatePassword(std::string playername, std::wstring password)
146 if(password.length() == 0)
149 std::string slt = playername + wide_to_narrow(password);
151 sha1.addBytes(slt.c_str(), slt.length());
152 unsigned char *digest = sha1.getDigest();
153 std::string pwd = base64_encode(digest, 20);
158 std::string urlencode(std::string str)
160 // Encodes non-unreserved URI characters by a percent sign
161 // followed by two hex digits. See RFC 3986, section 2.3.
162 static const char url_hex_chars[] = "0123456789ABCDEF";
163 std::ostringstream oss(std::ios::binary);
164 for (u32 i = 0; i < str.size(); i++) {
165 unsigned char c = str[i];
166 if (isalnum(c) || c == '-' || c == '.' || c == '_' || c == '~')
170 << url_hex_chars[(c & 0xf0) >> 4]
171 << url_hex_chars[c & 0x0f];
176 std::string urldecode(std::string str)
178 // Inverse of urlencode
179 std::ostringstream oss(std::ios::binary);
180 for (u32 i = 0; i < str.size(); i++) {
181 unsigned char highvalue, lowvalue;
183 hex_digit_decode(str[i+1], highvalue) &&
184 hex_digit_decode(str[i+2], lowvalue)) {
185 oss << (char) ((highvalue << 4) | lowvalue);
194 u32 readFlagString(std::string str, FlagDesc *flagdesc, u32 *flagmask)
196 u32 result = 0, mask = 0;
198 char *flagstr, *strpos = NULL;
200 while ((flagstr = strtok_r(s, ",", &strpos))) {
203 while (*flagstr == ' ' || *flagstr == '\t')
207 if (!strncasecmp(flagstr, "no", 2)) {
212 for (int i = 0; flagdesc[i].name; i++) {
213 if (!strcasecmp(flagstr, flagdesc[i].name)) {
214 mask |= flagdesc[i].flag;
216 result |= flagdesc[i].flag;
228 std::string writeFlagString(u32 flags, FlagDesc *flagdesc, u32 flagmask)
232 for (int i = 0; flagdesc[i].name; i++) {
233 if (flagmask & flagdesc[i].flag) {
234 if (!(flags & flagdesc[i].flag))
237 result += flagdesc[i].name;
242 size_t len = result.length();
244 result.erase(len - 2, 2);
249 size_t mystrlcpy(char *dst, const char *src, size_t size)
251 size_t srclen = strlen(src) + 1;
252 size_t copylen = MYMIN(srclen, size);
255 memcpy(dst, src, copylen);
256 dst[copylen - 1] = '\0';
262 char *mystrtok_r(char *s, const char *sep, char **lasts)
269 while (*s && strchr(sep, *s))
277 if (strchr(sep, *t)) {
288 u64 read_seed(const char *str)
293 if (str[0] == '0' && str[1] == 'x')
294 num = strtoull(str, &endptr, 16);
296 num = strtoull(str, &endptr, 10);
299 num = murmur_hash_64_ua(str, (int)strlen(str), 0x1337);