Coverage Report

Created: 2024-05-21 06:52

/src/nghttp2/lib/nghttp2_hd_huffman.c
Line
Count
Source (jump to first uncovered line)
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
197k
size_t nghttp2_hd_huff_encode_count(const uint8_t *src, size_t len) {
35
197k
  size_t i;
36
197k
  size_t nbits = 0;
37
38
5.83M
  for (i = 0; i < len; ++i) {
39
5.63M
    nbits += huff_sym_table[src[i]].nbits;
40
5.63M
  }
41
  /* pad the prefix of EOS (256) */
42
197k
  return (nbits + 7) / 8;
43
197k
}
44
45
int nghttp2_hd_huff_encode(nghttp2_bufs *bufs, const uint8_t *src,
46
51.9k
                           size_t srclen) {
47
51.9k
  const nghttp2_huff_sym *sym;
48
51.9k
  const uint8_t *end = src + srclen;
49
51.9k
  uint64_t code = 0;
50
51.9k
  uint32_t x;
51
51.9k
  size_t nbits = 0;
52
51.9k
  size_t avail;
53
51.9k
  int rv;
54
55
51.9k
  avail = nghttp2_bufs_cur_avail(bufs);
56
57
2.44M
  for (; src != end;) {
58
2.38M
    sym = &huff_sym_table[*src++];
59
2.38M
    code |= (uint64_t)sym->code << (32 - nbits);
60
2.38M
    nbits += sym->nbits;
61
2.38M
    if (nbits < 32) {
62
1.93M
      continue;
63
1.93M
    }
64
455k
    if (avail >= 4) {
65
455k
      x = htonl((uint32_t)(code >> 32));
66
455k
      memcpy(bufs->cur->buf.last, &x, 4);
67
455k
      bufs->cur->buf.last += 4;
68
455k
      avail -= 4;
69
455k
      code <<= 32;
70
455k
      nbits -= 32;
71
455k
      continue;
72
455k
    }
73
74
194
    for (; nbits >= 8;) {
75
157
      rv = nghttp2_bufs_addb(bufs, (uint8_t)(code >> 56));
76
157
      if (rv != 0) {
77
0
        return rv;
78
0
      }
79
157
      code <<= 8;
80
157
      nbits -= 8;
81
157
    }
82
83
37
    avail = nghttp2_bufs_cur_avail(bufs);
84
37
  }
85
86
131k
  for (; nbits >= 8;) {
87
79.8k
    rv = nghttp2_bufs_addb(bufs, (uint8_t)(code >> 56));
88
79.8k
    if (rv != 0) {
89
0
      return rv;
90
0
    }
91
79.8k
    code <<= 8;
92
79.8k
    nbits -= 8;
93
79.8k
  }
94
95
51.9k
  if (nbits) {
96
43.3k
    rv = nghttp2_bufs_addb(
97
43.3k
        bufs, (uint8_t)((uint8_t)(code >> 56) | ((1 << (8 - nbits)) - 1)));
98
43.3k
    if (rv != 0) {
99
0
      return rv;
100
0
    }
101
43.3k
  }
102
103
51.9k
  return 0;
104
51.9k
}
105
106
4.32k
void nghttp2_hd_huff_decode_context_init(nghttp2_hd_huff_decode_context *ctx) {
107
4.32k
  ctx->fstate = NGHTTP2_HUFF_ACCEPTED;
108
4.32k
}
109
110
nghttp2_ssize nghttp2_hd_huff_decode(nghttp2_hd_huff_decode_context *ctx,
111
                                     nghttp2_buf *buf, const uint8_t *src,
112
4.36k
                                     size_t srclen, int final) {
113
4.36k
  const uint8_t *end = src + srclen;
114
4.36k
  nghttp2_huff_decode node = {ctx->fstate, 0};
115
4.36k
  const nghttp2_huff_decode *t = &node;
116
4.36k
  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
122k
  for (; src != end;) {
123
118k
    c = *src++;
124
118k
    t = &huff_decode_table[t->fstate & 0x1ff][c >> 4];
125
118k
    if (t->fstate & NGHTTP2_HUFF_SYM) {
126
87.0k
      *buf->last++ = t->sym;
127
87.0k
    }
128
129
118k
    t = &huff_decode_table[t->fstate & 0x1ff][c & 0xf];
130
118k
    if (t->fstate & NGHTTP2_HUFF_SYM) {
131
67.0k
      *buf->last++ = t->sym;
132
67.0k
    }
133
118k
  }
134
135
4.36k
  ctx->fstate = t->fstate;
136
137
4.36k
  if (final && !(ctx->fstate & NGHTTP2_HUFF_ACCEPTED)) {
138
96
    return NGHTTP2_ERR_HEADER_COMP;
139
96
  }
140
141
4.26k
  return (nghttp2_ssize)srclen;
142
4.36k
}
143
144
4.26k
int nghttp2_hd_huff_decode_failure_state(nghttp2_hd_huff_decode_context *ctx) {
145
4.26k
  return ctx->fstate == 0x100;
146
4.26k
}