-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathNCPacket.cs
188 lines (177 loc) · 6.82 KB
/
NCPacket.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//NCShark - By AlSch092 @ Github, thanks to @Diamondo25 for MapleShark
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace NCShark
{
public sealed class NCPacket : ListViewItem
{
public DateTime Timestamp { get; private set; }
public bool Outbound { get; private set; }
public ushort Build { get; private set; }
public ushort Locale { get; private set; }
public ushort Opcode { get; private set; }
public new string Name { set { SubItems[4].Text = value; } }
public byte[] Buffer { get; private set; }
public int Cursor { get; private set; }
public int Length { get { return Buffer.Length; } }
public int Remaining { get { return Length - Cursor; } }
public uint PreDecodeIV { get; private set; }
public uint PostDecodeIV { get; private set; }
internal NCPacket(DateTime pTimestamp, bool pOutbound, ushort pOpcode, string pName, byte[] pBuffer)
: base(new string[] {
pTimestamp.ToString("yyyy-MM-dd HH:mm:ss.fff"),
pOutbound ? "Outbound" : "Inbound",
pBuffer.Length.ToString(),
"0x" + pOpcode.ToString("X4"),
pName })
{
Timestamp = pTimestamp;
Outbound = pOutbound;
Opcode = pOpcode;
Buffer = pBuffer;
}
internal NCPacket(DateTime pTimestamp, ushort pOpcode, string pName, byte[] pBuffer)
: base(new string[] {
pTimestamp.ToString("yyyy-MM-dd HH:mm:ss.fff"),
pBuffer.Length.ToString(),
"0x" + pOpcode.ToString("X4"),
pName })
{
Timestamp = pTimestamp;
Opcode = pOpcode;
Buffer = pBuffer;
}
public void Rewind() { Cursor = 0; }
public bool ReadByte(out byte pValue)
{
pValue = 0;
if (Cursor + 1 > Length) return false;
pValue = Buffer[Cursor++];
return true;
}
public bool ReadSByte(out sbyte pValue)
{
pValue = 0;
if (Cursor + 1 > Length) return false;
pValue = (sbyte)Buffer[Cursor++];
return true;
}
public bool ReadUShort(out ushort pValue)
{
pValue = 0;
if (Cursor + 2 > Length) return false;
pValue = (ushort)(Buffer[Cursor++] |
Buffer[Cursor++] << 8);
return true;
}
public bool ReadShort(out short pValue)
{
pValue = 0;
if (Cursor + 2 > Length) return false;
pValue = (short)(Buffer[Cursor++] |
Buffer[Cursor++] << 8);
return true;
}
public bool ReadUInt(out uint pValue)
{
pValue = 0;
if (Cursor + 4 > Length) return false;
pValue = (uint)(Buffer[Cursor++] |
Buffer[Cursor++] << 8 |
Buffer[Cursor++] << 16 |
Buffer[Cursor++] << 24);
return true;
}
public bool ReadInt(out int pValue)
{
pValue = 0;
if (Cursor + 4 > Length) return false;
pValue = (int)(Buffer[Cursor++] |
Buffer[Cursor++] << 8 |
Buffer[Cursor++] << 16 |
Buffer[Cursor++] << 24);
return true;
}
public bool ReadFloat(out float pValue)
{
pValue = 0;
if (Cursor + 4 > Length) return false;
pValue = BitConverter.ToSingle(Buffer, Cursor);
Cursor += 4;
return true;
}
public bool ReadULong(out ulong pValue)
{
pValue = 0;
if (Cursor + 8 > Length) return false;
pValue = (ulong)(Buffer[Cursor++] |
Buffer[Cursor++] << 8 |
Buffer[Cursor++] << 16 |
Buffer[Cursor++] << 24 |
Buffer[Cursor++] << 32 |
Buffer[Cursor++] << 40 |
Buffer[Cursor++] << 48 |
Buffer[Cursor++] << 56);
return true;
}
public bool ReadLong(out long pValue)
{
pValue = 0;
if (Cursor + 8 > Length) return false;
pValue = (long)(Buffer[Cursor++] |
Buffer[Cursor++] << 8 |
Buffer[Cursor++] << 16 |
Buffer[Cursor++] << 24 |
Buffer[Cursor++] << 32 |
Buffer[Cursor++] << 40 |
Buffer[Cursor++] << 48 |
Buffer[Cursor++] << 56);
return true;
}
public bool ReadFlippedLong(out long pValue) // 5 6 7 8 1 2 3 4
{
pValue = 0;
if (Cursor + 8 > Length) return false;
pValue = (long)(
Buffer[Cursor++] << 32 |
Buffer[Cursor++] << 40 |
Buffer[Cursor++] << 48 |
Buffer[Cursor++] << 56 |
Buffer[Cursor++] |
Buffer[Cursor++] << 8 |
Buffer[Cursor++] << 16 |
Buffer[Cursor++] << 24);
return true;
}
public bool ReadDouble(out double pValue)
{
pValue = 0;
if (Cursor + 8 > Length) return false;
pValue = BitConverter.ToDouble(Buffer, Cursor);
Cursor += 8;
return true;
}
public bool ReadBytes(byte[] pBytes) { return ReadBytes(pBytes, 0, pBytes.Length); }
public bool ReadBytes(byte[] pBytes, int pStart, int pLength)
{
if (Cursor + pLength > Length) return false;
System.Buffer.BlockCopy(Buffer, Cursor, pBytes, pStart, pLength);
Cursor += pLength;
return true;
}
public bool ReadPaddedString(out string pValue, int pLength)
{
pValue = "";
if (Cursor + pLength > Length) return false;
int length = 0;
while (length < pLength && Buffer[Cursor + length] != 0x00) ++length;
if (length > 0) pValue = Encoding.ASCII.GetString(Buffer, Cursor, length);
Cursor += pLength;
return true;
}
}
}