2002-08-02 23:57:46 +04:00
|
|
|
/*
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
|
|
|
* Copyright (C) 2002 Chris Schoeneman
|
|
|
|
*
|
|
|
|
* This package is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* found in the file COPYING that should have accompanied this file.
|
|
|
|
*
|
|
|
|
* This package is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
2001-10-06 18:13:28 +04:00
|
|
|
#include "CProtocolUtil.h"
|
2004-02-02 00:09:22 +03:00
|
|
|
#include "IStream.h"
|
2001-10-14 18:56:06 +04:00
|
|
|
#include "CLog.h"
|
2002-12-23 16:55:21 +03:00
|
|
|
#include "stdvector.h"
|
2002-06-11 02:06:45 +04:00
|
|
|
#include <cctype>
|
|
|
|
#include <cstring>
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
//
|
|
|
|
// CProtocolUtil
|
|
|
|
//
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2004-02-02 00:09:22 +03:00
|
|
|
CProtocolUtil::writef(IStream* stream, const char* fmt, ...)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
assert(stream != NULL);
|
|
|
|
assert(fmt != NULL);
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG2 "writef(%s)", fmt));
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
va_list args;
|
2004-02-02 00:09:22 +03:00
|
|
|
va_start(args, fmt);
|
|
|
|
UInt32 size = getLength(fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
va_start(args, fmt);
|
|
|
|
vwritef(stream, fmt, size, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CProtocolUtil::readf(IStream* stream, const char* fmt, ...)
|
|
|
|
{
|
|
|
|
assert(stream != NULL);
|
|
|
|
assert(fmt != NULL);
|
|
|
|
LOG((CLOG_DEBUG2 "readf(%s)", fmt));
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2004-02-02 00:09:22 +03:00
|
|
|
bool result;
|
|
|
|
va_list args;
|
2001-10-06 18:13:28 +04:00
|
|
|
va_start(args, fmt);
|
2004-02-02 00:09:22 +03:00
|
|
|
try {
|
|
|
|
vreadf(stream, fmt, args);
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
catch (XIO&) {
|
|
|
|
result = false;
|
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
va_end(args);
|
2004-02-02 00:09:22 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CProtocolUtil::vwritef(IStream* stream,
|
|
|
|
const char* fmt, UInt32 size, va_list args)
|
|
|
|
{
|
|
|
|
assert(stream != NULL);
|
|
|
|
assert(fmt != NULL);
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
// done if nothing to write
|
2004-02-02 00:09:22 +03:00
|
|
|
if (size == 0) {
|
2001-10-06 18:13:28 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// fill buffer
|
2004-02-02 00:09:22 +03:00
|
|
|
UInt8* buffer = new UInt8[size];
|
2001-10-06 18:13:28 +04:00
|
|
|
writef(buffer, fmt, args);
|
|
|
|
|
2004-02-02 00:09:22 +03:00
|
|
|
try {
|
|
|
|
// write buffer
|
|
|
|
stream->write(buffer, size);
|
|
|
|
LOG((CLOG_DEBUG2 "wrote %d bytes", size));
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2004-02-02 00:09:22 +03:00
|
|
|
delete[] buffer;
|
|
|
|
}
|
|
|
|
catch (XBase&) {
|
|
|
|
delete[] buffer;
|
|
|
|
throw;
|
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2004-02-28 15:19:49 +03:00
|
|
|
void
|
2004-02-02 00:09:22 +03:00
|
|
|
CProtocolUtil::vreadf(IStream* stream, const char* fmt, va_list args)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
assert(stream != NULL);
|
|
|
|
assert(fmt != NULL);
|
|
|
|
|
|
|
|
// begin scanning
|
|
|
|
while (*fmt) {
|
|
|
|
if (*fmt == '%') {
|
|
|
|
// format specifier. determine argument size.
|
|
|
|
++fmt;
|
|
|
|
UInt32 len = eatLength(&fmt);
|
|
|
|
switch (*fmt) {
|
2002-04-29 18:40:01 +04:00
|
|
|
case 'i': {
|
2001-10-06 18:13:28 +04:00
|
|
|
// check for valid length
|
|
|
|
assert(len == 1 || len == 2 || len == 4);
|
|
|
|
|
|
|
|
// read the data
|
|
|
|
UInt8 buffer[4];
|
|
|
|
read(stream, buffer, len);
|
|
|
|
|
|
|
|
// convert it
|
2002-04-27 22:06:40 +04:00
|
|
|
void* v = va_arg(args, void*);
|
2001-10-06 18:13:28 +04:00
|
|
|
switch (len) {
|
2002-04-29 18:40:01 +04:00
|
|
|
case 1:
|
2001-10-06 18:13:28 +04:00
|
|
|
// 1 byte integer
|
2002-04-27 22:06:40 +04:00
|
|
|
*reinterpret_cast<UInt8*>(v) = buffer[0];
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG2 "readf: read %d byte integer: %d (0x%x)", len, *reinterpret_cast<UInt8*>(v), *reinterpret_cast<UInt8*>(v)));
|
2001-10-06 18:13:28 +04:00
|
|
|
break;
|
|
|
|
|
2002-04-29 18:40:01 +04:00
|
|
|
case 2:
|
2001-10-06 18:13:28 +04:00
|
|
|
// 2 byte integer
|
2002-04-27 22:06:40 +04:00
|
|
|
*reinterpret_cast<UInt16*>(v) =
|
2002-05-22 21:08:37 +04:00
|
|
|
static_cast<UInt16>(
|
2002-04-27 22:06:40 +04:00
|
|
|
(static_cast<UInt16>(buffer[0]) << 8) |
|
2002-05-22 21:08:37 +04:00
|
|
|
static_cast<UInt16>(buffer[1]));
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG2 "readf: read %d byte integer: %d (0x%x)", len, *reinterpret_cast<UInt16*>(v), *reinterpret_cast<UInt16*>(v)));
|
2001-10-06 18:13:28 +04:00
|
|
|
break;
|
|
|
|
|
2002-04-29 18:40:01 +04:00
|
|
|
case 4:
|
2001-10-06 18:13:28 +04:00
|
|
|
// 4 byte integer
|
2002-04-27 22:06:40 +04:00
|
|
|
*reinterpret_cast<UInt32*>(v) =
|
|
|
|
(static_cast<UInt32>(buffer[0]) << 24) |
|
|
|
|
(static_cast<UInt32>(buffer[1]) << 16) |
|
|
|
|
(static_cast<UInt32>(buffer[2]) << 8) |
|
|
|
|
static_cast<UInt32>(buffer[3]);
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG2 "readf: read %d byte integer: %d (0x%x)", len, *reinterpret_cast<UInt32*>(v), *reinterpret_cast<UInt32*>(v)));
|
2001-10-06 18:13:28 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2002-04-29 18:40:01 +04:00
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2002-12-23 16:55:21 +03:00
|
|
|
case 'I': {
|
|
|
|
// check for valid length
|
|
|
|
assert(len == 1 || len == 2 || len == 4);
|
|
|
|
|
|
|
|
// read the vector length
|
|
|
|
UInt8 buffer[4];
|
|
|
|
read(stream, buffer, 4);
|
2002-12-23 17:48:12 +03:00
|
|
|
UInt32 n = (static_cast<UInt32>(buffer[0]) << 24) |
|
|
|
|
(static_cast<UInt32>(buffer[1]) << 16) |
|
|
|
|
(static_cast<UInt32>(buffer[2]) << 8) |
|
|
|
|
static_cast<UInt32>(buffer[3]);
|
2002-12-23 16:55:21 +03:00
|
|
|
|
|
|
|
// convert it
|
|
|
|
void* v = va_arg(args, void*);
|
|
|
|
switch (len) {
|
|
|
|
case 1:
|
|
|
|
// 1 byte integer
|
2002-12-23 17:48:12 +03:00
|
|
|
for (UInt32 i = 0; i < n; ++i) {
|
|
|
|
read(stream, buffer, 1);
|
2002-12-23 16:55:21 +03:00
|
|
|
reinterpret_cast<std::vector<UInt8>*>(v)->push_back(
|
|
|
|
buffer[0]);
|
|
|
|
LOG((CLOG_DEBUG2 "readf: read %d byte integer[%d]: %d (0x%x)", len, i, reinterpret_cast<std::vector<UInt8>*>(v)->back(), reinterpret_cast<std::vector<UInt8>*>(v)->back()));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
// 2 byte integer
|
2002-12-23 17:48:12 +03:00
|
|
|
for (UInt32 i = 0; i < n; ++i) {
|
|
|
|
read(stream, buffer, 2);
|
2002-12-23 16:55:21 +03:00
|
|
|
reinterpret_cast<std::vector<UInt16>*>(v)->push_back(
|
|
|
|
static_cast<UInt16>(
|
|
|
|
(static_cast<UInt16>(buffer[0]) << 8) |
|
|
|
|
static_cast<UInt16>(buffer[1])));
|
|
|
|
LOG((CLOG_DEBUG2 "readf: read %d byte integer[%d]: %d (0x%x)", len, i, reinterpret_cast<std::vector<UInt16>*>(v)->back(), reinterpret_cast<std::vector<UInt16>*>(v)->back()));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
// 4 byte integer
|
2002-12-23 17:48:12 +03:00
|
|
|
for (UInt32 i = 0; i < n; ++i) {
|
|
|
|
read(stream, buffer, 4);
|
2002-12-23 16:55:21 +03:00
|
|
|
reinterpret_cast<std::vector<UInt32>*>(v)->push_back(
|
|
|
|
(static_cast<UInt32>(buffer[0]) << 24) |
|
|
|
|
(static_cast<UInt32>(buffer[1]) << 16) |
|
|
|
|
(static_cast<UInt32>(buffer[2]) << 8) |
|
|
|
|
static_cast<UInt32>(buffer[3]));
|
|
|
|
LOG((CLOG_DEBUG2 "readf: read %d byte integer[%d]: %d (0x%x)", len, i, reinterpret_cast<std::vector<UInt32>*>(v)->back(), reinterpret_cast<std::vector<UInt32>*>(v)->back()));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2002-04-29 18:40:01 +04:00
|
|
|
case 's': {
|
2001-10-06 18:13:28 +04:00
|
|
|
assert(len == 0);
|
|
|
|
|
|
|
|
// read the string length
|
|
|
|
UInt8 buffer[128];
|
|
|
|
read(stream, buffer, 4);
|
|
|
|
UInt32 len = (static_cast<UInt32>(buffer[0]) << 24) |
|
|
|
|
(static_cast<UInt32>(buffer[1]) << 16) |
|
|
|
|
(static_cast<UInt32>(buffer[2]) << 8) |
|
|
|
|
static_cast<UInt32>(buffer[3]);
|
|
|
|
|
|
|
|
// use a fixed size buffer if its big enough
|
|
|
|
const bool useFixed = (len <= sizeof(buffer));
|
|
|
|
|
|
|
|
// allocate a buffer to read the data
|
2004-04-11 23:15:09 +04:00
|
|
|
UInt8* sBuffer = buffer;
|
|
|
|
if (!useFixed) {
|
2001-10-06 18:13:28 +04:00
|
|
|
sBuffer = new UInt8[len];
|
|
|
|
}
|
|
|
|
|
|
|
|
// read the data
|
|
|
|
try {
|
|
|
|
read(stream, sBuffer, len);
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
if (!useFixed) {
|
|
|
|
delete[] sBuffer;
|
|
|
|
}
|
|
|
|
throw;
|
|
|
|
}
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG2 "readf: read %d byte string: %.*s", len, len, sBuffer));
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
// save the data
|
|
|
|
CString* dst = va_arg(args, CString*);
|
|
|
|
dst->assign((const char*)sBuffer, len);
|
|
|
|
|
|
|
|
// release the buffer
|
|
|
|
if (!useFixed) {
|
|
|
|
delete[] sBuffer;
|
|
|
|
}
|
|
|
|
break;
|
2002-04-29 18:40:01 +04:00
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2002-04-29 18:40:01 +04:00
|
|
|
case '%':
|
2001-10-06 18:13:28 +04:00
|
|
|
assert(len == 0);
|
|
|
|
break;
|
|
|
|
|
2002-04-29 18:40:01 +04:00
|
|
|
default:
|
2001-10-06 18:13:28 +04:00
|
|
|
assert(0 && "invalid format specifier");
|
|
|
|
}
|
|
|
|
|
|
|
|
// next format character
|
|
|
|
++fmt;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// read next character
|
|
|
|
char buffer[1];
|
|
|
|
read(stream, buffer, 1);
|
|
|
|
|
|
|
|
// verify match
|
|
|
|
if (buffer[0] != *fmt) {
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG2 "readf: format mismatch: %c vs %c", *fmt, buffer[0]));
|
2001-10-06 18:13:28 +04:00
|
|
|
throw XIOReadMismatch();
|
|
|
|
}
|
|
|
|
|
|
|
|
// next format character
|
|
|
|
++fmt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
UInt32
|
2002-06-17 17:31:21 +04:00
|
|
|
CProtocolUtil::getLength(const char* fmt, va_list args)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
UInt32 n = 0;
|
|
|
|
while (*fmt) {
|
|
|
|
if (*fmt == '%') {
|
|
|
|
// format specifier. determine argument size.
|
|
|
|
++fmt;
|
|
|
|
UInt32 len = eatLength(&fmt);
|
|
|
|
switch (*fmt) {
|
2002-04-29 18:40:01 +04:00
|
|
|
case 'i':
|
2001-10-06 18:13:28 +04:00
|
|
|
assert(len == 1 || len == 2 || len == 4);
|
|
|
|
(void)va_arg(args, UInt32);
|
|
|
|
break;
|
|
|
|
|
2002-12-23 16:55:21 +03:00
|
|
|
case 'I':
|
|
|
|
assert(len == 1 || len == 2 || len == 4);
|
|
|
|
switch (len) {
|
|
|
|
case 1:
|
|
|
|
len = (va_arg(args, std::vector<UInt8>*))->size() + 4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
2002-12-23 17:48:12 +03:00
|
|
|
len = 2 * (va_arg(args, std::vector<UInt16>*))->size() + 4;
|
2002-12-23 16:55:21 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
2002-12-23 17:48:12 +03:00
|
|
|
len = 4 * (va_arg(args, std::vector<UInt32>*))->size() + 4;
|
2002-12-23 16:55:21 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2002-04-29 18:40:01 +04:00
|
|
|
case 's':
|
2001-11-25 21:32:41 +03:00
|
|
|
assert(len == 0);
|
|
|
|
len = (va_arg(args, CString*))->size() + 4;
|
|
|
|
(void)va_arg(args, UInt8*);
|
|
|
|
break;
|
|
|
|
|
2002-04-29 18:40:01 +04:00
|
|
|
case 'S':
|
2001-10-06 18:13:28 +04:00
|
|
|
assert(len == 0);
|
|
|
|
len = va_arg(args, UInt32) + 4;
|
|
|
|
(void)va_arg(args, UInt8*);
|
|
|
|
break;
|
|
|
|
|
2002-04-29 18:40:01 +04:00
|
|
|
case '%':
|
2001-10-06 18:13:28 +04:00
|
|
|
assert(len == 0);
|
|
|
|
len = 1;
|
|
|
|
break;
|
|
|
|
|
2002-04-29 18:40:01 +04:00
|
|
|
default:
|
2001-10-06 18:13:28 +04:00
|
|
|
assert(0 && "invalid format specifier");
|
|
|
|
}
|
|
|
|
|
|
|
|
// accumulate size
|
|
|
|
n += len;
|
|
|
|
++fmt;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// regular character
|
|
|
|
++n;
|
|
|
|
++fmt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-06-17 17:31:21 +04:00
|
|
|
CProtocolUtil::writef(void* buffer, const char* fmt, va_list args)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
UInt8* dst = reinterpret_cast<UInt8*>(buffer);
|
|
|
|
|
|
|
|
while (*fmt) {
|
|
|
|
if (*fmt == '%') {
|
|
|
|
// format specifier. determine argument size.
|
|
|
|
++fmt;
|
|
|
|
UInt32 len = eatLength(&fmt);
|
|
|
|
switch (*fmt) {
|
2002-04-29 18:40:01 +04:00
|
|
|
case 'i': {
|
2001-10-06 18:13:28 +04:00
|
|
|
const UInt32 v = va_arg(args, UInt32);
|
|
|
|
switch (len) {
|
2002-04-29 18:40:01 +04:00
|
|
|
case 1:
|
2001-10-06 18:13:28 +04:00
|
|
|
// 1 byte integer
|
|
|
|
*dst++ = static_cast<UInt8>(v & 0xff);
|
|
|
|
break;
|
|
|
|
|
2002-04-29 18:40:01 +04:00
|
|
|
case 2:
|
2001-10-06 18:13:28 +04:00
|
|
|
// 2 byte integer
|
|
|
|
*dst++ = static_cast<UInt8>((v >> 8) & 0xff);
|
|
|
|
*dst++ = static_cast<UInt8>( v & 0xff);
|
|
|
|
break;
|
|
|
|
|
2002-04-29 18:40:01 +04:00
|
|
|
case 4:
|
2001-10-06 18:13:28 +04:00
|
|
|
// 4 byte integer
|
|
|
|
*dst++ = static_cast<UInt8>((v >> 24) & 0xff);
|
|
|
|
*dst++ = static_cast<UInt8>((v >> 16) & 0xff);
|
|
|
|
*dst++ = static_cast<UInt8>((v >> 8) & 0xff);
|
|
|
|
*dst++ = static_cast<UInt8>( v & 0xff);
|
|
|
|
break;
|
|
|
|
|
2002-04-29 18:40:01 +04:00
|
|
|
default:
|
2001-10-06 18:13:28 +04:00
|
|
|
assert(0 && "invalid integer format length");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
2002-04-29 18:40:01 +04:00
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2002-12-23 16:55:21 +03:00
|
|
|
case 'I': {
|
|
|
|
switch (len) {
|
|
|
|
case 1: {
|
|
|
|
// 1 byte integers
|
|
|
|
const std::vector<UInt8>* list =
|
|
|
|
va_arg(args, const std::vector<UInt8>*);
|
2002-12-23 17:48:12 +03:00
|
|
|
const UInt32 n = list->size();
|
|
|
|
*dst++ = static_cast<UInt8>((n >> 24) & 0xff);
|
|
|
|
*dst++ = static_cast<UInt8>((n >> 16) & 0xff);
|
|
|
|
*dst++ = static_cast<UInt8>((n >> 8) & 0xff);
|
|
|
|
*dst++ = static_cast<UInt8>( n & 0xff);
|
|
|
|
for (UInt32 i = 0; i < n; ++i) {
|
2002-12-23 16:55:21 +03:00
|
|
|
*dst++ = (*list)[i];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 2: {
|
|
|
|
// 2 byte integers
|
|
|
|
const std::vector<UInt16>* list =
|
|
|
|
va_arg(args, const std::vector<UInt16>*);
|
2002-12-23 17:48:12 +03:00
|
|
|
const UInt32 n = list->size();
|
|
|
|
*dst++ = static_cast<UInt8>((n >> 24) & 0xff);
|
|
|
|
*dst++ = static_cast<UInt8>((n >> 16) & 0xff);
|
|
|
|
*dst++ = static_cast<UInt8>((n >> 8) & 0xff);
|
|
|
|
*dst++ = static_cast<UInt8>( n & 0xff);
|
|
|
|
for (UInt32 i = 0; i < n; ++i) {
|
2002-12-23 16:55:21 +03:00
|
|
|
const UInt16 v = (*list)[i];
|
|
|
|
*dst++ = static_cast<UInt8>((v >> 8) & 0xff);
|
|
|
|
*dst++ = static_cast<UInt8>( v & 0xff);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 4: {
|
|
|
|
// 4 byte integers
|
|
|
|
const std::vector<UInt32>* list =
|
|
|
|
va_arg(args, const std::vector<UInt32>*);
|
2002-12-23 17:48:12 +03:00
|
|
|
const UInt32 n = list->size();
|
|
|
|
*dst++ = static_cast<UInt8>((n >> 24) & 0xff);
|
|
|
|
*dst++ = static_cast<UInt8>((n >> 16) & 0xff);
|
|
|
|
*dst++ = static_cast<UInt8>((n >> 8) & 0xff);
|
|
|
|
*dst++ = static_cast<UInt8>( n & 0xff);
|
|
|
|
for (UInt32 i = 0; i < n; ++i) {
|
2002-12-23 16:55:21 +03:00
|
|
|
const UInt32 v = (*list)[i];
|
|
|
|
*dst++ = static_cast<UInt8>((v >> 24) & 0xff);
|
|
|
|
*dst++ = static_cast<UInt8>((v >> 16) & 0xff);
|
|
|
|
*dst++ = static_cast<UInt8>((v >> 8) & 0xff);
|
|
|
|
*dst++ = static_cast<UInt8>( v & 0xff);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(0 && "invalid integer vector format length");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2002-04-29 18:40:01 +04:00
|
|
|
case 's': {
|
2001-11-25 21:32:41 +03:00
|
|
|
assert(len == 0);
|
|
|
|
const CString* src = va_arg(args, CString*);
|
|
|
|
const UInt32 len = (src != NULL) ? src->size() : 0;
|
|
|
|
*dst++ = static_cast<UInt8>((len >> 24) & 0xff);
|
|
|
|
*dst++ = static_cast<UInt8>((len >> 16) & 0xff);
|
|
|
|
*dst++ = static_cast<UInt8>((len >> 8) & 0xff);
|
|
|
|
*dst++ = static_cast<UInt8>( len & 0xff);
|
|
|
|
if (len != 0) {
|
|
|
|
memcpy(dst, src->data(), len);
|
|
|
|
dst += len;
|
|
|
|
}
|
|
|
|
break;
|
2002-04-29 18:40:01 +04:00
|
|
|
}
|
2001-11-25 21:32:41 +03:00
|
|
|
|
2002-04-29 18:40:01 +04:00
|
|
|
case 'S': {
|
2001-10-06 18:13:28 +04:00
|
|
|
assert(len == 0);
|
|
|
|
const UInt32 len = va_arg(args, UInt32);
|
|
|
|
const UInt8* src = va_arg(args, UInt8*);
|
|
|
|
*dst++ = static_cast<UInt8>((len >> 24) & 0xff);
|
|
|
|
*dst++ = static_cast<UInt8>((len >> 16) & 0xff);
|
|
|
|
*dst++ = static_cast<UInt8>((len >> 8) & 0xff);
|
|
|
|
*dst++ = static_cast<UInt8>( len & 0xff);
|
|
|
|
memcpy(dst, src, len);
|
|
|
|
dst += len;
|
|
|
|
break;
|
2002-04-29 18:40:01 +04:00
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2002-04-29 18:40:01 +04:00
|
|
|
case '%':
|
2001-10-06 18:13:28 +04:00
|
|
|
assert(len == 0);
|
|
|
|
*dst++ = '%';
|
|
|
|
break;
|
|
|
|
|
2002-04-29 18:40:01 +04:00
|
|
|
default:
|
2001-10-06 18:13:28 +04:00
|
|
|
assert(0 && "invalid format specifier");
|
|
|
|
}
|
|
|
|
|
|
|
|
// next format character
|
|
|
|
++fmt;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// copy regular character
|
|
|
|
*dst++ = *fmt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
UInt32
|
2002-06-17 17:31:21 +04:00
|
|
|
CProtocolUtil::eatLength(const char** pfmt)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
const char* fmt = *pfmt;
|
|
|
|
UInt32 n = 0;
|
|
|
|
for (;;) {
|
|
|
|
UInt32 d;
|
|
|
|
switch (*fmt) {
|
2002-04-29 18:40:01 +04:00
|
|
|
case '0': d = 0; break;
|
|
|
|
case '1': d = 1; break;
|
|
|
|
case '2': d = 2; break;
|
|
|
|
case '3': d = 3; break;
|
|
|
|
case '4': d = 4; break;
|
|
|
|
case '5': d = 5; break;
|
|
|
|
case '6': d = 6; break;
|
|
|
|
case '7': d = 7; break;
|
|
|
|
case '8': d = 8; break;
|
|
|
|
case '9': d = 9; break;
|
|
|
|
default: *pfmt = fmt; return n;
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
n = 10 * n + d;
|
|
|
|
++fmt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2004-02-02 00:09:22 +03:00
|
|
|
CProtocolUtil::read(IStream* stream, void* vbuffer, UInt32 count)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
assert(stream != NULL);
|
|
|
|
assert(vbuffer != NULL);
|
|
|
|
|
|
|
|
UInt8* buffer = reinterpret_cast<UInt8*>(vbuffer);
|
|
|
|
while (count > 0) {
|
|
|
|
// read more
|
2004-02-02 00:09:22 +03:00
|
|
|
UInt32 n = stream->read(buffer, count);
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
// bail if stream has hungup
|
|
|
|
if (n == 0) {
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG2 "unexpected disconnect in readf(), %d bytes left", count));
|
2001-10-06 18:13:28 +04:00
|
|
|
throw XIOEndOfStream();
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepare for next read
|
|
|
|
buffer += n;
|
|
|
|
count -= n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// XIOReadMismatch
|
|
|
|
//
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
CString
|
|
|
|
XIOReadMismatch::getWhat() const throw()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-07-25 21:52:40 +04:00
|
|
|
return format("XIOReadMismatch", "CProtocolUtil::readf() mismatch");
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|