Browse Source

C++ improvements

develop
Robin Thoni 7 years ago
parent
commit
54bce7eecb

+ 1
- 1
cli/cli/MainClass.cpp View File

372
 void MainClass::printVersion()
372
 void MainClass::printVersion()
373
 {
373
 {
374
     cout() << "LibNfc version: " << LibNfc::Core::LibNfcContext::getLibNfcVersion() << std::endl;
374
     cout() << "LibNfc version: " << LibNfc::Core::LibNfcContext::getLibNfcVersion() << std::endl;
375
-    cout() << "Mifare-tools version: " << LibNfc::Core::LibNfcContext::getMifareToolsVersion() << std::endl;
375
+    cout() << "Mifare-tools version: " << LibNfc::Core::LibNfcContext::getLibNfcCppToolsVersion() << std::endl;
376
 }
376
 }
377
 
377
 
378
 std::shared_ptr<LibNfc::Core::NfcDevice> MainClass::getDevice(const std::string &deviceName, std::vector<std::shared_ptr<LibNfc::Core::NfcDevice>> devices)
378
 std::shared_ptr<LibNfc::Core::NfcDevice> MainClass::getDevice(const std::string &deviceName, std::vector<std::shared_ptr<LibNfc::Core::NfcDevice>> devices)

+ 3
- 3
libnfc_cpptools/inc/libnfc_cpptools/ArrayUtils.h View File

2
 // Created by robin on 7/23/16.
2
 // Created by robin on 7/23/16.
3
 //
3
 //
4
 
4
 
5
-#ifndef MIFARE_TOOLS_ARRAYUTILS_H
6
-#define MIFARE_TOOLS_ARRAYUTILS_H
5
+#ifndef LIBNFC_CPPTOOLS_ARRAYUTILS_H
6
+#define LIBNFC_CPPTOOLS_ARRAYUTILS_H
7
 
7
 
8
 
8
 
9
 namespace LibNfc
9
 namespace LibNfc
24
 }; // Utils
24
 }; // Utils
25
 }; // LibNfc
25
 }; // LibNfc
26
 
26
 
27
-#endif //MIFARE_TOOLS_ARRAYUTILS_H
27
+#endif // LIBNFC_CPPTOOLS_ARRAYUTILS_H

+ 5
- 4
libnfc_cpptools/inc/libnfc_cpptools/FreeFareAccessBits.h View File

2
 // Created by robin on 7/21/16.
2
 // Created by robin on 7/21/16.
3
 //
3
 //
4
 
4
 
5
-#ifndef MIFARE_TOOLS_ACCESSBITSDBO_H
6
-#define MIFARE_TOOLS_ACCESSBITSDBO_H
5
+#ifndef LIBNFC_CPPTOOLS_ACCESSBITSDBO_H
6
+#define LIBNFC_CPPTOOLS_ACCESSBITSDBO_H
7
 
7
 
8
 
8
 
9
 #include <string>
9
 #include <string>
21
     static const char invertedBitPosition[4][4];
21
     static const char invertedBitPosition[4][4];
22
 
22
 
23
     FreeFareAccessBits();
23
     FreeFareAccessBits();
24
-    FreeFareAccessBits(const std::string& bits);
24
+    explicit FreeFareAccessBits(const std::string& bits);
25
+    ~FreeFareAccessBits() = default;
25
 
26
 
26
     char getUserData() const;
27
     char getUserData() const;
27
     void setUserData(const char& data);
28
     void setUserData(const char& data);
67
 }; // FreeFare
68
 }; // FreeFare
68
 }; // LibNfc
69
 }; // LibNfc
69
 
70
 
70
-#endif //MIFARE_TOOLS_ACCESSBITSDBO_H
71
+#endif //LIBNFC_CPPTOOLS_ACCESSBITSDBO_H

+ 5
- 4
libnfc_cpptools/inc/libnfc_cpptools/FreeFareDevice.h View File

2
 // Created by robin on 7/22/16.
2
 // Created by robin on 7/22/16.
3
 //
3
 //
4
 
4
 
5
-#ifndef MIFARE_TOOLS_FREEFAREDEVICEBUSINESS_H
6
-#define MIFARE_TOOLS_FREEFAREDEVICEBUSINESS_H
5
+#ifndef LIBNFC_CPPTOOLS_FREEFAREDEVICEBUSINESS_H
6
+#define LIBNFC_CPPTOOLS_FREEFAREDEVICEBUSINESS_H
7
 
7
 
8
 #include "NfcDevice.h"
8
 #include "NfcDevice.h"
9
 #include "FreeFareTag.h"
9
 #include "FreeFareTag.h"
18
 class FreeFareDevice
18
 class FreeFareDevice
