-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinIO.cpp
More file actions
107 lines (85 loc) · 2.78 KB
/
Copy pathbinIO.cpp
File metadata and controls
107 lines (85 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "binIO.h"
#include <string>
#include <vector>
#include <iostream>
#include <charconv>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdint>
size_t BinIO::readHexBinary(std::vector<uint8_t>& binOut, const char* is)
{
// builds a string outta the input char array from stdin:
std::string argString {is};
// consistency checks on the input: the string shall be non-empty and
// made up of an even number of characters:
size_t inputLen = argString.length();
if (!inputLen || (inputLen % 2))
{
std::cerr << "BinIO::readHexBinary: Invalid Hex string in input - "
"please check that input is correctly populated and the "
"number of characters is even!\n"
<< argString
<< std::endl;
return 0;
}
for (size_t i = 0; i < argString.length(); i += 2)
{
size_t digitNumberOfChars = 2;
std::string argHexDigit {argString.substr(i, 2)};
int curByte;
try {
curByte = stoi(argHexDigit, &digitNumberOfChars, 16);
} catch (...) {
std::cerr << "BinIO::readHexBinary: invalid HEX characters in input"
"stream!\n"
<< argString
<< ".\n";
binOut.clear();
return 0;
}
binOut.push_back(curByte);
}
return binOut.size();
}
size_t BinIO::hexBinaryToString(std::string& outStr,
std::vector<uint8_t> inHex)
{
// initializes the output to the empty string, in case the caller didn't:
outStr = "";
if (!inHex.size())
return 0;
for (size_t i = 0; i < inHex.size(); ++i)
{
int offset = 0;
std::string tmp {"00"};
// if the number is smaller than 16, we need to add a leading 0 to keep
// a byte representation:
// TODO: we might make this a parameter and choose whether to also offer
// word alignment!
if (inHex[i] < 0x10u)
{
++offset;
}
auto rc = std::to_chars(tmp.data() + offset,
tmp.data() + tmp.length(),
inHex[i],
16);
if (std::errc::value_too_large == rc.ec)
{
throw InputError_IllegalConversion {};
}
outStr += tmp;
}
std::transform(outStr.begin(), outStr.end(),
outStr.begin(),
[](char c){return std::toupper(c);});
return outStr.length();
}
int BinIO::printHexBinary(const std::vector<uint8_t>& binIn)
{
std::string stringInput;
BinIO::hexBinaryToString(stringInput, binIn);
std::cout << stringInput << std::endl;
return 0;
}