void DS::Stream::writeSafeString(const ST::string& value, DS::StringType format) { if (format == e_StringUTF16) { ST::utf16_buffer buffer = value.to_utf16(); uint16_t length = value.size() & 0x0FFF; ST::utf16_buffer work; char16_t* data = work.create_writable_buffer(length); memcpy(data, buffer.data(), length * sizeof(char16_t)); for (uint16_t i=0; i<length; ++i) data[i] = ~data[i]; write<uint16_t>(length | 0xF000); writeBytes(data, length * sizeof(char16_t)); write<char16_t>(0); } else { ST::char_buffer buffer = (format == e_StringUTF8) ? value.to_utf8() : value.to_latin_1(ST::substitute_invalid); uint16_t length = value.size() & 0x0FFF; ST::char_buffer work; char* data = work.create_writable_buffer(length); memcpy(data, buffer.data(), length * sizeof(char)); for (uint16_t i=0; i<length; ++i) data[i] = ~data[i]; write<uint16_t>(length | 0xF000); writeBytes(data, length * sizeof(char)); } }
ST::string cdUp(ST::string path) { // Check for root paths, we can't go up from there! #ifdef _WIN32 if (path.substr(1) == ":\\") return path; #else if (path == "/") return path; #endif // Not very robust, but it works for one level of parent scanning if (path.is_empty()) return ".." PATHSEPSTR; // Strip the ending slash, if necessary, and then go up one dir if (path.char_at(path.size()-1) == PATHSEP) path = path.left(path.size() - 1); ST::string up = path.before_last(PATHSEP); if (path.char_at(0) == PATHSEP) { // Absolute path specified -- make sure we keep it that way return up + PATHSEPSTR; } else { // Relative path specified return up.is_empty() ? "" : up + PATHSEPSTR; } }
/***************************************************************************** ** pfMacPasswordStore ** *****************************************************************************/ ST::string pfMacPasswordStore::GetPassword(const ST::string& username) { ST::string service = GetServerDisplayName(); void* passwd = nullptr; uint32_t passwd_len = 0; if (SecKeychainFindGenericPassword(nullptr, service.size(), service.c_str(), username.size(), username.c_str(), &passwd_len, &passwd, nullptr) != errSecSuccess) { return ST::null; } ST::string ret(reinterpret_cast<const char*>(passwd), size_t(passwd_len)); SecKeychainItemFreeContent(nullptr, passwd); return ret; }
ST::string CleanFileName(const ST::string& fname) { ST::char_buffer result; char* buf = result.create_writable_buffer(fname.size()); memcpy(buf, fname.c_str(), fname.size() + 1); for (char* bp = buf; *bp; bp++) { if (*bp == '?' || *bp == '*' || *bp == '<' || *bp == '>' || *bp == '"' || *bp == '|' || *bp < (char)0x20) *bp = '_'; if (*bp == '/' || *bp == '\\' || *bp == ':') *bp = '_'; } return result; }
ST::string FixSlashes(const ST::string& src) { if (src.is_empty()) return src; ST::char_buffer dest; char* pbuf = dest.create_writable_buffer(src.size()); memcpy(pbuf, src.c_str(), src.size() + 1); for (char* pc = pbuf; *pc != 0; pc++) { if (*pc == '/' || *pc == '\\') *pc = PATHSEP; } return dest; }
bool pfMacPasswordStore::SetPassword(const ST::string& username, const ST::string& password) { ST::string service = GetServerDisplayName(); return SecKeychainAddGenericPassword(nullptr, service.size(), service.c_str(), username.size(), username.c_str(), password.size(), password.c_str(), nullptr) == errSecSuccess; }