Coverage Report

Created: 2026-01-25 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensips/parser/parse_hname2.c
Line
Count
Source
1
/*
2
 * Fast 32-bit Header Field Name Parser
3
 *
4
 * Copyright (C) 2001-2003 FhG Fokus
5
 *
6
 * This file is part of opensips, a free SIP server.
7
 *
8
 * opensips is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation; either version 2 of the License, or
11
 * (at your option) any later version
12
 *
13
 * opensips is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
21
 *
22
 * History:
23
 * --------
24
 * 2003-02-28 scratchpad compatibility abandoned (jiri)
25
 * 2003-01-27 next baby-step to removing ZT - PRESERVE_ZT (jiri)
26
 * 2003-05-01 added support for Accept HF (janakj)
27
 * 2006-02-17 Session-Expires, Min-SE (dhsueh@somanetworks.com)
28
 */
29
30
31
#include "parse_hname2.h"
32
#include "keys.h"
33
#include "../ut.h"  /* q_memchr */
34
35
1.90M
#define LOWER_BYTE(b) ((b) | 0x20U)
36
5.26M
#define LOWER_DWORD(d) ((d) | 0x20202020U)
37
38
/*
39
 * Skip all white-chars and return position of the first
40
 * non-white char
41
 */
42
static inline char* skip_ws(char* p, char *end)
43
132k
{
44
167k
  for(; p < end; p++) {
45
166k
    if ((*p != ' ') && (*p != '\t')) return p;
46
166k
  }
47
430
  return p;
48
132k
}
49
50
/*
51
 * Parser macros
52
 */
53
#include "case_via.h"      /* Via */
54
#include "case_from.h"     /* From */
55
#include "case_to.h"       /* To */
56
#include "case_cseq.h"     /* CSeq */
57
#include "case_call.h"     /* Call-ID */
58
#include "case_cont.h"     /* Contact, Content-Type, Content-Length,
59
                              Content-Purpose, Content-Action,
60
                              Content-Disposition */
61
#include "case_rout.h"     /* Route */
62
#include "case_max.h"      /* Max-Forwards */
63
#include "case_reco.h"     /* Record-Route */
64
#include "case_path.h"     /* Path */
65
#include "case_auth.h"     /* Authorization */
66
#include "case_expi.h"     /* Expires */
67
#include "case_prox.h"     /* Proxy-Authorization, Proxy-Require */
68
#include "case_allo.h"     /* Allow */
69
#include "case_unsu.h"     /* Unsupported */
70
#include "case_even.h"     /* Event */
71
#include "case_acce.h"     /* Accept, Accept-Language */
72
#include "case_orga.h"     /* Organization */
73
#include "case_prio.h"     /* Priority */
74
#include "case_subj.h"     /* Subject */
75
#include "case_user.h"     /* User-Agent */
76
#include "case_supp.h"     /* Supported */
77
#include "case_dive.h"     /* Diversion */
78
#include "case_remo.h"     /* Remote-Party-ID */
79
#include "case_refe.h"     /* Refer-To */
80
#include "case_sess.h"     /* Session-Expires */
81
#include "case_min_.h"     /* Min-SE */
82
#include "case_p_pr.h"     /* P-Preferred-Identity */
83
#include "case_p_as.h"     /* P-Asserted-Identity */
84
#include "case_priv.h"     /* Privacy */
85
#include "case_retr.h"     /* Retry-After */
86
#include "case_www.h"      /* WWW-Authenticate */
87
#include "case_feat.h"     /* Feature-Caps */
88
#include "case_repl.h"     /* Replaces */
89
#include "case_to_p.h"     /* To-Path */
90
#include "case_mess.h"     /* Message-ID */
91
#include "case_byte.h"     /* Byte-Range */
92
#include "case_fail.h"     /* Failure-Report */
93
#include "case_succ.h"     /* Success-Report */
94
#include "case_stat.h"     /* Status */
95
#include "case_use_.h"     /* Use-Path */
96
#include "case_secu.h"     /* Security-Client, Security-Server,
97
                              Security-Verify */
98
99
100
/*
101
 * Read 4-bytes from memory, as an unsigned integer
102
 * Reading byte by byte ensures that the code works also on HW which
103
 * does not allow reading 4-bytes at once from unaligned memory position
104
 * (Sparc for example)
105
 */
