Skip to content

Commit

Permalink
no message
Browse files Browse the repository at this point in the history
  • Loading branch information
petertechstories committed May 25, 2017
1 parent 763c6cd commit 862acbe
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 17 deletions.
20 changes: 11 additions & 9 deletions MTNetworkUsageManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,18 @@ - (instancetype)initWithInfo:(MTNetworkUsageCalculationInfo *)info {
_queue = [[MTQueue alloc] init];
_info = info;

NSString *path = info.filePath;
int32_t fd = open([path UTF8String], O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
if (fd >= 0) {
_fd = fd;
ftruncate(fd, 4096);
void *map = mmap(0, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (map != MAP_FAILED) {
_map = map;
[_queue dispatchOnQueue:^{
NSString *path = info.filePath;
int32_t fd = open([path UTF8String], O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
if (fd >= 0) {
_fd = fd;
ftruncate(fd, 4096);
void *map = mmap(0, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (map != MAP_FAILED) {
_map = map;
}
}
}
}];
}
return self;
}
Expand Down
5 changes: 5 additions & 0 deletions MTProtoKit/MTEncryption.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ NSData *MTSubdataSha1(NSData *data, NSUInteger offset, NSUInteger length);

NSData *MTSha256(NSData *data);

void MTRawSha1(void const *inData, NSUInteger length, void *outData);
void MTRawSha256(void const *inData, NSUInteger length, void *outData);

int32_t MTMurMurHash32(const void *bytes, int length);

void MTAesEncryptInplace(NSMutableData *data, NSData *key, NSData *iv);
void MTAesEncryptInplaceAndModifyIv(NSMutableData *data, NSData *key, NSMutableData *iv);
void MTAesEncryptBytesInplaceAndModifyIv(void *data, NSInteger length, NSData *key, void *iv);
void MTAesEncryptRaw(void const *data, void *outData, NSInteger length, void const *key, void const *iv);
void MTAesDecryptRaw(void const *data, void *outData, NSInteger length, void const *key, void const *iv);
void MTAesDecryptInplaceAndModifyIv(NSMutableData *data, NSData *key, NSMutableData *iv);
void MTAesDecryptBytesInplaceAndModifyIv(void *data, NSInteger length, NSData *key, void *iv);
NSData *MTAesEncrypt(NSData *data, NSData *key, NSData *iv);
Expand Down
36 changes: 36 additions & 0 deletions MTProtoKit/MTEncryption.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
return [[NSData alloc] initWithBytes:digest length:20];
}

void MTRawSha1(void const *inData, NSUInteger length, void *outData)
{
CC_SHA1(inData, (CC_LONG)length, outData);
}

NSData *MTSha256(NSData *data)
{
uint8_t digest[CC_SHA256_DIGEST_LENGTH];
Expand All @@ -50,6 +55,11 @@
return [[NSData alloc] initWithBytes:digest length:CC_SHA256_DIGEST_LENGTH];
}

void MTRawSha256(void const *inData, NSUInteger length, void *outData)
{
CC_SHA256(inData, (CC_LONG)length, outData);
}

#if defined(_MSC_VER)

#define FORCE_INLINE __forceinline
Expand Down Expand Up @@ -202,6 +212,20 @@ void MTAesEncryptBytesInplaceAndModifyIv(void *data, NSInteger length, NSData *k
memcpy(iv, aesIv, 32);
}

void MTAesEncryptRaw(void const *data, void *outData, NSInteger length, void const *key, void const *iv) {
unsigned char aesIv[32];
memcpy(aesIv, iv, 32);

MyAesIgeEncrypt(data, (int)length, outData, key, 32, aesIv);
}

void MTAesDecryptRaw(void const *data, void *outData, NSInteger length, void const *key, void const *iv) {
unsigned char aesIv[32];
memcpy(aesIv, iv, 32);

MyAesIgeDecrypt(data, (int)length, outData, key, 32, aesIv);
}

void MTAesDecryptInplaceAndModifyIv(NSMutableData *data, NSData *key, NSMutableData *iv)
{
unsigned char aesIv[16 * 2];
Expand All @@ -227,6 +251,18 @@ void MTAesDecryptBytesInplaceAndModifyIv(void *data, NSInteger length, NSData *k
memcpy(iv, aesIv, 16 * 2);
}

void MTAesDecryptRawInplaceAndModifyIv(void *data, NSInteger length, void *key, void *iv) {
unsigned char aesIv[16 * 2];
memcpy(aesIv, iv, 16 * 2);

void *outData = malloc(length);
MyAesIgeDecrypt(data, (int)length, outData, key, 32, aesIv);
memcpy(data, outData, length);
free(outData);

memcpy(iv, aesIv, 16 * 2);
}

NSData *MTAesEncrypt(NSData *data, NSData *key, NSData *iv)
{
if (key == nil || iv == nil)
Expand Down
8 changes: 6 additions & 2 deletions MTProtoKit/MTHttpTransport.m
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ - (void)stop
[delegate transportTransactionsMayHaveFailed:self transactionIds:activeTransactionId];
}];

[self cleanup];
[[MTHttpTransport httpTransportQueue] dispatchOnQueue:^{
[self cleanup];
}];

[super stop];
}
Expand All @@ -134,7 +136,9 @@ - (void)cleanup
for (MTHttpWorker *worker in workers)
{
worker.delegate = nil;
[worker stop];
[[MTHttpWorker httpWorkerProcessingQueue] dispatchOnQueue:^{
[worker stop];
}];
}
}];
}
Expand Down
3 changes: 3 additions & 0 deletions MTProtoKit/MTHttpWorker.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

