Skip to content

Commit

Permalink
tlv_dump: fix off by one, add string decode
Browse files Browse the repository at this point in the history
  • Loading branch information
calebccff committed Aug 3, 2022
1 parent 9e9b6ba commit 7c8e14e
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions lib/qmi_tlv.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include <stdint.h>
Expand Down Expand Up @@ -238,7 +239,7 @@ static inline uint8_t to_hex(uint8_t ch)
void qmi_tlv_dump(struct qmi_tlv *tlv) {
struct qmi_tlv_item *item;
struct qmi_header *pkt;
unsigned offset = 0;
unsigned offset = sizeof(struct qmi_header);
void *pkt_data;
int i = 0, li, j, k;
uint8_t ch;
Expand All @@ -254,10 +255,11 @@ void qmi_tlv_dump(struct qmi_tlv *tlv) {
printf("<<< msg_id : 0x%1$04x (%1$u)\n", pkt->msg_id);
printf("<<< txn_id : 0x%1$04x (%1$u)\n", pkt->txn_id);
printf("<<< TLVs:\n");
while (offset < tlv->size - sizeof(struct qmi_header)) {
// I do not understand why this -1 is needed
while (offset < tlv->size - 1) {
item = pkt_data + offset;
printf("<<< TLV %d: {id: 0x%02x, len: 0x%02x}\n", i, item->key, item->len);
if (item->len > pkt->msg_len - offset) {
if (item->len > pkt->msg_len + sizeof(struct qmi_header) - offset) {
fprintf(stderr, "Invalid item length!\n");
return;
}
Expand All @@ -271,6 +273,18 @@ void qmi_tlv_dump(struct qmi_tlv *tlv) {
line[li++] = to_hex(ch);
line[li++] = k < linelen - 1 ? ':' : ' ';
}

for (; j < LINE_LENGTH; j++) {
line[li++] = ' ';
line[li++] = ' ';
line[li++] = ' ';
}

for (j = 0; j < linelen; j++) {
ch = item->data[i + j];
line[li++] = isprint(ch) ? ch : '.';
}

line[li] = '\0';

printf("%s\n\n", line);
Expand Down

0 comments on commit 7c8e14e

Please sign in to comment.