Skip to content

Commit

Permalink
o Fix escape_common(), unescape_common(), unescape_xml() and compute_…
Browse files Browse the repository at this point in the history
…md5()

  to take "const char *src", not "char *src";

o Extend escape_user(), unescape_user(), escape_param() and unescape_param()
  to take either "const str *src" or "const str_const *src". This is to allow
  other code to use either.
  • Loading branch information
sobomax committed Jan 26, 2021
1 parent 87f235a commit f5ce953
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
25 changes: 14 additions & 11 deletions strcommon.c
Expand Up @@ -32,7 +32,7 @@
/*! \brief
* add backslashes to special characters
*/
int escape_common(char *dst, char *src, int src_len)
int escape_common(char *dst, const char *src, int src_len)
{
int i, j;

Expand Down Expand Up @@ -69,7 +69,7 @@ int escape_common(char *dst, char *src, int src_len)
/*! \brief
* remove backslashes to special characters
*/
int unescape_common(char *dst, char *src, int src_len)
int unescape_common(char *dst, const char *src, int src_len)
{
int i, j;

Expand Down Expand Up @@ -113,7 +113,7 @@ int unescape_common(char *dst, char *src, int src_len)
/*! \brief
* replace &#xx; with ascii character
*/
int unescape_xml(char *dst, char *src, int src_len)
int unescape_xml(char *dst, const char *src, int src_len)
{
int i, j;

Expand All @@ -135,7 +135,7 @@ int unescape_xml(char *dst, char *src, int src_len)
}

/*! \brief Compute MD5 checksum */
void compute_md5(char *dst, char *src, int src_len)
void compute_md5(char *dst, const char *src, int src_len)
{
MD5_CTX context;
unsigned char digest[16];
Expand All @@ -146,9 +146,10 @@ void compute_md5(char *dst, char *src, int src_len)
}

/*! \brief Unscape all printable ASCII characters */
int unescape_user(str *sin, str *sout)
int _unescape_user(const str_const *sin, str *sout)
{
char *at, *p, c;
char *at, c;
const char *p;

if(sin==NULL || sout==NULL || sout->s==NULL
|| sin->len<0 || sout->len < sin->len+1) {
Expand Down Expand Up @@ -263,10 +264,11 @@ int unescape_user(str *sin, str *sout)
* mark = - | _ | . | ! | ~ | * | ' | ( | )
* user-unreserved = & | = | + | $ | , | ; | ? | /
*/
int escape_user(str *sin, str *sout)
int _escape_user(const str_const *sin, str *sout)
{

char *at, *p;
char *at;
const char *p;
unsigned char x;

if(sin==NULL || sout==NULL || sin->s==NULL || sout->s==NULL
Expand Down Expand Up @@ -312,7 +314,7 @@ int escape_user(str *sin, str *sout)
}


int unescape_param(str *sin, str *sout)
int _unescape_param(const str_const *sin, str *sout)
{
return unescape_user(sin, sout);
}
Expand All @@ -326,9 +328,10 @@ int unescape_param(str *sin, str *sout)
* mark = - | _ | . | ! | ~ | * | ' | ( | )
* param-unreserved = [ | ] | / | : | & | + | $
*/
int escape_param(str *sin, str *sout)
int _escape_param(const str_const *sin, str *sout)
{
char *at, *p;
char *at;
const char *p;
unsigned char x;

if (sin==NULL || sout==NULL || sin->s==NULL || sout->s==NULL ||
Expand Down
33 changes: 24 additions & 9 deletions strcommon.h
Expand Up @@ -33,23 +33,38 @@
/*
* add backslashes to special characters
*/
int escape_common(char *dst, char *src, int src_len);
int escape_common(char *dst, const char *src, int src_len);
/*
* remove backslashes to special characters
*/
int unescape_common(char *dst, char *src, int src_len);
int unescape_common(char *dst, const char *src, int src_len);

int unescape_xml(char *dst, char *src, int src_len);
int unescape_xml(char *dst, const char *src, int src_len);

void compute_md5(char *dst, char *src, int src_len);
void compute_md5(char *dst, const char *src, int src_len);

int escape_user(str *sin, str *sout);
int _escape_user(const str_const *sin, str *sout);
static inline int _escape_userSS(const str *sin, str *sout){return _escape_user(str2const(sin), sout);}
#define escape_user(sin, sout) ( \
_Generic(*(sin), str: _escape_userSS, str_const: _escape_user)(sin, sout) \
)

int unescape_user(str *sin, str *sout);
int _unescape_user(const str_const *sin, str *sout);
static inline int _unescape_userSS(const str *sin, str *sout){return _unescape_user(str2const(sin), sout);}
#define unescape_user(sin, sout) ( \
_Generic(*(sin), str: _unescape_userSS, str_const: _unescape_user)(sin, sout) \
)

int escape_param(str *sin, str *sout);
int _escape_param(const str_const *sin, str *sout);
static inline int _escape_paramSS(const str *sin, str *sout){return _escape_param(str2const(sin), sout);}
#define escape_param(sin, sout) ( \
_Generic(*(sin), str: _escape_paramSS, str_const: _escape_param)(sin, sout) \
)

int unescape_param(str *sin, str *sout);
int _unescape_param(const str_const *sin, str *sout);
static inline int _unescape_paramSS(const str *sin, str *sout){return _unescape_param(str2const(sin), sout);}
#define unescape_param(sin, sout) ( \
_Generic(*(sin), str: _unescape_paramSS, str_const: _unescape_param)(sin, sout) \
)

#endif

0 comments on commit f5ce953

Please sign in to comment.