19
 {
19
 {
20
 public:
20
 public:
21
-    FreeFareDevice(std::shared_ptr<LibNfc::Core::NfcDevice> device);
21
+    explicit FreeFareDevice(std::shared_ptr<LibNfc::Core::NfcDevice> device);
22
+    ~FreeFareDevice() = default;
22
 
23
 
23
     LibNfc::Utils::Result<std::vector<std::shared_ptr<FreeFareTag>>> getTags() const;
24
     LibNfc::Utils::Result<std::vector<std::shared_ptr<FreeFareTag>>> getTags() const;
24
 
25
 
31
 }; // FreeFare
32
 }; // FreeFare
32
 }; // LibNfc
33
 }; // LibNfc
33
 
34
 
34
-#endif //MIFARE_TOOLS_FREEFAREDEVICEBUSINESS_H
35
+#endif //LIBNFC_CPPTOOLS_FREEFAREDEVICEBUSINESS_H

+ 5
- 4
libnfc_cpptools/inc/libnfc_cpptools/FreeFareSector.h View File

2
 // Created by robin on 7/22/16.
2
 // Created by robin on 7/22/16.
3
 //
3
 //
4
 
4
 
5
-#ifndef MIFARE_TOOLS_SECTORDBO_H
6
-#define MIFARE_TOOLS_SECTORDBO_H
5
+#ifndef LIBNFC_CPPTOOLS_SECTORDBO_H
6
+#define LIBNFC_CPPTOOLS_SECTORDBO_H
7
 
7
 
8
 
8
 
9
 #include <string>
9
 #include <string>
18
 class FreeFareSector
18
 class FreeFareSector
19
 {
19
 {
20
 public:
20
 public:
21
-    FreeFareSector(const std::string& data = "");
21
+    explicit FreeFareSector(const std::string& data = "");
22
+    ~FreeFareSector() = default;
22
 
23
 
23
     const std::string& getBlock(int block) const;
24
     const std::string& getBlock(int block) const;
24
 
25
 
63
 }; // FreeFare
64
 }; // FreeFare
64
 }; // LibNfc
65
 }; // LibNfc
65
 
66
 
66
-#endif //MIFARE_TOOLS_SECTORDBO_H
67
+#endif //LIBNFC_CPPTOOLS_SECTORDBO_H

+ 17
- 16
libnfc_cpptools/inc/libnfc_cpptools/FreeFareTag.h View File

2
 // Created by robin on 7/22/16.
2
 // Created by robin on 7/22/16.
3
 //
3
 //
4
 
4
 
5
-#ifndef MIFARE_TOOLS_FREEFARETAGBUSINESS_H
6
-#define MIFARE_TOOLS_FREEFARETAGBUSINESS_H
5
+#ifndef LIBNFC_CPPTOOLS_FREEFARETAGBUSINESS_H
6
+#define LIBNFC_CPPTOOLS_FREEFARETAGBUSINESS_H
7
 
7
 
8
 
8
 
9
 #include <boost/shared_ptr.hpp>
9
 #include <boost/shared_ptr.hpp>
34
 
34
 
35
     static std::string getTagTypeString(FreeFareTagType type);
35
     static std::string getTagTypeString(FreeFareTagType type);
36
 
36
 
37
-    FreeFareTag(std::shared_ptr<FreeFareTagInternal> tag);
37
+    explicit FreeFareTag(std::shared_ptr<FreeFareTagInternal> tag);
38
+    ~FreeFareTag() = default;
38
 
39
 
39
-    LibNfc::Utils::ResultBool authenticate(int sector, std::string key, int keyType);
40
+    LibNfc::Utils::ResultBool authenticate(int sector, const std::string& key, int keyType);
40
 
41
 
41
-    LibNfc::Utils::Result<MappedKeys> mapKeys(std::vector<std::string> keys, std::function<void(int, int)> cb = 0);
42
+    LibNfc::Utils::Result<MappedKeys> mapKeys(const std::vector<std::string>& keys, std::function<void(int, int)> cb = nullptr);
42
 
43
 
43
-    LibNfc::Utils::ResultString readBlock(int sector, int block, std::string key, int keyType);
44
+    LibNfc::Utils::ResultString readBlock(int sector, int block, const std::string& key, int keyType);
44
 
45
 
45
-    LibNfc::Utils::Result<FreeFareSector> readSector(int sector, std::string key, int keyType);
46
+    LibNfc::Utils::Result<FreeFareSector> readSector(int sector, const std::string& key, int keyType);
46
 
47
 
47
-    LibNfc::Utils::Result<std::vector<FreeFareSector>> read(MappedKeys keys, std::function<void(int, int)> cb = 0);
48
+    LibNfc::Utils::Result<std::vector<FreeFareSector>> read(const MappedKeys& keys, std::function<void(int, int)> cb = nullptr);
48
 
