From 2b70735aea2bee140df2984feea0e43b8cfe5781 Mon Sep 17 00:00:00 2001 From: Gabor Adam Toth Date: Sat, 30 Apr 2011 18:34:43 +0200 Subject: [PATCH] fixed content length issues in parser & renderer --- src/packet.c | 35 +++++++++++++++++++++-------------- src/parser.c | 3 +-- src/render.c | 5 +---- test/packets/full-1-length | 2 +- test/testRender.c | 2 +- 5 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/packet.c b/src/packet.c index 8455636..a29a21f 100644 --- a/src/packet.c +++ b/src/packet.c @@ -113,23 +113,30 @@ inline psycList psyc_newList(psycString *elems, size_t num_elems, psycListFlag f inline psycPacketFlag psyc_checkPacketLength(psycPacket *p) { - psycPacketFlag flag; - if (p->data.length == 1 && p->data.ptr[0] == C_GLYPH_PACKET_DELIMITER) - flag = PSYC_PACKET_NEED_LENGTH; - else if (p->data.length > PSYC_CONTENT_SIZE_THRESHOLD) - flag = PSYC_PACKET_NEED_LENGTH; - else if (memmem(p->data.ptr, p->data.length, PSYC_C2ARG(PSYC_PACKET_DELIMITER))) - flag = PSYC_PACKET_NEED_LENGTH; - else - flag = PSYC_PACKET_NO_LENGTH; + return PSYC_PACKET_NEED_LENGTH; - return flag; + if (p->data.length > PSYC_CONTENT_SIZE_THRESHOLD) + return PSYC_PACKET_NEED_LENGTH; + + int i; + // if any entity modifiers need length it is possible they contain + // a packet terminator, thus the content should have a length as well + for (i = 0; i < p->entity.lines; i++) + if (p->entity.modifiers[i].flag == PSYC_MODIFIER_NEED_LENGTH) + return PSYC_PACKET_NEED_LENGTH; + + if (memmem(p->data.ptr, p->data.length, PSYC_C2ARG(PSYC_PACKET_DELIMITER))) + return PSYC_PACKET_NEED_LENGTH; + + return PSYC_PACKET_NO_LENGTH; } inline size_t psyc_setPacketLength(psycPacket *p) { size_t i; + p->routingLength = 0; + p->contentLength = 0; // add routing header length for (i = 0; i < p->routing.lines; i++) @@ -145,14 +152,14 @@ inline size_t psyc_setPacketLength(psycPacket *p) if (p->data.length) p->contentLength += p->data.length + 1; // data\n - // set total length: routing-header \n content |\n - p->length = p->routingLength + p->contentLength + sizeof(PSYC_PACKET_DELIMITER) - 2; + // set total length: routing-header content |\n + p->length = p->routingLength + p->contentLength + 2; + if (p->contentLength > 0) { - p->contentLength--; // subtract the \n from the delimiter, as that doesn't belong to the content part p->length++; // add \n at the start of the content part if (p->flag == PSYC_PACKET_NEED_LENGTH) // add length of length if needed - p->length += log10((double)p->data.length) + 1; + p->length += log10((double)p->contentLength) + 1; } return p->length; diff --git a/src/parser.c b/src/parser.c index ffebd44..7db0b74 100644 --- a/src/parser.c +++ b/src/parser.c @@ -416,10 +416,9 @@ psycParseRC psyc_parse(psycParseState* state, char* oper, psycString* name, psyc if (state->cursor >= state->buffer.length) return PSYC_PARSE_BODY; - if (state->buffer.ptr[state->cursor] != '\n') + if (state->buffer.ptr[state->cursor] != '|') return PSYC_PARSE_ERROR_BODY; - state->cursor++; state->part = PSYC_PART_END; return PSYC_PARSE_BODY; } diff --git a/src/render.c b/src/render.c index e7b5793..b7a6773 100644 --- a/src/render.c +++ b/src/render.c @@ -47,7 +47,6 @@ inline size_t psyc_renderModifier(psycModifier *mod, char *buffer) if (mod->flag == PSYC_MODIFIER_NEED_LENGTH) { buffer[cur++] = ' '; - //cur += sprintf(buffer + cur, "%ld", mod->value.length); cur += itoa(mod->value.length, buffer + cur, 10); } @@ -71,10 +70,8 @@ psycRenderRC psyc_render(psycPacket *packet, char *buffer, size_t buflen) cur += psyc_renderModifier(&packet->routing.modifiers[i], buffer + cur); // add length if needed - if (packet->flag == PSYC_PACKET_NEED_LENGTH) { - //cur += sprintf(buffer + cur, "%ld", packet->contentLength); + if (packet->flag == PSYC_PACKET_NEED_LENGTH) cur += itoa(packet->contentLength, buffer + cur, 10); - } if (packet->entity.lines || packet->method.length || packet->data.length) buffer[cur++] = '\n'; // start of content part if there's content diff --git a/test/packets/full-1-length b/test/packets/full-1-length index d4334de..fddf47a 100644 --- a/test/packets/full-1-length +++ b/test/packets/full-1-length @@ -1,7 +1,7 @@ :_source psyc://foo/~bar :_target psyc://bar/~baz :_tag sch1828hu3r2cm -85 +86 :_foo bar baz :_abc_def 11 foo bar baz diff --git a/test/testRender.c b/test/testRender.c index 7d2ac5d..4a274f8 100644 --- a/test/testRender.c +++ b/test/testRender.c @@ -99,7 +99,7 @@ _notice_presence\n\ if (testList("\ :_source psyc://10.100.1000/~ludwig\n\ :_context psyc://10.100.1000/~ludwig\n\ -\n\ +85\n\ :_list_text |foo|bar|baz\n\ :_list_binary 21 3 foo|3 b|r|7 baz\n\ qux\n\