2007-11-25 19:42:40 +00:00
|
|
|
/* copyright (c) 2007 magnus auvinen, see licence.txt for more info */
|
2008-02-24 16:03:58 +00:00
|
|
|
#include <stdlib.h>
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2008-02-11 22:25:10 +00:00
|
|
|
#include "e_system.h"
|
2007-12-15 10:24:49 +00:00
|
|
|
#include "e_packer.h"
|
|
|
|
#include "e_compression.h"
|
2008-02-24 16:03:58 +00:00
|
|
|
#include "e_engine.h"
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2008-02-11 22:25:10 +00:00
|
|
|
/* useful for debugging */
|
2008-02-24 16:03:58 +00:00
|
|
|
#if 0
|
|
|
|
#define packing_error(p) p->error = 1; dbg_break()
|
|
|
|
#else
|
|
|
|
#define packing_error(p) p->error = 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int stress_get_int()
|
|
|
|
{
|
|
|
|
static const int nasty[] = {-1, 0, 1, 66000, -66000, (-1<<31), 0x7fffffff};
|
|
|
|
if(rand()&1)
|
|
|
|
return rand();
|
|
|
|
return nasty[rand()%6];
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *stress_get_string(int *size)
|
|
|
|
{
|
|
|
|
static char noise[1024];
|
|
|
|
int i;
|
|
|
|
int s;
|
|
|
|
s = (rand()%1024)-1;
|
|
|
|
for(i = 0; i < s; i++)
|
|
|
|
noise[i] = (rand()%254)+1;
|
|
|
|
noise[s] = 0;
|
|
|
|
if(size)
|
|
|
|
*size = s;
|
|
|
|
return noise;
|
|
|
|
}
|
2008-02-11 22:25:10 +00:00
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
void packer_reset(PACKER *p)
|
|
|
|
{
|
|
|
|
p->error = 0;
|
|
|
|
p->current = p->buffer;
|
|
|
|
p->end = p->current + PACKER_BUFFER_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void packer_add_int(PACKER *p, int i)
|
|
|
|
{
|
2008-02-11 22:25:10 +00:00
|
|
|
if(p->error)
|
|
|
|
return;
|
2008-02-24 16:03:58 +00:00
|
|
|
|
|
|
|
/*if(engine_stress(0.05f))
|
|
|
|
i = stress_get_int();*/
|
2008-02-11 22:25:10 +00:00
|
|
|
|
|
|
|
/* make sure that we have space enough */
|
|
|
|
if(p->end - p->current < 6)
|
|
|
|
{
|
|
|
|
dbg_break();
|
|
|
|
p->error = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
p->current = vint_pack(p->current, i);
|
2007-08-22 07:52:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void packer_add_string(PACKER *p, const char *str, int limit)
|
|
|
|
{
|
2008-02-11 22:25:10 +00:00
|
|
|
if(p->error)
|
|
|
|
return;
|
|
|
|
|
2008-02-24 16:03:58 +00:00
|
|
|
/* STRESS: do this better */
|
|
|
|
/*
|
|
|
|
if(engine_stress(0.1f))
|
|
|
|
{
|
|
|
|
str = stress_get_string(0);
|
|
|
|
limit = 0;
|
|
|
|
}*/
|
|
|
|
|
|
|
|
/* */
|
2007-08-22 07:52:33 +00:00
|
|
|
if(limit > 0)
|
|
|
|
{
|
|
|
|
while(*str && limit != 0)
|
|
|
|
{
|
|
|
|
*p->current++ = *str++;
|
|
|
|
limit--;
|
2008-02-11 22:25:10 +00:00
|
|
|
|
|
|
|
if(p->current >= p->end)
|
|
|
|
{
|
|
|
|
packing_error(p);
|
|
|
|
break;
|
|
|
|
}
|
2007-08-22 07:52:33 +00:00
|
|
|
}
|
|
|
|
*p->current++ = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while(*str)
|
2008-02-11 22:25:10 +00:00
|
|
|
{
|
2007-08-22 07:52:33 +00:00
|
|
|
*p->current++ = *str++;
|
2008-02-11 22:25:10 +00:00
|
|
|
|
|
|
|
if(p->current >= p->end)
|
|
|
|
{
|
|
|
|
packing_error(p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-08-22 07:52:33 +00:00
|
|
|
*p->current++ = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void packer_add_raw(PACKER *p, const unsigned char *data, int size)
|
|
|
|
{
|
2008-02-11 22:25:10 +00:00
|
|
|
if(p->error)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(p->current+size >= p->end)
|
|
|
|
{
|
|
|
|
packing_error(p);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
while(size)
|
|
|
|
{
|
|
|
|
*p->current++ = *data++;
|
|
|
|
size--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int packer_size(PACKER *p)
|
|
|
|
{
|
|
|
|
return (const unsigned char *)p->current-(const unsigned char *)p->buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
const unsigned char *packer_data(PACKER *p)
|
|
|
|
{
|
|
|
|
return (const unsigned char *)p->buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void unpacker_reset(UNPACKER *p, const unsigned char *data, int size)
|
|
|
|
{
|
|
|
|
p->error = 0;
|
|
|
|
p->start = data;
|
|
|
|
p->end = p->start + size;
|
|
|
|
p->current = p->start;
|
|
|
|
}
|
|
|
|
|
|
|
|
int unpacker_get_int(UNPACKER *p)
|
|
|
|
{
|
|
|
|
int i;
|
2008-02-24 16:03:58 +00:00
|
|
|
if(p->error)
|
2007-08-22 07:52:33 +00:00
|
|
|
return 0;
|
2008-02-24 16:03:58 +00:00
|
|
|
if(p->current >= p->end)
|
|
|
|
{
|
|
|
|
packing_error(p);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
p->current = vint_unpack(p->current, &i);
|
2008-02-11 22:25:10 +00:00
|
|
|
if(p->current > p->end)
|
|
|
|
{
|
|
|
|
packing_error(p);
|
|
|
|
return 0;
|
|
|
|
}
|
2007-08-22 07:52:33 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *unpacker_get_string(UNPACKER *p)
|
|
|
|
{
|
2008-02-24 16:03:58 +00:00
|
|
|
char *ptr;
|
2008-02-11 22:25:10 +00:00
|
|
|
if(p->error || p->current >= p->end)
|
2007-08-22 07:52:33 +00:00
|
|
|
return "";
|
|
|
|
|
2008-02-24 16:03:58 +00:00
|
|
|
ptr = (char *)p->current;
|
2007-08-22 07:52:33 +00:00
|
|
|
while(*p->current) /* skip the string */
|
2008-02-11 22:25:10 +00:00
|
|
|
{
|
2007-08-22 07:52:33 +00:00
|
|
|
p->current++;
|
2008-02-11 22:25:10 +00:00
|
|
|
if(p->current == p->end)
|
|
|
|
{
|
|
|
|
packing_error(p);
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
2007-08-22 07:52:33 +00:00
|
|
|
p->current++;
|
2008-02-24 16:03:58 +00:00
|
|
|
|
|
|
|
/* sanitize all strings */
|
|
|
|
str_sanitize(ptr);
|
2007-08-22 07:52:33 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const unsigned char *unpacker_get_raw(UNPACKER *p, int size)
|
|
|
|
{
|
|
|
|
const unsigned char *ptr = p->current;
|
2008-02-24 16:03:58 +00:00
|
|
|
if(p->error)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* check for nasty sizes */
|
|
|
|
if(size < 0 || p->current+size > p->end)
|
2008-02-11 22:25:10 +00:00
|
|
|
{
|
|
|
|
packing_error(p);
|
|
|
|
return 0;
|
|
|
|
}
|
2008-02-24 16:03:58 +00:00
|
|
|
|
|
|
|
/* "unpack" the data */
|
|
|
|
p->current += size;
|
2007-08-22 07:52:33 +00:00
|
|
|
return ptr;
|
|
|
|
}
|