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