简体   繁体   English

使用iOS 64位架构的Zlib解压缩方法警告

[英]Zlib decompression method warning using ios 64bit Architecture

I am just updating one of my applications and I have an error with my decompression method. 我只是更新我的一个应用程序,而我的解压缩方法却出错。

This is the warning I am experiencing 这是我遇到的警告

Implicit conversion loses integer precision: 'unsigned long' to 'unit' (aka 'unsigned int')

this is the line of code its happening on 这是发生的代码行

stream.avail_in = len - stream.total_in;

And this is what the whole method looks like 这就是整个方法的样子

#pragma mark - ZLib Compression Methods
//  Returns the decompressed version if the zlib compressed input data or nil if there was an error
- (NSData*) dataByDecompressingData:(NSData*)data {

     NSLog(@"%lu", (unsigned long)data.length);

    Byte* bytes = (Byte*)[data bytes];
    NSInteger len = [data length];
    NSMutableData *decompressedData = [[NSMutableData alloc] initWithCapacity:COMPRESSION_BLOCK];
    Byte* decompressedBytes = (Byte*) malloc(COMPRESSION_BLOCK);

    z_stream stream;
    int err;
    stream.zalloc = (alloc_func)0;
    stream.zfree = (free_func)0;
    stream.opaque = (voidpf)0;

    stream.next_in = bytes;
    err = inflateInit(&stream);
    CHECK_ERR(err, @"inflateInit");

    while (true) {
        stream.avail_in = len - stream.total_in;
        stream.next_out = decompressedBytes;
        stream.avail_out = COMPRESSION_BLOCK;
        err = inflate(&stream, Z_NO_FLUSH);
        [decompressedData appendBytes:decompressedBytes length:(stream.total_out-[decompressedData length])];
        if(err == Z_STREAM_END)
            break;
        CHECK_ERR(err, @"inflate");
    }

    err = inflateEnd(&stream);
    CHECK_ERR(err, @"inflateEnd");

    free(decompressedBytes);
    return decompressedData;
}

First off, you should not use stream.total_in . 首先,您不应该使用stream.total_in It may or may not be a large enough type for your application. 对于您的应用程序而言,它可能足够大,也可能不够大。 It is always unsigned long . 它始终是unsigned long Use your own total input counter sized for your application and ignore stream.total_in . 使用您自己的应用程序大小的总输入计数器,并忽略stream.total_in

Second, I'm guessing that your CHECK_ERR() aborts somehow. 其次,我猜测您的CHECK_ERR()以某种方式中止。 You should not abort in the event of a Z_BUF_ERROR . 如果发生Z_BUF_ERROR ,则不应中止。 In that case, you can continue by simply providing more input and/or more output space. 在这种情况下,您可以继续提供简单的输入和/或输出空间。

Third, the problem here is that you need to pick a stream.avail_in that is assured to fit in unsigned . 第三,这里的问题是您需要选择一个可以确保适合unsignedstream.avail_in You should be comparing the amount of remaining input to the largest value of unsigned , eg UINT_MAX or (unsigned)0 - 1 . 您应该将剩余输入量与unsigned的最大值进行比较,例如UINT_MAX(unsigned)0 - 1 UINT_MAX If the remaining data is larger, use the max value and deduct that from the remaining input. 如果剩余数据较大,请使用最大值,并从剩余输入中减去该最大值。 If smaller or equal, use all of it and set the remaining input to zero. 如果小于或等于,则全部使用并将其余输入设置为零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM