Hex.h - hex/binary encoding v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Wed, 20 Apr 2022 02:27:05 +0200
branchv_0
changeset 5 5a6828cfad41
parent 4 8a5b86415d80
child 6 b0b7b6f1bc88
Hex.h - hex/binary encoding
src/Hex.h
src/PosixMQHandler.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/Hex.h	Wed Apr 20 02:27:05 2022 +0200
@@ -0,0 +1,85 @@
+/**
+ * Relational pipes
+ * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#pragma once
+
+#include <iomanip>
+#include <iostream>
+#include <sstream>
+#include <vector>
+#include <stdexcept>
+
+namespace relpipe {
+namespace out {
+namespace posixmq {
+
+class Hex {
+private:
+
+	Hex() {
+	}
+
+	static char fromHex(wchar_t ch) {
+		if (L'0' <= ch && ch <= L'9') return ch - L'0';
+		else if (L'a' <= ch && ch <= L'f') return ch - L'a' + 10;
+		else throw std::invalid_argument("Unable to decode hexadeximal string.");
+	}
+
+public:
+
+	static std::stringstream fromHex(const std::wstring& hex) {
+		std::stringstream octets;
+
+		char octet = 0;
+
+		for (size_t i = 0, limit = hex.size(); i < limit; i++) {
+			if (i % 2 == 0) {
+				octet = fromHex(hex[i]) << 4;
+			} else {
+				octet += fromHex(hex[i]);
+				octets.put(octet);
+			}
+		}
+
+		return octets;
+	}
+
+	static std::wstring toHex(const std::string& octets) {
+		std::wstring_convert < codecvt_utf8<wchar_t>> convertor; // TODO: do not create converter each time
+		std::stringstream hex;
+		hex << std::hex << std::setfill('0') << std::hex;
+		for (size_t i = 0, size = octets.size(); i < size; i++) hex << std::setw(2) << (0xff & octets[i]);
+		return convertor.from_bytes(hex.str());
+	}
+
+	static std::wstring toTxt(const std::string& octets, bool* validEncoding = nullptr) {
+		std::wstring_convert < codecvt_utf8<wchar_t>> convertor; // TODO: do not create converter each time
+		try {
+			if (validEncoding) *validEncoding = true;
+			// TODO: use encoding from the HTTP response headers instead of the constant one?
+			return convertor.from_bytes(octets);
+		} catch (...) {
+			if (validEncoding) *validEncoding = false;
+			std::stringstream filtered;
+			for (char ch : octets) filtered << (ch >= ' ' && ch < 127 ? ch : '.');
+			return convertor.from_bytes(filtered.str());
+		}
+	}
+};
+
+}
+}
+}
--- a/src/PosixMQHandler.h	Tue Mar 15 00:28:13 2022 +0100
+++ b/src/PosixMQHandler.h	Wed Apr 20 02:27:05 2022 +0200
@@ -31,6 +31,7 @@
 
 #include "PosixMQ.h"
 #include "Configuration.h"
+#include "Hex.h"
 
 namespace relpipe {
 namespace out {
@@ -49,16 +50,6 @@
 		std::string currentValue;
 	} currentRelation;
 
-	std::string formatText(relpipe::common::type::StringX value) {
-		// TODO: check valid UTF-8; if invalid, return only ASCII characters
-		return convertor.to_bytes(value);
-	}
-
-	std::string formatData(relpipe::common::type::StringX value) {
-		// TODO: decode HEX if type is string; return original octets if type is octet-string (not yet implemented)
-		return convertor.to_bytes(value);
-	}
-
 public:
 
 	PosixMQHandler(Configuration configuration) : configuration(configuration) {
@@ -72,8 +63,8 @@
 	void attribute(const relpipe::common::type::StringX& value) override {
 
 		auto attributeName = currentRelation.attributes[currentRelation.attributeIndex].getAttributeName();
-		if (attributeName == L"text") currentRelation.currentValue = formatText(value);
-		else if (attributeName == L"data") currentRelation.currentValue = formatData(value);
+		if (attributeName == L"text") currentRelation.currentValue = convertor.to_bytes(value);
+		else if (attributeName == L"data") currentRelation.currentValue = Hex::fromHex(value).str();
 
 		currentRelation.attributeIndex++;
 		if (currentRelation.attributeIndex == currentRelation.attributes.size()) {