Refactor RawBuffer hex dumps 17/214617/2
authorKonrad Lipinski <k.lipinski2@samsung.com>
Tue, 24 Sep 2019 14:36:36 +0000 (16:36 +0200)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Wed, 25 Sep 2019 09:12:07 +0000 (09:12 +0000)
Change-Id: I2d52c63c908e3a69c8de5f20e275fecda0165a66

src/include/ckm/ckm-raw-buffer.h
src/manager/crypto/tz-backend/tz-context.cpp
src/manager/dpl/db/src/sql_connection.cpp
tests/test_common.cpp

index d9b41a7..c4c2a06 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2014 Samsung Electronics Co.
+/* Copyright (c) 2014-2019 Samsung Electronics Co., Ltd. All rights reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -113,6 +113,24 @@ struct SafeBuffer {
 // used to pass password and raw key data
 typedef SafeBuffer<unsigned char>::Type RawBuffer;
 
+template <class T, bool Uppercase = false>
+T hexDump(const RawBuffer &raw) {
+       T dump;
+       dump.reserve(2 * raw.size());
+       constexpr char digit[2][16] = {{
+               '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
+               'a', 'b', 'c', 'd', 'e', 'f'
+       }, {
+               '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
+               'A', 'B', 'C', 'D', 'E', 'F'
+       }};
+       for (auto &e : raw) {
+               dump.push_back(digit[Uppercase][e / 16]);
+               dump.push_back(digit[Uppercase][e % 16]);
+       }
+       return dump;
+}
+
 } // namespace CKM
 
 #endif //_SAFE_BUFFER_H_
index 9dcd13f..df421f7 100644 (file)
@@ -32,8 +32,6 @@
 #include <cstdint>
 #include <cstring>
 #include <cassert>
-#include <iomanip>
-#include <sstream>
 #include <unordered_map>
 
 namespace CKM {
@@ -56,16 +54,7 @@ const TEEC_UUID KEY_MANAGER_TA_UUID = KM_TA_UUID;
 //raw to hex string conversion to print persistent storage data ID
 static std::string rawToHexString(const RawBuffer &raw)
 {
-        std::string dump;
-
-        for (auto &e : raw) {
-                char buf[3];
-                snprintf(buf, sizeof(buf), "%02x", (e & 0xff));
-                dump.push_back(buf[0]);
-                dump.push_back(buf[1]);
-        }
-
-        return dump;
+       return hexDump<std::string>(raw);
 }
 
 /*
index 902c94d..15e3cc8 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2014-2019 Samsung Electronics Co., Ltd. All rights reserved
  *
  *    Licensed under the Apache License, Version 2.0 (the "License");
  *    you may not use this file except in compliance with the License.
@@ -678,16 +678,7 @@ const std::size_t SQLCIPHER_RAW_DATA_SIZE = 32;
 
 RawBuffer rawToHexString(const RawBuffer &raw)
 {
-       RawBuffer output;
-
-       for (auto &e : raw) {
-               char result[3];
-               snprintf(result, sizeof(result), "%02X", (e & 0xff));
-               output.push_back(static_cast<unsigned char>(result[0]));
-               output.push_back(static_cast<unsigned char>(result[1]));
-       }
-
-       return output;
+       return hexDump<RawBuffer, true>(raw);
 }
 
 RawBuffer createHexPass(const RawBuffer &rawPass)
index d01b92a..ab51cea 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2016-2019 Samsung Electronics Co., Ltd. All rights reserved
  *
  *  Contact: Kyungwook Tak <k.tak@samsung.com>
  *
@@ -21,7 +21,6 @@
  * @brief
  */
 #include <test_common.h>
-#include <iostream>
 #include <cstdlib>
 #include <time.h>
 
@@ -61,14 +60,5 @@ RawBuffer createRandom(std::size_t size)
 //raw to hex string conversion from SqlConnection
 std::string rawToHexString(const RawBuffer &raw)
 {
-       std::string dump;
-
-       for (auto &e : raw) {
-               char buf[3];
-               snprintf(buf, sizeof(buf), "%02x", (e & 0xff));
-               dump.push_back(buf[0]);
-               dump.push_back(buf[1]);
-       }
-
-       return dump;
+       return hexDump<std::string>(raw);
 }