Guest User

Untitled

a guest
Jun 19th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.65 KB | None | 0 0
  1. @implementation NSData (KDataCompressionCategory)
  2. - (NSData *)zlibInflate
  3. {
  4.     if ([self length] == 0)
  5.         return self;
  6.    
  7.     z_stream strm;
  8.     strm.next_in    = (Bytef *)[self bytes];
  9.     strm.avail_in   = (uInt)[self length];
  10.     strm.total_out  = 0;
  11.     strm.zalloc     = Z_NULL;
  12.     strm.zfree      = Z_NULL;
  13.    
  14.     if (Z_OK != inflateInit(&strm))
  15.     {
  16.         NSLog(@"%s: inflateInit() failed!\n", __PRETTY_FUNCTION__);
  17.         return nil;
  18.     }
  19.    
  20.     NSMutableData *outData = [[[NSMutableData alloc] init] autorelease];
  21.     Bytef tmpBuf[KZLibInflateBufferSize] = {0};
  22.    
  23.     if (!outData)
  24.         return nil;
  25.    
  26.     int status = Z_OK;
  27.     BOOL bDone = NO;
  28.    
  29.     while (!bDone)
  30.     {
  31.         strm.next_out   = tmpBuf;
  32.         strm.avail_out  = KZLibInflateBufferSize;
  33.        
  34.         status = inflate(&strm, Z_SYNC_FLUSH);
  35.        
  36.         switch (status)
  37.         {
  38.             case Z_OK:
  39.             case Z_STREAM_END:
  40.             {
  41.                 // Process this block
  42.                 [outData appendBytes:tmpBuf length:(KZLibInflateBufferSize - strm.avail_out)];
  43.                
  44.                 if (Z_STREAM_END == status)
  45.                     bDone = YES;
  46.                 break;
  47.             }
  48.             default:
  49.             {
  50.                 NSLog(@"%s: ZLib inflate() failed with status %d!\n", __PRETTY_FUNCTION__, status);
  51.                 return nil;
  52.             }
  53.  
  54.         }
  55.     }
  56.    
  57.     if (Z_OK != inflateEnd(&strm))
  58.         return nil;
  59.    
  60.     if (bDone)
  61.     {
  62.         return [[NSData dataWithData:outData] autorelease];
  63.     }
  64.     else
  65.     {
  66.         return nil;
  67.     }
  68. }
  69. @end
Add Comment
Please, Sign In to add comment