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