third_party/cairo/src/cairo-lzw.c
unsigned char *
_cairo_lzw_compress (unsigned char *data, unsigned long *size_in_out)
{
    int bytes_remaining = *size_in_out;
    lzw_buf_t buf;
    lzw_symbol_table_t table;
    lzw_symbol_t symbol, *slot = NULL; /* just to squelch a warning */
    int code_next = LZW_CODE_FIRST;
    int code_bits = LZW_BITS_MIN;
    int prev, next = 0; /* just to squelch a warning */

    if (*size_in_out == 0)
	return NULL;

    _lzw_buf_init (&buf, *size_in_out);

    _lzw_symbol_table_init (&table);

    /* The LZW header is a clear table code. */
    _lzw_buf_store_bits (&buf, LZW_CODE_CLEAR_TABLE, code_bits);

    while (1) {

	/* Find the longest existing code in the symbol table that
	 * matches the current input, if any. */
	prev = *data++;
	bytes_remaining--;
	if (bytes_remaining) {
	    do
	    {
		next = *data++;
		bytes_remaining--;
		LZW_SYMBOL_SET (symbol, prev, next);
		if (_lzw_symbol_table_lookup (&table, symbol, &slot))
		    prev = LZW_SYMBOL_GET_CODE (*slot);
	    } while (bytes_remaining && *slot != LZW_SYMBOL_FREE);
	    if (*slot == LZW_SYMBOL_FREE) {
		data--;
		bytes_remaining++;
	    }
	}

	/* Write the code into the output. This is either a byte read
	 * directly from the input, or a code from the last successful
	 * lookup. */
	_lzw_buf_store_bits (&buf, prev, code_bits);

	if (bytes_remaining == 0)
	    break;

	LZW_SYMBOL_SET_CODE (*slot, code_next++, prev, next);

	if (code_next > LZW_BITS_BOUNDARY(code_bits))
	{
	    code_bits++;
	    if (code_bits > LZW_BITS_MAX) {
		_lzw_symbol_table_init (&table);
		_lzw_buf_store_bits (&buf, LZW_CODE_CLEAR_TABLE, code_bits - 1);
		code_bits = LZW_BITS_MIN;
		code_next = LZW_CODE_FIRST;
	    }
	}
    }

    /* The LZW footer is an end-of-data code. */
    _lzw_buf_store_bits (&buf, LZW_CODE_EOD, code_bits);

    _lzw_buf_store_pending (&buf);

    /* See if we ever ran out of memory while writing to buf. */
    if (buf.status == CAIRO_STATUS_NO_MEMORY) {
	*size_in_out = 0;
	return NULL;
    }

    assert (buf.status == CAIRO_STATUS_SUCCESS);

    *size_in_out = buf.num_data;
    return buf.data;
}
/* Compress a bytestream using the LZW algorithm.
 *
 * This is an original implementation based on reading the
 * specification of the LZWDecode filter in the PostScript Language
 * Reference. The free parameters in the LZW algorithm are set to the
 * values mandated by PostScript, (symbols encoded with widths from 9
 * to 12 bits).
 *
 * This function returns a pointer to a newly allocated buffer holding
 * the compressed data, or %NULL if an out-of-memory situation
 * occurs.
 *
 * Notice that any one of the _lzw_buf functions called here could
 * trigger an out-of-memory condition. But lzw_buf_t uses cairo's
 * shutdown-on-error idiom, so it's safe to continue to call into
 * lzw_buf without having to check for errors, (until a final check at
 * the end).
 */
Compress a bytestream using the LZW algorithm.
This is an original implementation based on reading the
specification of the LZWDecode filter in the PostScript Language
Reference. The free parameters in the LZW algorithm are set to the
values mandated by PostScript, (symbols encoded with widths from 9
to 12 bits).

This function returns a pointer to a newly allocated buffer holding
the compressed data, or %NULL if an out-of-memory situation
occurs.

