Tests: Fix memory leaks in test_block_header.
[xz/debian.git] / src / liblzma / common / stream_flags_encoder.c
blobb98ab17c456c3710df71085165bbc6ee74108c9b
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.
11 ///////////////////////////////////////////////////////////////////////////////
13 #include "stream_flags_common.h"
16 static bool
17 stream_flags_encode(const lzma_stream_flags *options, uint8_t *out)
19 if ((unsigned int)(options->check) > LZMA_CHECK_ID_MAX)
20 return true;
22 out[0] = 0x00;
23 out[1] = options->check;
25 return false;
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;
38 // Magic
39 memcpy(out, lzma_header_magic, sizeof(lzma_header_magic));
41 // Stream Flags
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,
50 crc);
52 return LZMA_OK;
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;
65 // Backward Size
66 if (!is_backward_size_valid(options))
67 return LZMA_PROG_ERROR;
69 write32le(out + 4, options->backward_size / 4 - 1);
71 // Stream Flags
72 if (stream_flags_encode(options, out + 2 * 4))
73 return LZMA_PROG_ERROR;
75 // CRC32
76 const uint32_t crc = lzma_crc32(
77 out + 4, 4 + LZMA_STREAM_FLAGS_SIZE, 0);
79 write32le(out, crc);
81 // Magic
82 memcpy(out + 2 * 4 + LZMA_STREAM_FLAGS_SIZE,
83 lzma_footer_magic, sizeof(lzma_footer_magic));
85 return LZMA_OK;