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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#![allow(non_upper_case_globals)]
use crate::util::*;
use bytes::{BufMut, Bytes, BytesMut};
use rsfbclient_core::{ibase, FbError, StmtType};
use std::{convert::TryFrom, mem};
use crate::consts;
pub const XSQLDA_DESCRIBE_VARS: [u8; 17] = [
ibase::isc_info_sql_stmt_type as u8, ibase::isc_info_sql_bind as u8, ibase::isc_info_sql_describe_vars as u8, ibase::isc_info_sql_describe_end as u8, ibase::isc_info_sql_select as u8, ibase::isc_info_sql_describe_vars as u8, ibase::isc_info_sql_sqlda_seq as u8, ibase::isc_info_sql_type as u8, ibase::isc_info_sql_sub_type as u8, ibase::isc_info_sql_scale as u8, ibase::isc_info_sql_length as u8, ibase::isc_info_sql_null_ind as u8, ibase::isc_info_sql_field as u8, ibase::isc_info_sql_relation as u8, ibase::isc_info_sql_owner as u8, ibase::isc_info_sql_alias as u8, ibase::isc_info_sql_describe_end as u8, ];
#[derive(Debug, Default)]
pub struct XSqlVar {
pub sqltype: i16,
pub scale: i16,
pub sqlsubtype: i16,
pub data_length: i16,
pub null_ind: bool,
pub field_name: String,
pub relation_name: String,
pub owner_name: String,
pub alias_name: String,
}
impl XSqlVar {
pub fn coerce(&mut self) -> Result<(), FbError> {
let sqltype = self.sqltype & (!1);
let sqlsubtype = self.sqlsubtype;
match sqltype as u32 {
ibase::SQL_TEXT | ibase::SQL_VARYING => {
self.sqltype = ibase::SQL_VARYING as i16 + 1;
}
ibase::SQL_SHORT | ibase::SQL_LONG | ibase::SQL_INT64 => {
self.data_length = mem::size_of::<i64>() as i16;
if self.scale == 0 {
self.sqltype = ibase::SQL_INT64 as i16 + 1;
} else {
self.scale = 0;
self.sqltype = ibase::SQL_DOUBLE as i16 + 1;
}
}
ibase::SQL_FLOAT | ibase::SQL_DOUBLE => {
self.data_length = mem::size_of::<i64>() as i16;
self.sqltype = ibase::SQL_DOUBLE as i16 + 1;
}
ibase::SQL_TIMESTAMP | ibase::SQL_TYPE_DATE | ibase::SQL_TYPE_TIME => {
self.data_length = mem::size_of::<ibase::ISC_TIMESTAMP>() as i16;
self.sqltype = ibase::SQL_TIMESTAMP as i16 + 1;
}
ibase::SQL_BLOB if (sqlsubtype == 0 || sqlsubtype == 1) => {
self.sqltype = ibase::SQL_BLOB as i16 + 1;
}
ibase::SQL_BOOLEAN => {
self.sqltype = ibase::SQL_BOOLEAN as i16 + 1;
}
sqltype => {
return Err(format!("Unsupported column type ({})", sqltype).into());
}
}
Ok(())
}
}
pub fn xsqlda_to_blr(xsqlda: &[XSqlVar]) -> Result<Bytes, FbError> {
let mut blr = BytesMut::with_capacity(256);
blr.put_slice(&[
consts::blr::VERSION5,
consts::blr::BEGIN,
consts::blr::MESSAGE,
0, ]);
blr.put_u16_le(xsqlda.len() as u16 * 2);
for var in xsqlda {
let sqltype = var.sqltype as u32 & (!1);
match sqltype as u32 {
ibase::SQL_VARYING => {
blr.put_u8(consts::blr::VARYING);
blr.put_i16_le(var.data_length);
}
ibase::SQL_INT64 => blr.put_slice(&[
consts::blr::INT64,
0, ]),
ibase::SQL_DOUBLE => blr.put_u8(consts::blr::DOUBLE),
ibase::SQL_TIMESTAMP => blr.put_u8(consts::blr::TIMESTAMP),
ibase::SQL_BLOB => blr.put_slice(&[consts::blr::QUAD, var.sqlsubtype as u8]),
ibase::SQL_BOOLEAN => blr.put_u8(consts::blr::BOOL),
sqltype => {
return Err(format!("Conversion from sql type {} not implemented", sqltype).into());
}
}
blr.put_slice(&[consts::blr::SHORT, 0]);
}
blr.put_slice(&[consts::blr::END, consts::blr::EOC]);
Ok(blr.freeze())
}
pub struct PrepareInfo {
pub stmt_type: StmtType,
pub param_count: usize,
pub truncated: bool,
}
pub fn parse_xsqlda(resp: &mut Bytes, xsqlda: &mut Vec<XSqlVar>) -> Result<PrepareInfo, FbError> {
if resp.remaining() < 7 || resp[..3] != [ibase::isc_info_sql_stmt_type as u8, 0x04, 0x00] {
return err_invalid_xsqlda();
}
resp.advance(3)?;
let stmt_type =
StmtType::try_from(resp.get_u32_le()? as u8).map_err(|e| FbError::Other(e.to_string()))?;
if resp.remaining() < 8
|| resp[..2]
!= [
ibase::isc_info_sql_bind as u8, ibase::isc_info_sql_describe_vars as u8, ]
{
return err_invalid_xsqlda();
}
resp.advance(2)?;
resp.advance(2)?;
let param_count = resp.get_u32_le()? as usize;
while resp.remaining() > 0 && resp[0] == ibase::isc_info_sql_describe_end as u8 {
resp.advance(1)?;
}
if resp.remaining() < 8
|| resp[..2]
!= [
ibase::isc_info_sql_select as u8, ibase::isc_info_sql_describe_vars as u8, ]
{
return err_invalid_xsqlda();
}
resp.advance(2)?;
resp.advance(2)?;
let col_len = resp.get_u32_le()? as usize;
if col_len > 1024 {
return err_invalid_xsqlda();
}
if xsqlda.is_empty() {
xsqlda.reserve(col_len);
}
let truncated = parse_select_items(resp, xsqlda)?;
Ok(PrepareInfo {
stmt_type,
param_count,
truncated,
})
}
pub fn parse_select_items(resp: &mut Bytes, xsqlda: &mut Vec<XSqlVar>) -> Result<bool, FbError> {
if resp.remaining() == 0 {
return Ok(false);
}
let mut col_index = 0;
let truncated = loop {
match resp.get_u8()? as u32 {
ibase::isc_info_sql_sqlda_seq => {
resp.advance(2)?;
let col = resp.get_u32_le()?;
if col == 0 {
return err_invalid_xsqlda();
}
col_index = col as usize - 1;
if col_index >= xsqlda.len() {
xsqlda.push(Default::default());
#[cfg(not(feature = "fuzz_testing"))]
debug_assert_eq!(xsqlda.len() - 1, col_index);
}
}
ibase::isc_info_sql_type => {
resp.advance(2)?;
if let Some(var) = xsqlda.get_mut(col_index) {
var.sqltype = resp.get_i32_le()? as i16;
} else {
return err_invalid_xsqlda();
}
}
ibase::isc_info_sql_sub_type => {
resp.advance(2)?;
if let Some(var) = xsqlda.get_mut(col_index) {
var.sqlsubtype = resp.get_i32_le()? as i16;
} else {
return err_invalid_xsqlda();
}
}
ibase::isc_info_sql_scale => {
resp.advance(2)?;
if let Some(var) = xsqlda.get_mut(col_index) {
var.scale = resp.get_i32_le()? as i16;
} else {
return err_invalid_xsqlda();
}
}
ibase::isc_info_sql_length => {
resp.advance(2)?;
if let Some(var) = xsqlda.get_mut(col_index) {
var.data_length = resp.get_i32_le()? as i16;
} else {
return err_invalid_xsqlda();
}
}
ibase::isc_info_sql_null_ind => {
resp.advance(2)?;
if let Some(var) = xsqlda.get_mut(col_index) {
var.null_ind = resp.get_i32_le()? != 0;
} else {
return err_invalid_xsqlda();
}
}
ibase::isc_info_sql_field => {
let len = resp.get_u16_le()? as usize;
let mut buff = vec![0; len];
resp.copy_to_slice(&mut buff)?;
if let Some(var) = xsqlda.get_mut(col_index) {
var.field_name = String::from_utf8(buff).unwrap_or_default();
} else {
return err_invalid_xsqlda();
}
}
ibase::isc_info_sql_relation => {
let len = resp.get_u16_le()? as usize;
let mut buff = vec![0; len];
resp.copy_to_slice(&mut buff)?;
if let Some(var) = xsqlda.get_mut(col_index) {
var.relation_name = String::from_utf8(buff).unwrap_or_default();
} else {
return err_invalid_xsqlda();
}
}
ibase::isc_info_sql_owner => {
let len = resp.get_u16_le()? as usize;
let mut buff = vec![0; len];
resp.copy_to_slice(&mut buff)?;
if let Some(var) = xsqlda.get_mut(col_index) {
var.owner_name = String::from_utf8(buff).unwrap_or_default();
} else {
return err_invalid_xsqlda();
}
}
ibase::isc_info_sql_alias => {
let len = resp.get_u16_le()? as usize;
let mut buff = vec![0; len];
resp.copy_to_slice(&mut buff)?;
if let Some(var) = xsqlda.get_mut(col_index) {
var.alias_name = String::from_utf8(buff).unwrap_or_default();
} else {
return err_invalid_xsqlda();
}
}
ibase::isc_info_sql_describe_end => {}
ibase::isc_info_truncated => break true,
ibase::isc_info_end => break false,
item => {
return Err(FbError::Other(format!(
"Invalid item received in the xsqlda: {}",
item
)));
}
}
};
Ok(truncated)
}
fn err_invalid_xsqlda<T>() -> Result<T, FbError> {
Err(FbError::Other(
"Invalid Xsqlda received from server".to_string(),
))
}