2004-10-22 08:24:40 +02:00
|
|
|
/*
|
|
|
|
* PROGRAM: Client/Server Common Code
|
2004-12-10 23:54:16 +01:00
|
|
|
* MODULE: ClumpletWriter.cpp
|
2004-10-22 08:24:40 +02:00
|
|
|
* DESCRIPTION: Secure handling of clumplet buffers
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Initial
|
|
|
|
* Developer's Public License Version 1.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
|
|
|
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed AS IS,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing rights
|
|
|
|
* and limitations under the License.
|
|
|
|
*
|
|
|
|
* The Original Code was created by Nickolay Samofatov
|
|
|
|
* for the Firebird Open Source RDBMS project.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2004 Nickolay Samofatov <nickolay@broadviewsoftware.com>
|
|
|
|
* and all contributors signed below.
|
|
|
|
*
|
|
|
|
* All Rights Reserved.
|
|
|
|
* Contributor(s): ______________________________________.
|
|
|
|
*
|
|
|
|
*
|
2005-05-28 00:45:31 +02:00
|
|
|
|
2004-10-22 08:24:40 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "firebird.h"
|
|
|
|
|
|
|
|
#include "../common/classes/ClumpletWriter.h"
|
|
|
|
#include "fb_exception.h"
|
|
|
|
|
|
|
|
namespace Firebird {
|
|
|
|
|
2004-11-03 09:38:09 +01:00
|
|
|
ClumpletWriter::ClumpletWriter(bool isTagged, size_t limit, UCHAR tag) :
|
2004-12-09 20:19:47 +01:00
|
|
|
ClumpletReader(isTagged, NULL, 0), sizeLimit(limit), dynamic_buffer(getPool())
|
2004-10-22 08:24:40 +02:00
|
|
|
{
|
2004-11-03 09:38:09 +01:00
|
|
|
if (isTagged) {
|
2004-11-15 17:34:47 +01:00
|
|
|
dynamic_buffer.push(tag);
|
2004-11-03 09:38:09 +01:00
|
|
|
}
|
2004-10-22 08:24:40 +02:00
|
|
|
}
|
|
|
|
|
2004-12-09 20:19:47 +01:00
|
|
|
ClumpletWriter::ClumpletWriter(bool isTagged, size_t limit, const UCHAR* buffer, size_t buffLen, UCHAR tag) :
|
|
|
|
ClumpletReader(isTagged, NULL, 0), sizeLimit(limit), dynamic_buffer(getPool())
|
2004-10-22 08:24:40 +02:00
|
|
|
{
|
2004-12-13 16:49:28 +01:00
|
|
|
if (buffer && buffLen) {
|
2004-12-09 20:19:47 +01:00
|
|
|
dynamic_buffer.push(buffer, buffLen);
|
|
|
|
}
|
|
|
|
else if (isTagged) {
|
|
|
|
dynamic_buffer.push(tag);
|
|
|
|
}
|
2004-10-22 08:24:40 +02:00
|
|
|
}
|
|
|
|
|
2004-12-12 02:55:21 +01:00
|
|
|
void ClumpletWriter::reset(UCHAR tag)
|
|
|
|
{
|
2004-11-15 17:34:47 +01:00
|
|
|
dynamic_buffer.shrink(0);
|
|
|
|
|
2004-11-03 09:38:09 +01:00
|
|
|
if (!mIsTagged) {
|
|
|
|
usage_mistake("buffer is not tagged");
|
|
|
|
|
|
|
|
cur_offset = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-10-23 03:21:11 +02:00
|
|
|
cur_offset = 1;
|
2004-11-15 17:34:47 +01:00
|
|
|
dynamic_buffer.push(tag);
|
2004-10-23 03:21:11 +02:00
|
|
|
}
|
|
|
|
|
2004-12-12 02:55:21 +01:00
|
|
|
void ClumpletWriter::reset(const UCHAR* buffer, size_t buffLen)
|
|
|
|
{
|
2004-11-03 09:38:09 +01:00
|
|
|
cur_offset = mIsTagged ? 1 : 0;
|
2004-11-15 17:34:47 +01:00
|
|
|
dynamic_buffer.clear();
|
|
|
|
dynamic_buffer.push(buffer, buffLen);
|
2004-10-23 03:21:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-12 02:55:21 +01:00
|
|
|
void ClumpletWriter::size_overflow()
|
|
|
|
{
|
2004-10-22 08:24:40 +02:00
|
|
|
fatal_exception::raise("Clumplet buffer size limit reached");
|
|
|
|
}
|
|
|
|
|
2004-12-12 02:55:21 +01:00
|
|
|
void ClumpletWriter::insertInt(UCHAR tag, SLONG value)
|
|
|
|
{
|
2004-10-22 08:24:40 +02:00
|
|
|
#if defined(WORDS_BIGENDIAN)
|
2004-11-15 17:34:47 +01:00
|
|
|
UCHAR bytes[4];
|
2004-11-27 09:13:39 +01:00
|
|
|
const UCHAR* ptr = reinterpret_cast<UCHAR*>(&value);
|
2004-10-22 08:24:40 +02:00
|
|
|
bytes[0] = ptr[3];
|
|
|
|
bytes[1] = ptr[2];
|
|
|
|
bytes[2] = ptr[1];
|
|
|
|
bytes[3] = ptr[0];
|
2004-11-03 09:38:09 +01:00
|
|
|
insertBytesNoLengthCheck(tag, bytes, sizeof(bytes));
|
2004-10-22 08:24:40 +02:00
|
|
|
#else
|
2004-11-03 09:38:09 +01:00
|
|
|
insertBytesNoLengthCheck(tag, reinterpret_cast<UCHAR*>(&value), sizeof(value));
|
2004-10-22 08:24:40 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2004-12-12 02:55:21 +01:00
|
|
|
void ClumpletWriter::insertBigInt(UCHAR tag, SINT64 value)
|
|
|
|
{
|
2004-10-22 08:24:40 +02:00
|
|
|
#if defined(WORDS_BIGENDIAN)
|
2004-11-15 17:34:47 +01:00
|
|
|
UCHAR bytes[8];
|
2004-11-27 09:13:39 +01:00
|
|
|
const UCHAR* ptr = reinterpret_cast<UCHAR*>(&value);
|
2004-10-22 08:24:40 +02:00
|
|
|
bytes[0] = ptr[7];
|
|
|
|
bytes[1] = ptr[6];
|
|
|
|
bytes[2] = ptr[5];
|
|
|
|
bytes[3] = ptr[4];
|
|
|
|
bytes[4] = ptr[3];
|
|
|
|
bytes[5] = ptr[2];
|
|
|
|
bytes[6] = ptr[1];
|
|
|
|
bytes[7] = ptr[0];
|
2004-11-03 09:38:09 +01:00
|
|
|
insertBytesNoLengthCheck(tag, bytes, sizeof(bytes));
|
2004-10-22 08:24:40 +02:00
|
|
|
#else
|
2004-11-03 09:38:09 +01:00
|
|
|
insertBytesNoLengthCheck(tag, reinterpret_cast<UCHAR*>(&value), sizeof(value));
|
2004-10-22 08:24:40 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2004-12-12 02:55:21 +01:00
|
|
|
void ClumpletWriter::insertString(UCHAR tag, const string& str)
|
|
|
|
{
|
2004-11-03 09:38:09 +01:00
|
|
|
insertString(tag, str.c_str(), str.length());
|
|
|
|
}
|
|
|
|
|
2004-12-12 02:55:21 +01:00
|
|
|
void ClumpletWriter::insertPath(UCHAR tag, const PathName& str)
|
|
|
|
{
|
2004-11-15 17:34:47 +01:00
|
|
|
insertString(tag, str.c_str(), str.length());
|
|
|
|
}
|
|
|
|
|
2004-12-12 02:55:21 +01:00
|
|
|
void ClumpletWriter::insertString(UCHAR tag, const char* str, size_t length)
|
|
|
|
{
|
2004-11-03 09:38:09 +01:00
|
|
|
if (length > 255) {
|
|
|
|
string errStr(str, 100);
|
|
|
|
errStr.append("...");
|
|
|
|
|
|
|
|
string msgStr;
|
|
|
|
msgStr.printf("string \"%s\" is too long to be inserted into clumplet buffer",
|
|
|
|
errStr.c_str());
|
|
|
|
usage_mistake(msgStr.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
insertBytesNoLengthCheck(tag, reinterpret_cast<const UCHAR*>(str), length);
|
|
|
|
}
|
|
|
|
|
2004-12-12 02:55:21 +01:00
|
|
|
void ClumpletWriter::insertBytes(UCHAR tag, const UCHAR* bytes, size_t length)
|
|
|
|
{
|
2004-11-03 09:38:09 +01:00
|
|
|
if (length > 255) {
|
|
|
|
usage_mistake("byte sequence is too long to be inserted into clumplet buffer");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
insertBytesNoLengthCheck(tag, bytes, length);
|
2004-10-22 08:24:40 +02:00
|
|
|
}
|
|
|
|
|
2004-12-12 02:55:21 +01:00
|
|
|
void ClumpletWriter::insertBytesNoLengthCheck(UCHAR tag, const UCHAR* bytes, UCHAR length)
|
|
|
|
{
|
2004-10-22 08:24:40 +02:00
|
|
|
// Check that we're not beyond the end of buffer.
|
|
|
|
// We get there when we set end marker.
|
2004-11-15 17:34:47 +01:00
|
|
|
if (cur_offset > dynamic_buffer.getCount()) {
|
2004-11-03 09:38:09 +01:00
|
|
|
usage_mistake("write past EOF");
|
2004-10-22 08:24:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that resulting data doesn't overflow size limit
|
2004-11-15 17:34:47 +01:00
|
|
|
if (dynamic_buffer.getCount() + length + 2 > sizeLimit) {
|
2004-10-22 08:24:40 +02:00
|
|
|
size_overflow();
|
|
|
|
}
|
|
|
|
|
2004-12-09 20:19:47 +01:00
|
|
|
// Insert the data
|
2004-11-15 17:34:47 +01:00
|
|
|
dynamic_buffer.insert(cur_offset, tag);
|
|
|
|
dynamic_buffer.insert(cur_offset + 1, length);
|
|
|
|
dynamic_buffer.insert(cur_offset + 2, bytes, length);
|
2004-11-03 09:38:09 +01:00
|
|
|
cur_offset += length + 2;
|
2004-10-22 08:24:40 +02:00
|
|
|
}
|
|
|
|
|
2004-12-09 20:19:47 +01:00
|
|
|
void ClumpletWriter::insertTag(UCHAR tag)
|
|
|
|
{
|
|
|
|
// Check that we're not beyond the end of buffer.
|
|
|
|
// We get there when we set end marker.
|
|
|
|
if (cur_offset > dynamic_buffer.getCount()) {
|
|
|
|
usage_mistake("write past EOF");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that resulting data doesn't overflow size limit
|
|
|
|
if (dynamic_buffer.getCount() + 2 > sizeLimit) {
|
|
|
|
size_overflow();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert the data
|
|
|
|
dynamic_buffer.insert(cur_offset++, tag);
|
|
|
|
dynamic_buffer.insert(cur_offset++, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-12 02:55:21 +01:00
|
|
|
void ClumpletWriter::insertEndMarker(UCHAR tag)
|
|
|
|
{
|
2004-10-22 08:24:40 +02:00
|
|
|
// Check that we're not beyond the end of buffer.
|
|
|
|
// We get there when we set end marker.
|
2004-11-15 17:34:47 +01:00
|
|
|
if (cur_offset > dynamic_buffer.getCount()) {
|
2004-11-03 09:38:09 +01:00
|
|
|
usage_mistake("write past EOF");
|
2004-10-22 08:24:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that resulting data doesn't overflow size limit
|
2004-10-23 03:21:11 +02:00
|
|
|
if (cur_offset + 1 > sizeLimit) {
|
2004-10-22 08:24:40 +02:00
|
|
|
size_overflow();
|
|
|
|
}
|
|
|
|
|
2004-11-15 17:34:47 +01:00
|
|
|
dynamic_buffer.shrink(cur_offset);
|
|
|
|
dynamic_buffer.push(tag);
|
2004-10-22 08:24:40 +02:00
|
|
|
|
|
|
|
cur_offset += 2; // Go past EOF to indicate we set the marker
|
|
|
|
}
|
|
|
|
|
2004-12-12 02:55:21 +01:00
|
|
|
void ClumpletWriter::deleteClumplet()
|
|
|
|
{
|
2004-11-15 17:34:47 +01:00
|
|
|
const UCHAR* clumplet = getBuffer() + cur_offset;
|
|
|
|
const UCHAR* buffer_end = getBufferEnd();
|
2004-10-22 08:24:40 +02:00
|
|
|
|
|
|
|
// Check for EOF
|
|
|
|
if (clumplet >= buffer_end) {
|
2004-11-03 09:38:09 +01:00
|
|
|
usage_mistake("write past EOF");
|
2004-10-22 08:24:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer_end - clumplet < 2) {
|
|
|
|
// It appears we're erasing EOF marker
|
2004-11-15 17:34:47 +01:00
|
|
|
dynamic_buffer.shrink(cur_offset);
|
2004-10-22 08:24:40 +02:00
|
|
|
} else {
|
|
|
|
size_t length = clumplet[1];
|
2004-12-09 20:19:47 +01:00
|
|
|
dynamic_buffer.removeCount(cur_offset, length + 2);
|
2004-10-22 08:24:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-12 02:55:21 +01:00
|
|
|
} // namespace
|
|
|
|
|