You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
9.4 KiB

6 years ago
  1. //
  2. // GTMNSData+zlib.h
  3. //
  4. // Copyright 2007-2008 Google Inc.
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7. // use this file except in compliance with the License. You may obtain a copy
  8. // of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15. // License for the specific language governing permissions and limitations under
  16. // the License.
  17. //
  18. #import <Foundation/Foundation.h>
  19. #import "GTMDefines.h"
  20. /// Helpers for dealing w/ zlib inflate/deflate calls.
  21. @interface NSData (GTMZLibAdditions)
  22. // NOTE: For 64bit, none of these apis handle input sizes >32bits, they will
  23. // return nil when given such data. To handle data of that size you really
  24. // should be streaming it rather then doing it all in memory.
  25. #pragma mark Gzip Compression
  26. /// Return an autoreleased NSData w/ the result of gzipping the bytes.
  27. //
  28. // Uses the default compression level.
  29. + (NSData *)gtm_dataByGzippingBytes:(const void *)bytes
  30. length:(NSUInteger)length;
  31. + (NSData *)gtm_dataByGzippingBytes:(const void *)bytes
  32. length:(NSUInteger)length
  33. error:(NSError **)error;
  34. /// Return an autoreleased NSData w/ the result of gzipping the payload of |data|.
  35. //
  36. // Uses the default compression level.
  37. + (NSData *)gtm_dataByGzippingData:(NSData *)data __attribute__((deprecated("Use error variant")));
  38. + (NSData *)gtm_dataByGzippingData:(NSData *)data
  39. error:(NSError **)error;
  40. /// Return an autoreleased NSData w/ the result of gzipping the bytes using |level| compression level.
  41. //
  42. // |level| can be 1-9, any other values will be clipped to that range.
  43. + (NSData *)gtm_dataByGzippingBytes:(const void *)bytes
  44. length:(NSUInteger)length
  45. compressionLevel:(int)level __attribute__((deprecated("Use error variant")));
  46. + (NSData *)gtm_dataByGzippingBytes:(const void *)bytes
  47. length:(NSUInteger)length
  48. compressionLevel:(int)level
  49. error:(NSError **)error;
  50. /// Return an autoreleased NSData w/ the result of gzipping the payload of |data| using |level| compression level.
  51. + (NSData *)gtm_dataByGzippingData:(NSData *)data
  52. compressionLevel:(int)level __attribute__((deprecated("Use error variant")));
  53. + (NSData *)gtm_dataByGzippingData:(NSData *)data
  54. compressionLevel:(int)level
  55. error:(NSError **)error;
  56. #pragma mark Zlib "Stream" Compression
  57. // NOTE: deflate is *NOT* gzip. deflate is a "zlib" stream. pick which one
  58. // you really want to create. (the inflate api will handle either)
  59. /// Return an autoreleased NSData w/ the result of deflating the bytes.
  60. //
  61. // Uses the default compression level.
  62. + (NSData *)gtm_dataByDeflatingBytes:(const void *)bytes
  63. length:(NSUInteger)length __attribute__((deprecated("Use error variant")));
  64. + (NSData *)gtm_dataByDeflatingBytes:(const void *)bytes
  65. length:(NSUInteger)length
  66. error:(NSError **)error;
  67. /// Return an autoreleased NSData w/ the result of deflating the payload of |data|.
  68. //
  69. // Uses the default compression level.
  70. + (NSData *)gtm_dataByDeflatingData:(NSData *)data __attribute__((deprecated("Use error variant")));
  71. + (NSData *)gtm_dataByDeflatingData:(NSData *)data
  72. error:(NSError **)error;
  73. /// Return an autoreleased NSData w/ the result of deflating the bytes using |level| compression level.
  74. //
  75. // |level| can be 1-9, any other values will be clipped to that range.
  76. + (NSData *)gtm_dataByDeflatingBytes:(const void *)bytes
  77. length:(NSUInteger)length
  78. compressionLevel:(int)level __attribute__((deprecated("Use error variant")));
  79. + (NSData *)gtm_dataByDeflatingBytes:(const void *)bytes
  80. length:(NSUInteger)length
  81. compressionLevel:(int)level
  82. error:(NSError **)error;
  83. /// Return an autoreleased NSData w/ the result of deflating the payload of |data| using |level| compression level.
  84. + (NSData *)gtm_dataByDeflatingData:(NSData *)data
  85. compressionLevel:(int)level __attribute__((deprecated("Use error variant")));
  86. + (NSData *)gtm_dataByDeflatingData:(NSData *)data
  87. compressionLevel:(int)level
  88. error:(NSError **)error;
  89. #pragma mark Uncompress of Gzip or Zlib
  90. /// Return an autoreleased NSData w/ the result of decompressing the bytes.
  91. //
  92. // The bytes to decompress can be zlib or gzip payloads.
  93. + (NSData *)gtm_dataByInflatingBytes:(const void *)bytes
  94. length:(NSUInteger)length __attribute__((deprecated("Use error variant")));
  95. + (NSData *)gtm_dataByInflatingBytes:(const void *)bytes
  96. length:(NSUInteger)length
  97. error:(NSError **)error;
  98. /// Return an autoreleased NSData w/ the result of decompressing the payload of |data|.
  99. //
  100. // The data to decompress can be zlib or gzip payloads.
  101. + (NSData *)gtm_dataByInflatingData:(NSData *)data __attribute__((deprecated("Use error variant")));
  102. + (NSData *)gtm_dataByInflatingData:(NSData *)data
  103. error:(NSError **)error;
  104. #pragma mark "Raw" Compression Support
  105. // NOTE: raw deflate is *NOT* gzip or deflate. it does not include a header
  106. // of any form and should only be used within streams here an external crc/etc.
  107. // is done to validate the data. The RawInflate apis can be used on data
  108. // processed like this.
  109. /// Return an autoreleased NSData w/ the result of *raw* deflating the bytes.
  110. //
  111. // Uses the default compression level.
  112. // *No* header is added to the resulting data.
  113. + (NSData *)gtm_dataByRawDeflatingBytes:(const void *)bytes
  114. length:(NSUInteger)length __attribute__((deprecated("Use error variant")));
  115. + (NSData *)gtm_dataByRawDeflatingBytes:(const void *)bytes
  116. length:(NSUInteger)length
  117. error:(NSError **)error;
  118. /// Return an autoreleased NSData w/ the result of *raw* deflating the payload of |data|.
  119. //
  120. // Uses the default compression level.
  121. // *No* header is added to the resulting data.
  122. + (NSData *)gtm_dataByRawDeflatingData:(NSData *)data __attribute__((deprecated("Use error variant")));
  123. + (NSData *)gtm_dataByRawDeflatingData:(NSData *)data
  124. error:(NSError **)error;
  125. /// Return an autoreleased NSData w/ the result of *raw* deflating the bytes using |level| compression level.
  126. //
  127. // |level| can be 1-9, any other values will be clipped to that range.
  128. // *No* header is added to the resulting data.
  129. + (NSData *)gtm_dataByRawDeflatingBytes:(const void *)bytes
  130. length:(NSUInteger)length
  131. compressionLevel:(int)level __attribute__((deprecated("Use error variant")));
  132. + (NSData *)gtm_dataByRawDeflatingBytes:(const void *)bytes
  133. length:(NSUInteger)length
  134. compressionLevel:(int)level
  135. error:(NSError **)error;
  136. /// Return an autoreleased NSData w/ the result of *raw* deflating the payload of |data| using |level| compression level.
  137. // *No* header is added to the resulting data.
  138. + (NSData *)gtm_dataByRawDeflatingData:(NSData *)data
  139. compressionLevel:(int)level __attribute__((deprecated("Use error variant")));
  140. + (NSData *)gtm_dataByRawDeflatingData:(NSData *)data
  141. compressionLevel:(int)level
  142. error:(NSError **)error;
  143. /// Return an autoreleased NSData w/ the result of *raw* decompressing the bytes.
  144. //
  145. // The data to decompress, it should *not* have any header (zlib nor gzip).
  146. + (NSData *)gtm_dataByRawInflatingBytes:(const void *)bytes
  147. length:(NSUInteger)length __attribute__((deprecated("Use error variant")));
  148. + (NSData *)gtm_dataByRawInflatingBytes:(const void *)bytes
  149. length:(NSUInteger)length
  150. error:(NSError **)error;
  151. /// Return an autoreleased NSData w/ the result of *raw* decompressing the payload of |data|.
  152. //
  153. // The data to decompress, it should *not* have any header (zlib nor gzip).
  154. + (NSData *)gtm_dataByRawInflatingData:(NSData *)data __attribute__((deprecated("Use error variant")));
  155. + (NSData *)gtm_dataByRawInflatingData:(NSData *)data
  156. error:(NSError **)error;
  157. @end
  158. FOUNDATION_EXPORT NSString *const GTMNSDataZlibErrorDomain;
  159. FOUNDATION_EXPORT NSString *const GTMNSDataZlibErrorKey; // NSNumber
  160. FOUNDATION_EXPORT NSString *const GTMNSDataZlibRemainingBytesKey; // NSNumber
  161. typedef NS_ENUM(NSInteger, GTMNSDataZlibError) {
  162. GTMNSDataZlibErrorGreaterThan32BitsToCompress = 1024,
  163. // An internal zlib error.
  164. // GTMNSDataZlibErrorKey will contain the error value.
  165. // NSLocalizedDescriptionKey may contain an error string from zlib.
  166. // Look in zlib.h for list of errors.
  167. GTMNSDataZlibErrorInternal,
  168. // There was left over data in the buffer that was not used.
  169. // GTMNSDataZlibRemainingBytesKey will contain number of remaining bytes.
  170. GTMNSDataZlibErrorDataRemaining
  171. };