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
//!
//! IPP stream parser
//!
use byteorder::{BigEndian, ReadBytesExt};
use std::io::Read;

use num_traits::FromPrimitive;

use attribute::{IppAttribute, IppAttributeList};
use consts::statuscode::StatusCode;
use consts::tag::*;
use value::IppValue;
use {IppError, IppHeader, ReadIppExt, Result};

fn list_to_value(mut list: Vec<IppValue>) -> IppValue {
    if list.len() == 1 {
        list.remove(0)
    } else {
        IppValue::ListOf(list)
    }
}

/// IPP parsing result
pub struct IppParseResult {
    header: IppHeader,
    attributes: IppAttributeList,
}

impl IppParseResult {
    /// Create instance of the parsing result
    pub fn new(header: IppHeader, attributes: IppAttributeList) -> IppParseResult {
        IppParseResult { header, attributes }
    }

    /// Get parsed header
    pub fn header(&self) -> &IppHeader {
        &self.header
    }

    /// Get parsed attributes
    pub fn attributes(&self) -> &IppAttributeList {
        &self.attributes
    }
}

/// IPP parser implementation
pub struct IppParser<'a> {
    reader: &'a mut Read,
}

impl<'a> IppParser<'a> {
    /// Create IPP parser using the given Read
    pub fn new(reader: &'a mut Read) -> IppParser<'a> {
        IppParser { reader }
    }

    /// Parse IPP stream
    pub fn parse(&mut self) -> Result<IppParseResult> {
        // last delimiter tag
        let mut delimiter = DelimiterTag::EndOfAttributes;

        // stack of current attributes context. Used with lists and collections
        let mut stack = vec![vec![]];

        // holds the result of parsing
        let mut retval = IppAttributeList::new();

        // name of previous attribute name
        let mut last_name: Option<String> = None;

        // parse IPP header
        let header = IppHeader::from_reader(self.reader)?;
        debug!("IPP reply header: {:?}", header);

        loop {
            let tag = self.reader.read_u8()?;
            if is_delimiter_tag(tag) {
                debug!("Delimiter tag: {:0x}", tag);
                if tag == DelimiterTag::EndOfAttributes as u8 {
                    // end of stream, get last saved collection
                    if let Some(last_name) = last_name {
                        if let Some(val_list) = stack.pop() {
                            retval.add(
                                delimiter,
                                IppAttribute::new(&last_name, list_to_value(val_list)),
                            );
                        }
                    }
                    break;
                } else {
                    // remember delimiter tag
                    delimiter =
                        DelimiterTag::from_u8(tag).ok_or(StatusCode::ClientErrorBadRequest)?;
                }
            } else if is_value_tag(tag) {
                // value tag
                let namelen = self.reader.read_u16::<BigEndian>()?;
                let name = self.reader.read_string(namelen as usize)?;
                let value = IppValue::read(tag, &mut self.reader)?;

                debug!("Value tag: {:0x}: {}: {}", tag, name, value);

                if namelen > 0 {
                    // single attribute or begin of array
                    if let Some(last_name) = last_name {
                        // put the previous attribute into the retval
                        if let Some(val_list) = stack.pop() {
                            retval.add(
                                delimiter,
                                IppAttribute::new(&last_name, list_to_value(val_list)),
                            );
                        }
                        stack.push(vec![]);
                    }
                    // store it as a previous attribute
                    last_name = Some(name);
                }
                if tag == ValueTag::BegCollection as u8 {
                    // start new collection in the stack
                    debug!("Begin collection");
                    stack.push(vec![])
                } else if tag == ValueTag::EndCollection as u8 {
                    // get collection from the stack and add it to the previous element
                    debug!("End collection");
                    if let Some(arr) = stack.pop() {
                        if let Some(val_list) = stack.last_mut() {
                            val_list.push(IppValue::Collection(arr));
                        }
                    }
                } else if let Some(val_list) = stack.last_mut() {
                    // add attribute to the current collection
                    val_list.push(value);
                }
            } else {
                return Err(IppError::TagError(tag));
            }
        }

        Ok(IppParseResult::new(header, retval))
    }
}