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-11-16 07:22:12 +01:00
|
|
|
void ClumpletReader::usage_mistake(const char* what) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void ClumpletReader::invalid_structure() {
|
|
|
|
fatal_exception::raise("Invalid clumplet buffer structure");
|
|
|
|
}
|
|
|
|
|
|
|
|
UCHAR ClumpletReader::getBufferTag() {
|
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];
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
size_t co = getCurOffset();
|
|
|
|
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
|
|
|
|
UCHAR ClumpletReader::getClumpTag() {
|
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];
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ClumpletReader::getClumpLength() {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
SLONG ClumpletReader::getInt() {
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
SINT64 ClumpletReader::getBigInt() {
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
string& ClumpletReader::getString(string& str) {
|
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-11-24 19:26:24 +01:00
|
|
|
PathName& ClumpletReader::getPath(PathName& str)
|
|
|
|
{
|
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-11-24 19:26:24 +01:00
|
|
|
bool ClumpletReader::getBoolean()
|
|
|
|
{
|
|
|
|
const UCHAR* clumplet = getBuffer() + cur_offset;
|
|
|
|
size_t length = getClumpLength();
|
|
|
|
return length && clumplet[2];
|
|
|
|
}
|
2004-10-22 08:24:40 +02:00
|
|
|
|
|
|
|
}
|