lzs_decoder.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 <inttypes.h>
  19. #include "lha_decoder.h"
  20. #include "bit_stream_reader.c"
  21. // Parameters for ring buffer, used for storing history. This acts
  22. // as the dictionary for copy operations.
  23. #define RING_BUFFER_SIZE 2048
  24. #define START_OFFSET 17
  25. // Threshold offset. In the copy operation, the copy length is a 4-bit
  26. // value, giving a range 0..15. The threshold offsets this so that it
  27. // is interpreted as 2..17 - a more useful range.
  28. #define THRESHOLD 2
  29. // Size of output buffer. Must be large enough to hold the results of
  30. // the maximum copy operation.
  31. #define OUTPUT_BUFFER_SIZE (15 + THRESHOLD)
  32. // Decoder for the -lzs- compression method used by old versions of LArc.
  33. //
  34. // The input stream consists of commands, each of which is either "output
  35. // a literal byte value" or "copy block". A bit at the start of each
  36. // command signals which command it is.
  37. typedef struct {
  38. BitStreamReader bit_stream_reader;
  39. uint8_t ringbuf[RING_BUFFER_SIZE];
  40. unsigned int ringbuf_pos;
  41. } LHALZSDecoder;
  42. static int lha_lzs_init(void *data, LHADecoderCallback callback,
  43. void *callback_data)
  44. {
  45. LHALZSDecoder *decoder = data;
  46. memset(decoder->ringbuf, ' ', RING_BUFFER_SIZE);
  47. decoder->ringbuf_pos = RING_BUFFER_SIZE - START_OFFSET;
  48. bit_stream_reader_init(&decoder->bit_stream_reader, callback,
  49. callback_data);
  50. return 1;
  51. }
  52. // Add a single byte to the output buffer.
  53. static void output_byte(LHALZSDecoder *decoder, uint8_t *buf,
  54. size_t *buf_len, uint8_t b)
  55. {
  56. buf[*buf_len] = b;
  57. ++*buf_len;
  58. decoder->ringbuf[decoder->ringbuf_pos] = b;
  59. decoder->ringbuf_pos = (decoder->ringbuf_pos + 1) % RING_BUFFER_SIZE;
  60. }
  61. // Output a "block" of data from the specified range in the ring buffer.
  62. static void output_block(LHALZSDecoder *decoder,
  63. uint8_t *buf,
  64. size_t *buf_len,
  65. unsigned int start,
  66. unsigned int len)
  67. {
  68. unsigned int i;
  69. for (i = 0; i < len; ++i) {
  70. output_byte(decoder, buf, buf_len,
  71. decoder->ringbuf[(start + i) % RING_BUFFER_SIZE]);
  72. }
  73. }
  74. // Process a single command from the LZS input stream.
  75. static size_t lha_lzs_read(void *data, uint8_t *buf)
  76. {
  77. LHALZSDecoder *decoder = data;
  78. int bit;
  79. size_t result;
  80. // Start from an empty buffer.
  81. result = 0;
  82. // Each command starts with a bit that signals the type:
  83. bit = read_bit(&decoder->bit_stream_reader);
  84. if (bit < 0) {
  85. return 0;
  86. }
  87. // What type of command is this?
  88. if (bit) {
  89. int b;
  90. b = read_bits(&decoder->bit_stream_reader, 8);
  91. if (b < 0) {
  92. return 0;
  93. }
  94. output_byte(decoder, buf, &result, (uint8_t) b);
  95. } else {
  96. int pos, len;
  97. pos = read_bits(&decoder->bit_stream_reader, 11);
  98. len = read_bits(&decoder->bit_stream_reader, 4);
  99. if (pos < 0 || len < 0) {
  100. return 0;
  101. }
  102. output_block(decoder, buf, &result, (unsigned int) pos,
  103. (unsigned int) len + THRESHOLD);
  104. }
  105. return result;
  106. }
  107. LHADecoderType lha_lzs_decoder = {
  108. lha_lzs_init,
  109. NULL,
  110. lha_lzs_read,
  111. sizeof(LHALZSDecoder),
  112. OUTPUT_BUFFER_SIZE,
  113. RING_BUFFER_SIZE
  114. };