这个zlib字符串解压有什么问题吗?

发布于 2024-11-18 07:31:39 字数 1477 浏览 3 评论 0原文

我正在尝试为我的应用程序创建一个简单的字符串解压缩算法。

/*
 Decompresses the source buffer into the destination buffer.  sourceLen is
 the byte length of the source buffer.  Upon entry, destLen is the total size
 of the destination buffer, which must be large enough to hold the entire
 uncompressed data.  (The size of the uncompressed data must have been saved
 previously by the compressor and transmitted to the decompressor by some
 mechanism outside the scope of this compression library.) Upon exit, destLen
 is the actual size of the uncompressed buffer.

 uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
 enough memory, Z_BUF_ERROR if there was not enough room in the output
 buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete.
 */

[Base64 initialize];
NSData * data = [Base64 decode:@"MDAwMDAwNTB42vPMVkhKzVNIBeLsnNTMPB0IpVCWWZyVqpAJkalKTVUoS8xTSMpJLC0HALWrEYi="];

NSString * deBase64 = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];

int lengteOP = [[deBase64 substringWithRange:NSMakeRange(0,8)] intValue];
NSUInteger lengteIP = [deBase64 length];

const unsigned char *input = (const unsigned char *) [[deBase64 substringFromIndex:8]  cStringUsingEncoding:NSASCIIStringEncoding];
unsigned char * dest; 

uncompress(dest, lengteOP, input, lengteIP);

当我尝试此操作时,出现 EXC_BADD_ACCESS 错误。 该字符串是使用 ZLib 在 delphi 中使用代码构建的,与 iPhone sdk 中的库相同,

它是一个 base64 编码字符串,前 8 个字符表示字符串的长度,后跟 zlib-ed 字符串。

i'm trying to create a simple stringdecompression algorithm for my app.

/*
 Decompresses the source buffer into the destination buffer.  sourceLen is
 the byte length of the source buffer.  Upon entry, destLen is the total size
 of the destination buffer, which must be large enough to hold the entire
 uncompressed data.  (The size of the uncompressed data must have been saved
 previously by the compressor and transmitted to the decompressor by some
 mechanism outside the scope of this compression library.) Upon exit, destLen
 is the actual size of the uncompressed buffer.

 uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
 enough memory, Z_BUF_ERROR if there was not enough room in the output
 buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete.
 */

[Base64 initialize];
NSData * data = [Base64 decode:@"MDAwMDAwNTB42vPMVkhKzVNIBeLsnNTMPB0IpVCWWZyVqpAJkalKTVUoS8xTSMpJLC0HALWrEYi="];

NSString * deBase64 = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];

int lengteOP = [[deBase64 substringWithRange:NSMakeRange(0,8)] intValue];
NSUInteger lengteIP = [deBase64 length];

const unsigned char *input = (const unsigned char *) [[deBase64 substringFromIndex:8]  cStringUsingEncoding:NSASCIIStringEncoding];
unsigned char * dest; 

uncompress(dest, lengteOP, input, lengteIP);

A get a EXC_BADD_ACCESS error when i try this.
The string is build with code in delphi using ZLib, same as the library in the iPhone sdk

its a base64 encode string with the first 8 characters representing the length of the string followed by the zlib-ed string.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不如归去 2024-11-25 07:31:39

我没有运行代码,但您的问题可能是dest。这是文档中的一个片段。

输入时,destLen 是总大小
目标缓冲区,它必须
足够大以容纳整个
未压缩的数据。

目标端需要在调用该函数之前分配内存,否则它将尝试将数据写入无效内存,从而导致 EXC_BAD_ACCESS。

请尝试以下操作:

unsigned char * dest = malloc(sizeof(unsigned char) * lengteOP);

uncompress(dest, lengteOP, input, lengteIP);

//Use dest (create NSString with proper encoding for example)

free(dest);

I did not run the code but your issue is likely to be dest. Here is a snippet from the documentation.

Upon entry, destLen is the total size
of the destination buffer, which must
be large enough to hold the entire
uncompressed data.

