2011-07-05 3 views
0

내 앱에 간단한 stringdecompression 알고리즘을 만들려고합니다.이 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 오류가 발생합니다. 문자열 아이폰 SDK

의 라이브러리와 같은 ZLIB를 사용 델파이 코드 빌드 인 그것 ZLIB 혼성 문자열 뒤에 문자열의 길이를 나타내는 제 8 개 문자 base64로 인 코드 스트링.

답변

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'는 한 번만 호출되도록 보장되므로 해당 함수의 코드가 여러 호출을 처리하지 못할 수 있습니다. # 2는 malloc 호출을 누설 한 lengthOriginal의 주소에 다음 줄을 재 할당하기 때문에 malloc을 호출하지 않습니다. 그러면 free (legnteOP)를 제거합니다. 너는 정말로 legnteOP가 전혀 필요 없다. – Joe