- Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathblob.cpp
314 lines (268 loc) · 9.48 KB
/
blob.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
* PROGRAM: InterBase layered support library
* MODULE: blob.cpp
* DESCRIPTION: Dynamic blob support
*
* 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): ______________________________________.
*
* 2001.09.10 Claudio Valderrama: get_name() was preventing the API calls
* isc_blob_default_desc, isc_blob_lookup_desc & isc_blob_set_desc
* from working properly with dialect 3 names. Therefore, incorrect names
* could be returned or a lookup for a blob field could fail. In addition,
* a possible buffer overrun due to unchecked bounds was closed. The fc
* get_name() as been renamed copy_exact_name().
*
*/
#include"firebird.h"
#include"firebird/Message.h"
#include"ibase.h"
#include"../jrd/intl.h"
#include"../yvalve/blob_proto.h"
#include"../yvalve/YObjects.h"
#include"../common/StatusArg.h"
#include"../common/utils_proto.h"
#include"../jrd/constants.h"
usingnamespaceFirebird;
staticvoidcopy_exact_name (const UCHAR*, UCHAR*, SSHORT);
static ISC_STATUS error(ISC_STATUS* status, const Arg::StatusVector& v);
void API_ROUTINE isc_blob_default_desc(ISC_BLOB_DESC* desc,
const UCHAR* relation_name,
const UCHAR* field_name)
{
/**************************************
*
* i s c _ b l o b _ d e f a u l t _ d e s c
*
**************************************
*
* Functional description
*
* This function will set the default
* values in the blob_descriptor.
*
**************************************/
desc->blob_desc_subtype = isc_blob_text;
desc->blob_desc_charset = CS_dynamic;
desc->blob_desc_segment_size = 80;
copy_exact_name(field_name, desc->blob_desc_field_name, sizeof(desc->blob_desc_field_name));
copy_exact_name(relation_name, desc->blob_desc_relation_name, sizeof(desc->blob_desc_relation_name));
}
ISC_STATUS API_ROUTINE isc_blob_gen_bpb(ISC_STATUS* status,
const ISC_BLOB_DESC* to_desc,
const ISC_BLOB_DESC* from_desc,
USHORT bpb_buffer_length,
UCHAR* bpb_buffer,
USHORT* bpb_length)
{
/**************************************
*
* i s c _ b l o b _ g e n _ b p b
*
**************************************
*
* Functional description
*
* This function will generate a bpb
* given a to_desc and a from_desc
* which contain the subtype and
* character set information.
*
**************************************/
if (bpb_buffer_length < 17)
returnerror(status, Arg::Gds(isc_random) << Arg::Str("BPB buffer too small"));
UCHAR* p = bpb_buffer;
*p++ = isc_bpb_version1;
*p++ = isc_bpb_target_type;
*p++ = 2;
*p++ = (UCHAR)to_desc->blob_desc_subtype;
*p++ = (UCHAR)(to_desc->blob_desc_subtype >> 8);
*p++ = isc_bpb_source_type;
*p++ = 2;
*p++ = (UCHAR)from_desc->blob_desc_subtype;
*p++ = (UCHAR)(from_desc->blob_desc_subtype >> 8);
*p++ = isc_bpb_target_interp;
*p++ = 2;
*p++ = (UCHAR)to_desc->blob_desc_charset;
*p++ = (UCHAR)(to_desc->blob_desc_charset >> 8);
*p++ = isc_bpb_source_interp;
*p++ = 2;
*p++ = (UCHAR)from_desc->blob_desc_charset;
*p++ = (UCHAR)(from_desc->blob_desc_charset >> 8);
*bpb_length = p - bpb_buffer;
returnerror(status, Arg::Gds(FB_SUCCESS));
}
// Lookup the blob subtype, character set and segment size information from the metadata,
// given a relation/procedure name and column/parameter name.
// It will fill in the information in the BLOB_DESC.
voidiscBlobLookupDescImpl(Why::YAttachment* attachment, Why::YTransaction* transaction,
const UCHAR* relationName, const UCHAR* fieldName, ISC_BLOB_DESC* desc, UCHAR* global)
{
LocalStatus status;
CheckStatusWrapper statusWrapper(&status);
copy_exact_name(fieldName, desc->blob_desc_field_name, sizeof(desc->blob_desc_field_name));
copy_exact_name(relationName, desc->blob_desc_relation_name, sizeof(desc->blob_desc_relation_name));
bool flag = false;
// Shared by both queries.
FB_MESSAGE(OutputMessage, CheckStatusWrapper,
(FB_INTEGER, fieldSubType)
(FB_INTEGER, segmentLength)
(FB_INTEGER, characterSetId)
) outputMessage(&statusWrapper, MasterInterfacePtr());
{ // scope
constexprauto sql = R"""(
select f.rdb$field_sub_type,
f.rdb$segment_length,
f.rdb$character_set_id
from rdb$relation_fields rf
join rdb$fields f
on f.rdb$field_name = rf.rdb$field_source
where rf.rdb$relation_name = ? and
rf.rdb$field_name = ?
)""";
FB_MESSAGE(InputMessage, CheckStatusWrapper,
(FB_VARCHAR(MAX_SQL_IDENTIFIER_LEN), relationName)
(FB_VARCHAR(MAX_SQL_IDENTIFIER_LEN), fieldName)
) inputMessage(&statusWrapper, MasterInterfacePtr());
inputMessage.clear();
inputMessage->relationNameNull = FB_FALSE;
inputMessage->relationName.set((constchar*) relationName);
inputMessage->fieldNameNull = FB_FALSE;
inputMessage->fieldName.set((constchar*) fieldName);
auto resultSet = makeNoIncRef(attachment->openCursor(&statusWrapper, transaction, 0, sql,
SQL_DIALECT_CURRENT, inputMessage.getMetadata(), inputMessage.getData(),
outputMessage.getMetadata(), nullptr, 0));
status.check();
if (resultSet->fetchNext(&statusWrapper, outputMessage.getData()) == IStatus::RESULT_OK)
{
flag = true;
desc->blob_desc_subtype = outputMessage->fieldSubTypeNull ? 0 : outputMessage->fieldSubType;
desc->blob_desc_charset = outputMessage->characterSetIdNull ? 0 : outputMessage->characterSetId;
desc->blob_desc_segment_size = outputMessage->segmentLengthNull ? 0 : outputMessage->segmentLength;
}
status.check();
}
if (!flag)
{
constexprauto sql = R"""(
select f.rdb$field_sub_type,
f.rdb$segment_length,
f.rdb$character_set_id
from rdb$procedure_parameters pp
join rdb$fields f
on f.rdb$field_name = pp.rdb$field_source
where pp.rdb$procedure_name = ? and
pp.rdb$parameter_name = ? and
pp.rdb$package_name is null
)""";
FB_MESSAGE(InputMessage, CheckStatusWrapper,
(FB_VARCHAR(MAX_SQL_IDENTIFIER_LEN), procedureName)
(FB_VARCHAR(MAX_SQL_IDENTIFIER_LEN), fieldName)
) inputMessage(&statusWrapper, MasterInterfacePtr());
inputMessage.clear();
inputMessage->procedureNameNull = FB_FALSE;
inputMessage->procedureName.set((constchar*) relationName);
inputMessage->fieldNameNull = FB_FALSE;
inputMessage->fieldName.set((constchar*) fieldName);
auto resultSet = makeNoIncRef(attachment->openCursor(&statusWrapper, transaction, 0, sql,
SQL_DIALECT_CURRENT, inputMessage.getMetadata(), inputMessage.getData(),
outputMessage.getMetadata(), nullptr, 0));
status.check();
if (resultSet->fetchNext(&statusWrapper, outputMessage.getData()) == IStatus::RESULT_OK)
{
flag = true;
desc->blob_desc_subtype = outputMessage->fieldSubTypeNull ? 0 : outputMessage->fieldSubType;
desc->blob_desc_charset = outputMessage->characterSetIdNull ? 0 : outputMessage->characterSetId;
desc->blob_desc_segment_size = outputMessage->segmentLengthNull ? 0 : outputMessage->segmentLength;
}
status.check();
}
if (!flag)
{
(Arg::Gds(isc_fldnotdef) <<
Arg::Str((constchar*)(desc->blob_desc_field_name)) <<
Arg::Str((constchar*)(desc->blob_desc_relation_name))).raise();
}
if (global)
copy_exact_name(fieldName, global, sizeof(desc->blob_desc_field_name));
}
ISC_STATUS API_ROUTINE isc_blob_set_desc(ISC_STATUS* status,
const UCHAR* relation_name,
const UCHAR* field_name,
SSHORT subtype,
SSHORT charset,
SSHORT segment_size,
ISC_BLOB_DESC* desc)
{
/**************************************
*
* i s c _ b l o b _ s e t _ d e s c
*
**************************************
*
* Functional description
*
* This routine will set the subtype
* and character set information in the
* BLOB_DESC based on the information
* specifically passed in by the user.
*
**************************************/
copy_exact_name(field_name, desc->blob_desc_field_name, sizeof(desc->blob_desc_field_name));
copy_exact_name(relation_name, desc->blob_desc_relation_name, sizeof(desc->blob_desc_relation_name));
desc->blob_desc_subtype = subtype;
desc->blob_desc_charset = charset;
desc->blob_desc_segment_size = segment_size;
returnerror(status, Arg::Gds(FB_SUCCESS));
}
staticvoidcopy_exact_name (const UCHAR* from, UCHAR* to, SSHORT bsize)
{
/**************************************
*
* c o p y _ e x a c t _ n a m e
*
**************************************
*
* Functional description
* Copy null terminated name ot stops at bsize - 1.
*
**************************************/
const UCHAR* const from_end = from + bsize - 1;
UCHAR* to2 = to - 1;
while (*from && from < from_end)
{
if (*from != '') {
to2 = to;
}
*to++ = *from++;
}
*++to2 = 0;
}
static ISC_STATUS error(ISC_STATUS* status, const Arg::StatusVector& v)
{
/**************************************
*
* e r r o r
*
**************************************
*
* Functional description
* Stuff a status vector.
*
**************************************/
return v.copyTo(status);
}