tif_lzma.c 14.6 KB
Newer Older
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
/* $Id: tif_lzma.c,v 1.4 2011-12-22 00:29:29 bfriesen Exp $ */

/*
 * Copyright (c) 2010, Andrey Kiselev <dron@ak4719.spb.edu>
 *
 * Permission to use, copy, modify, distribute, and sell this software and
 * its documentation for any purpose is hereby granted without fee, provided
 * that (i) the above copyright notices and this permission notice appear in
 * all copies of the software and related documentation, and (ii) the names of
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
 * publicity relating to the software without the specific, prior written
 * permission of Sam Leffler and Silicon Graphics.
 *
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
 *
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THIS SOFTWARE.
 */

#include "tiffiop.h"
#ifdef LZMA_SUPPORT
/*
 * TIFF Library.
 *
 * LZMA2 Compression Support
 *
 * You need an LZMA2 SDK to link with. See http://tukaani.org/xz/ for details.
 *
 * The codec is derived from ZLIB codec (tif_zip.c).
 */

#include "tif_predict.h"
#include "lzma.h"

#include <stdio.h>

/*
 * State block for each open TIFF file using LZMA2 compression/decompression.
 */
typedef struct {
47
    TIFFPredictorState predict;
48
        lzma_stream	stream;
49 50 51 52 53 54
    lzma_filter	filters[LZMA_FILTERS_MAX + 1];
    lzma_options_delta opt_delta;		/* delta filter options */
    lzma_options_lzma opt_lzma;		/* LZMA2 filter options */
    int             preset;			/* compression level */
    lzma_check	check;			/* type of the integrity check */
    int             state;			/* state flags */
55 56 57
#define LSTATE_INIT_DECODE 0x01
#define LSTATE_INIT_ENCODE 0x02

58 59
    TIFFVGetMethod  vgetparent;            /* super-class method */
    TIFFVSetMethod  vsetparent;            /* super-class method */
60 61 62 63 64 65 66 67 68 69 70 71
} LZMAState;

#define LState(tif)             ((LZMAState*) (tif)->tif_data)
#define DecoderState(tif)       LState(tif)
#define EncoderState(tif)       LState(tif)

static int LZMAEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s);
static int LZMADecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s);

static const char *
LZMAStrerror(lzma_ret ret)
{
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
    switch (ret) {
        case LZMA_OK:
            return "operation completed successfully";
        case LZMA_STREAM_END:
            return "end of stream was reached";
        case LZMA_NO_CHECK:
            return "input stream has no integrity check";
        case LZMA_UNSUPPORTED_CHECK:
            return "cannot calculate the integrity check";
        case LZMA_GET_CHECK:
            return "integrity check type is now available";
        case LZMA_MEM_ERROR:
            return "cannot allocate memory";
        case LZMA_MEMLIMIT_ERROR:
            return "memory usage limit was reached";
        case LZMA_FORMAT_ERROR:
            return "file format not recognized";
        case LZMA_OPTIONS_ERROR:
            return "invalid or unsupported options";
        case LZMA_DATA_ERROR:
            return "data is corrupt";
        case LZMA_BUF_ERROR:
            return "no progress is possible (stream is truncated or corrupt)";
        case LZMA_PROG_ERROR:
            return "programming error";
        default:
            return "unindentified liblzma error";
    }
100 101 102 103 104
}

static int
LZMAFixupTags(TIFF* tif)
{
105 106
    (void) tif;
    return 1;
107 108 109 110 111
}

static int
LZMASetupDecode(TIFF* tif)
{
112 113 114
    LZMAState* sp = DecoderState(tif);

    assert(sp != NULL);
115 116

        /* if we were last encoding, terminate this mode */
117 118 119 120
    if (sp->state & LSTATE_INIT_ENCODE) {
        lzma_end(&sp->stream);
        sp->state = 0;
    }
121

122 123
    sp->state |= LSTATE_INIT_DECODE;
    return 1;
124 125 126 127 128 129 130 131
}

/*
 * Setup state for decoding a strip.
 */
