libwebsockets
Lightweight C library for HTML5 websockets
lws-tokenize.h File Reference
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  lws_tokenize
 
struct  lws_strexp
 

Macros

#define LWS_TOKENIZE_F_MINUS_NONTERM   (1 << 0)
 
#define LWS_TOKENIZE_F_AGG_COLON   (1 << 1)
 
#define LWS_TOKENIZE_F_COMMA_SEP_LIST   (1 << 2)
 
#define LWS_TOKENIZE_F_RFC7230_DELIMS   (1 << 3)
 
#define LWS_TOKENIZE_F_DOT_NONTERM   (1 << 4)
 
#define LWS_TOKENIZE_F_NO_FLOATS   (1 << 5)
 
#define LWS_TOKENIZE_F_NO_INTEGERS   (1 << 6)
 
#define LWS_TOKENIZE_F_HASH_COMMENT   (1 << 7)
 
#define LWS_TOKENIZE_F_SLASH_NONTERM   (1 << 8)
 
#define LWS_TOKENIZE_F_ASTERISK_NONTERM   (1 << 9)
 
#define LWS_TOKENIZE_F_EQUALS_NONTERM   (1 << 10)
 
#define LWS_TOKENIZE_F_COLON_NONTERM   (1 << 11)
 
#define LWS_TOKENIZE_F_EXPECT_MORE   (1 << 12)
 

Typedefs

typedef struct lws_tokenize lws_tokenize_t
 
typedef int(* lws_strexp_expand_cb) (void *priv, const char *name, char *out, size_t *pos, size_t olen, size_t *exp_ofs)
 
typedef struct lws_strexp lws_strexp_t
 

Enumerations

enum  lws_tokenize_elem {
  LWS_TOKZE_ERRS = 7 , LWS_TOKZE_TOO_LONG = -7 , LWS_TOKZE_WANT_READ = -6 , LWS_TOKZE_ERR_BROKEN_UTF8 = -5 ,
  LWS_TOKZE_ERR_UNTERM_STRING = -4 , LWS_TOKZE_ERR_MALFORMED_FLOAT = -3 , LWS_TOKZE_ERR_NUM_ON_LHS = -2 , LWS_TOKZE_ERR_COMMA_LIST = -1 ,
  LWS_TOKZE_ENDED = 0 , LWS_TOKZE_DELIMITER , LWS_TOKZE_TOKEN , LWS_TOKZE_INTEGER ,
  LWS_TOKZE_FLOAT , LWS_TOKZE_TOKEN_NAME_EQUALS , LWS_TOKZE_TOKEN_NAME_COLON , LWS_TOKZE_QUOTED_STRING
}
 
enum  lws_tokenize_delimiter_tracking { LWSTZ_DT_NEED_FIRST_CONTENT , LWSTZ_DT_NEED_DELIM , LWSTZ_DT_NEED_NEXT_CONTENT }
 
enum  lws_tokenize_state { LWS_TOKZS_LEADING_WHITESPACE , LWS_TOKZS_QUOTED_STRING , LWS_TOKZS_TOKEN , LWS_TOKZS_TOKEN_POST_TERMINAL }
 
enum  { LSTRX_DONE , LSTRX_FILLED_OUT , LSTRX_FATAL_NAME_TOO_LONG = -1 , LSTRX_FATAL_NAME_UNKNOWN = -2 }
 

Functions

LWS_VISIBLE LWS_EXTERN void lws_tokenize_init (struct lws_tokenize *ts, const char *start, int flags)
 
LWS_VISIBLE LWS_EXTERN lws_tokenize_elem lws_tokenize (struct lws_tokenize *ts)
 
LWS_VISIBLE LWS_EXTERN int lws_tokenize_cstr (struct lws_tokenize *ts, char *str, size_t max)
 
LWS_VISIBLE LWS_EXTERN void lws_strexp_init (lws_strexp_t *exp, void *priv, lws_strexp_expand_cb cb, char *out, size_t olen)
 
