]> git.tdb.fi Git - gldbg.git/blobdiff - source/gldecoder.c
Use a centralized packet framework
[gldbg.git] / source / gldecoder.c
index 6444fcaac8515b5054f7ea97a09ce354f6577f1b..999acccc9cec140d141f5db97f878af136282cdf 100644 (file)
@@ -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 <string.h>
 #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,158 +35,28 @@ void gldecoder_delete(GlDecoder *dec)
 
 int gldecoder_decode(GlDecoder *dec, const char *data, unsigned len)
 {
+       GlPacket *pkt;
        unsigned short func;
-       GlPacket packet;
-
-       if(len<sizeof(int)+sizeof(short))
-               return -1;
-
-       packet.ptr = data;
-       packet.chunk = len;
-       read_int((int *)&packet.chunk, &packet);
-
-       if(packet.chunk&0x80000000)
-       {
-               packet.chunk &= 0x7FFFFFFF;
-               packet.total = packet.chunk;
-
-               while(1)
-               {
-                       if(len<packet.total+sizeof(int))
-                               return -1;
-
-                       GlPacket p = { packet.ptr+packet.total, len-packet.total, 0 };
-                       unsigned chunk;
-                       read_int((int *)&chunk, &p);
-                       packet.total += chunk&0x7FFFFFFF;
-                       if(!(chunk&0x80000000))
-                               break;
-               }
-       }
-       else
-               packet.total = packet.chunk;
 
-       if(len<packet.total)
+       pkt = packet_receive_str(data, &len);
+       if(!pkt)
                return -1;
 
-       read_short((short *)&func, &packet);
+       packet_read_short(pkt, (short *)&func);
 
        if(dec)
        {
                int ret = 0;
 
                if(func&0x8000)
-                       ret = decode_gldfunc(dec, func, &packet);
+                       ret = decode_gldfunc(dec, func, pkt);
                else
-                       ret = decode_func(dec, func, &packet);
+                       ret = decode_func(dec, func, pkt);
                if(ret<0)
                        return -1;
        }
 
-       return packet.ptr+packet.total-data;
-}
-
-static void next_chunk(GlPacket *pkt)
-{
-       if(pkt->total==0)
-               return;
-       pkt->chunk = pkt->total;
-       read_int((int *)&pkt->chunk, pkt);
-       pkt->chunk &= 0x7FFFFFFF;
-}
-
-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;
-       }
-       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);
-}
-
-static void read_double(double *v, GlPacket *pkt)
-{
-       read_generic(v, sizeof(double), 1, pkt);
-}
-
-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; i<count; ++i)
-               read_string(*v+i, pkt);
+       return len;
 }
 
 #include "gensrc/gldecoder.funcs"
@@ -203,7 +64,7 @@ static void read_string_array(string **v, GlPacket *pkt)
 static void decode_gldError(GlDecoder *dec, GlPacket *pkt)
 {
        GLenum code;
-       read_int((int *)&code, pkt);
+       packet_read_int(pkt, (int *)&code);
        if(dec->gldError)
                dec->gldError(dec->user_data, code);
 }