static int
LZMAPreDecode(TIFF* tif, uint16 s)
{
132 133 134
    static const char module[] = "LZMAPreDecode";
    LZMAState* sp = DecoderState(tif);
    lzma_ret ret;
135

136 137
    (void) s;
    assert(sp != NULL);
138

139
    if( (sp->state & LSTATE_INIT_DECODE) == 0 )
140 141
            tif->tif_setupdecode(tif);

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
    sp->stream.next_in = tif->tif_rawdata;
    sp->stream.avail_in = (size_t) tif->tif_rawcc;
    if ((tmsize_t)sp->stream.avail_in != tif->tif_rawcc) {
        TIFFErrorExt(tif->tif_clientdata, module,
                 "Liblzma cannot deal with buffers this size");
        return 0;
    }

    /*
     * Disable memory limit when decoding. UINT64_MAX is a flag to disable
     * the limit, we are passing (uint64_t)-1 which should be the same.
     */
    ret = lzma_stream_decoder(&sp->stream, (uint64_t)-1, 0);
    if (ret != LZMA_OK) {
        TIFFErrorExt(tif->tif_clientdata, module,
                 "Error initializing the stream decoder, %s",
                 LZMAStrerror(ret));
        return 0;
    }
    return 1;
162 163 164 165 166
}

static int
LZMADecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
{
167 168
    static const char module[] = "LZMADecode";
    LZMAState* sp = DecoderState(tif);
169

170 171 172
    (void) s;
    assert(sp != NULL);
    assert(sp->state == LSTATE_INIT_DECODE);
173 174 175 176

        sp->stream.next_in = tif->tif_rawcp;
        sp->stream.avail_in = (size_t) tif->tif_rawcc;

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
    sp->stream.next_out = op;
    sp->stream.avail_out = (size_t) occ;
    if ((tmsize_t)sp->stream.avail_out != occ) {
        TIFFErrorExt(tif->tif_clientdata, module,
                 "Liblzma cannot deal with buffers this size");
        return 0;
    }

    do {
        /*
         * Save the current stream state to properly recover from the
         * decoding errors later.
         */
        const uint8_t *next_in = sp->stream.next_in;
        size_t avail_in = sp->stream.avail_in;

        lzma_ret ret = lzma_code(&sp->stream, LZMA_RUN);
        if (ret == LZMA_STREAM_END)
            break;
        if (ret == LZMA_MEMLIMIT_ERROR) {
            lzma_ret r = lzma_stream_decoder(&sp->stream,
                             lzma_memusage(&sp->stream), 0);
            if (r != LZMA_OK) {
                TIFFErrorExt(tif->tif_clientdata, module,
                         "Error initializing the stream decoder, %s",
                         LZMAStrerror(r));
                break;
            }
            sp->stream.next_in = next_in;
            sp->stream.avail_in = avail_in;
            continue;
        }
        if (ret != LZMA_OK) {
            TIFFErrorExt(tif->tif_clientdata, module,
                "Decoding error at scanline %lu, %s",
                (unsigned long) tif->tif_row, LZMAStrerror(ret));
            break;
        }
    } while (sp->stream.avail_out > 0);
    if (sp->stream.avail_out != 0) {
        TIFFErrorExt(tif->tif_clientdata, module,
            "Not enough data at scanline %lu (short %lu bytes)",
            (unsigned long) tif->tif_row, (unsigned long) sp->stream.avail_out);
        return 0;
    }
222 223 224

        tif->tif_rawcp = (uint8 *)sp->stream.next_in; /* cast away const */
        tif->tif_rawcc = sp->stream.avail_in;
225 226

    return 1;
227 228 229 230 231
}

static int
LZMASetupEncode(TIFF* tif)
{
232
    LZMAState* sp = EncoderState(tif);
233

234 235 236 237 238
    assert(sp != NULL);
    if (sp->state & LSTATE_INIT_DECODE) {
        lzma_end(&sp->stream);
        sp->state = 0;
    }
239

240 241
    sp->state |= LSTATE_INIT_ENCODE;
    return 1;
242 243 244 245 246 247 248 249
}

/*
 * Reset encoding state at the start of a strip.
 */
static int
LZMAPreEncode(TIFF* tif, uint16 s)
{
250 251
    static const char module[] = "LZMAPreEncode";
    LZMAState *sp = EncoderState(tif);
252

253 254 255
    (void) s;
    assert(sp != NULL);
    if( sp->state != LSTATE_INIT_ENCODE )
256 257
            tif->tif_setupencode(tif);

258 259 260 261 262 263 264 265
    sp->stream.next_out = tif->tif_rawdata;
    sp->stream.avail_out = (size_t)tif->tif_rawdatasize;
    if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize) {
        TIFFErrorExt(tif->tif_clientdata, module,
                 "Liblzma cannot deal with buffers this size");
        return 0;
    }
    return (lzma_stream_encoder(&sp->stream, sp->filters, sp->check) == LZMA_OK);
