void
Schedule::wireDecode(const Block& wire)
{
  if (wire.type() != tlv::Schedule)
    BOOST_THROW_EXCEPTION(tlv::Error("Unexpected TLV type when decoding RepetitiveInterval"));

  m_wire = wire;
  m_wire.parse();

  if (m_wire.elements_size() != 2)
    BOOST_THROW_EXCEPTION(tlv::Error("RepetitiveInterval tlv does not have two sub-TLVs"));

  Block::element_const_iterator it = m_wire.elements_begin();

  if (it != m_wire.elements_end() && it->type() == tlv::WhiteIntervalList) {
    it->parse();
    Block::element_const_iterator tempIt = it->elements_begin();
    while (tempIt != it->elements_end() && tempIt->type() == tlv::RepetitiveInterval) {
      m_whiteIntervalList.insert(RepetitiveInterval(*tempIt));
      tempIt++;
    }
    it++;
  }

  if (it != m_wire.elements_end() && it->type() == tlv::BlackIntervalList) {
    it->parse();
    Block::element_const_iterator tempIt = it->elements_begin();
    while (tempIt != it->elements_end() && tempIt->type() == tlv::RepetitiveInterval) {
      m_blackIntervalList.insert(RepetitiveInterval(*tempIt));
      tempIt++;
    }
    it++;
  }
}
void
Manifest::decode()
{
    Block content = getContent();
    content.parse();

    // Manifest ::= CONTENT-TLV TLV-LENGTH
    //                Catalogue?
    //                  Name*
    //                KeyValuePair*

    for ( Block::element_const_iterator val = content.elements_begin();
            val != content.elements_end(); ++val)
    {
        if (val->type() == tlv::ManifestCatalogue)
        {
            val->parse();
            for ( Block::element_const_iterator catalogueNameElem = val->elements_begin();
                    catalogueNameElem != val->elements_end(); ++catalogueNameElem)
            {
                if (catalogueNameElem->type() == tlv::Name)
                {
                    Name name(*catalogueNameElem);
                    m_catalogueNames.push_back(name);
                }
            }
        }
        else if (val->type() == tlv::KeyValuePair)
        {
            std::string str((char*)val->value(), val->value_size());

            size_t index = str.find_first_of('=');
            if (index == std::string::npos || index == 0 || (index == str.size() - 1))
                continue;

            std::string key = str.substr(0, index);
            std::string value = str.substr(index + 1, str.size() - index - 1);
            addKeyValuePair(key, value);
        }
    }
}