tiberius/tds/codec/column_data/
string.rs

1use std::borrow::Cow;
2
3use byteorder::{ByteOrder, LittleEndian};
4
5use crate::{error::Error, sql_read_bytes::SqlReadBytes, tds::Collation, VarLenType};
6
7pub(crate) async fn decode<R>(
8    src: &mut R,
9    ty: VarLenType,
10    len: usize,
11    collation: Option<Collation>,
12) -> crate::Result<Option<Cow<'static, str>>>
13where
14    R: SqlReadBytes + Unpin,
15{
16    use VarLenType::*;
17
18    let data = super::plp::decode(src, len).await?;
19
20    match (data, ty) {
21        // Codepages other than UTF
22        (Some(buf), BigChar) | (Some(buf), BigVarChar) => {
23            let collation = collation.as_ref().unwrap();
24            let encoder = collation.encoding()?;
25
26            let s = encoder
27                .decode_without_bom_handling_and_without_replacement(buf.as_ref())
28                .ok_or_else(|| Error::Encoding("invalid sequence".into()))?
29                .to_string();
30
31            Ok(Some(s.into()))
32        }
33        // UTF-16
34        (Some(buf), _) => {
35            if buf.len() % 2 != 0 {
36                return Err(Error::Protocol("nvarchar: invalid plp length".into()));
37            }
38
39            let buf: Vec<_> = buf.chunks(2).map(LittleEndian::read_u16).collect();
40            Ok(Some(String::from_utf16(&buf)?.into()))
41        }
42        _ => Ok(None),
43    }
44}