Encode blobs with Base64 in ckm tools 17/199917/4
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Fri, 15 Feb 2019 12:40:39 +0000 (13:40 +0100)
committerTomasz Swierczek <t.swierczek@samsung.com>
Tue, 19 Feb 2019 16:15:25 +0000 (17:15 +0100)
If an unprintable data is returned as a result of sqlite query
execution it will be encoded in base64 and prefixed with "b64:". This
is to make binary data readable in ckm_db_tool/merge.

Also extend the maximum size of printed row to 64 characters.

Change-Id: I4471090977b19ded9b1bea76f26ff1b98d5ba826

tools/ckm_db_tool/db-wrapper.cpp

index e7027db..00a4756 100644 (file)
  * @version    1.0
  */
 #include <string>
+#include <algorithm>
+
+#include <base64.h>
 
 #include <db-wrapper.h>
 
 namespace {
 
-const size_t MAX_LEN = 32;
+const size_t MAX_LEN = 64;
 const char ELLIPSIS[] = "...";
 const size_t ELLIPSIS_LEN = sizeof(ELLIPSIS) / sizeof(ELLIPSIS[0]);
 
@@ -111,6 +114,22 @@ void DbWrapper::displayRow(const DB::SqlConnection::Output::Row &row, bool trim)
        for (auto it = row.begin(); it != row.end(); it++) {
                std::string col = *it;
 
+               /*
+                * Convert unprintable data to base64. Note that a row may contain an incomplete data
+                * since it holds a binary data as a string.
+                */
+               if (std::any_of(col.begin(),
+                               col.end(),
+                               [](const char c){ return isprint(c) + iscntrl(c) == 0; })) {
+                       Base64Encoder b64enc;
+                       RawBuffer tmp(col.begin(), col.end());
+                       b64enc.append(tmp);
+                       b64enc.finalize();
+                       col = "b64:";
+                       RawBuffer encoded = b64enc.get();
+                       std::copy(encoded.begin(), encoded.end(), std::back_inserter(col));
+               }
+
                if (trim && col.size() > MAX_LEN) {
                        col.resize(MAX_LEN);
                        col.replace(MAX_LEN - ELLIPSIS_LEN, ELLIPSIS_LEN, ELLIPSIS);