#include <rdr/Exception.h>
#include <rdr/TLSException.h>
+#include <rfb/util.h>
+
#ifdef _WIN32
#include <winsock2.h>
#include <windows.h>
#include <string.h>
-#ifdef HAVE_GNUTLS
-#include <gnutls/gnutls.h>
-#endif
-
using namespace rdr;
Exception::Exception(const char *message)
{
- snprintf(str_, len, "%s", message);
+ snprintf(str_, sizeof(str_), "%s", message);
}
Exception::Exception(const std::string& message)
{
- snprintf(str_, len, "%s", message.c_str());
+ snprintf(str_, sizeof(str_), "%s", message.c_str());
}
GAIException::GAIException(const char* s, int err_)
- : Exception(s), err(err_)
+ : Exception(rfb::format("%s: %s (%d)", s, strerror(err_).c_str(), err_)),
+ err(err_)
+{
+}
+
+std::string GAIException::strerror(int err_) const
{
- strncat(str_, ": ", len-1-strlen(str_));
#ifdef _WIN32
- wchar_t *currStr = new wchar_t[len-strlen(str_)];
- wcsncpy(currStr, gai_strerrorW(err), len-1-strlen(str_));
- WideCharToMultiByte(CP_UTF8, 0, currStr, -1, str_+strlen(str_),
- len-1-strlen(str_), nullptr, nullptr);
- delete [] currStr;
+ char str[256];
+
+ WideCharToMultiByte(CP_UTF8, 0, gai_strerrorW(err_), -1, str,
+ sizeof(str), nullptr, nullptr);
+
+ return str;
#else
- strncat(str_, gai_strerror(err), len-1-strlen(str_));
+ return gai_strerror(err_);
#endif
- strncat(str_, " (", len-1-strlen(str_));
- char buf[20];
-#ifdef WIN32
- if (err < 0)
- sprintf(buf, "%x", err);
- else
-#endif
- sprintf(buf,"%d",err);
- strncat(str_, buf, len-1-strlen(str_));
- strncat(str_, ")", len-1-strlen(str_));
}
PosixException::PosixException(const char* s, int err_)
- : Exception(s), err(err_)
+ : Exception(rfb::format("%s: %s (%d)", s, strerror(err_).c_str(), err_)),
+ err(err_)
+{
+}
+
+std::string PosixException::strerror(int err_) const
{
- strncat(str_, ": ", len-1-strlen(str_));
#ifdef _WIN32
- wchar_t *currStr = new wchar_t[len-strlen(str_)];
- wcsncpy(currStr, _wcserror(err), len-1-strlen(str_));
- WideCharToMultiByte(CP_UTF8, 0, currStr, -1, str_+strlen(str_),
- len-1-strlen(str_), nullptr, nullptr);
- delete [] currStr;
+ char str[256];
+
+ WideCharToMultiByte(CP_UTF8, 0, _wcserror(err_), -1, str,
+ sizeof(str), nullptr, nullptr);
+
+ return str;
#else
- strncat(str_, strerror(err), len-1-strlen(str_));
+ return ::strerror(err_);
#endif
- strncat(str_, " (", len-1-strlen(str_));
- char buf[20];
- sprintf(buf,"%d",err);
- strncat(str_, buf, len-1-strlen(str_));
- strncat(str_, ")", len-1-strlen(str_));
}
#ifdef WIN32
Win32Exception::Win32Exception(const char* s, unsigned err_)
- : Exception(s), err(err_)
+ : Exception(rfb::format("%s: %s (%d)", s, strerror(err_).c_str(), err_)),
+ err(err_)
+{
+}
+
+std::string Win32Exception::strerror(unsigned err_) const
{
- strncat(str_, ": ", len-1-strlen(str_));
- wchar_t *currStr = new wchar_t[len-strlen(str_)];
+ wchar_t wstr[256];
+ char str[256];
+
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
- nullptr, err, 0, currStr, len-1-strlen(str_), nullptr);
- WideCharToMultiByte(CP_UTF8, 0, currStr, -1, str_+strlen(str_),
- len-1-strlen(str_), nullptr, nullptr);
- delete [] currStr;
-
- int l = strlen(str_);
- if ((l >= 2) && (str_[l-2] == '\r') && (str_[l-1] == '\n'))
- str_[l-2] = 0;
-
- strncat(str_, " (", len-1-strlen(str_));
- char buf[20];
- sprintf(buf,"%d",err);
- strncat(str_, buf, len-1-strlen(str_));
- strncat(str_, ")", len-1-strlen(str_));
+ nullptr, err_, 0, wstr, sizeof(wstr), nullptr);
+ WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str,
+ sizeof(str), nullptr, nullptr);
+
+ int l = strlen(str);
+ if ((l >= 2) && (str[l-2] == '\r') && (str[l-1] == '\n'))
+ str[l-2] = 0;
+
+ return str;
}
#endif