summaryrefslogtreecommitdiffstats
path: root/src/lib/base/String.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/base/String.cpp')
-rw-r--r--src/lib/base/String.cpp78
1 files changed, 70 insertions, 8 deletions
diff --git a/src/lib/base/String.cpp b/src/lib/base/String.cpp
index 1ab3623..adbb11d 100644
--- a/src/lib/base/String.cpp
+++ b/src/lib/base/String.cpp
@@ -1,11 +1,11 @@
/*
* barrier -- mouse and keyboard sharing utility
* Copyright (C) 2014-2016 Symless Ltd.
- *
+ *
* This package is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* found in the file LICENSE that should have accompanied this file.
- *
+ *
* This package is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@@ -35,6 +35,42 @@
namespace barrier {
namespace string {
+namespace {
+
+// returns negative in case of non-matching character
+int hex_to_number(char ch)
+{
+ switch (ch) {
+ case '0': return 0;
+ case '1': return 1;
+ case '2': return 2;
+ case '3': return 3;
+ case '4': return 4;
+ case '5': return 5;
+ case '6': return 6;
+ case '7': return 7;
+ case '8': return 8;
+ case '9': return 9;
+
+ case 'a': return 10;
+ case 'b': return 11;
+ case 'c': return 12;
+ case 'd': return 13;
+ case 'e': return 14;
+ case 'f': return 15;
+
+ case 'A': return 10;
+ case 'B': return 11;
+ case 'C': return 12;
+ case 'D': return 13;
+ case 'E': return 14;
+ case 'F': return 15;
+ }
+ return -1;
+}
+
+} // namespace
+
std::string
format(const char* fmt, ...)
{
@@ -135,7 +171,7 @@ sprintf(const char* fmt, ...)
// try printing into the buffer
va_list args;
va_start(args, fmt);
- int n = ARCH->vsnprintf(buffer, len, fmt, args);
+ int n = std::vsnprintf(buffer, len, fmt, args);
va_end(args);
// if the buffer wasn't big enough then make it bigger and try again
@@ -185,16 +221,42 @@ removeFileExt(std::string filename)
return filename.substr(0, dot);
}
-void
-toHex(std::string& subject, int width, const char fill)
+std::string to_hex(const std::vector<std::uint8_t>& subject, int width, const char fill)
{
std::stringstream ss;
ss << std::hex;
- for (unsigned int i = 0; i < subject.length(); i++) {
- ss << std::setw(width) << std::setfill(fill) << (int)(unsigned char)subject[i];
+ for (unsigned int i = 0; i < subject.size(); i++) {
+ ss << std::setw(width) << std::setfill(fill) << static_cast<int>(subject[i]);
}
- subject = ss.str();
+ return ss.str();
+}
+
+std::vector<std::uint8_t> from_hex(const std::string& data)
+{
+ std::vector<std::uint8_t> result;
+ result.reserve(data.size() / 2);
+
+ std::size_t i = 0;
+ while (i < data.size()) {
+ if (data[i] == ':') {
+ i++;
+ continue;
+ }
+
+ if (i + 2 > data.size()) {
+ return {}; // uneven character count follows, it's unclear how to interpret it
+ }
+
+ auto high = hex_to_number(data[i]);
+ auto low = hex_to_number(data[i + 1]);
+ if (high < 0 || low < 0) {
+ return {};
+ }
+ result.push_back(high * 16 + low);
+ i += 2;
+ }
+ return result;
}
void