1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
/* $Id$ */
/*
libg3d - 3D object loading library
Copyright (C) 2005-2009 Markus Dahms <mad@automagically.de>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <string.h>
#include <glib.h>
#include <g3d/stream.h>
#include "imp_ar.h"
#define AR_FLAG_COPIED 0x80
#define AR_FLAG_COMPRESS 0x40
guint8 *ar_decompress_chunk(guint8 *src, guint16 srcsize, guint16 *dstsize)
{
guint8 *dst = NULL, bit = 16;
gint32 i, j = 0, k, pos, size;
guint16 cmd;
#if DEBUG > 2
printf("D: decompressing %d bytes chunk\n", srcsize);
#endif
if(src[0] == AR_FLAG_COPIED)
{
*dstsize = srcsize - 1;
dst = g_new0(guint8, *dstsize);
memcpy(dst, src + 1, *dstsize);
return dst;
}
*dstsize = 0;
cmd = (src[1] << 8) + src[2];
for(i = 3; i < srcsize;)
{
if(bit == 0)
{
/* get new command */
cmd = (src[i] << 8) + src[i + 1];
i += 2;
bit = 16;
}
if(cmd & 0x8000)
{
pos = (src[i] << 4) + (src[i + 1] >> 4);
i ++;
if(pos != 0)
{
/* copy known chunk */
size = (src[i] & 0xF) + 3;
*dstsize += size;
dst = g_realloc(dst, *dstsize);
i ++;
for(k = 0; k < size; k ++)
dst[j + k] = dst[j - pos + k];
j += size;
}
else
{
/* RLE style... */
size = (src[i] << 8) + src[i + 1] + 16;
*dstsize += size;
dst = g_realloc(dst, *dstsize);
i += 2;
for(k = 0; k < size; k ++)
dst[j + k] = src[i];
i ++;
j += size;
}
}
else
{
/* plain copy */
*dstsize += 1;
dst = g_realloc(dst, *dstsize);
dst[j] = src[i];
i ++;
j ++;
}
cmd <<= 1;
bit --;
}
return dst;
}
gboolean ar_decompress_to_file(G3DStream *stream, ArDirEntry *dirent)
{
FILE *o;
gchar cmd;
guint32 size;
guint16 srcsize, dstsize;
guint8 *src, *dst;
o = fopen(dirent->name, "wb");
if(o == NULL) {
g_warning("failed to write to '%s'", dirent->name);
return FALSE;
}
/* seek to file start */
g3d_stream_seek(stream, dirent->offset, G_SEEK_SET);
/* skip tags */
do {
cmd = g3d_stream_read_int8(stream);
if(cmd != 'D') {
size = g3d_stream_read_int32_le(stream);
g3d_stream_skip(stream, size);
}
} while(cmd != 'D');
#if DEBUG > 2
printf("D: starting decompression part\n");
#endif
/* decompress stuff */
while(1) {
srcsize = g3d_stream_read_int16_le(stream);
if(srcsize == 0)
break;
src = g_new0(guint8, srcsize);
g3d_stream_read(stream, src, srcsize);
dst = ar_decompress_chunk(src, srcsize, &dstsize);
if(dstsize > 0) {
fwrite(dst, 1, dstsize, o);
g_free(dst);
}
g_free(src);
}
fclose(o);
return TRUE;
}
|