-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppForm.cs
169 lines (147 loc) · 5.71 KB
/
AppForm.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
using BinViewer.Utils;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace BinViewer
{
public partial class AppForm : Form
{
private bool _fileOpen = false;
private byte[] _file;
private bool _started = false;
public AppForm()
{
InitializeComponent();
}
private void MainFormLoad(object sender, EventArgs e)
{
cboViewMode.SelectedIndex = 4;
cboBytesByLine.SelectedIndex = 3;
cboBytesByGroup.SelectedIndex = 1;
tsslFileName.Text = "No file";
tsslViewMode.Text = lblViewMode.Text + ": " + cboViewMode.Items[cboViewMode.SelectedIndex].ToString();
tsslBytesByLine.Text = lblBytesByLine.Text + ": " + cboBytesByLine.Items[cboBytesByLine.SelectedIndex].ToString();
tsslBytesByGroup.Text = lblBytesByGroup.Text + ": " + cboBytesByGroup.Items[cboBytesByGroup.SelectedIndex].ToString();
_started = true;
}
private void OpenFile(object sender, EventArgs e)
{
openFileDialog.ShowDialog();
}
[Obsolete]
private void CloseFile(object sender, EventArgs e)
{
rtbFileViewer.Text = "";
_fileOpen = false;
tsslFileName.Text = "No file";
}
private void SelectedIndexChanged(object sender, EventArgs e)
{
if (!_started) return;
tsslViewMode.Text = lblViewMode.Text + ": " + cboViewMode.Items[cboViewMode.SelectedIndex].ToString();
tsslBytesByLine.Text = lblBytesByLine.Text + ": " + cboBytesByLine.Items[cboBytesByLine.SelectedIndex].ToString();
tsslBytesByGroup.Text = lblBytesByGroup.Text + ": " + cboBytesByGroup.Items[cboBytesByGroup.SelectedIndex].ToString();
if (sender == cboViewMode)
{
int i = cboViewMode.SelectedIndex;
if (i == Constants.ASCII || i == Constants.UTF8 || i == Constants.BASE64)
{
cboBytesByLine.Enabled = false;
cboBytesByGroup.Enabled = false;
tsslBytesByLine.Text = lblBytesByLine.Text + ": -";
tsslBytesByGroup.Text = lblBytesByGroup.Text + ": -";
}
else
{
cboBytesByLine.Enabled = true;
cboBytesByGroup.Enabled = true;
}
}
if (cboBytesByGroup.SelectedIndex > cboBytesByLine.SelectedIndex)
{
cboBytesByGroup.SelectedIndex = cboBytesByLine.SelectedIndex;
}
if (_fileOpen)
{
ShowFileUpdate();
}
}
private void LoadFile(object sender, CancelEventArgs e)
{
_file = File.ReadAllBytes(openFileDialog.FileName);
var sp = openFileDialog.FileName.Split('\\');
var name = sp[sp.Length - 1];
tsslFileName.Text = "File name: " + name;
_fileOpen = true;
ShowFileUpdate();
}
private void ShowFileUpdate()
{
int viewMode = cboViewMode.SelectedIndex;
int bytesByLine = int.Parse(cboBytesByLine.Items[cboBytesByLine.SelectedIndex].ToString()[0].ToString());
int byteGroupSize = int.Parse(cboBytesByGroup.Items[cboBytesByGroup.SelectedIndex].ToString()[0].ToString());
string newText = "";
switch (viewMode)
{
case Constants.BIT:
{
newText = CustomEncoding.ToBitString(_file, bytesByLine, byteGroupSize);
break;
}
case Constants.BYTE:
{
newText = CustomEncoding.ToByteString(_file, bytesByLine, byteGroupSize);
break;
}
case Constants.OCTAL:
{
newText = CustomEncoding.ToOctalString(_file, bytesByLine, byteGroupSize);
break;
}
case Constants.DECIMAL:
{
newText = CustomEncoding.ToDecimalString(_file, bytesByLine, byteGroupSize);
break;
}
case Constants.HEXADECIMAL:
{
newText = CustomEncoding.ToHexadecimalString(_file, bytesByLine, byteGroupSize);
break;
}
case Constants.ASCII:
{
newText += Encoding.ASCII.GetString(_file, 0, _file.Length);
break;
}
case Constants.UTF8:
{
newText += Encoding.UTF8.GetString(_file, 0, _file.Length);
break;
}
case Constants.BASE64:
{
newText += Convert.ToBase64String(_file);
break;
}
}
//lc-3 = big endian
//windows = little endian
rtbFileViewer.Text = newText;
}
private void tsmiNumericBaseConverter_Click(object sender, EventArgs e)
{
new NumericBaseConverterForm().ShowDialog();
}
private void tsmiAbout_Click(object sender, EventArgs e)
{
Process.Start("https://github.com/carlosebmachado/BinViewer");
}
private void tsmiExit_Click(object sender, EventArgs e)
{
Close();
}
}
}