]> git.tdb.fi Git - gldbg.git/blob - source/gldecoder.c
Enhance the object-orientedness of genwrap.py
[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         if(dec)
50         {
51                 ret = decode_func(dec, func, data+pos);
52                 if(ret<0)
53                         return -1;
54         }
55         return pktlen;
56 }
57
58 static unsigned read_char(char *v, const char *data)
59 {
60         *v = *data;
61         return 1;
62 }
63
64 static unsigned read_short(short *v, const char *data)
65 {
66         *v = *(short *)data;
67         return sizeof(short);
68 }
69
70 static unsigned read_int(int *v, const char *data)
71 {
72         *v = *(int *)data;
73         return sizeof(int);
74 }
75
76 static unsigned read_long(long *v, const char *data)
77 {
78         *v = *(long *)data;
79         return sizeof(long);
80 }
81
82 static unsigned read_ulong(unsigned long *v, const char *data)
83 {
84         *v = *(unsigned long *)data;
85         return sizeof(unsigned long);
86 }
87
88 static unsigned read_longlong(long long *v, const char *data)
89 {
90         *v = *(long long *)data;
91         return sizeof(long long);
92 }
93
94 static unsigned read_float(float *v, const char *data)
95 {
96         *v = *(float *)data;
97         return sizeof(float);
98 }
99
100 static unsigned read_double(double *v, const char *data)
101 {
102         *v = *(double *)data;
103         return sizeof(double);
104 }
105
106 static unsigned read_pointer(void **v, const char *data)
107 {
108         *v = *(void **)data;
109         return sizeof(void *);
110 }
111
112 static unsigned read_data(const void **v, const char *data)
113 {
114         int vlen;
115         unsigned pos = 0;
116         pos += read_int(&vlen, data);
117         if(vlen)
118                 *v = data+pos;
119         else
120                 *v = NULL;
121         return pos+vlen;
122 }
123
124 static unsigned read_string(const unsigned char **v, const char *data)
125 {
126         return read_data((const void **)v, data);
127 }
128
129 #include "gldecoder.funcs"