src/Hex.h
author František Kučera <franta-hg@frantovo.cz>
Thu, 21 Apr 2022 00:57:30 +0200
branchv_0
changeset 28 fe61bf7d1716
permissions -rw-r--r--
use common hex function, send also binary content, not only text
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <iomanip>
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <iostream>
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <sstream>
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <vector>
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <stdexcept>
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
namespace relpipe {
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
namespace tr {
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
namespace http {
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
class Hex {
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
private:
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
	Hex() {
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
	}
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
	static char fromHex(wchar_t ch) {
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
		if (L'0' <= ch && ch <= L'9') return ch - L'0';
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
		else if (L'a' <= ch && ch <= L'f') return ch - L'a' + 10;
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
		else throw std::invalid_argument("Unable to decode hexadeximal string.");
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
	}
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
public:
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	static std::stringstream fromHex(const std::wstring& hex) {
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
		std::stringstream octets;
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
		char octet = 0;
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
		for (size_t i = 0, limit = hex.size(); i < limit; i++) {
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
			if (i % 2 == 0) {
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
				octet = fromHex(hex[i]) << 4;
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
			} else {
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
				octet += fromHex(hex[i]);
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
				octets.put(octet);
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
			}
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
		}
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
		return octets;
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
	}
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
	static std::wstring toHex(const std::string& octets) {
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
		std::wstring_convert < codecvt_utf8<wchar_t>> convertor; // TODO: do not create converter each time
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
		std::stringstream hex;
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
		hex << std::hex << std::setfill('0') << std::hex;
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
		for (size_t i = 0, size = octets.size(); i < size; i++) hex << std::setw(2) << (0xff & octets[i]);
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
		return convertor.from_bytes(hex.str());
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
	}
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
	static std::wstring toTxt(const std::string& octets, bool* validEncoding = nullptr) {
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
		std::wstring_convert < codecvt_utf8<wchar_t>> convertor; // TODO: do not create converter each time
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
		try {
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
			if (validEncoding) *validEncoding = true;
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
			// TODO: use encoding from the HTTP response headers instead of the constant one?
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
			return convertor.from_bytes(octets);
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
		} catch (...) {
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
			if (validEncoding) *validEncoding = false;
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
			std::stringstream filtered;
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
			for (char ch : octets) filtered << (ch >= ' ' && ch < 127 ? ch : '.');
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
			return convertor.from_bytes(filtered.str());
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
		}
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
	}
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
};
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
}
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
}
fe61bf7d1716 use common hex function, send also binary content, not only text
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
}