49
 
49
-    LibNfc::Utils::Result<std::vector<FreeFareSector>> read(std::vector<std::string> keys, std::function<void(int, int)> mapCb = 0,
50
-                                        std::function<void(int, int)> readCb = 0);
50
+    LibNfc::Utils::Result<std::vector<FreeFareSector>> read(const std::vector<std::string>& keys, std::function<void(int, int)> mapCb = nullptr,
51
+                                        std::function<void(int, int)> readCb = nullptr);
51
 
52
 
52
-    LibNfc::Utils::ResultBool writeBlock(int sector, int block, std::string key, int keyType, const std::string& data);
53
+    LibNfc::Utils::ResultBool writeBlock(int sector, int block, const std::string& key, int keyType, const std::string& data);
53
 
54
 
54
-    LibNfc::Utils::ResultBool writeSector(int sector, std::string key, int keyType, const std::string& data);
55
+    LibNfc::Utils::ResultBool writeSector(int sector, const std::string& key, int keyType, const std::string& data);
55
 
56
 
56
-    LibNfc::Utils::ResultBool write(MappedKeys keys, const std::string& data, bool writeSector0, std::function<void(int, int)> cb = 0);
57
+    LibNfc::Utils::ResultBool write(const MappedKeys& keys, const std::string& data, bool writeSector0, std::function<void(int, int)> cb = nullptr);
57
 
58
 
58
-    LibNfc::Utils::ResultBool write(std::vector<std::string> keys, const std::string& data, bool writeSector0,
59
-                     std::function<void(int, int)> mapCb = 0, std::function<void(int, int)> writeCb = 0);
59
+    LibNfc::Utils::ResultBool write(const std::vector<std::string>& keys, const std::string& data, bool writeSector0,
60
+                     std::function<void(int, int)> mapCb = nullptr, std::function<void(int, int)> writeCb = nullptr);
60
 
61
 
61
     const std::string& getUid() const;
62
     const std::string& getUid() const;
62
 
63
 
71
 }; // FreeFare
72
 }; // FreeFare
72
 }; // LibNfc
73
 }; // LibNfc
73
 
74
 
74
-#endif //MIFARE_TOOLS_FREEFARETAGBUSINESS_H
75
+#endif //LIBNFC_CPPTOOLS_FREEFARETAGBUSINESS_H

+ 4
- 4
libnfc_cpptools/inc/libnfc_cpptools/LibNfc.h View File

2
 // Created by robin on 7/22/16.
2
 // Created by robin on 7/22/16.
3
 //
3
 //
4
 
4
 
5
-#ifndef MIFARE_TOOLS_LIBNFCBUSINESS_H
6
-#define MIFARE_TOOLS_LIBNFCBUSINESS_H
5
+#ifndef LIBNFC_CPPTOOLS_LIBNFCBUSINESS_H
6
+#define LIBNFC_CPPTOOLS_LIBNFCBUSINESS_H
7
 
7
 
8
 
8
 
9
 #include <libnfc_cpptools/Result.h>
9
 #include <libnfc_cpptools/Result.h>
32
 
32
 
33
     static std::string getLibNfcVersion();
33
     static std::string getLibNfcVersion();
34
 
34
 
35
-    static std::string getMifareToolsVersion();
35
+    static std::string getLibNfcCppToolsVersion();
36
 
36
 
37
 protected:
37
 protected:
38
     std::shared_ptr<LibNfcInternal> _libNfc;
38
     std::shared_ptr<LibNfcInternal> _libNfc;
41
 }; // Core
41
 }; // Core
42
 }; // LibNfc
42
 }; // LibNfc
43
 
43
 
44
-#endif //MIFARE_TOOLS_LIBNFCBUSINESS_H
44
+#endif //LIBNFC_CPPTOOLS_LIBNFCBUSINESS_H

+ 5
- 4
libnfc_cpptools/inc/libnfc_cpptools/NfcDevice.h View File

2
 // Created by robin on 7/22/16.
2
 // Created by robin on 7/22/16.
3
 //
3
 //
4
 
4
 
5
-#ifndef MIFARE_TOOLS_NFCDEVICEBUSINESS_H
6
-#define MIFARE_TOOLS_NFCDEVICEBUSINESS_H
5
+#ifndef LIBNFC_CPPTOOLS_NFCDEVICEBUSINESS_H
6
+#define LIBNFC_CPPTOOLS_NFCDEVICEBUSINESS_H
7
 
7
 
8
 #include <string>
8
 #include <string>
9
 #include <memory>
9
 #include <memory>
20
 class NfcDevice
20
 class NfcDevice
