1313
1414@implementation NSData (QNGZip)
1515
16- - (NSData *)qn_gZip {
16+ + (NSData *)qn_gZip : ( NSData *) data {
1717
18- if (self .length == 0 || [self qn_isGzippedData ]){
19- return self ;
18+ if (data .length == 0 || [self qn_isGzippedData: data ]){
19+ return data ;
2020 }
2121
2222 z_stream stream;
@@ -25,8 +25,8 @@ - (NSData *)qn_gZip{
2525 stream.zfree = Z_NULL;
2626 stream.total_out = 0 ;
2727 stream.avail_out = 0 ;
28- stream.avail_in = (uint)self .length ;
29- stream.next_in = (Bytef *)(void *)self .bytes ;
28+ stream.avail_in = (uint)data .length ;
29+ stream.next_in = (Bytef *)(void *)data .bytes ;
3030
3131 static const NSUInteger chunkSize = 16384 ;
3232
@@ -49,26 +49,26 @@ - (NSData *)qn_gZip{
4949 return gzippedData;
5050}
5151
52- - (NSData *)qn_gUnzip {
53- if (self .length == 0 || ![self qn_isGzippedData ]){
54- return self ;
52+ + (NSData *)qn_gUnzip : ( NSData *) data {
53+ if (data .length == 0 || ![self qn_isGzippedData: data ]){
54+ return data ;
5555 }
5656
5757 z_stream stream;
5858 stream.zalloc = Z_NULL;
5959 stream.zfree = Z_NULL;
6060 stream.total_out = 0 ;
6161 stream.avail_out = 0 ;
62- stream.avail_in = (uint)self .length ;
63- stream.next_in = (Bytef *)self .bytes ;
62+ stream.avail_in = (uint)data .length ;
63+ stream.next_in = (Bytef *)data .bytes ;
6464
6565 NSMutableData *gunzippedData = nil ;
6666 if (inflateInit2 (&stream, 47 ) == Z_OK) {
6767 int status = Z_OK;
68- gunzippedData = [NSMutableData dataWithCapacity: self .length * 2 ];
68+ gunzippedData = [NSMutableData dataWithCapacity: data .length * 2 ];
6969 while (status == Z_OK) {
7070 if (stream.total_out >= gunzippedData.length ) {
71- gunzippedData.length += self .length / 2 ;
71+ gunzippedData.length += data .length / 2 ;
7272 }
7373 stream.next_out = (uint8_t *)gunzippedData.mutableBytes + stream.total_out ;
7474 stream.avail_out = (uInt)(gunzippedData.length - stream.total_out );
@@ -84,9 +84,12 @@ - (NSData *)qn_gUnzip{
8484 return gunzippedData;
8585}
8686
87- - (BOOL )qn_isGzippedData {
88- const UInt8 *bytes = (const UInt8 *)self.bytes ;
89- return (self.length >= 2 && bytes[0 ] == 0x1f && bytes[1 ] == 0x8b );
87+ + (BOOL )qn_isGzippedData : (NSData *)data {
88+ if (!data || data.length == 0 ) {
89+ return false ;
90+ }
91+ const UInt8 *bytes = (const UInt8 *)data.bytes ;
92+ return (data.length >= 2 && bytes[0 ] == 0x1f && bytes[1 ] == 0x8b );
9093}
9194
9295@end
0 commit comments