Coverage Report

Created: 2025-11-24 06:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/nghttp2/lib/nghttp2_hd_huffman.c
Line
Count
Source
1
/*
2
 * nghttp2 - HTTP/2 C Library
3
 *
4
 * Copyright (c) 2013 Tatsuhiro Tsujikawa
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining
7
 * a copy of this software and associated documentation files (the
8
 * "Software"), to deal in the Software without restriction, including
9
 * without limitation the rights to use, copy, modify, merge, publish,
10
 * distribute, sublicense, and/or sell copies of the Software, and to
11
 * permit persons to whom the Software is furnished to do so, subject to
12
 * the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be
15
 * included in all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
 */
25
#include "nghttp2_hd_huffman.h"
26
27
#include <string.h>
28
#include <assert.h>
29
#include <stdio.h>
30
31
#include "nghttp2_hd.h"
32
#include "nghttp2_net.h"
33
34
16.4k
size_t nghttp2_hd_huff_encode_count(const uint8_t *src, size_t len) {
35
16.4k
  size_t i;
36
16.4k
  size_t nbits = 0;
37
38
255k
  for (i = 0; i < len; ++i) {
39
239k
    nbits += huff_sym_table[src[i]].nbits;
40
239k
  }
41
  /* pad the prefix of EOS (256) */
42
16.4k
  return (nbits + 7) / 8;
43
16.4k
}
44
45
int nghttp2_hd_huff_encode(nghttp2_bufs *bufs, const uint8_t *src,
46
1.21k
                           size_t srclen) {
47
1.21k
  const nghttp2_huff_sym *sym;
48
1.21k
  const uint8_t *end = src + srclen;
49
1.21k
  uint64_t code = 0;
50
1.21k
  uint32_t x;
51
1.21k
  size_t nbits = 0;
52
1.21k
  size_t avail;
53
1.21k
  int rv;
54
55
1.21k
  avail = nghttp2_bufs_cur_avail(bufs);
56
57
36.6k
  for (; src != end;) {
58
35.4k
    sym = &huff_sym_table[*src++];
59
35.4k
    code |= (uint64_t)sym->code << (32 - nbits);
60
35.4k
    nbits += sym->nbits;
61
35.4k
    if (nbits < 32) {
62
29.4k
      continue;
63
29.4k
    }
64
5.97k
    if (avail >= 4) {
65
5.97k
      x = htonl((uint32_t)(code >> 32));
66
5.97k
      memcpy(bufs->cur->buf.last, &x, 4);
67
5.97k
      bufs->cur->buf.last += 4;
68
5.97k
      avail -= 4;
69
5.97k
      code <<= 32;
70
5.97k
      nbits -= 32;
71
5.97k
      continue;
72
5.97k
    }
73
74
0
    for (; nbits >= 8;) {
75
0
      rv = nghttp2_bufs_addb(bufs, (uint8_t)(code >> 56));
76
0
      if (rv != 0) {
77
0
        return rv;
78
0
      }
79
0
      code <<= 8;
80
0
      nbits -= 8;
81
0
    }
82
83
0
    avail = nghttp2_bufs_cur_avail(bufs);
84
0
  }
85
86
3.15k
  for (; nbits >= 8;) {
87
1.93k
    rv = nghttp2_bufs_addb(bufs, (uint8_t)(code >> 56));
88
1.93k
    if (rv != 0) {
89
0
      return rv;
90
0
    }
91
1.93k
    code <<= 8;
92
1.93k
    nbits -= 8;
93
1.93k
  }
94
95
1.21k
  if (nbits) {
96
898
    rv = nghttp2_bufs_addb(
97
898
      bufs, (uint8_t)((uint8_t)(code >> 56) | ((1 << (8 - nbits)) - 1)));
98
898
    if (rv != 0) {
99
0
      return rv;
100
0
    }
101
898
  }
102
103
1.21k
  return 0;
104
1.21k
}
105
106
38.4k
void nghttp2_hd_huff_decode_context_init(nghttp2_hd_huff_decode_context *ctx) {
107
38.4k
  ctx->fstate = 0;
108
38.4k
  ctx->flags = NGHTTP2_HUFF_ACCEPTED;
109
38.4k
}
110
111
nghttp2_ssize nghttp2_hd_huff_decode(nghttp2_hd_huff_decode_context *ctx,
112
                                     nghttp2_buf *buf, const uint8_t *src,
113
38.4k
                                     size_t srclen, int final) {
114
38.4k
  const uint8_t *end = src + srclen;
115
38.4k
  nghttp2_huff_decode t = {ctx->fstate, ctx->flags, 0};
116
38.4k
  uint8_t c;
117
118
  /* We use the decoding algorithm described in
119
      - http://graphics.ics.uci.edu/pub/Prefix.pdf [!!! NO LONGER VALID !!!]
120
      - https://ics.uci.edu/~dan/pubs/Prefix.pdf
121
      - https://github.com/nghttp2/nghttp2/files/15141264/Prefix.pdf */
122
1.17M
  for (; src != end;) {
123
1.13M
    c = *src++;
124
1.13M
    t = huff_decode_table[t.fstate][c >> 4];
125
1.13M
    if (t.flags & NGHTTP2_HUFF_SYM) {
126
708k
      *buf->last++ = t.sym;
127
708k
    }
128
129
1.13M
    t = huff_decode_table[t.fstate][c & 0xf];
130
1.13M
    if (t.flags & NGHTTP2_HUFF_SYM) {
131
803k
      *buf->last++ = t.sym;
132
803k
    }
133
1.13M
  }
134
135
38.4k
  ctx->fstate = t.fstate;
136
38.4k
  ctx->flags = t.flags;
137
138
38.4k
  if (final && !(ctx->flags & NGHTTP2_HUFF_ACCEPTED)) {
139
36
    return NGHTTP2_ERR_HEADER_COMP;
140
36
  }
141
142
38.3k
  return (nghttp2_ssize)srclen;
143
38.4k
}
144
145
38.3k
int nghttp2_hd_huff_decode_failure_state(nghttp2_hd_huff_decode_context *ctx) {
146
38.3k
  return ctx->fstate == 0x100;
147
38.3k
}