@class MTDatacenterAddress;
@class MTHttpWorker;
@class MTQueue;

@protocol MTHttpWorkerDelegate <NSObject>

Expand All @@ -35,6 +36,8 @@
@property (nonatomic, weak) id<MTHttpWorkerDelegate> delegate;
@property (nonatomic, readonly) bool performsLongPolling;

+ (MTQueue *)httpWorkerProcessingQueue;

- (instancetype)initWithDelegate:(id<MTHttpWorkerDelegate>)delegate address:(MTDatacenterAddress *)address payloadData:(NSData *)payloadData performsLongPolling:(bool)performsLongPolling;

- (bool)isConnected;
Expand Down
8 changes: 5 additions & 3 deletions MTProtoKit/MTTcpConnection.m
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,11 @@ - (void)setUsageCalculationInfo:(MTNetworkUsageCalculationInfo *)usageCalculatio

- (void)setDelegate:(id<MTTcpConnectionDelegate>)delegate
{
_delegate = delegate;

_delegateImplementsProgressUpdated = [delegate respondsToSelector:@selector(tcpConnectionProgressUpdated:packetProgressToken:packetLength:progress:)];
[[MTTcpConnection tcpQueue] dispatchOnQueue:^{
_delegate = delegate;

_delegateImplementsProgressUpdated = [delegate respondsToSelector:@selector(tcpConnectionProgressUpdated:packetProgressToken:packetLength:progress:)];
}];
}

- (void)start
Expand Down
14 changes: 11 additions & 3 deletions MTSubscriber.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,18 @@ - (instancetype)initWithNext:(void (^)(id))next error:(void (^)(id))error comple

- (void)_assignDisposable:(id<MTDisposable>)disposable
{
if (_terminated)
[disposable dispose];
else
bool dispose = false;
OSSpinLockLock(&_lock);
if (_terminated) {
dispose = true;
} else {
_disposable = disposable;
}
OSSpinLockUnlock(&_lock);

if (dispose) {
[disposable dispose];
}
}

- (void)_markTerminatedWithoutDisposal
Expand Down

0 comments on commit 862acbe

Please sign in to comment.