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
use crate::sql_read_bytes::SqlReadBytes;
// Decode a partially length-prefixed type.
//
// NOTE: values are read via the packet-aware `read_u8`/`read_u16_le`/`read_u32_le`
// helpers (which transparently span TDS packet boundaries). The generic
// `AsyncReadExt::read_exact` must NOT be used here: a PLP value can span multiple
// packets, and `read_exact` treats a packet-boundary `Ok(0)` as EOF.
pub(crate) async fn decode<R>(src: &mut R, len: usize) -> crate::Result<Option<Vec<u8>>>
where
R: SqlReadBytes + Unpin,
{
match len {
// Fixed size
len if len < 0xffff => {
let len = src.read_u16_le().await? as usize;
match len {
// NULL
0xffff => Ok(None),
_ => {
let mut data = Vec::with_capacity(len.min(super::MAX_PREALLOC));
for _ in 0..len {
data.push(src.read_u8().await?);
}
Ok(Some(data))
}
}
}
// Unknown size, length-prefixed blobs
_ => {
let len = src.read_u64_le().await?;
let mut data = match len {
// NULL
0xffffffffffffffff => return Ok(None),
// Unknown size
0xfffffffffffffffe => Vec::new(),
// Known size. `len` is an untrusted 64-bit wire value; cap the
// up-front reservation (avoids memory-exhaustion and the
// `Vec` capacity-overflow panic for values near u64::MAX).
_ => Vec::with_capacity((len as usize).min(super::MAX_PREALLOC)),
};
let mut chunk_data_left = 0usize;
loop {
if chunk_data_left == 0 {
// We have no chunk. Start a new one.
let chunk_size = src.read_u32_le().await? as usize;
if chunk_size == 0 {
break; // found a sentinel, we're done
}
// The number of chunks in an "unknown length" PLP value is
// unbounded on the wire. Cap the running total so a hostile
// server cannot stream chunks forever and exhaust memory on
// a single value.
if data.len().saturating_add(chunk_size) > super::MAX_PLP_SIZE {
return Err(crate::Error::Protocol(
format!(
"PLP value exceeds the maximum supported size of {} bytes",
super::MAX_PLP_SIZE
)
.into(),
));
}
chunk_data_left = chunk_size;
} else {
// Read a byte (packet-aware).
let byte = src.read_u8().await?;
chunk_data_left -= 1;
data.push(byte);
}
}
Ok(Some(data))
}
}
}