src/lib/GenericASN1ContentHandler.h
branchv_0
changeset 23 8941a679299f
parent 21 705036445672
child 26 e39de9b8b3a1
equal deleted inserted replaced
22:9b6f86760384 23:8941a679299f
    13  *
    13  *
    14  * You should have received a copy of the GNU General Public License
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    16  */
    16  */
    17 #pragma once
    17 #pragma once
       
    18 
       
    19 #include <sstream>
       
    20 #include <iomanip>
    18 
    21 
    19 #include "ASN1ContentHandler.h"
    22 #include "ASN1ContentHandler.h"
    20 #include "XMLContentHandler.h"
    23 #include "XMLContentHandler.h"
    21 
    24 
    22 namespace relpipe {
    25 namespace relpipe {
    75 		handlers.writeStartElement("integer",{"hex", value.toHex()});
    78 		handlers.writeStartElement("integer",{"hex", value.toHex()});
    76 		handlers.writeCharacters(value.toString());
    79 		handlers.writeCharacters(value.toString());
    77 		handlers.writeEndElement();
    80 		handlers.writeEndElement();
    78 	}
    81 	}
    79 
    82 
    80 	void writeString(StringType type, std::string value) override {
    83 	void writeTextString(StringType type, std::string value) override {
    81 		handlers.writeStartElement("string");
    84 		handlers.writeStartElement("text-string",{"type", std::to_string((uint64_t) type)}); // TODO: type name, better attributes
    82 		handlers.writeCharacters(value);
    85 		handlers.writeCharacters(value);
    83 		handlers.writeEndElement();
    86 		handlers.writeEndElement();
    84 	}
    87 	}
    85 
    88 
       
    89 	void writeOctetString(std::string value) override {
       
    90 		std::stringstream hex;
       
    91 		hex << std::hex << std::setfill('0');
       
    92 		for (uint8_t b : value) hex << std::setw(2) << (int) b;
       
    93 		handlers.writeStartElement("octet-string",{"length", std::to_string(value.size())});
       
    94 		handlers.writeCharacters(hex.str());
       
    95 		handlers.writeEndElement();
       
    96 	}
       
    97 
       
    98 	void writeBitString(std::vector<bool> value) override {
       
    99 		std::stringstream bits;
       
   100 		for (bool b : value) bits << (int) b;
       
   101 		// for (bool b : value) bits << (b ? ':' : '.'); // TODO: configurable true/false symbols?
       
   102 		handlers.writeStartElement("bit-string",{"length", std::to_string(value.size())});
       
   103 		handlers.writeCharacters(bits.str());
       
   104 		handlers.writeEndElement();
       
   105 	}
       
   106 
    86 	void writeOID(ObjectIdentifier value) override {
   107 	void writeOID(ObjectIdentifier value) override {
       
   108 		// TODO: optionally expand into separate elements with additional metadata
    87 		handlers.writeStartElement("oid");
   109 		handlers.writeStartElement("oid");
    88 		handlers.writeCharacters(value.toString());
   110 		handlers.writeCharacters(value.toString());
    89 		handlers.writeEndElement();
   111 		handlers.writeEndElement();
    90 	}
   112 	}
    91 
   113