Skip to content

Commit c0f7037

Browse files
tlv_from_apdu now returns an enum instead of a simple bool
1 parent da3d9d6 commit c0f7037

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
@@ -15,26 +15,26 @@ static void reset_state(void) {
1515
explicit_bzero(&g_tlv, sizeof(g_tlv));
1616
}
1717

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

2626
if ((payload == NULL) || (handler == NULL)) {
27-
return false;
27+
return TLV_APDU_ERROR;
2828
}
2929

3030
if (first_chunk) {
3131
if (g_tlv.payload != NULL) {
3232
PRINTF("Error: remnants from an incomplete TLV payload!\n");
3333
reset_state();
34-
return false;
34+
return TLV_APDU_ERROR;
3535
}
3636
if ((offset + sizeof(g_tlv.size)) > lc) {
37-
return false;
37+
return TLV_APDU_ERROR;
3838
}
3939
g_tlv.size = read_u16_be(payload, offset);
4040
offset += sizeof(g_tlv.size);
@@ -43,22 +43,22 @@ bool tlv_from_apdu(bool first_chunk,
4343
if (g_tlv.size > (lc - offset)) {
4444
if ((g_tlv.payload = APP_MEM_ALLOC(g_tlv.size)) == NULL) {
4545
reset_state();
46-
return false;
46+
return TLV_APDU_ERROR;
4747
}
4848
}
4949
}
5050

5151
if (g_tlv.size == 0) {
5252
PRINTF("Error: zero-length TLV payload is invalid!\n");
5353
reset_state();
54-
return false;
54+
return TLV_APDU_ERROR;
5555
}
5656

5757
chunk_length = lc - offset;
5858
if ((g_tlv.pos + chunk_length) > g_tlv.size) {
5959
PRINTF("Error: TLV payload bigger than expected!\n");
6060
reset_state();
61-
return false;
61+
return TLV_APDU_ERROR;
6262
}
6363

6464
if (g_tlv.payload != NULL) {
@@ -79,6 +79,7 @@ bool tlv_from_apdu(bool first_chunk,
7979
}
8080
ret = (*handler)(&buf);
8181
reset_state();
82+
return ret ? TLV_APDU_SUCCESS : TLV_APDU_ERROR;
8283
}
83-
return ret;
84+
return TLV_APDU_PENDING;
8485
}

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)