lha_decoder.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. #include <stdlib.h>
  17. #include <string.h>
  18. #include <limits.h>
  19. #include "crc16.h"
  20. #include "lha_decoder.h"
  21. // Null decoder, used for -lz4-, -lh0-, -pm0-:
  22. extern LHADecoderType lha_null_decoder;
  23. // LArc compression algorithms:
  24. extern LHADecoderType lha_lz5_decoder;
  25. extern LHADecoderType lha_lzs_decoder;
  26. // LHarc compression algorithms:
  27. extern LHADecoderType lha_lh1_decoder;
  28. extern LHADecoderType lha_lh4_decoder;
  29. extern LHADecoderType lha_lh5_decoder;
  30. extern LHADecoderType lha_lh6_decoder;
  31. extern LHADecoderType lha_lh7_decoder;
  32. extern LHADecoderType lha_lhx_decoder;
  33. // PMarc compression algorithms:
  34. extern LHADecoderType lha_pm1_decoder;
  35. extern LHADecoderType lha_pm2_decoder;
  36. static struct {
  37. char *name;
  38. LHADecoderType *dtype;
  39. } decoders[] = {
  40. { "-lz4-", &lha_null_decoder },
  41. { "-lz5-", &lha_lz5_decoder },
  42. { "-lzs-", &lha_lzs_decoder },
  43. { "-lh0-", &lha_null_decoder },
  44. { "-lh1-", &lha_lh1_decoder },
  45. { "-lh4-", &lha_lh4_decoder },
  46. { "-lh5-", &lha_lh5_decoder },
  47. { "-lh6-", &lha_lh6_decoder },
  48. { "-lh7-", &lha_lh7_decoder },
  49. { "-lhx-", &lha_lhx_decoder },
  50. { "-pm0-", &lha_null_decoder },
  51. { "-pm1-", &lha_pm1_decoder },
  52. { "-pm2-", &lha_pm2_decoder },
  53. };
  54. LHADecoder *lha_decoder_new(LHADecoderType *dtype,
  55. LHADecoderCallback callback,
  56. void *callback_data,
  57. size_t stream_length)
  58. {
  59. LHADecoder *decoder;
  60. void *extra_data;
  61. // Space is allocated together: the LHADecoder structure,
  62. // then the private data area used by the algorithm,
  63. // followed by the output buffer,
  64. decoder = calloc(1, sizeof(LHADecoder) + dtype->extra_size
  65. + dtype->max_read);
  66. if (decoder == NULL) {
  67. return NULL;
  68. }
  69. decoder->dtype = dtype;
  70. decoder->progress_callback = NULL;
  71. decoder->last_block = UINT_MAX;
  72. decoder->outbuf_pos = 0;
  73. decoder->outbuf_len = 0;
  74. decoder->stream_pos = 0;
  75. decoder->stream_length = stream_length;
  76. decoder->decoder_failed = 0;
  77. decoder->crc = 0;
  78. // Private data area follows the structure.
  79. extra_data = decoder + 1;
  80. decoder->outbuf = ((uint8_t *) extra_data) + dtype->extra_size;
  81. if (dtype->init != NULL
  82. && !dtype->init(extra_data, callback, callback_data)) {
  83. free(decoder);
  84. return NULL;
  85. }
  86. return decoder;
  87. }
  88. LHADecoderType *lha_decoder_for_name(char *name)
  89. {
  90. unsigned int i;
  91. for (i = 0; i < sizeof(decoders) / sizeof(*decoders); ++i) {
  92. if (!strcmp(name, decoders[i].name)) {
  93. return decoders[i].dtype;
  94. }
  95. }
  96. // Unknown?
  97. return NULL;
  98. }
  99. void lha_decoder_free(LHADecoder *decoder)
  100. {
  101. if (decoder->dtype->free != NULL) {
  102. decoder->dtype->free(decoder + 1);
  103. }
  104. free(decoder);
  105. }
  106. // Check if the stream has progressed far enough that the progress callback
  107. // should be invoked again.
  108. static void check_progress_callback(LHADecoder *decoder)
  109. {
  110. unsigned int block;
  111. block = (decoder->stream_pos + decoder->dtype->block_size - 1)
  112. / decoder->dtype->block_size;
  113. // If the stream has advanced by another block, invoke the callback
  114. // function. Invoke it multiple times if it has advanced by
  115. // more than one block.
  116. while (decoder->last_block != block) {
  117. ++decoder->last_block;
  118. decoder->progress_callback(decoder->last_block,
  119. decoder->total_blocks,
  120. decoder->progress_callback_data);
  121. }
  122. }
  123. void lha_decoder_monitor(LHADecoder *decoder,
  124. LHADecoderProgressCallback callback,
  125. void *callback_data)
  126. {
  127. decoder->progress_callback = callback;
  128. decoder->progress_callback_data = callback_data;
  129. decoder->total_blocks
  130. = (decoder->stream_length + decoder->dtype->block_size - 1)
  131. / decoder->dtype->block_size;
  132. check_progress_callback(decoder);
  133. }
  134. size_t lha_decoder_read(LHADecoder *decoder, uint8_t *buf, size_t buf_len)
  135. {
  136. size_t filled, bytes;
  137. // When we reach the end of the stream, we must truncate the
  138. // decompressed data at exactly the right point (stream_length),
  139. // or we may read a few extra false byte(s) by mistake.
  140. // Reduce buf_len when we get to the end to limit it to the
  141. // real number of remaining characters.
  142. if (decoder->stream_pos + buf_len > decoder->stream_length) {
  143. buf_len = decoder->stream_length - decoder->stream_pos;
  144. }
  145. // Try to fill up the buffer that has been passed with as much
  146. // data as possible. Each call to read() will fill up outbuf
  147. // with some data; this is then copied into buf, with some
  148. // data left at the end for the next call.
  149. filled = 0;
  150. while (filled < buf_len) {
  151. // Try to empty out some of the output buffer first.
  152. bytes = decoder->outbuf_len - decoder->outbuf_pos;
  153. if (buf_len - filled < bytes) {
  154. bytes = buf_len - filled;
  155. }
  156. memcpy(buf + filled, decoder->outbuf + decoder->outbuf_pos,
  157. bytes);
  158. decoder->outbuf_pos += bytes;
  159. filled += bytes;
  160. // If we previously encountered a failure reading from
  161. // the decoder, don't try to call the read function again.
  162. if (decoder->decoder_failed) {
  163. break;
  164. }
  165. // If outbuf is now empty, we can process another run to
  166. // re-fill it.
  167. if (decoder->outbuf_pos >= decoder->outbuf_len) {
  168. decoder->outbuf_len
  169. = decoder->dtype->read(decoder + 1,
  170. decoder->outbuf);
  171. decoder->outbuf_pos = 0;
  172. }
  173. // No more data to be read?
  174. if (decoder->outbuf_len == 0) {
  175. decoder->decoder_failed = 1;
  176. break;
  177. }
  178. }
  179. // Update CRC.
  180. lha_crc16_buf(&decoder->crc, buf, filled);
  181. // Track stream position.
  182. decoder->stream_pos += filled;
  183. // Check progress callback, if one is set:
  184. if (decoder->progress_callback != NULL) {
  185. check_progress_callback(decoder);
  186. }
  187. return filled;
  188. }
  189. uint16_t lha_decoder_get_crc(LHADecoder *decoder)
  190. {
  191. return decoder->crc;
  192. }
  193. size_t lha_decoder_get_length(LHADecoder *decoder)
  194. {
  195. return decoder->stream_pos;
  196. }