Skip to content

Commit

Permalink
sst: clang-format for coherent indentation and coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxmaniac committed May 18, 2023
1 parent e051bdc commit 578a17a
Show file tree
Hide file tree
Showing 7 changed files with 355 additions and 353 deletions.
201 changes: 106 additions & 95 deletions src/modules/sst/parse_sst.c
Expand Up @@ -34,34 +34,50 @@
#include "../../core/mem/mem.h"


static inline int/*bool*/ is_space( char c ) { return (c == ' ' || c == '\t'); }
static inline int/*bool*/ is_num( char c ) { return (c >= '0' && c <= '9'); }

static inline unsigned lower_byte( char b ) { return b | 0x20; }
static inline unsigned lower_4bytes( unsigned d ) { return d | 0x20202020; }
static inline unsigned lower_3bytes( unsigned d ) { return d | 0x202020; }
static inline unsigned read_4bytes( char *val ) {
return (*(val + 0) + (*(val + 1) << 8)
+ (*(val + 2) << 16) + (*(val + 3) << 24));
static inline int /*bool*/ is_space(char c)
{
return (c == ' ' || c == '\t');
}
static inline int /*bool*/ is_num(char c)
{
return (c >= '0' && c <= '9');
}

static inline unsigned lower_byte(char b)
{
return b | 0x20;
}
static inline unsigned lower_4bytes(unsigned d)
{
return d | 0x20202020;
}
static inline unsigned lower_3bytes(unsigned d)
{
return d | 0x202020;
}
static inline unsigned read_3bytes( char *val ) {
static inline unsigned read_4bytes(char *val)
{
return (*(val + 0) + (*(val + 1) << 8) + (*(val + 2) << 16)
+ (*(val + 3) << 24));
}
static inline unsigned read_3bytes(char *val)
{
return (*(val + 0) + (*(val + 1) << 8) + (*(val + 2) << 16));
}

/* compile-time constants if called with constants */
#define MAKE_4BYTES( a, b, c, d ) \
( ((a)&0xFF) | (((b)&0xFF)<<8) | (((c)&0xFF)<<16) | (((d)&0xFF)<<24) )
#define MAKE_3BYTES( a, b, c ) \
( ((a)&0xFF) | (((b)&0xFF)<<8) | (((c)&0xFF)<<16) )
#define MAKE_4BYTES(a, b, c, d) \
(((a)&0xFF) | (((b)&0xFF) << 8) | (((c)&0xFF) << 16) | (((d)&0xFF) << 24))
#define MAKE_3BYTES(a, b, c) \
(((a)&0xFF) | (((b)&0xFF) << 8) | (((c)&0xFF) << 16))


struct session_expires *
malloc_session_expires( void )
struct session_expires *malloc_session_expires(void)
{
struct session_expires *se = (struct session_expires *)
pkg_malloc( sizeof(struct session_expires) );
if ( se )
memset( se, 0, sizeof(struct session_expires) );
struct session_expires *se = (struct session_expires *)pkg_malloc(
sizeof(struct session_expires));
if(se)
memset(se, 0, sizeof(struct session_expires));
return se;
}

Expand All @@ -71,105 +87,104 @@ malloc_session_expires( void )
void hf_free_session_expires(void *parsed)
{
struct session_expires *se;
se = (struct session_expires*)parsed;
se = (struct session_expires *)parsed;
free_session_expires(se);
}


void
free_session_expires( struct session_expires *se )
void free_session_expires(struct session_expires *se)
{
if ( se )
pkg_free( se );
if(se)
pkg_free(se);
}


enum parse_sst_result
parse_session_expires_body( struct hdr_field *hf )
enum parse_sst_result parse_session_expires_body(struct hdr_field *hf)
{
register char *p = hf->body.s;
int pos = 0;
int len = hf->body.len;
char *q;
struct session_expires se = { 0, 0, sst_refresher_unspecified };
struct session_expires se = {0, 0, sst_refresher_unspecified};
unsigned tok;

if ( !p || len <= 0 ) {
LM_ERR(" no body for header field\n" );
if(!p || len <= 0) {
LM_ERR(" no body for header field\n");
return parse_sst_header_not_found;
}

/* skip whitespace */
for ( ; pos < len && is_space(*p); ++pos, ++p )
for(; pos < len && is_space(*p); ++pos, ++p)
/*nothing*/;

/* collect a number */
for ( q = p; pos < len && is_num(*q); ++pos, ++q )
se.interval = se.interval*10/*radix*/ + (*q - '0');
for(q = p; pos < len && is_num(*q); ++pos, ++q)
se.interval = se.interval * 10 /*radix*/ + (*q - '0');

if ( q == p ) /*nothing parsed */ {
LM_ERR(" no expiry interval\n" );
if(q == p) /*nothing parsed */ {
LM_ERR(" no expiry interval\n");
return parse_sst_no_value;
}
p = q;

/* continue on with params */
while ( pos < len ) {

if ( *p == ';' ) {
++p; ++pos;

if ( pos + 4 < len ) {
switch ( lower_4bytes(read_4bytes(p)) ) {
case /*refr*/MAKE_4BYTES('r','e','f','r'):
if ( pos + 9 <= len
&& lower_4bytes(read_4bytes(p+4))
== /*eshe*/MAKE_4BYTES('e','s','h','e')
&& lower_byte(*(p+8)) == 'r'
&& *(p+9) == '=' ) {
tok = lower_3bytes( read_3bytes(p+10) );
if ( tok == MAKE_3BYTES('u','a','c') ) {
while(pos < len) {

if(*p == ';') {
++p;
++pos;

if(pos + 4 < len) {
switch(lower_4bytes(read_4bytes(p))) {
case /*refr*/ MAKE_4BYTES('r', 'e', 'f', 'r'):
if(pos + 9 <= len
&& lower_4bytes(read_4bytes(p + 4))
== /*eshe*/ MAKE_4BYTES(
'e', 's', 'h', 'e')
&& lower_byte(*(p + 8)) == 'r'
&& *(p + 9) == '=') {
tok = lower_3bytes(read_3bytes(p + 10));
if(tok == MAKE_3BYTES('u', 'a', 'c')) {
se.refresher = sst_refresher_uac;
p += 13; pos += 13;
}
else if ( tok == MAKE_3BYTES('u','a','s') ) {
p += 13;
pos += 13;
} else if(tok == MAKE_3BYTES('u', 'a', 's')) {
se.refresher = sst_refresher_uas;
p += 13; pos += 13;
}
else /* unrecognized refresher-param */ {
LM_ERR(" unrecognized refresher\n" );
p += 13;
pos += 13;
} else /* unrecognized refresher-param */ {
LM_ERR(" unrecognized refresher\n");
return parse_sst_parse_error;
}
}
else /* not "esher=" */ {
} else /* not "esher=" */ {
/* there are no other se-params
that start with "refr" */
for ( ; pos < len && *p != ';'; ++pos, ++p )
for(; pos < len && *p != ';'; ++pos, ++p)
/*skip to ';'*/;
}
break;
default:
/* unrecognized se-param */
for ( ; pos < len && *p != ';'; ++pos, ++p )
for(; pos < len && *p != ';'; ++pos, ++p)
/*skip to ';'*/;
break;
} /*switch*/
} /* exist 4 bytes to check */
} /* exist 4 bytes to check */
else /* less than 4 bytes left */ {
/* not enough text left for any of the recognized se-params */
/* no other recognized se-param */
for ( ; pos < len && *p != ';'; ++pos, ++p ) /*skip to ';'*/;
for(; pos < len && *p != ';'; ++pos, ++p) /*skip to ';'*/
;
}
}
else /* not ';' */ {
} else /* not ';' */ {
LM_ERR("no semicolon separating se-params\n");
return parse_sst_parse_error;
} /* if ';' */
} /* while */
} /* while */

hf->parsed = malloc_session_expires();
if ( !hf->parsed ) {
LM_ERR(" out of pkg memory\n" );
if(!hf->parsed) {
LM_ERR(" out of pkg memory\n");
return parse_sst_out_of_mem;
}
se.hfree = hf_free_session_expires;
Expand All @@ -179,71 +194,67 @@ parse_session_expires_body( struct hdr_field *hf )
}


enum parse_sst_result
parse_session_expires( struct sip_msg *msg, struct session_expires *se )
enum parse_sst_result parse_session_expires(
struct sip_msg *msg, struct session_expires *se)
{
enum parse_sst_result result;

if ( msg->session_expires ) {
if ( msg->session_expires->parsed == 0
&& (result = parse_session_expires_body(msg->session_expires))
!= parse_sst_success ) {
if(msg->session_expires) {
if(msg->session_expires->parsed == 0
&& (result = parse_session_expires_body(msg->session_expires))
!= parse_sst_success) {
return result;
}
if ( se ) {
if(se) {
*se = *((struct session_expires *)msg->session_expires->parsed);
}
return parse_sst_success;
}
else {
} else {
return parse_sst_header_not_found;
}
}


enum parse_sst_result
parse_min_se_body( struct hdr_field *hf )
enum parse_sst_result parse_min_se_body(struct hdr_field *hf)
{
int len = hf->body.len;
char *p = hf->body.s;
int pos = 0;
unsigned int interval = 0;

/* skip whitespace */
for ( ; pos < len && is_space(*p); ++pos, ++p )
for(; pos < len && is_space(*p); ++pos, ++p)
/*nothing*/;
if ( pos == len )
if(pos == len)
return parse_sst_no_value;
/* collect a number */
for ( ; pos < len && is_num(*p); ++pos, ++p )
interval = interval*10/*radix*/ + (*p - '0');
for(; pos < len && is_num(*p); ++pos, ++p)
interval = interval * 10 /*radix*/ + (*p - '0');
/* skip whitespace */
for ( ; pos < len && is_space(*p); ++pos, ++p )
for(; pos < len && is_space(*p); ++pos, ++p)
/*nothing*/;
if ( pos != len ) /* shouldn't be any more junk */
if(pos != len) /* shouldn't be any more junk */
return parse_sst_parse_error;
hf->parsed=(void*)(long)interval;
hf->parsed = (void *)(long)interval;
return parse_sst_success;
}


enum parse_sst_result
parse_min_se( struct sip_msg *msg, unsigned int *min_se )
enum parse_sst_result parse_min_se(struct sip_msg *msg, unsigned int *min_se)
{
enum parse_sst_result result;

if ( msg->min_se ) {
if ( msg->min_se->parsed == 0
&& (result = parse_min_se_body(msg->min_se))
!= parse_sst_success ) {
if(msg->min_se) {
if(msg->min_se->parsed == 0
&& (result = parse_min_se_body(msg->min_se))
!= parse_sst_success) {
return result;
}
if ( min_se ) {
if(min_se) {
*min_se = (unsigned int)(long)msg->min_se->parsed;
}
return parse_sst_success;
}
else {
} else {
return parse_sst_header_not_found;
}
}
34 changes: 17 additions & 17 deletions src/modules/sst/parse_sst.h
Expand Up @@ -37,7 +37,8 @@
/*!
* Indicate the "refresher=" value of the Session-Expires header.
*/
enum sst_refresher {
enum sst_refresher
{
sst_refresher_unspecified,
sst_refresher_uac,
sst_refresher_uas,
Expand All @@ -48,37 +49,37 @@ enum sst_refresher {
* We will treat the 'void* parsed' field of struct hdr_field as
* a pointer to a struct session_expires.
*/
struct session_expires {
hf_parsed_free_f hfree; /* function to free the content */
unsigned interval; /* in seconds */
enum sst_refresher refresher;
struct session_expires
{
hf_parsed_free_f hfree; /* function to free the content */
unsigned interval; /* in seconds */
enum sst_refresher refresher;
};


enum parse_sst_result {
enum parse_sst_result
{
parse_sst_success,
parse_sst_header_not_found, /* no header */
parse_sst_header_not_found, /* no header */
parse_sst_no_value, /* no interval specified */
#if NOT_IMPLEMENTED_YET
parse_sst_duplicate, /* multiple s-e / x / min-se headers found */
parse_sst_duplicate, /* multiple s-e / x / min-se headers found */
#endif
parse_sst_out_of_mem,
parse_sst_parse_error, /* something puked */
parse_sst_parse_error, /* something puked */
};


/*!
* Allocate a zeroed-out struct session_expires.
*/
struct session_expires *
malloc_session_expires( void );
struct session_expires *malloc_session_expires(void);


/*!
* Deallocates memory previously allocated via malloc_session_expires().
*/
void
free_session_expires( struct session_expires * );
void free_session_expires(struct session_expires *);


/*!
Expand All @@ -103,8 +104,8 @@ free_session_expires( struct session_expires * );
* *((struct session_expires *)msg->session_expires->parsed)
* \return parse_sst_result
*/
enum parse_sst_result
parse_session_expires( struct sip_msg *msg, struct session_expires *se );
enum parse_sst_result parse_session_expires(
struct sip_msg *msg, struct session_expires *se);


/*!
Expand All @@ -118,8 +119,7 @@ parse_session_expires( struct sip_msg *msg, struct session_expires *se );
* result is also available in (unsigned)msg->min_se->parsed
* \return parse_sst_result
*/
enum parse_sst_result
parse_min_se( struct sip_msg *msg, unsigned *min_se );
enum parse_sst_result parse_min_se(struct sip_msg *msg, unsigned *min_se);


#endif /* ! PARSE_SST_H */

0 comments on commit 578a17a

Please sign in to comment.