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 static unsigned read_short(short *, const char *);
15 static unsigned read_int(int *, const char *);
16 static int decode_func(GlDecoder *, unsigned short, const char *);
17 static int decode_gldfunc(GlDecoder *, unsigned short, const char *);
19 GlDecoder *gldecoder_new(void *user_data, void (*destroy)(void *))
23 dec = (GlDecoder *)malloc(sizeof(GlDecoder));
24 memset(dec, 0, sizeof(GlDecoder));
25 dec->user_data = user_data;
26 dec->destroy = destroy;
31 void gldecoder_delete(GlDecoder *dec)
34 dec->destroy(dec->user_data);
38 int gldecoder_decode(GlDecoder *dec, const char *data, unsigned len)
45 if(len<sizeof(int)+sizeof(short))
47 pos += read_int((int *)&pktlen, data);
50 pos += read_short((short *)&func, data+pos);
54 ret = decode_gldfunc(dec, func, data+pos);
56 ret = decode_func(dec, func, data+pos);
63 static unsigned read_char(char *v, const char *data)
69 static unsigned read_short(short *v, const char *data)
75 static unsigned read_int(int *v, const char *data)
81 static unsigned read_long(long *v, const char *data)
87 static unsigned read_long_long(long long *v, const char *data)
89 *v = *(long long *)data;
90 return sizeof(long long);
93 static unsigned read_float(float *v, const char *data)
99 static unsigned read_double(double *v, const char *data)
101 *v = *(double *)data;
102 return sizeof(double);
105 typedef void *pointer;
107 static unsigned read_pointer(pointer *v, const char *data)
110 return sizeof(void *);
113 static unsigned read_data(const void **v, const char *data)
117 pos += read_int(&vlen, data);
125 typedef const char *string;
127 static unsigned read_string(string *v, const char *data)
129 return read_data((const void **)v, data);
132 static unsigned read_string_array(string **v, const char *data)
137 pos += read_int(&count, data);
138 *v = (string *)tmpalloc(count*sizeof(string));
139 for(i=0; i<count; ++i)
140 pos += read_string(*v+i, data+pos);
144 #include "gldecoder.funcs"
146 static int decode_gldError(GlDecoder *dec, const char *data)
150 pos += read_int((int *)&code, data);
152 dec->gldError(dec->user_data, code);
156 static int decode_gldfunc(GlDecoder *dec, unsigned short func, const char *data)
160 case FUNC_GLDERROR: return decode_gldError(dec, data);