-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrmKuid.vb
More file actions
199 lines (163 loc) · 6.76 KB
/
frmKuid.vb
File metadata and controls
199 lines (163 loc) · 6.76 KB
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
189
190
191
192
193
194
195
196
197
198
199
Imports System.Globalization
Public Class frmKuid
''' <summary>
''' Translates hexadecimal kuids to string
''' </summary>
''' <param name="kuid">Byte-array kuid</param>
''' <returns>Kuid string</returns>
Function HexToKuid(ByVal kuid As Byte()) As String
Dim rstring As String = "<kuid"
Dim ubytes(3), cbytes(3) As Byte
Dim version As Integer
Dim uInt As Integer
Array.Copy(kuid, 0, ubytes, 0, 4)
Array.Copy(kuid, 4, cbytes, 0, 4)
If (ubytes(3) And (1 << 0)) <> 0 Then
'negative user id, ignore version
version = 0
Else
version = Convert.ToInt32(ubytes(3) >> 1)
ubytes(3) = ubytes(3) And &H1
End If
uInt = BitConverter.ToInt32(ubytes, 0)
If version <> 0 Then
rstring = rstring & "2:"
Else
rstring = rstring & ":"
End If
rstring = rstring & uInt
rstring = rstring & ":"
rstring = rstring & BitConverter.ToInt32(cbytes, 0)
If version <> 0 Then
rstring = rstring & ":" & version
End If
rstring = rstring & ">"
Return rstring
End Function
''' <summary>
''' Translated string kuid to 8 byte kuid (for routes/sessions)
''' </summary>
''' <param name="kuid">The kuid as string</param>
''' <returns>The kuid as 8 bytes</returns>
''' <remarks></remarks>
Function KuidToHex(ByVal kuid As String) As Byte()
Dim num(2) As Integer 'variable to store kuid parts
Dim ubytes(3), cbytes(3) As Byte 'user bytes and content bytes
Dim str() As String 'split kuid to tokens
Dim rbytes(7) As Byte 'returned bytes
If kuid = "" Then
'MessageBox.Show("NULL kuid encountered!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
'err = True
Return {0, 0, 0, 0, 0, 0, 0, 0}
End If
str = System.Text.RegularExpressions.Regex.Split(kuid, ":")
If str.Length < 3 Then
'MessageBox.Show("Invalid kuid encountered!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return {0, 0, 0, 0, 0, 0, 0, 0}
End If
Try
'parse the kuid parts (skip first) and convert to integers
For i As Integer = 1 To IIf(str.Length > 4, 4, str.Length) - 1
num(i - 1) = Val(str(i))
Next
'get the bytes from integers
ubytes = BitConverter.GetBytes(num(0))
cbytes = BitConverter.GetBytes(num(1))
If (num(2) > 0 AndAlso num(2) < 128 AndAlso num(0) >= 0) Then 'check if uid is negative
ubytes(3) = ubytes(3) Xor (CByte(num(2)) << 1) 'add the version number to byte 4 of uid
End If
'merge bytes
For i As Integer = 0 To 3
rbytes(i) = ubytes(i)
rbytes(i + 4) = cbytes(i)
Next
Catch ex As Exception
MessageBox.Show(ex.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Return rbytes
End Function
''' <summary>
''' Computes the hash of a kuid
''' </summary>
''' <param name="kuid">Kuid to be hashed</param>
''' <returns>One byte - hash code</returns>
''' <remarks></remarks>
Function computeHash(ByVal kuid As Byte()) As Byte()
Dim hash As Byte() = {&H0}
Try
'calculate hash using xor
For i As Integer = 0 To 7
hash(0) = hash(0) Xor kuid(i)
Next
'version needs to be ignored, so xor that out, only if uid is positive
If (kuid(3) And (1 << 0)) = 0 Then
hash(0) = hash(0) Xor kuid(3)
End If
Catch ex As Exception
MessageBox.Show(ex.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Return hash
End Function
''' <summary>
''' Converts hex string to byte array
''' </summary>
''' <param name="t">String to be parsed</param>
''' <returns>8 byte array</returns>
Function ConvertHex(ByVal t As String) As Byte()
Dim data(7) As Byte
Dim s() As String = t.Split({" "c, "-"c, ":"c, ","c})
'data = New Byte(s.Length - 1) {}
For i As Integer = 0 To s.Length - 1
If Not Byte.TryParse(s(i), NumberStyles.HexNumber, CultureInfo.CurrentCulture, data(i)) Then
data(i) = 0
'MsgBox("error converting!", MsgBoxStyle.Information)
End If
Next
Return data
End Function
''' <summary>
''' Reverses the UID with the CID (for map files)
''' </summary>
''' <param name="kuid">The kuid as 8 bytes</param>
''' <returns>The reversed kuid as 8 bytes</returns>
''' <remarks></remarks>
Function revKuid(ByVal kuid As Byte()) As Byte()
Dim rbytes(7) As Byte 'returned bytes
Try
'reverse bytes (a-b-c-d-e-f-g-h -> e-f-g-h-a-b-c-d)
For i As Integer = 0 To 3
rbytes(i) = kuid(i + 4)
rbytes(i + 4) = kuid(i)
Next
Catch ex As Exception
MessageBox.Show(ex.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Return rbytes
End Function
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles txtKuid.TextChanged
If txtKuid.Focused = True Then
txtHash.Text = "hash-" & BitConverter.ToString(computeHash(KuidToHex(txtKuid.Text)))
If CheckBox1.Checked = True Then
txtHex.Text = BitConverter.ToString(revKuid(KuidToHex(txtKuid.Text))).Replace("-"c, " "c)
Else
txtHex.Text = BitConverter.ToString(KuidToHex(txtKuid.Text)).Replace("-"c, " "c)
End If
End If
End Sub
Private Sub LinkLabel1_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
Process.Start("http://vvmm.freeforums.org/")
End Sub
Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles txtHex.TextChanged
If txtHex.Focused = True Then
If CheckBox1.Checked = True Then
txtKuid.Text = HexToKuid(revKuid(ConvertHex(txtHex.Text)))
Else
txtKuid.Text = HexToKuid(ConvertHex(txtHex.Text))
End If
txtHash.Text = "hash-" & BitConverter.ToString(computeHash(KuidToHex(txtKuid.Text)))
End If
End Sub
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
txtHex.Text = BitConverter.ToString(revKuid(ConvertHex(txtHex.Text))).Replace("-"c, " "c)
End Sub
End Class