2004-12-10 23:54:16 +01:00
|
|
|
/*
|
|
|
|
* PROGRAM: Client/Server Common Code
|
|
|
|
* MODULE: ClumpletReader.cpp
|
|
|
|
* 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): ______________________________________.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2004-10-22 08:24:40 +02:00
|
|
|
#include "firebird.h"
|
|
|
|
|
|
|
|
#include "../common/classes/ClumpletReader.h"
|
|
|
|
#include "fb_exception.h"
|
|
|
|
|
|
|
|
namespace Firebird {
|
|
|
|
|
2004-11-03 09:38:09 +01:00
|
|
|
ClumpletReader::ClumpletReader(bool isTagged, const UCHAR* buffer, size_t buffLen) :
|
|
|
|
cur_offset(isTagged ? 1 : 0), mIsTagged(isTagged), static_buffer(buffer),
|
|
|
|
static_buffer_end(buffer + buffLen)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2004-12-12 07:50:10 +01:00
|
|
|
void ClumpletReader::usage_mistake(const char* what) const {
|
2004-11-03 09:38:09 +01:00
|
|
|
fatal_exception::raiseFmt(
|
2004-11-16 07:22:12 +01:00
|
|
|
"Internal error when using clumplet API: %s", what);
|
2004-10-22 08:24:40 +02:00
|
|
|
}
|
|
|
|
|
2004-12-12 07:50:10 +01:00
|
|
|
void ClumpletReader::invalid_structure() const {
|
2004-10-22 08:24:40 +02:00
|
|
|
fatal_exception::raise("Invalid clumplet buffer structure");
|
|
|
|
}
|
|
|
|
|
2004-12-12 07:50:10 +01:00
|
|
|
UCHAR ClumpletReader::getBufferTag() const
|
|
|
|
{
|
2004-11-03 09:38:09 +01:00
|
|
|
if (!mIsTagged) {
|
|
|
|
usage_mistake("buffer is not tagged");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-11-15 17:34:47 +01:00
|
|
|
const UCHAR* buffer_end = getBufferEnd();
|
|
|
|
const UCHAR* buffer_start = getBuffer();
|
2004-10-22 08:24:40 +02:00
|
|
|
if (buffer_end - buffer_start == 0) {
|
|
|
|
invalid_structure();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return buffer_start[0];
|
|
|
|
}
|
|
|
|
|
2004-12-12 07:50:10 +01:00
|
|
|
void ClumpletReader::moveNext()
|
|
|
|
{
|
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("read past EOF");
|
2004-10-22 08:24:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// It appears we didn't receive length component for clumplet
|
|
|
|
if (buffer_end - clumplet < 2) {
|
|
|
|
invalid_structure();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t length = clumplet[1];
|
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 ClumpletReader::rewind()
|
|
|
|
{
|
|
|
|
cur_offset = mIsTagged ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ClumpletReader::find(UCHAR tag)
|
|
|
|
{
|
2004-12-10 23:54:16 +01:00
|
|
|
const size_t co = getCurOffset();
|
2004-12-09 20:19:47 +01:00
|
|
|
for (rewind(); !isEof(); moveNext())
|
|
|
|
{
|
|
|
|
if (tag == getClumpTag())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setCurOffset(co);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-10-22 08:24:40 +02:00
|
|
|
// Methods which work with currently selected clumplet
|
2004-12-12 07:50:10 +01:00
|
|
|
UCHAR ClumpletReader::getClumpTag() const
|
|
|
|
{
|
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("read past EOF");
|
2004-10-23 03:21:11 +02:00
|
|
|
return 0;
|
2004-10-22 08:24:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return clumplet[0];
|
|
|
|
}
|
|
|
|
|
2004-12-12 07:50:10 +01:00
|
|
|
size_t ClumpletReader::getClumpLength() const
|
|
|
|
{
|
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("read past EOF");
|
2004-10-23 03:21:11 +02:00
|
|
|
return 0;
|
2004-10-22 08:24:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// See if we have length byte available
|
|
|
|
if (buffer_end - clumplet < 2) {
|
|
|
|
invalid_structure();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if length seems kosher
|
|
|
|
size_t length = clumplet[1];
|
|
|
|
if (buffer_end - clumplet < length + 1) {
|
|
|
|
invalid_structure();
|
|
|
|
return buffer_end - clumplet - 1;
|
|
|
|
}
|
2004-10-23 03:21:11 +02:00
|
|
|
|
|
|
|
return length;
|
2004-10-22 08:24:40 +02:00
|
|
|
}
|
|
|
|
|
2004-12-12 07:50:10 +01:00
|
|
|
SLONG ClumpletReader::getInt() const
|
|
|
|
{
|
2004-11-15 17:34:47 +01:00
|
|
|
const UCHAR* clumplet = getBuffer() + cur_offset;
|
2004-10-22 08:24:40 +02:00
|
|
|
size_t length = getClumpLength();
|
|
|
|
|
|
|
|
if (length > 4)
|
|
|
|
invalid_structure();
|
|
|
|
|
2004-11-15 17:34:47 +01:00
|
|
|
const UCHAR* ptr = clumplet + 2;
|
2004-10-22 08:24:40 +02:00
|
|
|
|
|
|
|
// This code is taken from gds__vax_integer
|
|
|
|
SLONG value = 0;
|
|
|
|
int shift = 0;
|
2004-11-24 19:26:24 +01:00
|
|
|
while (length > 0) {
|
|
|
|
--length;
|
2004-10-22 08:24:40 +02:00
|
|
|
value += ((SLONG) *ptr++) << shift;
|
|
|
|
shift += 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2004-12-12 07:50:10 +01:00
|
|
|
SINT64 ClumpletReader::getBigInt() const
|
|
|
|
{
|
2004-11-15 17:34:47 +01:00
|
|
|
const UCHAR* clumplet = getBuffer() + cur_offset;
|
2004-10-22 08:24:40 +02:00
|
|
|
size_t length = getClumpLength();
|
|
|
|
|
|
|
|
if (length > 8)
|
|
|
|
invalid_structure();
|
|
|
|
|
2004-11-15 17:34:47 +01:00
|
|
|
const UCHAR* ptr = clumplet + 2;
|
2004-10-22 08:24:40 +02:00
|
|
|
|
|
|
|
// This code is taken from isc_portable_integer
|
|
|
|
SINT64 value = 0;
|
|
|
|
int shift = 0;
|
2004-11-24 19:26:24 +01:00
|
|
|
while (length > 0) {
|
|
|
|
--length;
|
2004-10-22 08:24:40 +02:00
|
|
|
value += ((SINT64) *ptr++) << shift;
|
|
|
|
shift += 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2004-12-12 07:50:10 +01:00
|
|
|
string& ClumpletReader::getString(string& str) const
|
|
|
|
{
|
2004-11-15 17:34:47 +01:00
|
|
|
const UCHAR* clumplet = getBuffer() + cur_offset;
|
2004-10-22 08:24:40 +02:00
|
|
|
size_t length = getClumpLength();
|
2004-11-15 17:34:47 +01:00
|
|
|
str.assign(reinterpret_cast<const char*>(clumplet + 2), length);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2004-12-12 07:50:10 +01:00
|
|
|
PathName& ClumpletReader::getPath(PathName& str) const
|
2004-11-24 19:26:24 +01:00
|
|
|
{
|
2004-11-15 17:34:47 +01:00
|
|
|
const UCHAR* clumplet = getBuffer() + cur_offset;
|
|
|
|
size_t length = getClumpLength();
|
|
|
|
str.assign(reinterpret_cast<const char*>(clumplet + 2), length);
|
2004-10-22 08:24:40 +02:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2004-12-12 07:50:10 +01:00
|
|
|
bool ClumpletReader::getBoolean() const
|
2004-11-24 19:26:24 +01:00
|
|
|
{
|
|
|
|
const UCHAR* clumplet = getBuffer() + cur_offset;
|
2004-12-12 07:50:10 +01:00
|
|
|
// CVC: Maybe we should assert here that length == 1?
|
2004-11-24 19:26:24 +01:00
|
|
|
size_t length = getClumpLength();
|
|
|
|
return length && clumplet[2];
|
|
|
|
}
|
2004-10-22 08:24:40 +02:00
|
|
|
|
2004-12-12 07:50:10 +01:00
|
|
|
} // namespace
|
|
|
|
|