-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProgramDec.cs
executable file
·242 lines (203 loc) · 8.94 KB
/
ProgramDec.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// YCC_encoder//YCC_decoder V 1.3.1 build 128
// (c) S. Manzhulovsky KIT-24B NTU "KPI" 2010
// create 24.11.2009
// modified 22.01.2010
using System;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
namespace YCC_encoder
{
class ProgramDec
{
static int exitCode = 0;
// вывод помощи
static void WriteHelp()
{
Console.WriteLine("YCC decoder V 1.3.1");
Console.WriteLine("");
Console.WriteLine("Usage:");
Console.WriteLine("");
Console.WriteLine("YCC_decoder Source [Flag(s)]");
Console.WriteLine("");
Console.WriteLine("Source - is a file, an image will decompress from which.");
Console.WriteLine("Flags:");
Console.WriteLine("-d(ebug) - debug mode: showing the additional information.");
Console.WriteLine("-nocrc - ignore CRC-check errors.");
Console.WriteLine("-p%PASSWORD% - if data was protected in file by %PASSWORD%.");
}
// основная функция чтения файла
static int Main(string[] args)
{
// Если нехватает параметров - выводим "помощь"
if (args.Length < 1)
{
WriteHelp();
return 2;
}
bool debug = false;
string pass = "";
bool ch_crc = true;
foreach (string s in args)
{
if (s.ToUpper() == "-NOCRC" || s.ToUpper() == "-NCRC") ch_crc = false; // Отключение контрольной суммы
if (s.ToUpper() == "-DEBUG" || s.ToUpper() == "-D") debug = true; // Включение отладочной информации
if (s.ToUpper() == "-H" || s.ToUpper() == "-?" || s.ToUpper() == "/?") { WriteHelp(); return 0; }
// вывод "помощи"
// указание пароля
if (s.Substring(0, 2).ToUpper() == "-P")
{
pass = s.Substring(2);
}
}
// пробуем читать файл
System.IO.FileStream fs;
try
{
fs = new System.IO.FileStream(args[0], System.IO.FileMode.Open);
}
catch { return 2; }
byte[] IBytes = new byte[4];
fs.Read(IBytes, 0, 4);
#region V2
// для формата версии 2
if (IBytes[3] == 2)
{
fs.Read(IBytes, 0, 4);
int chanel = BitConverter.ToInt32(IBytes, 0);
fs.Read(IBytes, 0, 4);
int width = BitConverter.ToInt32(IBytes, 0);
fs.Read(IBytes, 0, 4);
int height = BitConverter.ToInt32(IBytes, 0);
fs.Read(IBytes, 0, 4);
int widthR = BitConverter.ToInt32(IBytes, 0);
fs.Read(IBytes, 0, 4);
YCrCb q;
// для наличия отсутствия фона
if (IBytes[0] == 1)
q = new YCrCb(chanel, width, height, widthR, new YCrCb.YCrCbPoint(IBytes[1], IBytes[2], IBytes[3]));
else
q = new YCrCb(chanel, width, height, widthR, null);
if (debug)
{
Console.WriteLine("File version 2");
Console.WriteLine("W:" + widthR + " H:" + height);
if (IBytes[0] == 1)
Console.WriteLine("Base color present.");
else
Console.WriteLine("Without base color.");
}
int yy = 0;
while (true)
{
try
{
// читаем маркер
int y = fs.Read(IBytes, 0, 4);
if (y == 0) break;
int QL = BitConverter.ToInt32(IBytes, 0);
if (QL > 0)
{
// читаем фрагмент
byte[] Zcmpr = new byte[QL];
fs.Read(Zcmpr, 0, QL);
if (debug)
{
yy++;
Console.WriteLine();
Console.WriteLine("Ch#" + yy);
Console.WriteLine("Size in file " + QL + " bytes");
}
q.setZoneBytes(Zcmpr, debug);
}
}
catch { exitCode = 3; break; }
}
q.YCrCb2RGB();
q.writeBMP(args[0] + ".BMP");
return exitCode;
}
#endregion V2
#region V3-4
// для формата версии 3
if (IBytes[3] == 3 || IBytes[3] == 4)
{
int VV = IBytes[3];
fs.Read(IBytes, 0, 4);
int chanel = BitConverter.ToInt32(IBytes, 0);
fs.Read(IBytes, 0, 4);
int width = BitConverter.ToInt32(IBytes, 0);
fs.Read(IBytes, 0, 4);
int height = BitConverter.ToInt32(IBytes, 0);
fs.Read(IBytes, 0, 4);
int widthR = BitConverter.ToInt32(IBytes, 0);
fs.Read(IBytes, 0, 4);
YCrCb q;
// с наличием или отсутствием фона
if (IBytes[0] == 1)
q = new YCrCb(chanel, width, height, widthR, new YCrCb.YCrCbPoint(IBytes[1], IBytes[2], IBytes[3]));
else
q = new YCrCb(chanel, width, height, widthR, null);
if (debug)
{
Console.WriteLine("File version " + VV);
if (VV == 4) Console.WriteLine("Crypted");
Console.WriteLine("W:" + widthR + " H:" + height);
if (IBytes[0] == 1)
Console.WriteLine("Base color present.");
else
Console.WriteLine("Without base color.");
}
_Rijndael crpt = new _Rijndael();
crpt.Key = pass;
int yy = 0;
while (true)
{
try
{
// чтение маркера фрагмента (длины)
int y = fs.Read(IBytes, 0, 4);
if (y == 0) break;
int QL = BitConverter.ToInt32(IBytes, 0);
if (QL > 0)
{
// чтение фрагмента
byte[] Zcmpr = new byte[QL];
fs.Read(Zcmpr, 0, QL);
if (QL > 16)
{
if (debug)
{
yy++;
Console.WriteLine();
Console.WriteLine("Ch#" + yy);
Console.WriteLine("Size in file " + QL + " bytes");
}
if (VV == 4)
{
// при включенном шифровании
int yyy = q.setZoneBytesV3(crpt.Decrypt(Zcmpr), debug, VV, ch_crc);
if (exitCode == 0 && yyy != 0) exitCode = yyy;
}
else
{
// при отключенном шифровании
int yyy = q.setZoneBytesV3(Zcmpr, debug, VV, ch_crc);
if (exitCode == 0 && yyy != 0) exitCode = yyy;
}
}
}
}
catch { break; }
}
q.YCrCb2RGB();
q.writeBMP(args[0] + ".BMP");
return exitCode;
}
#endregion V3-4
// сообщение об ошибке
Console.WriteLine("The file contains the unsupported version");
return 1;
}
}
}