libs-gsweb/GSWeb.framework/NSData+Compress.m
Manuel Guesdon 1e88f582b9 2003-05-14 Manuel Guesdon <mguesdon@orange-concept.com>
* GSWeb.framework/GSWRequest.h/.m:
		o added ivar _browserAcceptedEncodings
		o added -browserAcceptedEncodings
		o modified browserLanguages to use quality indicator
	* GSWeb.framework/GSWConstants.h/.m:
		o added GSWHTTPHeader_AcceptEncoding
	* GSWeb.framework/GSWResponse.h/.m:
		o added ivar _acceptedEncodings
		o added -acceptedEncodings
		o added -setAcceptedEncodings:
	* GSWeb.framework/GSWComponentRequestHandler.m:
		o set request  browserAcceptedEncodings to response
	* GSWeb.framework/GSWComponent.m:
		o set request  browserAcceptedEncodings to response
	* GSWeb.framework/NSData+Compress.h/.m:
		o added
	* config.h.in
		o added HAVE_ZLIB


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gsweb/trunk@16718 72102866-910b-0410-8b05-ffd578937521
2003-05-14 08:30:22 +00:00

169 lines
4.9 KiB
Objective-C

/** NSData+Compress.m - <title>GSWeb: NSData / zlib</title>
Copyright (C) 2003 Free Software Foundation, Inc.
Written by: Manuel Guesdon <mguesdon@orange-concept.com>
Date: May 2003
$Revision$
$Date$
$Id$
This file is part of the GNUstep Web Library.
<license>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
</license>
**/
#include "config.h"
RCS_ID("$Id$")
#include "GSWeb.h"
#ifdef HAVE_ZLIB
#include <zlib.h>
void GZPutLong(void* ptr,uLong value)
{
int n;
for (n = 0; n < 4; n++)
{
((unsigned char*)ptr)[n]=(value & 0xff);
value >>= 8;
}
};
static char gzMagic[2]= {0x1f, 0x8b}; // gzip magic header
static int gzHeaderSize=10;
//====================================================================
@implementation NSData (GSWZLib)
-(NSData*)deflate
{
NSMutableData* outData=nil;
z_stream c_stream; // compression stream
int err=Z_OK;
unsigned int selfLength=[self length];
c_stream.zalloc = (alloc_func)0;
c_stream.zfree = (free_func)0;
c_stream.opaque = (voidpf)0;
err = deflateInit2(&c_stream,
Z_BEST_COMPRESSION,
Z_DEFLATED,
-15,
9,
Z_DEFAULT_STRATEGY);
if (err!=Z_OK)
{
LOGError(@"deflateInit2 error: %d",err);
}
else
{
const void* inBytes=[self bytes];
NSMutableData* outTempData=[NSMutableData dataWithCapacity:max(1024,selfLength/10)];
void* outTempBytes=[outTempData bytes];
void* outBytes=NULL;
uLong crc = crc32(0L, Z_NULL, 0);
unsigned flushedData=0;
crc = crc32(crc,inBytes,selfLength);//calculate crc
outData=[NSMutableData dataWithCapacity:gzHeaderSize];
// gzip nead header !
[outData setLength:gzHeaderSize];
outBytes=[outData bytes];
((unsigned char*)outBytes)[0]=gzMagic[0];
((unsigned char*)outBytes)[1]=gzMagic[1];
((unsigned char*)outBytes)[2]=Z_DEFLATED;
((unsigned char*)outBytes)[3]=0; //flags
((unsigned char*)outBytes)[4]=0; //time
((unsigned char*)outBytes)[5]=0;//time
((unsigned char*)outBytes)[6]=0;//time
((unsigned char*)outBytes)[7]=0;//time
((unsigned char*)outBytes)[8]=2;//binary
((unsigned char*)outBytes)[9]=0x3;//OS
c_stream.next_in = inBytes;
c_stream.avail_in = (uInt)selfLength;
[outTempData setLength:[outTempData capacity]];
c_stream.next_out = outTempBytes;
c_stream.avail_out = (uInt)[outTempData capacity];
do
{
err = deflate(&c_stream, Z_NO_FLUSH);
if (err!=Z_OK)
{
LOGError(@"deflate error: %d",err);
}
else
{
if (c_stream.avail_out==0)
{
[outData appendData:outTempData];
flushedData+=[outTempData length];
c_stream.next_out = outTempBytes;
c_stream.avail_out = (uInt)[outTempData capacity];
[outTempData setLength:[outTempData capacity]];
};
};
}
while (c_stream.avail_in>0 && err==Z_OK);
if (err==Z_OK)
{
do
{
err = deflate(&c_stream, Z_FINISH);
if (err==Z_STREAM_END || err==Z_OK)
{
[outTempData setLength:c_stream.total_out-flushedData];
[outData appendData:outTempData];
flushedData+=[outTempData length];
c_stream.next_out = outTempBytes;
c_stream.avail_out = (uInt)[outTempData capacity];
[outTempData setLength:[outTempData capacity]];
}
else
{
LOGError(@"deflate error: %d",err);
};
} while (err == Z_OK);
};
if (err==Z_STREAM_END || err==Z_OK)
{
[outTempData setLength:8];
GZPutLong(outTempBytes, crc);
GZPutLong(outTempBytes+4, selfLength);
[outData appendData:outTempData];
};
err = deflateEnd(&c_stream);
if (err!=Z_OK)
{
LOGError(@"deflateEnd error: %d",err);
}
};
return outData;
}
@end
#endif