266 267 268 269 270 271 272 273
}

/*
 * Encode a chunk of pixels.
 */
static int
LZMAEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
{
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 299 300 301 302 303
    static const char module[] = "LZMAEncode";
    LZMAState *sp = EncoderState(tif);

    assert(sp != NULL);
    assert(sp->state == LSTATE_INIT_ENCODE);

    (void) s;
    sp->stream.next_in = bp;
    sp->stream.avail_in = (size_t) cc;
    if ((tmsize_t)sp->stream.avail_in != cc) {
        TIFFErrorExt(tif->tif_clientdata, module,
                 "Liblzma cannot deal with buffers this size");
        return 0;
    }
    do {
        lzma_ret ret = lzma_code(&sp->stream, LZMA_RUN);
        if (ret != LZMA_OK) {
            TIFFErrorExt(tif->tif_clientdata, module,
                "Encoding error at scanline %lu, %s",
                (unsigned long) tif->tif_row, LZMAStrerror(ret));
            return 0;
        }
        if (sp->stream.avail_out == 0) {
            tif->tif_rawcc = tif->tif_rawdatasize;
            TIFFFlushData1(tif);
            sp->stream.next_out = tif->tif_rawdata;
            sp->stream.avail_out = (size_t)tif->tif_rawdatasize;  /* this is a safe typecast, as check is made already in LZMAPreEncode */
        }
    } while (sp->stream.avail_in > 0);
    return 1;
304 305 306 307 308 309 310 311 312
}

/*
 * Finish off an encoded strip by flushing the last
 * string and tacking on an End Of Information code.
 */
static int
LZMAPostEncode(TIFF* tif)
{
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
    static const char module[] = "LZMAPostEncode";
    LZMAState *sp = EncoderState(tif);
    lzma_ret ret;

    sp->stream.avail_in = 0;
    do {
        ret = lzma_code(&sp->stream, LZMA_FINISH);
        switch (ret) {
        case LZMA_STREAM_END:
        case LZMA_OK:
            if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize) {
                tif->tif_rawcc =
                    tif->tif_rawdatasize - sp->stream.avail_out;
                TIFFFlushData1(tif);
                sp->stream.next_out = tif->tif_rawdata;
                sp->stream.avail_out = (size_t)tif->tif_rawdatasize;  /* this is a safe typecast, as check is made already in ZIPPreEncode */
            }
            break;
        default:
            TIFFErrorExt(tif->tif_clientdata, module, "Liblzma error: %s",
                     LZMAStrerror(ret));
            return 0;
        }
    } while (ret != LZMA_STREAM_END);
    return 1;
338 339 340 341 342
}

static void
LZMACleanup(TIFF* tif)
{
343
    LZMAState* sp = LState(tif);
344

345
    assert(sp != 0);
346

347
    (void)TIFFPredictorCleanup(tif);
348

349 350
    tif->tif_tagmethods.vgetfield = sp->vgetparent;
    tif->tif_tagmethods.vsetfield = sp->vsetparent;
351

352 353 354 355 356 357
    if (sp->state) {
        lzma_end(&sp->stream);
        sp->state = 0;
    }
    _TIFFfree(sp);
    tif->tif_data = NULL;
358

359
    _TIFFSetDefaultCompressionState(tif);
360 361 362 363 364
}

static int
LZMAVSetField(TIFF* tif, uint32 tag, va_list ap)
{
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
    static const char module[] = "LZMAVSetField";
    LZMAState* sp = LState(tif);

    switch (tag) {
    case TIFFTAG_LZMAPRESET:
        sp->preset = (int) va_arg(ap, int);
        lzma_lzma_preset(&sp->opt_lzma, sp->preset);
        if (sp->state & LSTATE_INIT_ENCODE) {
            lzma_ret ret = lzma_stream_encoder(&sp->stream,
                               sp->filters,
                               sp->check);
            if (ret != LZMA_OK) {
                TIFFErrorExt(tif->tif_clientdata, module,
                         "Liblzma error: %s",
                         LZMAStrerror(ret));
            }
        }
        return 1;
    default:
        return (*sp->vsetparent)(tif, tag, ap);
    }
    /*NOTREACHED*/
387 388 389 390 391
}

