Coverage Report

Created: 2026-01-10 06:18

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
4.08k
{
19
4.08k
  if (block->version > 1)
20
0
    return LZMA_OPTIONS_ERROR;
21
22
  // Block Header Size + Block Flags + CRC32.
23
4.08k
  uint32_t size = 1 + 1 + 4;
24
25
  // Compressed Size
26
4.08k
  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
4.08k
  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
4.08k
  if (block->filters == NULL || block->filters[0].id == LZMA_VLI_UNKNOWN)
45
0
    return LZMA_PROG_ERROR;
46
47
8.17k
  for (size_t i = 0; block->filters[i].id != LZMA_VLI_UNKNOWN; ++i) {
48
    // Don't allow too many filters.
49
4.08k
    if (i == LZMA_FILTERS_MAX)
50
0
      return LZMA_PROG_ERROR;
51
52
4.08k
    uint32_t add;
53
4.08k
    return_if_error(lzma_filter_flags_size(&add,
54
4.08k
        block->filters + i));
55
56
4.08k
    size += add;
57
4.08k
  }
58
59
  // Pad to a multiple of four bytes.
60
4.08k
  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
4.08k
  return LZMA_OK;
69
4.08k
}
70
71
72
extern LZMA_API(lzma_ret)
73
lzma_block_header_encode(const lzma_block *block, uint8_t *out)
74
4.08k
{
75
  // Validate everything but filters.
76
4.08k
  if (lzma_block_unpadded_size(block) == 0
77
4.08k
      || !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
4.08k
  const size_t out_size = block->header_size - 4;
82
83
  // Store the Block Header Size.
84
4.08k
  out[0] = out_size / 4;
85
86
  // We write Block Flags in pieces.
87
4.08k
  out[1] = 0x00;
88
4.08k
  size_t out_pos = 2;
89
90
  // Compressed Size
91
4.08k
  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
4.08k
  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
4.08k
  if (block->filters == NULL || block->filters[0].id == LZMA_VLI_UNKNOWN)
108
0
    return LZMA_PROG_ERROR;
109
110
4.08k
  size_t filter_count = 0;
111
4.08k
  do {
112
    // There can be a maximum of four filters.
113
4.08k
    if (filter_count == LZMA_FILTERS_MAX)
114
0
      return LZMA_PROG_ERROR;
115
116
4.08k
    return_if_error(lzma_filter_flags_encode(
117
4.08k
        block->filters + filter_count,
118
4.08k
        out, &out_pos, out_size));
119
120
4.08k
  } while (block->filters[++filter_count].id != LZMA_VLI_UNKNOWN);
121
122
4.08k
  out[1] |= filter_count - 1;
123
124
  // Padding
125
4.08k
  memzero(out + out_pos, out_size - out_pos);
126
127
  // CRC32
128
4.08k
  write32le(out + out_size, lzma_crc32(out, out_size, 0));
129
130
4.08k
  return LZMA_OK;
131
4.08k
}