This repository has been archived by the owner on Dec 10, 2021. It is now read-only.
forked from svendahl/cap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
143 lines (110 loc) · 2.45 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
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
using System;
namespace cap
{
class Program
{
public static void Main(string[] args)
{
if (!ValidInput(args))
{
PrintUsage();
System.Environment.Exit(-1);
}
string command = args[0];
string loadname = args[1];
string savename = args[2];
var input = LoadFile(loadname);
var output = new byte[0];
if (command == "e")
{
output = cap.encode(input);
}
else if (command == "d")
{
output = cap.decode(input);
}
SaveFile(output, savename);
}
private static byte[] LoadFile(string filename)
{
if (!System.IO.File.Exists(filename))
{
System.Console.WriteLine("File not found");
System.Environment.Exit(-1);
}
var fs = System.IO.File.OpenRead(filename);
try
{
if (fs.Length <= 2)
{
System.Console.WriteLine("File too small");
System.Environment.Exit(-1);
}
else if (fs.Length > 65538)
{
System.Console.WriteLine("File too large");
System.Environment.Exit(-1);
}
var data = new byte[fs.Length];
fs.Read(data, 0, (int)fs.Length);
return data;
}
catch (System.Exception e)
{
System.Console.WriteLine("Exception: {0}", e);
}
finally
{
fs.Close();
}
return new byte[0];
}
private static void SaveFile(byte[] input, string filename)
{
if (filename.Length < 1)
{
System.Console.WriteLine("No filename");
System.Environment.Exit(-1);
}
if (input.Length < 1)
{
System.Console.WriteLine("No data to save");
System.Environment.Exit(-1);
}
if (System.IO.File.Exists(filename))
{
System.IO.File.Delete(filename);
}
var fs = System.IO.File.OpenWrite(filename);
try
{
fs.Write(input, 0, input.Length);
}
catch (System.Exception e)
{
System.Console.WriteLine("Exception: {0}", e);
}
finally
{
fs.Close();
}
}
private static void PrintUsage()
{
System.Console.WriteLine("cap v1.2");
System.Console.WriteLine();
System.Console.WriteLine("Usage: cap d|e infile outfile");
}
private static bool ValidInput(string[] input)
{
return (
input.Length == 3 &&
input[0].Length == 1 &&
(input[0] == "e" || input[0] == "d") &&
System.IO.File.Exists(input[1]) &&
//!System.IO.File.Exists(input[2]) &&
!input[1].Equals(input[2])
);
}
}
}