LWS_VISIBLE LWS_EXTERN void lws_strexp_reset_out (lws_strexp_t *exp, char *out, size_t olen)
 
LWS_VISIBLE LWS_EXTERN int lws_strexp_expand (lws_strexp_t *exp, const char *in, size_t len, size_t *pused_in, size_t *pused_out)
 
LWS_VISIBLE LWS_EXTERN int lws_strcmp_wildcard (const char *wildcard, size_t wlen, const char *check, size_t clen)
 

Data Structure Documentation

◆ lws_tokenize

struct lws_tokenize

Definition at line 101 of file lws-tokenize.h.

+ Collaboration diagram for lws_tokenize:
Data Fields
char collect[256]
const char * start

set to the start of the string to tokenize

const char * token

the start of an identified token or delimiter

size_t len

set to the length of the string to tokenize

size_t token_len

the length of the identied token or delimiter

lws_tokenize_state state
int line
int effline
uint16_t flags

optional LWS_TOKENIZE_F_ flags, or 0

uint8_t delim
int8_t e

convenient for storing lws_tokenize return

uint8_t reset_token:1
uint8_t crlf:1
uint8_t dry:1

◆ lws_strexp

struct lws_strexp

Definition at line 194 of file lws-tokenize.h.

+ Collaboration diagram for lws_strexp:
Data Fields
char name[32]
lws_strexp_expand_cb cb
void * priv
char * out
size_t olen
size_t pos
size_t exp_ofs
uint8_t name_pos
char state

Macro Definition Documentation

◆ LWS_TOKENIZE_F_MINUS_NONTERM

#define LWS_TOKENIZE_F_MINUS_NONTERM   (1 << 0)

Definition at line 26 of file lws-tokenize.h.

◆ LWS_TOKENIZE_F_AGG_COLON

#define LWS_TOKENIZE_F_AGG_COLON   (1 << 1)

Definition at line 28 of file lws-tokenize.h.

◆ LWS_TOKENIZE_F_COMMA_SEP_LIST

#define LWS_TOKENIZE_F_COMMA_SEP_LIST   (1 << 2)

Definition at line 30 of file lws-tokenize.h.

◆ LWS_TOKENIZE_F_RFC7230_DELIMS

#define LWS_TOKENIZE_F_RFC7230_DELIMS   (1 << 3)

Definition at line 33 of file lws-tokenize.h.

◆ LWS_TOKENIZE_F_DOT_NONTERM

#define LWS_TOKENIZE_F_DOT_NONTERM   (1 << 4)

Definition at line 35 of file lws-tokenize.h.

◆ LWS_TOKENIZE_F_NO_FLOATS

#define LWS_TOKENIZE_F_NO_FLOATS   (1 << 5)

Definition at line 39 of file lws-tokenize.h.

◆ LWS_TOKENIZE_F_NO_INTEGERS

#define LWS_TOKENIZE_F_NO_INTEGERS   (1 << 6)

Definition at line 41 of file lws-tokenize.h.

◆ LWS_TOKENIZE_F_HASH_COMMENT

#define LWS_TOKENIZE_F_HASH_COMMENT   (1 << 7)

Definition at line 43 of file lws-tokenize.h.

◆ LWS_TOKENIZE_F_SLASH_NONTERM

#define LWS_TOKENIZE_F_SLASH_NONTERM   (1 << 8)

Definition at line 45 of file lws-tokenize.h.

◆ LWS_TOKENIZE_F_ASTERISK_NONTERM

#define LWS_TOKENIZE_F_ASTERISK_NONTERM   (1 << 9)

Definition at line 47 of file lws-tokenize.h.

◆ LWS_TOKENIZE_F_EQUALS_NONTERM

#define LWS_TOKENIZE_F_EQUALS_NONTERM   (1 << 10)

Definition at line 49 of file lws-tokenize.h.

◆ LWS_TOKENIZE_F_COLON_NONTERM

#define LWS_TOKENIZE_F_COLON_NONTERM   (1 << 11)

Definition at line 51 of file lws-tokenize.h.