106
#define READ(addr) \
107
2.59M
  ((unsigned)*((unsigned char *)addr + 0) + \
108
2.59M
   ((unsigned)*((unsigned char *)addr + 1) << 8) + \
109
2.59M
   ((unsigned)*((unsigned char *)addr + 2) << 16) + \
110
2.59M
   ((unsigned)*((unsigned char *)addr + 3) << 24))
111
112
#ifdef FUZZ_BUILD
113
/* fuzzers are sensible to heap read overflows, so enable all "HAVE" checks */
114
2.24M
#define HAVE(bytes) (end - p >= (long)(bytes))
115
#else
116
/* with PKG memory, parser read overflows of a few bytes are harmless, since
117
 * the memory is pre-allocated and the read cannot SIGSEGV, making the parser
118
 * a lot more performant in production */
119
#define HAVE(bytes) 1
120
#endif
121
122
#define FIRST_QUATERNIONS       \
123
645
  case _via1_: via1_CASE; \
124
22.3k
  case _from_: from_CASE; \
125
538
  case _to12_: to12_CASE; \
126
132k
  case _cseq_: cseq_CASE; \
127
132k
  case _call_: call_CASE; \
128
86.6k
  case _cont_: cont_CASE; \
129
15.6k
  case _rout_: rout_CASE; \
130
31.0k
  case _max__: max_CASE;  \
131
26.5k
  case _reco_: reco_CASE; \
132
896
  case _via2_: via2_CASE; \
133
35.1k
  case _auth_: auth_CASE; \
134
30.7k
  case _supp_: supp_CASE; \
135
35.4k
  case _expi_: expi_CASE; \
136
141k
  case _prox_: prox_CASE; \
137
21.9k
  case _allo_: allo_CASE; \
138
26.7k
  case _path_: path_CASE; \
139
27.7k
  case _unsu_: unsu_CASE; \
140
16.4k
  case _even_: even_CASE; \
141
122k
  case _acce_: acce_CASE; \
142
43.3k
  case _orga_: orga_CASE; \
143
28.3k
  case _prio_: prio_CASE; \
144
30.7k
  case _subj_: subj_CASE; \
145
24.1k
  case _user_: user_CASE; \
146
29.1k
  case _dive_: dive_CASE; \
147
18.0k
  case _remo_: remo_CASE; \
148
19.1k
  case _refe_: refe_CASE; \
149
33.0k
  case _sess_: sess_CASE; \
150
63.8k
  case _min__: min__CASE; \
151
49.4k
  case _p_pr_: p_pr_CASE; \
152
29.3k
  case _p_as_: p_as_CASE; \
153
29.4k
  case _priv_: priv_CASE; \
154
27.0k
  case _retr_: retr_CASE; \
155
56.0k
  case _www__: www_CASE;  \
156
25.5k
  case _feat_: feat_CASE; \
157
16.5k
  case _repl_: repl_CASE; \
158
30.7k
  case _to_p_: to_p_CASE; \
159
16.9k
  case _mess_: mess_CASE; \
160
15.9k
  case _byte_: byte_CASE; \
161
21.7k
  case _fail_: fail_CASE; \
162
49.7k
  case _succ_: succ_CASE; \
163
7.41k
  case _stat_: stat_CASE; \
164
2.65k
  case _use__: use__CASE; \
165
96.0k
  case _secu_: secu_CASE; \
166
167
168
#define PARSE_COMPACT(id)      \
169
498k
  switch(*(p + 1)) {         \
170
14.1k
    case ' ':              \
171
46.7k
    case '\t':             \
172
46.7k
      hdr->type = id;    \
173
46.7k
      hdr->name.len = 1; \
174
46.7k
      p += 2;            \
175
46.7k
      goto dc_end;       \
176
287k
    case ':':              \
177
287k
      hdr->type = id;    \
178
287k
      hdr->name.len = 1; \
179
287k
      return (p + 2);    \
180
498k
  }