static int
LZMAVGetField(TIFF* tif, uint32 tag, va_list ap)
{
392 393 394 395 396 397 398 399 400 401
    LZMAState* sp = LState(tif);

    switch (tag) {
    case TIFFTAG_LZMAPRESET:
        *va_arg(ap, int*) = sp->preset;
        break;
    default:
        return (*sp->vgetparent)(tif, tag, ap);
    }
    return 1;
402 403 404
}

static const TIFFField lzmaFields[] = {
405 406
    { TIFFTAG_LZMAPRESET, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED,
        FIELD_PSEUDO, TRUE, FALSE, "LZMA2 Compression Preset", NULL },
407 408 409 410 411
};

int
TIFFInitLZMA(TIFF* tif, int scheme)
{
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
    static const char module[] = "TIFFInitLZMA";
    LZMAState* sp;
    lzma_stream tmp_stream = LZMA_STREAM_INIT;

    assert( scheme == COMPRESSION_LZMA );

    /*
     * Merge codec-specific tag information.
     */
    if (!_TIFFMergeFields(tif, lzmaFields, TIFFArrayCount(lzmaFields))) {
        TIFFErrorExt(tif->tif_clientdata, module,
                 "Merging LZMA2 codec-specific tags failed");
        return 0;
    }

    /*
     * Allocate state block so tag methods have storage to record values.
     */
    tif->tif_data = (uint8*) _TIFFmalloc(sizeof(LZMAState));
    if (tif->tif_data == NULL)
        goto bad;
    sp = LState(tif);
    memcpy(&sp->stream, &tmp_stream, sizeof(lzma_stream));

    /*
     * Override parent get/set field methods.
     */
    sp->vgetparent = tif->tif_tagmethods.vgetfield;
    tif->tif_tagmethods.vgetfield = LZMAVGetField;	/* hook for codec tags */
    sp->vsetparent = tif->tif_tagmethods.vsetfield;
    tif->tif_tagmethods.vsetfield = LZMAVSetField;	/* hook for codec tags */

    /* Default values for codec-specific fields */
    sp->preset = LZMA_PRESET_DEFAULT;		/* default comp. level */
    sp->check = LZMA_CHECK_NONE;
    sp->state = 0;

    /* Data filters. So far we are using delta and LZMA2 filters only. */
    sp->opt_delta.type = LZMA_DELTA_TYPE_BYTE;
    /*
     * The sample size in bytes seems to be reasonable distance for delta
     * filter.
     */
    sp->opt_delta.dist = (tif->tif_dir.td_bitspersample % 8) ?
        1 : tif->tif_dir.td_bitspersample / 8;
    sp->filters[0].id = LZMA_FILTER_DELTA;
    sp->filters[0].options = &sp->opt_delta;

    lzma_lzma_preset(&sp->opt_lzma, sp->preset);
    sp->filters[1].id = LZMA_FILTER_LZMA2;
    sp->filters[1].options = &sp->opt_lzma;

    sp->filters[2].id = LZMA_VLI_UNKNOWN;
    sp->filters[2].options = NULL;

    /*
     * Install codec methods.
     */
    tif->tif_fixuptags = LZMAFixupTags;
    tif->tif_setupdecode = LZMASetupDecode;
    tif->tif_predecode = LZMAPreDecode;
    tif->tif_decoderow = LZMADecode;
    tif->tif_decodestrip = LZMADecode;
    tif->tif_decodetile = LZMADecode;
    tif->tif_setupencode = LZMASetupEncode;
    tif->tif_preencode = LZMAPreEncode;
    tif->tif_postencode = LZMAPostEncode;
    tif->tif_encoderow = LZMAEncode;
    tif->tif_encodestrip = LZMAEncode;
    tif->tif_encodetile = LZMAEncode;
    tif->tif_cleanup = LZMACleanup;
    /*
     * Setup predictor setup.
     */
    (void) TIFFPredictorInit(tif);
    return 1;
488
bad:
489 490 491
    TIFFErrorExt(tif->tif_clientdata, module,
             "No space for LZMA2 state block");
    return 0;
492 493 494 495
}
#endif /* LZMA_SUPORT */

/* vim: set ts=8 sts=8 sw=8 noet: */