Notice that any one of the _lzw_buf functions called here could
trigger an out-of-memory condition. But lzw_buf_t uses cairo's
shutdown-on-error idiom, so it's safe to continue to call into
lzw_buf without having to check for errors, (until a final check at
the end).
Compress a bytestream using the LZW algorithm . This is an original implementation based on reading the specification of the LZWDecode filter in the PostScript Language Reference . The free parameters in the LZW algorithm are set to the values mandated by PostScript ( symbols encoded with widths from 9 to 12 bits ) . This function returns a pointer to a newly allocated buffer holding the compressed data or %NULL if an out - of - memory situation occurs . Notice that any one of the _lzw_buf functions called here could trigger an out - of - memory condition . But lzw_buf_t uses cairo ' s shutdown - on - error idiom so it ' s safe to continue to call into lzw_buf without having to check for errors ( until a final check at the end ) .
unsigned char *
_cairo_lzw_compress (unsigned char *data, unsigned long *size_in_out)
{
    int bytes_remaining = *size_in_out;
    lzw_buf_t buf;
    lzw_symbol_table_t table;
    lzw_symbol_t symbol, *slot = NULL; 
    int code_next = LZW_CODE_FIRST;
    int code_bits = LZW_BITS_MIN;
    int prev, next = 0; 
    if (*size_in_out == 0)
	return NULL;
    _lzw_buf_init (&buf, *size_in_out);
    _lzw_symbol_table_init (&table);
    _lzw_buf_store_bits (&buf, LZW_CODE_CLEAR_TABLE, code_bits);
    while (1) {
	prev = *data++;
	bytes_remaining--;
	if (bytes_remaining) {
	    do
	    {
		next = *data++;
		bytes_remaining--;
		LZW_SYMBOL_SET (symbol, prev, next);
		if (_lzw_symbol_table_lookup (&table, symbol, &slot))
		    prev = LZW_SYMBOL_GET_CODE (*slot);
	    } while (bytes_remaining && *slot != LZW_SYMBOL_FREE);
	    if (*slot == LZW_SYMBOL_FREE) {
		data--;
		bytes_remaining++;
	    }
	}
	_lzw_buf_store_bits (&buf, prev, code_bits);
	if (bytes_remaining == 0)
	    break;
	LZW_SYMBOL_SET_CODE (*slot, code_next++, prev, next);
	if (code_next > LZW_BITS_BOUNDARY(code_bits))
	{
	    code_bits++;
	    if (code_bits > LZW_BITS_MAX) {
		_lzw_symbol_table_init (&table);
		_lzw_buf_store_bits (&buf, LZW_CODE_CLEAR_TABLE, code_bits - 1);
		code_bits = LZW_BITS_MIN;
		code_next = LZW_CODE_FIRST;
	    }
	}
    }
    _lzw_buf_store_bits (&buf, LZW_CODE_EOD, code_bits);
    _lzw_buf_store_pending (&buf);
    if (buf.status == CAIRO_STATUS_NO_MEMORY) {
	*size_in_out = 0;
	return NULL;
    }
    assert (buf.status == CAIRO_STATUS_SUCCESS);
    *size_in_out = buf.num_data;
    return buf.data;
}
unsigned char * _cairo_lzw_compress ( unsigned char * data , unsigned long * size_in_out ) { int bytes_remaining = * size_in_out ; lzw_buf_t buf ; lzw_symbol_table_t table ; lzw_symbol_t symbol , * slot = NULL ; int code_next = LZW_CODE_FIRST ; int code_bits = LZW_BITS_MIN ; int prev , next = 0 ; if ( * size_in_out == 0 ) return NULL ; _lzw_buf_init ( & buf , * size_in_out ) ; _lzw_symbol_table_init ( & table ) ; _lzw_buf_store_bits ( & buf , LZW_CODE_CLEAR_TABLE , code_bits ) ; while ( 1 ) { prev = * data ++ ; bytes_remaining -- ; if ( bytes_remaining ) { do { next = * data ++ ; bytes_remaining -- ; LZW_SYMBOL_SET ( symbol , prev , next ) ; if ( _lzw_symbol_table_lookup ( & table , symbol , & slot ) ) prev = LZW_SYMBOL_GET_CODE ( * slot ) ; } while ( bytes_remaining && * slot != LZW_SYMBOL_FREE ) ; if ( * slot == LZW_SYMBOL_FREE ) { data -- ; bytes_remaining ++ ; } } _lzw_buf_store_bits ( & buf , prev , code_bits ) ; if ( bytes_remaining == 0 ) break ; LZW_SYMBOL_SET_CODE ( * slot , code_next ++ , prev , next ) ; if ( code_next > LZW_BITS_BOUNDARY ( code_bits ) ) { code_bits ++ ; if ( code_bits > LZW_BITS_MAX ) { _lzw_symbol_table_init ( & table ) ; _lzw_buf_store_bits ( & buf , LZW_CODE_CLEAR_TABLE , code_bits - 1 ) ; code_bits = LZW_BITS_MIN ; code_next = LZW_CODE_FIRST ; } } } _lzw_buf_store_bits ( & buf , LZW_CODE_EOD , code_bits ) ; _lzw_buf_store_pending ( & buf ) ; if ( buf . status == CAIRO_STATUS_NO_MEMORY ) { * size_in_out = 0 ; return NULL ; } assert ( buf . status == CAIRO_STATUS_SUCCESS ) ; * size_in_out = buf . num_data ; return buf . data ; }
Compress a bytestream using the LZW algorithm.
Compress a bytestream using the LZW algorithm .
/* just to squelch a warning */ /* just to squelch a warning */ /* The LZW header is a clear table code. */ /* Find the longest existing code in the symbol table that
	 * matches the current input, if any. */ /* Write the code into the output. This is either a byte read
	 * directly from the input, or a code from the last successful
	 * lookup. */ /* The LZW footer is an end-of-data code. */ /* See if we ever ran out of memory while writing to buf. */