diff options
| author | Magnus Auvinen <magnus.auvinen@gmail.com> | 2007-08-22 07:52:33 +0000 |
|---|---|---|
| committer | Magnus Auvinen <magnus.auvinen@gmail.com> | 2007-08-22 07:52:33 +0000 |
| commit | 8b3c16e6152a527f9aec1a88a9eed74119de7000 (patch) | |
| tree | f0bde5cea15e696e42cade06a3b12ff6b13acc57 /src/engine/packer.c | |
| parent | 9899666a7ce6679a3b9667ab09f615f4d0769c16 (diff) | |
| download | zcatch-8b3c16e6152a527f9aec1a88a9eed74119de7000.tar.gz zcatch-8b3c16e6152a527f9aec1a88a9eed74119de7000.zip | |
major engine cleanup. dependency on baselib removed. engine is now C code (not ansi tho). some other cruft removed aswell
Diffstat (limited to 'src/engine/packer.c')
| -rw-r--r-- | src/engine/packer.c | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/src/engine/packer.c b/src/engine/packer.c new file mode 100644 index 00000000..190749c2 --- /dev/null +++ b/src/engine/packer.c @@ -0,0 +1,89 @@ + +#include "packer.h" +#include "compression.h" + +void packer_reset(PACKER *p) +{ + p->error = 0; + p->current = p->buffer; + p->end = p->current + PACKER_BUFFER_SIZE; +} + +void packer_add_int(PACKER *p, int i) +{ + p->current = vint_pack(p->current, i); +} + +void packer_add_string(PACKER *p, const char *str, int limit) +{ + if(limit > 0) + { + while(*str && limit != 0) + { + *p->current++ = *str++; + limit--; + } + *p->current++ = 0; + } + else + { + while(*str) + *p->current++ = *str++; + *p->current++ = 0; + } +} + +void packer_add_raw(PACKER *p, const unsigned char *data, int size) +{ + while(size) + { + *p->current++ = *data++; + size--; + } +} + +int packer_size(PACKER *p) +{ + return (const unsigned char *)p->current-(const unsigned char *)p->buffer; +} + +const unsigned char *packer_data(PACKER *p) +{ + return (const unsigned char *)p->buffer; +} + +void unpacker_reset(UNPACKER *p, const unsigned char *data, int size) +{ + p->error = 0; + p->start = data; + p->end = p->start + size; + p->current = p->start; +} + +int unpacker_get_int(UNPACKER *p) +{ + int i; + if(p->current >= p->end) + return 0; + p->current = vint_unpack(p->current, &i); + return i; +} + +const char *unpacker_get_string(UNPACKER *p) +{ + if(p->current >= p->end) + return ""; + + const char *ptr = (const char *)p->current; + while(*p->current) /* skip the string */ + p->current++; + p->current++; + return ptr; +} + +const unsigned char *unpacker_get_raw(UNPACKER *p, int size) +{ + const unsigned char *ptr = p->current; + p->current += size; + return ptr; +} |