1/*
2 * PROGRAM: Client/Server Common Code
3 * MODULE: ClumpletWriter.h
4 * DESCRIPTION: Secure handling of clumplet buffers
5 *
6 * The contents of this file are subject to the Initial
7 * Developer's Public License Version 1.0 (the "License");
8 * you may not use this file except in compliance with the
9 * License. You may obtain a copy of the License at
10 * http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
11 *
12 * Software distributed under the License is distributed AS IS,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied.
14 * See the License for the specific language governing rights
15 * and limitations under the License.
16 *
17 * The Original Code was created by Nickolay Samofatov
18 * for the Firebird Open Source RDBMS project.
19 *
20 * Copyright (c) 2004 Nickolay Samofatov <nickolay@broadviewsoftware.com>
21 * and all contributors signed below.
22 *
23 * All Rights Reserved.
24 * Contributor(s): ______________________________________.
25 *
26 *
27 */
28
29#ifndef CLUMPLETWRITER_H
30#define CLUMPLETWRITER_H
31
32#include "../common/classes/ClumpletReader.h"
33#include "../common/classes/array.h"
34
35// This setting of maximum dpb size doesn't mean, that we
36// can't process larger DBPs! This is just recommended limit
37// cause it's hard to imagine sensefull DPB of even this size.
38const size_t MAX_DPB_SIZE = 1024 * 1024;
39
40namespace Firebird {
41
42// At the moment you can only declare it on stack, permanent objects are not allowed
43class ClumpletWriter : public ClumpletReader
44{
45public:
46 // Create empty clumplet writer.
47 ClumpletWriter(Kind k, size_t limit, UCHAR tag = 0);
48 ClumpletWriter(MemoryPool& pool, Kind k, size_t limit, UCHAR tag = 0);
49
50 // Create writer from a given buffer
51 ClumpletWriter(Kind k, size_t limit, const UCHAR* buffer, size_t buffLen, UCHAR tag = 0);
52 ClumpletWriter(MemoryPool& pool, Kind k, size_t limit, const UCHAR* buffer, size_t buffLen, UCHAR tag = 0);
53
54 // Create writer from a given buffer with possibly different clumplet version
55 ClumpletWriter(const KindList* kl, size_t limit, const UCHAR* buffer, size_t buffLen);
56 ClumpletWriter(MemoryPool& pool, const KindList* kl, size_t limit,
57 const UCHAR* buffer, size_t buffLen);
58
59 // Create empty writer
60 ClumpletWriter(const KindList* kl, size_t limit);
61 ClumpletWriter(MemoryPool& pool, const KindList* kl, size_t limit);
62
63 // Create a copy of writer
64 ClumpletWriter(MemoryPool& pool, const ClumpletWriter& from);
65 ClumpletWriter(const ClumpletWriter& from);
66
67 void reset(UCHAR tag = 0);
68 void reset(const UCHAR* buffer, const size_t buffLen);
69 void clear();
70
71 // Methods to create new clumplet at current position
72 void insertInt(UCHAR tag, const SLONG value);
73 void insertBigInt(UCHAR tag, const SINT64 value);
74 void insertBytes(UCHAR tag, const void* bytes, size_t length);
75 void insertString(UCHAR tag, const string& str);
76 void insertPath(UCHAR tag, const PathName& str);
77 void insertString(UCHAR tag, const char* str, size_t length);
78 void insertByte(UCHAR tag, const UCHAR byte);
79 void insertTag(UCHAR tag);
80 void insertDouble(UCHAR tag, const double value);
81 void insertTimeStamp(UCHAR tag, const ISC_TIMESTAMP value);
82 void insertTime(UCHAR tag, ISC_TIME value) { insertInt(tag, value); }
83 void insertDate(UCHAR tag, ISC_DATE value) { insertInt(tag, value); }
84 void insertEndMarker(UCHAR tag);
85 void insertClumplet(const SingleClumplet& clumplet);
86
87 // Delete currently selected clumplet from buffer
88 void deleteClumplet();
89
90 // Delete all clumplets with given tag
91 // Returns true if any found
92 bool deleteWithTag(UCHAR tag);
93
94 virtual const UCHAR* getBuffer() const;
95
96protected:
97 virtual const UCHAR* getBufferEnd() const;
98 virtual void size_overflow();
99 void insertBytesLengthCheck(UCHAR tag, const void* bytes, const size_t length);
100 bool upgradeVersion(); // upgrade clumplet version - obtain newest from kindList
101
102private:
103 // Assignment not implemented.
104 ClumpletWriter& operator=(const ClumpletWriter& from);
105
106 size_t sizeLimit;
107 const KindList* kindList;
108 HalfStaticArray<UCHAR, 128> dynamic_buffer;
109
110 void initNewBuffer(UCHAR tag);
111 void create(const UCHAR* buffer, size_t buffLen, UCHAR tag);
112 static void toVaxInteger(UCHAR* ptr, size_t length, const SINT64 value);
113};
114
115} // namespace Firebird
116
117#endif // CLUMPLETWRITER_H
118