21
 {
21
 {
22
 public:
22
 public:
23
-    NfcDevice(std::shared_ptr<NfcDeviceInternal> device);
23
+    explicit NfcDevice(std::shared_ptr<NfcDeviceInternal> device);
24
+    ~NfcDevice() = default;
24
 
25
 
25
     LibNfc::Utils::ResultBool open();
26
     LibNfc::Utils::ResultBool open();
26
 
27
 
39
 }; // Core
40
 }; // Core
40
 }; // LibNfc
41
 }; // LibNfc
41
 
42
 
42
-#endif //MIFARE_TOOLS_NFCDEVICEBUSINESS_H
43
+#endif //LIBNFC_CPPTOOLS_NFCDEVICEBUSINESS_H

+ 5
- 4
libnfc_cpptools/inc/libnfc_cpptools/Result.h View File

8
 #include <string>
8
 #include <string>
9
 #include <vector>
9
 #include <vector>
10
 #include <iostream>
10
 #include <iostream>
11
-#include <errno.h>
12
-#include <string.h>
11
+#include <cerrno>
12
+#include <cstring>
13
 
13
 
14
 namespace LibNfc
14
 namespace LibNfc
15
 {
15
 {
19
 template <class T> class Result
19
 template <class T> class Result
20
 {
20
 {
21
 public:
21
 public:
22
+    Result();
23
+
22
     static const Result<T> ok(const T& data);
24
     static const Result<T> ok(const T& data);
23
     static const Result<T> strerror();
25
     static const Result<T> strerror();
24
     static const Result<T> error(const std::string& error);
26
     static const Result<T> error(const std::string& error);
37
     const Result<T>& print() const;
39
     const Result<T>& print() const;
38
 
40
 
39
 private:
41
 private:
40
-    Result();
41
-
42
     T _data;
42
     T _data;
43
 
43
 
44
     std::string _error;
44
     std::string _error;
63
 
63
 
64
 template<class T>
64
 template<class T>
65
 Result<T>::Result()
65
 Result<T>::Result()
66
+    : _success(false)
66
 {
67
 {
67
 }
68
 }
68
 
69
 

+ 3
- 3
libnfc_cpptools/inc/libnfc_cpptools/StringUtils.h View File

2
 // Created by robin on 6/20/16.
2
 // Created by robin on 6/20/16.
3
 //
3
 //
4
 
4
 
5
-#ifndef MIFARE_TOOLS_HEX_H
6
-#define MIFARE_TOOLS_HEX_H
5
+#ifndef LIBNFC_CPPTOOLS_HEX_H
6
+#define LIBNFC_CPPTOOLS_HEX_H
7
 
7
 
8
 
8
 
9
 #include <string>
9
 #include <string>
48
 }; // Utils
48
 }; // Utils
49
 }; // LibNfc
49
 }; // LibNfc
50
 
50
 
51
-#endif //MIFARE_TOOLS_HEX_H
51
+#endif //LIBNFC_CPPTOOLS_HEX_H

+ 0
- 2
libnfc_cpptools/src/freefare/FreeFareAccessBits.cpp View File

145
 {
145
 {
146
     bool c1 = getBit(1, 3);
146
     bool c1 = getBit(1, 3);
147
     bool c2 = getBit(2, 3);
147
     bool c2 = getBit(2, 3);
148
-    bool c3 = getBit(3, 3);
149
     return !c1 && !c2;
148
     return !c1 && !c2;
150
 }
149
 }
151
 
150
 
203
 {
202
 {
204
     bool c1 = getBit(1, 3);
203
     bool c1 = getBit(1, 3);
205
     bool c2 = getBit(2, 3);
204
     bool c2 = getBit(2, 3);
206
-    bool c3 = getBit(3, 3);
207
     return !c1 && !c2;
205
     return !c1 && !c2;
208
 }
206
 }
209
 
207
 

+ 5
- 4
libnfc_cpptools/src/freefare/FreeFareDeviceInternal.h View File

2
 // Created by robin on 6/19/16.
2
 // Created by robin on 6/19/16.
3
 //
3
 //
4
 
4
 
5
-#ifndef MIFARE_TOOLS_FREEFAREDEVICE_H
6
-#define MIFARE_TOOLS_FREEFAREDEVICE_H
5
+#ifndef LIBNFC_CPPTOOLS_FREEFAREDEVICE_H
6
+#define LIBNFC_CPPTOOLS_FREEFAREDEVICE_H
7
 
7
 
8
 
8
 
9
 #include <freefare.h>
9
 #include <freefare.h>
18
 class FreeFareDeviceInternal
18
 class FreeFareDeviceInternal
