From 529dfcd10aed9f8876240c3f5f407c53eb7fc16f Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Sun, 7 Jul 2019 16:40:40 +0200 Subject: [PATCH] Fixed gunzip --- submodules/GZip/Sources/GZip.h | 4 ++-- submodules/GZip/Sources/GZip.m | 14 +++++++++++--- .../TelegramUI/AnimatedStickerNode.swift | 2 +- .../TelegramUI/AnimatedStickerUtils.swift | 4 ++-- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/submodules/GZip/Sources/GZip.h b/submodules/GZip/Sources/GZip.h index ee4c756f4d..ea1f93b9b3 100644 --- a/submodules/GZip/Sources/GZip.h +++ b/submodules/GZip/Sources/GZip.h @@ -10,8 +10,8 @@ FOUNDATION_EXPORT const unsigned char GZipVersionString[]; extern "C" { #endif -NSData *TGGZipData(NSData *data, float level); -NSData * _Nullable TGGUnzipData(NSData *data); +NSData * _Nonnull TGGZipData(NSData * _Nonnull data, float level); +NSData * _Nullable TGGUnzipData(NSData * _Nonnull data, uint sizeLimit); #ifdef __cplusplus } diff --git a/submodules/GZip/Sources/GZip.m b/submodules/GZip/Sources/GZip.m index 4d55ac33bc..9e7a828e7d 100644 --- a/submodules/GZip/Sources/GZip.m +++ b/submodules/GZip/Sources/GZip.m @@ -42,7 +42,7 @@ NSData *TGGZipData(NSData *data, float level) { return output; } -NSData * _Nullable TGGUnzipData(NSData *data) +NSData * _Nullable TGGUnzipData(NSData *data, uint sizeLimit) { if (data.length == 0 || !TGIsGzippedData(data)) { return nil; @@ -61,12 +61,20 @@ NSData * _Nullable TGGUnzipData(NSData *data) int status = Z_OK; output = [NSMutableData dataWithCapacity:data.length * 2]; while (status == Z_OK) { + if (sizeLimit > 0 && stream.total_out > sizeLimit) { + return nil; + } + if (stream.total_out >= output.length) { - output.length += data.length / 2; + NSUInteger length = output.length + data.length / 2; + if (sizeLimit > 0 && length > sizeLimit) { + return nil; + } + output.length = length; } stream.next_out = (uint8_t *)output.mutableBytes + stream.total_out; stream.avail_out = (uInt)(output.length - stream.total_out); - status = inflate (&stream, Z_SYNC_FLUSH); + status = inflate(&stream, Z_SYNC_FLUSH); } if (inflateEnd(&stream) == Z_OK) { if (status == Z_STREAM_END) { diff --git a/submodules/TelegramUI/TelegramUI/AnimatedStickerNode.swift b/submodules/TelegramUI/TelegramUI/AnimatedStickerNode.swift index bb5986a994..a7a817af47 100644 --- a/submodules/TelegramUI/TelegramUI/AnimatedStickerNode.swift +++ b/submodules/TelegramUI/TelegramUI/AnimatedStickerNode.swift @@ -214,7 +214,7 @@ private final class AnimatedStickerDirectFrameSource: AnimatedStickerFrameSource self.width = width self.height = height self.currentFrame = 0 - guard let rawData = TGGUnzipData(data) else { + guard let rawData = TGGUnzipData(data, 1024 * 1024) else { return nil } guard let animation = LottieInstance(data: rawData, cacheKey: "") else { diff --git a/submodules/TelegramUI/TelegramUI/AnimatedStickerUtils.swift b/submodules/TelegramUI/TelegramUI/AnimatedStickerUtils.swift index c836c4afe1..c9baab8fa8 100644 --- a/submodules/TelegramUI/TelegramUI/AnimatedStickerUtils.swift +++ b/submodules/TelegramUI/TelegramUI/AnimatedStickerUtils.swift @@ -98,7 +98,7 @@ func fetchCompressedLottieFirstFrameAJpeg(data: Data, size: CGSize, cacheKey: St return } - let decompressedData = TGGUnzipData(data) + let decompressedData = TGGUnzipData(data, 1024 * 1024) if let decompressedData = decompressedData, let player = LottieInstance(data: decompressedData, cacheKey: cacheKey) { if cancelled.with({ $0 }) { return @@ -193,7 +193,7 @@ func experimentalConvertCompressedLottieToCombinedMp4(data: Data, size: CGSize, var deltaTime: Double = 0 var compressionTime: Double = 0 - let decompressedData = TGGUnzipData(data) + let decompressedData = TGGUnzipData(data, 1024 * 1024) if let decompressedData = decompressedData, let player = LottieInstance(data: decompressedData, cacheKey: cacheKey) { let endFrame = Int(player.frameCount)