Skip to content

Commit 57100b2

Browse files
tlv_from_apdu now returns an enum instead of a simple bool
1 parent d9cbc8f commit 57100b2

2 files changed

Lines changed: 23 additions & 16 deletions

File tree

src/tlv_apdu.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,27 @@ static void reset_state(void) {
1616
explicit_bzero(&g_tlv, sizeof(g_tlv));
1717
}
1818

19-
bool tlv_from_apdu(bool first_chunk,
20-
uint8_t lc,
21-
const uint8_t *payload,
22-
f_tlv_payload_handler handler) {
23-
bool ret = true;
19+
e_tlv_apdu_ret tlv_from_apdu(bool first_chunk,
20+
uint8_t lc,
21+
const uint8_t *payload,
22+
f_tlv_payload_handler handler) {
23+
bool ret;
2424
uint8_t offset = 0;
2525
uint8_t chunk_length;
2626

2727
if ((payload == NULL) || (handler == NULL)) {
2828
reset_state();
29-
return false;
29+
return TLV_APDU_ERROR;
3030
}
3131

3232
if (first_chunk) {
3333
if (g_tlv.payload != NULL) {
3434
PRINTF("Error: remnants from an incomplete TLV payload!\n");
3535
reset_state();
36-
return false;
36+
return TLV_APDU_ERROR;
3737
}
3838
if ((offset + sizeof(g_tlv.size)) > lc) {
39-
return false;
39+
return TLV_APDU_ERROR;
4040
}
4141
g_tlv.size = read_u16_be(payload, offset);
4242
offset += sizeof(g_tlv.size);
@@ -45,7 +45,7 @@ bool tlv_from_apdu(bool first_chunk,
4545
if (g_tlv.size > (lc - offset)) {
4646
if ((g_tlv.payload = APP_MEM_ALLOC(g_tlv.size)) == NULL) {
4747
reset_state();
48-
return false;
48+
return TLV_APDU_ERROR;
4949
}
5050
}
5151
g_tlv.handler_ptr = handler;
@@ -60,14 +60,14 @@ bool tlv_from_apdu(bool first_chunk,
6060
if (g_tlv.size == 0) {
6161
PRINTF("Error: zero-length TLV payload is invalid!\n");
6262
reset_state();
63-
return false;
63+
return TLV_APDU_ERROR;
6464
}
6565

6666
chunk_length = lc - offset;
6767
if ((g_tlv.pos + chunk_length) > g_tlv.size) {
6868
PRINTF("Error: TLV payload bigger than expected!\n");
6969
reset_state();
70-
return false;
70+
return TLV_APDU_ERROR;
7171
}
7272

7373
if (g_tlv.payload != NULL) {
@@ -88,6 +88,7 @@ bool tlv_from_apdu(bool first_chunk,
8888
}
8989
ret = (*handler)(&buf);
9090
reset_state();
91+
return ret ? TLV_APDU_SUCCESS : TLV_APDU_ERROR;
9192
}
92-
return ret;
93+
return TLV_APDU_PENDING;
9394
}

src/tlv_apdu.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66

77
typedef bool (*f_tlv_payload_handler)(const buffer_t *payload);
88

9-
bool tlv_from_apdu(bool first_chunk,
10-
uint8_t lc,
11-
const uint8_t *payload,
12-
f_tlv_payload_handler handler);
9+
typedef enum {
10+
TLV_APDU_ERROR = 0,
11+
TLV_APDU_PENDING,
12+
TLV_APDU_SUCCESS,
13+
} e_tlv_apdu_ret;
14+
15+
e_tlv_apdu_ret tlv_from_apdu(bool first_chunk,
16+
uint8_t lc,
17+
const uint8_t *payload,
18+
f_tlv_payload_handler handler);

0 commit comments

Comments
 (0)