diff --git a/include/psyc/lib.h b/include/psyc/lib.h index 699f601..e6c8c6d 100644 --- a/include/psyc/lib.h +++ b/include/psyc/lib.h @@ -7,3 +7,8 @@ #define unless(COND) if (!(COND)) #define until(COND) while (!(COND)) +#ifndef _GNU_SOURCE +void *memmem(const void *haystack, size_t haystacklen, + const void *needle, size_t needlelen); +#endif + diff --git a/include/psyc/render.h b/include/psyc/render.h index 1b3f576..43eb0c7 100644 --- a/include/psyc/render.h +++ b/include/psyc/render.h @@ -1,13 +1,26 @@ -int PSYC_renderHeader(struct PSYC_Buffers* pbuf, +typedef enum { + PSYC_FINE = 0, + PSYC_NEED_LENGTH = 1 +} PSYC_RenderFlag; + +typedef struct { + PSYC_RenderFlag flag; ///< flags for the renderer + size_t cursor; ///< current position in buffer + size_t start; ///< position where the rendered packet starts + size_t length; ///< how big is the buffer we allocated + uint8_t buffer[]; ///< OMG a C99 feature! variable size buffer! +} PSYC_RenderState; + +int PSYC_renderHeader(PSYC_RenderState render, const uint8_t* name, const size_t nlength, const uint8_t* value, const size_t vlength, - const uchar flags, const uchar modifier); + const uint8_t flags, const uint8_t modifier); -int PSYC_renderBody(struct PSYC_Buffers* pbuf, +int PSYC_renderBody(PSYC_RenderState render, const uint8_t* method, const size_t mlength, const uint8_t* data, const size_t dlength, - const uchar flags); + const uint8_t flags); -int PSYC_doneRender(struct PSYC_Buffers* pbuf, +int PSYC_doneRender(PSYC_RenderState render, uint8_t** buf, size_t* written); diff --git a/include/psyc/syntax.h b/include/psyc/syntax.h index c15889e..004845e 100644 --- a/include/psyc/syntax.h +++ b/include/psyc/syntax.h @@ -10,8 +10,9 @@ # define PSYC_CONTENT_SIZE_THRESHOLD 444 #endif -#define C_GLYPH_PACKET_DELIMITER '.' -#define S_GLYPH_PACKET_DELIMITER "." +#define C_GLYPH_PACKET_DELIMITER '|' +#define S_GLYPH_PACKET_DELIMITER "|" +#define PSYC_PACKET_DELIMITER "\n|\n" #define C_GLYPH_SEPARATOR_KEYWORD '_' #define S_GLYPH_SEPARATOR_KEYWORD "_" diff --git a/src/Makefile b/src/Makefile index 837a9ce..5251245 100644 --- a/src/Makefile +++ b/src/Makefile @@ -2,8 +2,8 @@ CFLAGS=-I../../include -DDEBUG=2 -DPSYC_COMPILE_LIBRARY -g -O0 -Wall CC=cc -I../include # CC=clang -S=parser.c match.c -O=parser.o match.o +S=parser.c match.c render.c memmem.c +O=parser.o match.o render.o memmem.o default: @/bin/echo -e "Usage:\n\tmake diet - compile with diet libc\n\tmake lib - compile with normal gnu libc" diff --git a/src/memmem.c b/src/memmem.c new file mode 100644 index 0000000..a5e1ad3 --- /dev/null +++ b/src/memmem.c @@ -0,0 +1,67 @@ +#ifndef _GNU_SOURCE + +/* Copyright (C) 1991,92,93,94,96,97,98,2000,2004,2007 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +/* +#ifndef _LIBC +# include +#endif +*/ + +#include +#include + +#ifndef _LIBC +# define __builtin_expect(expr, val) (expr) +#endif + +#undef memmem + +/* Return the first occurrence of NEEDLE in HAYSTACK. */ +void * +memmem (haystack, haystack_len, needle, needle_len) + const void *haystack; + size_t haystack_len; + const void *needle; + size_t needle_len; +{ + const char *begin; + const char *const last_possible + = (const char *) haystack + haystack_len - needle_len; + + if (needle_len == 0) + /* The first occurrence of the empty string is deemed to occur at + the beginning of the string. */ + return (void *) haystack; + + /* Sanity check, otherwise the loop might search through the whole + memory. */ + if (__builtin_expect (haystack_len < needle_len, 0)) + return NULL; + + for (begin = (const char *) haystack; begin <= last_possible; ++begin) + if (begin[0] == ((const char *) needle)[0] && + !memcmp ((const void *) &begin[1], + (const void *) ((const char *) needle + 1), + needle_len - 1)) + return (void *) begin; + + return NULL; +} + +#endif diff --git a/src/render.c b/src/render.c index 595b8e1..269fba0 100644 --- a/src/render.c +++ b/src/render.c @@ -1 +1,25 @@ +#include "psyc/lib.h" +#include "psyc/render.h" +#include "psyc/syntax.h" + /* render PSYC packets */ + +int PSYC_renderBody(PSYC_RenderState render, + const uint8_t* method, const size_t mlength, + const uint8_t* data, const size_t dlength, + const uint8_t flags) { + + // find out if this packet needs a prepended length + if (dlength == 1 && data[0] == C_GLYPH_PACKET_DELIMITER) + render.flag = PSYC_NEED_LENGTH; + else if (dlength > 404) + render.flag = PSYC_NEED_LENGTH; + else if (memmem(data, dlength, PSYC_PACKET_DELIMITER, + sizeof(PSYC_PACKET_DELIMITER))) + render.flag = PSYC_NEED_LENGTH; + else + render.flag = PSYC_FINE; + + // TBD + return 0; +}