index_encoder.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file index_encoder.c
  4. /// \brief Encodes the Index field
  5. //
  6. // Author: Lasse Collin
  7. //
  8. // This file has been put into the public domain.
  9. // You can do whatever you want with this file.
  10. //
  11. ///////////////////////////////////////////////////////////////////////////////
  12. #include "index_encoder.h"
  13. #include "index.h"
  14. #include "check.h"
  15. typedef struct {
  16. enum {
  17. SEQ_INDICATOR,
  18. SEQ_COUNT,
  19. SEQ_UNPADDED,
  20. SEQ_UNCOMPRESSED,
  21. SEQ_NEXT,
  22. SEQ_PADDING,
  23. SEQ_CRC32,
  24. } sequence;
  25. /// Index being encoded
  26. const lzma_index *index;
  27. /// Iterator for the Index being encoded
  28. lzma_index_iter iter;
  29. /// Position in integers
  30. size_t pos;
  31. /// CRC32 of the List of Records field
  32. uint32_t crc32;
  33. } lzma_index_coder;
  34. static lzma_ret
  35. index_encode(void *coder_ptr,
  36. const lzma_allocator *allocator lzma_attribute((__unused__)),
  37. const uint8_t *restrict in lzma_attribute((__unused__)),
  38. size_t *restrict in_pos lzma_attribute((__unused__)),
  39. size_t in_size lzma_attribute((__unused__)),
  40. uint8_t *restrict out, size_t *restrict out_pos,
  41. size_t out_size,
  42. lzma_action action lzma_attribute((__unused__)))
  43. {
  44. lzma_index_coder *coder = coder_ptr;
  45. // Position where to start calculating CRC32. The idea is that we
  46. // need to call lzma_crc32() only once per call to index_encode().
  47. const size_t out_start = *out_pos;
  48. // Return value to use if we return at the end of this function.
  49. // We use "goto out" to jump out of the while-switch construct
  50. // instead of returning directly, because that way we don't need
  51. // to copypaste the lzma_crc32() call to many places.
  52. lzma_ret ret = LZMA_OK;
  53. while (*out_pos < out_size)
  54. switch (coder->sequence) {
  55. case SEQ_INDICATOR:
  56. out[*out_pos] = 0x00;
  57. ++*out_pos;
  58. coder->sequence = SEQ_COUNT;
  59. break;
  60. case SEQ_COUNT: {
  61. const lzma_vli count = lzma_index_block_count(coder->index);
  62. ret = lzma_vli_encode(count, &coder->pos,
  63. out, out_pos, out_size);
  64. if (ret != LZMA_STREAM_END)
  65. goto out;
  66. ret = LZMA_OK;
  67. coder->pos = 0;
  68. coder->sequence = SEQ_NEXT;
  69. break;
  70. }
  71. case SEQ_NEXT:
  72. if (lzma_index_iter_next(
  73. &coder->iter, LZMA_INDEX_ITER_BLOCK)) {
  74. // Get the size of the Index Padding field.
  75. coder->pos = lzma_index_padding_size(coder->index);
  76. assert(coder->pos <= 3);
  77. coder->sequence = SEQ_PADDING;
  78. break;
  79. }
  80. coder->sequence = SEQ_UNPADDED;
  81. // Fall through
  82. case SEQ_UNPADDED:
  83. case SEQ_UNCOMPRESSED: {
  84. const lzma_vli size = coder->sequence == SEQ_UNPADDED
  85. ? coder->iter.block.unpadded_size
  86. : coder->iter.block.uncompressed_size;
  87. ret = lzma_vli_encode(size, &coder->pos,
  88. out, out_pos, out_size);
  89. if (ret != LZMA_STREAM_END)
  90. goto out;
  91. ret = LZMA_OK;
  92. coder->pos = 0;
  93. // Advance to SEQ_UNCOMPRESSED or SEQ_NEXT.
  94. ++coder->sequence;
  95. break;
  96. }
  97. case SEQ_PADDING:
  98. if (coder->pos > 0) {
  99. --coder->pos;
  100. out[(*out_pos)++] = 0x00;
  101. break;
  102. }
  103. // Finish the CRC32 calculation.
  104. coder->crc32 = lzma_crc32(out + out_start,
  105. *out_pos - out_start, coder->crc32);
  106. coder->sequence = SEQ_CRC32;
  107. // Fall through
  108. case SEQ_CRC32:
  109. // We don't use the main loop, because we don't want
  110. // coder->crc32 to be touched anymore.
  111. do {
  112. if (*out_pos == out_size)
  113. return LZMA_OK;
  114. out[*out_pos] = (coder->crc32 >> (coder->pos * 8))
  115. & 0xFF;
  116. ++*out_pos;
  117. } while (++coder->pos < 4);
  118. return LZMA_STREAM_END;
  119. default:
  120. assert(0);
  121. return LZMA_PROG_ERROR;
  122. }
  123. out:
  124. // Update the CRC32.
  125. coder->crc32 = lzma_crc32(out + out_start,
  126. *out_pos - out_start, coder->crc32);
  127. return ret;
  128. }
  129. static void
  130. index_encoder_end(void *coder, const lzma_allocator *allocator)
  131. {
  132. lzma_free(coder, allocator);
  133. return;
  134. }
  135. static void
  136. index_encoder_reset(lzma_index_coder *coder, const lzma_index *i)
  137. {
  138. lzma_index_iter_init(&coder->iter, i);
  139. coder->sequence = SEQ_INDICATOR;
  140. coder->index = i;
  141. coder->pos = 0;
  142. coder->crc32 = 0;
  143. return;
  144. }
  145. extern lzma_ret
  146. lzma_index_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
  147. const lzma_index *i)
  148. {
  149. lzma_next_coder_init(&lzma_index_encoder_init, next, allocator);
  150. if (i == NULL)
  151. return LZMA_PROG_ERROR;
  152. if (next->coder == NULL) {
  153. next->coder = lzma_alloc(sizeof(lzma_index_coder), allocator);
  154. if (next->coder == NULL)
  155. return LZMA_MEM_ERROR;
  156. next->code = &index_encode;
  157. next->end = &index_encoder_end;
  158. }
  159. index_encoder_reset(next->coder, i);
  160. return LZMA_OK;
  161. }
  162. extern LZMA_API(lzma_ret)
  163. lzma_index_encoder(lzma_stream *strm, const lzma_index *i)
  164. {
  165. lzma_next_strm_init(lzma_index_encoder_init, strm, i);
  166. strm->internal->supported_actions[LZMA_RUN] = true;
  167. strm->internal->supported_actions[LZMA_FINISH] = true;
  168. return LZMA_OK;
  169. }
  170. extern LZMA_API(lzma_ret)
  171. lzma_index_buffer_encode(const lzma_index *i,
  172. uint8_t *out, size_t *out_pos, size_t out_size)
  173. {
  174. // Validate the arguments.
  175. if (i == NULL || out == NULL || out_pos == NULL || *out_pos > out_size)
  176. return LZMA_PROG_ERROR;
  177. // Don't try to encode if there's not enough output space.
  178. if (out_size - *out_pos < lzma_index_size(i))
  179. return LZMA_BUF_ERROR;
  180. // The Index encoder needs just one small data structure so we can
  181. // allocate it on stack.
  182. lzma_index_coder coder;
  183. index_encoder_reset(&coder, i);
  184. // Do the actual encoding. This should never fail, but store
  185. // the original *out_pos just in case.
  186. const size_t out_start = *out_pos;
  187. lzma_ret ret = index_encode(&coder, NULL, NULL, NULL, 0,
  188. out, out_pos, out_size, LZMA_RUN);
  189. if (ret == LZMA_STREAM_END) {
  190. ret = LZMA_OK;
  191. } else {
  192. // We should never get here, but just in case, restore the
  193. // output position and set the error accordingly if something
  194. // goes wrong and debugging isn't enabled.
  195. assert(0);
  196. *out_pos = out_start;
  197. ret = LZMA_PROG_ERROR;
  198. }
  199. return ret;
  200. }