Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standard PK (weak) decryption support, with basis for future AES decryption support. #49

Merged
merged 6 commits into from
Jan 3, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 91 additions & 7 deletions zipzap.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions zipzap/ZZArchive.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
//

#import <Foundation/Foundation.h>
#import "ZZConstants.h"

/**
* The ZZArchive class represents a zip file for reading only.
Expand Down Expand Up @@ -101,7 +102,7 @@
* @param error The error information when an error occurs. Pass in nil if you do not want error information.
* @return Whether the load was successful or not.
*/
- (BOOL)load:(NSError**)error;
- (BOOL)load:(out NSError**)error;

@end

Expand All @@ -124,6 +125,6 @@
*
*/
- (BOOL)updateEntries:(NSArray*)newEntries
error:(NSError**)error;
error:(out NSError**)error;

@end
3 changes: 2 additions & 1 deletion zipzap/ZZArchive.mm
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ - (BOOL)load:(NSError**)error

ZZLocalFileHeader* nextLocalFileHeader = (ZZLocalFileHeader*)(beginContent
+ nextCentralFileHeader->relativeOffsetOfLocalHeader);

[entries addObject:[[ZZOldArchiveEntry alloc] initWithCentralFileHeader:nextCentralFileHeader
localFileHeader:nextLocalFileHeader
encoding:_encoding]];
Expand Down Expand Up @@ -275,3 +275,4 @@ - (BOOL)updateEntries:(NSArray*)newEntries
}

@end

43 changes: 39 additions & 4 deletions zipzap/ZZArchiveEntry.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
*/
@property (readonly, nonatomic) BOOL compressed;

/**
* Whether the entry is encrypted.
*/
@property (readonly, nonatomic) BOOL encrypted;

/**
* The last modified date and time of the entry. The time value is only accurate to 2 seconds.
*/
Expand Down Expand Up @@ -155,28 +160,58 @@
* @param error The error information when an error occurs. Pass in nil if you do not want error information.
* @return Whether entry file is consistent or not.
*/
- (BOOL)check:(NSError**)error;
- (BOOL)check:(out NSError**)error;

/**
* Creates a stream to represent the entry file.
*
* @param error A pointer to a variable that will contain the error if any.
* @return The new stream: nil for new entries.
*/
- (NSInputStream*)newStream;
- (NSInputStream*)newStreamWithError:(NSError**)error;

/**
* Creates a stream to represent the entry file.
*
* @param password The password to be used for decryption.
* @param error A pointer to a variable that will contain the error if any.
* @return The new stream: nil for new entries.
*/
- (NSInputStream*)newStreamWithPassword:(NSString*)password error:(NSError**)error;

/**
* Creates data to represent the entry file.
*
* @param error A pointer to a variable that will contain the error if any.
* @return The new data: nil for new entries.
*/
- (NSData*)newData;
- (NSData*)newDataWithError:(NSError**)error;

/**
* Creates data to represent the entry file.
*
* @param password The password to be used for decryption.
* @param error A pointer to a variable that will contain the error if any.
* @return The new data: nil for new entries.
*/
- (NSData*)newDataWithPassword:(NSString*)password error:(NSError**)error;

/**
* Creates a data provider to represent the entry file.
*
* @param error A pointer to a variable that will contain the error if any.
* @return The new data provider: nil for new entries.
*/
- (CGDataProviderRef)newDataProviderWithError:(NSError**)error;

/**
* Creates a data provider to represent the entry file.
*
* @param password The password to be used for decryption.
* @param error A pointer to a variable that will contain the error if any.
* @return The new data provider: nil for new entries.
*/
- (CGDataProviderRef)newDataProvider;
- (CGDataProviderRef)newDataProviderWithPassword:(NSString*)password error:(NSError**)error;

- (id<ZZArchiveEntryWriter>)newWriterCanSkipLocalFile:(BOOL)canSkipLocalFile;

