]> git.tdb.fi Git - gldbg.git/blob - source/gldecoder.c
6e903736f726a5840c9252056858c52b910d0592
[gldbg.git] / source / gldecoder.c
1 /* $Id$
2
3 This file is part of gldbg
4 Copyright © 2009  Mikko Rasa, Mikkosoft Productions
5 Distributed under the GPL
6 */
7
8 #include <stdlib.h>
9 #include <string.h>
10 #include "functions.h"
11 #include "gldecoder.h"
12
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 *);
17
18 GlDecoder *gldecoder_new(void *user_data, void (*destroy)(void *))
19 {
20         GlDecoder *dec;
21
22         dec = (GlDecoder *)malloc(sizeof(GlDecoder));
23         memset(dec, 0, sizeof(GlDecoder));
24         dec->user_data = user_data;
25         dec->destroy = destroy;
26
27         return dec;
28 }
29
30 void gldecoder_delete(GlDecoder *dec)
31 {
32         if(dec->destroy)
33                 dec->destroy(dec->user_data);
34         free(dec);
35 }
36
37 int gldecoder_decode(GlDecoder *dec, const char *data, unsigned len)
38 {
39         unsigned pos = 0;
40         int pktlen;
41         unsigned short func;
42         int ret;
43
44         if(len<sizeof(int)+sizeof(short))
45                 return -1;
46         pos += read_int(&pktlen, data);
47         if(len<pktlen)
48                 return -1;
49         pos += read_short(&func, data+pos);
50         if(dec)
51         {
52                 if(func&0x8000)
53                         ret = decode_gldfunc(dec, func, data+pos);
54                 else
55                         ret = decode_func(dec, func, data+pos);
56                 if(ret<0)
57                         return -1;
58         }
59         return pktlen;
60 }
61
62 static unsigned read_char(char *v, const char *data)
63 {
64         *v = *data;
65         return 1;
66 }
67
68 static unsigned read_short(short *v, const char *data)
69 {
70         *v = *(short *)data;
71         return sizeof(short);
72 }
73
74 static unsigned read_int(int *v, const char *data)
75 {
76         *v = *(int *)data;
77         return sizeof(int);
78 }
79
80 static unsigned read_long(long *v, const char *data)
81 {
82         *v = *(long *)data;
83         return sizeof(long);
84 }
85
86 static unsigned read_ulong(unsigned long *v, const char *data)
87 {
88         *v = *(unsigned long *)data;
89         return sizeof(unsigned long);
90 }
91
92 static unsigned read_longlong(long long *v, const char *data)
93 {
94         *v = *(long long *)data;
95         return sizeof(long long);
96 }
97
98 static unsigned read_float(float *v, const char *data)
99 {
100         *v = *(float *)data;
101         return sizeof(float);
102 }
103
104 static unsigned read_double(double *v, const char *data)
105 {
106         *v = *(double *)data;
107         return sizeof(double);
108 }
109
110 static unsigned read_pointer(void **v, const char *data)
111 {
112         *v = *(void **)data;
113         return sizeof(void *);
114 }
115
116 static unsigned read_data(const void **v, const char *data)
117 {
118         int vlen;
119         unsigned pos = 0;
120         pos += read_int(&vlen, data);
121         if(vlen)
122                 *v = data+pos;
123         else
124                 *v = NULL;
125         return pos+vlen;
126 }
127
128 static unsigned read_string(const unsigned char **v, const char *data)
129 {
130         return read_data((const void **)v, data);
131 }
132
133 #include "gldecoder.funcs"
134
135 static int decode_gldError(GlDecoder *dec, const char *data)
136 {
137         unsigned pos = 0;
138         GLenum code;
139         pos += read_int(&code, data);
140         if(dec->gldError)
141                 dec->gldError(dec->user_data, code);
142         return pos;
143 }
144
145 static int decode_gldfunc(GlDecoder *dec, unsigned short func, const char *data)
146 {
147         switch(func)
148         {
149         case FUNC_GLDERROR: return decode_gldError(dec, data);
150         default: return -1;
151         }
152 }