19
 {
19
 {
20
 public:
20
 public:
21
-    FreeFareDeviceInternal(std::shared_ptr<LibNfc::Core::NfcDeviceInternal> device);
21
+    explicit FreeFareDeviceInternal(std::shared_ptr<LibNfc::Core::NfcDeviceInternal> device);
22
+    ~FreeFareDeviceInternal() = default;
22
 
23
 
23
     LibNfc::Utils::Result<std::vector<std::shared_ptr<FreeFareTagInternal>>> getTags();
24
     LibNfc::Utils::Result<std::vector<std::shared_ptr<FreeFareTagInternal>>> getTags();
24
 
25
 
29
 }; // FreeFare
30
 }; // FreeFare
30
 }; // LibNfc
31
 }; // LibNfc
31
 
32
 
32
-#endif //MIFARE_TOOLS_FREEFAREDEVICE_H
33
+#endif //LIBNFC_CPPTOOLS_FREEFAREDEVICE_H

+ 1
- 1
libnfc_cpptools/src/freefare/FreeFareSector.cpp View File

13
 FreeFareSector::FreeFareSector(const std::string &data)
13
 FreeFareSector::FreeFareSector(const std::string &data)
14
 {
14
 {
15
     std::string d = LibNfc::Utils::StringUtils::ensureSize(data, 64);
15
     std::string d = LibNfc::Utils::StringUtils::ensureSize(data, 64);
16
-    for (int i = 0; i < 4; ++i) {
16
+    for (unsigned int i = 0; i < 4; ++i) {
17
         _blocks[i] = d.substr(16 * i, 16);
17
         _blocks[i] = d.substr(16 * i, 16);
18
         _haveBlocks[i] = true;
18
         _haveBlocks[i] = true;
19
     }
19
     }

+ 28
- 27
libnfc_cpptools/src/freefare/FreeFareTag.cpp View File

49
 {
49
 {
50
 }
50
 }
51
 
51
 
52
-LibNfc::Utils::ResultBool FreeFareTag::authenticate(int sector, std::string key, int keyType)
52
+LibNfc::Utils::ResultBool FreeFareTag::authenticate(int sector, const std::string& key, int keyType)
53
 {
53
 {
54
     return _tag->authenticate(sector, key, keyType);
54
     return _tag->authenticate(sector, key, keyType);
55
 }
55
 }
56
 
56
 
57
-LibNfc::Utils::Result<MappedKeys> FreeFareTag::mapKeys(std::vector<std::string> keys, std::function<void(int, int)> cb)
57
+LibNfc::Utils::Result<MappedKeys> FreeFareTag::mapKeys(const std::vector<std::string>& keys, std::function<void(int, int)> cb)
58
 {
58
 {
59
     MappedKeys mappedKeys;
59
     MappedKeys mappedKeys;
60
-    int done = 0;
61
-    int total = 16 * keys.size();
60
+    unsigned long done = 0;
61
+    unsigned long total = 16 * keys.size();
62
 
62
 
63
     for (int i = 0; i < 16; ++i) {
63
     for (int i = 0; i < 16; ++i) {
64
         std::pair<std::string, std::string> blockKeys;
64
         std::pair<std::string, std::string> blockKeys;
65
-        for (int k = 0; k < keys.size(); ++k) {
66
-            auto key = keys[k];
65
+        for (const auto& key : keys)
66
+        {
67
             if (authenticate(i, key, MFC_KEY_A)) {
67
             if (authenticate(i, key, MFC_KEY_A)) {
68
                 blockKeys.first = key;
68
                 blockKeys.first = key;
69
             }
69
             }
70
             if (authenticate(i, key, MFC_KEY_B)) {
70
             if (authenticate(i, key, MFC_KEY_B)) {
71
                 blockKeys.second = key;
71
                 blockKeys.second = key;
72
             }
72
             }
73
-            if (cb != 0) {
73
+            if (cb != nullptr) {
74
                 cb(++done, total);
74
                 cb(++done, total);
75
             }
75
             }
76
             if (!blockKeys.first.empty() && !blockKeys.second.empty()) {
76
             if (!blockKeys.first.empty() && !blockKeys.second.empty()) {
79
         }
79
         }
80
         mappedKeys.push_back(blockKeys);
80
         mappedKeys.push_back(blockKeys);
81
     }
81
     }
82
-    if (cb != 0 && done < total) {
82
+    if (cb != nullptr && done < total) {
83
         cb(total, total);
83
         cb(total, total);
84
     }
84
     }
85
 
85
 
86
     return LibNfc::Utils::Result<MappedKeys>::ok(mappedKeys);
86
     return LibNfc::Utils::Result<MappedKeys>::ok(mappedKeys);
87
 }
87
 }
88
 
88
 
89
-LibNfc::Utils::ResultString FreeFareTag::readBlock(int sector, int block, std::string key, int keyType)
89
+LibNfc::Utils::ResultString FreeFareTag::readBlock(int sector, int block, const std::string& key, int keyType)
90
 {
90
 {
91
     return _tag->readBlock(sector, block, key, keyType);
91
     return _tag->readBlock(sector, block, key, keyType);
92
 }
92
 }
93
 
93
 
94
-LibNfc::Utils::Result<FreeFareSector> FreeFareTag::readSector(int sector, std::string key, int keyType)
94
+LibNfc::Utils::Result<FreeFareSector> FreeFareTag::readSector(int sector, const std::string& key, int keyType)
95
 {
95
 {
96
     std::string res;
96
     std::string res;
97
     int lastBlock = _tag->getSectorBlockCount(sector);
97
     int lastBlock = _tag->getSectorBlockCount(sector);
107
     return LibNfc::Utils::Result<FreeFareSector>::ok(FreeFareSector(res));
107
     return LibNfc::Utils::Result<FreeFareSector>::ok(FreeFareSector(res));
108
 }
108
 }
109
 
109
 
110
-LibNfc::Utils::Result<std::vector<FreeFareSector>> FreeFareTag::read(MappedKeys keys, std::function<void(int, int)> cb)
110
+LibNfc::Utils::Result<std::vector<FreeFareSector>> FreeFareTag::read(const MappedKeys& keys, std::function<void(int, int)> cb)
111
 {
111
 {
112
     if (keys.size() != 16) {
112
     if (keys.size() != 16) {
113
         return LibNfc::Utils::Result<std::vector<FreeFareSector>>::error("Must have 16 sectors keys");
113
         return LibNfc::Utils::Result<std::vector<FreeFareSector>>::error("Must have 16 sectors keys");
139
                 }
139
                 }
140
             }
140
             }
141
             sector.setBlock(b, data);
141
             sector.setBlock(b, data);
142
-            if (cb != 0) {
142
+            if (cb != nullptr) {
143
                 cb(++done, total);
143
                 cb(++done, total);
144
             }
144
             }
145
         }
145
         }
146
         int b = 3;
146
         int b = 3;
147
-        std::string dataA = "";
148
-        std::string dataB = "";
147
+        std::string dataA;
148
+        std::string dataB;
149
         if (!sectorKey.first.empty()) {
149
         if (!sectorKey.first.empty()) {
150
             auto blockResult = readBlock(s, b, sectorKey.first, MFC_KEY_A);
150
             auto blockResult = readBlock(s, b, sectorKey.first, MFC_KEY_A);
151
             if (blockResult) {
151
             if (blockResult) {
160
                 keyB = true;
160
                 keyB = true;
161
             }
161
             }
162
         }
162
         }
163
-        if (cb != 0) {
163
+        if (cb != nullptr) {
164
             cb(++done, total);
164
             cb(++done, total);
165
         }
165
         }
166
 
166
 
182
 
182
 
183
         sectors.push_back(sector);
183
         sectors.push_back(sector);
184
     }
184
     }
