2011-04-20 12:52:40 +00:00
|
|
|
/**
|
2011-04-19 20:27:38 +00:00
|
|
|
* @file psyc/parser.h
|
2011-04-20 12:52:40 +00:00
|
|
|
* @brief Interface for various psyc parser functions.
|
2011-04-19 20:27:38 +00:00
|
|
|
*
|
2011-04-20 12:52:40 +00:00
|
|
|
* All parsing functions and the definitions they use are
|
2011-04-19 20:27:38 +00:00
|
|
|
* defined in this file.
|
|
|
|
*/
|
|
|
|
|
2011-04-20 12:52:40 +00:00
|
|
|
/**
|
|
|
|
* @defgroup parsing Parsing Functions
|
|
|
|
*
|
|
|
|
* This module contains all parsing functions.
|
|
|
|
* @{
|
2011-04-19 20:57:49 +00:00
|
|
|
*/
|
|
|
|
|
2011-04-19 20:01:51 +00:00
|
|
|
#ifndef PSYC_PARSER_H
|
|
|
|
# define PSYC_PARSER_H
|
|
|
|
|
2010-02-20 16:40:09 +00:00
|
|
|
#include <stdint.h>
|
2011-04-15 23:42:36 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2011-04-20 14:28:15 +00:00
|
|
|
typedef enum
|
2011-04-17 10:56:24 +00:00
|
|
|
{
|
2011-04-22 15:09:32 +00:00
|
|
|
PSYC_PARSE_HEADER_ONLY = 1,
|
2011-04-21 12:20:24 +00:00
|
|
|
} PSYC_ParseFlag;
|
2011-04-17 10:56:24 +00:00
|
|
|
|
2011-04-20 12:52:40 +00:00
|
|
|
/**
|
|
|
|
* The return value definitions for the packet parsing function.
|
|
|
|
* @see PSYC_parse()
|
2011-04-19 20:57:49 +00:00
|
|
|
*/
|
2011-04-20 14:28:15 +00:00
|
|
|
typedef enum
|
2011-04-17 10:05:14 +00:00
|
|
|
{
|
2011-04-22 15:09:32 +00:00
|
|
|
PSYC_PARSE_ERROR_END = -7,
|
|
|
|
PSYC_PARSE_ERROR_METHOD = -6,
|
|
|
|
PSYC_PARSE_ERROR_VAR_LEN = -5,
|
|
|
|
PSYC_PARSE_ERROR_VAR_TAB = -4,
|
|
|
|
PSYC_PARSE_ERROR_VAR_NAME = -3,
|
|
|
|
PSYC_PARSE_ERROR_LENGTH = -2,
|
|
|
|
PSYC_PARSE_ERROR = -1,
|
|
|
|
PSYC_PARSE_SUCCESS = 0,
|
2011-04-20 17:18:35 +00:00
|
|
|
/// Buffer contains insufficient amount of data.
|
|
|
|
/// Fill another buffer and concatenate it with the end of the current buffer,
|
|
|
|
/// from the cursor position to the end.
|
2011-04-22 15:09:32 +00:00
|
|
|
PSYC_PARSE_INSUFFICIENT = 1,
|
2011-04-20 17:18:35 +00:00
|
|
|
/// Routing variable parsing done.
|
2011-04-22 18:50:59 +00:00
|
|
|
/// Operator, name & value contains the respective parts.
|
2011-04-22 15:09:32 +00:00
|
|
|
PSYC_PARSE_ROUTING = 2,
|
2011-04-20 17:18:35 +00:00
|
|
|
/// Entity variable parsing done.
|
2011-04-22 18:50:59 +00:00
|
|
|
/// Operator, name & value contains the respective parts.
|
2011-04-22 15:09:32 +00:00
|
|
|
PSYC_PARSE_ENTITY = 3,
|
2011-04-20 17:18:35 +00:00
|
|
|
/// Entity variable parsing is incomplete.
|
2011-04-22 18:50:59 +00:00
|
|
|
/// Operator & name are complete, value is incomplete.
|
2011-04-22 15:09:32 +00:00
|
|
|
PSYC_PARSE_ENTITY_INCOMPLETE = 4,
|
2011-04-20 14:28:15 +00:00
|
|
|
/// Body parsing done, name contains method, value contains body.
|
2011-04-22 15:09:32 +00:00
|
|
|
PSYC_PARSE_BODY = 5,
|
2011-04-20 14:28:15 +00:00
|
|
|
/// Body parsing is incomplete, name contains method, value contains part of the body.
|
2011-04-22 15:09:32 +00:00
|
|
|
PSYC_PARSE_BODY_INCOMPLETE = 6,
|
2011-04-20 14:28:15 +00:00
|
|
|
/// Reached end of packet, parsing done.
|
2011-04-22 15:09:32 +00:00
|
|
|
PSYC_PARSE_COMPLETE = 7,
|
2011-04-20 17:18:35 +00:00
|
|
|
/// Binary value parsing incomplete, used internally.
|
2011-04-22 15:09:32 +00:00
|
|
|
PSYC_PARSE_INCOMPLETE = 8,
|
2011-04-20 20:31:04 +00:00
|
|
|
} PSYC_ParseRC;
|
2011-04-19 07:21:00 +00:00
|
|
|
|
2011-04-20 12:52:40 +00:00
|
|
|
/**
|
|
|
|
* The return value definitions for the list parsing function.
|
|
|
|
* @see PSYC_parseList()
|
|
|
|
*/
|
2011-04-20 14:28:15 +00:00
|
|
|
typedef enum
|
2011-04-19 17:41:25 +00:00
|
|
|
{
|
2011-04-22 15:09:32 +00:00
|
|
|
PSYC_PARSE_LIST_ERROR_DELIM = -5,
|
|
|
|
PSYC_PARSE_LIST_ERROR_LEN = -4,
|
|
|
|
PSYC_PARSE_LIST_ERROR_TYPE = -3,
|
|
|
|
PSYC_PARSE_LIST_ERROR_NAME = -2,
|
|
|
|
PSYC_PARSE_LIST_ERROR= -1,
|
2011-04-20 17:18:35 +00:00
|
|
|
/// Completed parsing a list element.
|
2011-04-22 15:09:32 +00:00
|
|
|
PSYC_PARSE_LIST_ELEM = 1,
|
2011-04-20 17:18:35 +00:00
|
|
|
/// Reached end of buffer.
|
2011-04-22 15:09:32 +00:00
|
|
|
PSYC_PARSE_LIST_END = 2,
|
2011-04-20 17:18:35 +00:00
|
|
|
/// Binary list is incomplete.
|
2011-04-22 15:09:32 +00:00
|
|
|
PSYC_PARSE_LIST_INCOMPLETE = 3,
|
2011-04-21 12:20:24 +00:00
|
|
|
} PSYC_ParseListRC;
|
2011-04-15 23:42:36 +00:00
|
|
|
|
2011-04-20 12:52:40 +00:00
|
|
|
/**
|
|
|
|
* Struct for keeping parser state.
|
|
|
|
*/
|
2011-04-19 17:41:25 +00:00
|
|
|
typedef struct
|
2011-04-15 23:42:36 +00:00
|
|
|
{
|
2011-04-19 19:54:44 +00:00
|
|
|
size_t cursor; ///< current position in buffer
|
2011-04-20 17:18:35 +00:00
|
|
|
size_t startc; ///< position where the parsing would be resumed
|
2011-04-21 12:20:24 +00:00
|
|
|
PSYC_Array buffer; ///< buffer with data to be parsed
|
|
|
|
uint8_t flags; ///< flags for the parser, see PSYC_ParseFlag
|
2011-04-20 14:28:15 +00:00
|
|
|
PSYC_Part part; ///< part of the packet being parsed currently
|
2011-04-15 23:42:36 +00:00
|
|
|
|
2011-04-19 19:54:44 +00:00
|
|
|
size_t contentParsed; ///< number of bytes parsed from the content so far
|
|
|
|
size_t contentLength; ///< expected length of the content
|
2011-04-20 17:18:35 +00:00
|
|
|
PSYC_Bool contentLengthFound; ///< is there a length given for this packet?
|
2011-04-19 19:54:44 +00:00
|
|
|
size_t valueParsed; ///< number of bytes parsed from the value so far
|
|
|
|
size_t valueLength; ///< expected length of the value
|
2011-04-21 12:20:24 +00:00
|
|
|
} PSYC_ParseState;
|
2011-04-15 23:42:36 +00:00
|
|
|
|
2011-04-20 12:52:40 +00:00
|
|
|
/**
|
|
|
|
* Struct for keeping list parser state.
|
|
|
|
*/
|
2011-04-19 17:41:25 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
2011-04-19 19:54:44 +00:00
|
|
|
size_t cursor; ///< current position in buffer
|
|
|
|
size_t startc; ///< line start position
|
2011-04-19 17:41:25 +00:00
|
|
|
PSYC_Array buffer;
|
2011-04-20 14:28:15 +00:00
|
|
|
PSYC_ListType type; ///< list type
|
2011-04-19 17:41:25 +00:00
|
|
|
|
2011-04-19 19:54:44 +00:00
|
|
|
size_t elemParsed; ///< number of bytes parsed from the elem so far
|
|
|
|
size_t elemLength; ///< expected length of the elem
|
2011-04-21 12:20:24 +00:00
|
|
|
} PSYC_ParseListState;
|
2011-04-19 17:41:25 +00:00
|
|
|
|
2011-04-19 19:54:44 +00:00
|
|
|
/**
|
|
|
|
* Shortcut for creating an array.
|
2011-04-17 12:47:25 +00:00
|
|
|
*
|
2011-04-19 17:41:25 +00:00
|
|
|
* @param memory Pointer to the buffer.
|
|
|
|
* @param length Length of that buffer.
|
2011-04-17 12:47:25 +00:00
|
|
|
*
|
2011-04-19 19:54:44 +00:00
|
|
|
* @return An instance of the PSYC_Array struct.
|
|
|
|
*/
|
2011-04-22 18:33:22 +00:00
|
|
|
inline PSYC_Array PSYC_createArray (const char* memory, size_t length);
|
2011-04-18 08:09:35 +00:00
|
|
|
|
2011-04-19 19:54:44 +00:00
|
|
|
/**
|
2011-04-22 18:33:22 +00:00
|
|
|
* Initiates the state struct.
|
2011-04-17 12:47:25 +00:00
|
|
|
*
|
2011-04-19 17:41:25 +00:00
|
|
|
* @param state Pointer to the state struct that should be initiated.
|
|
|
|
*/
|
2011-04-22 18:33:22 +00:00
|
|
|
inline void PSYC_initParseState (PSYC_ParseState* state);
|
2011-04-17 10:56:24 +00:00
|
|
|
|
2011-04-19 19:54:44 +00:00
|
|
|
/**
|
2011-04-22 18:33:22 +00:00
|
|
|
* Initiates the state struct with flags.
|
2011-04-18 08:09:35 +00:00
|
|
|
*
|
2011-04-19 17:41:25 +00:00
|
|
|
* @param state Pointer to the state struct that should be initiated.
|
2011-04-22 18:33:22 +00:00
|
|
|
* @param flags Flags to be set for the parser, see PSYC_ParseFlag.
|
2011-04-19 17:41:25 +00:00
|
|
|
*/
|
2011-04-22 18:33:22 +00:00
|
|
|
inline void PSYC_initParseState2 (PSYC_ParseState* state, uint8_t flags);
|
2011-04-15 23:42:36 +00:00
|
|
|
|
2011-04-19 19:54:44 +00:00
|
|
|
/**
|
|
|
|
* Initiates the list state struct.
|
2011-04-19 17:41:25 +00:00
|
|
|
*
|
|
|
|
* @param state Pointer to the list state struct that should be initiated.
|
|
|
|
*/
|
2011-04-22 18:33:22 +00:00
|
|
|
inline void PSYC_initParseListState (PSYC_ParseListState* state);
|
2011-04-19 17:41:25 +00:00
|
|
|
|
2011-04-22 18:33:22 +00:00
|
|
|
inline void PSYC_nextParseBuffer (PSYC_ParseState* state, PSYC_Array newBuf);
|
2011-04-19 17:41:25 +00:00
|
|
|
|
2011-04-22 18:33:22 +00:00
|
|
|
inline void PSYC_nextParseListBuffer (PSYC_ParseListState* state, PSYC_Array newBuf);
|
2011-04-15 23:42:36 +00:00
|
|
|
|
2011-04-22 18:33:22 +00:00
|
|
|
inline size_t PSYC_getContentLength (PSYC_ParseState* s);
|
2011-04-18 08:09:35 +00:00
|
|
|
|
2011-04-20 12:52:40 +00:00
|
|
|
/**
|
|
|
|
* Parse PSYC packets.
|
|
|
|
*
|
2011-04-19 20:57:49 +00:00
|
|
|
* Generalized line-based packet parser.
|
2011-04-20 12:52:40 +00:00
|
|
|
*
|
2011-04-21 12:20:24 +00:00
|
|
|
* @param state An initialized PSYC_ParseState
|
2011-04-22 18:50:59 +00:00
|
|
|
* @param operator A pointer to a character. In case of a variable, it will
|
|
|
|
* be set to the operator of that variable
|
2011-04-19 20:57:49 +00:00
|
|
|
* @param name A pointer to a PSYC_Array. It will point to the name of
|
|
|
|
* the variable or method and its length will be set accordingly
|
|
|
|
* @param value A pointer to a PSYC_Array. It will point to the
|
|
|
|
* value/body the variable/method and its length will be set accordingly
|
2011-04-19 20:31:43 +00:00
|
|
|
*/
|
2011-04-22 20:59:15 +00:00
|
|
|
PSYC_ParseRC PSYC_parse(PSYC_ParseState* state, char* oper, PSYC_Array* name, PSYC_Array* value);
|
2011-04-19 17:41:25 +00:00
|
|
|
|
2011-04-19 20:31:43 +00:00
|
|
|
/**
|
|
|
|
* List value parser.
|
|
|
|
*/
|
2011-04-21 12:20:24 +00:00
|
|
|
PSYC_ParseListRC PSYC_parseList(PSYC_ParseListState* state, PSYC_Array *name, PSYC_Array* value, PSYC_Array* elem);
|
2011-04-19 19:55:22 +00:00
|
|
|
|
2011-04-19 20:01:51 +00:00
|
|
|
#endif // PSYC_PARSER_H
|
2011-04-19 20:57:49 +00:00
|
|
|
|
|
|
|
/** @} */ // end of parsing group
|