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