lha_decoder.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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_DECODER_H
  17. #define LHASA_PUBLIC_LHA_DECODER_H
  18. #include <stdlib.h>
  19. #include <inttypes.h>
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. /**
  24. * @file lha_decoder.h
  25. *
  26. * @brief Raw LHA data decoder.
  27. *
  28. * This file defines the interface to the decompression code, which can
  29. * be used to decompress the raw compressed data from an LZH file.
  30. *
  31. * Implementations of the various compression algorithms used in LZH
  32. * archives are provided - these are represented by the
  33. * @ref LHADecoderType structure, and can be retrieved using the
  34. * @ref lha_decoder_for_name function. One of these can then be passed to
  35. * the @ref lha_decoder_new function to create a @ref LHADecoder structure
  36. * and decompress the data.
  37. */
  38. /**
  39. * Opaque type representing a type of decoder.
  40. *
  41. * This is an implementation of the decompression code for one of the
  42. * algorithms used in LZH archive files. Pointers to these structures are
  43. * retrieved by using the @ref lha_decoder_for_name function.
  44. */
  45. typedef struct _LHADecoderType LHADecoderType;
  46. /**
  47. * Opaque type representing an instance of a decoder.
  48. *
  49. * This is a decoder structure being used to decompress a stream of
  50. * compressed data. Instantiated using the @ref lha_decoder_new
  51. * function and freed using the @ref lha_decoder_free function.
  52. */
  53. typedef struct _LHADecoder LHADecoder;
  54. /**
  55. * Callback function invoked when a decoder wants to read more compressed
  56. * data.
  57. *
  58. * @param buf Pointer to the buffer in which to store the data.
  59. * @param buf_len Size of the buffer, in bytes.
  60. * @param user_data Extra pointer to pass to the decoder.
  61. * @return Number of bytes read.
  62. */
  63. typedef size_t (*LHADecoderCallback)(void *buf, size_t buf_len,
  64. void *user_data);
  65. /**
  66. * Callback function used for monitoring decode progress.
  67. * The callback is invoked for every block processed (block size depends on
  68. * decode algorithm).
  69. *
  70. * @param num_blocks Number of blocks processed so far.
  71. * @param total_blocks Total number of blocks to process.
  72. * @paaram callback_data Extra user-specified data passed to the callback.
  73. */
  74. typedef void (*LHADecoderProgressCallback)(unsigned int num_blocks,
  75. unsigned int total_blocks,
  76. void *callback_data);
  77. /**
  78. * Get the decoder type for the specified name.
  79. *
  80. * @param name String identifying the decoder type, for
  81. * example, "-lh1-".
  82. * @return Pointer to the decoder type, or NULL if there
  83. * is no decoder type for the specified name.
  84. */
  85. LHADecoderType *lha_decoder_for_name(char *name);
  86. /**
  87. * Allocate a new decoder for the specified type.
  88. *
  89. * @param dtype The decoder type.
  90. * @param callback Callback function for the decoder to call to read
  91. * more compressed data.
  92. * @param callback_data Extra data to pass to the callback function.
  93. * @param stream_length Length of the uncompressed data, in bytes. When
  94. * this point is reached, decompression will stop.
  95. * @return Pointer to the new decoder, or NULL for failure.
  96. */
  97. LHADecoder *lha_decoder_new(LHADecoderType *dtype,
  98. LHADecoderCallback callback,
  99. void *callback_data,
  100. size_t stream_length);
  101. /**
  102. * Free a decoder.
  103. *
  104. * @param decoder The decoder to free.
  105. */
  106. void lha_decoder_free(LHADecoder *decoder);
  107. /**
  108. * Set a callback function to monitor decode progress.
  109. *
  110. * @param decoder The decoder.
  111. * @param callback Callback function to monitor decode progress.
  112. * @param callback_data Extra data to pass to the decoder.
  113. */
  114. void lha_decoder_monitor(LHADecoder *decoder,
  115. LHADecoderProgressCallback callback,
  116. void *callback_data);
  117. /**
  118. * Decode (decompress) more data.
  119. *
  120. * @param decoder The decoder.
  121. * @param buf Pointer to buffer to store decompressed data.
  122. * @param buf_len Size of the buffer, in bytes.
  123. * @return Number of bytes decompressed.
  124. */
  125. size_t lha_decoder_read(LHADecoder *decoder, uint8_t *buf, size_t buf_len);
  126. /**
  127. * Get the current 16-bit CRC of the decompressed data.
  128. *
  129. * This should be called at the end of decompression to check that the
  130. * data was extracted correctly, and the value compared against the CRC
  131. * from the file header.
  132. *
  133. * @param decoder The decoder.
  134. * @return 16-bit CRC of the data decoded so far.
  135. */
  136. uint16_t lha_decoder_get_crc(LHADecoder *decoder);
  137. /**
  138. * Get the count of the number of bytes decoded.
  139. *
  140. * This should be called at the end of decompression, and the value
  141. * compared against the file length from the file header.
  142. *
  143. * @param decoder The decoder.
  144. * @return The number of decoded bytes.
  145. */
  146. size_t lha_decoder_get_length(LHADecoder *decoder);
  147. #ifdef __cplusplus
  148. }
  149. #endif
  150. #endif /* #ifndef LHASA_LHA_DECODER_H */