Skip to content

Commit 6ba4543

Browse files
metsmamrts
authored andcommitted
Do not allocate another std::vector to append hex string
Signed-off-by: Raul Metsma <raul@metsma.ee>
1 parent 9fffbe6 commit 6ba4543

File tree

7 files changed

+24
-46
lines changed

7 files changed

+24
-46
lines changed

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

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ constexpr uint16_t toSW(byte_type sw1, byte_type sw2) noexcept
8181
return uint16_t(sw1 << 8) | sw2;
8282
}
8383

84+
/** Convert bytes to hex string. */
85+
std::string bytes2hexstr(const byte_vector& bytes);
86+
8487
/** Struct that wraps response APDUs. */
8588
struct ResponseApdu
8689
{
@@ -120,22 +123,14 @@ struct ResponseApdu
120123
return {sw1, sw2, std::move(data)};
121124
}
122125

123-
byte_vector toBytes() const
124-
{
125-
// makes a copy, valid both if data is empty or full
126-
auto bytes = data;
127-
128-
bytes.push_back(sw1);
129-
bytes.push_back(sw2);
130-
131-
return bytes;
132-
}
133-
134126
constexpr uint16_t toSW() const noexcept { return pcsc_cpp::toSW(sw1, sw2); }
135127

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

138-
// TODO: friend function toString() in utilities.hpp
130+
friend std::string operator+(std::string&& lhs, const ResponseApdu& rhs)
131+
{
132+
return lhs + pcsc_cpp::bytes2hexstr(rhs.data) + pcsc_cpp::bytes2hexstr({rhs.sw1, rhs.sw2});
133+
}
139134
};
140135

141136
/** Struct that wraps command APDUs. */
@@ -270,9 +265,6 @@ std::vector<Reader> listReaders();
270265

271266
// Utility functions.
272267

273-
/** Convert bytes to hex string. */
274-
std::string bytes2hexstr(const byte_vector& bytes);
275-
276268
/** Transmit APDU command and verify that expected response is received. */
277269
void transmitApduWithExpectedResponse(const SmartCard& card, const CommandApdu& command);
278270

lib/libpcsc-cpp/src/SmartCard.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,7 @@ class CardImpl
243243
break;
244244
default:
245245
THROW(Error,
246-
"Error response: '" + bytes2hexstr({response.sw1, response.sw2}) + "', protocol "
247-
+ std::to_string(protocol()));
246+
"Error response: '" + response + "', protocol " + std::to_string(protocol()));
248247
}
249248

250249
if (response.sw1 == ResponseApdu::WRONG_LE_LENGTH) {

lib/libpcsc-cpp/src/utils.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ class UnexpectedResponseError : public Error
5858
const char* file, const int line,
5959
const char* callerFunctionName) :
6060
Error("transmitApduWithExpectedResponse(): Unexpected response to command '"s
61-
+ bytes2hexstr(command) + "' - expected '9000', got '"s
62-
+ bytes2hexstr(response.toBytes()) + "' in " + removeAbsolutePathPrefix(file) + ':'
63-
+ std::to_string(line) + ':' + callerFunctionName)
61+
+ bytes2hexstr(command) + "' - expected '9000', got '"s + response + "' in "
62+
+ removeAbsolutePathPrefix(file) + ':' + std::to_string(line) + ':'
63+
+ callerFunctionName)
6464
{
6565
}
6666
};

src/electronic-id.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ AutoSelectFailed::AutoSelectFailed(Reason r) :
289289
VerifyPinFailed::VerifyPinFailed(const Status s, const observer_ptr<pcsc_cpp::ResponseApdu> ra,
290290
const int8_t r) :
291291
Error(std::string("Verify PIN failed, status: ") + std::string(magic_enum::enum_name(s))
292-
+ (ra ? ", response: " + pcsc_cpp::bytes2hexstr(ra->toBytes()) : "")),
292+
+ (ra ? ", response: " + *ra : "")),
293293
_status(s), _retries(r)
294294
{
295295
}