◆ LWS_TOKENIZE_F_EXPECT_MORE

#define LWS_TOKENIZE_F_EXPECT_MORE   (1 << 12)

Definition at line 54 of file lws-tokenize.h.

Typedef Documentation

◆ lws_tokenize_t

typedef struct lws_tokenize lws_tokenize_t

◆ lws_strexp_expand_cb

typedef int(* lws_strexp_expand_cb) (void *priv, const char *name, char *out, size_t *pos, size_t olen, size_t *exp_ofs)

Definition at line 191 of file lws-tokenize.h.

◆ lws_strexp_t

typedef struct lws_strexp lws_strexp_t

Enumeration Type Documentation

◆ lws_tokenize_elem

Enumerator
LWS_TOKZE_ERRS 
LWS_TOKZE_TOO_LONG 
LWS_TOKZE_WANT_READ 
LWS_TOKZE_ERR_BROKEN_UTF8 
LWS_TOKZE_ERR_UNTERM_STRING 
LWS_TOKZE_ERR_MALFORMED_FLOAT 
LWS_TOKZE_ERR_NUM_ON_LHS 
LWS_TOKZE_ERR_COMMA_LIST 
LWS_TOKZE_ENDED 
LWS_TOKZE_DELIMITER 
LWS_TOKZE_TOKEN 
LWS_TOKZE_INTEGER 
LWS_TOKZE_FLOAT 
LWS_TOKZE_TOKEN_NAME_EQUALS 
LWS_TOKZE_TOKEN_NAME_COLON 
LWS_TOKZE_QUOTED_STRING 

Definition at line 56 of file lws-tokenize.h.

