58 attributes.push_back(tagClass); |
58 attributes.push_back(tagClass); |
59 |
59 |
60 return attributes; |
60 return attributes; |
61 } |
61 } |
62 |
62 |
|
63 /** |
|
64 * @param header |
|
65 * @param value an octet string |
|
66 * @return original value if octet string was a text string; otherwise an empty string |
|
67 */ |
|
68 const std::string getText(const Header& header, const std::string& value) { |
|
69 // TODO: support also UTF-8 |
|
70 for (uint8_t b : value) if (b < 32 || b >= 127) return ""; |
|
71 return value; |
|
72 } |
|
73 |
63 public: |
74 public: |
64 |
75 |
65 void addHandler(std::shared_ptr<XMLContentHandler> handler) { |
76 void addHandler(std::shared_ptr<XMLContentHandler> handler) { |
66 handlers.addHandler(handler); |
77 handlers.addHandler(handler); |
67 } |
78 } |
110 handlers.writeCharacters(value); |
121 handlers.writeCharacters(value); |
111 handlers.writeEndElement(); |
122 handlers.writeEndElement(); |
112 } |
123 } |
113 |
124 |
114 void writeOctetString(const Header& header, std::string value) override { |
125 void writeOctetString(const Header& header, std::string value) override { |
|
126 std::vector<std::string> attributes = getCommonAttributes(header,{"length", std::to_string(value.size())}); |
|
127 std::string text = getText(header, value); |
|
128 if (text.size()) { |
|
129 attributes.push_back("text"); |
|
130 attributes.push_back(text); |
|
131 } |
|
132 |
115 std::stringstream hex; |
133 std::stringstream hex; |
116 hex << std::hex << std::setfill('0'); |
134 hex << std::hex << std::setfill('0'); |
117 for (uint8_t b : value) hex << std::setw(2) << (int) b; |
135 for (uint8_t b : value) hex << std::setw(2) << (int) b; |
118 handlers.writeStartElement("octet-string", getCommonAttributes(header,{"length", std::to_string(value.size())})); |
136 handlers.writeStartElement("octet-string", attributes); |
119 handlers.writeCharacters(hex.str()); |
137 handlers.writeCharacters(hex.str()); |
120 handlers.writeEndElement(); |
138 handlers.writeEndElement(); |
121 } |
139 } |
122 |
140 |
123 void writeBitString(const Header& header, std::vector<bool> value) override { |
141 void writeBitString(const Header& header, std::vector<bool> value) override { |