| /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| /** |
| * Copyright (C) 2013 Regents of the University of California. |
| * @author: Yingdi Yu <yingdi@cs.ucla.edu> |
| * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
| * See COPYING for copyright and distribution information. |
| */ |
| |
| #ifndef NDN_DER_HPP |
| #define NDN_DER_HPP |
| |
| #include <vector> |
| #include <string> |
| #include <istream> |
| #include <ostream> |
| #include <sstream> |
| |
| #include <ndn-cpp/common.hpp> |
| #include <ndn-cpp/encoding/oid.hpp> |
| |
| #include "visitor/visitor.hpp" |
| #include "visitor/void-visitor.hpp" |
| #include "visitor/no-arguments-visitor.hpp" |
| #include "visitor/void-no-arguments-visitor.hpp" |
| |
| namespace ndn { |
| |
| namespace der { |
| |
| enum DerType { |
| DER_EOC = 0, |
| DER_BOOLEAN = 1, |
| DER_INTEGER = 2, |
| DER_BIT_STRING = 3, |
| DER_OCTET_STRING = 4, |
| DER_NULL = 5, |
| DER_OBJECT_IDENTIFIER = 6, |
| DER_OBJECT_DESCRIPTOR = 7, |
| DER_EXTERNAL = 40, |
| DER_REAL = 9, |
| DER_ENUMERATED = 10, |
| DER_EMBEDDED_PDV = 43, |
| DER_UTF8_STRING = 12, |
| DER_RELATIVE_OID = 13, |
| DER_SEQUENCE = 48, |
| DER_SET = 49, |
| DER_NUMERIC_STRING = 18, |
| DER_PRINTABLE_STRING = 19, |
| DER_T61_STRING = 20, |
| DER_VIDEOTEX_STRING = 21, |
| DER_IA5_STRING = 22, |
| DER_UTC_TIME = 23, |
| DER_GENERALIZED_TIME = 24, |
| DER_GRAPHIC_STRING = 25, |
| DER_VISIBLE_STRING = 26, |
| DER_GENERAL_STRING = 27, |
| DER_UNIVERSAL_STRING = 28, |
| DER_CHARACTER_STRING = 29, |
| DER_BMP_STRING = 30, |
| }; |
| |
| class DerComplex; |
| |
| class DerNode |
| { |
| public: |
| DerNode(); |
| |
| DerNode(DerType type); |
| |
| DerNode(std::istream& start); |
| |
| virtual |
| ~DerNode(); |
| |
| virtual int |
| getSize() { return header_.size() + payload_.size(); } |
| |
| virtual void |
| encode(std::ostream& start); |
| |
| void |
| setParent(DerComplex * parent) { parent_ = parent; } |
| |
| static ptr_lib::shared_ptr<DerNode> |
| parse(std::istream& start); |
| |
| const std::vector<uint8_t>& |
| getHeader() const { return header_; } |
| |
| std::vector<uint8_t>& |
| getHeader() { return header_; } |
| |
| const std::vector<uint8_t>& |
| getPayload() const { return payload_; } |
| |
| std::vector<uint8_t>& |
| getPayload() { return payload_; } |
| |
| const DerType& |
| getType() { return type_; } |
| |
| virtual ptr_lib::shared_ptr<std::vector<uint8_t> > |
| getRaw() |
| { |
| ptr_lib::shared_ptr<std::vector<uint8_t> > blob(new std::vector<uint8_t>()); |
| blob->insert(blob->end(), header_.begin(), header_.end()); |
| blob->insert(blob->end(), payload_.begin(), payload_.end()); |
| |
| return blob; |
| } |
| |
| virtual void accept(VoidNoArgumentsVisitor& visitor) = 0; |
| virtual void accept(VoidVisitor& visitor, ndnboost::any param) = 0; |
| virtual ndnboost::any accept(NoArgumentsVisitor& visitor) = 0; |
| virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) = 0; |
| |
| protected: |
| void |
| decode(std::istream& start); |
| |
| void |
| encodeHeader(int size); |
| |
| int |
| decodeHeader(std::istream& start); |
| |
| protected: |
| DerType type_; |
| std::vector<uint8_t> header_; |
| std::vector<uint8_t> payload_; |
| DerComplex * parent_; |
| }; |
| |
| |
| typedef std::vector<ptr_lib::shared_ptr<DerNode> > DerNodePtrList; |
| |
| class DerComplex : public DerNode |
| { |
| public: |
| DerComplex(); |
| |
| DerComplex(DerType type); |
| |
| DerComplex(std::istream& start); |
| |
| virtual |
| ~DerComplex(); |
| |
| virtual int |
| getSize(); |
| |
| void |
| addChild(ptr_lib::shared_ptr<DerNode> nodePtr, bool notifyParent = true); |
| |
| virtual void |
| encode(std::ostream& start); |
| |
| const DerNodePtrList& |
| getChildren() const { return nodeList_; } |
| |
| DerNodePtrList& |
| getChildren() { return nodeList_; } |
| |
| virtual ptr_lib::shared_ptr<std::vector<uint8_t> > |
| getRaw(); |
| |
| private: |
| void |
| updateSize(); |
| |
| void |
| setChildChanged(); |
| |
| private: |
| bool childChanged_; |
| int size_; |
| DerNodePtrList nodeList_; |
| }; |
| |
| |
| |
| class DerByteString : public DerNode |
| { |
| public: |
| DerByteString(const std::string& str, DerType type); |
| |
| DerByteString(const std::vector<uint8_t>& blob, DerType type); |
| |
| DerByteString(std::istream& start); |
| |
| virtual |
| ~DerByteString(); |
| }; |
| |
| |
| //0x01 |
| class DerBool : public DerNode |
| { |
| public: |
| DerBool(bool value); |
| |
| DerBool(std::istream& start); |
| |
| virtual |
| ~DerBool(); |
| |
| virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); } |
| virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); } |
| virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); } |
| virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); } |
| }; |
| |
| //0x02 |
| class DerInteger : public DerNode |
| { |
| public: |
| DerInteger(const std::vector<uint8_t>& blob); |
| |
| DerInteger(std::istream& start); |
| |
| virtual |
| ~DerInteger(); |
| |
| virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); } |
| virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); } |
| virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); } |
| virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); } |
| }; |
| |
| //0x03 |
| class DerBitString : public DerNode |
| { |
| public: |
| DerBitString(const std::vector<uint8_t>& blob, uint8_t paddingLen); |
| |
| DerBitString(std::istream& start); |
| |
| virtual |
| ~DerBitString(); |
| |
| virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); } |
| virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); } |
| virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); } |
| virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); } |
| }; |
| |
| //0x04 |
| class DerOctetString : public DerByteString |
| { |
| public: |
| DerOctetString(const std::string& str); |
| |
| DerOctetString(const std::vector<uint8_t>& blob); |
| |
| DerOctetString(std::istream& start); |
| |
| virtual |
| ~DerOctetString(); |
| |
| virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); } |
| virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); } |
| virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); } |
| virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); } |
| }; |
| |
| |
| //0x05 |
| class DerNull : public DerNode |
| { |
| public: |
| DerNull(); |
| |
| DerNull(std::istream& start); |
| |
| virtual |
| ~DerNull(); |
| |
| virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); } |
| virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); } |
| virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); } |
| virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); } |
| }; |
| |
| |
| //0x06 |
| class DerOid : public DerNode |
| { |
| public: |
| DerOid(const OID& oid); |
| |
| DerOid(const std::string& oidStr); |
| |
| DerOid(const std::vector<int>& value); |
| |
| DerOid(std::istream& start); |
| |
| virtual |
| ~DerOid(); |
| |
| virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); } |
| virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); } |
| virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); } |
| virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); } |
| |
| int |
| decode128(int& offset); |
| |
| private: |
| void |
| prepareEncoding(const std::vector<int>& value); |
| |
| void |
| encode128(int value, std::ostringstream& os); |
| }; |
| |
| |
| //0x10 |
| class DerSequence : public DerComplex |
| { |
| public: |
| DerSequence(); |
| |
| DerSequence(std::istream& start); |
| |
| virtual |
| ~DerSequence(); |
| |
| virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); } |
| virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); } |
| virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); } |
| virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); } |
| }; |
| |
| //0x13 |
| class DerPrintableString : public DerByteString |
| { |
| public: |
| DerPrintableString(const std::string& str); |
| |
| DerPrintableString(const std::vector<uint8_t>& blob); |
| |
| DerPrintableString(std::istream& start); |
| |
| virtual |
| ~DerPrintableString(); |
| |
| virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); } |
| virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); } |
| virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); } |
| virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); } |
| }; |
| |
| //0x1b |
| class DerGtime : public DerNode |
| { |
| public: |
| DerGtime(const MillisecondsSince1970& time); |
| |
| DerGtime(std::istream& start); |
| |
| virtual |
| ~DerGtime(); |
| |
| virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); } |
| virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); } |
| virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); } |
| virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); } |
| |
| /** |
| * Convert to the ISO string representation of the time. |
| * @param time Milliseconds since 1/1/1970. |
| * @return The ISO string. |
| */ |
| static std::string toIsoString(const MillisecondsSince1970& time); |
| |
| /** |
| * Convert from the ISO string representation to the internal time format. |
| * @param isoString The ISO time formatted string. |
| * @return The time in milliseconds since 1/1/1970. |
| */ |
| static MillisecondsSince1970 fromIsoString(const std::string& isoString); |
| }; |
| |
| } // der |
| |
| } |
| |
| #endif |