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
//! #  de/binary -- de-serialize LLSD, binary form.
//!
//!  Library for serializing and de-serializing data in
//!  Linden Lab Structured Data format.
//!
//!  Format documentation is at http://wiki.secondlife.com/wiki/LLSD
//!
//!  Binary format.
//
//  Animats
//  March, 2021.
//  License: LGPL.
//
use crate::LLSDValue;
use anyhow::{anyhow, Error};
use std::collections::HashMap;
use std::io::{Cursor, Read};
use uuid;
//
//  Constants
//
pub const LLSDBINARYPREFIX: &[u8] = b"<? LLSD/Binary ?>\n"; // binary LLSD prefix
pub const LLSDBINARYSENTINEL: &[u8] = LLSDBINARYPREFIX; // prefix must match exactly

///    Parse LLSD array expressed in binary into an LLSDObject tree. No header.
pub fn from_bytes(b: &[u8]) -> Result<LLSDValue, Error> {
    let mut cursor: Cursor<&[u8]> = Cursor::new(b);
    parse_value(&mut cursor)
}

///    Parse LLSD reader expressed in binary into an LLSDObject tree. No header.
pub fn from_reader(cursor: &mut dyn Read) -> Result<LLSDValue, Error> {
    parse_value(cursor)
}

/// Parse one value - real, integer, map, etc. Recursive.
fn parse_value(cursor: &mut dyn Read) -> Result<LLSDValue, Error> {
    //  These could be generic if generics with numeric parameters were in stable Rust.
    fn read_u8(cursor: &mut dyn Read) -> Result<u8, Error> {
        let mut b: [u8; 1] = [0; 1];
        cursor.read_exact(&mut b)?; // read one byte
        Ok(b[0])
    }
    fn read_u32(cursor: &mut dyn Read) -> Result<u32, Error> {
        let mut b: [u8; 4] = [0; 4];
        cursor.read_exact(&mut b)?; // read one byte
        Ok(u32::from_be_bytes(b))
    }
    fn read_i32(cursor: &mut dyn Read) -> Result<i32, Error> {
        let mut b: [u8; 4] = [0; 4];
        cursor.read_exact(&mut b)?; // read one byte
        Ok(i32::from_be_bytes(b))
    }
    fn read_i64(cursor: &mut dyn Read) -> Result<i64, Error> {
        let mut b: [u8; 8] = [0; 8];
        cursor.read_exact(&mut b)?; // read one byte
        Ok(i64::from_be_bytes(b))
    }
    fn read_f64(cursor: &mut dyn Read) -> Result<f64, Error> {
        let mut b: [u8; 8] = [0; 8];
        cursor.read_exact(&mut b)?; // read one byte
        Ok(f64::from_be_bytes(b))
    }
    fn read_variable(cursor: &mut dyn Read) -> Result<Vec<u8>, Error> {
        let length = read_u32(cursor)?; // read length in bytes
        let mut buf = vec![0u8; length as usize];
        cursor.read_exact(&mut buf)?;
        Ok(buf) // read bytes of string
    }

    let typecode = read_u8(cursor)?;
    match typecode {
        //  Undefined - the empty value
        b'!' => Ok(LLSDValue::Undefined),
        //  Boolean - 1 or 0
        b'0' => Ok(LLSDValue::Boolean(false)),
        b'1' => Ok(LLSDValue::Boolean(true)),
        //  String - length followed by data
        b's' => Ok(LLSDValue::String(
            std::str::from_utf8(&read_variable(cursor)?)?.to_string(),
        )),
        //  URI - length followed by data
        b'l' => Ok(LLSDValue::URI(
            std::str::from_utf8(&read_variable(cursor)?)?.to_string(),
        )),
        //  Integer - 4 bytes
        b'i' => Ok(LLSDValue::Integer(read_i32(cursor)?)),
        //  Real - 4 bytes
        b'r' => Ok(LLSDValue::Real(read_f64(cursor)?)),
        //  UUID - 16 bytes
        b'u' => {
            let mut buf: [u8; 16] = [0u8; 16];
            cursor.read_exact(&mut buf)?; // read bytes of string
            Ok(LLSDValue::UUID(uuid::Uuid::from_bytes(buf)))
        }
        //  Binary - length followed by data
        b'b' => Ok(LLSDValue::Binary(read_variable(cursor)?)),
        //  Date - 64 bits
        b'd' => Ok(LLSDValue::Date(read_i64(cursor)?)),
        //  Map -- keyed collection of items
        b'{' => {
            let mut dict: HashMap<String, LLSDValue> = HashMap::new(); // accumulate hash here
            let count = read_u32(cursor)?; // number of items
            for _ in 0..count {
                let keyprefix = &read_u8(cursor)?; // key should begin with b'k';
                match keyprefix {
                    b'k' => {
                        let key = std::str::from_utf8(&read_variable(cursor)?)?.to_string();
                        let _ = dict.insert(key, parse_value(cursor)?); // recurse and add, allowing dups
                    }
                    _ => {
                        return Err(anyhow!(
                            "Binary LLSD map key had {:?} instead of expected 'k'",
                            keyprefix
                        ))
                    }
                }
            }
            if read_u8(cursor)? != b'}' {
                return Err(anyhow!("Binary LLSD map did not end properly with }}"));
            }
            Ok(LLSDValue::Map(dict))
        }
        //  Array -- array of items
        b'[' => {
            let mut array: Vec<LLSDValue> = Vec::new(); // accumulate hash here
            let count = read_u32(cursor)?; // number of items
            for _ in 0..count {
                array.push(parse_value(cursor)?); // recurse and add, allowing dups
            }
            if read_u8(cursor)? != b']' {
                return Err(anyhow!("Binary LLSD array did not end properly with ] "));
            }
            Ok(LLSDValue::Array(array))
        }

        _ => Err(anyhow!("Binary LLSD, unexpected type code {:?}", typecode)),
    }
}

// Unit test

#[test]
fn binaryparsetest1() {
    //  Construct a test value.
    let test1map: HashMap<String, LLSDValue> = [
        ("val1".to_string(), LLSDValue::Real(456.0)),
        ("val2".to_string(), LLSDValue::Integer(999)),
    ]
    .iter()
    .cloned()
    .collect();
    let test1: LLSDValue = LLSDValue::Array(vec![
        LLSDValue::Real(123.5),
        LLSDValue::Map(test1map),
        LLSDValue::Integer(42),
        LLSDValue::String("Hello world".to_string()),
    ]);
    //  Convert to binary form.
    let test1bin = crate::to_bytes(&test1).unwrap();
    println!("Binary form: {:?}", test1bin);
    //  Convert back to value form.
    let test1value = from_bytes(&test1bin[LLSDBINARYSENTINEL.len()..]).unwrap();
    println!("Value after round-trip conversion: {:?}", test1value);
    //  Check that results match after round trip.
    assert_eq!(test1, test1value);
}