X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fgldecoder.c;h=22b6285a244130fae4dd8d7af9481433402e7741;hb=6d297b506314c07bff3d77c2853a5f59380cfcb0;hp=cd290aa513b3f1e69ea21009b95513bdff46a5ac;hpb=4aba13fc7690cf79e949d9765b371e85e7530207;p=gldbg.git diff --git a/source/gldecoder.c b/source/gldecoder.c index cd290aa..22b6285 100644 --- a/source/gldecoder.c +++ b/source/gldecoder.c @@ -1,7 +1,7 @@ /* $Id$ This file is part of gldbg -Copyright © 2009 Mikko Rasa, Mikkosoft Productions +Copyright © 2009-2010 Mikko Rasa, Mikkosoft Productions Distributed under the GPL */ @@ -9,17 +9,8 @@ Distributed under the GPL #include #include "functions.h" #include "gldecoder.h" -#include "tmpalloc.h" +#include "packet.h" -typedef struct sGlPacket -{ - const char *ptr; - unsigned chunk; - unsigned total; -} GlPacket; - -static void read_short(short *, GlPacket *); -static void read_int(int *, GlPacket *); static int decode_func(GlDecoder *, unsigned short, GlPacket *); static int decode_gldfunc(GlDecoder *, unsigned short, GlPacket *); @@ -44,165 +35,52 @@ void gldecoder_delete(GlDecoder *dec) int gldecoder_decode(GlDecoder *dec, const char *data, unsigned len) { + GlPacket *pkt; unsigned short func; - GlPacket packet; - - if(lentotal==0) - return; - pkt->chunk = pkt->total; - read_int((int *)&pkt->chunk, pkt); - pkt->chunk &= 0x7FFFFFFF; -} + int ret = 0; -static void read_generic(void *v, unsigned size, int byteswap, GlPacket *pkt) -{ - if(pkt->chunk==0) - next_chunk(pkt); - - if(pkt->chunk>=size) - { - // TODO: Actually implement byteswap if needed - (void)byteswap; - memcpy(v, pkt->ptr, size); - pkt->ptr += size; - pkt->chunk -= size; - pkt->total -= size; - } + if(func&0x8000) + ret = decode_gldfunc(dec, func, pkt); else - { - memset(v, 0, size); - pkt->total -= pkt->chunk; - pkt->chunk = 0; - } -} - -static void read_char(char *v, GlPacket *pkt) -{ - read_generic(v, 1, 0, pkt); -} - -static void read_short(short *v, GlPacket *pkt) -{ - read_generic(v, sizeof(short), 1, pkt); -} - -static void read_int(int *v, GlPacket *pkt) -{ - read_generic(v, sizeof(int), 1, pkt); -} - -static void read_long(long *v, GlPacket *pkt) -{ - read_generic(v, sizeof(long), 1, pkt); -} - -static void read_long_long(long long *v, GlPacket *pkt) -{ - read_generic(v, sizeof(long long), 1, pkt); -} - -static void read_float(float *v, GlPacket *pkt) -{ - read_generic(v, sizeof(float), 1, pkt); -} + ret = decode_func(dec, func, pkt); + if(ret<0) + return -1; -static void read_double(double *v, GlPacket *pkt) -{ - read_generic(v, sizeof(double), 1, pkt); + return len; } typedef const void *pointer; - -static void read_pointer(pointer *v, GlPacket *pkt) -{ - read_generic(v, sizeof(pointer), 1, pkt); -} - -static void read_data(pointer *v, GlPacket *pkt) -{ - int vlen; - read_int(&vlen, pkt); - if(vlen) - *v = pkt->ptr; - else - *v = NULL; -} - typedef const char *string; -static void read_string(string *v, GlPacket *pkt) -{ - read_data((pointer *)v, pkt); -} - -static void read_string_array(string **v, GlPacket *pkt) -{ - int count; - int i; - read_int(&count, pkt); - *v = (string *)tmpalloc(count*sizeof(string)); - for(i=0; igldError) dec->gldError(dec->user_data, code); + else if(dec->unhandled) + dec->unhandled(dec->user_data, FUNC_GLDERROR); +} + +static void decode_gldBreak(GlDecoder *dec, GlPacket *pkt) +{ + unsigned short func; + unsigned char flag; + packet_read_short(pkt, (short *)&func); + packet_read_char(pkt, (char *)&flag); + if(dec->gldBreak) + dec->gldBreak(dec->user_data, func, flag); + else if(dec->unhandled) + dec->unhandled(dec->user_data, FUNC_GLDBREAK); } static int decode_gldfunc(GlDecoder *dec, unsigned short func, GlPacket *pkt) @@ -210,6 +88,7 @@ static int decode_gldfunc(GlDecoder *dec, unsigned short func, GlPacket *pkt) switch(func) { case FUNC_GLDERROR: decode_gldError(dec, pkt); break; + case FUNC_GLDBREAK: decode_gldBreak(dec, pkt); break; default: return -1; } return 0;