- Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathmerge.cpp
225 lines (196 loc) · 5.19 KB
/
merge.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* PROGRAM: JRD Remote Interface/Server
* MODULE: merge.cpp
* DESCRIPTION: Merge database/server information
*
* The contents of this file are subject to the Interbase 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.Inprise.com/IPL.html
*
* Software distributed under the License is distributed on an
* "AS IS" basis, 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 Inprise Corporation
* and its predecessors. Portions created by Inprise Corporation are
* Copyright (C) Inprise Corporation.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*/
#include"firebird.h"
#include<string.h>
#include"ibase.h"
#include"../remote/remote.h"
#include"../remote/merge_proto.h"
#include"../yvalve/gds_proto.h"
#include"../common/classes/DbImplementation.h"
usingnamespaceFirebird;
inlinevoidPUT_WORD(UCHAR*& ptr, USHORT value)
{
*ptr++ = static_cast<UCHAR>(value);
*ptr++ = static_cast<UCHAR>(value >> 8);
}
#definePUT(ptr, value) *(ptr)++ = value;
static ISC_STATUS merge_setup(const ClumpletReader&, UCHAR**, const UCHAR* const, FB_SIZE_T);
USHORT MERGE_database_info(const UCHAR* const in,
UCHAR* out,
USHORT buf_length,
USHORT impl,
USHORT class_,
USHORT base_level,
const UCHAR* version,
const UCHAR* id,
USHORT protocol)
{
/**************************************
*
* M E R G E _ d a t a b a s e _ i n f o
*
**************************************
*
* Functional description
* Merge server / remote interface / Y-valve information into
* database block. Return the actual length of the packet.
* See also jrd/utl.cpp for decoding of this block.
*
**************************************/
SSHORT l;
const UCHAR* p;
UCHAR* start = out;
const UCHAR* const end = out + buf_length;
UCHAR mergeLevel = 0;
ClumpletReader input(ClumpletReader::InfoResponse, in, buf_length);
while (!input.isEof())
{
bool flStop = true;
switch(input.getClumpTag())
{
case isc_info_implementation:
mergeLevel = input.getBytes()[0];
break;
case isc_info_end:
case isc_info_truncated:
break;
default:
flStop = false;
break;
}
if (flStop)
break;
input.moveNext();
}
for (input.rewind(); !input.isEof(); input.moveNext())
{
*out++ = input.getClumpTag();
switch (input.getClumpTag())
{
case isc_info_end:
if (protocol)
{
--out;
if (out + (1 + 2 + 2 + 1) <= end)
{
PUT(out, (UCHAR)fb_info_protocol_version);
PUT_WORD(out, 2u);
PUT_WORD(out, protocol);
protocol = 0;
PUT(out, (UCHAR)isc_info_end);
}
else
PUT(out, (UCHAR)isc_info_truncated);
}
// fall down...
case isc_info_truncated:
return out - start;
case isc_info_firebird_version:
l = static_cast<SSHORT>(strlen((char *) (p = version)));
if (l > MAX_UCHAR)
l = MAX_UCHAR;
if (merge_setup(input, &out, end, l + 1))
return0;
for (*out++ = (UCHAR) l; l; --l)
*out++ = *p++;
break;
case isc_info_db_id:
l = static_cast<SSHORT>(strlen((SCHAR *) (p = id)));
if (l > MAX_UCHAR)
l = MAX_UCHAR;
if (merge_setup(input, &out, end, l + 1))
return0;
for (*out++ = (UCHAR) l; l; --l)
*out++ = *p++;
break;
case isc_info_implementation:
if (merge_setup(input, &out, end, 2))
return0;
PUT(out, (UCHAR) impl);
PUT(out, (UCHAR) class_);
break;
case fb_info_implementation:
if (merge_setup(input, &out, end, 6))
return0;
DbImplementation::current.stuff(&out);
PUT(out, (UCHAR) class_);
PUT(out, mergeLevel);
break;
case isc_info_base_level:
if (merge_setup(input, &out, end, 1))
return0;
PUT(out, (UCHAR) base_level);
break;
case fb_info_protocol_version:
--out;
break;
default:
{
USHORT length = input.getClumpLength();
if (out + length + 2 >= end)
{
out[-1] = isc_info_truncated;
return0;
}
PUT_WORD(out, length);
memcpy(out, input.getBytes(), length);
out += length;
}
break;
}
}
return0; // error - missing isc_info_end item
}
static ISC_STATUS merge_setup(const ClumpletReader& input, UCHAR** out, const UCHAR* const end,
FB_SIZE_T delta_length)
{
/**************************************
*
* m e r g e _ s e t u p
*
**************************************
*
* Functional description
* Get ready to toss new stuff onto an info packet. This involves
* picking up and bumping the "count" field and copying what is
* already there.
*
**************************************/
FB_SIZE_T length = input.getClumpLength();
const FB_SIZE_T new_length = length + delta_length;
if (new_length > MAX_USHORT || *out + new_length + 2 >= end)
{
(*out)[-1] = isc_info_truncated;
return FB_FAILURE;
}
const USHORT count = 1 + *(input.getBytes());
PUT_WORD(*out, new_length);
PUT(*out, (UCHAR) count);
// Copy data portion of information sans original count
if (--length)
{
memcpy(*out, input.getBytes() + 1, length);
*out += length;
}
return FB_SUCCESS;
}