-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProgram.cs
78 lines (70 loc) · 3.13 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using NCIDecoder.Structs;
using System;
using System.Collections.Generic;
using System.IO;
namespace NCIDecoder
{
class Program
{
static void Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("Usage: ncidecoder.exe <inputFilePath>");
Console.WriteLine("The input file must be the output of https://github.com/VivoKey/NFCSnoopDecoder");
return;
}
try
{
string[] lines = File.ReadAllLines(args[0]);
if (lines.Length < 4)
{
throw new Exception("Input file must have more than 3 lines!");
}
string NFCSnoopVersion = lines[0].Substring(20);
Console.WriteLine("NFCSnoop Version: " + NFCSnoopVersion);
var nciPackets = new List<NCIPacket>();
long currentTimestamp = 0;
for (int i = 3; i < lines.Length; i++)
{
var dataLineItemized = lines[i].Split(new char[] { ',' });
if (dataLineItemized.Length != 3)
{
throw new Exception("Incorrect data line at ");
}
bool isReceived = dataLineItemized[1] == " true";
var dir = isReceived ? PacketDirection.NFCCToCPU : PacketDirection.CPUToNFCC;
currentTimestamp += long.Parse(dataLineItemized[0]);
byte[] data = Convert.FromHexString(dataLineItemized[2].Trim());
nciPackets.Add(NCIPacketParser.Parse(dir, data, currentTimestamp));
}
//TODO: perhaps output json instead?
//TODO: support segmented packets, control ones have a max len until the last segmented packet
foreach (var item in nciPackets)
{
Console.Write($"{item.Timestamp}|{(item.PacketDirection == PacketDirection.NFCCToCPU ? "R" : "S")}|[{item.MessageTypeDecoded}] ");
if (item is NCIDataPacket)
{
var casted = (NCIDataPacket)item;
Console.WriteLine($"PayloadLength: {casted.PayloadLength} Payload: {Convert.ToHexString(casted.Payload)}");
}
else if (item is NCIControlPacket)
{
var casted = (NCIControlPacket)item;
Console.WriteLine($"OpcodeStr: {casted.GetControlOpcodeTypeString()} " +
$"Payload({casted.PayloadLength}): {Convert.ToHexString(casted.Payload)}");
}
else if (item is NCIUnknownPacket)
{
Console.WriteLine($"RawDataLength: {item.RawData.Length} Payload: {Convert.ToHexString(item.RawData)}");
}
}
}
catch (Exception exc)
{
Console.WriteLine("Error while trying to parse the file! Exception: " + exc);
return;
}
}
}
}