185
-    if (cb != 0 && done < total) {
185
+    if (cb != nullptr && done < total) {
186
         cb(total, total);
186
         cb(total, total);
187
     }
187
     }
188
 
188
 
189
     return LibNfc::Utils::Result<std::vector<FreeFareSector>>::ok(sectors);
189
     return LibNfc::Utils::Result<std::vector<FreeFareSector>>::ok(sectors);
190
 }
190
 }
191
 
191
 
192
-LibNfc::Utils::Result<std::vector<FreeFareSector>> FreeFareTag::read(std::vector<std::string> keys, std::function<void(int, int)> mapCb,
193
-                                                         std::function<void(int, int)> readCb)
192
+LibNfc::Utils::Result<std::vector<FreeFareSector>> FreeFareTag::read(const std::vector<std::string>& keys,
193
+                                                                     std::function<void(int, int)> mapCb,
194
+                                                                     std::function<void(int, int)> readCb)
194
 {
195
 {
195
     auto mappedKeysResult = mapKeys(keys, mapCb);
196
     auto mappedKeysResult = mapKeys(keys, mapCb);
196
     if (!mappedKeysResult) {
197
     if (!mappedKeysResult) {
199
     return read(mappedKeysResult.getData(), readCb);
200
     return read(mappedKeysResult.getData(), readCb);
200
 }
201
 }
201
 
202
 
202
-LibNfc::Utils::ResultBool FreeFareTag::writeBlock(int sector, int block, std::string key, int keyType, const std::string &data)
203
+LibNfc::Utils::ResultBool FreeFareTag::writeBlock(int sector, int block, const std::string& key, int keyType, const std::string& data)
203
 {
204
 {
204
     return _tag->writeBlock(sector, block, key, keyType, LibNfc::Utils::StringUtils::ensureSize(data, 16));
205
     return _tag->writeBlock(sector, block, key, keyType, LibNfc::Utils::StringUtils::ensureSize(data, 16));
205
 }
206
 }
206
 
207
 
207
-LibNfc::Utils::ResultBool FreeFareTag::writeSector(int sector, std::string key, int keyType, const std::string &data)
208
+LibNfc::Utils::ResultBool FreeFareTag::writeSector(int sector, const std::string& key, int keyType, const std::string& data)
208
 {
209
 {
209
     std::string d = LibNfc::Utils::StringUtils::ensureSize(data, 64);
210
     std::string d = LibNfc::Utils::StringUtils::ensureSize(data, 64);
210
     std::string errors;
211
     std::string errors;
221
     return LibNfc::Utils::ResultBool::error(errors);
222
     return LibNfc::Utils::ResultBool::error(errors);
222
 }
223
 }
223
 
224
 
224
-LibNfc::Utils::ResultBool FreeFareTag::write(MappedKeys keys, const std::string &data, bool writeSector0, std::function<void(int, int)> cb)
225
+LibNfc::Utils::ResultBool FreeFareTag::write(const MappedKeys& keys, const std::string& data, bool writeSector0, std::function<void(int, int)> cb)
225
 {
226
 {
226
     if (keys.size() != 16) {
227
     if (keys.size() != 16) {
227
         return LibNfc::Utils::ResultBool::error("Must have 16 sectors keys");
228
         return LibNfc::Utils::ResultBool::error("Must have 16 sectors keys");
228
     }
229
     }
229
     std::string d = LibNfc::Utils::StringUtils::ensureSize(data, 1024);
230
     std::string d = LibNfc::Utils::StringUtils::ensureSize(data, 1024);
230
     std::string errors;
231
     std::string errors;
231
-    int done = 0;
232
-    int total = 4 * keys.size();
233
-    for (int s = 0; s < keys.size(); ++s) {
232
+    unsigned long done = 0;
233
+    unsigned long total = 4 * keys.size();
234
+    for (unsigned long s = 0; s < keys.size(); ++s) {
234
         auto sectorKey = keys[s];
235
         auto sectorKey = keys[s];
235
         for (int b = 0; b < 4; ++b) {
236
         for (int b = 0; b < 4; ++b) {
236
             if (s == 0 && b == 0 && !writeSector0) {
237
             if (s == 0 && b == 0 && !writeSector0) {
237
                 continue;
238
                 continue;
238
             }
239
             }
239
             std::string blockData = d.substr((s * 64) + (b * 16), 16);
240
             std::string blockData = d.substr((s * 64) + (b * 16), 16);
240
-            if (cb != 0 && done < total) {
241
+            if (cb != nullptr && done < total) {
241
                 bool keyA = false;
242
                 bool keyA = false;
242
                 bool keyB = false;
243
                 bool keyB = false;
243
                 std::string sectorErrors;
244
                 std::string sectorErrors;
268
             }
269
             }
269
         }
270
         }
270
     }
271
     }
271
-    if (cb != 0 && done < total) {
272
+    if (cb != nullptr && done < total) {
272
         cb(total, total);
273
         cb(total, total);
273
     }
274
     }
274
     if (errors.empty()) {
275
     if (errors.empty()) {
277
     return LibNfc::Utils::ResultBool::error(errors);
278
     return LibNfc::Utils::ResultBool::error(errors);
278
 }
279
 }
279
 
280
 
280
-LibNfc::Utils::ResultBool FreeFareTag::write(std::vector<std::string> keys, const std::string &data, bool writeSector0,
281
+LibNfc::Utils::ResultBool FreeFareTag::write(const std::vector<std::string>& keys, const std::string& data, bool writeSector0,
281
                                       std::function<void(int, int)> mapCb, std::function<void(int, int)> writeCb)
282
                                       std::function<void(int, int)> mapCb, std::function<void(int, int)> writeCb)
282
 {
283
 {
283
     auto mappedKeysResult = mapKeys(keys, mapCb);
284
     auto mappedKeysResult = mapKeys(keys, mapCb);

+ 4
- 4
libnfc_cpptools/src/freefare/FreeFareTagInternal.h View File

2
 // Created by robin on 6/19/16.
2
 // Created by robin on 6/19/16.
3
 //
3
 //
4
 
4
 
5
-#ifndef MIFARE_TOOLS_FREEFARETAG_H
6
-#define MIFARE_TOOLS_FREEFARETAG_H
5
+#ifndef LIBNFC_CPPTOOLS_FREEFARETAG_H
6
+#define LIBNFC_CPPTOOLS_FREEFARETAG_H
7
 
7
 
8
 #include <freefare.h>
8
 #include <freefare.h>
9
 #include <string>
9
 #include <string>
18
 {
18
 {
19
 public:
19
 public:
20
 
20
 
21
-    FreeFareTagInternal(FreefareTag tag);
21
+    explicit FreeFareTagInternal(FreefareTag tag);
22
     ~FreeFareTagInternal();
22
     ~FreeFareTagInternal();
23
 
23
 
24
     LibNfc::Utils::ResultBool authenticate(int sector, std::string key, int keyType);
24
     LibNfc::Utils::ResultBool authenticate(int sector, std::string key, int keyType);
50
 }; // FreeFare
50
 }; // FreeFare
51
 }; // LibNfc
51
 }; // LibNfc
52
 
52
 
53
-#endif //MIFARE_TOOLS_FREEFARETAG_H
53
+#endif //LIBNFC_CPPTOOLS_FREEFARETAG_H

+ 1
- 1
libnfc_cpptools/src/libnfc/LibNfc.cpp View File

74
     return LibNfc::Utils::Result<std::vector<std::shared_ptr<NfcDevice>>>::ok(devicesBusiness);
74
     return LibNfc::Utils::Result<std::vector<std::shared_ptr<NfcDevice>>>::ok(devicesBusiness);
75
 }
75
 }
76
 
76
 
77
-std::string LibNfcContext::getMifareToolsVersion()
77
+std::string LibNfcContext::getLibNfcCppToolsVersion()
78
 {
78
 {
79
     return QUOTE(GIT_REF_NAME)  "-"  QUOTE(GIT_SHA1);
79
     return QUOTE(GIT_REF_NAME)  "-"  QUOTE(GIT_SHA1);
80
 }
80
 }

+ 3
- 3
libnfc_cpptools/src/libnfc/LibNfcInternal.h View File

2
 // Created by robin on 6/19/16.
2
 // Created by robin on 6/19/16.
3
 //
3
 //
4
 
4
 
5
-#ifndef MIFARE_TOOLS_LIBNFC_H
6
-#define MIFARE_TOOLS_LIBNFC_H
5
+#ifndef LIBNFC_CPPTOOLS_LIBNFC_H
6
+#define LIBNFC_CPPTOOLS_LIBNFC_H
7
 
7
 
8
 #include <memory>
8
 #include <memory>
9
 #include <nfc/nfc.h>
9
 #include <nfc/nfc.h>
40
 }; // Core
40
 }; // Core
