Przeglądaj źródła

tests; hex tools

master
Robin Thoni 9 lat temu
rodzic
commit
07fb89933b

+ 1
- 1
src/CMakeLists.txt Wyświetl plik

@@ -16,5 +16,5 @@ set(SOURCE_FILES
16 16
   DataAccess/ScReader.cpp
17 17
   include/ScReader.h
18 18
   include/ScReader.hxx)
19
-add_library(pcsc_cpptools SHARED ${SOURCE_FILES})
19
+add_library(pcsc_cpptools SHARED ${SOURCE_FILES} include/ScHex.h DBO/ScHex.cpp)
20 20
 target_link_libraries(pcsc_cpptools pcsclite)

+ 2
- 15
src/DBO/ScBasicCommand.cpp Wyświetl plik

@@ -3,25 +3,12 @@
3 3
 //
4 4
 
5 5
 #include <string>
6
+#include <ScHex.h>
6 7
 #include "ScBasicCommand.h"
7 8
 
8 9
 ScBasicCommand::ScBasicCommand(const std::string &data)
9 10
 {
10
-    DWORD size = 0;
11
-    for (auto c : data)
12
-    {
13
-        if (isxdigit(c))
14
-            ++size;
15
-    }
16
-    auto d = new BYTE[size];
17
-    size = 0;
18
-    for (int i = 0; i < data.size(); ++i)
19
-    {
20
-        auto c = data[i];
21
-        if (isxdigit(c))
22
-            d[size++] = hexToDec(c);
23
-    }
24
-    _data = ScByteArray(d, size);
11
+    _data = ScHex::stringToByteArray(data);
25 12
 }
26 13
 
27 14
 ScBasicCommand::~ScBasicCommand()

+ 0
- 8
src/DBO/ScCommand.cpp Wyświetl plik

@@ -8,11 +8,3 @@ ScCommand::~ScCommand()
8 8
 {
9 9
 }
10 10
 
11
-int ScCommand::hexToDec(const char c)
12
-{
13
-    if (c >= 'a')
14
-        return 10 + (c - ('a' - 'A')) - 'A';
15
-    if (c >= 'A')
16
-        return 10 + c - 'A';
17
-    return c - '0';
18
-}

+ 59
- 0
src/DBO/ScHex.cpp Wyświetl plik

@@ -0,0 +1,59 @@
1
+//
2
+// Created by robin on 6/29/15.
3
+//
4
+
5
+#include <sstream>
6
+#include "ScHex.h"
7
+
8
+
9
+char ScHex::hexCharToInt(const char& c)
10
+{
11
+    if (c >= 'a')
12
+        return (char)(10 + (c - ('a' - 'A')) - 'A');
13
+    if (c >= 'A')
14
+        return (char)(10 + c - 'A');
15
+    return c - '0';
16
+}
17
+
18
+ScByteArray ScHex::stringToByteArray(const std::string &str)
19
+{
20
+    std::string hexa;
21
+    for (auto c : str)
22
+    {
23
+        if (isxdigit(c))
24
+            hexa += c;
25
+    }
26
+    if (hexa.size() % 2)
27
+        hexa.insert(0, "0");
28
+    auto data = new BYTE[hexa.size() / 2];
29
+    for (size_t i = 0; i < hexa.size(); i += 2)
30
+    {
31
+        data[i / 2] = (hexCharToInt(hexa[i]) << 4) | hexCharToInt(hexa[i + 1]);
32
+    }
33
+    return ScByteArray(data, hexa.size() / 2);
34
+}
35
+
36
+std::string ScHex::byteArrayToString(const ScByteArray &bytes, const std::string &separator)
37
+{
38
+    std::string res;
39
+    for (DWORD i = 0; i < bytes.getSize();++i)
40
+    {
41
+        res += intToHexChar(bytes.getData()[i]);
42
+        if (i != bytes.getSize() - 1)
43
+            res += separator;
44
+    }
45
+    return res;
46
+}
47
+
48
+std::string ScHex::intToHexChar(const char &c)
49
+{
50
+    std::string res;
51
+    for (auto c1 : {(c & 0xf0) >> 4, c & 0x0f})
52
+    {
53
+        if (c1 < 10)
54
+            res += '0' + c1;
55
+        else
56
+            res += 'A' + (c1 - 10);
57
+    }
58
+    return res;
59
+}

+ 1
- 1
src/DataAccess/ScReader.cpp Wyświetl plik

@@ -6,7 +6,7 @@
6 6
 #include <vector>
7 7
 #include "ScReader.h"
8 8
 
9
-LPSCARDCONTEXT ScReader::_context = nullptr;
9
+thread_local LPSCARDCONTEXT ScReader::_context = nullptr;
10 10
 
11 11
 ScReader::~ScReader()
12 12
 {

+ 0
- 2
src/include/ScCommand.h Wyświetl plik

@@ -14,8 +14,6 @@ public:
14 14
 
15 15
     virtual ScByteArray getData() const = 0;
16 16
 
17
-    static int hexToDec(const char c);
18
-
19 17
 };
20 18
 
21 19
 # include "ScCommand.hxx"

+ 24
- 0
src/include/ScHex.h Wyświetl plik

@@ -0,0 +1,24 @@
1
+//
2
+// Created by robin on 6/29/15.
3
+//
4
+
5
+#ifndef PCSC_CPPTOOLS_SCHEX_H
6
+# define PCSC_CPPTOOLS_SCHEX_H
7
+
8
+# include <string>
9
+#include "ScByteArray.h"
10
+
11
+class ScHex
12
+{
13
+public:
14
+    static char hexCharToInt(const char& c);
15
+
16
+    static std::string intToHexChar(const char& c);
17
+
18
+    static std::string byteArrayToString(const ScByteArray& bytes, const std::string& separator = " ");
19
+
20
+    static ScByteArray stringToByteArray(const std::string& str);
21
+
22
+};
23
+
24
+#endif //PCSC_CPPTOOLS_SCHEX_H

+ 10
- 1
tests/test_pcsc_cpp_tools.cpp Wyświetl plik

@@ -1,8 +1,17 @@
1 1
 #include <iostream>
2
-#include "ScReader.h"
2
+#include <ScHex.h>
3
+#include <ScReader.h>
3 4
 
4 5
 int main()
5 6
 {
7
+  auto bytes = new BYTE[5];
8
+  bytes[0] = 0x42;
9
+  bytes[1] = 0x00;
10
+  bytes[2] = 0xff;
11
+  bytes[3] = 0xaa;
12
+  bytes[4] = 0x24;
13
+  std::cout << "hex: " << ScHex::byteArrayToString(ScByteArray(bytes, 5)) << std::endl;
14
+
6 15
   std::cout << ScReader::establishContext() << std::endl;
7 16
   auto readers = ScReader::getReaders();
8 17
   for (auto reader : readers)

Ładowanie…
Anuluj
Zapisz