lha_reader.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. Copyright (c) 2011, 2012, Simon Howard
  3. Permission to use, copy, modify, and/or distribute this software
  4. for any purpose with or without fee is hereby granted, provided
  5. that the above copyright notice and this permission notice appear
  6. in all copies.
  7. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  8. WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  9. WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  10. AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
  11. CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  12. LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifndef LHASA_PUBLIC_LHA_READER_H
  17. #define LHASA_PUBLIC_LHA_READER_H
  18. #include "lha_decoder.h"
  19. #include "lha_input_stream.h"
  20. #include "lha_file_header.h"
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. /**
  25. * @file lha_reader.h
  26. *
  27. * @brief LHA file reader.
  28. *
  29. * This file contains the interface functions for the @ref LHAReader
  30. * structure, used to decode data from a compressed LZH file and
  31. * extract compressed files.
  32. */
  33. /**
  34. * Opaque structure used to decode the contents of an LZH file.
  35. */
  36. typedef struct _LHAReader LHAReader;
  37. /**
  38. * Policy for extracting directories.
  39. *
  40. * When extracting a directory, some of the metadata associated with
  41. * it needs to be set after the contents of the directory have been
  42. * extracted. This includes the modification time (which would
  43. * otherwise be reset to the current time) and the permissions (which
  44. * can affect the ability to extract files into the directory).
  45. * To work around this problem there are several ways of handling
  46. * directory extraction.
  47. */
  48. typedef enum {
  49. /**
  50. * "Plain" policy. In this mode, the metadata is set at the
  51. * same time that the directory is created. This is the
  52. * simplest to comprehend, and the files returned from
  53. * @ref lha_reader_next_file will match the files in the
  54. * archive, but it is not recommended.
  55. */
  56. LHA_READER_DIR_PLAIN,
  57. /**
  58. * "End of directory" policy. In this mode, if a directory
  59. * is extracted, the directory name will be saved. Once the
  60. * contents of the directory appear to have been extracted
  61. * (ie. a file is found that is not within the directory),
  62. * the directory will be returned again by
  63. * @ref lha_reader_next_file. This time, when the directory
  64. * is "extracted" (via @ref lha_reader_extract), the metadata
  65. * will be set.
  66. *
  67. * This method uses less memory than
  68. * @ref LHA_READER_DIR_END_OF_FILE, but there is the risk
  69. * that a file will appear within the archive after the
  70. * metadata has been set for the directory. However, this is
  71. * not normally the case, as files and directories typically
  72. * appear within an archive in order. GNU tar uses the same
  73. * method to address this problem with tar files.
  74. *
  75. * This is the default policy.
  76. */
  77. LHA_READER_DIR_END_OF_DIR,
  78. /**
  79. * "End of file" policy. In this mode, each directory that
  80. * is extracted is recorded in a list. When the end of the
  81. * archive is reached, these directories are returned again by
  82. * @ref lha_reader_next_file. When the directories are
  83. * "extracted" again (via @ref lha_reader_extract), the
  84. * metadata is set.
  85. *
  86. * This avoids the problems that can potentially occur with
  87. * @ref LHA_READER_DIR_END_OF_DIR, but uses more memory.
  88. */
  89. LHA_READER_DIR_END_OF_FILE
  90. } LHAReaderDirPolicy;
  91. /**
  92. * Create a new @ref LHAReader to read data from an @ref LHAInputStream.
  93. *
  94. * @param stream The input stream to read data from.
  95. * @return Pointer to a new @ref LHAReader structure,
  96. * or NULL for error.
  97. */
  98. LHAReader *lha_reader_new(LHAInputStream *stream);
  99. /**
  100. * Free a @ref LHAReader structure.
  101. *
  102. * @param reader The @ref LHAReader structure.
  103. */
  104. void lha_reader_free(LHAReader *reader);
  105. /**
  106. * Set the @ref LHAReaderDirPolicy used to extract directories.
  107. *
  108. * @param reader The @ref LHAReader structure.
  109. * @param policy The policy to use for directories.
  110. */
  111. void lha_reader_set_dir_policy(LHAReader *reader,
  112. LHAReaderDirPolicy policy);
  113. /**
  114. * Read the header of the next archived file from the input stream.
  115. *
  116. * @param reader The @ref LHAReader structure.
  117. * @return Pointer to an @ref LHAFileHeader structure, or NULL if
  118. * an error occurred. This pointer is only valid until
  119. * the next time that lha_reader_next_file is called.
  120. */
  121. LHAFileHeader *lha_reader_next_file(LHAReader *reader);
  122. /**
  123. * Read some of the (decompresed) data for the current archived file,
  124. * decompressing as appropriate.
  125. *
  126. * @param reader The @ref LHAReader structure.
  127. * @param buf Pointer to a buffer in which to store the data.
  128. * @param buf_len Size of the buffer, in bytes.
  129. * @return Number of bytes stored in the buffer, or zero if
  130. * there is no more data to decompress.
  131. */
  132. size_t lha_reader_read(LHAReader *reader, void *buf, size_t buf_len);
  133. /**
  134. * Decompress the contents of the current archived file, and check
  135. * that the checksum matches correctly.
  136. *
  137. * @param reader The @ref LHAReader structure.
  138. * @param callback Callback function to invoke to monitor progress (or
  139. * NULL if progress does not need to be monitored).
  140. * @param callback_data Extra data to pass to the callback function.
  141. * @return Non-zero if the checksum matches.
  142. */
  143. int lha_reader_check(LHAReader *reader,
  144. LHADecoderProgressCallback callback,
  145. void *callback_data);
  146. /**
  147. * Extract the contents of the current archived file.
  148. *
  149. * @param reader The @ref LHAReader structure.
  150. * @param filename Filename to extract the archived file to, or NULL
  151. * to use the path and filename from the header.
  152. * @param callback Callback function to invoke to monitor progress (or
  153. * NULL if progress does not need to be monitored).
  154. * @param callback_data Extra data to pass to the callback function.
  155. * @return Non-zero for success, or zero for failure (including
  156. * CRC error).
  157. */
  158. int lha_reader_extract(LHAReader *reader,
  159. char *filename,
  160. LHADecoderProgressCallback callback,
  161. void *callback_data);
  162. /**
  163. * Check if the current file (last returned by @ref lha_reader_next_file)
  164. * was generated internally by the extract process. This occurs when a
  165. * directory or symbolic link must be created as a two-stage process, with
  166. * some of the extraction process deferred to later in the stream.
  167. *
  168. * These "fake" duplicates should usually be hidden in the user interface
  169. * when a summary of extraction is presented.
  170. *
  171. * @param reader The @ref LHAReader structure.
  172. * @return Non-zero if the current file is a "fake", or zero
  173. * for a normal file.
  174. */
  175. int lha_reader_current_is_fake(LHAReader *reader);
  176. #ifdef __cplusplus
  177. }
  178. #endif
  179. #endif /* #ifndef LHASA_PUBLIC_LHA_READER_H */