]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/stream_flags_encoder.c
Imported to git.
[icculus/xz.git] / src / liblzma / common / stream_flags_encoder.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       stream_flags_encoder.c
4 /// \brief      Encodes Stream Header and Footer for .lzma files
5 //
6 //  Copyright (C) 2007 Lasse Collin
7 //
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.
12 //
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.
17 //
18 ///////////////////////////////////////////////////////////////////////////////
19
20 #include "stream_common.h"
21
22
23 static bool
24 stream_flags_encode(uint8_t *flags_byte, const lzma_stream_flags *options)
25 {
26         // Check type
27         if ((unsigned int)(options->check) > LZMA_CHECK_ID_MAX)
28                 return true;
29
30         *flags_byte = options->check;
31
32         // Usage of CRC32 in Block Headers
33         if (options->has_crc32)
34                 *flags_byte |= 0x08;
35
36         // Single- or Multi-Block
37         if (options->is_multi)
38                 *flags_byte |= 0x10;
39
40         return false;
41 }
42
43
44 extern LZMA_API lzma_ret
45 lzma_stream_header_encode(uint8_t *out, const lzma_stream_flags *options)
46 {
47         // Magic
48         memcpy(out, lzma_header_magic, sizeof(lzma_header_magic));
49
50         // Stream Flags
51         if (stream_flags_encode(out + sizeof(lzma_header_magic), options))
52                 return LZMA_PROG_ERROR;;
53
54         // CRC32 of the Stream Header
55         const uint32_t crc = lzma_crc32(out + sizeof(lzma_header_magic), 1, 0);
56
57         for (size_t i = 0; i < 4; ++i)
58                 out[sizeof(lzma_header_magic) + 1 + i] = crc >> (i * 8);
59
60         return LZMA_OK;
61 }
62
63
64 extern LZMA_API lzma_ret
65 lzma_stream_tail_encode(uint8_t *out, const lzma_stream_flags *options)
66 {
67         // Stream Flags
68         if (stream_flags_encode(out, options))
69                 return LZMA_PROG_ERROR;
70
71         // Magic
72         memcpy(out + 1, lzma_footer_magic, sizeof(lzma_footer_magic));
73
74         return LZMA_OK;
75 }