-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSAZ-DotNetZip.cs
More file actions
352 lines (306 loc) · 9.87 KB
/
SAZ-DotNetZip.cs
File metadata and controls
352 lines (306 loc) · 9.87 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#if SAZ_SUPPORT
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Ionic.Zip;
using System.Diagnostics;
namespace Fiddler
{
class DNZSAZProvider : ISAZProvider
{
// This is Func<String> but we don't have that in .NET2.0
public delegate string ObtainPasswordDelegate();
public static ObtainPasswordDelegate fnObtainPwd
{
get;
set;
}
public ISAZWriter CreateSAZ(string sFilename)
{
return new DNZSAZWriter(sFilename);
}
public ISAZReader LoadSAZ(string sFilename)
{
return new DNZSAZReader(sFilename);
}
public bool BufferLocally
{
get
{
return false;
}
}
public bool SupportsEncryption
{
get
{
return true;
}
}
}
class DNZSAZReader : ISAZReader
{
private ZipFile _oZip;
private string _sFilename;
private string _sPassword;
public string Filename
{
get
{
return _sFilename;
}
}
public void Close()
{
_oZip.Dispose();
_oZip = null;
}
public string Comment
{
get
{
return _oZip.Comment;
}
}
string _EncryptionMethod;
public string EncryptionMethod
{
get
{
return _EncryptionMethod;
}
}
string _EncryptionStrength;
public string EncryptionStrength
{
get
{
return _EncryptionStrength;
}
}
private bool PromptForPassword()
{
DNZSAZProvider.ObtainPasswordDelegate fnGetPwd = DNZSAZProvider.fnObtainPwd;
if (null == fnGetPwd) return false;
_sPassword = fnGetPwd.Invoke();
if (!String.IsNullOrEmpty(_sPassword))
{
_oZip.Password = _sPassword;
return true;
}
return false;
}
public Stream GetFileStream(string sFilename)
{
ZipEntry oZE = _oZip[sFilename];
if (null == oZE)
{
return null;
}
if ((oZE.UsesEncryption) && String.IsNullOrEmpty(_sPassword))
{
StoreEncryptionInfo(oZE.Encryption);
if (!PromptForPassword()) { throw new OperationCanceledException("Password required."); }
}
Stream strmResult = null;
RetryWithPassword:
try
{
strmResult = oZE.OpenReader();
}
catch (Ionic.Zip.BadPasswordException)
{
if (!PromptForPassword()) { throw new OperationCanceledException("Password required."); }
goto RetryWithPassword;
}
catch (Exception eX)
{
Debug.Assert(false, eX.Message);
FiddlerApplication.ReportException(eX, "Error saving SAZ");
}
return strmResult;
}
private void StoreEncryptionInfo(EncryptionAlgorithm oEA)
{
switch (oEA)
{
case EncryptionAlgorithm.PkzipWeak:
_EncryptionMethod = "PKZip";
_EncryptionStrength = "56"; // Is that right?
break;
case EncryptionAlgorithm.WinZipAes128:
_EncryptionMethod = "WinZipAes";
_EncryptionStrength = "128";
break;
case EncryptionAlgorithm.WinZipAes256:
_EncryptionMethod = "WinZipAes";
_EncryptionStrength = "256";
break;
default:
Debug.Assert(false, "Unknown encryption algorithm");
_EncryptionMethod = "Unknown";
_EncryptionStrength = "0";
break;
}
}
public byte[] GetFileBytes(string sFilename)
{
Stream strmBytes = this.GetFileStream(sFilename);
if (strmBytes == null) return null;
byte[] arrData = Utilities.ReadEntireStream(strmBytes);
strmBytes.Close();
return arrData;
}
public string[] GetRequestFileList()
{
List<string> listFiles = new List<string>();
// TODO: Use (faster?) _oZip.SelectEntries method instead
foreach (ZipEntry oZE in _oZip)
{
if (!oZE.FileName.EndsWith("_c.txt", StringComparison.OrdinalIgnoreCase) || !oZE.FileName.StartsWith("raw/", StringComparison.OrdinalIgnoreCase))
{
// Not a request. Skip it.
continue;
}
listFiles.Add(oZE.FileName);
}
return listFiles.ToArray();
}
internal DNZSAZReader(string sFilename)
{
_sFilename = sFilename;
_oZip = new ZipFile(sFilename);
foreach (string s in _oZip.EntryFileNames)
{
Trace.WriteLine(s);
}
/*if (!_oZip.EntryFileNames.Contains("raw/"))
{
throw new Exception("The selected file is not a Fiddler-generated .SAZ archive of Web Sessions.");
}*/
}
}
class DNZSAZWriter : ISAZWriter
{
private ZipFile _oZip;
private string _sFilename;
internal DNZSAZWriter(string sFilename)
{
_sFilename = sFilename;
_oZip = new ZipFile(sFilename);
// Create the directory explicitly (not strictly required) because this matches
// legacy behavior and some code checks for it.
_oZip.AddDirectoryByName("raw");
}
private string _EncryptionMethod;
public string EncryptionMethod
{
get
{
if (String.IsNullOrEmpty(_EncryptionMethod)) StoreEncryptionInfo(_oZip.Encryption);
return _EncryptionMethod;
}
}
private void StoreEncryptionInfo(EncryptionAlgorithm oEA)
{
switch (oEA)
{
case EncryptionAlgorithm.PkzipWeak:
_EncryptionMethod = "PKZip";
_EncryptionStrength = "56"; // Is that right?
break;
case EncryptionAlgorithm.WinZipAes128:
_EncryptionMethod = "WinZipAes";
_EncryptionStrength = "128";
break;
case EncryptionAlgorithm.WinZipAes256:
_EncryptionMethod = "WinZipAes";
_EncryptionStrength = "256";
break;
default:
Debug.Assert(false, "Unknown encryption algorithm");
_EncryptionMethod = "Unknown";
_EncryptionStrength = "0";
break;
}
}
private string _EncryptionStrength;
public string EncryptionStrength
{
get
{
if (String.IsNullOrEmpty(_EncryptionStrength)) StoreEncryptionInfo(_oZip.Encryption);
return _EncryptionStrength;
}
}
public void AddFile(string sFilename, SAZWriterDelegate oSWD)
{
Ionic.Zip.WriteDelegate oWD = (sFN, oS) =>
{
oSWD.Invoke(oS);
};
_oZip.AddEntry(sFilename, oWD );
}
/// <summary>
/// Writes the ContentTypes XML to the ZIP so Packaging APIs can read it.
/// See http://en.wikipedia.org/wiki/Open_Packaging_Conventions
/// </summary>
/// <param name="odfZip"></param>
private void WriteODCXML()
{
const string oPFContentTypeXML =
"<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n<Types xmlns=\"http://schemas.openxmlformats.org/package/2006/content-types\">\r\n" +
"<Default Extension=\"htm\" ContentType=\"text/html\" />\r\n<Default Extension=\"xml\" ContentType=\"application/xml\" />\r\n<Default Extension=\"txt\" ContentType=\"text/plain\" />\r\n</Types>";
_oZip.AddEntry("[Content_Types].xml", new WriteDelegate(delegate(string sn, Stream strmToWrite)
{
byte[] arrODCXML = Encoding.UTF8.GetBytes(oPFContentTypeXML);
strmToWrite.Write(arrODCXML, 0, arrODCXML.Length);
}));
}
public bool CompleteArchive()
{
WriteODCXML();
_oZip.Save();
_oZip = null;
return true;
}
public string Filename
{
get { return _sFilename; }
}
public string Comment
{
get
{
return _oZip.Comment;
}
set
{
_oZip.Comment = value;
}
}
public bool SetPassword(string sPassword)
{
#region PasswordProtectIfNeeded
if (!String.IsNullOrEmpty(sPassword))
{
if (CONFIG.bUseAESForSAZ)
{
if (FiddlerApplication.Prefs.GetBoolPref("fiddler.saz.AES.Use256Bit", false))
{
_oZip.Encryption = EncryptionAlgorithm.WinZipAes256;
}
else
{
_oZip.Encryption = EncryptionAlgorithm.WinZipAes128;
}
}
_oZip.Password = sPassword;
}
#endregion PasswordProtectIfNeeded
return true;
}
}
}
#endif