| 1 |
/*--[lzxglue.c]---------------------------------------------------------------- |
| 2 |
| Copyright (C) 2004 DRS |
| 3 |
| |
| 4 |
| This file is part of the "openclit" library for processing .LIT files. |
| 5 |
| |
| 6 |
| "Openclit" is free software; you can redistribute it and/or modify |
| 7 |
| it under the terms of the GNU General Public License as published by |
| 8 |
| the Free Software Foundation; either version 2 of the License, or |
| 9 |
| (at your option) any later version. |
| 10 |
| |
| 11 |
| This program is distributed in the hope that it will be useful, |
| 12 |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
| GNU General Public License for more details. |
| 15 |
| |
| 16 |
| You should have received a copy of the GNU General Public License |
| 17 |
| along with this program; if not, write to the Free Software |
| 18 |
| Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 |
| |
| 20 |
| The GNU General Public License may also be available at the following |
| 21 |
| URL: http://www.gnu.org/licenses/gpl.html |
| 22 |
*/ |
| 23 |
|
| 24 |
/* This provides a "glue" between Stuart Caie's libmspack library and the |
| 25 |
* Openclit calls to the earlier LZX library. |
| 26 |
* |
| 27 |
* This way, I should be able to use the files unmodified. |
| 28 |
*/ |
| 29 |
#include <stdio.h> |
| 30 |
#include <stdlib.h> |
| 31 |
#include "litlib.h" |
| 32 |
#include "mspack.h" |
| 33 |
#include "lzx.h" |
| 34 |
|
| 35 |
typedef struct memory_file |
| 36 |
{ |
| 37 |
unsigned int magic; /* 0xB5 */ |
| 38 |
void * buffer; |
| 39 |
int total_bytes; |
| 40 |
int current_bytes; |
| 41 |
} memory_file; |
| 42 |
|
| 43 |
|
| 44 |
void * glue_alloc(struct mspack_system *this, size_t bytes) |
| 45 |
{ |
| 46 |
void * p; |
| 47 |
p = (void *)malloc(bytes); |
| 48 |
if (p == NULL) { |
| 49 |
lit_error(ERR_R|ERR_LIBC,"Malloc(%d) failed!", bytes); |
| 50 |
} |
| 51 |
return p; |
| 52 |
} |
| 53 |
|
| 54 |
void glue_free(void * p) |
| 55 |
{ |
| 56 |
free(p); |
| 57 |
} |
| 58 |
|
| 59 |
void glue_copy(void *src, void *dest, size_t bytes) |
| 60 |
{ |
| 61 |
memcpy(dest, src, bytes); |
| 62 |
} |
| 63 |
|
| 64 |
struct mspack_file * glue_open(struct mspack_system *this, char *filename, |
| 65 |
int mode) |
| 66 |
{ |
| 67 |
lit_error(0,"MSPACK_OPEN unsupported!"); |
| 68 |
return NULL; |
| 69 |
} |
| 70 |
|
| 71 |
void glue_close(struct mspack_file * file) { |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
int glue_read(struct mspack_file * file, void * buffer, int bytes) |
| 77 |
{ |
| 78 |
memory_file * mem; |
| 79 |
int remaining; |
| 80 |
|
| 81 |
mem = (memory_file *)file; |
| 82 |
if (mem->magic != 0xB5) return -1; |
| 83 |
|
| 84 |
remaining = mem->total_bytes - mem->current_bytes; |
| 85 |
if (!remaining) return 0; |
| 86 |
if (bytes > remaining) bytes = remaining; |
| 87 |
memcpy(buffer, (unsigned char *)mem->buffer+mem->current_bytes, bytes); |
| 88 |
mem->current_bytes += bytes; |
| 89 |
return bytes; |
| 90 |
} |
| 91 |
|
| 92 |
int glue_write(struct mspack_file * file, void * buffer, int bytes) |
| 93 |
{ |
| 94 |
memory_file * mem; |
| 95 |
int remaining; |
| 96 |
|
| 97 |
mem = (memory_file *)file; |
| 98 |
if (mem->magic != 0xB5) return -1; |
| 99 |
|
| 100 |
remaining = mem->total_bytes - mem->current_bytes; |
| 101 |
if (!remaining) return 0; |
| 102 |
if (bytes > remaining) { |
| 103 |
lit_error(0,"MSPACK_READ tried to write %d bytes, only %d left.", |
| 104 |
bytes, remaining); |
| 105 |
bytes = remaining; |
| 106 |
} |
| 107 |
memcpy((unsigned char *)mem->buffer+mem->current_bytes, buffer, bytes); |
| 108 |
mem->current_bytes += bytes; |
| 109 |
return bytes; |
| 110 |
} |
| 111 |
|
| 112 |
struct mspack_system lzxglue_system = |
| 113 |
{ |
| 114 |
glue_open, |
| 115 |
glue_close, |
| 116 |
glue_read, /* Read */ |
| 117 |
glue_write, /* Write */ |
| 118 |
NULL, /* Seek */ |
| 119 |
NULL, /* Tell */ |
| 120 |
NULL, /* Message */ |
| 121 |
glue_alloc, |
| 122 |
glue_free, |
| 123 |
glue_copy, |
| 124 |
NULL /* Termination */ |
| 125 |
}; |
| 126 |
|
| 127 |
int LZXwindow; |
| 128 |
struct lzxd_stream * lzx_stream = NULL; |
| 129 |
|
| 130 |
|
| 131 |
/* Can't really init here,don't know enough */ |
| 132 |
int LZXinit(int window) |
| 133 |
{ |
| 134 |
LZXwindow = window; |
| 135 |
lzx_stream = NULL; |
| 136 |
|
| 137 |
return 0; |
| 138 |
} |
| 139 |
|
| 140 |
/* Doesn't exist. Oh well, reinitialize state every time anyway */ |
| 141 |
void LZXreset(void) |
| 142 |
{ |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
int LZXdecompress(unsigned char *inbuf, unsigned char *outbuf, |
| 147 |
unsigned int inlen, unsigned int outlen) |
| 148 |
{ |
| 149 |
int err; |
| 150 |
memory_file source; |
| 151 |
memory_file dest; |
| 152 |
|
| 153 |
source.magic = 0xB5; |
| 154 |
source.buffer = inbuf; |
| 155 |
source.current_bytes = 0; |
| 156 |
source.total_bytes = inlen; |
| 157 |
|
| 158 |
dest.magic = 0xB5; |
| 159 |
dest.buffer = outbuf; |
| 160 |
dest.current_bytes = 0; |
| 161 |
dest.total_bytes = outlen; |
| 162 |
|
| 163 |
lzx_stream = lzxd_init(&lzxglue_system, (struct mspack_file *)&source, |
| 164 |
(struct mspack_file *)&dest, LZXwindow, |
| 165 |
0x7fff /* Never reset, I do it */, 4096, outlen); |
| 166 |
err = -1; |
| 167 |
if (lzx_stream) err = lzxd_decompress(lzx_stream, outlen); |
| 168 |
|
| 169 |
lzxd_free(lzx_stream); |
| 170 |
lzx_stream = NULL; |
| 171 |
return err; |
| 172 |
} |