From ecefc4912f36f9153e63e7e0ee90d8ef69898fed Mon Sep 17 00:00:00 2001 From: Eugene Crosser Date: Thu, 31 Oct 2013 17:05:07 +0400 Subject: [PATCH 1/1] add serializer --- Makefile.am | 4 ++-- serial.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ serial.h | 16 ++++++++++++++++ 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 serial.c create mode 100644 serial.h diff --git a/Makefile.am b/Makefile.am index 05241a3..3559ca1 100644 --- a/Makefile.am +++ b/Makefile.am @@ -3,10 +3,10 @@ AUTOMAKE_OPTIONS = foreign ACLOCAL_AMFLAGS = -I m4 -noinst_HEADERS = pcsc_cr.h token.h crypto_if.h crypto.h +noinst_HEADERS = pcsc_cr.h token.h crypto_if.h crypto.h serial.h EXTRA_LTLIBRARIES = libpcsc_cr.la -libpcsc_cr_la_SOURCES = crypto.c pcsc_cr.c ykneo.c +libpcsc_cr_la_SOURCES = serial.c crypto.c pcsc_cr.c ykneo.c EXTRA_libpcsc_cr_la_SOURCES = ossl_crypto.c tom_crypto.c libpcsc_cr_la_LIBADD = @CRYPTO_OBJS@ libpcsc_cr_la_DEPENDENCIES = @CRYPTO_OBJS@ diff --git a/serial.c b/serial.c new file mode 100644 index 0000000..0719746 --- /dev/null +++ b/serial.c @@ -0,0 +1,47 @@ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif +#include +#include "serial.h" + +int serial_init(serializer_t *srl, void *buffer, int size) +{ + srl->buffer = srl->cursor = buffer; + srl->bufsize = size; +} + +int serial_switch(serializer_t *srl, void *buffer, int size) +{ + int used = srl->cursor - srl->buffer; + + memcpy(buffer, srl->buffer, used); + srl->buffer = buffer; + srl->bufsize = size; + srl->cursor = buffer + used; +} + +int serial_put(serializer_t *srl, void *item, int size) +{ + int left = srl->bufsize - (srl->cursor - srl->buffer); + if (left < size + sizeof(short)) return left - sizeof(short); + *((short *)srl->cursor) = size; + srl->cursor += 2; + if (size) memcpy(srl->cursor, item, size); + srl->cursor += size; + return size; +} + +int serial_get(serializer_t *srl, void *item, int bufsize) +{ + short isize = *((short *)srl->cursor); + if (isize > bufsize || isize == 0) return isize; + srl->cursor += sizeof(short); + memcpy(item, srl->cursor, isize); + srl->cursor += isize; + return isize; +} + +int serial_size(serializer_t *srl) +{ + return srl->cursor - srl->buffer; +} diff --git a/serial.h b/serial.h new file mode 100644 index 0000000..5f8f155 --- /dev/null +++ b/serial.h @@ -0,0 +1,16 @@ +#ifndef _SERIAL_H +#define _SERIAL_H + +typedef struct _serializer { + char *buffer; + int bufsize; + char *cursor; +} serializer_t; + +int serial_init(serializer_t *srl, void *buffer, int size); +int serial_switch(serializer_t *srl, void *buffer, int size); +int serial_put(serializer_t *srl, void *item, int size); +int serial_get(serializer_t *srl, void *item, int bufsize); +int serial_size(serializer_t *srl); + +#endif -- 2.39.2