src/electronic-ids/pcsc/EIDIDEMIA.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,7 @@ ElectronicID::PinRetriesRemainingAndMax EIDIDEMIA::pinRetriesLeft(byte_type pinR
152152
0x00};
153153
const auto response = card->transmit(GET_DATA_ODD);
154154
if (!response.isOK()) {
155-
THROW(SmartCardError,
156-
"Command GET DATA ODD failed with error "
157-
+ pcsc_cpp::bytes2hexstr(response.toBytes()));
155+
THROW(SmartCardError, "Command GET DATA ODD failed with error " + response);
158156
}
159157
if (response.data.size() < 14) {
160158
THROW(SmartCardError,

src/electronic-ids/pcsc/FinEID.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -148,25 +148,20 @@ byte_vector FinEIDv3::sign(const HashAlgorithm hashAlgo, const byte_vector& hash
148148

149149
if (response.sw1 == ResponseApdu::WRONG_LENGTH) {
150150
THROW(SmartCardError,
151-
"Wrong data length in command COMPUTE SIGNATURE argument: "
152-
+ bytes2hexstr(response.toBytes()));
151+
"Wrong data length in command COMPUTE SIGNATURE argument: " + response);
153152
}
154153
if (!response.isOK()) {
155-
THROW(SmartCardError,
156-
"Command COMPUTE SIGNATURE failed with error " + bytes2hexstr(response.toBytes()));
154+
THROW(SmartCardError, "Command COMPUTE SIGNATURE failed with error " + response);
157155
}
158156

159157
const CommandApdu getSignature {0x00, 0x2A, 0x9E, 0x9A, LE};
160158
const auto signature = card->transmit(getSignature);
161159

162160
if (signature.sw1 == ResponseApdu::WRONG_LENGTH) {
163-
THROW(SmartCardError,
164-
"Wrong data length in command GET SIGNATURE argument: "
165-
+ bytes2hexstr(response.toBytes()));
161+
THROW(SmartCardError, "Wrong data length in command GET SIGNATURE argument: " + response);
166162
}
167163
if (!signature.isOK()) {
168-
THROW(SmartCardError,
169-
"Command GET SIGNATURE failed with error " + bytes2hexstr(signature.toBytes()));
164+
THROW(SmartCardError, "Command GET SIGNATURE failed with error " + signature);
170165
}
171166

172167
return signature.data;
@@ -178,8 +173,7 @@ ElectronicID::PinRetriesRemainingAndMax FinEIDv3::pinRetriesLeft(byte_type pinRe
178173
0x00, 0xCB, 0x00, 0xFF, {0xA0, 0x03, 0x83, 0x01, pinReference}};
179174
const auto response = card->transmit(GET_DATA);
180175
if (!response.isOK()) {
181-
THROW(SmartCardError,
182-
"Command GET DATA failed with error " + pcsc_cpp::bytes2hexstr(response.toBytes()));
176+
THROW(SmartCardError, "Command GET DATA failed with error " + response);
183177
}
184178
if (response.data.size() < 21) {
185179
THROW(SmartCardError,

src/electronic-ids/pcsc/pcsc-common.hpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,12 @@ inline pcsc_cpp::byte_vector internalAuthenticate(pcsc_cpp::SmartCard& card,
123123

124124
if (response.sw1 == pcsc_cpp::ResponseApdu::WRONG_LENGTH) {
125125
THROW(SmartCardError,
126-
cardType + ": Wrong data length in command INTERNAL AUTHENTICATE argument: "
127-
+ pcsc_cpp::bytes2hexstr(response.toBytes()));
126+
cardType
127+
+ ": Wrong data length in command INTERNAL AUTHENTICATE argument: " + response);
128128
}
129129
if (!response.isOK()) {
130130
THROW(SmartCardError,
131-
cardType + ": Command INTERNAL AUTHENTICATE failed with error "
132-
+ pcsc_cpp::bytes2hexstr(response.toBytes()));
131+
cardType + ": Command INTERNAL AUTHENTICATE failed with error " + response);
133132
}
134133

135134
return response.data;
@@ -144,13 +143,11 @@ inline pcsc_cpp::byte_vector computeSignature(pcsc_cpp::SmartCard& card,
144143

145144
if (response.sw1 == pcsc_cpp::ResponseApdu::WRONG_LENGTH) {
146145
THROW(SmartCardError,
147-
cardType + ": Wrong data length in command COMPUTE SIGNATURE argument: "
148-
+ pcsc_cpp::bytes2hexstr(response.toBytes()));
146+
cardType + ": Wrong data length in command COMPUTE SIGNATURE argument: " + response);
149147
}
150148
if (!response.isOK()) {
151149
THROW(SmartCardError,
152-
cardType + ": Command COMPUTE SIGNATURE failed with error "
153-
+ pcsc_cpp::bytes2hexstr(response.toBytes()));
150+
cardType + ": Command COMPUTE SIGNATURE failed with error " + response);
154151
}
155152

156153
return response.data;
@@ -165,9 +162,7 @@ inline pcsc_cpp::byte_type selectSecurityEnv(pcsc_cpp::SmartCard& card, pcsc_cpp
165162
{0x00, 0x22, 0x41, env, {0x80, 0x01, signatureAlgo, 0x84, 0x01, keyReference}});
166163

167164
if (!response.isOK()) {
168-
THROW(SmartCardError,
169-
cardType + ": Command SET ENV failed with error "
170-
+ pcsc_cpp::bytes2hexstr(response.toBytes()));
165+
THROW(SmartCardError, cardType + ": Command SET ENV failed with error " + response);
171166
}
172167
return signatureAlgo;
173168
}

0 commit comments

Comments
 (0)