The destination needs to have the memory allocated before calling the function, otherwise it will attempt to write data to invalid memory causing EXC_BAD_ACCESS.

Try the following:

unsigned char * dest = malloc(sizeof(unsigned char) * lengteOP);

uncompress(dest, lengteOP, input, lengteIP);

//Use dest (create NSString with proper encoding for example)

free(dest);
浅沫记忆 2024-11-25 07:31:39

-完成的代码

/*
 Decompresses the source buffer into the destination buffer.  sourceLen is
 the byte length of the source buffer.  Upon entry, destLen is the total size
 of the destination buffer, which must be large enough to hold the entire
 uncompressed data.  (The size of the uncompressed data must have been saved
 previously by the compressor and transmitted to the decompressor by some
 mechanism outside the scope of this compression library.) Upon exit, destLen
 is the actual size of the uncompressed buffer.

 uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
 enough memory, Z_BUF_ERROR if there was not enough room in the output
 buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete.

 ZEXTERN int ZEXPORT uncompress OF((Bytef *dest,   uLongf *destLen,
 const Bytef *source, uLong sourceLen));

 */

[Base64 initialize];
NSData * data = [Base64 decode:@"MDAwMDAwNTB42vPMVkhKzVNIBeLsnNTMPB0IpVCWWZyVqpAJkalKTVUoS8xTSMpJLC0HALWrEYi="];

NSString * deBase64 = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];

uLongf lengthOriginal = [[deBase64 substringWithRange:NSMakeRange(0,8)] floatValue]; 
uLongf * lengteOP = malloc(sizeof(uLongf));

lengteOP = &lengthOriginal;

NSUInteger lengteIP = [deBase64 length];

NSString * codedString = [deBase64 substringFromIndex:8];

const unsigned char *input = (const unsigned char *) [codedString cStringUsingEncoding:NSISOLatin1StringEncoding];

unsigned char * dest = malloc((sizeof(unsigned char) * lengthOriginal));

uncompress(dest, lengteOP, input, lengteIP);

NSString *  bla = @"Decoded string :";  
NSLog([bla stringByAppendingString:[NSString stringWithCString:dest encoding:NSASCIIStringEncoding]]);



free(dest);
free(lengteOP);

- finished code

/*
 Decompresses the source buffer into the destination buffer.  sourceLen is
 the byte length of the source buffer.  Upon entry, destLen is the total size
 of the destination buffer, which must be large enough to hold the entire
 uncompressed data.  (The size of the uncompressed data must have been saved
 previously by the compressor and transmitted to the decompressor by some
 mechanism outside the scope of this compression library.) Upon exit, destLen
 is the actual size of the uncompressed buffer.

 uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
 enough memory, Z_BUF_ERROR if there was not enough room in the output
 buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete.

 ZEXTERN int ZEXPORT uncompress OF((Bytef *dest,   uLongf *destLen,
 const Bytef *source, uLong sourceLen));

 */

[Base64 initialize];
NSData * data = [Base64 decode:@"MDAwMDAwNTB42vPMVkhKzVNIBeLsnNTMPB0IpVCWWZyVqpAJkalKTVUoS8xTSMpJLC0HALWrEYi="];

NSString * deBase64 = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];

uLongf lengthOriginal = [[deBase64 substringWithRange:NSMakeRange(0,8)] floatValue]; 
uLongf * lengteOP = malloc(sizeof(uLongf));

lengteOP = &lengthOriginal;

NSUInteger lengteIP = [deBase64 length];

NSString * codedString = [deBase64 substringFromIndex:8];

const unsigned char *input = (const unsigned char *) [codedString cStringUsingEncoding:NSISOLatin1StringEncoding];

unsigned char * dest = malloc((sizeof(unsigned char) * lengthOriginal));

uncompress(dest, lengteOP, input, lengteIP);

NSString *  bla = @"Decoded string :";  
NSLog([bla stringByAppendingString:[NSString stringWithCString:dest encoding:NSASCIIStringEncoding]]);



free(dest);
free(lengteOP);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文