• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Is there any compression library for Cocoa?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Is there any compression library for Cocoa?


  • Subject: Re: Is there any compression library for Cocoa?
  • From: Trygve Inda <email@hidden>
  • Date: Wed, 30 Apr 2008 09:24:16 +0000
  • Thread-topic: Is there any compression library for Cocoa?

> There's a handy NSData-Compression class in Dustin Mierau's NetSocket
> 0.9 sample code available here:
>
> http://blackholemedia.com/code/
>
> Here's a snippet from the header file.
>
> @interface NSData (Compression)
> - (NSData*)compressedData;
> - (NSData*)compressedDataWithLevel:(int)inLevel;
> - (NSData*)uncompressedData;
> @end
>
> @interface NSMutableData (Compression)
> - (BOOL)compress;
> - (BOOL)uncompress;
> @end

This code (from above) appears to leak:

- (NSData*)compressedDataWithLevel:(int)inLevel
{
    Bytef*    data = (Bytef*)[self bytes];
    uLongf    originalLength = [self length];
    uLongf    bufferLength = ( [self length] * 1.1 ) + 16;
    Bytef*    buffer;
    int        err;

    if( inLevel > NSDataCompressionBest )
        inLevel = NSDataCompressionBest;

    if( inLevel < Z_DEFAULT_COMPRESSION )
        inLevel = NSDataCompressionNone;

    buffer = (Bytef*)malloc( (uInt)bufferLength );
    err = compress2( buffer, &bufferLength, (const Bytef*)data, [self
length], inLevel );
    if( err != Z_OK )
    {
        free( buffer );
        return nil;
    }

    bcopy( buffer, buffer + sizeof( uLongf ), bufferLength );
    bcopy( (void*)&originalLength, buffer, sizeof( uLongf ) );

    return [NSData dataWithBytesNoCopy:buffer length:bufferLength + sizeof(
uLongf )];
}


If the compression is successful, and I later release the (compressed)
NSData, buffer is still around, right?

Is there an update to this code?

How could I add buffer to a pool of sorts to be released when the object is
released as it is just malloc'd.

Thanks,

Trygve


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Is there any compression library for Cocoa?
      • From: Luc Heinrich <email@hidden>
    • Re: Is there any compression library for Cocoa?
      • From: email@hidden
  • Prev by Date: valueForKey to call function
  • Next by Date: Re: valueForKey to call function
  • Previous by thread: Re: valueForKey to call function
  • Next by thread: Re: Is there any compression library for Cocoa?
  • Index(es):
    • Date
    • Thread