41
 }; // LibNfc
41
 }; // LibNfc
42
 
42
 
43
-#endif //MIFARE_TOOLS_LIBNFC_H
43
+#endif //LIBNFC_CPPTOOLS_LIBNFC_H

+ 2
- 2
libnfc_cpptools/src/libnfc/NfcDeviceInternal.cpp View File

11
 
11
 
12
 NfcDeviceInternal::NfcDeviceInternal(const LibNfcInternal* libNfc, const std::string& str)
12
 NfcDeviceInternal::NfcDeviceInternal(const LibNfcInternal* libNfc, const std::string& str)
13
     : _connStr(str)
13
     : _connStr(str)
14
-    , _device(0)
14
+    , _device(nullptr)
15
     , _libNfc(libNfc)
15
     , _libNfc(libNfc)
16
 {
16
 {
17
 }
17
 }
33
 void NfcDeviceInternal::close()
33
 void NfcDeviceInternal::close()
34
 {
34
 {
35
     nfc_close(_device);
35
     nfc_close(_device);
36
-    _device = 0;
36
+    _device = nullptr;
37
 }
37
 }
38
 
38
 
39
 nfc_device *NfcDeviceInternal::getDevice() const
39
 nfc_device *NfcDeviceInternal::getDevice() const

+ 3
- 3
libnfc_cpptools/src/libnfc/NfcDeviceInternal.h View File

2
 // Created by robin on 6/19/16.
2
 // Created by robin on 6/19/16.
3
 //
3
 //
4
 
4
 
5
-#ifndef MIFARE_TOOLS_NFCDEVICEDBO_H
6
-#define MIFARE_TOOLS_NFCDEVICEDBO_H
5
+#ifndef LIBNFC_CPPTOOLS_NFCDEVICEDBO_H
6
+#define LIBNFC_CPPTOOLS_NFCDEVICEDBO_H
7
 
7
 
8
 
8
 
9
 #include <string>
9
 #include <string>
43
 }; // Core
43
 }; // Core
44
 }; // LibNfc
44
 }; // LibNfc
45
 
45
 
46
-#endif //MIFARE_TOOLS_NFCDEVICEDBO_H
46
+#endif //LIBNFC_CPPTOOLS_NFCDEVICEDBO_H

Loading…
Cancel
Save