Skip to content

Commit 168cc75

Browse files
author
David Pierson
committed
Added #if directives around AES code for CF and 1.1 frameworks
1 parent 6a5f4b3 commit 168cc75

File tree

8 files changed

+149
-115
lines changed

8 files changed

+149
-115
lines changed

mkDistribution.bat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ if exist current (
66

77
mkdir current
88

9-
mkdir current\netcf-10
10-
nant -t:netcf-1.0 -D:build.output.dir=current\netcf-10 -buildfile:sharpZLib.build build
9+
REM mkdir current\netcf-10
10+
REM nant -t:netcf-1.0 -D:build.output.dir=current\netcf-10 -buildfile:sharpZLib.build build
1111

1212
mkdir current\netcf-20
1313
nant -t:netcf-2.0 -D:build.output.dir=current\netcf-20 -buildfile:sharpZLib.build build

src/BZip2/BZip2.cs

Lines changed: 46 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// BZip2.cs
22
//
3-
// Copyright (C) 2001 Mike Krueger
3+
// Copyright (C) 2010 David Pierson
44
//
55
// This program is free software; you can redistribute it and/or
66
// modify it under the terms of the GNU General Public License
@@ -33,94 +33,73 @@
3333
// obligated to do so. If you do not wish to do so, delete this
3434
// exception statement from your version.
3535

36+
// Suppress this in CF and 1.1, not needed. Static classes introduced in C# version 2.0
37+
#if !NETCF_2_0 && !NET_1_1
38+
3639
using System;
3740
using System.IO;
3841

39-
namespace ICSharpCode.SharpZipLib.BZip2
40-
{
42+
namespace ICSharpCode.SharpZipLib.BZip2 {
4143

4244
/// <summary>
43-
/// A helper class to simplify compressing and decompressing streams.
45+
/// An example class to demonstrate compression and decompression of BZip2 streams.
4446
/// </summary>
45-
public sealed class BZip2
47+
public static class BZip2
4648
{
4749
/// <summary>
48-
/// Decompress <paramref name="inStream">input</paramref> writing
49-
/// decompressed data to the <paramref name="outStream">output stream</paramref>
50+
/// Decompress the <paramref name="inStream">input</paramref> writing
51+
/// uncompressed data to the <paramref name="outStream">output stream</paramref>
5052
/// </summary>
51-
/// <param name="inStream">The stream containing data to decompress.</param>
52-
/// <param name="outStream">The stream to write decompressed data to.</param>
53-
/// <remarks>Both streams are closed on completion</remarks>
54-
public static void Decompress(Stream inStream, Stream outStream)
53+
/// <param name="inStream">The readable stream containing data to decompress.</param>
54+
/// <param name="outStream">The output stream to receive the decompressed data.</param>
55+
/// <param name="isStreamOwner">Both streams are closed on completion if true.</param>
56+
public static void Decompress(Stream inStream, Stream outStream, bool isStreamOwner)
5557
{
56-
if ( inStream == null ) {
57-
throw new ArgumentNullException("inStream");
58-
}
59-
60-
if ( outStream == null ) {
61-
throw new ArgumentNullException("outStream");
58+
if (inStream == null || outStream == null) {
59+
throw new Exception("Null Stream");
6260
}
6361

64-
using ( outStream ) {
65-
using ( BZip2InputStream bzis = new BZip2InputStream(inStream) ) {
66-
int ch = bzis.ReadByte();
67-
while (ch != -1) {
68-
outStream.WriteByte((byte)ch);
69-
ch = bzis.ReadByte();
70-
}
62+
try {
63+
using (BZip2InputStream bzipInput = new BZip2InputStream(inStream)) {
64+
bzipInput.IsStreamOwner = isStreamOwner;
65+
Core.StreamUtils.Copy(bzipInput, outStream, new byte[4096]);
66+
}
67+
} finally {
68+
if (isStreamOwner) {
69+
// inStream is closed by the BZip2InputStream if stream owner
70+
outStream.Close();
7171
}
7272
}
7373
}
7474

7575
/// <summary>
76-
/// Compress <paramref name="inStream">input stream</paramref> sending
77-
/// result to <paramref name="outStream">output stream</paramref>
76+
/// Compress the <paramref name="inStream">input stream</paramref> sending
77+
/// result data to <paramref name="outStream">output stream</paramref>
7878
/// </summary>
79-
/// <param name="inStream">The stream to compress.</param>
80-
/// <param name="outStream">The stream to write compressed data to.</param>
81-
/// <param name="blockSize">The block size to use.</param>
82-
/// <remarks>Both streams are closed on completion</remarks>
83-
public static void Compress(Stream inStream, Stream outStream, int blockSize)
84-
{
85-
if ( inStream == null ) {
86-
throw new ArgumentNullException("inStream");
87-
}
88-
89-
if ( outStream == null ) {
90-
throw new ArgumentNullException("outStream");
79+
/// <param name="inStream">The readable stream to compress.</param>
80+
/// <param name="outStream">The output stream to receive the compressed data.</param>
81+
/// <param name="isStreamOwner">Both streams are closed on completion if true.</param>
82+
/// <param name="level">Block size acts as compression level (1 to 9) with 1 giving
83+
/// the lowest compression and 9 the highest.</param>
84+
public static void Compress(Stream inStream, Stream outStream, bool isStreamOwner, int level)
85+
{
86+
if (inStream == null || outStream == null) {
87+
throw new Exception("Null Stream");
9188
}
92-
93-
using ( inStream ) {
94-
using (BZip2OutputStream bzos = new BZip2OutputStream(outStream, blockSize)) {
95-
int ch = inStream.ReadByte();
96-
while (ch != -1) {
97-
bzos.WriteByte((byte)ch);
98-
ch = inStream.ReadByte();
99-
}
89+
90+
try {
91+
using (BZip2OutputStream bzipOutput = new BZip2OutputStream(outStream, level)) {
92+
bzipOutput.IsStreamOwner = isStreamOwner;
93+
Core.StreamUtils.Copy(inStream, bzipOutput, new byte[4096]);
94+
}
95+
} finally {
96+
if (isStreamOwner) {
97+
// outStream is closed by the BZip2OutputStream if stream owner
98+
inStream.Close();
10099
}
101100
}
102101
}
103102

104-
/// <summary>
105-
/// Initialise a default instance of this class.
106-
/// </summary>
107-
BZip2()
108-
{
109-
}
110103
}
111104
}
112-
113-
/* derived from a file which contained this license :
114-
* Copyright (c) 1999-2001 Keiron Liddle, Aftex Software
115-
*
116-
* This library is free software; you can redistribute it and/or
117-
* modify it under the terms of the GNU Lesser General Public
118-
* License as published by the Free Software Foundation; either
119-
* version 2.1 of the License, or (at your option) any later version.
120-
*
121-
* This library is distributed in the hope that it will be useful,
122-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
123-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
124-
* Lesser General Public License for more details.
125-
*
126-
*/
105+
#endif

src/Encryption/ZipAESStream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
// exception statement from your version.
3636
//
3737

38-
#if !NETCF_1_0
38+
#if !NET_1_1 && !NETCF_2_0
3939

4040
using System;
4141
using System.IO;

src/Encryption/ZipAESTransform.cs

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
// exception statement from your version.
3636
//
3737

38-
#if !NETCF_1_0
38+
#if !NET_1_1 && !NETCF_2_0
39+
// Framework version 2.0 required for Rfc2898DeriveBytes
3940

4041
using System;
4142
using System.Security.Cryptography;
@@ -47,7 +48,7 @@ namespace ICSharpCode.SharpZipLib.Encryption {
4748
/// </summary>
4849
public class ZipAESTransform : ICryptoTransform {
4950

50-
public const int PWD_VER_LENGTH = 2;
51+
private const int PWD_VER_LENGTH = 2;
5152

5253
// WinZip use iteration count of 1000 for PBKDF2 key generation
5354
private const int KEY_ROUNDS = 1000;
@@ -88,7 +89,6 @@ public ZipAESTransform(string key, byte[] saltBytes, int blockSize, bool writeMo
8889
_encryptBuffer = new byte[_blockSize];
8990
_encrPos = ENCRYPT_BLOCK;
9091

91-
// Needs .NET Framework version 2.0
9292
// Performs the equivalent of derive_key in Dr Brian Gladman's pwd2key.c
9393
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(key, saltBytes, KEY_ROUNDS);
9494
RijndaelManaged rm = new RijndaelManaged();
@@ -103,10 +103,6 @@ public ZipAESTransform(string key, byte[] saltBytes, int blockSize, bool writeMo
103103
_writeMode = writeMode;
104104
}
105105

106-
public void Dispose() {
107-
_encryptor.Dispose();
108-
}
109-
110106
/// <summary>
111107
/// Implement the ICryptoTransform method.
112108
/// </summary>
@@ -141,56 +137,83 @@ public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, b
141137
return inputCount;
142138
}
143139

140+
/// <summary>
141+
/// Returns the 2 byte password verifier
142+
/// </summary>
143+
public byte[] PwdVerifier {
144+
get {
145+
return _pwdVerifier;
146+
}
147+
}
148+
149+
/// <summary>
150+
/// Returns the 10 byte AUTH CODE to be checked or appended immediately following the AES data stream.
151+
/// </summary>
152+
public byte[] GetAuthCode() {
153+
// We usually don't get advance notice of final block. Hash requres a TransformFinal.
154+
if (!_finalised) {
155+
byte[] dummy = new byte[0];
156+
_hmacsha1.TransformFinalBlock(dummy, 0, 0);
157+
_finalised = true;
158+
}
159+
return _hmacsha1.Hash;
160+
}
161+
162+
#region ICryptoTransform Members
163+
164+
/// <summary>
165+
/// Not implemented.
166+
/// </summary>
144167
public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount) {
145168

146169
throw new NotImplementedException("ZipAESTransform.TransformFinalBlock");
147170
}
148171

172+
/// <summary>
173+
/// Gets the size of the input data blocks in bytes.
174+
/// </summary>
149175
public int InputBlockSize {
150176
get {
151177
return _blockSize;
152178
}
153179
}
154180

181+
/// <summary>
182+
/// Gets the size of the output data blocks in bytes.
183+
/// </summary>
155184
public int OutputBlockSize {
156185
get {
157186
return _blockSize;
158187
}
159188
}
160189

190+
/// <summary>
191+
/// Gets a value indicating whether multiple blocks can be transformed.
192+
/// </summary>
161193
public bool CanTransformMultipleBlocks {
162194
get {
163195
return true;
164196
}
165197
}
166198

199+
/// <summary>
200+
/// Gets a value indicating whether the current transform can be reused.
201+
/// </summary>
167202
public bool CanReuseTransform {
168203
get {
169204
return true;
170205
}
171206
}
172207

173208
/// <summary>
174-
/// Returns the 2 byte password verifier
209+
/// Cleanup internal state.
175210
/// </summary>
176-
public byte[] PwdVerifier {
177-
get {
178-
return _pwdVerifier;
179-
}
211+
public void Dispose() {
212+
_encryptor.Dispose();
180213
}
181214

182-
/// <summary>
183-
/// Returns the 10 byte AUTH CODE to be checked or appended immediately following the AES data stream.
184-
/// </summary>
185-
public byte[] GetAuthCode() {
186-
// We usually don't get advance notice of final block. Hash requres a TransformFinal.
187-
if (!_finalised) {
188-
byte[] dummy = new byte[0];
189-
_hmacsha1.TransformFinalBlock(dummy, 0, 0);
190-
_finalised = true;
191-
}
192-
return _hmacsha1.Hash;
193-
}
215+
#endregion
216+
194217
}
195218
}
196219
#endif

0 commit comments

Comments
 (0)