Skip to content

Commit 69be3b6

Browse files
committed
Optimize, use default comparisons no need to allocate
Signed-off-by: Raul Metsma <raul@metsma.ee>
1 parent 553415d commit 69be3b6

File tree

4 files changed

+11
-21
lines changed

4 files changed

+11
-21
lines changed

lib/libpcsc-cpp/include/pcsc-cpp/pcsc-cpp.hpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,11 @@ struct ResponseApdu
9292
byte_type sw1 {};
9393
byte_type sw2 {};
9494

95-
byte_vector data;
95+
byte_vector data {};
9696

9797
static constexpr size_t MAX_DATA_SIZE = 256;
9898
static constexpr size_t MAX_SIZE = MAX_DATA_SIZE + 2; // + sw1 and sw2
9999

100-
ResponseApdu(byte_type s1, byte_type s2, byte_vector d = {}) :
101-
sw1(s1), sw2(s2), data(std::move(d))
102-
{
103-
}
104-
105-
ResponseApdu() = default;
106-
107100
static ResponseApdu fromBytes(byte_vector data)
108101
{
109102
if (data.size() < 2) {
@@ -136,6 +129,8 @@ struct ResponseApdu
136129

137130
constexpr bool isOK() const noexcept { return sw1 == OK && sw2 == 0x00; }
138131

132+
bool operator==(const ResponseApdu& other) const = default;
133+
139134
// TODO: friend function toString() in utilities.hpp
140135
};
141136

@@ -282,14 +277,11 @@ std::vector<Reader> listReaders();
282277

283278
// Utility functions.
284279

285-
extern const byte_vector APDU_RESPONSE_OK;
286-
287280
/** Convert bytes to hex string. */
288281
std::string bytes2hexstr(const byte_vector& bytes);
289282

290283
/** Transmit APDU command and verify that expected response is received. */
291-
void transmitApduWithExpectedResponse(const SmartCard& card, const CommandApdu& command,
292-
const byte_vector& expectedResponseBytes = APDU_RESPONSE_OK);
284+
void transmitApduWithExpectedResponse(const SmartCard& card, const CommandApdu& command);
293285

294286
/** Read data length from currently selected file header, file must be ASN.1-encoded. */
295287
size_t readDataLengthFromAsn1(const SmartCard& card);

lib/libpcsc-cpp/src/SmartCard.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ class CardImpl
258258
{
259259
byte_vector getResponseCommand {0x00, 0xc0, 0x00, 0x00, 0x00};
260260

261-
auto newResponse = ResponseApdu(response.sw1, response.sw2);
261+
ResponseApdu newResponse{response.sw1, response.sw2};
262262

263263
while (newResponse.sw1 == ResponseApdu::MORE_DATA_AVAILABLE) {
264264
getResponseCommand[4] = newResponse.sw2;

lib/libpcsc-cpp/src/utils.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ class UnexpectedResponseError : public Error
7272
namespace pcsc_cpp
7373
{
7474

75-
const byte_vector APDU_RESPONSE_OK {ResponseApdu::OK, 0x00};
76-
7775
std::string bytes2hexstr(const byte_vector& bytes)
7876
{
7977
std::ostringstream hexStringBuilder;
@@ -86,13 +84,13 @@ std::string bytes2hexstr(const byte_vector& bytes)
8684
return hexStringBuilder.str();
8785
}
8886

89-
void transmitApduWithExpectedResponse(const SmartCard& card, const CommandApdu& command,
90-
const byte_vector& expectedResponseBytes)
87+
void transmitApduWithExpectedResponse(const SmartCard& card, const CommandApdu& command)
9188
{
89+
static const ResponseApdu APDU_RESPONSE_OK {ResponseApdu::OK, 0x00};
9290
const auto response = card.transmit(command);
93-
if (response.toBytes() != expectedResponseBytes) {
94-
throw UnexpectedResponseError(command, expectedResponseBytes, response, __FILE__, __LINE__,
95-
__func__);
91+
if (response != APDU_RESPONSE_OK) {
92+
throw UnexpectedResponseError(command, APDU_RESPONSE_OK.toBytes(), response, __FILE__,
93+
__LINE__, __func__);
9694
}
9795
}
9896

lib/libpcsc-cpp/tests/mock/test-connect-to-card-transmit-apdus.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,5 @@ TEST(pcsc_cpp_test, transmitApduSuccess)
6262
auto transactionGuard = card->beginTransaction();
6363
auto response = card->transmit(command);
6464

65-
EXPECT_EQ(response.toBytes(), expectedResponse.toBytes());
65+
EXPECT_EQ(response, expectedResponse);
6666
}

0 commit comments

Comments
 (0)