Add attributes

This commit is contained in:
moznion
2020-11-20 00:03:05 +09:00
parent c386bce28e
commit 0766f51c62
3 changed files with 52 additions and 1 deletions

View File

@@ -4,7 +4,7 @@ use std::string::FromUtf8Error;
use chrono::{DateTime, Utc, TimeZone};
pub struct Attribute(Vec<u8>);
pub struct Attribute(pub(crate) Vec<u8>);
impl Attribute {
pub fn from_integer32(v: &u32) -> Self {

50
src/attributes.rs Normal file
View File

@@ -0,0 +1,50 @@
use crate::attribute::Attribute;
pub type Type = u8;
pub const TYPE_INVALID: Type = 1;
pub struct AVP {
typ: Type,
attribute: Attribute,
}
pub struct Attributes(Vec<AVP>);
impl Attributes {
pub fn parse_attributes(bs: &Vec<u8>) -> Result<Attributes, String> {
let mut i = 0;
let mut attrs = Vec::new();
while bs.len() < i {
if bs[i..].len() < 2 {
return Err("short buffer".to_owned());
}
let length = bs[i + 1] as usize;
if length > bs[i..].len() || length < 2 || length > 255 {
return Err("invalid attribute length".to_owned());
}
attrs.push(AVP {
typ: bs[i + 0],
attribute: if length > 2 {
Attribute(bs[i + 2..].to_vec())
} else {
Attribute(vec![])
},
});
i += length;
}
Ok(Attributes(attrs))
}
pub fn add(&mut self, typ: Type, attribute: Attribute) {
self.0.push(AVP {
typ,
attribute,
})
}
}

View File

@@ -1,2 +1,3 @@
pub mod code;
pub mod attribute;
pub mod attributes;