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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
/*
Copyright (c) 2011, 2012, Simon Howard
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include "crc16.h"
#include "lha_decoder.h"
// Null decoder, used for -lz4-, -lh0-, -pm0-:
extern const LHADecoderType lha_null_decoder;
// LArc compression algorithms:
extern const LHADecoderType lha_lz5_decoder;
extern const LHADecoderType lha_lzs_decoder;
// LHarc compression algorithms:
extern const LHADecoderType lha_lh1_decoder;
extern const LHADecoderType lha_lh4_decoder;
extern const LHADecoderType lha_lh5_decoder;
extern const LHADecoderType lha_lh6_decoder;
extern const LHADecoderType lha_lh7_decoder;
extern const LHADecoderType lha_lhx_decoder;
extern const LHADecoderType lha_lk7_decoder;
// PMarc compression algorithms:
extern const LHADecoderType lha_pm1_decoder;
extern const LHADecoderType lha_pm2_decoder;
static const struct {
const char *name;
const LHADecoderType *dtype;
} decoders[] = {
{ "-lz4-", &lha_null_decoder },
{ "-lz5-", &lha_lz5_decoder },
{ "-lzs-", &lha_lzs_decoder },
{ "-lh0-", &lha_null_decoder },
{ "-lh1-", &lha_lh1_decoder },
{ "-lh4-", &lha_lh4_decoder },
{ "-lh5-", &lha_lh5_decoder },
{ "-lh6-", &lha_lh6_decoder },
{ "-lh7-", &lha_lh7_decoder },
{ "-lhx-", &lha_lhx_decoder },
{ "-lk7-", &lha_lk7_decoder },
{ "-pm0-", &lha_null_decoder },
{ "-pm1-", &lha_pm1_decoder },
{ "-pm2-", &lha_pm2_decoder },
};
#undef lha_decoder_new
// The "actual" lha_decoder_new; code gets #define-renamed to use this.
LHADecoder *lha_decoder_new64(const LHADecoderType *dtype,
LHADecoderCallback callback,
void *callback_data,
uint64_t stream_length)
{
LHADecoder *decoder;
void *extra_data;
// Space is allocated together: the LHADecoder structure,
// then the private data area used by the algorithm,
// followed by the output buffer,
decoder = calloc(1, sizeof(LHADecoder) + dtype->extra_size
+ dtype->max_read);
if (decoder == NULL) {
return NULL;
}
decoder->dtype = dtype;
decoder->progress_callback = NULL;
decoder->last_block = UINT_MAX;
decoder->outbuf_pos = 0;
decoder->outbuf_len = 0;
decoder->stream_pos = 0;
decoder->stream_length = stream_length;
decoder->decoder_failed = 0;
decoder->crc = 0;
// Private data area follows the structure.
extra_data = decoder + 1;
decoder->outbuf = ((uint8_t *) extra_data) + dtype->extra_size;
if (dtype->init != NULL
&& !dtype->init(extra_data, callback, callback_data)) {
free(decoder);
return NULL;
}
return decoder;
}
// This is the old version of lha_decoder_new, retained for ABI
// compatibility purposes.
LHADecoder *lha_decoder_new(const LHADecoderType *dtype,
LHADecoderCallback callback,
void *callback_data,
size_t stream_length)
{
return lha_decoder_new64(dtype, callback, callback_data,
stream_length);
}
const LHADecoderType *lha_decoder_for_name(const char *name)
{
unsigned int i;
for (i = 0; i < sizeof(decoders) / sizeof(*decoders); ++i) {
if (!strcmp(name, decoders[i].name)) {
return decoders[i].dtype;
}
}
// Unknown?
return NULL;
}
void lha_decoder_free(LHADecoder *decoder)
{
if (decoder->dtype->free != NULL) {
decoder->dtype->free(decoder + 1);
}
free(decoder);
}
// Check if the stream has progressed far enough that the progress callback
// should be invoked again.
static void check_progress_callback(LHADecoder *decoder)
{
unsigned int block;
block = (decoder->stream_pos + decoder->block_size - 1)
/ decoder->block_size;
// If the stream has advanced by another block, invoke the callback
// function. Invoke it multiple times if it has advanced by
// more than one block.
while (decoder->last_block != block) {
++decoder->last_block;
decoder->progress_callback(decoder->last_block,
decoder->total_blocks,
decoder->progress_callback_data);
}
}
void lha_decoder_monitor(LHADecoder *decoder,
LHADecoderProgressCallback callback,
void *callback_data)
{
decoder->progress_callback = callback;
decoder->progress_callback_data = callback_data;
// Usually, the block size we pass to the callback function is just
// the block size from the codec. However, for huge file sizes (100s
// of megabytes) we scale the block size up; this limits the number of
// blocks that we report to 128K. The reasons here are twofold:
// * Progress reporting shouldn't require any more detail than that
// anyway; for a file gigabytes in size we don't need to report on
// every single 4KiB block.
// * It ensures the block counts never overflow the 32-bit limit,
// without needing an ABI change for the callback interface.
decoder->block_size = decoder->dtype->block_size;
while (decoder->stream_length / (128 * 1024) > decoder->block_size) {
decoder->block_size <<= 1;
}
decoder->total_blocks
= (decoder->stream_length + decoder->block_size - 1)
/ decoder->block_size;
check_progress_callback(decoder);
}
size_t lha_decoder_read(LHADecoder *decoder, uint8_t *buf, size_t buf_len)
{
size_t filled, bytes;
// When we reach the end of the stream, we must truncate the
// decompressed data at exactly the right point (stream_length),
// or we may read a few extra false byte(s) by mistake.
// Reduce buf_len when we get to the end to limit it to the
// real number of remaining characters.
if (decoder->stream_pos + buf_len > decoder->stream_length) {
buf_len = decoder->stream_length - decoder->stream_pos;
}
// Try to fill up the buffer that has been passed with as much
// data as possible. Each call to read() will fill up outbuf
// with some data; this is then copied into buf, with some
// data left at the end for the next call.
filled = 0;
while (filled < buf_len) {
// Try to empty out some of the output buffer first.
bytes = decoder->outbuf_len - decoder->outbuf_pos;
if (buf_len - filled < bytes) {
bytes = buf_len - filled;
}
memcpy(buf + filled, decoder->outbuf + decoder->outbuf_pos,
bytes);
decoder->outbuf_pos += bytes;
filled += bytes;
// If we previously encountered a failure reading from
// the decoder, don't try to call the read function again.
if (decoder->decoder_failed) {
break;
}
// If outbuf is now empty, we can process another run to
// re-fill it.
if (decoder->outbuf_pos >= decoder->outbuf_len) {
decoder->outbuf_len
= decoder->dtype->read(decoder + 1,
decoder->outbuf);
decoder->outbuf_pos = 0;
}
// No more data to be read?
if (decoder->outbuf_len == 0) {
decoder->decoder_failed = 1;
break;
}
}
// Update CRC.
lha_crc16_buf(&decoder->crc, buf, filled);
// Track stream position.
decoder->stream_pos += filled;
// Check progress callback, if one is set:
if (decoder->progress_callback != NULL) {
check_progress_callback(decoder);
}
return filled;
}
uint16_t lha_decoder_get_crc(LHADecoder *decoder)
{
return decoder->crc;
}
#undef lha_decoder_get_length
// This is the old version of lha_decoder_get_length, retained for ABI
// compatibility purposes.
size_t lha_decoder_get_length(LHADecoder *decoder)
{
if (decoder->stream_pos > SIZE_MAX) {
return SIZE_MAX;
}
return decoder->stream_pos;
}
// The "actual" lha_decoder_get_length; code gets #define-renamed to use this.
uint64_t lha_decoder_get_length64(LHADecoder *decoder)
{
return decoder->stream_pos;
}
|