Coverage Report

Created: 2026-10-28 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/libdeflate-src/lib/gzip_decompress.c
Line
Count
Source
1
/*
2
 * gzip_decompress.c - decompress with a gzip wrapper
3
 *
4
 * Copyright 2016 Eric Biggers
5
 *
6
 * Permission is hereby granted, free of charge, to any person
7
 * obtaining a copy of this software and associated documentation
8
 * files (the "Software"), to deal in the Software without
9
 * restriction, including without limitation the rights to use,
10
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the
12
 * Software is furnished to do so, subject to the following
13
 * conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be
16
 * included in all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25
 * OTHER DEALINGS IN THE SOFTWARE.
26
 */
27
28
#include "lib_common.h"
29
#include "gzip_constants.h"
30
31
LIBDEFLATEAPI enum libdeflate_result
32
libdeflate_gzip_decompress_ex(struct libdeflate_decompressor *d,
33
            const void *in, size_t in_nbytes,
34
            void *out, size_t out_nbytes_avail,
35
            size_t *actual_in_nbytes_ret,
36
            size_t *actual_out_nbytes_ret)
37
1.90k
{
38
1.90k
  const u8 *in_next = in;
39
1.90k
  const u8 * const in_end = in_next + in_nbytes;
40
1.90k
  u8 flg;
41
1.90k
  size_t actual_in_nbytes;
42
1.90k
  size_t actual_out_nbytes;
43
1.90k
  enum libdeflate_result result;
44
45
1.90k
  if (in_nbytes < GZIP_MIN_OVERHEAD)
46
0
    return LIBDEFLATE_BAD_DATA;
47
48
  /* ID1 */
49
1.90k
  if (*in_next++ != GZIP_ID1)
50
0
    return LIBDEFLATE_BAD_DATA;
51
  /* ID2 */
52
1.90k
  if (*in_next++ != GZIP_ID2)
53
0
    return LIBDEFLATE_BAD_DATA;
54
  /* CM */
55
1.90k
  if (*in_next++ != GZIP_CM_DEFLATE)
56
25
    return LIBDEFLATE_BAD_DATA;
57
1.88k
  flg = *in_next++;
58
  /* MTIME */
59
1.88k
  in_next += 4;
60
  /* XFL */
61
1.88k
  in_next += 1;
62
  /* OS */
63
1.88k
  in_next += 1;
64
65
1.88k
  if (flg & GZIP_FRESERVED)
66
9
    return LIBDEFLATE_BAD_DATA;
67
68
  /* Extra field */
69
1.87k
  if (flg & GZIP_FEXTRA) {
70
148
    u16 xlen = get_unaligned_le16(in_next);
71
148
    in_next += 2;
72
73
148
    if (in_end - in_next < (u32)xlen + GZIP_FOOTER_SIZE)
74
5
      return LIBDEFLATE_BAD_DATA;
75
76
143
    in_next += xlen;
77
143
  }
78
79
  /* Original file name (zero terminated) */
80
1.86k
  if (flg & GZIP_FNAME) {
81
5.60k
    while (*in_next++ != 0 && in_next != in_end)
82
5.31k
      ;
83
290
    if (in_end - in_next < GZIP_FOOTER_SIZE)
84
10
      return LIBDEFLATE_BAD_DATA;
85
290
  }
86
87
  /* File comment (zero terminated) */
88
1.85k
  if (flg & GZIP_FCOMMENT) {
89
4.04k
    while (*in_next++ != 0 && in_next != in_end)
90
3.69k
      ;
91
349
    if (in_end - in_next < GZIP_FOOTER_SIZE)
92
12
      return LIBDEFLATE_BAD_DATA;
93
349
  }
94
95
  /* CRC16 for gzip header */
96
1.84k
  if (flg & GZIP_FHCRC) {
97
533
    in_next += 2;
98
533
    if (in_end - in_next < GZIP_FOOTER_SIZE)
99
5
      return LIBDEFLATE_BAD_DATA;
100
533
  }
101
102
  /* Compressed data  */
103
1.84k
  result = libdeflate_deflate_decompress_ex(d, in_next,
104
1.84k
          in_end - GZIP_FOOTER_SIZE - in_next,
105
1.84k
          out, out_nbytes_avail,
106
1.84k
          &actual_in_nbytes,
107
1.84k
          actual_out_nbytes_ret);
108
1.84k
  if (result != LIBDEFLATE_SUCCESS)
109
1.64k
    return result;
110
111
198
  if (actual_out_nbytes_ret)
112
198
    actual_out_nbytes = *actual_out_nbytes_ret;
113
0
  else
114
0
    actual_out_nbytes = out_nbytes_avail;
115
116
198
  in_next += actual_in_nbytes;
117
118
  /* CRC32 */
119
198
  if (libdeflate_crc32(0, out, actual_out_nbytes) !=
120
198
      get_unaligned_le32(in_next))
121
138
    return LIBDEFLATE_BAD_DATA;
122
60
  in_next += 4;
123
124
  /* ISIZE */
125
60
  if ((u32)actual_out_nbytes != get_unaligned_le32(in_next))
126
49
    return LIBDEFLATE_BAD_DATA;
127
11
  in_next += 4;
128
129
11
  if (actual_in_nbytes_ret)
130
0
    *actual_in_nbytes_ret = in_next - (u8 *)in;
131
132
11
  return LIBDEFLATE_SUCCESS;
133
60
}
134
135
LIBDEFLATEAPI enum libdeflate_result
136
libdeflate_gzip_decompress(struct libdeflate_decompressor *d,
137
         const void *in, size_t in_nbytes,
138
         void *out, size_t out_nbytes_avail,
139
         size_t *actual_out_nbytes_ret)
140
1.90k
{
141
1.90k
  return libdeflate_gzip_decompress_ex(d, in, in_nbytes,
142
1.90k
               out, out_nbytes_avail,
143
               NULL, actual_out_nbytes_ret);
144
1.90k
}