tiberius/tds/codec/column_data/plp.rs
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
use crate::sql_read_bytes::SqlReadBytes;
// Decode a partially length-prefixed type.
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 u64;
match len {
// NULL
0xffff => Ok(None),
_ => {
let mut data = Vec::with_capacity(len as usize);
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
_ => Vec::with_capacity(len as usize),
};
let mut chunk_data_left = 0;
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
} else {
chunk_data_left = chunk_size
}
} else {
// Just read a byte
let byte = src.read_u8().await?;
chunk_data_left -= 1;
data.push(byte);
}
}
Ok(Some(data))
}
}
}