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;
}
- (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;
*length = packetLength - (int)(payload - packetBytes);
NSUInteger length = packetLength - (int)(payload - packetBytes);
if (!block) {
return payload;
return [NSData dataWithBytes:payload length:length];
}
NSInteger payloadOffset;
@ -359,8 +359,8 @@ static const NSInteger CryptoAEADTagLength = 16;
if (!block(payload, &payloadOffset, &payloadHeaderLength, packetBytes, packetLength, error)) {
return NULL;
}
*length -= payloadHeaderLength;
return payload + payloadOffset;
length -= payloadHeaderLength;
return [NSData dataWithBytes:(payload + payloadOffset) length:length];
}
@end

View File

@ -368,13 +368,13 @@ const NSInteger CryptoCBCMaxHMACLength = 100;
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;
payload += sizeof(uint32_t); // packet id
*length = packetLength - (int)(payload - packetBytes);
NSUInteger length = packetLength - (int)(payload - packetBytes);
if (!block) {
return payload;
return [NSData dataWithBytes:payload length:length];
}
NSInteger payloadOffset;
@ -382,8 +382,8 @@ const NSInteger CryptoCBCMaxHMACLength = 100;
if (!block(payload, &payloadOffset, &payloadHeaderLength, packetBytes, packetLength, error)) {
return NULL;
}
*length -= payloadHeaderLength;
return payload + payloadOffset;
length -= payloadHeaderLength;
return [NSData dataWithBytes:(payload + payloadOffset) length:length];
}
@end

View File

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

View File

@ -82,7 +82,7 @@ typedef BOOL (^DataPathParseBlock)(uint8_t *payload,
@protocol DataPathDecrypter <DataPathChannel>
- (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

View File

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