Expand Down
28 changes: 24 additions & 4 deletions zipzap/ZZArchiveEntry.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ - (BOOL)compressed
return NO;
}

- (BOOL)encrypted
{
return NO;
}

- (NSDate*)lastModified
{
return nil;
Expand Down Expand Up @@ -115,26 +120,41 @@ - (NSString*)fileName
return nil;
}

- (NSInputStream*)newStream
- (NSInputStream*)newStreamWithError:(NSError**)error
{
return nil;
}

- (BOOL)check:(NSError **)error
- (NSInputStream*)newStreamWithPassword:(NSString*)password error:(NSError**)error
{ // Assume that by default the subclass did not implement it, so we will fallback to the newStreamWithError:
return [self newStreamWithError:error];
}

- (BOOL)check:(NSError**)error
{
return YES;
}

- (NSData*)newData
- (NSData*)newDataWithError:(NSError**)error
{
return nil;
}

- (CGDataProviderRef)newDataProvider
- (NSData*)newDataWithPassword:(NSString*)password error:(NSError**)error
{ // Assume that by default the subclass did not implement it, so we will fallback to the newStreamWithError:
return [self newDataWithError:error];
}

- (CGDataProviderRef)newDataProviderWithError:(NSError**)error
{
return NULL;
}

- (CGDataProviderRef)newDataProviderWithPassword:(NSString*)password error:(NSError**)error
{ // Assume that by default the subclass did not implement it, so we will fallback to the newStreamWithError:
return [self newDataProviderWithError:error];
}

- (id<ZZArchiveEntryWriter>)newWriterCanSkipLocalFile:(BOOL)canSkipLocalFile
{
return nil;
Expand Down
4 changes: 2 additions & 2 deletions zipzap/ZZArchiveEntryWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
- (uint32_t)offsetToLocalFileEnd;
- (BOOL)writeLocalFileToChannelOutput:(id<ZZChannelOutput>)channelOutput
withInitialSkip:(uint32_t)initialSkip
error:(NSError**)error;
error:(out NSError**)error;
- (BOOL)writeCentralFileHeaderToChannelOutput:(id<ZZChannelOutput>)channelOutput
error:(NSError**)error;
error:(out NSError**)error;
@end
8 changes: 4 additions & 4 deletions zipzap/ZZChannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

@property (readonly, nonatomic) NSURL* URL;

- (instancetype)temporaryChannel:(NSError**)error;
- (instancetype)temporaryChannel:(out NSError**)error;
- (BOOL)replaceWithChannel:(id<ZZChannel>)channel
error:(NSError**)error;
error:(out NSError**)error;
- (void)removeAsTemporary;

- (NSData*)newInput:(NSError**)error;
- (id<ZZChannelOutput>)newOutput:(NSError**)error;
- (NSData*)newInput:(out NSError**)error;
- (id<ZZChannelOutput>)newOutput:(out NSError**)error;

@end
6 changes: 3 additions & 3 deletions zipzap/ZZChannelOutput.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@

- (uint32_t)offset;
- (BOOL)seekToOffset:(uint32_t)offset
error:(NSError**)error;
error:(out NSError**)error;

- (BOOL)writeData:(NSData*)data
error:(NSError**)error;
error:(out NSError**)error;
- (BOOL)truncateAtOffset:(uint32_t)offset
error:(NSError**)error;
error:(out NSError**)error;
- (void)close;

@end
45 changes: 45 additions & 0 deletions zipzap/ZZConstants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// ZZConstants.h
// zipzap
//
// Created by Daniel Cohen Gindi on 12/29/13.
//

//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
// BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
// OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//

typedef NS_ENUM(NSInteger, ZZEncryptionMode)
{
ZZEncryptionModeNone,
ZZEncryptionModeStandard,
ZZEncryptionModeStrong,
ZZEncryptionModeAES
};

typedef NS_ENUM(uint8_t, ZZAesStrength)
{
ZZAesStrength128 = 0x01,
ZZAesStrength192 = 0x02,
ZZAesStrength256 = 0x03
};
8 changes: 4 additions & 4 deletions zipzap/ZZDataChannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

- (id)initWithData:(NSData*)data;

- (instancetype)temporaryChannel:(NSError**)error;
- (instancetype)temporaryChannel:(out NSError**)error;
- (BOOL)replaceWithChannel:(id<ZZChannel>)channel
error:(NSError**)error;
error:(out NSError**)error;
- (void)removeAsTemporary;

- (NSData*)newInput:(NSError**)error;
- (id<ZZChannelOutput>)newOutput:(NSError**)error;
- (NSData*)newInput:(out NSError**)error;
- (id<ZZChannelOutput>)newOutput:(out NSError**)error;

@end
8 changes: 4 additions & 4 deletions zipzap/ZZDataChannel.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ - (NSURL*)URL
return nil;
}

