ZipUtils.h 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2017 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #ifndef __SUPPORT_ZIPUTILS_H__
  22. #define __SUPPORT_ZIPUTILS_H__
  23. /// @cond DO_NOT_SHOW
  24. #include <string>
  25. /**
  26. * @addtogroup base
  27. * @{
  28. */
  29. #ifndef _unz64_H
  30. typedef struct unz_file_info_s unz_file_info;
  31. #endif
  32. /** XXX: pragma pack ???
  33. * @struct CCZHeader
  34. */
  35. struct CCZHeader {
  36. unsigned char sig[4]; /** Signature. Should be 'CCZ!' 4 bytes. */
  37. unsigned short compression_type; /** Should be 0. */
  38. unsigned short version; /** Should be 2 (although version type==1 is also supported). */
  39. unsigned int reserved; /** Reserved for users. */
  40. unsigned int len; /** Size of the uncompressed file. */
  41. };
  42. enum {
  43. CCZ_COMPRESSION_ZLIB, /** zlib format. */
  44. CCZ_COMPRESSION_BZIP2, /** bzip2 format (not supported yet). */
  45. CCZ_COMPRESSION_GZIP, /** gzip format (not supported yet). */
  46. CCZ_COMPRESSION_NONE, /** plain (not supported yet). */
  47. };
  48. class ZipUtils
  49. {
  50. public:
  51. /**
  52. * Inflates either zlib or gzip deflated memory. The inflated memory is expected to be freed by the caller.
  53. *
  54. * It will allocate 256k for the destination buffer. If it is not enough it will multiply the previous buffer size per 2, until there is enough memory.
  55. *
  56. * @return The length of the deflated buffer.
  57. * @since v0.8.1
  58. */
  59. static ssize_t ccInflateMemory(unsigned char *in, ssize_t inLength, unsigned char **out) { return inflateMemory(in, inLength, out); }
  60. static ssize_t inflateMemory(unsigned char *in, ssize_t inLength, unsigned char **out);
  61. /**
  62. * Inflates either zlib or gzip deflated memory. The inflated memory is expected to be freed by the caller.
  63. *
  64. * @param outLengthHint It is assumed to be the needed room to allocate the inflated buffer.
  65. *
  66. * @return The length of the deflated buffer.
  67. * @since v1.0.0
  68. */
  69. static ssize_t ccInflateMemoryWithHint(unsigned char *in, ssize_t inLength, unsigned char **out, ssize_t outLengthHint) { return inflateMemoryWithHint(in, inLength, out, outLengthHint); }
  70. static ssize_t inflateMemoryWithHint(unsigned char *in, ssize_t inLength, unsigned char **out, ssize_t outLengthHint);
  71. private:
  72. static int inflateMemoryWithHint(unsigned char *in, ssize_t inLength, unsigned char **out, ssize_t *outLength, ssize_t outLengthHint);
  73. };
  74. // end group
  75. /// @}
  76. /// @endcond
  77. #endif // __SUPPORT_ZIPUTILS_H__