X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fgldecoder.c;h=35a365a76fff1735a19069cb76a0c0078fea7f2f;hb=d72ef6d75a11f6cc05ab8ec039520719e1044741;hp=6444fcaac8515b5054f7ea97a09ce354f6577f1b;hpb=498c37732c8849b49819c6b64fe42256ca7f6e04;p=gldbg.git diff --git a/source/gldecoder.c b/source/gldecoder.c index 6444fca..35a365a 100644 --- a/source/gldecoder.c +++ b/source/gldecoder.c @@ -1,25 +1,9 @@ -/* $Id$ - -This file is part of gldbg -Copyright © 2009 Mikko Rasa, Mikkosoft Productions -Distributed under the GPL -*/ - #include #include #include "functions.h" #include "gldecoder.h" -#include "tmpalloc.h" - -typedef struct sGlPacket -{ - const char *ptr; - unsigned chunk; - unsigned total; -} GlPacket; +#include "packet.h" -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,168 +28,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; -} + packet_read_short(pkt, (short *)&func); -static void read_generic(void *v, unsigned size, int byteswap, GlPacket *pkt) -{ - if(pkt->chunk==0) - next_chunk(pkt); + int ret = 0; - 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; - pkt->ptr += vlen; - pkt->chunk -= vlen; - pkt->total -= vlen; -} - 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) @@ -213,6 +81,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;