forked from PKISolutions/Asn1Editor.WPF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Asn1Lite.cs
192 lines (185 loc) · 6.62 KB
/
Asn1Lite.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
189
190
191
192
using System;
using System.Windows;
using SysadminsLV.Asn1Editor.API.Utils;
using SysadminsLV.Asn1Editor.API.Utils.ASN;
using SysadminsLV.Asn1Editor.API.ViewModel;
using SysadminsLV.Asn1Editor.Properties;
using SysadminsLV.Asn1Parser;
namespace SysadminsLV.Asn1Editor.API.ModelObjects {
public class Asn1Lite : ViewModelBase {
Byte tag, unusedBits;
Boolean invalidData;
Int32 offset, offsetChange;
Int32 payloadLength, deepness;
String tagName, explicitValue, treePath;
public Asn1Lite(Asn1Reader asn) {
Offset = asn.Offset;
Tag = asn.Tag;
TagName = asn.TagName;
PayloadLength = asn.PayloadLength;
PayloadStartOffset = asn.PayloadStartOffset;
IsContainer = asn.IsConstructed;
Deepness = 0;
Path = String.Empty;
IsRoot = true;
try {
if (!asn.IsConstructed) {
ExplicitValue = AsnDecoder.GetViewValue(asn);
}
} catch {
InvalidData = true;
}
}
public Asn1Lite(Asn1Reader root, Asn1TreeNode tree, Int32 index) {
Offset = root.Offset;
Tag = root.Tag;
TagName = root.TagName;
PayloadLength = root.PayloadLength;
PayloadStartOffset = root.PayloadStartOffset;
IsContainer = root.IsConstructed;
Deepness = tree.Value.Deepness + 1;
Path = tree.Value.Path + "/" + index;
if (Tag == (Byte)Asn1Type.BIT_STRING) {
if (root.PayloadLength > 0) UnusedBits = root.RawData[root.PayloadStartOffset];
}
if (!root.IsConstructed) {
try {
ExplicitValue = AsnDecoder.GetViewValue(root);
} catch {
InvalidData = true;
}
}
}
public String Caption {
get {
String value = String.IsNullOrEmpty(explicitValue) || !Settings.Default.DecodePayload
? String.Empty
: " : " + explicitValue;
String value2 = IsContainer && Tag == (Byte)Asn1Type.BIT_STRING
? " Unused bits: " + UnusedBits
: String.Empty;
return IsContainer
? $"({Offset}, {PayloadLength}) {TagName}{value2.TrimEnd()}"
: $"({Offset}, {PayloadLength}) {TagName}{value.TrimEnd()}";
}
}
public Byte Tag {
get => tag;
set {
tag = value;
OnPropertyChanged("Tag");
}
}
public Byte UnusedBits {
get => unusedBits;
set {
unusedBits = value;
OnPropertyChanged("UnusedBits");
OnPropertyChanged("Caption");
}
}
public String TagName {
get => tagName;
set {
tagName = value;
if (value.StartsWith("CONTEXT SPECIFIC")) { IsContextSpecific = true; }
OnPropertyChanged("Caption");
}
}
public Int32 Offset {
get => offset;
set {
offset = value;
OnPropertyChanged("Caption");
}
}
public Int32 OffsetChange {
get => offsetChange;
set {
if (offsetChange == value) { return; }
offsetChange = value;
OnPropertyChanged("OffsetChange");
}
}
public Int32 PayloadStartOffset { get; set; }
public Int32 HeaderLength => PayloadStartOffset - Offset;
public Int32 PayloadLength {
get => payloadLength;
set {
payloadLength = value;
OnPropertyChanged("Caption");
}
}
public Int32 TagLength => HeaderLength + PayloadLength;
public Boolean IsContainer { get; set; }
public Boolean IsContextSpecific { get; set; }
public Boolean IsRoot { get; set; }
public Boolean InvalidData {
get => invalidData;
set {
invalidData = value;
OnPropertyChanged("InvalidData");
}
} //TODO
public Int32 Deepness {
get => deepness;
set {
deepness = value;
OnPropertyChanged("Caption");
}
}
public String Path {
get => treePath;
set {
treePath = value;
OnPropertyChanged("Caption");
}
}
public String ExplicitValue {
get => explicitValue;
set {
explicitValue = value;
OnPropertyChanged("Caption");
}
}
public void UpdateView() {
OnPropertyChanged("Caption");
}
public Boolean ValidateValue(String newValue, Byte unused) {
Byte[] binValue;
try {
binValue = AsnDecoder.EncodeGeneric(Tag, newValue, unused);
} catch (Exception e) {
Tools.MsgBox("Error", e.Message);
return false;
}
if (Tag == (Byte)Asn1Type.BIT_STRING) { UnusedBits = unused; }
Asn1Reader asn = new Asn1Reader(binValue);
UpdateBinaryCopy(binValue);
PayloadStartOffset = Offset + asn.TagLength - asn.PayloadLength;
ExplicitValue = AsnDecoder.GetViewValue(asn);
OffsetChange = asn.PayloadLength - PayloadLength;
PayloadLength = asn.PayloadLength;
((MainWindowVM)Application.Current.MainWindow.DataContext).HexViewerContext.BuildHexView(null);
return true;
}
public override Boolean Equals(Object obj) {
if (ReferenceEquals(null, obj)) { return false; }
if (ReferenceEquals(this, obj)) { return true; }
return obj.GetType() == typeof (Asn1Lite) && Equals((Asn1Lite) obj);
}
protected Boolean Equals(Asn1Lite other) {
return offset == other.offset && tag == other.tag;
}
public override Int32 GetHashCode() {
unchecked {
return (offset * 397) ^ tag.GetHashCode();
}
}
void UpdateBinaryCopy(Byte[] newBytes) {
MainWindowVM.RawData.RemoveRange(Offset, TagLength);
MainWindowVM.RawData.InsertRange(Offset, newBytes);
//((MainWindowVM)Application.Current.MainWindow.DataContext).HexViewerContext.BuildHexView(null);
}
}
}