# $Id$
-CFLAGS=-Igensrc -ggdb
+CFLAGS = -Igensrc -ggdb
all: glwrap.so gldump
out = open(os.path.join(outdir, "gldecoder.funcs"), "w")
for f in funcs:
- out.write("static unsigned decode_%s(GlDecoder *dec, const char *data, unsigned len)\n{\n"%(f[0]))
+ out.write("static unsigned decode_%s(GlDecoder *dec, const char *data)\n{\n"%(f[0]))
out.write("\tunsigned pos = 0;\n")
if f[1]!="void":
out.write("\t%s ret;\n"%f[1])
for p in f[2]:
out.write("\t%s arg_%s;\n"%(p[1], p[0]))
if f[1]!="void":
- out.write("\tpos += read_%s(&ret, data+pos, len);\n"%iomap[f[1]])
+ out.write("\tpos += read_%s(&ret, data+pos);\n"%iomap[f[1]])
for p in f[2]:
(t, c) = getread(f, p)
- out.write("\tpos += read_%s(%s&arg_%s, data+pos, len-pos);\n"%(t, c, p[0]))
+ out.write("\tpos += read_%s(%s&arg_%s, data+pos);\n"%(t, c, p[0]))
out.write("\tif(dec->%s)\n"%f[0])
out.write("\t\tdec->%s(dec->user_data"%f[0])
if f[1]!="void":
out.write("\treturn pos;\n")
out.write("}\n\n")
-out.write("""unsigned gldecoder_decode(GlDecoder *dec, const char *data, unsigned len)
+out.write("""static int decode_func(GlDecoder *dec, short func, const char *data)
{
- unsigned pos = 0;
- short func;
-
- pos += read_short(&func, data, len);
switch(func)
{
""")
for f in funcs:
- out.write("\t\tcase FUNC_%s: pos += decode_%s(dec, data+pos, len); break;\n"%(f[0].upper(), f[0]))
+ out.write("\t\tcase FUNC_%s: return decode_%s(dec, data);\n"%(f[0].upper(), f[0]))
out.write(""" }
- return pos;
+ return -1;
}
""")
out.close()
#include "functions.h"
#include "gldecoder.h"
+static unsigned read_short(short *, const char *);
+static unsigned read_int(int *, const char *);
+static int decode_func(GlDecoder *, short, const char *);
+
GlDecoder *gldecoder_new(void *user_data, void (*destroy)(void *))
{
GlDecoder *dec;
free(dec);
}
-unsigned read_char(char *v, const char *data, unsigned len)
+int gldecoder_decode(GlDecoder *dec, const char *data, unsigned len)
+{
+ unsigned pos = 0;
+ int pktlen;
+ short func;
+ int ret;
+
+ if(len<sizeof(int)+sizeof(short))
+ return -1;
+ pos += read_int(&pktlen, data);
+ if(len<pktlen)
+ return -1;
+ pos += read_short(&func, data+pos);
+ ret = decode_func(dec, func, data+pos);
+ if(ret<0)
+ return -1;
+ return pos+ret;
+}
+
+static unsigned read_char(char *v, const char *data)
{
*v = *data;
return 1;
}
-unsigned read_short(short *v, const char *data, unsigned len)
+static unsigned read_short(short *v, const char *data)
{
*v = *(short *)data;
return sizeof(short);
}
-unsigned read_int(int *v, const char *data, unsigned len)
+static unsigned read_int(int *v, const char *data)
{
*v = *(int *)data;
return sizeof(int);
}
-unsigned read_long(long *v, const char *data, unsigned len)
+static unsigned read_long(long *v, const char *data)
{
*v = *(long *)data;
return sizeof(long);
}
-unsigned read_ulong(unsigned long *v, const char *data, unsigned len)
+static unsigned read_ulong(unsigned long *v, const char *data)
{
*v = *(unsigned long *)data;
return sizeof(unsigned long);
}
-unsigned read_longlong(long long *v, const char *data, unsigned len)
+static unsigned read_longlong(long long *v, const char *data)
{
*v = *(long long *)data;
return sizeof(long long);
}
-unsigned read_float(float *v, const char *data, unsigned len)
+static unsigned read_float(float *v, const char *data)
{
*v = *(float *)data;
return sizeof(float);
}
-unsigned read_double(double *v, const char *data, unsigned len)
+static unsigned read_double(double *v, const char *data)
{
*v = *(double *)data;
return sizeof(double);
}
-unsigned read_pointer(void **v, const char *data, unsigned len)
+static unsigned read_pointer(void **v, const char *data)
{
*v = *(void **)data;
return sizeof(void *);
}
-unsigned read_data(const void **v, const char *data, unsigned len)
+static unsigned read_data(const void **v, const char *data)
{
int vlen;
unsigned pos = 0;
- pos += read_int(&vlen, data, len);
- *v = data+pos;
+ pos += read_int(&vlen, data);
+ if(vlen)
+ *v = data+pos;
+ else
+ *v = NULL;
return pos+vlen;
}
-unsigned read_string(const unsigned char **v, const char *data, unsigned len)
+static unsigned read_string(const unsigned char **v, const char *data)
{
- return read_data((const void **)v, data, len);
+ return read_data((const void **)v, data);
}
#include "gldecoder.funcs"
GlDecoder *gldecoder_new(void *, void (*)(void *));
void gldecoder_delete(GlDecoder *);
-unsigned gldecoder_decode(GlDecoder *, const char *, unsigned);
+int gldecoder_decode(GlDecoder *, const char *, unsigned);
#endif
#include <stdio.h>
#include <stdlib.h>
-#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
#include "glprint.h"
int main(int argc, char **argv)
{
- FILE *in;
+ int fd;
+ struct stat st;
GlDecoder *dec;
- char *buf;
- unsigned size;
- unsigned start;
- unsigned end;
+ char *addr;
+ char *end;
+ char *ptr;
- in = fopen(argv[1], "r");
+ if(argc<2)
+ {
+ fprintf(stderr, "Usage: %s <dumpfile>\n", argv[0]);
+ return 1;
+ }
+
+ fd = open(argv[1], O_RDONLY);
+ if(fd==-1)
+ {
+ perror("open");
+ return 1;
+ }
+ fstat(fd, &st);
+ addr = (char *)mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
+ end = addr+st.st_size;
+ ptr = addr;
dec = glprint_new(NULL, 0);
- size = 16384;
- buf = (char *)malloc(size);
- start = 0;
- end = 0;
- while(1)
+ while(ptr<end)
{
- if(start>size/2)
+ int ret;
+ ret = gldecoder_decode(dec, ptr, end-ptr);
+ if(ret<0)
{
- memmove(buf, buf+start, end-start);
- end -= start;
- start = 0;
- }
- if(end<size && !feof(in))
- end += fread(buf+end, 1, size-end, in);
- else if(start==end)
+ fprintf(stderr, "Decode error\n");
break;
- start += gldecoder_decode(dec, buf+start, end-start);
+ }
+ ptr += ret;
printf("%s\n", glprint_get_buffer(dec));
}
- fclose(in);
- free(buf);
+ munmap(addr, st.st_size);
+ close(fd);
gldecoder_delete(dec);
return 0;
static inline void *glsym(const char *sym)
{
- static void *libgl=NULL;
+ static void *libgl = NULL;
if(!libgl)
{
- libgl=dlopen("libGL.so", RTLD_NOW);
+ libgl = dlopen("libGL.so", RTLD_NOW);
if(!libgl)
{
fprintf(stderr, "Could not open libGL: %s\n", dlerror());
return dlsym(libgl, sym);
}
-char *buffer=0;
-char *write_pos;
-struct iovec *iovecs=0;
-struct iovec *cur_vec;
+static char *buffer = 0;
+static char *write_pos;
+static struct iovec *iovecs = 0;
+static struct iovec *cur_vec;
+static unsigned length;
static inline void next_vec()
{
if(write_pos!=cur_vec->iov_base)
{
- cur_vec->iov_len=write_pos-(char *)cur_vec->iov_base;
+ cur_vec->iov_len = write_pos-(char *)cur_vec->iov_base;
+ length += cur_vec->iov_len;
++cur_vec;
- cur_vec->iov_base=write_pos;
+ cur_vec->iov_base = write_pos;
}
}
{
unsigned i;
for(i=0; i<size; ++i)
- *write_pos++=*ptr++;
+ *write_pos++ = *ptr++;
}
static inline void write_char(char v)
{
- *write_pos++=v;
+ *write_pos++ = v;
}
static inline void write_short(short v)
write_bytes((char *)&v, sizeof(float));
}
-static inline void write_double(float v)
+static inline void write_double(double v)
{
write_bytes((char *)&v, sizeof(double));
}
static inline void write_data(const void *data, unsigned size)
{
- write_int(size);
- next_vec();
- cur_vec->iov_base=(void *)data;
- cur_vec->iov_len=size;
- ++cur_vec;
- cur_vec->iov_base=write_pos;
+ if(data)
+ {
+ write_int(size);
+ next_vec();
+ cur_vec->iov_base = (void *)data;
+ cur_vec->iov_len = size;
+ length += size;
+ ++cur_vec;
+ cur_vec->iov_base = write_pos;
+ }
+ else
+ write_int(0);
}
static inline void write_string(const unsigned char *s)
{
write_data(s, strlen(s));
- /*int len=strlen(s);
- write_int(len);
- write_bytes(s, len);*/
}
static inline void begin_packet(int func)
{
if(!buffer)
- buffer=(char *)malloc(1024);
+ buffer = (char *)malloc(1024);
if(!iovecs)
- iovecs=(struct iovec *)malloc(16*sizeof(struct iovec));
- write_pos=buffer;
- cur_vec=iovecs;
- cur_vec->iov_base=write_pos;
+ iovecs = (struct iovec *)malloc(16*sizeof(struct iovec));
+ write_pos = buffer;
+ cur_vec = iovecs;
+ cur_vec->iov_base = write_pos;
+ length = 0;
+ write_int(0);
write_short(func);
}
static inline void send_packet()
{
- static int fd=-1;
+ static int fd = -1;
if(fd<0)
{
- const char *var=getenv("GLWRAP_FD");
+ const char *var = getenv("GLWRAP_FD");
if(var)
- fd=strtol(var, NULL, 0);
+ fd = strtol(var, NULL, 0);
else
- fd=2;
+ fd = 2;
}
next_vec();
+ write_pos = buffer;
+ write_int(length);
writev(fd, iovecs, cur_vec-iovecs);
}