1
0
Fork 0
mirror of git://git.psyc.eu/libpsyc synced 2024-08-15 03:19:02 +00:00
libpsyc/include/psyc/parser.h

112 lines
2.5 KiB
C
Raw Normal View History

2010-02-20 16:40:09 +00:00
#include <stdint.h>
2011-04-15 23:42:36 +00:00
#include <string.h>
2011-04-17 10:56:24 +00:00
enum PSYC_Flags
{
PSYC_HEADER_ONLY = 1
};
enum PSYC_ReturnCodes
2011-04-17 10:05:14 +00:00
{
PSYC_ERROR_END = -7,
PSYC_ERROR_METHOD = -6,
PSYC_ERROR_VAR_LEN = -5,
PSYC_ERROR_VAR_TAB = -4,
PSYC_ERROR_VAR_NAME = -3,
PSYC_ERROR_LENGTH = -2,
PSYC_ERROR = -1,
PSYC_SUCCESS = 0,
2011-04-18 08:09:35 +00:00
PSYC_BODY = 1,
PSYC_BODY_INCOMPLETE = 2,
PSYC_INSUFFICIENT = 3,
PSYC_ROUTING = 4,
PSYC_ENTITY = 5,
PSYC_ENTITY_INCOMPLETE = 6,
PSYC_HEADER_COMPLETE = 7,
PSYC_COMPLETE = 8,
PSYC_INCOMPLETE = 9,
};
enum PSYC_Parts
{
PSYC_PART_RESET = -1,
PSYC_PART_HEADER = 0,
PSYC_PART_LENGTH,
PSYC_PART_HEADER_END,
PSYC_PART_CONTENT,
PSYC_PART_METHOD,
PSYC_PART_DATA,
PSYC_PART_END,
2011-04-17 10:05:14 +00:00
};
2011-04-15 23:42:36 +00:00
typedef struct
{
unsigned int length;
const uint8_t * ptr;
2011-04-15 23:42:36 +00:00
} PSYC_Array;
2011-04-18 08:09:35 +00:00
2011-04-15 23:42:36 +00:00
typedef struct
{
unsigned int cursor; /** current position in buffer */
unsigned int startc; /** line start position */
2011-04-15 23:42:36 +00:00
PSYC_Array buffer;
uint8_t flags;
char part; /** part of the packet being parsed currently, see PSYC_Parts */
2011-04-15 23:42:36 +00:00
unsigned int contentParsed; /** number of bytes parsed from the content so far */
unsigned int contentLength; /** expected length of the content */
char contentLengthFound; /** is there a length given for this packet? */
unsigned int valueParsed; /** number of bytes parsef from the value so far */
unsigned int valueLength; /** expected length of the value */
2011-04-15 23:42:36 +00:00
} PSYC_State;
#ifndef PSYC_COMPILE_LIBRARY
2011-04-17 12:47:25 +00:00
/* @brief shortcut for creating an array
*
* @param memory pointer to the buffer
* @param length length of that buffer
*
* @returns an instance of the PSYC_Array struct */
2011-04-18 08:09:35 +00:00
inline PSYC_Array PSYC_createArray (uint8_t* const memory, unsigned int length)
2011-04-15 23:42:36 +00:00
{
PSYC_Array arr = {length, memory};
return arr;
}
2011-04-18 08:09:35 +00:00
2011-04-17 12:47:25 +00:00
/* @brief initiates the state struct with flags
*
* @param state pointer to the state struct that should be initiated
2011-04-18 08:09:35 +00:00
* @param flags the flags that one ones to set, see PSYC_Flags */
2011-04-17 10:56:24 +00:00
inline void PSYC_initState2 (PSYC_State* state, uint8_t flags )
{
memset(state, 0, sizeof(PSYC_State));
state->flags = flags;
}
2011-04-18 08:09:35 +00:00
/* @brief initiates the state struct
*
* @param state pointer to the state struct that should be initiated */
2011-04-15 23:42:36 +00:00
inline void PSYC_initState (PSYC_State* state)
{
memset(state, 0, sizeof(PSYC_State));
}
inline void PSYC_nextBuffer (PSYC_State* state, PSYC_Array newBuf)
{
state->buffer = newBuf;
2011-04-18 08:09:35 +00:00
state->cursor = 0;
2011-04-15 23:42:36 +00:00
}
2011-04-18 08:09:35 +00:00
inline unsigned int PSYC_getContentLength (PSYC_State* s)
2011-04-15 23:42:36 +00:00
{
2011-04-18 08:09:35 +00:00
return s->contentLength;
2011-04-15 23:42:36 +00:00
}
2011-04-18 08:09:35 +00:00
#endif
2011-04-15 23:42:36 +00:00
int PSYC_parse(PSYC_State* state, uint8_t* modifier, PSYC_Array* name, PSYC_Array* value);