diff --git a/submodules/LegacyComponents/PublicHeaders/LegacyComponents/NSInputStream+TL.h b/submodules/LegacyComponents/PublicHeaders/LegacyComponents/NSInputStream+TL.h index aacf17f8e8..5c9c0105b4 100644 --- a/submodules/LegacyComponents/PublicHeaders/LegacyComponents/NSInputStream+TL.h +++ b/submodules/LegacyComponents/PublicHeaders/LegacyComponents/NSInputStream+TL.h @@ -4,11 +4,8 @@ @interface NSInputStream (TL) -- (int32_t)readInt32; - (int32_t)readInt32:(bool *)failed __attribute__((nonnull(1))); -- (int64_t)readInt64; - (int64_t)readInt64:(bool *)failed __attribute__((nonnull(1))); -- (double)readDouble; - (double)readDouble:(bool *)failed __attribute__((nonnull(1))); - (NSData *)readData:(int)length; - (NSData *)readData:(int)length failed:(bool *)failed __attribute__((nonnull(2))); diff --git a/submodules/LegacyComponents/Sources/NSInputStream+TL.m b/submodules/LegacyComponents/Sources/NSInputStream+TL.m index 36fd423570..a27517b88b 100644 --- a/submodules/LegacyComponents/Sources/NSInputStream+TL.m +++ b/submodules/LegacyComponents/Sources/NSInputStream+TL.m @@ -22,27 +22,6 @@ static inline int roundUpInput(int numToRound, int multiple) @implementation NSInputStream (TL) -- (int32_t)readInt32 -{ - int32_t value = 0; - - if ([self read:(uint8_t *)&value maxLength:4] != 4) - { - TGLegacyLog(@"***** Couldn't read int32"); - - @throw [[NSException alloc] initWithName:@"NSInputStream+TLException" reason:@"readInt32 end of stream" userInfo:@{}]; - } - -#if __BYTE_ORDER == __LITTLE_ENDIAN -#elif __BYTE_ORDER == __BIG_ENDIAN -# error "Big endian is not implemented" -#else -# error "Unknown byte order" -#endif - - return value; -} - - (int32_t)readInt32:(bool *)failed { int32_t value = 0; diff --git a/submodules/LegacyComponents/Sources/TGAuthorSignatureMediaAttachment.m b/submodules/LegacyComponents/Sources/TGAuthorSignatureMediaAttachment.m index 4cd356c201..f24d9ef706 100644 --- a/submodules/LegacyComponents/Sources/TGAuthorSignatureMediaAttachment.m +++ b/submodules/LegacyComponents/Sources/TGAuthorSignatureMediaAttachment.m @@ -34,8 +34,15 @@ - (TGMediaAttachment *)parseMediaAttachment:(NSInputStream *)is { - int32_t length = [is readInt32]; - NSData *data = [is readData:length]; + bool readingError = false; + int32_t length = [is readInt32:&readingError]; + if (readingError) { + return nil; + } + NSData *data = [is readData:length failed:&readingError]; + if (readingError) { + return nil; + } #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" diff --git a/submodules/LegacyComponents/Sources/TGBotContextResultAttachment.m b/submodules/LegacyComponents/Sources/TGBotContextResultAttachment.m index bd97cf7b3b..502f98e44c 100644 --- a/submodules/LegacyComponents/Sources/TGBotContextResultAttachment.m +++ b/submodules/LegacyComponents/Sources/TGBotContextResultAttachment.m @@ -36,8 +36,15 @@ - (TGMediaAttachment *)parseMediaAttachment:(NSInputStream *)is { - int32_t length = [is readInt32]; - NSData *data = [is readData:length]; + bool readingError = false; + int32_t length = [is readInt32:&readingError]; + if (readingError) { + return nil; + } + NSData *data = [is readData:length failed:&readingError]; + if (readingError) { + return nil; + } #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" return [NSKeyedUnarchiver unarchiveObjectWithData:data]; diff --git a/submodules/LegacyComponents/Sources/TGGameMediaAttachment.m b/submodules/LegacyComponents/Sources/TGGameMediaAttachment.m index 365ff1d221..d2ccf31bb2 100644 --- a/submodules/LegacyComponents/Sources/TGGameMediaAttachment.m +++ b/submodules/LegacyComponents/Sources/TGGameMediaAttachment.m @@ -48,8 +48,15 @@ - (TGMediaAttachment *)parseMediaAttachment:(NSInputStream *)is { - int32_t length = [is readInt32]; - NSData *data = [is readData:length]; + bool readingError = false; + int32_t length = [is readInt32:&readingError]; + if (readingError) { + return nil; + } + NSData *data = [is readData:length failed:&readingError]; + if (readingError) { + return nil; + } #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" return [NSKeyedUnarchiver unarchiveObjectWithData:data]; diff --git a/submodules/LegacyComponents/Sources/TGInvoiceMediaAttachment.m b/submodules/LegacyComponents/Sources/TGInvoiceMediaAttachment.m index 1d5f1bc4b4..89220c4470 100644 --- a/submodules/LegacyComponents/Sources/TGInvoiceMediaAttachment.m +++ b/submodules/LegacyComponents/Sources/TGInvoiceMediaAttachment.m @@ -87,8 +87,15 @@ - (TGMediaAttachment *)parseMediaAttachment:(NSInputStream *)is { - int32_t length = [is readInt32]; - NSData *data = [is readData:length]; + bool readingError = false; + int32_t length = [is readInt32:&readingError]; + if (readingError) { + return nil; + } + NSData *data = [is readData:length failed:&readingError]; + if (readingError) { + return nil; + } #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" return [NSKeyedUnarchiver unarchiveObjectWithData:data]; diff --git a/submodules/LegacyComponents/Sources/TGMessageEntitiesAttachment.m b/submodules/LegacyComponents/Sources/TGMessageEntitiesAttachment.m index 02e8ce7701..fd959f11b3 100644 --- a/submodules/LegacyComponents/Sources/TGMessageEntitiesAttachment.m +++ b/submodules/LegacyComponents/Sources/TGMessageEntitiesAttachment.m @@ -54,8 +54,15 @@ - (TGMediaAttachment *)parseMediaAttachment:(NSInputStream *)is { - int32_t length = [is readInt32]; - NSData *data = [is readData:length]; + bool readingError = false; + int32_t length = [is readInt32:&readingError]; + if (readingError) { + return nil; + } + NSData *data = [is readData:length failed:&readingError]; + if (readingError) { + return nil; + } #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" return [NSKeyedUnarchiver unarchiveObjectWithData:data]; diff --git a/submodules/LegacyComponents/Sources/TGReplyMarkupAttachment.m b/submodules/LegacyComponents/Sources/TGReplyMarkupAttachment.m index 6a4dac0ad7..cb88e10a15 100644 --- a/submodules/LegacyComponents/Sources/TGReplyMarkupAttachment.m +++ b/submodules/LegacyComponents/Sources/TGReplyMarkupAttachment.m @@ -64,8 +64,15 @@ - (TGMediaAttachment *)parseMediaAttachment:(NSInputStream *)is { - int32_t length = [is readInt32]; - NSData *data = [is readData:length]; + bool readingError = false; + int32_t length = [is readInt32:&readingError]; + if (readingError) { + return nil; + } + NSData *data = [is readData:length failed:&readingError]; + if (readingError) { + return nil; + } #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" return [NSKeyedUnarchiver unarchiveObjectWithData:data]; diff --git a/submodules/LegacyComponents/Sources/TGViaUserAttachment.m b/submodules/LegacyComponents/Sources/TGViaUserAttachment.m index 51fb7394d8..964941c4e6 100644 --- a/submodules/LegacyComponents/Sources/TGViaUserAttachment.m +++ b/submodules/LegacyComponents/Sources/TGViaUserAttachment.m @@ -36,8 +36,15 @@ - (TGMediaAttachment *)parseMediaAttachment:(NSInputStream *)is { - int32_t length = [is readInt32]; - NSData *data = [is readData:length]; + bool readingError = false; + int32_t length = [is readInt32:&readingError]; + if (readingError) { + return nil; + } + NSData *data = [is readData:length failed:&readingError]; + if (readingError) { + return nil; + } #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" return [NSKeyedUnarchiver unarchiveObjectWithData:data]; diff --git a/submodules/LegacyComponents/Sources/TGWebPageMediaAttachment.m b/submodules/LegacyComponents/Sources/TGWebPageMediaAttachment.m index 0550d67ced..413f423174 100644 --- a/submodules/LegacyComponents/Sources/TGWebPageMediaAttachment.m +++ b/submodules/LegacyComponents/Sources/TGWebPageMediaAttachment.m @@ -157,8 +157,15 @@ - (TGMediaAttachment *)parseMediaAttachment:(NSInputStream *)is { - int32_t length = [is readInt32]; - NSData *data = [is readData:length]; + bool readingError = false; + int32_t length = [is readInt32:&readingError]; + if (readingError) { + return nil; + } + NSData *data = [is readData:length failed:&readingError]; + if (readingError) { + return nil; + } @try { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" diff --git a/submodules/MtProtoKit/PublicHeaders/MtProtoKit/MTInputStream.h b/submodules/MtProtoKit/PublicHeaders/MtProtoKit/MTInputStream.h index bc0bf67327..5e6636fe6c 100644 --- a/submodules/MtProtoKit/PublicHeaders/MtProtoKit/MTInputStream.h +++ b/submodules/MtProtoKit/PublicHeaders/MtProtoKit/MTInputStream.h @@ -9,17 +9,12 @@ - (int32_t)readInt32; - (int32_t)readInt32:(bool *)failed __attribute__((nonnull(1))); -- (int64_t)readInt64; - (int64_t)readInt64:(bool *)failed __attribute__((nonnull(1))); - (double)readDouble; - (double)readDouble:(bool *)failed __attribute__((nonnull(1))); -- (NSData *)readData:(int)length; - (NSData *)readData:(int)length failed:(bool *)failed __attribute__((nonnull(2))); -- (NSMutableData *)readMutableData:(NSUInteger)length; - (NSMutableData *)readMutableData:(NSUInteger)length failed:(bool *)failed __attribute__((nonnull(2))); -- (NSString *)readString; - (NSString *)readString:(bool *)failed __attribute__((nonnull(1))); -- (NSData *)readBytes; - (NSData *)readBytes:(bool *)failed __attribute__((nonnull(1))); @end diff --git a/submodules/MtProtoKit/Sources/MTInputStream.m b/submodules/MtProtoKit/Sources/MTInputStream.m index 509427debe..44c54ffa94 100644 --- a/submodules/MtProtoKit/Sources/MTInputStream.m +++ b/submodules/MtProtoKit/Sources/MTInputStream.m @@ -96,27 +96,6 @@ static inline int roundUpInput(int numToRound, int multiple) return value; } -- (int64_t)readInt64 -{ - int64_t value = 0; - - if ([_wrappedInputStream read:(uint8_t *)&value maxLength:8] != 8) - { - if (MTLogEnabled()) { - MTLog(@"***** Couldn't read int64"); - } - } - -#if __BYTE_ORDER == __LITTLE_ENDIAN -#elif __BYTE_ORDER == __BIG_ENDIAN -# error "Big endian is not implemented" -#else -# error "Unknown byte order" -#endif - - return value; -} - - (int64_t)readInt64:(bool *)failed { int64_t value = 0; @@ -178,20 +157,6 @@ static inline int roundUpInput(int numToRound, int multiple) return value; } -- (NSData *)readData:(int)length -{ - uint8_t *bytes = (uint8_t *)malloc(length); - NSInteger readLen = [_wrappedInputStream read:bytes maxLength:length]; - if (readLen != length) - { - if (MTLogEnabled()) { - MTLog(@"***** Couldn't read %d bytes", length); - } - } - NSData *data = [[NSData alloc] initWithBytesNoCopy:bytes length:length freeWhenDone:true]; - return data; -} - - (NSData *)readData:(int)length failed:(bool *)failed { uint8_t *bytes = (uint8_t *)malloc(length); @@ -206,20 +171,6 @@ static inline int roundUpInput(int numToRound, int multiple) return data; } -- (NSMutableData *)readMutableData:(NSUInteger)length -{ - uint8_t *bytes = (uint8_t *)malloc(length); - NSInteger readLen = [_wrappedInputStream read:bytes maxLength:length]; - if (readLen != length) - { - if (MTLogEnabled()) { - MTLog(@"***** Couldn't read %d bytes", length); - } - } - NSMutableData *data = [[NSMutableData alloc] initWithBytesNoCopy:bytes length:length freeWhenDone:true]; - return data; -} - - (NSMutableData *)readMutableData:(NSUInteger)length failed:(bool *)failed { uint8_t *bytes = (uint8_t *)malloc(length); @@ -234,60 +185,6 @@ static inline int roundUpInput(int numToRound, int multiple) return data; } -- (NSString *)readString -{ - uint8_t tmp = 0; - [_wrappedInputStream read:&tmp maxLength:1]; - - int paddingBytes = 0; - - int32_t length = tmp; - if (length == 254) - { - length = 0; - [_wrappedInputStream read:((uint8_t *)&length) + 1 maxLength:3]; - length >>= 8; - -#if __BYTE_ORDER == __LITTLE_ENDIAN -#elif __BYTE_ORDER == __BIG_ENDIAN -# error "Big endian is not implemented" -#else -# error "Unknown byte order" -#endif - - paddingBytes = roundUpInput(length, 4) - length; - } - else - { - paddingBytes = roundUpInput(length + 1, 4) - (length + 1); - } - - NSString *string = nil; - - if (length > 0) - { - uint8_t *bytes = (uint8_t *)malloc(length); - NSInteger readLen = [_wrappedInputStream read:bytes maxLength:length]; - if (readLen != length) - { - if (MTLogEnabled()) { - MTLog(@"***** Couldn't read %d bytes", length); - } - } - - string = [[NSString alloc] initWithBytesNoCopy:bytes length:length encoding:NSUTF8StringEncoding freeWhenDone:true]; - } - else - { - string = @""; - } - - for (int i = 0; i < paddingBytes; i++) - [_wrappedInputStream read:&tmp maxLength:1]; - - return string; -} - - (NSString *)readString:(bool *)failed { uint8_t tmp = 0; @@ -342,44 +239,6 @@ static inline int roundUpInput(int numToRound, int multiple) return string; } -- (NSData *)readBytes -{ - uint8_t tmp = 0; - [_wrappedInputStream read:&tmp maxLength:1]; - - int paddingBytes = 0; - - int32_t length = tmp; - if (length == 254) - { - length = 0; - [_wrappedInputStream read:((uint8_t *)&length) + 1 maxLength:3]; - length >>= 8; - - paddingBytes = roundUpInput(length, 4) - length; - } - else - { - paddingBytes = roundUpInput(length + 1, 4) - (length + 1); - } - - uint8_t *bytes = (uint8_t *)malloc(length); - NSInteger readLen = [_wrappedInputStream read:bytes maxLength:length]; - if (readLen != length) - { - if (MTLogEnabled()) { - MTLog(@"***** Couldn't read %d bytes", length); - } - } - - NSData *result = [NSData dataWithBytesNoCopy:bytes length:length freeWhenDone:true]; - - for (int i = 0; i < paddingBytes; i++) - [_wrappedInputStream read:&tmp maxLength:1]; - - return result; -} - - (NSData *)readBytes:(bool *)failed { uint8_t tmp = 0; diff --git a/submodules/MtProtoKit/Sources/MTProto.m b/submodules/MtProtoKit/Sources/MTProto.m index efc50c77da..71ac74b747 100644 --- a/submodules/MtProtoKit/Sources/MTProto.m +++ b/submodules/MtProtoKit/Sources/MTProto.m @@ -1800,17 +1800,30 @@ static const NSUInteger MTMaxUnacknowledgedMessageCount = 64; MTInputStream *is = [[MTInputStream alloc] initWithData:data]; - int64_t keyId = [is readInt64]; + bool readingError = false; + + int64_t keyId = [is readInt64:&readingError]; + if (readingError) { + return; + } if (keyId == authKey.authKeyId) { - NSData *messageKey = [is readData:16]; + + NSData *messageKey = [is readData:16 failed:&readingError]; + if (readingError) { + return; + } MTMessageEncryptionKey *encryptionKey = [MTMessageEncryptionKey messageEncryptionKeyV2ForAuthKey:effectiveAuthKey.authKey messageKey:messageKey toClient:true]; if (data.length <= 24) { return; } - NSMutableData *encryptedMessageData = [is readMutableData:(data.length - 24)]; + NSMutableData *encryptedMessageData = [is readMutableData:(data.length - 24) failed:&readingError]; + if (readingError) { + return; + } + while (encryptedMessageData.length % 16 != 0) { [encryptedMessageData setLength:encryptedMessageData.length - 1]; } @@ -1819,47 +1832,36 @@ static const NSUInteger MTMaxUnacknowledgedMessageCount = 64; NSData *decryptedData = MTAesDecrypt(encryptedMessageData, encryptionKey.key, encryptionKey.iv); MTInputStream *messageIs = [[MTInputStream alloc] initWithData:decryptedData]; - [messageIs readInt64]; - [messageIs readInt64]; - - [messageIs readInt64]; - [messageIs readInt32]; - [messageIs readInt32]; bool stop = false; + if (!stop) { + [messageIs readInt64:&stop]; + } + if (!stop) { + [messageIs readInt64:&stop]; + } + if (!stop) { + [messageIs readInt64:&stop]; + } + if (!stop) { + [messageIs readInt32:&stop]; + } + if (!stop) { + [messageIs readInt32:&stop]; + } + int64_t reqMsgId = 0; - /*if (true) - {*/ - while (!stop && reqMsgId == 0) - { - int32_t signature = [messageIs readInt32:&stop]; + while (!stop && reqMsgId == 0) { + int32_t signature = [messageIs readInt32:&stop]; + if (!stop) { [self findReqMsgId:messageIs signature:signature reqMsgId:&reqMsgId failed:&stop]; } - /*} - else - { - int32_t signature = [messageIs readInt32]; - if (signature == (int)0xf35c6d01) - reqMsgId = [messageIs readInt64]; - else if (signature == (int)0x73f1f8dc) - { - int count = [messageIs readInt32]; - if (count != 0) - { - [messageIs readInt64]; - [messageIs readInt32]; - [messageIs readInt32]; - - signature = [messageIs readInt32]; - if (signature == (int)0xf35c6d01) - reqMsgId = [messageIs readInt64]; - } - } - }*/ + } - if (reqMsgId != 0) + if (reqMsgId != 0) { completion(token, @(reqMsgId)); + } } } }]; @@ -2306,38 +2308,48 @@ static bool isDataEqualToDataConstTime(NSData *data1, NSData *data2) { if (_useUnauthorizedMode) { - int64_t authKeyId = [is readInt64]; + bool readingError = false; + int64_t authKeyId = [is readInt64:&readingError]; + if (readingError) { + if (parseError != NULL) { + *parseError = true; + } + return nil; + } if (authKeyId != 0) { - if (parseError != NULL) + if (parseError != NULL) { *parseError = true; + } return nil; } embeddedMessageId = [is readInt64:&readError]; if (readError) { - if (parseError != NULL) + if (parseError != NULL) { *parseError = true; + } return nil; } topMessageSize = [is readInt32:&readError]; if (readError || topMessageSize < 4) { - if (parseError != NULL) + if (parseError != NULL) { *parseError = true; + } return nil; } - if (dataMessageId != 0) + if (dataMessageId != 0) { *dataMessageId = embeddedMessageId; + } } else { embeddedSalt = [is readInt64:&readError]; - if (readError) - { + if (readError) { if (parseError != NULL) *parseError = true; return nil; @@ -2346,39 +2358,44 @@ static bool isDataEqualToDataConstTime(NSData *data1, NSData *data2) { embeddedSessionId = [is readInt64:&readError]; if (readError) { - if (parseError != NULL) + if (parseError != NULL) { *parseError = true; + } return nil; } if (embeddedSessionId != _sessionInfo.sessionId) { - if (parseError != NULL) + if (parseError != NULL) { *parseError = true; + } return nil; } embeddedMessageId = [is readInt64:&readError]; if (readError) { - if (parseError != NULL) + if (parseError != NULL) { *parseError = true; + } return nil; } embeddedSeqNo = [is readInt32:&readError]; if (readError) { - if (parseError != NULL) + if (parseError != NULL) { *parseError = true; + } return nil; } [is readInt32:&readError]; if (readError) { - if (parseError != NULL) + if (parseError != NULL) { *parseError = true; + } return nil; } } @@ -2388,31 +2405,33 @@ static bool isDataEqualToDataConstTime(NSData *data1, NSData *data2) { while (true) { NSInteger readBytes = [[is wrappedInputStream] read:buffer maxLength:128]; - if (readBytes <= 0) + if (readBytes <= 0) { break; + } [topMessageData appendBytes:buffer length:readBytes]; } id topObject = [self parseMessage:topMessageData]; if (topObject == nil) { - if (parseError != NULL) + if (parseError != NULL) { *parseError = true; + } return nil; } NSMutableArray *messages = [[NSMutableArray alloc] init]; NSTimeInterval timestamp = embeddedMessageId / 4294967296.0; - if ([topObject isKindOfClass:[MTMsgContainerMessage class]]) - { + if ([topObject isKindOfClass:[MTMsgContainerMessage class]]) { for (MTMessage *subMessage in ((MTMsgContainerMessage *)topObject).messages) { id subObject = [self parseMessage:subMessage.data]; if (subObject == nil) { - if (parseError != NULL) + if (parseError != NULL) { *parseError = true; + } return nil; } @@ -2421,15 +2440,14 @@ static bool isDataEqualToDataConstTime(NSData *data1, NSData *data2) { int32_t subMessageLength = (int32_t)subMessage.data.length; [messages addObject:[[MTIncomingMessage alloc] initWithMessageId:subMessageId seqNo:subMessageSeqNo authKeyId:embeddedAuthKeyId sessionId:embeddedSessionId salt:embeddedSalt timestamp:timestamp size:subMessageLength body:subObject]]; } - } - else if ([topObject isKindOfClass:[MTMessage class]]) - { + } else if ([topObject isKindOfClass:[MTMessage class]]) { MTMessage *message = topObject; id subObject = [self parseMessage:message.data]; if (subObject == nil) { - if (parseError != NULL) + if (parseError != NULL) { *parseError = true; + } return nil; } @@ -2437,9 +2455,9 @@ static bool isDataEqualToDataConstTime(NSData *data1, NSData *data2) { int32_t subMessageSeqNo = message.seqNo; int32_t subMessageLength = (int32_t)message.data.length; [messages addObject:[[MTIncomingMessage alloc] initWithMessageId:subMessageId seqNo:subMessageSeqNo authKeyId:embeddedAuthKeyId sessionId:embeddedSessionId salt:embeddedSalt timestamp:timestamp size:subMessageLength body:subObject]]; - } - else + } else { [messages addObject:[[MTIncomingMessage alloc] initWithMessageId:embeddedMessageId seqNo:embeddedSeqNo authKeyId:embeddedAuthKeyId sessionId:embeddedSessionId salt:embeddedSalt timestamp:timestamp size:topMessageSize body:topObject]]; + } return messages; }