]> git.tdb.fi Git - gldbg.git/blobdiff - source/gldecoder.c
Check for and report OpenGL errors after each function call
[gldbg.git] / source / gldecoder.c
index 93a525876c64c4b033bb92cd6f770239fcbc2dd9..6e903736f726a5840c9252056858c52b910d0592 100644 (file)
@@ -10,6 +10,11 @@ Distributed under the GPL
 #include "functions.h"
 #include "gldecoder.h"
 
+static unsigned read_short(short *, const char *);
+static unsigned read_int(int *, const char *);
+static int decode_func(GlDecoder *, unsigned short, const char *);
+static int decode_gldfunc(GlDecoder *, unsigned short, const char *);
+
 GlDecoder *gldecoder_new(void *user_data, void (*destroy)(void *))
 {
        GlDecoder *dec;
@@ -29,72 +34,119 @@ void gldecoder_delete(GlDecoder *dec)
        free(dec);
 }
 
-unsigned read_char(char *v, const char *data, unsigned len)
+int gldecoder_decode(GlDecoder *dec, const char *data, unsigned len)
+{
+       unsigned pos = 0;
+       int pktlen;
+       unsigned short func;
+       int ret;
+
+       if(len<sizeof(int)+sizeof(short))
+               return -1;
+       pos += read_int(&pktlen, data);
+       if(len<pktlen)
+               return -1;
+       pos += read_short(&func, data+pos);
+       if(dec)
+       {
+               if(func&0x8000)
+                       ret = decode_gldfunc(dec, func, data+pos);
+               else
+                       ret = decode_func(dec, func, data+pos);
+               if(ret<0)
+                       return -1;
+       }
+       return pktlen;
+}
+
+static unsigned read_char(char *v, const char *data)
 {
        *v = *data;
        return 1;
 }
 
-unsigned read_short(short *v, const char *data, unsigned len)
+static unsigned read_short(short *v, const char *data)
 {
        *v = *(short *)data;
        return sizeof(short);
 }
 
-unsigned read_int(int *v, const char *data, unsigned len)
+static unsigned read_int(int *v, const char *data)
 {
        *v = *(int *)data;
        return sizeof(int);
 }
 
-unsigned read_long(long *v, const char *data, unsigned len)
+static unsigned read_long(long *v, const char *data)
 {
        *v = *(long *)data;
        return sizeof(long);
 }
 
-unsigned read_ulong(unsigned long *v, const char *data, unsigned len)
+static unsigned read_ulong(unsigned long *v, const char *data)
 {
        *v = *(unsigned long *)data;
        return sizeof(unsigned long);
 }
 
-unsigned read_longlong(long long *v, const char *data, unsigned len)
+static unsigned read_longlong(long long *v, const char *data)
 {
        *v = *(long long *)data;
        return sizeof(long long);
 }
 
-unsigned read_float(float *v, const char *data, unsigned len)
+static unsigned read_float(float *v, const char *data)
 {
        *v = *(float *)data;
        return sizeof(float);
 }
 
-unsigned read_double(double *v, const char *data, unsigned len)
+static unsigned read_double(double *v, const char *data)
 {
        *v = *(double *)data;
        return sizeof(double);
 }
 
-unsigned read_pointer(void **v, const char *data, unsigned len)
+static unsigned read_pointer(void **v, const char *data)
 {
        *v = *(void **)data;
        return sizeof(void *);
 }
 
-unsigned read_data(const void **v, const char *data, unsigned len)
+static unsigned read_data(const void **v, const char *data)
 {
        int vlen;
        unsigned pos = 0;
-       pos += read_int(&vlen, data, len);
-       *v = data+pos;
+       pos += read_int(&vlen, data);
+       if(vlen)
+               *v = data+pos;
+       else
+               *v = NULL;
        return pos+vlen;
 }
 
-unsigned read_string(const unsigned char **v, const char *data, unsigned len)
+static unsigned read_string(const unsigned char **v, const char *data)
 {
-       return read_data((const void **)v, data, len);
+       return read_data((const void **)v, data);
 }
 
 #include "gldecoder.funcs"
+
+static int decode_gldError(GlDecoder *dec, const char *data)
+{
+       unsigned pos = 0;
+       GLenum code;
+       pos += read_int(&code, data);
+       if(dec->gldError)
+               dec->gldError(dec->user_data, code);
+       return pos;
+}
+
+static int decode_gldfunc(GlDecoder *dec, unsigned short func, const char *data)
+{
+       switch(func)
+       {
+       case FUNC_GLDERROR: return decode_gldError(dec, data);
+       default: return -1;
+       }
+}