]> git.tdb.fi Git - gldbg.git/blob - source/gldecoder.c
Packetize the command stream for more robustness
[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 *, short, const char *);
16
17 GlDecoder *gldecoder_new(void *user_data, void (*destroy)(void *))
18 {
19         GlDecoder *dec;
20
21         dec = (GlDecoder *)malloc(sizeof(GlDecoder));
22         memset(dec, 0, sizeof(GlDecoder));
23         dec->user_data = user_data;
24         dec->destroy = destroy;
25
26         return dec;
27 }
28
29 void gldecoder_delete(GlDecoder *dec)
30 {
31         if(dec->destroy)
32                 dec->destroy(dec->user_data);
33         free(dec);
34 }
35
36 int gldecoder_decode(GlDecoder *dec, const char *data, unsigned len)
37 {
38         unsigned pos = 0;
39         int pktlen;
40         short func;
41         int ret;
42
43         if(len<sizeof(int)+sizeof(short))
44                 return -1;
45         pos += read_int(&pktlen, data);
46         if(len<pktlen)
47                 return -1;
48         pos += read_short(&func, data+pos);
49         ret = decode_func(dec, func, data+pos);
50         if(ret<0)
51                 return -1;
52         return pos+ret;
53 }
54
55 static unsigned read_char(char *v, const char *data)
56 {
57         *v = *data;
58         return 1;
59 }
60
61 static unsigned read_short(short *v, const char *data)
62 {
63         *v = *(short *)data;
64         return sizeof(short);
65 }
66
67 static unsigned read_int(int *v, const char *data)
68 {
69         *v = *(int *)data;
70         return sizeof(int);
71 }
72
73 static unsigned read_long(long *v, const char *data)
74 {
75         *v = *(long *)data;
76         return sizeof(long);
77 }
78
79 static unsigned read_ulong(unsigned long *v, const char *data)
80 {
81         *v = *(unsigned long *)data;
82         return sizeof(unsigned long);
83 }
84
85 static unsigned read_longlong(long long *v, const char *data)
86 {
87         *v = *(long long *)data;
88         return sizeof(long long);
89 }
90
91 static unsigned read_float(float *v, const char *data)
92 {
93         *v = *(float *)data;
94         return sizeof(float);
95 }
96
97 static unsigned read_double(double *v, const char *data)
98 {
99         *v = *(double *)data;
100         return sizeof(double);
101 }
102
103 static unsigned read_pointer(void **v, const char *data)
104 {
105         *v = *(void **)data;
106         return sizeof(void *);
107 }
108
109 static unsigned read_data(const void **v, const char *data)
110 {
111         int vlen;
112         unsigned pos = 0;
113         pos += read_int(&vlen, data);
114         if(vlen)
115                 *v = data+pos;
116         else
117                 *v = NULL;
118         return pos+vlen;
119 }
120
121 static unsigned read_string(const unsigned char **v, const char *data)
122 {
123         return read_data((const void **)v, data);
124 }
125
126 #include "gldecoder.funcs"