Fix version.sh compatiblity with Solaris
[xz/debian.git] / src / liblzma / common / stream_flags_encoder.c
blobf94b5cd0a2373e37e84d6ab17f8df8dabf943e0d
1 // SPDX-License-Identifier: 0BSD
3 ///////////////////////////////////////////////////////////////////////////////
4 //
5 /// \file stream_flags_encoder.c
6 /// \brief Encodes Stream Header and Stream Footer for .xz files
7 //
8 // Author: Lasse Collin
9 //
10 ///////////////////////////////////////////////////////////////////////////////
12 #include "stream_flags_common.h"
15 static bool
16 stream_flags_encode(const lzma_stream_flags *options, uint8_t *out)
18 if ((unsigned int)(options->check) > LZMA_CHECK_ID_MAX)
19 return true;
21 out[0] = 0x00;
22 out[1] = options->check;
24 return false;
28 extern LZMA_API(lzma_ret)
29 lzma_stream_header_encode(const lzma_stream_flags *options, uint8_t *out)
31 assert(sizeof(lzma_header_magic) + LZMA_STREAM_FLAGS_SIZE
32 + 4 == LZMA_STREAM_HEADER_SIZE);
34 if (options->version != 0)
35 return LZMA_OPTIONS_ERROR;
37 // Magic
38 memcpy(out, lzma_header_magic, sizeof(lzma_header_magic));
40 // Stream Flags
41 if (stream_flags_encode(options, out + sizeof(lzma_header_magic)))
42 return LZMA_PROG_ERROR;
44 // CRC32 of the Stream Header
45 const uint32_t crc = lzma_crc32(out + sizeof(lzma_header_magic),
46 LZMA_STREAM_FLAGS_SIZE, 0);
48 write32le(out + sizeof(lzma_header_magic) + LZMA_STREAM_FLAGS_SIZE,
49 crc);
51 return LZMA_OK;
55 extern LZMA_API(lzma_ret)
56 lzma_stream_footer_encode(const lzma_stream_flags *options, uint8_t *out)
58 assert(2 * 4 + LZMA_STREAM_FLAGS_SIZE + sizeof(lzma_footer_magic)
59 == LZMA_STREAM_HEADER_SIZE);
61 if (options->version != 0)
62 return LZMA_OPTIONS_ERROR;
64 // Backward Size
65 if (!is_backward_size_valid(options))
66 return LZMA_PROG_ERROR;
68 write32le(out + 4, options->backward_size / 4 - 1);
70 // Stream Flags
71 if (stream_flags_encode(options, out + 2 * 4))
72 return LZMA_PROG_ERROR;
74 // CRC32
75 const uint32_t crc = lzma_crc32(
76 out + 4, 4 + LZMA_STREAM_FLAGS_SIZE, 0);
78 write32le(out, crc);
80 // Magic
81 memcpy(out + 2 * 4 + LZMA_STREAM_FLAGS_SIZE,
82 lzma_footer_magic, sizeof(lzma_footer_magic));
84 return LZMA_OK;