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"
13 static unsigned read_short(short *, const char *);
14 static unsigned read_int(int *, const char *);
15 static int decode_func(GlDecoder *, unsigned short, const char *);
16 static int decode_gldfunc(GlDecoder *, unsigned short, const char *);
18 GlDecoder *gldecoder_new(void *user_data, void (*destroy)(void *))
22 dec = (GlDecoder *)malloc(sizeof(GlDecoder));
23 memset(dec, 0, sizeof(GlDecoder));
24 dec->user_data = user_data;
25 dec->destroy = destroy;
30 void gldecoder_delete(GlDecoder *dec)
33 dec->destroy(dec->user_data);
37 int gldecoder_decode(GlDecoder *dec, const char *data, unsigned len)
44 if(len<sizeof(int)+sizeof(short))
46 pos += read_int(&pktlen, data);
49 pos += read_short(&func, data+pos);
53 ret = decode_gldfunc(dec, func, data+pos);
55 ret = decode_func(dec, func, data+pos);
62 static unsigned read_char(char *v, const char *data)
68 static unsigned read_short(short *v, const char *data)
74 static unsigned read_int(int *v, const char *data)
80 static unsigned read_long(long *v, const char *data)
86 static unsigned read_ulong(unsigned long *v, const char *data)
88 *v = *(unsigned long *)data;
89 return sizeof(unsigned long);
92 static unsigned read_longlong(long long *v, const char *data)
94 *v = *(long long *)data;
95 return sizeof(long long);
98 static unsigned read_float(float *v, const char *data)
101 return sizeof(float);
104 static unsigned read_double(double *v, const char *data)
106 *v = *(double *)data;
107 return sizeof(double);
110 static unsigned read_pointer(void **v, const char *data)
113 return sizeof(void *);
116 static unsigned read_data(const void **v, const char *data)
120 pos += read_int(&vlen, data);
128 static unsigned read_string(const unsigned char **v, const char *data)
130 return read_data((const void **)v, data);
133 static unsigned read_string_array(const unsigned char ***v, const char *data)
138 pos += read_int(&count, data);
139 *v = (const unsigned char **)tmpalloc(count*sizeof(const unsigned char *));
140 for(i=0; i<count; ++i)
141 pos += read_string(*v+i, data+pos);
145 #include "gldecoder.funcs"
147 static int decode_gldError(GlDecoder *dec, const char *data)
151 pos += read_int(&code, data);
153 dec->gldError(dec->user_data, code);
157 static int decode_gldfunc(GlDecoder *dec, unsigned short func, const char *data)
161 case FUNC_GLDERROR: return decode_gldError(dec, data);