Return NSData from parsePayloadWithBlock

More friendly to (de)compression stage.
This commit is contained in:
Davide De Rosa 2019-03-19 09:15:18 +01:00
parent 5cc32b1060
commit 197679057d
5 changed files with 19 additions and 23 deletions

View File

@ -346,12 +346,12 @@ static const NSInteger CryptoAEADTagLength = 16;
return YES; return YES;
} }
- (const uint8_t *)parsePayloadWithBlock:(DataPathParseBlock)block length:(NSInteger *)length packetBytes:(uint8_t *)packetBytes packetLength:(NSInteger)packetLength error:(NSError * _Nullable __autoreleasing * _Nullable)error - (NSData *)parsePayloadWithBlock:(DataPathParseBlock)block packetBytes:(uint8_t *)packetBytes packetLength:(NSInteger)packetLength error:(NSError * _Nullable __autoreleasing *)error
{ {
uint8_t *payload = packetBytes; uint8_t *payload = packetBytes;
*length = packetLength - (int)(payload - packetBytes); NSUInteger length = packetLength - (int)(payload - packetBytes);
if (!block) { if (!block) {
return payload; return [NSData dataWithBytes:payload length:length];
} }
NSInteger payloadOffset; NSInteger payloadOffset;
@ -359,8 +359,8 @@ static const NSInteger CryptoAEADTagLength = 16;
if (!block(payload, &payloadOffset, &payloadHeaderLength, packetBytes, packetLength, error)) { if (!block(payload, &payloadOffset, &payloadHeaderLength, packetBytes, packetLength, error)) {
return NULL; return NULL;
} }
*length -= payloadHeaderLength; length -= payloadHeaderLength;
return payload + payloadOffset; return [NSData dataWithBytes:(payload + payloadOffset) length:length];
} }
@end @end

View File

@ -368,13 +368,13 @@ const NSInteger CryptoCBCMaxHMACLength = 100;
return YES; return YES;
} }
- (const uint8_t *)parsePayloadWithBlock:(DataPathParseBlock)block length:(NSInteger *)length packetBytes:(uint8_t *)packetBytes packetLength:(NSInteger)packetLength error:(NSError * _Nullable __autoreleasing * _Nullable)error - (NSData *)parsePayloadWithBlock:(DataPathParseBlock)block packetBytes:(uint8_t *)packetBytes packetLength:(NSInteger)packetLength error:(NSError * _Nullable __autoreleasing *)error
{ {
uint8_t *payload = packetBytes; uint8_t *payload = packetBytes;
payload += sizeof(uint32_t); // packet id payload += sizeof(uint32_t); // packet id
*length = packetLength - (int)(payload - packetBytes); NSUInteger length = packetLength - (int)(payload - packetBytes);
if (!block) { if (!block) {
return payload; return [NSData dataWithBytes:payload length:length];
} }
NSInteger payloadOffset; NSInteger payloadOffset;
@ -382,8 +382,8 @@ const NSInteger CryptoCBCMaxHMACLength = 100;
if (!block(payload, &payloadOffset, &payloadHeaderLength, packetBytes, packetLength, error)) { if (!block(payload, &payloadOffset, &payloadHeaderLength, packetBytes, packetLength, error)) {
return NULL; return NULL;
} }
*length -= payloadHeaderLength; length -= payloadHeaderLength;
return payload + payloadOffset; return [NSData dataWithBytes:(payload + payloadOffset) length:length];
} }
@end @end

View File

@ -289,17 +289,15 @@
continue; continue;
} }
NSInteger payloadLength; NSData *payload = [self.decrypter parsePayloadWithBlock:self.parsePayloadBlock
const uint8_t *payloadBytes = [self.decrypter parsePayloadWithBlock:self.parsePayloadBlock
length:&payloadLength
packetBytes:dataPacketBytes packetBytes:dataPacketBytes
packetLength:dataPacketLength packetLength:dataPacketLength
error:error]; error:error];
if (!payloadBytes) { if (!payload) {
return nil; return nil;
} }
if ((payloadLength == sizeof(DataPacketPingData)) && !memcmp(payloadBytes, DataPacketPingData, payloadLength)) { if ((payload.length == sizeof(DataPacketPingData)) && !memcmp(payload.bytes, DataPacketPingData, payload.length)) {
if (keepAlive) { if (keepAlive) {
*keepAlive = true; *keepAlive = true;
} }
@ -308,7 +306,6 @@
// MSSFix(payloadBytes, payloadLength); // MSSFix(payloadBytes, payloadLength);
NSData *payload = [[NSData alloc] initWithBytes:payloadBytes length:payloadLength];
[self.inPackets addObject:payload]; [self.inPackets addObject:payload];
} }

View File

@ -82,7 +82,7 @@ typedef BOOL (^DataPathParseBlock)(uint8_t *payload,
@protocol DataPathDecrypter <DataPathChannel> @protocol DataPathDecrypter <DataPathChannel>
- (BOOL)decryptDataPacket:(NSData *)packet into:(uint8_t *)packetBytes length:(NSInteger *)packetLength packetId:(uint32_t *)packetId error:(NSError **)error; - (BOOL)decryptDataPacket:(NSData *)packet into:(uint8_t *)packetBytes length:(NSInteger *)packetLength packetId:(uint32_t *)packetId error:(NSError **)error;
- (const uint8_t * _Nullable)parsePayloadWithBlock:(nullable DataPathParseBlock)block length:(NSInteger *)length packetBytes:(uint8_t *)packetBytes packetLength:(NSInteger)packetLength error:(NSError **)error; - (nullable NSData *)parsePayloadWithBlock:(nullable DataPathParseBlock)block packetBytes:(uint8_t *)packetBytes packetLength:(NSInteger)packetLength error:(NSError **)error;
@end @end

View File

@ -135,10 +135,9 @@ class DataPathEncryptionTests: XCTestCase {
var decryptedBytes: [UInt8] = [UInt8](repeating: 0, count: 1000) var decryptedBytes: [UInt8] = [UInt8](repeating: 0, count: 1000)
var decryptedLength: Int = 0 var decryptedLength: Int = 0
var packetId: UInt32 = 0 var packetId: UInt32 = 0
var payloadLength: Int = 0 var compressionHeader: UInt8 = 0
try! dec.decryptDataPacket(encrypted, into: &decryptedBytes, length: &decryptedLength, packetId: &packetId) try! dec.decryptDataPacket(encrypted, into: &decryptedBytes, length: &decryptedLength, packetId: &packetId)
let payloadBytes = try! dec.parsePayload(nil, length: &payloadLength, packetBytes: &decryptedBytes, packetLength: decryptedLength) let payload = try! dec.parsePayload(nil, compressionHeader: &compressionHeader, packetBytes: &decryptedBytes, packetLength: decryptedLength)
let payload = Data(bytes: payloadBytes, count: payloadLength)
XCTAssertEqual(payload, expectedPayload) XCTAssertEqual(payload, expectedPayload)
XCTAssertEqual(packetId, expectedPacketId) XCTAssertEqual(packetId, expectedPacketId)