181
182
183
char* parse_hname2(char* begin, char* end, struct hdr_field* hdr)
184
2.72M
{
185
2.72M
  register char* p;
186
2.72M
  register unsigned int val;
187
188
2.72M
  if ((end - begin) < 4) {
189
1.70k
    hdr->type = HDR_ERROR_T;
190
1.70k
    return begin;
191
1.70k
  }
192
193
2.72M
  p = begin;
194
195
2.72M
  val = LOWER_DWORD(READ(p));
196
2.72M
  hdr->name.s = begin;
197
198
2.72M
  switch(val) {
199
200
2.73M
    FIRST_QUATERNIONS;
201
    /* fall through */
202
203
2.73M
    default:
204
1.12M
      switch(LOWER_BYTE(*p)) {
205
116k
        case 't':
206
116k
          switch(LOWER_BYTE(*(p + 1))) {
207
9.96k
            case 'o':
208
9.96k
              p += 2;
209
9.96k
              hdr->type = HDR_TO_T;
210
9.96k
              hdr->name.len = 2;
211
9.96k
              goto dc_cont;
212
2.26k
            case ' ':
213
2.26k
            case '\t':
214
2.26k
              p += 2;
215
2.26k
              hdr->type = HDR_TO_T;
216
2.26k
              hdr->name.len = 1;
217
2.26k
              goto dc_end;
218
86.9k
            case ':':
219
86.9k
              hdr->type = HDR_TO_T;
220
86.9k
              hdr->name.len = 1;
221
86.9k
              return (p + 2);
222
116k
          }
223
17.8k
          break;
224
158k
        case 'v': PARSE_COMPACT(HDR_VIA_T);           break;
225
30.5k
        case 'f': PARSE_COMPACT(HDR_FROM_T);          break;
226
33.8k
        case 'i': PARSE_COMPACT(HDR_CALLID_T);        break;
227
64.6k
        case 'm': PARSE_COMPACT(HDR_CONTACT_T);       break;
228
13.6k
        case 'l': PARSE_COMPACT(HDR_CONTENTLENGTH_T); break;
229
10.9k
        case 'k': PARSE_COMPACT(HDR_SUPPORTED_T);     break;
230
98.5k
        case 'c': PARSE_COMPACT(HDR_CONTENTTYPE_T);   break;
231
58.9k
        case 'o': PARSE_COMPACT(HDR_EVENT_T);         break;
232
28.7k
        case 'x': PARSE_COMPACT(HDR_SESSION_EXPIRES_T); break;
233
1.12M
      }
234
695k
      goto other;
235
2.72M
  }
236
  /* the above swtich will never continue here */
237
238
239
64.3k
 dc_end:
240
  /* HDR name entirely found, consume WS till colon */
241
  /* overflow during the "switch-case" parsing ? */
242
64.3k
  if (p>=end)
243
33
    goto error;
244
64.3k
  p = skip_ws(p, end);
245
64.3k
  if (p >= end || *p != ':')
246
215
    goto error;
247
  /* hdr type, name should be already set at this point */
248
64.1k
  return (p+1);
249
  /*done*/
250
251
252
682k
 dc_cont:
253
  /* HDR name partially found, see what's next */
254
  /* overflow during the "switch-case" parsing ? */
255
682k
  if (p>=end)
256
346
    goto error;
257
  /* hdr type, name should be already set at this point (for partial finding) */
258
682k
  switch (*p) {
259
601k
    case ':' :
260
601k
      return (p+1);
261
1.82k
    case ' ':
262
9.80k
    case '\t':
263
      /* consume spaces to the end of name */
264
9.80k
      p = skip_ws( p+1, end);
265
9.80k
      if (p >= end || *p != ':')
266
51
        goto error;
267
9.75k
      return (p+1);
268
    /* default: it seems the hdr name continues, fall to "other" */
269
682k
  }
270
271
272
1.48M
 other:
273
  /* Unknown header type */
274
1.48M
  hdr->type = HDR_OTHER_T;
275
  /* if overflow during the "switch-case" parsing, the "while" will
276
   * exit and we will fall in the "error" section */
277
18.9M
  while ( p < end ) {
278
18.9M
    switch (*p) {
279
1.42M
      case ':' :
280
1.42M
        hdr->name.len = p - hdr->name.s;
281
1.42M
        return (p + 1);
282
53.1k
      case ' ' :
283
58.1k
      case '\t':
284
58.1k
        hdr->name.len = p - hdr->name.s;
285
58.1k
        p = skip_ws(p+1, end);
286
58.1k
        if (p >= end || *p != ':')
287
1.93k
          goto error;
288
56.2k
        return (p+1);
289
18.9M
    }
290
17.4M
    p++;
291
17.4M
  }
292
293
9.80k
 error:
294
  /* No colon found, error.. */
295
9.80k
  hdr->type = HDR_ERROR_T;
296
9.80k
  hdr->name.s = 0;
297
9.80k
  hdr->name.len = 0;
298
9.80k
  return 0;
299
1.48M
}