1
0
Fork 0
mirror of git://git.psyc.eu/libpsyc synced 2024-08-15 03:19:02 +00:00
This commit is contained in:
tg(x) 2011-11-11 22:18:24 +01:00
parent aee9203df6
commit 1cc58abd0e
31 changed files with 2797 additions and 2759 deletions

View file

@ -1,4 +1,5 @@
#ifndef PSYC_PACKET_H
#define PSYC_PACKET_H
/**
* @file psyc/packet.h
@ -20,185 +21,182 @@
#include <math.h>
/** Modifier flags. */
typedef enum
{
/// Modifier needs to be checked if it needs length.
PSYC_MODIFIER_CHECK_LENGTH = 0,
/// Modifier needs length.
PSYC_MODIFIER_NEED_LENGTH = 1,
/// Modifier doesn't need length.
PSYC_MODIFIER_NO_LENGTH = 2,
/// Routing modifier, which implies that it doesn't need length.
PSYC_MODIFIER_ROUTING = 3,
typedef enum {
/// Modifier needs to be checked if it needs length.
PSYC_MODIFIER_CHECK_LENGTH = 0,
/// Modifier needs length.
PSYC_MODIFIER_NEED_LENGTH = 1,
/// Modifier doesn't need length.
PSYC_MODIFIER_NO_LENGTH = 2,
/// Routing modifier, which implies that it doesn't need length.
PSYC_MODIFIER_ROUTING = 3,
} PsycModifierFlag;
/** List flags. */
typedef enum
{
/// List needs to be checked if it needs length.
PSYC_LIST_CHECK_LENGTH = 0,
/// List needs length.
PSYC_LIST_NEED_LENGTH = 1,
/// List doesn't need length.
PSYC_LIST_NO_LENGTH = 2,
typedef enum {
/// List needs to be checked if it needs length.
PSYC_LIST_CHECK_LENGTH = 0,
/// List needs length.
PSYC_LIST_NEED_LENGTH = 1,
/// List doesn't need length.
PSYC_LIST_NO_LENGTH = 2,
} PsycListFlag;
/** Packet flags. */
typedef enum
{
/// Packet needs to be checked if it needs content length.
PSYC_PACKET_CHECK_LENGTH = 0,
/// Packet needs content length.
PSYC_PACKET_NEED_LENGTH = 1,
/// Packet doesn't need content length.
PSYC_PACKET_NO_LENGTH = 2,
typedef enum {
/// Packet needs to be checked if it needs content length.
PSYC_PACKET_CHECK_LENGTH = 0,
/// Packet needs content length.
PSYC_PACKET_NEED_LENGTH = 1,
/// Packet doesn't need content length.
PSYC_PACKET_NO_LENGTH = 2,
} PsycPacketFlag;
typedef enum
{
PSYC_OPERATOR_SET = ':',
PSYC_OPERATOR_ASSIGN = '=',
PSYC_OPERATOR_AUGMENT = '+',
PSYC_OPERATOR_DIMINISH = '-',
PSYC_OPERATOR_QUERY = '?',
typedef enum {
PSYC_OPERATOR_SET = ':',
PSYC_OPERATOR_ASSIGN = '=',
PSYC_OPERATOR_AUGMENT = '+',
PSYC_OPERATOR_DIMINISH = '-',
PSYC_OPERATOR_QUERY = '?',
} PsycOperator;
typedef enum
{
PSYC_STATE_NOOP = 0,
PSYC_STATE_RESET = '=',
PSYC_STATE_RESYNC = '?',
typedef enum {
PSYC_STATE_NOOP = 0,
PSYC_STATE_RESET = '=',
PSYC_STATE_RESYNC = '?',
} PsycStateOp;
/** Structure for a modifier. */
typedef struct
{
char oper;
PsycString name;
PsycString value;
PsycModifierFlag flag;
typedef struct {
char oper;
PsycString name;
PsycString value;
PsycModifierFlag flag;
} PsycModifier;
/** Structure for an entity or routing header. */
typedef struct
{
size_t lines;
PsycModifier *modifiers;
typedef struct {
size_t lines;
PsycModifier *modifiers;
} PsycHeader;
/** Structure for a list. */
typedef struct
{
size_t num_elems;
PsycString *elems;
size_t length;
PsycListFlag flag;
typedef struct {
size_t num_elems;
PsycString *elems;
size_t length;
PsycListFlag flag;
} PsycList;
/** Intermediate struct for a PSYC packet */
typedef struct
{
PsycHeader routing; ///< Routing header.
PsycHeader entity; ///< Entity header.
char stateop; ///< State operation. @see PsycStateOp
PsycString method; ///< Contains the method.
PsycString data; ///< Contains the data.
PsycString content; ///< Contains the whole content.
size_t routingLength; ///< Length of routing part.
size_t contentLength; ///< Length of content part.
size_t length; ///< Total length of packet.
PsycPacketFlag flag; ///< Packet flag.
typedef struct {
PsycHeader routing; ///< Routing header.
PsycHeader entity; ///< Entity header.
char stateop; ///< State operation. @see PsycStateOp
PsycString method; ///< Contains the method.
PsycString data; ///< Contains the data.
PsycString content; ///< Contains the whole content.
size_t routingLength; ///< Length of routing part.
size_t contentLength; ///< Length of content part.
size_t length; ///< Total length of packet.
PsycPacketFlag flag; ///< Packet flag.
} PsycPacket;
/**
* Return the number of digits a number has in its base 10 representation.
*/
static inline
size_t psyc_num_length (size_t n)
static inline size_t
psyc_num_length (size_t n)
{
return n < 10 ? 1 : log10(n) + 1;
return n < 10 ? 1 : log10(n) + 1;
}
/**
* \internal
* Check if a modifier needs length.
*/
static inline
PsycModifierFlag psyc_modifier_length_check (PsycModifier *m)
static inline PsycModifierFlag
psyc_modifier_length_check (PsycModifier *m)
{
PsycModifierFlag flag;
PsycModifierFlag flag;
if (m->value.length > PSYC_MODIFIER_SIZE_THRESHOLD)
flag = PSYC_MODIFIER_NEED_LENGTH;
else if (memchr(m->value.data, (int)'\n', m->value.length))
flag = PSYC_MODIFIER_NEED_LENGTH;
else
flag = PSYC_MODIFIER_NO_LENGTH;
if (m->value.length > PSYC_MODIFIER_SIZE_THRESHOLD)
flag = PSYC_MODIFIER_NEED_LENGTH;
else if (memchr(m->value.data, (int) '\n', m->value.length))
flag = PSYC_MODIFIER_NEED_LENGTH;
else
flag = PSYC_MODIFIER_NO_LENGTH;
return flag;
return flag;
}
/** Initialize modifier */
static inline
void psyc_modifier_init (PsycModifier *m, char oper,
char *name, size_t namelen,
char *value, size_t valuelen,
PsycModifierFlag flag)
static inline void
psyc_modifier_init (PsycModifier *m, char oper,
char *name, size_t namelen,
char *value, size_t valuelen, PsycModifierFlag flag)
{
*m = (PsycModifier) {oper, {namelen, name}, {valuelen, value}, flag};
*m = (PsycModifier) {oper, {namelen, name}, {valuelen, value}, flag};
if (flag == PSYC_MODIFIER_CHECK_LENGTH) // find out if it needs a length
m->flag = psyc_modifier_length_check(m);
if (flag == PSYC_MODIFIER_CHECK_LENGTH) // find out if it needs a length
m->flag = psyc_modifier_length_check(m);
}
/**
* \internal
* Get the total length of a modifier when rendered.
*/
size_t psyc_modifier_length (PsycModifier *m);
size_t
psyc_modifier_length (PsycModifier *m);
/**
* \internal
* Check if a list needs length.
*/
PsycListFlag psyc_list_length_check (PsycList *list);
PsycListFlag
psyc_list_length_check (PsycList *list);
/**
* \internal
* Get the total length of a list when rendered.
*/
PsycListFlag psyc_list_length (PsycList *list);
PsycListFlag
psyc_list_length (PsycList *list);
/**
* \internal
* Check if a packet needs length.
*/
PsycPacketFlag psyc_packet_length_check (PsycPacket *p);
PsycPacketFlag
psyc_packet_length_check (PsycPacket *p);
/**
* Calculate and set the rendered length of packet parts and total packet length.
*/
size_t psyc_packet_length_set (PsycPacket *p);
size_t
psyc_packet_length_set (PsycPacket *p);
/** Initialize list. */
void psyc_list_init (PsycList *list, PsycString *elems, size_t num_elems,
PsycListFlag flag);
void
psyc_list_init (PsycList *list, PsycString *elems, size_t num_elems,
PsycListFlag flag);
/** Initialize packet. */
void psyc_packet_init (PsycPacket *packet,
PsycModifier *routing, size_t routinglen,
PsycModifier *entity, size_t entitylen,
char *method, size_t methodlen,
char *data, size_t datalen,
char stateop, PsycPacketFlag flag);
void
psyc_packet_init (PsycPacket *packet,
PsycModifier *routing, size_t routinglen,
PsycModifier *entity, size_t entitylen,
char *method, size_t methodlen,
char *data, size_t datalen,
char stateop, PsycPacketFlag flag);
/** Initialize packet with raw content. */
void psyc_packet_init_raw (PsycPacket *packet,
PsycModifier *routing, size_t routinglen,
char *content, size_t contentlen,
PsycPacketFlag flag);
void
psyc_packet_init_raw (PsycPacket *packet,
PsycModifier *routing, size_t routinglen,
char *content, size_t contentlen,
PsycPacketFlag flag);
/** @} */ // end of packet group
#define PSYC_PACKET_H
#endif

View file

@ -1,4 +1,5 @@
#ifndef PSYC_PARSE_H
#define PSYC_PARSE_H
/**
* @file psyc/parse.h
@ -115,13 +116,13 @@
#include <psyc.h>
typedef enum {
/// Default Flag. Parse everything.
PSYC_PARSE_ALL = 0,
/// Parse only the header
PSYC_PARSE_ROUTING_ONLY = 1,
/// Parse only the content.
/// Parsing starts at the content and the content must be complete.
PSYC_PARSE_START_AT_CONTENT = 2,
/// Default Flag. Parse everything.
PSYC_PARSE_ALL = 0,
/// Parse only the header
PSYC_PARSE_ROUTING_ONLY = 1,
/// Parse only the content.
/// Parsing starts at the content and the content must be complete.
PSYC_PARSE_START_AT_CONTENT = 2,
} PsycParseFlag;
/**
@ -129,69 +130,69 @@ typedef enum {
* @see psyc_parse()
*/
typedef enum {
/// Error, packet is not ending with a valid delimiter.
PSYC_PARSE_ERROR_END = -8,
/// Error, expected NL after the method.
PSYC_PARSE_ERROR_METHOD = -7,
/// Error, expected NL after a modifier.
PSYC_PARSE_ERROR_MOD_NL = -6,
/// Error, modifier length is not numeric.
PSYC_PARSE_ERROR_MOD_LEN = -5,
/// Error, expected TAB before modifier value.
PSYC_PARSE_ERROR_MOD_TAB = -4,
/// Error, modifier name is missing.
PSYC_PARSE_ERROR_MOD_NAME = -3,
/// Error, expected NL after the content length.
PSYC_PARSE_ERROR_LENGTH = -2,
/// Error in packet.
PSYC_PARSE_ERROR = -1,
/// 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.
PSYC_PARSE_INSUFFICIENT = 1,
/// Routing modifier parsing done.
/// Operator, name & value contains the respective parts.
PSYC_PARSE_ROUTING = 2,
/// State sync operation.
PSYC_PARSE_STATE_RESYNC = 3,
/// State reset operation.
PSYC_PARSE_STATE_RESET = 4,
/// Start of an incomplete entity modifier.
/// Operator & name are complete, value is incomplete.
PSYC_PARSE_ENTITY_START = 5,
/// Continuation of an incomplete entity modifier.
PSYC_PARSE_ENTITY_CONT = 6,
/// End of an incomplete entity modifier.
PSYC_PARSE_ENTITY_END = 7,
/// Entity modifier parsing done in one go.
/// Operator, name & value contains the respective parts.
PSYC_PARSE_ENTITY = 8,
/// Start of an incomplete body.
/// Name contains method, value contains part of the body.
/// Used when packet length is given
PSYC_PARSE_BODY_START = 9,
/// Continuation of an incomplete body.
/// Used when packet length is given
PSYC_PARSE_BODY_CONT = 10,
/// End of an incomplete body.
/// Used when packet length is given
PSYC_PARSE_BODY_END = 11,
/// Body parsing done in one go, name contains method, value contains body.
PSYC_PARSE_BODY = 12,
/// Start of an incomplete content, value contains part of content.
/// Used when PSYC_PARSE_ROUTING_ONLY is set.
PSYC_PARSE_CONTENT_START = 9,
/// Continuation of an incomplete content.
/// Used when PSYC_PARSE_ROUTING_ONLY is set.
PSYC_PARSE_CONTENT_CONT = 10,
/// End of an incomplete content.
/// Used when PSYC_PARSE_ROUTING_ONLY is set.
PSYC_PARSE_CONTENT_END = 11,
/// Content parsing done in one go, value contains the whole content.
/// Used when PSYC_PARSE_ROUTING_ONLY is set.
PSYC_PARSE_CONTENT = 12,
/// Finished parsing packet.
PSYC_PARSE_COMPLETE = 13,
/// Error, packet is not ending with a valid delimiter.
PSYC_PARSE_ERROR_END = -8,
/// Error, expected NL after the method.
PSYC_PARSE_ERROR_METHOD = -7,
/// Error, expected NL after a modifier.
PSYC_PARSE_ERROR_MOD_NL = -6,
/// Error, modifier length is not numeric.
PSYC_PARSE_ERROR_MOD_LEN = -5,
/// Error, expected TAB before modifier value.
PSYC_PARSE_ERROR_MOD_TAB = -4,
/// Error, modifier name is missing.
PSYC_PARSE_ERROR_MOD_NAME = -3,
/// Error, expected NL after the content length.
PSYC_PARSE_ERROR_LENGTH = -2,
/// Error in packet.
PSYC_PARSE_ERROR = -1,
/// 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.
PSYC_PARSE_INSUFFICIENT = 1,
/// Routing modifier parsing done.
/// Operator, name & value contains the respective parts.
PSYC_PARSE_ROUTING = 2,
/// State sync operation.
PSYC_PARSE_STATE_RESYNC = 3,
/// State reset operation.
PSYC_PARSE_STATE_RESET = 4,
/// Start of an incomplete entity modifier.
/// Operator & name are complete, value is incomplete.
PSYC_PARSE_ENTITY_START = 5,
/// Continuation of an incomplete entity modifier.
PSYC_PARSE_ENTITY_CONT = 6,
/// End of an incomplete entity modifier.
PSYC_PARSE_ENTITY_END = 7,
/// Entity modifier parsing done in one go.
/// Operator, name & value contains the respective parts.
PSYC_PARSE_ENTITY = 8,
/// Start of an incomplete body.
/// Name contains method, value contains part of the body.
/// Used when packet length is given
PSYC_PARSE_BODY_START = 9,
/// Continuation of an incomplete body.
/// Used when packet length is given
PSYC_PARSE_BODY_CONT = 10,
/// End of an incomplete body.
/// Used when packet length is given
PSYC_PARSE_BODY_END = 11,
/// Body parsing done in one go, name contains method, value contains body.
PSYC_PARSE_BODY = 12,
/// Start of an incomplete content, value contains part of content.
/// Used when PSYC_PARSE_ROUTING_ONLY is set.
PSYC_PARSE_CONTENT_START = 9,
/// Continuation of an incomplete content.
/// Used when PSYC_PARSE_ROUTING_ONLY is set.
PSYC_PARSE_CONTENT_CONT = 10,
/// End of an incomplete content.
/// Used when PSYC_PARSE_ROUTING_ONLY is set.
PSYC_PARSE_CONTENT_END = 11,
/// Content parsing done in one go, value contains the whole content.
/// Used when PSYC_PARSE_ROUTING_ONLY is set.
PSYC_PARSE_CONTENT = 12,
/// Finished parsing packet.
PSYC_PARSE_COMPLETE = 13,
} PsycParseRC;
/**
@ -199,48 +200,48 @@ typedef enum {
* @see psyc_parse_list()
*/
typedef enum {
PSYC_PARSE_LIST_ERROR_DELIM = -4,
PSYC_PARSE_LIST_ERROR_LEN = -3,
PSYC_PARSE_LIST_ERROR_TYPE = -2,
PSYC_PARSE_LIST_ERROR = -1,
/// Completed parsing a list element.
PSYC_PARSE_LIST_ELEM = 1,
/// Reached end of buffer.
PSYC_PARSE_LIST_END = 2,
/// Binary list is incomplete.
PSYC_PARSE_LIST_INCOMPLETE = 3,
PSYC_PARSE_LIST_ERROR_DELIM = -4,
PSYC_PARSE_LIST_ERROR_LEN = -3,
PSYC_PARSE_LIST_ERROR_TYPE = -2,
PSYC_PARSE_LIST_ERROR = -1,
/// Completed parsing a list element.
PSYC_PARSE_LIST_ELEM = 1,
/// Reached end of buffer.
PSYC_PARSE_LIST_END = 2,
/// Binary list is incomplete.
PSYC_PARSE_LIST_INCOMPLETE = 3,
} PsycParseListRC;
/**
* Struct for keeping parser state.
*/
typedef struct {
size_t cursor; ///< Current position in buffer.
size_t startc; ///< Position where the parsing would be resumed.
PsycString buffer; ///< Buffer with data to be parsed.
uint8_t flags; ///< Flags for the parser, see PsycParseFlag.
PsycPart part; ///< Part of the packet being parsed currently.
size_t cursor; ///< Current position in buffer.
size_t startc; ///< Position where the parsing would be resumed.
PsycString buffer; ///< Buffer with data to be parsed.
uint8_t flags; ///< Flags for the parser, see PsycParseFlag.
PsycPart part; ///< Part of the packet being parsed currently.
size_t routingLength; ///< Length of routing part parsed so far.
size_t contentParsed; ///< Number of bytes parsed from the content so far.
size_t contentLength; ///< Expected length of the content.
PsycBool contentLengthFound; ///< Is there a length given for this packet?
size_t valueParsed; ///< Number of bytes parsed from the value so far.
size_t valueLength; ///< Expected length of the value.
PsycBool valueLengthFound; ///< Is there a length given for this modifier?
size_t routingLength; ///< Length of routing part parsed so far.
size_t contentParsed; ///< Number of bytes parsed from the content so far.
size_t contentLength; ///< Expected length of the content.
PsycBool contentLengthFound;///< Is there a length given for this packet?
size_t valueParsed; ///< Number of bytes parsed from the value so far.
size_t valueLength; ///< Expected length of the value.
PsycBool valueLengthFound; ///< Is there a length given for this modifier?
} PsycParseState;
/**
* Struct for keeping list parser state.
*/
typedef struct {
size_t cursor; ///< Current position in buffer.
size_t startc; ///< Line start position.
PsycString buffer; ///< Buffer with data to be parsed.
PsycListType type; ///< List type.
size_t cursor; ///< Current position in buffer.
size_t startc; ///< Line start position.
PsycString buffer; ///< Buffer with data to be parsed.
PsycListType type; ///< List type.
size_t elemParsed; ///< Number of bytes parsed from the elem so far.
size_t elemLength; ///< Expected length of the elem.
size_t elemParsed; ///< Number of bytes parsed from the elem so far.
size_t elemLength; ///< Expected length of the elem.
} PsycParseListState;
/**
@ -250,14 +251,14 @@ typedef struct {
* @param flags Flags to be set for the parser, see PsycParseFlag.
* @see PsycParseFlag
*/
static inline
void psyc_parse_state_init (PsycParseState *state, uint8_t flags)
static inline void
psyc_parse_state_init (PsycParseState *state, uint8_t flags)
{
memset(state, 0, sizeof(PsycParseState));
state->flags = flags;
memset(state, 0, sizeof(PsycParseState));
state->flags = flags;
if (flags & PSYC_PARSE_START_AT_CONTENT)
state->part = PSYC_PART_CONTENT;
if (flags & PSYC_PARSE_START_AT_CONTENT)
state->part = PSYC_PART_CONTENT;
}
/**
@ -271,16 +272,17 @@ void psyc_parse_state_init (PsycParseState *state, uint8_t flags)
* @param length length of the data in bytes
* @see PsycString
*/
static inline
void psyc_parse_buffer_set (PsycParseState *state, char *buffer, size_t length)
static inline void
psyc_parse_buffer_set (PsycParseState *state, char *buffer,
size_t length)
{
state->buffer = (PsycString) {length, buffer};
state->cursor = 0;
state->buffer = (PsycString) {length, buffer};
state->cursor = 0;
if (state->flags & PSYC_PARSE_START_AT_CONTENT) {
state->contentLength = length;
state->contentLengthFound = PSYC_TRUE;
}
if (state->flags & PSYC_PARSE_START_AT_CONTENT) {
state->contentLength = length;
state->contentLengthFound = PSYC_TRUE;
}
}
/**
@ -288,68 +290,68 @@ void psyc_parse_buffer_set (PsycParseState *state, char *buffer, size_t length)
*
* @param state Pointer to the list state struct that should be initialized.
*/
static inline
void psyc_parse_list_state_init (PsycParseListState *state)
static inline void
psyc_parse_list_state_init (PsycParseListState *state)
{
memset(state, 0, sizeof(PsycParseListState));
memset(state, 0, sizeof(PsycParseListState));
}
/**
* Sets a new buffer in the list parser state struct with data to be parsed.
*/
static inline
void psyc_parse_list_buffer_set (PsycParseListState *state, char *buffer, size_t length)
static inline void
psyc_parse_list_buffer_set (PsycParseListState *state, char *buffer, size_t length)
{
state->buffer = (PsycString) {length, buffer};
state->cursor = 0;
state->buffer = (PsycString) {length, buffer};
state->cursor = 0;
}
static inline
size_t psyc_parse_content_length (PsycParseState *state)
static inline size_t
psyc_parse_content_length (PsycParseState *state)
{
return state->contentLength;
return state->contentLength;
}
static inline
PsycBool psyc_parse_content_length_found (PsycParseState *state)
static inline PsycBool
psyc_parse_content_length_found (PsycParseState *state)
{
return state->contentLengthFound;
return state->contentLengthFound;
}
static inline
size_t psyc_parse_value_length (PsycParseState *state)
static inline size_t
psyc_parse_value_length (PsycParseState *state)
{
return state->valueLength;
return state->valueLength;
}
static inline
PsycBool psyc_parse_value_length_found (PsycParseState *state)
static inline PsycBool
psyc_parse_value_length_found (PsycParseState *state)
{
return state->valueLengthFound;
return state->valueLengthFound;
}
static inline
size_t psyc_parse_cursor (PsycParseState *state)
static inline size_t
psyc_parse_cursor (PsycParseState *state)
{
return state->cursor;
return state->cursor;
}
static inline
size_t psyc_parse_buffer_length (PsycParseState *state)
static inline size_t
psyc_parse_buffer_length (PsycParseState *state)
{
return state->buffer.length;
return state->buffer.length;
}
static inline
size_t psyc_parse_remaining_length (PsycParseState *state)
static inline size_t
psyc_parse_remaining_length (PsycParseState *state)
{
return state->buffer.length - state->cursor;
return state->buffer.length - state->cursor;
}
static inline
const char * psyc_parse_remaining_buffer (PsycParseState *state)
static inline const char *
psyc_parse_remaining_buffer (PsycParseState *state)
{
return state->buffer.data + state->cursor;
return state->buffer.data + state->cursor;
}
/**
@ -371,8 +373,9 @@ const char * psyc_parse_remaining_buffer (PsycParseState *state)
#ifdef __INLINE_PSYC_PARSE
static inline
#endif
PsycParseRC psyc_parse (PsycParseState *state, char *oper,
PsycString *name, PsycString *value);
PsycParseRC
psyc_parse (PsycParseState *state, char *oper,
PsycString *name, PsycString *value);
/**
* List parser.
@ -388,142 +391,142 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
#ifdef __INLINE_PSYC_PARSE
static inline
#endif
PsycParseListRC psyc_parse_list (PsycParseListState *state, PsycString *elem);
PsycParseListRC
psyc_parse_list (PsycParseListState *state, PsycString *elem);
static inline
PsycBool psyc_parse_number (const char *value, size_t len, int64_t *n)
static inline PsycBool
psyc_parse_number (const char *value, size_t len, int64_t *n)
{
size_t c = 0;
uint8_t neg = 0;
size_t c = 0;
uint8_t neg = 0;
if (!value)
return PSYC_FALSE;
if (value[0] == '-')
neg = ++c;
*n = 0;
while (c < len && value[c] >= '0' && value[c] <= '9')
*n = 10 * *n + (value[c++] - '0');
if (c != len)
return PSYC_FALSE;
if (neg)
*n = 0 - *n;
return PSYC_TRUE;
}
static inline
PsycBool psyc_parse_number_unsigned (const char *value, size_t len, uint64_t *n)
{
size_t c = 0;
if (!value)
return PSYC_FALSE;
*n = 0;
while (c < len && value[c] >= '0' && value[c] <= '9')
*n = 10 * *n + (value[c++] - '0');
return c == len ? PSYC_TRUE : PSYC_FALSE;
}
static inline
PsycBool psyc_parse_time (const char *value, size_t len, time_t *t)
{
return psyc_parse_number(value, len, t);
}
static inline
PsycBool psyc_parse_date (const char *value, size_t len, time_t *t)
{
if (psyc_parse_number(value, len, t)) {
*t += PSYC_EPOCH;
return PSYC_TRUE;
}
if (!value)
return PSYC_FALSE;
if (value[0] == '-')
neg = ++c;
*n = 0;
while (c < len && value[c] >= '0' && value[c] <= '9')
*n = 10 * *n + (value[c++] - '0');
if (c != len)
return PSYC_FALSE;
if (neg)
*n = 0 - *n;
return PSYC_TRUE;
}
static inline PsycBool
psyc_parse_number_unsigned (const char *value, size_t len, uint64_t *n)
{
size_t c = 0;
if (!value)
return PSYC_FALSE;
*n = 0;
while (c < len && value[c] >= '0' && value[c] <= '9')
*n = 10 * *n + (value[c++] - '0');
return c == len ? PSYC_TRUE : PSYC_FALSE;
}
static inline PsycBool
psyc_parse_time (const char *value, size_t len, time_t *t)
{
return psyc_parse_number(value, len, t);
}
static inline PsycBool
psyc_parse_date (const char *value, size_t len, time_t *t)
{
if (psyc_parse_number(value, len, t)) {
*t += PSYC_EPOCH;
return PSYC_TRUE;
}
return PSYC_FALSE;
}
/**
* Determines if the argument is a glyph.
* Glyphs are: : = + - ? !
*/
static inline
char psyc_is_glyph (uint8_t g)
static inline char
psyc_is_glyph (uint8_t g)
{
switch(g) {
case ':':
case '=':
case '+':
case '-':
case '?':
case '!':
return 1;
default:
return 0;
}
switch (g) {
case ':':
case '=':
case '+':
case '-':
case '?':
case '!':
return 1;
default:
return 0;
}
}
/**
* Determines if the argument is numeric.
*/
static inline
char psyc_is_numeric (uint8_t c)
static inline char
psyc_is_numeric (uint8_t c)
{
return c >= '0' && c <= '9';
return c >= '0' && c <= '9';
}
/**
* Determines if the argument is alphabetic.
*/
static inline
char psyc_is_alpha (uint8_t c)
static inline char
psyc_is_alpha (uint8_t c)
{
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
/**
* Determines if the argument is alphanumeric.
*/
static inline
char psyc_is_alpha_numeric (uint8_t c)
static inline char
psyc_is_alpha_numeric (uint8_t c)
{
return psyc_is_alpha(c) || psyc_is_numeric(c);
return psyc_is_alpha(c) || psyc_is_numeric(c);
}
/**
* Determines if the argument is a keyword character.
* Keyword characters are: alphanumeric and _
*/
static inline
char psyc_is_kw_char (uint8_t c)
static inline char
psyc_is_kw_char (uint8_t c)
{
return psyc_is_alpha_numeric(c) || c == '_';
return psyc_is_alpha_numeric(c) || c == '_';
}
/**
* Determines if the argument is a name character.
* Name characters are: see opaque_part in RFC 2396
*/
static inline
char psyc_is_name_char (uint8_t c)
static inline char
psyc_is_name_char (uint8_t c)
{
return psyc_is_alpha(c) || (c >= '$' && c <= ';') ||
c == '_' || c == '!' || c == '?' || c == '=' || c == '@' || c == '~';
return psyc_is_alpha(c) || (c >= '$' && c <= ';')
|| c == '_' || c == '!' || c == '?' || c == '=' || c == '@' || c == '~';
}
/**
* Determines if the argument is a hostname character.
* Hostname characters are: alphanumeric and -
*/
static inline
char psyc_is_host_char (uint8_t c)
static inline char
psyc_is_host_char (uint8_t c)
{
return psyc_is_alpha_numeric(c) || c == '.' || c == '-';
return psyc_is_alpha_numeric(c) || c == '.' || c == '-';
}
/** @} */ // end of parse group
#define PSYC_PARSE_H
#endif

View file

@ -1,4 +1,5 @@
#ifndef PSYC_RENDER_H
#define PSYC_RENDER_H
#include <psyc/packet.h>
@ -19,27 +20,25 @@
/**
* Return codes for psyc_render.
*/
typedef enum
{
/// Error, method is missing, but data is present.
PSYC_RENDER_ERROR_METHOD_MISSING = -3,
/// Error, a modifier name is missing.
PSYC_RENDER_ERROR_MODIFIER_NAME_MISSING = -2,
/// Error, buffer is too small to render the packet.
PSYC_RENDER_ERROR = -1,
/// Packet is rendered successfully in the buffer.
PSYC_RENDER_SUCCESS = 0,
typedef enum {
/// Error, method is missing, but data is present.
PSYC_RENDER_ERROR_METHOD_MISSING = -3,
/// Error, a modifier name is missing.
PSYC_RENDER_ERROR_MODIFIER_NAME_MISSING = -2,
/// Error, buffer is too small to render the packet.
PSYC_RENDER_ERROR = -1,
/// Packet is rendered successfully in the buffer.
PSYC_RENDER_SUCCESS = 0,
} PsycRenderRC;
/**
* Return codes for psyc_render_list.
*/
typedef enum
{
/// Error, buffer is too small to render the list.
PSYC_RENDER_LIST_ERROR = -1,
/// List is rendered successfully in the buffer.
PSYC_RENDER_LIST_SUCCESS = 0,
typedef enum {
/// Error, buffer is too small to render the list.
PSYC_RENDER_LIST_ERROR = -1,
/// List is rendered successfully in the buffer.
PSYC_RENDER_LIST_SUCCESS = 0,
} PsycRenderListRC;
/**
@ -59,7 +58,8 @@ typedef enum
#ifdef __INLINE_PSYC_RENDER
static inline
#endif
PsycRenderRC psyc_render (PsycPacket *packet, char *buffer, size_t buflen);
PsycRenderRC
psyc_render (PsycPacket *packet, char *buffer, size_t buflen);
/**
* Render a PSYC list into a buffer.
@ -67,9 +67,9 @@ PsycRenderRC psyc_render (PsycPacket *packet, char *buffer, size_t buflen);
#ifdef __INLINE_PSYC_RENDER
static inline
#endif
PsycRenderListRC psyc_render_list (PsycList *list, char *buffer, size_t buflen);
PsycRenderListRC
psyc_render_list (PsycList *list, char *buffer, size_t buflen);
/** @} */ // end of render group
#define PSYC_RENDER_H
#endif

View file

@ -1,4 +1,5 @@
#ifndef PSYC_TEXT_H
#define PSYC_TEXT_H
/**
* @file psyc/text.h
@ -19,39 +20,36 @@
* Return values for the text template parsing function.
* @see psyc_text()
*/
typedef enum
{
/// No substitution was made, nothing was written to the buffer.
PSYC_TEXT_NO_SUBST = -1,
/// Text template parsing & rendering complete.
PSYC_TEXT_COMPLETE = 0,
/// Text template parsing & rendering is incomplete, because the buffer was too small.
/// Another call is required to this function after setting a new buffer.
PSYC_TEXT_INCOMPLETE = 1,
typedef enum {
/// No substitution was made, nothing was written to the buffer.
PSYC_TEXT_NO_SUBST = -1,
/// Text template parsing & rendering complete.
PSYC_TEXT_COMPLETE = 0,
/// Text template parsing & rendering is incomplete, because the buffer was too
/// small. Another call is required to this function after setting a new buffer.
PSYC_TEXT_INCOMPLETE = 1,
} PsycTextRC;
/**
* Return values for PsycTextCB.
*/
typedef enum
{
/// Value not found, don't substitute anything.
PSYC_TEXT_VALUE_NOT_FOUND = -1,
/// Value found, substitute contents of the value variable.
PSYC_TEXT_VALUE_FOUND = 0,
typedef enum {
/// Value not found, don't substitute anything.
PSYC_TEXT_VALUE_NOT_FOUND = -1,
/// Value found, substitute contents of the value variable.
PSYC_TEXT_VALUE_FOUND = 0,
} PsycTextValueRC;
/**
* Struct for keeping PSYC text parser state.
*/
typedef struct
{
size_t cursor; ///< current position in the template
size_t written; ///< number of bytes written to buffer
PsycString tmpl; ///< input buffer with text template to parse
PsycString buffer; ///< output buffer for rendered text
PsycString open;
PsycString close;
typedef struct {
size_t cursor; ///< current position in the template
size_t written; ///< number of bytes written to buffer
PsycString tmpl; ///< input buffer with text template to parse
PsycString buffer; ///< output buffer for rendered text
PsycString open;
PsycString close;
} PsycTextState;
/**
@ -64,7 +62,8 @@ typedef struct
* PSYC_TEXT_VALUE_NOT_FOUND if no match found in which case psyc_text
* leaves the original template text as is.
*/
typedef PsycTextValueRC (*PsycTextCB)(const char *name, size_t len, PsycString *value, void *extra);
typedef PsycTextValueRC (*PsycTextCB) (const char *name, size_t len,
PsycString *value, void *extra);
/**
* Initializes the PSYC text state struct.
@ -75,17 +74,21 @@ typedef PsycTextValueRC (*PsycTextCB)(const char *name, size_t len, PsycString *
* @param buffer Output buffer where the rendered text is going to be written.
* @param buflen Length of output buffer.
*/
static inline
void psyc_text_state_init (PsycTextState *state,
char *tmpl, size_t tmplen,
char *buffer, size_t buflen)
static inline void
psyc_text_state_init (PsycTextState *state,
char *tmpl, size_t tmplen,
char *buffer, size_t buflen)
{
state->cursor = 0;
state->written = 0;
state->tmpl = (PsycString) {tmplen, tmpl};
state->buffer = (PsycString) {buflen, buffer};
state->open = (PsycString) {1, "["};
state->close = (PsycString) {1, "]"};
state->cursor = 0;
state->written = 0;
state->tmpl = (PsycString) {
tmplen, tmpl};
state->buffer = (PsycString) {
buflen, buffer};
state->open = (PsycString) {
1, "["};
state->close = (PsycString) {
1, "]"};
}
/**
@ -101,36 +104,40 @@ void psyc_text_state_init (PsycTextState *state,
* @param close Closing brace.
* @param closelen Length of closing brace.
*/
static inline
void psyc_text_state_init_custom (PsycTextState *state,
char *tmpl, size_t tmplen,
char *buffer, size_t buflen,
char *open, size_t openlen,
char *close, size_t closelen)
static inline void
psyc_text_state_init_custom (PsycTextState *state,
char *tmpl, size_t tmplen,
char *buffer, size_t buflen,
char *open, size_t openlen,
char *close, size_t closelen)
{
state->cursor = 0;
state->written = 0;
state->tmpl = (PsycString) {tmplen, tmpl};
state->buffer = (PsycString) {buflen, buffer};
state->open = (PsycString) {openlen, open};
state->close = (PsycString) {closelen, close};
state->cursor = 0;
state->written = 0;
state->tmpl = (PsycString) {
tmplen, tmpl};
state->buffer = (PsycString) {
buflen, buffer};
state->open = (PsycString) {
openlen, open};
state->close = (PsycString) {
closelen, close};
}
/**
* Sets a new output buffer in the PSYC text state struct.
*/
static inline
void psyc_text_buffer_set (PsycTextState *state,
char *buffer, size_t length)
static inline void
psyc_text_buffer_set (PsycTextState *state, char *buffer, size_t length)
{
state->buffer = (PsycString){length, buffer};
state->written = 0;
state->buffer = (PsycString) {
length, buffer};
state->written = 0;
}
static inline
size_t psyc_text_bytes_written (PsycTextState *state)
static inline size_t
psyc_text_bytes_written (PsycTextState *state)
{
return state->written;
return state->written;
}
/**
@ -148,9 +155,9 @@ size_t psyc_text_bytes_written (PsycTextState *state)
*
* @see http://about.psyc.eu/psyctext
**/
PsycTextRC psyc_text (PsycTextState *state, PsycTextCB getValue, void *extra);
PsycTextRC
psyc_text (PsycTextState *state, PsycTextCB getValue, void *extra);
/** @} */ // end of text group
#define PSYC_TEXT_H
#endif

View file

@ -1,4 +1,6 @@
#ifndef PSYC_UNIFORM_H
#define PSYC_UNIFORM_H
/**
* @file uniform.h
* @brief Uniform parsing.
@ -7,76 +9,76 @@
#include <psyc.h>
typedef enum {
PSYC_SCHEME_PSYC = 0,
PSYC_SCHEME_IRC = 1,
PSYC_SCHEME_XMPP = 2,
PSYC_SCHEME_SIP = 3,
PSYC_SCHEME_PSYC = 0,
PSYC_SCHEME_IRC = 1,
PSYC_SCHEME_XMPP = 2,
PSYC_SCHEME_SIP = 3,
} PsycScheme;
typedef struct {
// essential parts
uint8_t valid;
PsycScheme type;
PsycString scheme;
PsycString user;
PsycString pass;
PsycString host;
PsycString port;
PsycString transport;
PsycString resource;
PsycString query;
PsycString channel;
uint8_t valid;
PsycScheme type;
PsycString scheme;
PsycString user;
PsycString pass;
PsycString host;
PsycString port;
PsycString transport;
PsycString resource;
PsycString query;
PsycString channel;
// convenient snippets of the URL
PsycString full; // the URL as such
PsycString body; // the URL without scheme and '//'
PsycString user_host; // mailto and xmpp style
PsycString host_port; // just host:port (and transport)
PsycString root; // root UNI
PsycString entity; // entity UNI, without the channel
PsycString slashes; // the // if the protocol has them
PsycString slash; // first / after host
PsycString nick; // whatever works as a nickname
PsycString full; // the URL as such
PsycString body; // the URL without scheme and '//'
PsycString user_host; // mailto and xmpp style
PsycString host_port; // just host:port (and transport)
PsycString root; // root UNI
PsycString entity; // entity UNI, without the channel
PsycString slashes; // the // if the protocol has them
PsycString slash; // first / after host
PsycString nick; // whatever works as a nickname
} PsycUniform;
typedef enum {
PSYC_UNIFORM_SCHEME = 0,
PSYC_UNIFORM_SLASHES,
PSYC_UNIFORM_USER,
PSYC_UNIFORM_PASS,
PSYC_UNIFORM_HOST,
PSYC_UNIFORM_PORT,
PSYC_UNIFORM_TRANSPORT,
PSYC_UNIFORM_RESOURCE,
PSYC_UNIFORM_QUERY,
PSYC_UNIFORM_CHANNEL,
PSYC_UNIFORM_SCHEME = 0,
PSYC_UNIFORM_SLASHES,
PSYC_UNIFORM_USER,
PSYC_UNIFORM_PASS,
PSYC_UNIFORM_HOST,
PSYC_UNIFORM_PORT,
PSYC_UNIFORM_TRANSPORT,
PSYC_UNIFORM_RESOURCE,
PSYC_UNIFORM_QUERY,
PSYC_UNIFORM_CHANNEL,
} PsycUniformPart;
typedef enum {
PSYC_PARSE_UNIFORM_INVALID_SLASHES = -7,
PSYC_PARSE_UNIFORM_INVALID_CHANNEL = -6,
PSYC_PARSE_UNIFORM_INVALID_RESOURCE = -5,
PSYC_PARSE_UNIFORM_INVALID_TRANSPORT = -4,
PSYC_PARSE_UNIFORM_INVALID_PORT = -3,
PSYC_PARSE_UNIFORM_INVALID_HOST = -2,
PSYC_PARSE_UNIFORM_INVALID_SCHEME = -1,
PSYC_PARSE_UNIFORM_INVALID_SLASHES = -7,
PSYC_PARSE_UNIFORM_INVALID_CHANNEL = -6,
PSYC_PARSE_UNIFORM_INVALID_RESOURCE = -5,
PSYC_PARSE_UNIFORM_INVALID_TRANSPORT = -4,
PSYC_PARSE_UNIFORM_INVALID_PORT = -3,
PSYC_PARSE_UNIFORM_INVALID_HOST = -2,
PSYC_PARSE_UNIFORM_INVALID_SCHEME = -1,
} PsycParseUniformRC;
typedef enum {
PSYC_TRANSPORT_TCP = 'c',
PSYC_TRANSPORT_UDP = 'd',
PSYC_TRANSPORT_TLS = 's',
PSYC_TRANSPORT_GNUNET = 'g',
PSYC_TRANSPORT_TCP = 'c',
PSYC_TRANSPORT_UDP = 'd',
PSYC_TRANSPORT_TLS = 's',
PSYC_TRANSPORT_GNUNET = 'g',
} PsycTransport;
typedef enum {
PSYC_ENTITY_ROOT = 0,
PSYC_ENTITY_PERSON = '~',
PSYC_ENTITY_PLACE = '@',
PSYC_ENTITY_SERVICE = '$',
PSYC_ENTITY_ROOT = 0,
PSYC_ENTITY_PERSON = '~',
PSYC_ENTITY_PLACE = '@',
PSYC_ENTITY_SERVICE = '$',
} PsycEntityType;
int psyc_uniform_parse (PsycUniform *uni, char *str, size_t length);
int
psyc_uniform_parse (PsycUniform * uni, char *str, size_t length);
#define PSYC_UNIFORM_H
#endif

View file

@ -1,4 +1,5 @@
#ifndef PSYC_VARIABLE_H
#define PSYC_VARIABLE_H
/**
* @file psyc/variable.h
@ -16,22 +17,23 @@ extern const size_t psyc_var_types_num;
/**
* Is this a routing variable name?
*/
PsycBool psyc_var_is_routing (const char *name, size_t len);
PsycBool
psyc_var_is_routing (const char *name, size_t len);
/**
* Get the type of variable name.
*/
PsycType psyc_var_type (const char *name, size_t len);
PsycType
psyc_var_type (const char *name, size_t len);
/**
* Is this a list variable name?
*/
static inline
PsycBool psyc_var_is_list (const char *name, size_t len)
static inline PsycBool
psyc_var_is_list (const char *name, size_t len)
{
return len < 5 || memcmp(name, "_list", 5) != 0 ||
(len > 5 && name[5] != '_') ? PSYC_FALSE : PSYC_TRUE;
return len < 5 || memcmp(name, "_list", 5) != 0 || (len > 5 && name[5] != '_')
? PSYC_FALSE : PSYC_TRUE;
}
#define PSYC_VARIABLE_H
#endif