3 This file is part of gldbg
4 Copyright © 2009 Mikko Rasa, Mikkosoft Productions
5 Distributed under the GPL
10 #include "functions.h"
11 #include "gldecoder.h"
14 typedef struct sGlPacket
21 static void read_short(short *, GlPacket *);
22 static void read_int(int *, GlPacket *);
23 static int decode_func(GlDecoder *, unsigned short, GlPacket *);
24 static int decode_gldfunc(GlDecoder *, unsigned short, GlPacket *);
26 GlDecoder *gldecoder_new(void *user_data, void (*destroy)(void *))
30 dec = (GlDecoder *)malloc(sizeof(GlDecoder));
31 memset(dec, 0, sizeof(GlDecoder));
32 dec->user_data = user_data;
33 dec->destroy = destroy;
38 void gldecoder_delete(GlDecoder *dec)
41 dec->destroy(dec->user_data);
45 int gldecoder_decode(GlDecoder *dec, const char *data, unsigned len)
50 if(len<sizeof(int)+sizeof(short))
55 read_int((int *)&packet.chunk, &packet);
57 if(packet.chunk&0x80000000)
59 packet.chunk &= 0x7FFFFFFF;
60 packet.total = packet.chunk;
64 if(len<packet.total+sizeof(int))
67 GlPacket p = { packet.ptr+packet.total, len-packet.total, 0 };
69 read_int((int *)&chunk, &p);
70 packet.total += chunk&0x7FFFFFFF;
71 if(!(chunk&0x80000000))
76 packet.total = packet.chunk;
81 read_short((short *)&func, &packet);
88 ret = decode_gldfunc(dec, func, &packet);
90 ret = decode_func(dec, func, &packet);
95 return packet.ptr+packet.total-data;
98 static void next_chunk(GlPacket *pkt)
102 pkt->chunk = pkt->total;
103 read_int((int *)&pkt->chunk, pkt);
104 pkt->chunk &= 0x7FFFFFFF;
107 static void read_generic(void *v, unsigned size, int byteswap, GlPacket *pkt)
114 // TODO: Actually implement byteswap if needed
116 memcpy(v, pkt->ptr, size);
124 pkt->total -= pkt->chunk;
129 static void read_char(char *v, GlPacket *pkt)
131 read_generic(v, 1, 0, pkt);
134 static void read_short(short *v, GlPacket *pkt)
136 read_generic(v, sizeof(short), 1, pkt);
139 static void read_int(int *v, GlPacket *pkt)
141 read_generic(v, sizeof(int), 1, pkt);
144 static void read_long(long *v, GlPacket *pkt)
146 read_generic(v, sizeof(long), 1, pkt);
149 static void read_long_long(long long *v, GlPacket *pkt)
151 read_generic(v, sizeof(long long), 1, pkt);
154 static void read_float(float *v, GlPacket *pkt)
156 read_generic(v, sizeof(float), 1, pkt);
159 static void read_double(double *v, GlPacket *pkt)
161 read_generic(v, sizeof(double), 1, pkt);
164 typedef const void *pointer;
166 static void read_pointer(pointer *v, GlPacket *pkt)
168 read_generic(v, sizeof(pointer), 1, pkt);
171 static void read_data(pointer *v, GlPacket *pkt)
174 read_int(&vlen, pkt);
184 typedef const char *string;
186 static void read_string(string *v, GlPacket *pkt)
188 read_data((pointer *)v, pkt);
191 static void read_string_array(string **v, GlPacket *pkt)
195 read_int(&count, pkt);
196 *v = (string *)tmpalloc(count*sizeof(string));
197 for(i=0; i<count; ++i)
198 read_string(*v+i, pkt);
201 #include "gensrc/gldecoder.funcs"
203 static void decode_gldError(GlDecoder *dec, GlPacket *pkt)
206 read_int((int *)&code, pkt);
208 dec->gldError(dec->user_data, code);
211 static int decode_gldfunc(GlDecoder *dec, unsigned short func, GlPacket *pkt)
215 case FUNC_GLDERROR: decode_gldError(dec, pkt); break;