family = pattern.address.u.sa.sa_family;
if (pattern.prefixlen > (family == AF_INET ? 32: 128))
- throw Exception("invalid prefix length for filter address: %u",
- pattern.prefixlen);
+ throw Exception(rfb::format("invalid prefix length for filter address: %u",
+ pattern.prefixlen));
// Compute mask from address and prefix length
memset (&pattern.mask, 0, sizeof (pattern.mask));
#include <rdr/BufferedInStream.h>
#include <rdr/Exception.h>
+#include <rfb/util.h>
+
using namespace rdr;
static const size_t DEFAULT_BUF_SIZE = 8192;
uint8_t* newBuffer;
if (needed > MAX_BUF_SIZE)
- throw Exception("BufferedInStream overrun: requested size of "
- "%lu bytes exceeds maximum of %lu bytes",
- (long unsigned)needed, (long unsigned)MAX_BUF_SIZE);
+ throw Exception(rfb::format("BufferedInStream overrun: requested "
+ "size of %lu bytes exceeds maximum "
+ "of %lu bytes", (long unsigned)needed,
+ (long unsigned)MAX_BUF_SIZE));
newSize = DEFAULT_BUF_SIZE;
while (newSize < needed)
#include <rdr/BufferedOutStream.h>
#include <rdr/Exception.h>
+#include <rfb/util.h>
using namespace rdr;
// We'll need to allocate more buffer space...
if (totalNeeded > MAX_BUF_SIZE)
- throw Exception("BufferedOutStream overrun: requested size of "
- "%lu bytes exceeds maximum of %lu bytes",
- (long unsigned)totalNeeded,
- (long unsigned)MAX_BUF_SIZE);
+ throw Exception(rfb::format("BufferedOutStream overrun: requested "
+ "size of %lu bytes exceeds maximum of "
+ "%lu bytes", (long unsigned)totalNeeded,
+ (long unsigned)MAX_BUF_SIZE));
newSize = DEFAULT_BUF_SIZE;
while (newSize < totalNeeded)
using namespace rdr;
-Exception::Exception(const char *format, ...) {
- va_list ap;
+Exception::Exception(const char *message)
+{
+ snprintf(str_, len, "%s", message);
+}
- va_start(ap, format);
- (void) vsnprintf(str_, len, format, ap);
- va_end(ap);
+Exception::Exception(const std::string& message)
+{
+ snprintf(str_, len, "%s", message.c_str());
}
+
GAIException::GAIException(const char* s, int err_)
- : Exception("%s", s), err(err_)
+ : Exception(s), err(err_)
{
strncat(str_, ": ", len-1-strlen(str_));
#ifdef _WIN32
}
PosixException::PosixException(const char* s, int err_)
- : Exception("%s", s), err(err_)
+ : Exception(s), err(err_)
{
strncat(str_, ": ", len-1-strlen(str_));
#ifdef _WIN32
#ifdef WIN32
Win32Exception::Win32Exception(const char* s, unsigned err_)
- : Exception("%s", s), err(err_)
+ : Exception(s), err(err_)
{
strncat(str_, ": ", len-1-strlen(str_));
wchar_t *currStr = new wchar_t[len-strlen(str_)];
#ifndef __RDR_EXCEPTION_H__
#define __RDR_EXCEPTION_H__
+#include <string>
+
namespace rdr {
struct Exception {
enum { len = 256 };
char str_[len];
- Exception(const char *format=nullptr, ...)
- __attribute__((__format__ (__printf__, 2, 3)));
+ Exception(const char* message);
+ Exception(const std::string& message);
virtual ~Exception() {}
virtual const char* str() const { return str_; }
};
#include <rdr/TLSException.h>
+#include <rfb/util.h>
+
#include <string.h>
#include <stdio.h>
#ifdef HAVE_GNUTLS
#ifdef HAVE_GNUTLS
TLSException::TLSException(const char* s, int err_)
- : Exception("%s: %s (%d)", s, gnutls_strerror(err_), err_), err(err_)
+ : Exception(rfb::format("%s: %s (%d)", s, gnutls_strerror(err_), err_)),
+ err(err_)
{
}
#endif /* HAVE_GNUTLS */
vlog.error("Server gave unsupported RFB protocol version %d.%d",
server.majorVersion, server.minorVersion);
state_ = RFBSTATE_INVALID;
- throw Exception("Server gave unsupported RFB protocol version %d.%d",
- server.majorVersion, server.minorVersion);
+ throw Exception(format("Server gave unsupported RFB protocol version %d.%d",
+ server.majorVersion, server.minorVersion));
} else if (server.beforeVersion(3,7)) {
server.setVersion(3,3);
} else if (server.afterVersion(3,8)) {
ret = readEndOfContinuousUpdates();
break;
default:
- throw Exception("Unknown message type %d", currentMsgType);
+ throw Exception(format("Unknown message type %d", currentMsgType));
}
if (ret)
gnutls_free(status_str.data);
- throw Exception("Invalid server certificate: %s", error.c_str());
+ throw Exception(format("Invalid server certificate: %s",
+ error.c_str()));
}
err = gnutls_certificate_verification_status_print(status,
#include <rfb/ledStates.h>
#include <rfb/clipboardTypes.h>
#include <rfb/ClientParams.h>
+#include <rfb/util.h>
using namespace rfb;
return clipSizes[i];
}
- throw Exception("Invalid clipboard format 0x%x", format);
+ throw Exception(rfb::format("Invalid clipboard format 0x%x", format));
}
void ClientParams::setClipboardCaps(uint32_t flags, const uint32_t* lengths)
if (!decoder->readRect(r, conn->getInStream(), conn->server, bufferStream))
return false;
} catch (rdr::Exception& e) {
- throw Exception("Error reading rect: %s", e.str());
+ throw Exception(format("Error reading rect: %s", e.str()));
}
stats[encoding].rects++;
if (threadException != nullptr)
return;
- threadException = new rdr::Exception("Exception on worker thread: %s", e.str());
+ threadException = new rdr::Exception(format("Exception on worker thread: %s", e.str()));
}
void DecodeManager::throwThreadException()
typedef rdr::Exception Exception;
struct AuthFailureException : public Exception {
AuthFailureException(const char* reason)
- : Exception("%s", reason) {}
+ : Exception(reason) {}
+ AuthFailureException(std::string& reason)
+ : Exception(reason) {}
};
struct AuthCancelledException : public rfb::Exception {
AuthCancelledException()
if(setjmp(err->jmpBuffer)) {
// this will execute if libjpeg has an error
- throw rdr::Exception("%s", err->lastError);
+ throw rdr::Exception(err->lastError);
}
jpeg_create_compress(cinfo);
jpeg_abort_compress(cinfo);
if (srcBufIsTemp && srcBuf) delete[] srcBuf;
if (rowPointer) delete[] rowPointer;
- throw rdr::Exception("%s", err->lastError);
+ throw rdr::Exception(err->lastError);
}
cinfo->image_width = w;
if(setjmp(err->jmpBuffer)) {
// this will execute if libjpeg has an error
- throw rdr::Exception("%s", err->lastError);
+ throw rdr::Exception(err->lastError);
}
jpeg_create_decompress(dinfo);
jpeg_abort_decompress(dinfo);
if (dstBufIsTemp && dstBuf) delete[] dstBuf;
if (rowPointer) delete[] rowPointer;
- throw rdr::Exception("%s", err->lastError);
+ throw rdr::Exception(err->lastError);
}
src->pub.next_input_byte = jpegBuf;
#include <rfb/Exception.h>
#include <rfb/LogWriter.h>
#include <rfb/PixelBuffer.h>
+#include <rfb/util.h>
using namespace rfb;
const uint8_t* end;
if (!r.enclosed_by(getRect()))
- throw rfb::Exception("Source rect %dx%d at %d,%d exceeds framebuffer %dx%d",
- r.width(), r.height(),
- r.tl.x, r.tl.y, width(), height());
+ throw rfb::Exception(rfb::format("Source rect %dx%d at %d,%d exceeds framebuffer %dx%d",
+ r.width(), r.height(),
+ r.tl.x, r.tl.y,
+ width(), height()));
data = getBuffer(r, &inStride);
}
if (!r.enclosed_by(getRect()))
- throw rfb::Exception("Source rect %dx%d at %d,%d exceeds framebuffer %dx%d",
- r.width(), r.height(),
- r.tl.x, r.tl.y, width(), height());
+ throw rfb::Exception(rfb::format("Source rect %dx%d at %d,%d exceeds framebuffer %dx%d",
+ r.width(), r.height(),
+ r.tl.x, r.tl.y,
+ width(), height()));
if (stride == 0)
stride = r.width();
void PixelBuffer::setSize(int width, int height)
{
if ((width < 0) || (width > maxPixelBufferWidth))
- throw rfb::Exception("Invalid PixelBuffer width of %d pixels requested", width);
+ throw rfb::Exception(rfb::format("Invalid PixelBuffer width of %d pixels requested", width));
if ((height < 0) || (height > maxPixelBufferHeight))
- throw rfb::Exception("Invalid PixelBuffer height of %d pixels requested", height);
+ throw rfb::Exception(rfb::format("Invalid PixelBuffer height of %d pixels requested", height));
width_ = width;
height_ = height;
int w, h, b;
if (!r.enclosed_by(getRect()))
- throw rfb::Exception("Destination rect %dx%d at %d,%d exceeds framebuffer %dx%d",
- r.width(), r.height(), r.tl.x, r.tl.y, width(), height());
+ throw rfb::Exception(rfb::format("Destination rect %dx%d at %d,%d exceeds framebuffer %dx%d",
+ r.width(), r.height(),
+ r.tl.x, r.tl.y,
+ width(), height()));
w = r.width();
h = r.height();
uint8_t* end;
if (!r.enclosed_by(getRect()))
- throw rfb::Exception("Destination rect %dx%d at %d,%d exceeds framebuffer %dx%d",
- r.width(), r.height(),
- r.tl.x, r.tl.y, width(), height());
+ throw rfb::Exception(rfb::format("Destination rect %dx%d at %d,%d exceeds framebuffer %dx%d",
+ r.width(), r.height(),
+ r.tl.x, r.tl.y,
+ width(), height()));
bytesPerPixel = getPF().bpp/8;
drect = rect;
if (!drect.enclosed_by(getRect()))
- throw rfb::Exception("Destination rect %dx%d at %d,%d exceeds framebuffer %dx%d",
- drect.width(), drect.height(),
- drect.tl.x, drect.tl.y, width(), height());
+ throw rfb::Exception(rfb::format("Destination rect %dx%d at %d,%d exceeds framebuffer %dx%d",
+ drect.width(), drect.height(),
+ drect.tl.x, drect.tl.y,
+ width(), height()));
srect = drect.translate(move_by_delta.negate());
if (!srect.enclosed_by(getRect()))
- throw rfb::Exception("Source rect %dx%d at %d,%d exceeds framebuffer %dx%d",
- srect.width(), srect.height(),
- srect.tl.x, srect.tl.y, width(), height());
+ throw rfb::Exception(rfb::format("Source rect %dx%d at %d,%d exceeds framebuffer %dx%d",
+ srect.width(), srect.height(),
+ srect.tl.x, srect.tl.y,
+ width(), height()));
bytesPerPixel = format.bpp/8;
int dstStride;
if (!dest.enclosed_by(getRect()))
- throw rfb::Exception("Destination rect %dx%d at %d,%d exceeds framebuffer %dx%d",
- dest.width(), dest.height(),
- dest.tl.x, dest.tl.y, width(), height());
+ throw rfb::Exception(rfb::format("Destination rect %dx%d at %d,%d exceeds framebuffer %dx%d",
+ dest.width(), dest.height(),
+ dest.tl.x, dest.tl.y,
+ width(), height()));
if (stride == 0)
stride = dest.width();
uint8_t* FullFramePixelBuffer::getBufferRW(const Rect& r, int* stride_)
{
if (!r.enclosed_by(getRect()))
- throw rfb::Exception("Pixel buffer request %dx%d at %d,%d exceeds framebuffer %dx%d",
- r.width(), r.height(),
- r.tl.x, r.tl.y, width(), height());
+ throw rfb::Exception(rfb::format("Pixel buffer request %dx%d at %d,%d exceeds framebuffer %dx%d",
+ r.width(), r.height(),
+ r.tl.x, r.tl.y,
+ width(), height()));
*stride_ = stride;
return &data[(r.tl.x + (r.tl.y * stride)) * (format.bpp/8)];
const uint8_t* FullFramePixelBuffer::getBuffer(const Rect& r, int* stride_) const
{
if (!r.enclosed_by(getRect()))
- throw rfb::Exception("Pixel buffer request %dx%d at %d,%d exceeds framebuffer %dx%d",
- r.width(), r.height(),
- r.tl.x, r.tl.y, width(), height());
+ throw rfb::Exception(rfb::format("Pixel buffer request %dx%d at %d,%d exceeds framebuffer %dx%d",
+ r.width(), r.height(),
+ r.tl.x, r.tl.y,
+ width(), height()));
*stride_ = stride;
return &data[(r.tl.x + (r.tl.y * stride)) * (format.bpp/8)];
uint8_t* data_, int stride_)
{
if ((width < 0) || (width > maxPixelBufferWidth))
- throw rfb::Exception("Invalid PixelBuffer width of %d pixels requested", width);
+ throw rfb::Exception(rfb::format("Invalid PixelBuffer width of %d pixels requested", width));
if ((height < 0) || (height > maxPixelBufferHeight))
- throw rfb::Exception("Invalid PixelBuffer height of %d pixels requested", height);
+ throw rfb::Exception(rfb::format("Invalid PixelBuffer height of %d pixels requested", height));
if ((stride_ < 0) || (stride_ > maxPixelBufferStride) || (stride_ < width))
- throw rfb::Exception("Invalid PixelBuffer stride of %d pixels requested", stride_);
+ throw rfb::Exception(rfb::format("Invalid PixelBuffer stride of %d pixels requested", stride_));
if ((width != 0) && (height != 0) && (data_ == nullptr))
- throw rfb::Exception("PixelBuffer requested without a valid memory area");
+ throw rfb::Exception(rfb::format("PixelBuffer requested without a valid memory area"));
ModifiablePixelBuffer::setSize(width, height);
stride = stride_;
if (client.majorVersion != 3) {
// unknown protocol version
- failConnection("Client needs protocol version %d.%d, server has %d.%d",
- client.majorVersion, client.minorVersion,
- defaultMajorVersion, defaultMinorVersion);
+ failConnection(format("Client needs protocol version %d.%d, "
+ "server has %d.%d",
+ client.majorVersion, client.minorVersion,
+ defaultMajorVersion, defaultMinorVersion));
}
if (client.minorVersion != 3 && client.minorVersion != 7 && client.minorVersion != 8) {
if (*i == secTypeNone || *i == secTypeVncAuth) break;
}
if (i == secTypes.end()) {
- failConnection("No supported security type for %d.%d client",
- client.majorVersion, client.minorVersion);
+ failConnection(format("No supported security type for "
+ "%d.%d client",
+ client.majorVersion, client.minorVersion));
}
os->writeU32(*i);
state_ = RFBSTATE_SECURITY;
ssecurity = security.GetSSecurity(this, secType);
} catch (rdr::Exception& e) {
- failConnection("%s", e.str());
+ failConnection(e.str());
}
}
close(authFailureMsg.c_str());
}
-void SConnection::failConnection(const char* format, ...)
+void SConnection::failConnection(const char* message)
{
- va_list ap;
- char str[256];
-
- va_start(ap, format);
- (void) vsnprintf(str, sizeof(str), format, ap);
- va_end(ap);
-
- vlog.info("Connection failed: %s", str);
+ vlog.info("Connection failed: %s", message);
if (state_ == RFBSTATE_PROTOCOL_VERSION) {
if (client.majorVersion == 3 && client.minorVersion == 3) {
os->writeU32(0);
- os->writeU32(strlen(str));
- os->writeBytes((const uint8_t*)str, strlen(str));
+ os->writeU32(strlen(message));
+ os->writeBytes((const uint8_t*)message, strlen(message));
os->flush();
} else {
os->writeU8(0);
- os->writeU32(strlen(str));
- os->writeBytes((const uint8_t*)str, strlen(str));
+ os->writeU32(strlen(message));
+ os->writeBytes((const uint8_t*)message, strlen(message));
os->flush();
}
}
state_ = RFBSTATE_INVALID;
- throw Exception("%s", str);
+ throw Exception(message);
+}
+
+void SConnection::failConnection(const std::string& message)
+{
+ failConnection(message.c_str());
}
void SConnection::setAccessRights(AccessRights ar)
// failConnection() prints a message to the log, sends a connection
// failed message to the client (if possible) and throws an
// Exception.
- void failConnection(const char* format, ...)
- __attribute__((__format__ (__printf__, 2, 3)));
+ void failConnection(const char* message);
+ void failConnection(const std::string& message);
void setState(stateEnum s) { state_ = s; }
ret = readQEMUKeyEvent();
break;
default:
- throw Exception("unknown QEMU submessage type %d", subType);
+ throw Exception(format("unknown QEMU submessage type %d", subType));
}
if (!ret) {
#include <rfb/Exception.h>
#include <rfb/ledStates.h>
#include <rfb/ServerParams.h>
+#include <rfb/util.h>
using namespace rfb;
return clipSizes[i];
}
- throw Exception("Invalid clipboard format 0x%x", format);
+ throw Exception(rfb::format("Invalid clipboard format 0x%x", format));
}
void ServerParams::setClipboardCaps(uint32_t flags, const uint32_t* lengths)
#include <rfb/PixelBuffer.h>
#include <rfb/TightConstants.h>
#include <rfb/TightDecoder.h>
+#include <rfb/util.h>
using namespace rfb;
int palSize = 0;
if (r.width() > TIGHT_MAX_WIDTH)
- throw Exception("TightDecoder: too large rectangle (%d pixels)", r.width());
+ throw Exception(format("TightDecoder: too large rectangle (%d pixels)", r.width()));
// Possible palette
if ((comp_ctl & tightExplicitFilter) != 0) {
if (!XkbQueryExtension(dpy, &xkbOpcode, &xkbEventBase,
&xkbErrorBase, &major, &minor)) {
vlog.error("XKEYBOARD extension not present");
- throw Exception();
+ throw Exception("XKEYBOARD extension not present");
}
XkbSelectEvents(dpy, XkbUseCoreKbd, XkbIndicatorStateNotifyMask,
if (len == (sizeof(line) - 1)) {
fclose(f);
- throw Exception(_("Failed to read line %d in file %s: %s"),
- lineNr, filepath, _("Line too long"));
+ throw Exception(format(_("Failed to read line %d in file %s: %s"),
+ lineNr, filepath, _("Line too long")));
}
if ((len > 0) && (line[len-1] == '\n')) {
setKeyString("ServerName", servername, &hKey);
} catch (Exception& e) {
RegCloseKey(hKey);
- throw Exception(_("Failed to save \"%s\": %s"),
- "ServerName", e.str());
+ throw Exception(format(_("Failed to save \"%s\": %s"),
+ "ServerName", e.str()));
}
for (size_t i = 0; i < sizeof(parameterArray)/sizeof(VoidParameter*); i++) {
}
} catch (Exception& e) {
RegCloseKey(hKey);
- throw Exception(_("Failed to save \"%s\": %s"),
- parameterArray[i]->getName(), e.str());
+ throw Exception(format(_("Failed to save \"%s\": %s"),
+ parameterArray[i]->getName(), e.str()));
}
}
removeValue(readOnlyParameterArray[i]->getName(), &hKey);
} catch (Exception& e) {
RegCloseKey(hKey);
- throw Exception(_("Failed to remove \"%s\": %s"),
- readOnlyParameterArray[i]->getName(), e.str());
+ throw Exception(format(_("Failed to remove \"%s\": %s"),
+ readOnlyParameterArray[i]->getName(),
+ e.str()));
}
}
if (!encodeValue(servername, encodingBuffer, buffersize)) {
fclose(f);
- throw Exception(_("Failed to save \"%s\": %s"),
- "ServerName", _("Could not encode parameter"));
+ throw Exception(format(_("Failed to save \"%s\": %s"),
+ "ServerName",
+ _("Could not encode parameter")));
}
fprintf(f, "ServerName=%s\n", encodingBuffer);
if (!encodeValue(*(StringParameter*)param,
encodingBuffer, buffersize)) {
fclose(f);
- throw Exception(_("Failed to save \"%s\": %s"),
- param->getName(),
- _("Could not encode parameter"));
+ throw Exception(format(_("Failed to save \"%s\": %s"),
+ param->getName(),
+ _("Could not encode parameter")));
}
fprintf(f, "%s=%s\n", ((StringParameter*)param)->getName(), encodingBuffer);
} else if (dynamic_cast<IntParameter*>(param) != nullptr) {
fprintf(f, "%s=%d\n", ((BoolParameter*)param)->getName(), (int)*(BoolParameter*)param);
} else {
fclose(f);
- throw Exception(_("Failed to save \"%s\": %s"),
- param->getName(),
- _("Unknown parameter type"));
+ throw Exception(format(_("Failed to save \"%s\": %s"),
+ param->getName(),
+ _("Unknown parameter type")));
}
}
fclose(f);
if (strlen(line) == (sizeof(line) - 1)) {
fclose(f);
- throw Exception(_("Failed to read line %d in file %s: %s"),
- lineNr, filepath, _("Line too long"));
+ throw Exception(format(_("Failed to read line %d in file %s: %s"),
+ lineNr, filepath, _("Line too long")));
}
// Make sure that the first line of the file has the file identifier string
continue;
fclose(f);
- throw Exception(_("Configuration file %s is in an invalid format"),
- filepath);
+ throw Exception(format(_("Configuration file %s is in an invalid "
+ "format"), filepath));
}
// Skip empty lines and comments