Coverage Report

Created: 2026-01-20 07:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/xz/src/liblzma/common/block_header_encoder.c
Line
Count
Source
1
// SPDX-License-Identifier: 0BSD
2
3
///////////////////////////////////////////////////////////////////////////////
4
//
5
/// \file       block_header_encoder.c
6
/// \brief      Encodes Block Header for .xz files
7
//
8
//  Author:     Lasse Collin
9
//
10
///////////////////////////////////////////////////////////////////////////////
11
12
#include "common.h"
13
#include "check.h"
14
15
16
extern LZMA_API(lzma_ret)
17
lzma_block_header_size(lzma_block *block)
18
0
{
19
0
  if (block->version > 1)
20
0
    return LZMA_OPTIONS_ERROR;
21
22
  // Block Header Size + Block Flags + CRC32.
23
0
  uint32_t size = 1 + 1 + 4;
24
25
  // Compressed Size
26
0
  if (block->compressed_size != LZMA_VLI_UNKNOWN) {
27
0
    const uint32_t add = lzma_vli_size(block->compressed_size);
28
0
    if (add == 0 || block->compressed_size == 0)
29
0
      return LZMA_PROG_ERROR;
30
31
0
    size += add;
32
0
  }
33
34
  // Uncompressed Size
35
0
  if (block->uncompressed_size != LZMA_VLI_UNKNOWN) {
36
0
    const uint32_t add = lzma_vli_size(block->uncompressed_size);
37
0
    if (add == 0)
38
0
      return LZMA_PROG_ERROR;
39
40
0
    size += add;
41
0
  }
42
43
  // List of Filter Flags
44
0
  if (block->filters == NULL || block->filters[0].id == LZMA_VLI_UNKNOWN)
45
0
    return LZMA_PROG_ERROR;
46
47
0
  for (size_t i = 0; block->filters[i].id != LZMA_VLI_UNKNOWN; ++i) {
48
    // Don't allow too many filters.
49
0
    if (i == LZMA_FILTERS_MAX)
50
0
      return LZMA_PROG_ERROR;
51
52
0
    uint32_t add;
53
0
    return_if_error(lzma_filter_flags_size(&add,
54
0
        block->filters + i));
55
56
0
    size += add;
57
0
  }
58
59
  // Pad to a multiple of four bytes.
60
0
  block->header_size = (size + 3) & ~UINT32_C(3);
61
62
  // NOTE: We don't verify that the encoded size of the Block stays
63
  // within limits. This is because it is possible that we are called
64
  // with exaggerated Compressed Size (e.g. LZMA_VLI_MAX) to reserve
65
  // space for Block Header, and later called again with lower,
66
  // real values.
67
68
0
  return LZMA_OK;
69
0
}
70
71
72
extern LZMA_API(lzma_ret)
73
lzma_block_header_encode(const lzma_block *block, uint8_t *out)
74
0
{
75
  // Validate everything but filters.
76
0
  if (lzma_block_unpadded_size(block) == 0
77
0
      || !lzma_vli_is_valid(block->uncompressed_size))
78
0
    return LZMA_PROG_ERROR;
79
80
  // Indicate the size of the buffer _excluding_ the CRC32 field.
81
0
  const size_t out_size = block->header_size - 4;
82
83
  // Store the Block Header Size.
84
0
  out[0] = out_size / 4;
85
86
  // We write Block Flags in pieces.
87
0
  out[1] = 0x00;
88
0
  size_t out_pos = 2;
89
90
  // Compressed Size
91
0
  if (block->compressed_size != LZMA_VLI_UNKNOWN) {
92
0
    return_if_error(lzma_vli_encode(block->compressed_size, NULL,
93
0
        out, &out_pos, out_size));
94
95
0
    out[1] |= 0x40;
96
0
  }
97
98
  // Uncompressed Size
99
0
  if (block->uncompressed_size != LZMA_VLI_UNKNOWN) {
100
0
    return_if_error(lzma_vli_encode(block->uncompressed_size, NULL,
101
0
        out, &out_pos, out_size));
102
103
0
    out[1] |= 0x80;
104
0
  }
105
106
  // Filter Flags
107
0
  if (block->filters == NULL || block->filters[0].id == LZMA_VLI_UNKNOWN)
108
0
    return LZMA_PROG_ERROR;
109
110
0
  size_t filter_count = 0;
111
0
  do {
112
    // There can be a maximum of four filters.
113
0
    if (filter_count == LZMA_FILTERS_MAX)
114
0
      return LZMA_PROG_ERROR;
115
116
0
    return_if_error(lzma_filter_flags_encode(
117
0
        block->filters + filter_count,
118
0
        out, &out_pos, out_size));
119
120
0
  } while (block->filters[++filter_count].id != LZMA_VLI_UNKNOWN);
121
122
0
  out[1] |= filter_count - 1;
123
124
  // Padding
125
0
  memzero(out + out_pos, out_size - out_pos);
126
127
  // CRC32
128
0
  write32le(out + out_size, lzma_crc32(out, out_size, 0));
129
130
0
  return LZMA_OK;
131
0
}