2011-04-25 12:20:13 +00:00
|
|
|
#include <psyc/lib.h>
|
|
|
|
#include <psyc/syntax.h>
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
2011-04-25 21:40:38 +00:00
|
|
|
inline psycString psyc_newString(const char *str, size_t strlen)
|
2011-04-25 12:20:13 +00:00
|
|
|
{
|
2011-04-25 21:40:38 +00:00
|
|
|
psycString s = {strlen, str};
|
2011-04-25 12:20:13 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2011-04-25 21:40:38 +00:00
|
|
|
inline psycModifier psyc_newModifier(char oper, psycString *name, psycString *value,
|
2011-04-26 15:23:06 +00:00
|
|
|
psycModifierFlag flag)
|
2011-04-25 12:20:13 +00:00
|
|
|
{
|
2011-04-25 21:40:38 +00:00
|
|
|
psycModifier m = {oper, *name, *value, flag};
|
2011-04-25 12:20:13 +00:00
|
|
|
|
|
|
|
if (flag == PSYC_MODIFIER_CHECK_LENGTH) // find out if it needs a length
|
|
|
|
{
|
|
|
|
if (value->length > PSYC_MODIFIER_SIZE_THRESHOLD)
|
2011-04-25 12:47:16 +00:00
|
|
|
m.flag = PSYC_MODIFIER_NEED_LENGTH;
|
2011-04-25 12:20:13 +00:00
|
|
|
else if (memchr(value->ptr, (int)'\n', value->length))
|
2011-04-25 12:47:16 +00:00
|
|
|
m.flag = PSYC_MODIFIER_NEED_LENGTH;
|
2011-04-25 12:20:13 +00:00
|
|
|
else
|
2011-04-25 12:47:16 +00:00
|
|
|
m.flag = PSYC_MODIFIER_NO_LENGTH;
|
2011-04-25 12:20:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2011-04-25 21:40:38 +00:00
|
|
|
inline psycModifier psyc_newModifier2(char oper,
|
2011-04-26 15:23:06 +00:00
|
|
|
const char *name, size_t namelen,
|
|
|
|
const char *value, size_t valuelen,
|
|
|
|
psycModifierFlag flag)
|
2011-04-25 12:20:13 +00:00
|
|
|
{
|
2011-04-25 21:40:38 +00:00
|
|
|
psycString n = {namelen, name};
|
|
|
|
psycString v = {valuelen, value};
|
2011-04-25 12:20:13 +00:00
|
|
|
|
2011-04-25 21:40:38 +00:00
|
|
|
return psyc_newModifier(oper, &n, &v, flag);
|
2011-04-25 12:20:13 +00:00
|
|
|
}
|
|
|
|
|
2011-04-25 21:40:38 +00:00
|
|
|
inline size_t psyc_getModifierLength(psycModifier *m)
|
2011-04-25 12:20:13 +00:00
|
|
|
{
|
2011-04-25 19:57:03 +00:00
|
|
|
size_t length = 1 + // oper
|
2011-04-25 12:20:13 +00:00
|
|
|
m->name.length + 1 + // name\t
|
|
|
|
m->value.length + 1; // value\n
|
|
|
|
|
|
|
|
if (m->flag == PSYC_MODIFIER_NEED_LENGTH) // add length of length if needed
|
|
|
|
length += log10((double)m->value.length) + 2; // SP length
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2011-04-26 15:23:06 +00:00
|
|
|
inline psycList psyc_newList(psycString *elems, size_t num_elems, psycListFlag flag)
|
|
|
|
{
|
|
|
|
psycList list = {num_elems, elems, 0, flag};
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (flag == PSYC_LIST_CHECK_LENGTH) // check if list elements need length
|
|
|
|
{
|
|
|
|
for (i = 0; i < num_elems; i++)
|
|
|
|
{
|
|
|
|
if (memchr(elems[i].ptr, (int)'|', elems[i].length) ||
|
|
|
|
memchr(elems[i].ptr, (int)'\n', elems[i].length))
|
|
|
|
{
|
|
|
|
list.flag = PSYC_LIST_NEED_LENGTH;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (list.flag == PSYC_LIST_NEED_LENGTH)
|
|
|
|
{
|
|
|
|
for (i = 0; i < num_elems; i++)
|
|
|
|
{
|
|
|
|
if (i > 0)
|
|
|
|
list.length++; // |
|
|
|
|
list.length += log10((double)elems[i].length) + 2 + elems[i].length; // length SP elem
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (i = 0; i < num_elems; i++)
|
|
|
|
list.length += 1 + elems[i].length; // |elem
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline psycPacket psyc_newPacket(psycHeader *routing,
|
|
|
|
psycHeader *entity,
|
|
|
|
psycString *method, psycString *data,
|
|
|
|
psycPacketFlag flag)
|
2011-04-25 12:20:13 +00:00
|
|
|
{
|
2011-04-25 21:40:38 +00:00
|
|
|
psycPacket p = {*routing, *entity, *method, *data, 0, 0, flag};
|
2011-04-25 12:20:13 +00:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (flag == PSYC_PACKET_CHECK_LENGTH) // find out if it needs a length
|
|
|
|
{
|
|
|
|
if (data->length == 1 && data->ptr[0] == C_GLYPH_PACKET_DELIMITER)
|
|
|
|
p.flag = PSYC_PACKET_NEED_LENGTH;
|
|
|
|
else if (data->length > PSYC_CONTENT_SIZE_THRESHOLD)
|
|
|
|
p.flag = PSYC_PACKET_NEED_LENGTH;
|
|
|
|
else if (memmem(data->ptr, data->length, PSYC_C2ARG(PSYC_PACKET_DELIMITER)))
|
|
|
|
p.flag = PSYC_PACKET_NEED_LENGTH;
|
|
|
|
else
|
|
|
|
p.flag = PSYC_PACKET_NO_LENGTH;
|
|
|
|
}
|
|
|
|
|
2011-04-25 19:57:03 +00:00
|
|
|
// add routing header length
|
2011-04-25 12:39:58 +00:00
|
|
|
for (i = 0; i < routing->lines; i++)
|
2011-04-25 21:40:38 +00:00
|
|
|
p.routingLength += psyc_getModifierLength(&routing->modifiers[i]);
|
2011-04-25 12:20:13 +00:00
|
|
|
|
2011-04-25 19:57:03 +00:00
|
|
|
// add entity header length
|
2011-04-25 12:39:58 +00:00
|
|
|
for (i = 0; i < entity->lines; i++)
|
2011-04-25 21:40:38 +00:00
|
|
|
p.contentLength += psyc_getModifierLength(&entity->modifiers[i]);
|
2011-04-25 12:20:13 +00:00
|
|
|
|
|
|
|
// add length of method, data & delimiter
|
2011-04-25 19:57:03 +00:00
|
|
|
if (method->length)
|
|
|
|
p.contentLength += method->length + 1; // method\n
|
|
|
|
if (data->length)
|
|
|
|
p.contentLength += data->length + 1; // data\n
|
2011-04-25 12:20:13 +00:00
|
|
|
|
2011-04-25 19:57:03 +00:00
|
|
|
// set total length: routing-header \n content |\n
|
2011-04-25 20:47:24 +00:00
|
|
|
p.length = p.routingLength + 1 + p.contentLength + sizeof(PSYC_PACKET_DELIMITER) - 2;
|
2011-04-25 12:20:13 +00:00
|
|
|
if (flag == PSYC_PACKET_NEED_LENGTH) // add length of length if needed
|
2011-04-25 20:47:24 +00:00
|
|
|
p.length += log10((double)data->length) + 1;
|
2011-04-25 12:20:13 +00:00
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2011-04-25 21:40:38 +00:00
|
|
|
inline psycPacket psyc_newPacket2(psycModifier *routing, size_t routinglen,
|
2011-04-26 15:23:06 +00:00
|
|
|
psycModifier *entity, size_t entitylen,
|
|
|
|
const char *method, size_t methodlen,
|
|
|
|
const char *data, size_t datalen,
|
|
|
|
psycPacketFlag flag)
|
2011-04-25 12:20:13 +00:00
|
|
|
{
|
2011-04-26 15:23:06 +00:00
|
|
|
psycHeader r = {routinglen, routing};
|
|
|
|
psycHeader e = {entitylen, entity};
|
2011-04-25 21:40:38 +00:00
|
|
|
psycString m = {methodlen, method};
|
|
|
|
psycString d = {datalen, data};
|
2011-04-25 12:20:13 +00:00
|
|
|
|
2011-04-25 21:40:38 +00:00
|
|
|
return psyc_newPacket(&r, &e, &m, &d, flag);
|
2011-04-25 12:20:13 +00:00
|
|
|
}
|