56  {
57 
58  LWS_TOKZE_ERRS = 7, /* the number of errors defined */
59 
60  LWS_TOKZE_TOO_LONG = -7, /* token too long */
61  LWS_TOKZE_WANT_READ = -6, /* need more input */
62  LWS_TOKZE_ERR_BROKEN_UTF8 = -5, /* malformed or partial utf8 */
63  LWS_TOKZE_ERR_UNTERM_STRING = -4, /* ended while we were in "" */
64  LWS_TOKZE_ERR_MALFORMED_FLOAT = -3, /* like 0..1 or 0.1.1 */
65  LWS_TOKZE_ERR_NUM_ON_LHS = -2, /* like 123= or 0.1= */
66  LWS_TOKZE_ERR_COMMA_LIST = -1, /* like ",tok", or, "tok,," */
67 
68  LWS_TOKZE_ENDED = 0, /* no more content */
69 
70  /* Note: results have ordinal 1+, EOT is 0 and errors are < 0 */
71 
72  LWS_TOKZE_DELIMITER, /* a delimiter appeared */
73  LWS_TOKZE_TOKEN, /* a token appeared */
74  LWS_TOKZE_INTEGER, /* an integer appeared */
75  LWS_TOKZE_FLOAT, /* a float appeared */
76  LWS_TOKZE_TOKEN_NAME_EQUALS, /* token [whitespace] = */
77  LWS_TOKZE_TOKEN_NAME_COLON, /* token [whitespace] : (only with
78  LWS_TOKENIZE_F_AGG_COLON flag) */
79  LWS_TOKZE_QUOTED_STRING, /* "*", where * may have any char */
80 
lws_tokenize_elem
Definition: lws-tokenize.h:56
@ LWS_TOKZE_ERR_NUM_ON_LHS
Definition: lws-tokenize.h:65
@ LWS_TOKZE_WANT_READ
Definition: lws-tokenize.h:61
@ LWS_TOKZE_TOKEN
Definition: lws-tokenize.h:73
@ LWS_TOKZE_QUOTED_STRING
Definition: lws-tokenize.h:79
@ LWS_TOKZE_ERR_MALFORMED_FLOAT
Definition: lws-tokenize.h:64
@ LWS_TOKZE_DELIMITER
Definition: lws-tokenize.h:72
@ LWS_TOKZE_TOKEN_NAME_EQUALS
Definition: lws-tokenize.h:76
@ LWS_TOKZE_TOO_LONG
Definition: lws-tokenize.h:60
@ LWS_TOKZE_FLOAT
Definition: lws-tokenize.h:75
@ LWS_TOKZE_ERR_BROKEN_UTF8
Definition: lws-tokenize.h:62
@ LWS_TOKZE_ERRS
Definition: lws-tokenize.h:58
@ LWS_TOKZE_INTEGER
Definition: lws-tokenize.h:74
@ LWS_TOKZE_ENDED
Definition: lws-tokenize.h:68
@ LWS_TOKZE_TOKEN_NAME_COLON
Definition: lws-tokenize.h:77
@ LWS_TOKZE_ERR_UNTERM_STRING
Definition: lws-tokenize.h:63
@ LWS_TOKZE_ERR_COMMA_LIST
Definition: lws-tokenize.h:66

◆ lws_tokenize_delimiter_tracking

Enumerator
LWSTZ_DT_NEED_FIRST_CONTENT 
LWSTZ_DT_NEED_DELIM 
LWSTZ_DT_NEED_NEXT_CONTENT 

Definition at line 88 of file lws-tokenize.h.

88  {
92 };
@ LWSTZ_DT_NEED_NEXT_CONTENT
Definition: lws-tokenize.h:91
@ LWSTZ_DT_NEED_DELIM
Definition: lws-tokenize.h:90
@ LWSTZ_DT_NEED_FIRST_CONTENT
Definition: lws-tokenize.h:89

◆ lws_tokenize_state

Enumerator
LWS_TOKZS_LEADING_WHITESPACE 
LWS_TOKZS_QUOTED_STRING 
LWS_TOKZS_TOKEN 
LWS_TOKZS_TOKEN_POST_TERMINAL 

Definition at line 94 of file lws-tokenize.h.

94  {
lws_tokenize_state
Definition: lws-tokenize.h:94
@ LWS_TOKZS_TOKEN
Definition: lws-tokenize.h:97
@ LWS_TOKZS_TOKEN_POST_TERMINAL
Definition: lws-tokenize.h:98
@ LWS_TOKZS_QUOTED_STRING
Definition: lws-tokenize.h:96
@ LWS_TOKZS_LEADING_WHITESPACE
Definition: lws-tokenize.h:95

◆ anonymous enum

anonymous enum
Enumerator
LSTRX_DONE 
LSTRX_FILLED_OUT 
LSTRX_FATAL_NAME_TOO_LONG 
LSTRX_FATAL_NAME_UNKNOWN 

Definition at line 208 of file lws-tokenize.h.

208  {
209  LSTRX_DONE, /* it completed OK */
210  LSTRX_FILLED_OUT, /* out buf filled and needs resetting */
211  LSTRX_FATAL_NAME_TOO_LONG = -1, /* fatal */
213 };
@ LSTRX_DONE
Definition: lws-tokenize.h:209
@ LSTRX_FATAL_NAME_TOO_LONG
Definition: lws-tokenize.h:211
@ LSTRX_FATAL_NAME_UNKNOWN
Definition: lws-tokenize.h:212
@ LSTRX_FILLED_OUT
Definition: lws-tokenize.h:210

Function Documentation

◆ lws_tokenize_init()

LWS_VISIBLE LWS_EXTERN void lws_tokenize_init ( struct lws_tokenize ts,
const char *  start,
int  flags 
)

lws_tokenize() - breaks down a string into tokens and delimiters in-place

Parameters
tsthe lws_tokenize struct to init
startthe string to tokenize
flagsLWS_TOKENIZE_F_ option flags

This initializes the tokenize struct to point to the given string, and sets the length to 2GiB - 1 (so there must be a terminating NUL)... you can override this requirement by setting ts.len yourself before using it.

.delim is also initialized to LWSTZ_DT_NEED_FIRST_CONTENT.

◆ lws_tokenize()

lws_tokenize() - breaks down a string into tokens and delimiters in-place

Parameters
tsthe lws_tokenize struct with information and state on what to do

The ts struct should have its start, len and flags members initialized to reflect the string to be tokenized and any options.

Then lws_tokenize() may be called repeatedly on the struct, returning one of lws_tokenize_elem each time, and with the struct's token and token_len members set to describe the content of the delimiter or token payload each time.

There are no allocations during the process.

returns lws_tokenize_elem that was identified (LWS_TOKZE_ENDED means reached the end of the string).

◆ lws_tokenize_cstr()

LWS_VISIBLE LWS_EXTERN int lws_tokenize_cstr ( struct lws_tokenize ts,
char *  str,
size_t  max 
)

lws_tokenize_cstr() - copy token string to NUL-terminated buffer

Parameters
tspointer to lws_tokenize struct to operate on
strdestination buffer \pparam max: bytes in destination buffer

returns 0 if OK or nonzero if the string + NUL won't fit.

◆ lws_strexp_init()

LWS_VISIBLE LWS_EXTERN void lws_strexp_init ( lws_strexp_t exp,
void *  priv,
lws_strexp_expand_cb  cb,
char *  out,
size_t  olen 
)

lws_strexp_init() - initialize an lws_strexp_t for use

exp: the exp object to init priv: the user's object pointer to pass to callback cb: the callback to expand named objects out: the start of the output buffer, or NULL just to get the length olen: the length of the output buffer in bytes

Prepares an lws_strexp_t for use and sets the initial output buffer

If out is NULL, substitution proceeds normally, but no output is produced, only the length is returned. olen should be set to the largest feasible overall length. To use this mode, the substitution callback must also check for NULL out and avoid producing the output.

◆ lws_strexp_reset_out()

LWS_VISIBLE LWS_EXTERN void lws_strexp_reset_out ( lws_strexp_t exp,
char *  out,
size_t  olen 
)

lws_strexp_reset_out() - reset the output buffer on an existing strexp

exp: the exp object to init out: the start of the output buffer, or NULL to just get length olen: the length of the output buffer in bytes

Provides a new output buffer for lws_strexp_expand() to continue to write into. It can be the same as the old one if it has been copied out or used. The position of the next write will be reset to the start of the given buf.

If out is NULL, substitution proceeds normally, but no output is produced, only the length is returned. olen should be set to the largest feasible overall length. To use this mode, the substitution callback must also check for NULL out and avoid producing the output.

◆ lws_strexp_expand()

LWS_VISIBLE LWS_EXTERN int lws_strexp_expand ( lws_strexp_t exp,
const char *  in,
size_t  len,
size_t *  pused_in,
size_t *  pused_out 
)

lws_strexp_expand() - copy / expand a string into the output buffer

exp: the exp object for the copy / expansion in: the start of the next input data len: the length of the input data pused_in: pointer to write the amount of input used pused_out: pointer to write the amount of output used

Copies in to the output buffer set in exp, expanding any ${name} tokens using the callback. *pused_in is set to the number of input chars used and *pused_out the number of output characters used

May return LSTRX_FILLED_OUT early with *pused < len if the output buffer is filled. Handle the output buffer and reset it with lws_strexp_reset_out() before calling again with adjusted in / len to continue.

In the case of large expansions, the expansion itself may fill the output buffer, in which case the expansion callback returns the LSTRX_FILLED_OUT and will be called again to continue with its *exp_ofs parameter set appropriately.

◆ lws_strcmp_wildcard()

LWS_VISIBLE LWS_EXTERN int lws_strcmp_wildcard ( const char *  wildcard,
size_t  wlen,
const char *  check,
size_t  clen 
)

lws_strcmp_wildcard() - strcmp but the first arg can have wildcards

wildcard: a string that may contain zero to three *, and may lack a NUL wlen: length of the wildcard string check: string to test to see if it matches wildcard clen: length of check string

Like strcmp, but supports patterns like "a*", "a*b", "a*b*" etc where a and b are arbitrary substrings. Both the wc and check strings need not be NUL terminated, but are specified by lengths.