mirror of
https://github.com/qpdf/qpdf.git
synced 2025-01-22 22:58:33 +00:00
Add QUtil::hex_decode
This commit is contained in:
parent
68572df2bf
commit
3e306ae64c
@ -104,6 +104,14 @@ namespace QUtil
|
||||
QPDF_DLL
|
||||
std::string hex_encode(std::string const&);
|
||||
|
||||
// Returns a string that is the result of decoding the input
|
||||
// string. The input string may consist of mixed case hexadecimal
|
||||
// digits. Any characters that are not hexadecimal digits will be
|
||||
// silently ignored. If there are an odd number of hexadecimal
|
||||
// digits, a trailing 0 will be assumed.
|
||||
QPDF_DLL
|
||||
std::string hex_decode(std::string const&);
|
||||
|
||||
// Set stdin, stdout to binary mode
|
||||
QPDF_DLL
|
||||
void binary_stdout();
|
||||
|
@ -290,6 +290,50 @@ QUtil::hex_encode(std::string const& input)
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string
|
||||
QUtil::hex_decode(std::string const& input)
|
||||
{
|
||||
std::string result;
|
||||
size_t pos = 0;
|
||||
for (std::string::const_iterator p = input.begin(); p != input.end(); ++p)
|
||||
{
|
||||
char ch = *p;
|
||||
bool skip = false;
|
||||
if ((*p >= 'A') && (*p <= 'F'))
|
||||
{
|
||||
ch -= 'A';
|
||||
ch += 10;
|
||||
}
|
||||
else if ((*p >= 'a') && (*p <= 'f'))
|
||||
{
|
||||
ch -= 'a';
|
||||
ch += 10;
|
||||
}
|
||||
else if ((*p >= '0') && (*p <= '9'))
|
||||
{
|
||||
ch -= '0';
|
||||
}
|
||||
else
|
||||
{
|
||||
skip = true;
|
||||
}
|
||||
if (! skip)
|
||||
{
|
||||
if (pos == 0)
|
||||
{
|
||||
result.push_back(ch << 4);
|
||||
pos = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.back() += ch;
|
||||
pos = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void
|
||||
QUtil::binary_stdout()
|
||||
{
|
||||
|
File diff suppressed because one or more lines are too long
@ -247,6 +247,44 @@ void read_lines_from_file_test()
|
||||
}
|
||||
}
|
||||
|
||||
void assert_hex_encode(std::string const& input, std::string const& expected)
|
||||
{
|
||||
std::string actual = QUtil::hex_encode(input);
|
||||
if (expected != actual)
|
||||
{
|
||||
std::cout << "hex encode " << input
|
||||
<< ": expected = " << expected
|
||||
<< "; actual = " << actual
|
||||
<< std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void assert_hex_decode(std::string const& input, std::string const& expected)
|
||||
{
|
||||
std::string actual = QUtil::hex_decode(input);
|
||||
if (expected != actual)
|
||||
{
|
||||
std::cout << "hex encode " << input
|
||||
<< ": expected = " << expected
|
||||
<< "; actual = " << actual
|
||||
<< std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void hex_encode_decode_test()
|
||||
{
|
||||
std::cout << "begin hex encode/decode\n";
|
||||
assert_hex_encode("", "");
|
||||
assert_hex_encode("Potato", "506f7461746f");
|
||||
std::string with_null("a\367" "00w");
|
||||
with_null[3] = '\0';
|
||||
assert_hex_encode(with_null, "61f7300077");
|
||||
assert_hex_decode("", "");
|
||||
assert_hex_decode("61F7-3000-77", with_null);
|
||||
assert_hex_decode("41455", "AEP");
|
||||
std::cout << "end hex encode/decode\n";
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
try
|
||||
@ -266,6 +304,8 @@ int main(int argc, char* argv[])
|
||||
same_file_test();
|
||||
std::cout << "----" << std::endl;
|
||||
read_lines_from_file_test();
|
||||
std::cout << "----" << std::endl;
|
||||
hex_encode_decode_test();
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user