2011-07-05 46 views
0

我想为我的应用程序创建一个简单的字符串解压缩算法。此zlib字符串解压缩有什么问题?

/* 
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,相同在iPhone SDK

库中Delphi代码其一个base64编码串与表示字符串随后的zlib-ED字符串的长度的前8个字符。

回答

0

我没有运行代码,但您的问题可能是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); 
+0

坦克你了,我终于得到它在您的帮助工作。 – user829660

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. 

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); 
+0

很高兴它帮助你,想指出一些事情。 #1不要调用[Base64 initialize]; '+ initialize'保证只被调用一次,所以该函数中的代码可能无法处理多个调用。 #2不要malloc lengteOP,因为在下一行中,您将其重新分配给泄漏malloc调用的lengthOriginal的地址,然后删除空闲(legnteOP);你真的不需要legnteOP。 – Joe