1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file stream_flags_encoder.c
4 /// \brief Encodes Stream Header and Stream Footer for .xz files
6 // Author: Lasse Collin
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
13 #include "stream_flags_common.h"
17 stream_flags_encode(const lzma_stream_flags
*options
, uint8_t *out
)
19 if ((unsigned int)(options
->check
) > LZMA_CHECK_ID_MAX
)
23 out
[1] = options
->check
;
29 extern LZMA_API(lzma_ret
)
30 lzma_stream_header_encode(const lzma_stream_flags
*options
, uint8_t *out
)
32 assert(sizeof(lzma_header_magic
) + LZMA_STREAM_FLAGS_SIZE
33 + 4 == LZMA_STREAM_HEADER_SIZE
);
35 if (options
->version
!= 0)
36 return LZMA_OPTIONS_ERROR
;
39 memcpy(out
, lzma_header_magic
, sizeof(lzma_header_magic
));
42 if (stream_flags_encode(options
, out
+ sizeof(lzma_header_magic
)))
43 return LZMA_PROG_ERROR
;
45 // CRC32 of the Stream Header
46 const uint32_t crc
= lzma_crc32(out
+ sizeof(lzma_header_magic
),
47 LZMA_STREAM_FLAGS_SIZE
, 0);
49 write32le(out
+ sizeof(lzma_header_magic
) + LZMA_STREAM_FLAGS_SIZE
,
56 extern LZMA_API(lzma_ret
)
57 lzma_stream_footer_encode(const lzma_stream_flags
*options
, uint8_t *out
)
59 assert(2 * 4 + LZMA_STREAM_FLAGS_SIZE
+ sizeof(lzma_footer_magic
)
60 == LZMA_STREAM_HEADER_SIZE
);
62 if (options
->version
!= 0)
63 return LZMA_OPTIONS_ERROR
;
66 if (!is_backward_size_valid(options
))
67 return LZMA_PROG_ERROR
;
69 write32le(out
+ 4, options
->backward_size
/ 4 - 1);
72 if (stream_flags_encode(options
, out
+ 2 * 4))
73 return LZMA_PROG_ERROR
;
76 const uint32_t crc
= lzma_crc32(
77 out
+ 4, 4 + LZMA_STREAM_FLAGS_SIZE
, 0);
82 memcpy(out
+ 2 * 4 + LZMA_STREAM_FLAGS_SIZE
,
83 lzma_footer_magic
, sizeof(lzma_footer_magic
));