Skip to content

Commit a14c74f

Browse files
committed
Move all _Generic() statements into lib/str2const.h, so that we can have
a less-typesafe callback for older compilers. Provide a fallback if HAVE_GENERICS is not definedi (e.g. CentOS 7).
1 parent 5af4f12 commit a14c74f

File tree

4 files changed

+67
-24
lines changed

4 files changed

+67
-24
lines changed

lib/str2const.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (C) 2020 Sippy Software, Inc.
3+
*
4+
* This file is part of opensips, a free SIP server.
5+
*
6+
* opensips is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version
10+
*
11+
* opensips is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19+
*
20+
*/
21+
22+
#ifndef __LIB_STR2CONST_H__
23+
#define __LIB_STR2CONST_H__
24+
25+
#if defined(HAVE_GENERICS)
26+
#define str2const(_sp) ( \
27+
_Generic((_sp), str *: _s2c, const str *: _cs2cc)(_sp) \
28+
)
29+
30+
#define escape_user(sin, sout) ( \
31+
_Generic(*(sin), str: _escape_userSS, str_const: _escape_user)(sin, sout) \
32+
)
33+
34+
#define unescape_user(sin, sout) ( \
35+
_Generic(*(sin), str: _unescape_userSS, str_const: _unescape_user)(sin, sout) \
36+
)
37+
38+
#define escape_param(sin, sout) ( \
39+
_Generic(*(sin), str: _escape_paramSS, str_const: _escape_param)(sin, sout) \
40+
)
41+
42+
#define unescape_param(sin, sout) ( \
43+
_Generic(*(sin), str: _unescape_paramSS, str_const: _unescape_param)(sin, sout) \
44+
)
45+
46+
#define str_strcmp(_a, _b) _Generic(*(_a), \
47+
str: _Generic(*(_b), \
48+
str: _str_strcmpSS, \
49+
str_const: _str_strcmpSC), \
50+
str_const: _Generic(*(_b), \
51+
str: _str_strcmpCS, \
52+
str_const: _str_strcmpCC) \
53+
)(_a, _b)
54+
#else /* !HAVE_GENERICS */
55+
#define str2const(_sp) ((str_const *)(void *)(_sp))
56+
#define escape_user(sin, sout) _escape_user(str2const(sin), sout)
57+
#define unescape_user(sin, sout) _unescape_user(str2const(sin), sout)
58+
#define escape_param(sin, sout) _escape_param(str2const(sin), sout)
59+
#define unescape_param(sin, sout) _unescape_param(str2const(sin), sout)
60+
#define str_strcmp(_a, _b) _str_strcmpCC(str2const(_a), str2const(_b))
61+
#endif /* HAVE_GENERICS */
62+
63+
#endif /* __LIB_STR2CONST_H__ */

str.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#define str_h
2323

2424
#include <string.h>
25+
#include "lib/str2const.h"
2526

2627
/**
2728
* \file
@@ -74,10 +75,6 @@ typedef struct __str_const str_const;
7475
static inline const str_const *_cs2cc(const str *_sp) {return (const str_const *)(const void *)(_sp);}
7576
static inline str_const *_s2c(str *_sp) {return (str_const *)(void *)(_sp);}
7677

77-
#define str2const(_sp) ( \
78-
_Generic((_sp), str *: _s2c, const str *: _cs2cc)(_sp) \
79-
)
80-
8178
static inline void init_str(str *dest, const char *src)
8279
{
8380
dest->s = (char *)src;

strcommon.h

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "str.h"
3030
#include "md5.h"
3131
#include "crc.h"
32+
#include "lib/str2const.h"
3233

3334
/*
3435
* add backslashes to special characters
@@ -45,26 +46,14 @@ void compute_md5(char *dst, const char *src, int src_len);
4546

4647
int _escape_user(const str_const *sin, str *sout);
4748
static inline int _escape_userSS(const str *sin, str *sout){return _escape_user(str2const(sin), sout);}
48-
#define escape_user(sin, sout) ( \
49-
_Generic(*(sin), str: _escape_userSS, str_const: _escape_user)(sin, sout) \
50-
)
5149

5250
int _unescape_user(const str_const *sin, str *sout);
5351
static inline int _unescape_userSS(const str *sin, str *sout){return _unescape_user(str2const(sin), sout);}
54-
#define unescape_user(sin, sout) ( \
55-
_Generic(*(sin), str: _unescape_userSS, str_const: _unescape_user)(sin, sout) \
56-
)
5752

5853
int _escape_param(const str_const *sin, str *sout);
5954
static inline int _escape_paramSS(const str *sin, str *sout){return _escape_param(str2const(sin), sout);}
60-
#define escape_param(sin, sout) ( \
61-
_Generic(*(sin), str: _escape_paramSS, str_const: _escape_param)(sin, sout) \
62-
)
6355

6456
int _unescape_param(const str_const *sin, str *sout);
6557
static inline int _unescape_paramSS(const str *sin, str *sout){return _unescape_param(str2const(sin), sout);}
66-
#define unescape_param(sin, sout) ( \
67-
_Generic(*(sin), str: _unescape_paramSS, str_const: _unescape_param)(sin, sout) \
68-
)
6958

7059
#endif

ut.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
#include "mem/mem.h"
4444
#include "mem/shm_mem.h"
4545

46+
#include "lib/str2const.h"
47+
4648
typedef struct _int_str_t {
4749
union {
4850
int i;
@@ -1033,14 +1035,6 @@ static inline int _str_strcmpCS(const str_const *a, const str *b)
10331035
{
10341036
return _str_strcmpCC(a, str2const(b));
10351037
}
1036-
#define str_strcmp(_a, _b) _Generic(*(_a), \
1037-
str: _Generic(*(_b), \
1038-
str: _str_strcmpSS, \
1039-
str_const: _str_strcmpSC), \
1040-
str_const: _Generic(*(_b), \
1041-
str: _str_strcmpCS, \
1042-
str_const: _str_strcmpCC) \
1043-
)(_a, _b)
10441038

10451039
/*
10461040
* compares a str with a const null terminated string

0 commit comments

Comments
 (0)