zip.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. Copyright (c) 2010, Florian Reuter
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. * Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. * Neither the name of Florian Reuter nor the names of its contributors
  14. may be used to endorse or promote products derived from this
  15. software without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  19. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  20. COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  22. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  25. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  27. OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. /** @file opc/zip.h
  30. The ZIP file backend of an OPC container.
  31. */
  32. #include <opc/config.h>
  33. #include <opc/file.h>
  34. #include <opc/container.h>
  35. #ifndef OPC_ZIP_H
  36. #define OPC_ZIP_H
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. /**
  41. Default growth hint of an OPC stream.
  42. */
  43. #define OPC_DEFAULT_GROWTH_HINT 512
  44. /**
  45. Handle to a ZIP archive.
  46. \see internal.h
  47. */
  48. typedef struct OPC_ZIP_STRUCT opcZip;
  49. /**
  50. Handle to a raw ZIP input stream.
  51. \see internal.h
  52. */
  53. typedef struct OPC_ZIPINPUTSTREAM_STRUCT opcZipInputStream;
  54. /**
  55. Handle to a raw ZIP output stream.
  56. \see internal.h
  57. */
  58. typedef struct OPC_ZIPOUTPUTSTREAM_STRUCT opcZipOutputStream;
  59. /**
  60. Holds all information of a ZIP segment.
  61. */
  62. typedef struct OPC_ZIP_SEGMENT_INFO_STRUCT {
  63. xmlChar name[OPC_MAX_PATH];
  64. opc_uint32_t name_len;
  65. opc_uint32_t segment_number;
  66. opc_bool_t last_segment;
  67. opc_bool_t rels_segment;
  68. opc_uint32_t header_size;
  69. opc_uint32_t min_header_size;
  70. opc_uint32_t trailing_bytes;
  71. opc_uint32_t compressed_size;
  72. opc_uint32_t uncompressed_size;
  73. opc_uint16_t bit_flag;
  74. opc_uint32_t data_crc;
  75. opc_uint16_t compression_method;
  76. opc_ofs_t stream_ofs;
  77. opc_uint16_t growth_hint;
  78. } opcZipSegmentInfo_t;
  79. /**
  80. \see opcZipLoader
  81. */
  82. typedef int opcZipLoaderOpenCallback(void *iocontext);
  83. /**
  84. \see opcZipLoader
  85. */
  86. typedef int opcZipLoaderSkipCallback(void *iocontext);
  87. /**
  88. \see opcZipLoader
  89. */
  90. typedef int opcZipLoaderReadCallback(void *iocontext, char *buffer, int len);
  91. /**
  92. \see opcZipLoader
  93. */
  94. typedef int opcZipLoaderCloseCallback(void *iocontext);
  95. /**
  96. \see opcZipLoader
  97. */
  98. typedef opc_error_t (opcZipLoaderSegmentCallback_t)(void *iocontext, void *userctx, opcZipSegmentInfo_t *info, opcZipLoaderOpenCallback *open, opcZipLoaderReadCallback *read, opcZipLoaderCloseCallback *close, opcZipLoaderSkipCallback *skip);
  99. /**
  100. Walks every segment in a ZIP archive and calls the \c segmentCallback callback method.
  101. The implementer \c segmentCallback method must then eiher use the passed \c open, \c read and \c close methods
  102. to read the stream or the passed \c skip methods to skip the stream.
  103. This method can be used to e.g. read ZIP file in stream mode.
  104. */
  105. opc_error_t opcZipLoader(opcIO_t *io, void *userctx, opcZipLoaderSegmentCallback_t *segmentCallback);
  106. /**
  107. \see opcZipClose
  108. */
  109. typedef opc_error_t (opcZipSegmentReleaseCallback)(opcZip *zip, opc_uint32_t segment_id);
  110. /**
  111. Closes the ZIP archive \c zip and will call \c releaseCallback for every segment to give the implementer a chance
  112. to free user resources.
  113. */
  114. void opcZipClose(opcZip *zip, opcZipSegmentReleaseCallback* releaseCallback);
  115. /**
  116. Creates an empty ZIP archive with the given \c io.
  117. */
  118. opcZip *opcZipCreate(opcIO_t *io);
  119. /**
  120. Commits all buffers and writes the ZIP archives local header directories.
  121. if \c trim is true then padding bytes will be removed, i.e. the ZIP file size fill be minimalized.
  122. */
  123. opc_error_t opcZipCommit(opcZip *zip, opc_bool_t trim);
  124. /**
  125. Garbage collection on the passed \c zip archive. This will e.g. make deleted files available as free space.
  126. */
  127. opc_error_t opcZipGC(opcZip *zip);
  128. /**
  129. Load segment information into \c info.
  130. If \c rels_segment is -1 then load the info for part with name \c partName.
  131. Otherwise load the segment information for the ".rels." segment of \c partName.
  132. \return Returns the segment_id.
  133. */
  134. opc_uint32_t opcZipLoadSegment(opcZip *zip, const xmlChar *partName, opc_bool_t rels_segment, opcZipSegmentInfo_t *info);
  135. /**
  136. Create a segment with the given parameters.
  137. \return Returns the segment_id.
  138. */
  139. opc_uint32_t opcZipCreateSegment(opcZip *zip,
  140. const xmlChar *partName,
  141. opc_bool_t relsSegment,
  142. opc_uint32_t segment_size,
  143. opc_uint32_t growth_hint,
  144. opc_uint16_t compression_method,
  145. opc_uint16_t bit_flag);
  146. /**
  147. Creates an input stream for the segment with \c segment_id.
  148. \see opcZipLoadSegment
  149. \see opcZipCreateSegment
  150. */
  151. opcZipInputStream *opcZipOpenInputStream(opcZip *zip, opc_uint32_t segment_id);
  152. /**
  153. Free all resources of the input stream.
  154. */
  155. opc_error_t opcZipCloseInputStream(opcZip *zip, opcZipInputStream *stream);
  156. /**
  157. Read maximal \c buf_len bytes from the input stream into \buf.
  158. \return Returns the number of bytes read.
  159. */
  160. opc_uint32_t opcZipReadInputStream(opcZip *zip, opcZipInputStream *stream, opc_uint8_t *buf, opc_uint32_t buf_len);
  161. /**
  162. Creates an output stream for the segment with \c segment_id.
  163. If \c *segment_id is -1 then a new segment will be created.
  164. Otherwise the segment with \c *segment_id will be overwritten.
  165. */
  166. opcZipOutputStream *opcZipCreateOutputStream(opcZip *zip,
  167. opc_uint32_t *segment_id,
  168. const xmlChar *partName,
  169. opc_bool_t relsSegment,
  170. opc_uint32_t segment_size,
  171. opc_uint32_t growth_hint,
  172. opc_uint16_t compression_method,
  173. opc_uint16_t bit_flag);
  174. /**
  175. Opens an existing ouput stream for reading.
  176. The \c *segment_id will be set to -1 and reset on opcZipCloseOutputStream.
  177. \see opcZipCloseOutputStream
  178. */
  179. opcZipOutputStream *opcZipOpenOutputStream(opcZip *zip, opc_uint32_t *segment_id);
  180. /**
  181. Will close the stream and free all resources. Additionally the new segment id will be stored in \c *segment_id.
  182. \see opcZipOpenOutputStream
  183. */
  184. opc_error_t opcZipCloseOutputStream(opcZip *zip, opcZipOutputStream *stream, opc_uint32_t *segment_id);
  185. /**
  186. Write \c buf_len bytes to \c buf.
  187. \return Returns the number of bytes written.
  188. */
  189. opc_uint32_t opcZipWriteOutputStream(opcZip *zip, opcZipOutputStream *stream, const opc_uint8_t *buf, opc_uint32_t buf_len);
  190. /**
  191. Returns the first segment id or -1.
  192. Use the following code to iterarte through all segments.
  193. \code
  194. for(opc_uint32_t segment_id=opcZipGetFirstSegmentId(zip);
  195. -1!=segment_id;
  196. segment_id=opcZipGetNextSegmentId(zip, segment_id) {
  197. ...
  198. }
  199. \endcode
  200. \see opcZipGetNextSegmentId
  201. */
  202. opc_uint32_t opcZipGetFirstSegmentId(opcZip *zip);
  203. /**
  204. Returns the next segment id or -1.
  205. \see opcZipGetFirstSegmentId
  206. */
  207. opc_uint32_t opcZipGetNextSegmentId(opcZip *zip, opc_uint32_t segment_id);
  208. /**
  209. Returns info about the given segment id.
  210. */
  211. opc_error_t opcZipGetSegmentInfo(opcZip *zip, opc_uint32_t segment_id, const xmlChar **name, opc_bool_t *rels_segment, opc_uint32_t *crc);
  212. /**
  213. Marks a given segments as deleted.
  214. \see opcZipGC
  215. */
  216. opc_bool_t opcZipSegmentDelete(opcZip *zip, opc_uint32_t *first_segment, opc_uint32_t *last_segment, opcZipSegmentReleaseCallback* releaseCallback);
  217. #ifdef __cplusplus
  218. } /* extern "C" */
  219. #endif
  220. #endif /* OPC_ZIP_H */