libwebsockets
Lightweight C library for HTML5 websockets
Loading...
Searching...
No Matches
lws-purify.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define LWS_JSON_PURIFY_FLAG_HTML_SAFE   1

Functions

LWS_VISIBLE LWS_EXTERN const char * lws_sql_purify (char *escaped, const char *string, size_t len)
int lws_sql_purify_len (const char *p)
LWS_VISIBLE LWS_EXTERN const char * lws_json_purify (char *escaped, const char *string, int len, int *in_used)
LWS_VISIBLE LWS_EXTERN const char * lws_json_purify_flags (char *escaped, const char *string, int len, int *in_used, int flags)
LWS_VISIBLE LWS_EXTERN int lws_json_purify_len (const char *string)
LWS_VISIBLE LWS_EXTERN void lws_filename_purify_inplace (char *filename)
LWS_VISIBLE LWS_EXTERN int lws_plat_write_cert (struct lws_vhost *vhost, int is_key, int fd, void *buf, size_t len)
LWS_VISIBLE LWS_EXTERN int lws_plat_write_file (const char *filename, void *buf, size_t len)
LWS_VISIBLE LWS_EXTERN int lws_plat_read_file (const char *filename, void *buf, size_t len)
LWS_VISIBLE LWS_EXTERN int lws_plat_recommended_rsa_bits (void)

Macro Definition Documentation

◆ LWS_JSON_PURIFY_FLAG_HTML_SAFE

#define LWS_JSON_PURIFY_FLAG_HTML_SAFE   1

Definition at line 108 of file lws-purify.h.

Function Documentation

◆ lws_sql_purify()

LWS_VISIBLE LWS_EXTERN const char * lws_sql_purify ( char * escaped,
const char * string,
size_t len )

lws_sql_purify() - like strncpy but with escaping for sql quotes

Parameters
escapedoutput buffer
stringinput buffer ('/0' terminated)
lenoutput buffer max length

Because escaping expands the output string, it's not possible to do it in-place, ie, with escaped == string

◆ lws_sql_purify_len()

int lws_sql_purify_len ( const char * p)

lws_sql_purify_len() - return length of purified version of input string

Parameters
pinput buffer ('/0' terminated)

Calculates any character escaping without writing it anywhere and returns the calculated length of the purified string.

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_json_purify()

LWS_VISIBLE LWS_EXTERN const char * lws_json_purify ( char * escaped,
const char * string,
int len,
int * in_used )

lws_json_purify() - like strncpy but with escaping for json chars

Parameters
escapedoutput buffer
stringinput buffer ('/0' terminated)
lenoutput buffer max length
in_usedNULL, or pointer to input cap on entry and the number of bytes of string we could escape in len on output

Because escaping expands the output string, it's not possible to do it in-place, ie, with escaped == string

If in_used is non-NULL, then on entry a positive value is taken as a cap on the number of bytes of string that may be processed; this allows processing non-NUL-terminated data like blobs in bounded chunks. A zero or negative value on entry means no cap. On return, it's set to the number of input bytes that were actually processed.

Escaped output is always truncated on an escape boundary, so the output is always syntactically valid however small len is. When it is truncated because len is full, it is also truncated on a UTF-8 sequence boundary, so a valid UTF-8 input never gives invalid UTF-8 output (in_used then points at the start of the sequence that did not fit). A cut made by the caller's input cap is left where the caller put it.

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_json_purify_flags()

LWS_VISIBLE LWS_EXTERN const char * lws_json_purify_flags ( char * escaped,
const char * string,
int len,
int * in_used,
int flags )

lws_json_purify_flags() - lws_json_purify() with additional options

Parameters
escapedoutput buffer
stringinput buffer ('/0' terminated)
lenoutput buffer max length
in_usedNULL, or pointer to input cap on entry and the number of bytes of string we could escape in len on output
flagsOR of LWS_JSON_PURIFY_FLAG_*

Performs the same job as lws_json_purify() with extra behaviours selected using flags.

◆ lws_json_purify_len()

LWS_VISIBLE LWS_EXTERN int lws_json_purify_len ( const char * string)

lws_json_purify_len() - find out the escaped length of a string

Parameters
stringinput buffer ('/0' terminated)

JSON may have to expand escapes by up to 6x the original depending on what it is. This doesn't actually do the escaping but goes through the motions and computes the length of the escaped string.

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_filename_purify_inplace()

LWS_VISIBLE LWS_EXTERN void lws_filename_purify_inplace ( char * filename)

lws_filename_purify_inplace() - make a name usable as one path component

Parameters
filenameNUL-terminated filename to be purified in place

Turns an untrusted name, eg, a filename a peer sent, into a single path component that names a file inside whatever directory it is used in, on every platform, by replacing characters with '_'. The length does not change. The result is identical whichever platform it is done on.

Afterwards, the name has no

  • path separator, neither '/' nor '\' (so it is not absolute and has no subdirectory part)
  • control characters (< 0x20, 0x7f)
  • ':' (drive or NTFS stream), '*', '?', '"', '<', '>', '|', '$', ''
  • ".." anywhere, or leading '.' (so it is never ".", ".." or a hidden file)

and is not a Windows device name (CON, NUL, COM1, LPT1, ... with or without an extension).

An empty name stays empty: the caller must refuse it. The caller must also still bound its length, and must not follow an existing symlink of that name, eg, by creating it with O_CREAT | O_EXCL.

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_plat_write_cert()

LWS_VISIBLE LWS_EXTERN int lws_plat_write_cert ( struct lws_vhost * vhost,
int is_key,
int fd,
void * buf,
size_t len )

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_plat_write_file()

LWS_VISIBLE LWS_EXTERN int lws_plat_write_file ( const char * filename,
void * buf,
size_t len )

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_plat_read_file()

LWS_VISIBLE LWS_EXTERN int lws_plat_read_file ( const char * filename,
void * buf,
size_t len )

References LWS_EXTERN, and LWS_VISIBLE.

◆ lws_plat_recommended_rsa_bits()

LWS_VISIBLE LWS_EXTERN int lws_plat_recommended_rsa_bits ( void )