stream_flags_encoder.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file stream_flags_encoder.c
  4. /// \brief Encodes Stream Header and Stream Footer for .xz files
  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 "stream_flags_common.h"
  13. static bool
  14. stream_flags_encode(const lzma_stream_flags *options, uint8_t *out)
  15. {
  16. if ((unsigned int)(options->check) > LZMA_CHECK_ID_MAX)
  17. return true;
  18. out[0] = 0x00;
  19. out[1] = options->check;
  20. return false;
  21. }
  22. extern LZMA_API(lzma_ret)
  23. lzma_stream_header_encode(const lzma_stream_flags *options, uint8_t *out)
  24. {
  25. assert(sizeof(lzma_header_magic) + LZMA_STREAM_FLAGS_SIZE
  26. + 4 == LZMA_STREAM_HEADER_SIZE);
  27. if (options->version != 0)
  28. return LZMA_OPTIONS_ERROR;
  29. // Magic
  30. memcpy(out, lzma_header_magic, sizeof(lzma_header_magic));
  31. // Stream Flags
  32. if (stream_flags_encode(options, out + sizeof(lzma_header_magic)))
  33. return LZMA_PROG_ERROR;
  34. // CRC32 of the Stream Header
  35. const uint32_t crc = lzma_crc32(out + sizeof(lzma_header_magic),
  36. LZMA_STREAM_FLAGS_SIZE, 0);
  37. unaligned_write32le(out + sizeof(lzma_header_magic)
  38. + LZMA_STREAM_FLAGS_SIZE, crc);
  39. return LZMA_OK;
  40. }
  41. extern LZMA_API(lzma_ret)
  42. lzma_stream_footer_encode(const lzma_stream_flags *options, uint8_t *out)
  43. {
  44. assert(2 * 4 + LZMA_STREAM_FLAGS_SIZE + sizeof(lzma_footer_magic)
  45. == LZMA_STREAM_HEADER_SIZE);
  46. if (options->version != 0)
  47. return LZMA_OPTIONS_ERROR;
  48. // Backward Size
  49. if (!is_backward_size_valid(options))
  50. return LZMA_PROG_ERROR;
  51. unaligned_write32le(out + 4, options->backward_size / 4 - 1);
  52. // Stream Flags
  53. if (stream_flags_encode(options, out + 2 * 4))
  54. return LZMA_PROG_ERROR;
  55. // CRC32
  56. const uint32_t crc = lzma_crc32(
  57. out + 4, 4 + LZMA_STREAM_FLAGS_SIZE, 0);
  58. unaligned_write32le(out, crc);
  59. // Magic
  60. memcpy(out + 2 * 4 + LZMA_STREAM_FLAGS_SIZE,
  61. lzma_footer_magic, sizeof(lzma_footer_magic));
  62. return LZMA_OK;
  63. }