1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file stream_flags_encoder.c
4 /// \brief Encodes Stream Header and Stream Footer for .lzma files
6 // Copyright (C) 2007 Lasse Collin
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
18 ///////////////////////////////////////////////////////////////////////////////
20 #include "stream_common.h"
24 stream_flags_encode(const lzma_stream_flags *options, uint8_t *out)
26 if ((unsigned int)(options->check) > LZMA_CHECK_ID_MAX)
30 out[1] = options->check;
36 extern LZMA_API lzma_ret
37 lzma_stream_header_encode(const lzma_stream_flags *options, uint8_t *out)
39 assert(sizeof(lzma_header_magic) + LZMA_STREAM_FLAGS_SIZE
40 + 4 == LZMA_STREAM_HEADER_SIZE);
43 memcpy(out, lzma_header_magic, sizeof(lzma_header_magic));
46 if (stream_flags_encode(options, out + sizeof(lzma_header_magic)))
47 return LZMA_PROG_ERROR;
49 // CRC32 of the Stream Header
50 const uint32_t crc = lzma_crc32(out + sizeof(lzma_header_magic),
51 LZMA_STREAM_FLAGS_SIZE, 0);
53 integer_write_32(out + sizeof(lzma_header_magic)
54 + LZMA_STREAM_FLAGS_SIZE, crc);
60 extern LZMA_API lzma_ret
61 lzma_stream_footer_encode(const lzma_stream_flags *options, uint8_t *out)
63 assert(2 * 4 + LZMA_STREAM_FLAGS_SIZE + sizeof(lzma_footer_magic)
64 == LZMA_STREAM_HEADER_SIZE);
67 if (options->backward_size < LZMA_BACKWARD_SIZE_MIN
68 || options->backward_size > LZMA_BACKWARD_SIZE_MAX
69 || (options->backward_size & 3))
70 return LZMA_PROG_ERROR;
72 integer_write_32(out + 4, options->backward_size / 4 - 1);
75 if (stream_flags_encode(options, out + 2 * 4))
76 return LZMA_PROG_ERROR;
79 const uint32_t crc = lzma_crc32(
80 out + 4, 4 + LZMA_STREAM_FLAGS_SIZE, 0);
82 integer_write_32(out, crc);
85 memcpy(out + 2 * 4 + LZMA_STREAM_FLAGS_SIZE,
86 lzma_footer_magic, sizeof(lzma_footer_magic));