lz5_decoder.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. // Parameters for ring buffer, used for storing history. This acts
  21. // as the dictionary for copy operations.
  22. #define RING_BUFFER_SIZE 4096
  23. #define START_OFFSET 18
  24. // Threshold offset. In the copy operation, the copy length is a 4-bit
  25. // value, giving a range 0..15. The threshold offsets this so that it
  26. // is interpreted as 3..18 - a more useful range.
  27. #define THRESHOLD 3
  28. // Size of output buffer. Must be large enough to hold the results of
  29. // a complete "run" (see below).
  30. #define OUTPUT_BUFFER_SIZE (15 + THRESHOLD) * 8
  31. // Decoder for the -lz5- compression method used by LArc.
  32. //
  33. // This processes "runs" of eight commands, each of which is either
  34. // "output a character" or "copy block". The result of that run
  35. // is written into the output buffer.
  36. typedef struct {
  37. uint8_t ringbuf[RING_BUFFER_SIZE];
  38. unsigned int ringbuf_pos;
  39. LHADecoderCallback callback;
  40. void *callback_data;
  41. } LHALZ5Decoder;
  42. static void fill_initial(LHALZ5Decoder *decoder)
  43. {
  44. unsigned int i, j;
  45. uint8_t *p;
  46. p = decoder->ringbuf;
  47. // For each byte value, the history buffer includes a run of 13
  48. // bytes all with that value. This is useful eg. for text files
  49. // that include a long run like this (eg. ===========).
  50. for (i = 0; i < 256; ++i) {
  51. for (j = 0; j < 13; ++j) {
  52. *p++ = (uint8_t) i;
  53. }
  54. }
  55. // Next we include all byte values ascending and descending.
  56. for (i = 0; i < 256; ++i) {
  57. *p++ = (uint8_t) i;
  58. }
  59. for (i = 0; i < 256; ++i) {
  60. *p++ = (uint8_t) (255 - i);
  61. }
  62. // Block of zeros, and then ASCII space characters. I think these are
  63. // towards the end of the range because they're most likely to be
  64. // useful and therefore last to get overwritten?
  65. for (i = 0; i < 128; ++i) {
  66. *p++ = 0;
  67. }
  68. for (i = 0; i < 110; ++i) {
  69. *p++ = ' ';
  70. }
  71. // Final 18 characters are all zeros, probably because of START_OFFSET.
  72. for (i = 0; i < 18; ++i) {
  73. *p++ = 0;
  74. }
  75. }
  76. static int lha_lz5_init(void *data, LHADecoderCallback callback,
  77. void *callback_data)
  78. {
  79. LHALZ5Decoder *decoder = data;
  80. fill_initial(decoder);
  81. decoder->ringbuf_pos = RING_BUFFER_SIZE - START_OFFSET;
  82. decoder->callback = callback;
  83. decoder->callback_data = callback_data;
  84. return 1;
  85. }
  86. // Add a single byte to the output buffer.
  87. static void output_byte(LHALZ5Decoder *decoder, uint8_t *buf,
  88. size_t *buf_len, uint8_t b)
  89. {
  90. buf[*buf_len] = b;
  91. ++*buf_len;
  92. decoder->ringbuf[decoder->ringbuf_pos] = b;
  93. decoder->ringbuf_pos = (decoder->ringbuf_pos + 1) % RING_BUFFER_SIZE;
  94. }
  95. // Output a "block" of data from the specified range in the ring buffer.
  96. static void output_block(LHALZ5Decoder *decoder,
  97. uint8_t *buf,
  98. size_t *buf_len,
  99. unsigned int start,
  100. unsigned int len)
  101. {
  102. unsigned int i;
  103. for (i = 0; i < len; ++i) {
  104. output_byte(decoder, buf, buf_len,
  105. decoder->ringbuf[(start + i) % RING_BUFFER_SIZE]);
  106. }
  107. }
  108. // Process a "run" of LZ5-compressed data (a control byte followed by
  109. // eight "commands").
  110. static size_t lha_lz5_read(void *data, uint8_t *buf)
  111. {
  112. LHALZ5Decoder *decoder = data;
  113. uint8_t bitmap;
  114. unsigned int bit;
  115. size_t result;
  116. // Start from an empty buffer.
  117. result = 0;
  118. // Read the bitmap byte first.
  119. if (!decoder->callback(&bitmap, 1, decoder->callback_data)) {
  120. return 0;
  121. }
  122. // Each bit in the bitmap is a command.
  123. // If the bit is set, it is an "output byte" command.
  124. // If it is not set, it is a "copy block" command.
  125. for (bit = 0; bit < 8; ++bit) {
  126. if ((bitmap & (1 << bit)) != 0) {
  127. uint8_t b;
  128. if (!decoder->callback(&b, 1, decoder->callback_data)) {
  129. break;
  130. }
  131. output_byte(decoder, buf, &result, b);
  132. } else {
  133. uint8_t cmd[2];
  134. unsigned int seqstart, seqlen;
  135. if (!decoder->callback(cmd, 2, decoder->callback_data)) {
  136. break;
  137. }
  138. seqstart = (((unsigned int) cmd[1] & 0xf0) << 4)
  139. | cmd[0];
  140. seqlen = ((unsigned int) cmd[1] & 0x0f) + THRESHOLD;
  141. output_block(decoder, buf, &result, seqstart, seqlen);
  142. }
  143. }
  144. return result;
  145. }
  146. LHADecoderType lha_lz5_decoder = {
  147. lha_lz5_init,
  148. NULL,
  149. lha_lz5_read,
  150. sizeof(LHALZ5Decoder),
  151. OUTPUT_BUFFER_SIZE,
  152. RING_BUFFER_SIZE
  153. };