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.
|
---|
10 | //
|
---|
11 | ///////////////////////////////////////////////////////////////////////////////
|
---|
12 |
|
---|
13 | #include "stream_flags_common.h"
|
---|
14 |
|
---|
15 |
|
---|
16 | static bool
|
---|
17 | stream_flags_encode(const lzma_stream_flags *options, uint8_t *out)
|
---|
18 | {
|
---|
19 | if ((unsigned int)(options->check) > LZMA_CHECK_ID_MAX)
|
---|
20 | return true;
|
---|
21 |
|
---|
22 | out[0] = 0x00;
|
---|
23 | out[1] = options->check;
|
---|
24 |
|
---|
25 | return false;
|
---|
26 | }
|
---|
27 |
|
---|
28 |
|
---|
29 | extern LZMA_API(lzma_ret)
|
---|
30 | lzma_stream_header_encode(const lzma_stream_flags *options, uint8_t *out)
|
---|
31 | {
|
---|
32 | assert(sizeof(lzma_header_magic) + LZMA_STREAM_FLAGS_SIZE
|
---|
33 | + 4 == LZMA_STREAM_HEADER_SIZE);
|
---|
34 |
|
---|
35 | if (options->version != 0)
|
---|
36 | return LZMA_OPTIONS_ERROR;
|
---|
37 |
|
---|
38 | // Magic
|
---|
39 | memcpy(out, lzma_header_magic, sizeof(lzma_header_magic));
|
---|
40 |
|
---|
41 | // Stream Flags
|
---|
42 | if (stream_flags_encode(options, out + sizeof(lzma_header_magic)))
|
---|
43 | return LZMA_PROG_ERROR;
|
---|
44 |
|
---|
45 | // CRC32 of the Stream Header
|
---|
46 | const uint32_t crc = lzma_crc32(out + sizeof(lzma_header_magic),
|
---|
47 | LZMA_STREAM_FLAGS_SIZE, 0);
|
---|
48 |
|
---|
49 | write32le(out + sizeof(lzma_header_magic) + LZMA_STREAM_FLAGS_SIZE,
|
---|
50 | crc);
|
---|
51 |
|
---|
52 | return LZMA_OK;
|
---|
53 | }
|
---|
54 |
|
---|
55 |
|
---|
56 | extern LZMA_API(lzma_ret)
|
---|
57 | lzma_stream_footer_encode(const lzma_stream_flags *options, uint8_t *out)
|
---|
58 | {
|
---|
59 | assert(2 * 4 + LZMA_STREAM_FLAGS_SIZE + sizeof(lzma_footer_magic)
|
---|
60 | == LZMA_STREAM_HEADER_SIZE);
|
---|
61 |
|
---|
62 | if (options->version != 0)
|
---|
63 | return LZMA_OPTIONS_ERROR;
|
---|
64 |
|
---|
65 | // Backward Size
|
---|
66 | if (!is_backward_size_valid(options))
|
---|
67 | return LZMA_PROG_ERROR;
|
---|
68 |
|
---|
69 | write32le(out + 4, options->backward_size / 4 - 1);
|
---|
70 |
|
---|
71 | // Stream Flags
|
---|
72 | if (stream_flags_encode(options, out + 2 * 4))
|
---|
73 | return LZMA_PROG_ERROR;
|
---|
74 |
|
---|
75 | // CRC32
|
---|
76 | const uint32_t crc = lzma_crc32(
|
---|
77 | out + 4, 4 + LZMA_STREAM_FLAGS_SIZE, 0);
|
---|
78 |
|
---|
79 | write32le(out, crc);
|
---|
80 |
|
---|
81 | // Magic
|
---|
82 | memcpy(out + 2 * 4 + LZMA_STREAM_FLAGS_SIZE,
|
---|
83 | lzma_footer_magic, sizeof(lzma_footer_magic));
|
---|
84 |
|
---|
85 | return LZMA_OK;
|
---|
86 | }
|
---|