- (instancetype)temporaryChannel:(NSError**)error
- (instancetype)temporaryChannel:(out NSError**)error
{
return [[ZZDataChannel alloc] initWithData:[NSMutableData data]];
}

- (BOOL)replaceWithChannel:(id<ZZChannel>)channel
error:(NSError**)error
error:(out NSError**)error
{
[(NSMutableData*)_allData setData:((ZZDataChannel*)channel)->_allData];
return YES;
Expand All @@ -43,12 +43,12 @@ - (void)removeAsTemporary
_allData = nil;
}

- (NSData*)newInput:(NSError**)error
- (NSData*)newInput:(out NSError**)error
{
return _allData;
}

- (id<ZZChannelOutput>)newOutput:(NSError**)error
- (id<ZZChannelOutput>)newOutput:(out NSError**)error
{
return [[ZZDataChannelOutput alloc] initWithData:(NSMutableData*)_allData];
}
Expand Down
6 changes: 3 additions & 3 deletions zipzap/ZZDataChannelOutput.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

- (uint32_t)offset;
- (BOOL)seekToOffset:(uint32_t)offset
error:(NSError**)error;
error:(out NSError**)error;

- (BOOL)writeData:(NSData*)data
error:(NSError**)error;
error:(out NSError**)error;
- (BOOL)truncateAtOffset:(uint32_t)offset
error:(NSError**)error;
error:(out NSError**)error;
- (void)close;

@end
6 changes: 3 additions & 3 deletions zipzap/ZZDataChannelOutput.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ - (uint32_t)offset
}

- (BOOL)seekToOffset:(uint32_t)offset
error:(NSError**)error
error:(out NSError**)error
{
_offset = offset;
return YES;
}

- (BOOL)writeData:(NSData*)data
error:(NSError**)error
error:(out NSError**)error
{
NSUInteger allDataLength = _allData.length;
NSUInteger dataLength = data.length;
Expand All @@ -59,7 +59,7 @@ - (BOOL)writeData:(NSData*)data
}

- (BOOL)truncateAtOffset:(uint32_t)offset
error:(NSError**)error
error:(out NSError**)error
{
_allData.length = offset;
return YES;
Expand Down
23 changes: 23 additions & 0 deletions zipzap/ZZDecryptInputStream.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// ZZDecryptInputStream.h
// zipzap
//
// Created by Daniel Cohen Gindi on 12/29/13.
//
//

#import <Foundation/Foundation.h>
#include "ZZDecrypter.h"

@interface ZZDecryptInputStream : NSInputStream

- (id)initWithStream:(NSInputStream*)upstream decrypter:(ZZDecrypter *)decrypter;

- (void)open;
- (void)close;

- (NSInteger)read:(uint8_t*)buffer maxLength:(NSUInteger)len;
- (BOOL)getBuffer:(uint8_t**)buffer length:(NSUInteger*)len;
- (BOOL)hasBytesAvailable;

@end
Loading