comparison libcommon/dumpdirfunc.c @ 6:2c72709e0891

EF_DIR dump meaty function factored out into libcommon
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 11 Feb 2021 23:42:53 +0000
parents
children dcfec53643c5
comparison
equal deleted inserted replaced
5:ce189c97b7b1 6:2c72709e0891
1 /*
2 * This module implements the common function for dumping EF_DIR.
3 */
4
5 #include <sys/types.h>
6 #include <stdio.h>
7 #include "simresp.h"
8
9 static void
10 dump_aid(tlv)
11 u_char *tlv;
12 {
13 unsigned reclen, n;
14
15 reclen = tlv[1];
16 printf(" AID:");
17 for (n = 0; n < reclen; n++)
18 printf(" %02X", tlv[n+2]);
19 putchar('\n');
20 }
21
22 static void
23 dump_label(tlv)
24 u_char *tlv;
25 {
26 int rc;
27 unsigned textlen;
28
29 printf(" Label: ");
30 rc = validate_alpha_field(tlv + 2, tlv[1], &textlen);
31 if (rc < 0) {
32 printf("malformed\n");
33 return;
34 }
35 print_alpha_field(tlv + 2, textlen, stdout);
36 putchar('\n');
37 }
38
39 static void
40 dump_unknown_tlv(tlv)
41 u_char *tlv;
42 {
43 unsigned reclen, n;
44
45 reclen = tlv[1] + 2;
46 printf(" TLV:");
47 for (n = 0; n < reclen; n++)
48 printf(" %02X", tlv[n]);
49 putchar('\n');
50 }
51
52 void
53 dump_efdir_record()
54 {
55 unsigned totlen, reclen;
56 u_char *dp, *endp;
57
58 if (sim_resp_data[0] != 0x61) {
59 printf(" bad: first byte != 0x61\n");
60 return;
61 }
62 totlen = sim_resp_data[1];
63 if (totlen < 3 || totlen > 0x7F) {
64 printf(" bad: global length byte 0x%02X is invalid\n", totlen);
65 return;
66 }
67 if (totlen + 2 > sim_resp_data_len) {
68 printf(" bad: TLV global length exceeds EF record length\n");
69 return;
70 }
71 dp = sim_resp_data + 2;
72 endp = sim_resp_data + 2 + totlen;
73 while (dp < endp) {
74 if (endp - dp < 2) {
75 trunc_error: printf(" bad: truncated TLV record\n");
76 return;
77 }
78 if ((dp[0] & 0x1F) == 0x1F) {
79 printf(" bad: extended tag not supported\n");
80 return;
81 }
82 if (dp[1] & 0x80) {
83 printf(" bad: extended length not supported\n");
84 return;
85 }
86 reclen = dp[1] + 2;
87 if (endp - dp < reclen)
88 goto trunc_error;
89 switch (dp[0]) {
90 case 0x4F:
91 dump_aid(dp);
92 break;
93 case 0x50:
94 dump_label(dp);
95 break;
96 default:
97 dump_unknown_tlv(dp);
98 }
99 dp += reclen;
100 }
101 }