|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/fatih/color" |
| 8 | + "github.com/spensercai/nfc_apdu_runner/tools/ResponseDecoder/pkg/tlv" |
| 9 | + "github.com/spf13/cobra" |
| 10 | +) |
| 11 | + |
| 12 | +var ( |
| 13 | + hexData string |
| 14 | + tagList string |
| 15 | + dataType string |
| 16 | +) |
| 17 | + |
| 18 | +// tlvCmd 表示tlv命令 |
| 19 | +var tlvCmd = &cobra.Command{ |
| 20 | + Use: "tlv", |
| 21 | + Short: "解析TLV数据并提取指定标签的值", |
| 22 | + Long: `解析TLV数据并提取指定标签的值。 |
| 23 | +
|
| 24 | +示例: |
| 25 | + # 解析所有标签 |
| 26 | + response_decoder tlv --hex 6F198407A0000000031010A50E500A4D617374657243617264 |
| 27 | +
|
| 28 | + # 解析指定标签 |
| 29 | + response_decoder tlv --hex 6F198407A0000000031010A50E500A4D617374657243617264 --tag 84,50 |
| 30 | +
|
| 31 | + # 指定数据类型 |
| 32 | + response_decoder tlv --hex 6F198407A0000000031010A50E500A4D617374657243617264 --tag 50 --type ascii`, |
| 33 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 34 | + if hexData == "" { |
| 35 | + return fmt.Errorf("必须提供十六进制数据 (--hex)") |
| 36 | + } |
| 37 | + |
| 38 | + // 解析TLV数据 |
| 39 | + tlvList, err := tlv.ParseHex(hexData) |
| 40 | + if err != nil { |
| 41 | + return fmt.Errorf("解析TLV数据失败: %w", err) |
| 42 | + } |
| 43 | + |
| 44 | + // 如果没有指定标签,则显示所有标签 |
| 45 | + if tagList == "" { |
| 46 | + return displayAllTags(tlvList) |
| 47 | + } |
| 48 | + |
| 49 | + // 解析标签列表 |
| 50 | + tags := strings.Split(tagList, ",") |
| 51 | + for _, tag := range tags { |
| 52 | + tag = strings.TrimSpace(tag) |
| 53 | + if tag == "" { |
| 54 | + continue |
| 55 | + } |
| 56 | + |
| 57 | + // 查找标签 |
| 58 | + tlvItem, err := tlvList.FindTagRecursive(tag) |
| 59 | + if err != nil { |
| 60 | + fmt.Printf("标签 %s 未找到: %v\n", tag, err) |
| 61 | + continue |
| 62 | + } |
| 63 | + |
| 64 | + // 显示标签值 |
| 65 | + displayTagValue(tag, tlvItem) |
| 66 | + } |
| 67 | + |
| 68 | + return nil |
| 69 | + }, |
| 70 | +} |
| 71 | + |
| 72 | +// displayAllTags 显示所有标签 |
| 73 | +func displayAllTags(tlvList tlv.TLVList) error { |
| 74 | + titleColor := color.New(color.FgCyan, color.Bold) |
| 75 | + |
| 76 | + fmt.Println(strings.Repeat("=", 50)) |
| 77 | + titleColor.Println("TLV 数据解析结果") |
| 78 | + fmt.Println(strings.Repeat("=", 50)) |
| 79 | + |
| 80 | + // 递归显示TLV结构 |
| 81 | + displayTLVStructure(tlvList, 0) |
| 82 | + |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +// displayTLVStructure 递归显示TLV结构 |
| 87 | +func displayTLVStructure(tlvList tlv.TLVList, level int) { |
| 88 | + indent := strings.Repeat(" ", level) |
| 89 | + tagColor := color.New(color.FgYellow, color.Bold) |
| 90 | + valueColor := color.New(color.FgGreen) |
| 91 | + constructedColor := color.New(color.FgMagenta, color.Bold) |
| 92 | + |
| 93 | + for _, item := range tlvList { |
| 94 | + // 显示标签 |
| 95 | + tagColor.Printf("%s标签: %s", indent, item.Tag) |
| 96 | + |
| 97 | + // 显示长度 |
| 98 | + fmt.Printf(", 长度: %d", item.Length) |
| 99 | + |
| 100 | + // 如果是构造型标签,递归显示其内容 |
| 101 | + if item.IsConstructed() { |
| 102 | + constructedColor.Println(" [构造型]") |
| 103 | + |
| 104 | + // 解析嵌套TLV |
| 105 | + nestedList, err := tlv.Parse(item.Value) |
| 106 | + if err != nil { |
| 107 | + fmt.Printf("%s 解析嵌套TLV失败: %v\n", indent, err) |
| 108 | + continue |
| 109 | + } |
| 110 | + |
| 111 | + // 递归显示嵌套TLV |
| 112 | + displayTLVStructure(nestedList, level+1) |
| 113 | + } else { |
| 114 | + // 显示值 |
| 115 | + fmt.Print(", 值: ") |
| 116 | + |
| 117 | + // 根据数据类型显示值 |
| 118 | + switch dataType { |
| 119 | + case "utf8", "utf-8": |
| 120 | + data, err := tlv.GetTagValueAsString(hexData, item.Tag.String(), "utf-8") |
| 121 | + if err != nil { |
| 122 | + valueColor.Printf("%s\n", item.Value) |
| 123 | + } else { |
| 124 | + valueColor.Printf("%s\n", data) |
| 125 | + } |
| 126 | + case "ascii": |
| 127 | + data, err := tlv.GetTagValueAsString(hexData, item.Tag.String(), "ascii") |
| 128 | + if err != nil { |
| 129 | + valueColor.Printf("%s\n", item.Value) |
| 130 | + } else { |
| 131 | + valueColor.Printf("%s\n", data) |
| 132 | + } |
| 133 | + case "numeric": |
| 134 | + data, err := tlv.GetTagValueAsString(hexData, item.Tag.String(), "numeric") |
| 135 | + if err != nil { |
| 136 | + valueColor.Printf("%s\n", item.Value) |
| 137 | + } else { |
| 138 | + valueColor.Printf("%s\n", data) |
| 139 | + } |
| 140 | + default: |
| 141 | + valueColor.Printf("%s\n", item.Value) |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +// displayTagValue 显示标签值 |
| 148 | +func displayTagValue(tag string, tlvItem *tlv.TLV) { |
| 149 | + tagColor := color.New(color.FgYellow, color.Bold) |
| 150 | + valueColor := color.New(color.FgGreen) |
| 151 | + |
| 152 | + // 显示标签 |
| 153 | + tagColor.Printf("标签: %s", tag) |
| 154 | + |
| 155 | + // 显示长度 |
| 156 | + fmt.Printf(", 长度: %d", tlvItem.Length) |
| 157 | + |
| 158 | + // 显示值 |
| 159 | + fmt.Print(", 值: ") |
| 160 | + |
| 161 | + // 根据数据类型显示值 |
| 162 | + switch dataType { |
| 163 | + case "utf8", "utf-8": |
| 164 | + data, err := tlv.GetTagValueAsString(hexData, tag, "utf-8") |
| 165 | + if err != nil { |
| 166 | + valueColor.Printf("%s\n", tlvItem.Value) |
| 167 | + } else { |
| 168 | + valueColor.Printf("%s\n", data) |
| 169 | + } |
| 170 | + case "ascii": |
| 171 | + data, err := tlv.GetTagValueAsString(hexData, tag, "ascii") |
| 172 | + if err != nil { |
| 173 | + valueColor.Printf("%s\n", tlvItem.Value) |
| 174 | + } else { |
| 175 | + valueColor.Printf("%s\n", data) |
| 176 | + } |
| 177 | + case "numeric": |
| 178 | + data, err := tlv.GetTagValueAsString(hexData, tag, "numeric") |
| 179 | + if err != nil { |
| 180 | + valueColor.Printf("%s\n", tlvItem.Value) |
| 181 | + } else { |
| 182 | + valueColor.Printf("%s\n", data) |
| 183 | + } |
| 184 | + default: |
| 185 | + valueColor.Printf("%s\n", tlvItem.Value) |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +func init() { |
| 190 | + rootCmd.AddCommand(tlvCmd) |
| 191 | + |
| 192 | + // 添加命令行标志 |
| 193 | + tlvCmd.Flags().StringVar(&hexData, "hex", "", "要解析的十六进制TLV数据") |
| 194 | + tlvCmd.Flags().StringVar(&tagList, "tag", "", "要提取的标签列表,用逗号分隔") |
| 195 | + tlvCmd.Flags().StringVar(&dataType, "type", "", "数据类型 (utf8, ascii, numeric)") |
| 196 | +} |
0 commit comments