2018-06-03 09:01:17 +02:00
|
|
|
/****************************************************************************
|
|
|
|
*
|
|
|
|
* ftlzw.c
|
|
|
|
*
|
|
|
|
* FreeType support for .Z compressed files.
|
|
|
|
*
|
|
|
|
* This optional component relies on NetBSD's zopen(). It should mainly
|
|
|
|
* be used to parse compressed PCF fonts, as found with many X11 server
|
|
|
|
* distributions.
|
|
|
|
*
|
2022-01-11 10:54:10 +01:00
|
|
|
* Copyright (C) 2004-2022 by
|
2018-06-03 09:01:17 +02:00
|
|
|
* Albert Chin-A-Young.
|
|
|
|
*
|
|
|
|
* based on code in `src/gzip/ftgzip.c'
|
|
|
|
*
|
|
|
|
* This file is part of the FreeType project, and may only be used,
|
|
|
|
* modified, and distributed under the terms of the FreeType project
|
|
|
|
* license, LICENSE.TXT. By continuing to use, modify, or distribute
|
|
|
|
* this file you indicate that you have read the license and
|
|
|
|
* understand and accept it fully.
|
|
|
|
*
|
|
|
|
*/
|
2005-10-20 17:33:34 +02:00
|
|
|
|
2020-06-08 13:31:55 +02:00
|
|
|
#include <freetype/internal/ftmemory.h>
|
|
|
|
#include <freetype/internal/ftstream.h>
|
|
|
|
#include <freetype/internal/ftdebug.h>
|
|
|
|
#include <freetype/ftlzw.h>
|
2009-01-13 18:34:48 +01:00
|
|
|
#include FT_CONFIG_STANDARD_LIBRARY_H
|
2005-10-20 17:33:34 +02:00
|
|
|
|
|
|
|
|
2020-06-08 13:31:55 +02:00
|
|
|
#include <freetype/ftmoderr.h>
|
2005-10-20 17:33:34 +02:00
|
|
|
|
2016-01-12 21:37:13 +01:00
|
|
|
#undef FTERRORS_H_
|
2005-10-20 17:33:34 +02:00
|
|
|
|
Prepare source code for amalgamation.
* src\autofit\aferrors.h, src\bdf\bdferror.h, src\bzip2\ftbzip2.c,
src\cache\ftcerror.h, src\cff\cfferrs.h, src\cid\ciderrs.h,
src\gxvalid\gxverror.h, src\gzip\ftgzip.c, src\lzw\ftlzw.c,
src\otvalid\otverror.h, src\pcf\pcferror.h, src\pfr\pfrerror.h,
src\psaux\psauxerr.h, src\pshinter\pshnterr.h,
src\psnames\psnamerr.h, src\raster\rasterrs.h, src\sfnt\sferrors.h,
src\smooth\ftsmerrs.h, src\truetype\tterrors.h,
src\type1\t1errors.h, src\type42\t42error.h, src\winfonts\fnterrs.h:
Add #undef FT_ERR_PREFIX before #define FT_ERR_PREFIX.
2012-03-08 06:04:03 +01:00
|
|
|
#undef FT_ERR_PREFIX
|
2005-10-20 17:33:34 +02:00
|
|
|
#define FT_ERR_PREFIX LZW_Err_
|
|
|
|
#define FT_ERR_BASE FT_Mod_Err_LZW
|
|
|
|
|
2020-06-08 13:31:55 +02:00
|
|
|
#include <freetype/fterrors.h>
|
2005-10-20 17:33:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
#ifdef FT_CONFIG_OPTION_USE_LZW
|
|
|
|
|
|
|
|
#include "ftzopen.h"
|
|
|
|
|
|
|
|
|
|
|
|
/***************************************************************************/
|
|
|
|
/***************************************************************************/
|
|
|
|
/***** *****/
|
|
|
|
/***** M E M O R Y M A N A G E M E N T *****/
|
|
|
|
/***** *****/
|
|
|
|
/***************************************************************************/
|
|
|
|
/***************************************************************************/
|
|
|
|
|
|
|
|
/***************************************************************************/
|
|
|
|
/***************************************************************************/
|
|
|
|
/***** *****/
|
|
|
|
/***** F I L E D E S C R I P T O R *****/
|
|
|
|
/***** *****/
|
|
|
|
/***************************************************************************/
|
|
|
|
/***************************************************************************/
|
|
|
|
|
2005-10-23 21:25:41 +02:00
|
|
|
#define FT_LZW_BUFFER_SIZE 4096
|
2005-10-20 17:33:34 +02:00
|
|
|
|
2005-10-23 21:25:41 +02:00
|
|
|
typedef struct FT_LZWFileRec_
|
2005-10-20 17:33:34 +02:00
|
|
|
{
|
|
|
|
FT_Stream source; /* parent/source stream */
|
|
|
|
FT_Stream stream; /* embedding stream */
|
|
|
|
FT_Memory memory; /* memory allocator */
|
|
|
|
FT_LzwStateRec lzw; /* lzw decompressor state */
|
|
|
|
|
2005-10-23 21:25:41 +02:00
|
|
|
FT_Byte buffer[FT_LZW_BUFFER_SIZE]; /* output buffer */
|
|
|
|
FT_ULong pos; /* position in output */
|
2005-10-20 17:33:34 +02:00
|
|
|
FT_Byte* cursor;
|
|
|
|
FT_Byte* limit;
|
|
|
|
|
|
|
|
} FT_LZWFileRec, *FT_LZWFile;
|
|
|
|
|
|
|
|
|
|
|
|
/* check and skip .Z header */
|
|
|
|
static FT_Error
|
|
|
|
ft_lzw_check_header( FT_Stream stream )
|
|
|
|
{
|
|
|
|
FT_Error error;
|
|
|
|
FT_Byte head[2];
|
|
|
|
|
|
|
|
|
|
|
|
if ( FT_STREAM_SEEK( 0 ) ||
|
|
|
|
FT_STREAM_READ( head, 2 ) )
|
|
|
|
goto Exit;
|
|
|
|
|
2005-10-23 21:25:41 +02:00
|
|
|
/* head[0] && head[1] are the magic numbers */
|
2014-12-07 11:03:57 +01:00
|
|
|
if ( head[0] != 0x1F ||
|
|
|
|
head[1] != 0x9D )
|
2013-03-14 10:27:35 +01:00
|
|
|
error = FT_THROW( Invalid_File_Format );
|
2005-10-20 17:33:34 +02:00
|
|
|
|
|
|
|
Exit:
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static FT_Error
|
|
|
|
ft_lzw_file_init( FT_LZWFile zip,
|
|
|
|
FT_Stream stream,
|
|
|
|
FT_Stream source )
|
|
|
|
{
|
|
|
|
FT_LzwState lzw = &zip->lzw;
|
2013-06-04 10:30:48 +02:00
|
|
|
FT_Error error;
|
2005-10-20 17:33:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
zip->stream = stream;
|
|
|
|
zip->source = source;
|
|
|
|
zip->memory = stream->memory;
|
|
|
|
|
|
|
|
zip->limit = zip->buffer + FT_LZW_BUFFER_SIZE;
|
|
|
|
zip->cursor = zip->limit;
|
|
|
|
zip->pos = 0;
|
|
|
|
|
|
|
|
/* check and skip .Z header */
|
2010-01-05 20:00:35 +01:00
|
|
|
error = ft_lzw_check_header( source );
|
|
|
|
if ( error )
|
|
|
|
goto Exit;
|
2005-10-20 17:33:34 +02:00
|
|
|
|
|
|
|
/* initialize internal lzw variable */
|
|
|
|
ft_lzwstate_init( lzw, source );
|
|
|
|
|
|
|
|
Exit:
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
ft_lzw_file_done( FT_LZWFile zip )
|
|
|
|
{
|
|
|
|
/* clear the rest */
|
|
|
|
ft_lzwstate_done( &zip->lzw );
|
|
|
|
|
|
|
|
zip->memory = NULL;
|
|
|
|
zip->source = NULL;
|
|
|
|
zip->stream = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static FT_Error
|
|
|
|
ft_lzw_file_reset( FT_LZWFile zip )
|
|
|
|
{
|
|
|
|
FT_Stream stream = zip->source;
|
|
|
|
FT_Error error;
|
|
|
|
|
|
|
|
|
|
|
|
if ( !FT_STREAM_SEEK( 0 ) )
|
|
|
|
{
|
|
|
|
ft_lzwstate_reset( &zip->lzw );
|
|
|
|
|
|
|
|
zip->limit = zip->buffer + FT_LZW_BUFFER_SIZE;
|
|
|
|
zip->cursor = zip->limit;
|
|
|
|
zip->pos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static FT_Error
|
|
|
|
ft_lzw_file_fill_output( FT_LZWFile zip )
|
|
|
|
{
|
|
|
|
FT_LzwState lzw = &zip->lzw;
|
|
|
|
FT_ULong count;
|
2013-03-14 11:21:17 +01:00
|
|
|
FT_Error error = FT_Err_Ok;
|
2005-10-20 17:33:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
zip->cursor = zip->buffer;
|
|
|
|
|
|
|
|
count = ft_lzwstate_io( lzw, zip->buffer, FT_LZW_BUFFER_SIZE );
|
|
|
|
|
|
|
|
zip->limit = zip->cursor + count;
|
|
|
|
|
|
|
|
if ( count == 0 )
|
2013-03-14 10:27:35 +01:00
|
|
|
error = FT_THROW( Invalid_Stream_Operation );
|
2005-10-20 17:33:34 +02:00
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* fill output buffer; `count' must be <= FT_LZW_BUFFER_SIZE */
|
|
|
|
static FT_Error
|
|
|
|
ft_lzw_file_skip_output( FT_LZWFile zip,
|
|
|
|
FT_ULong count )
|
|
|
|
{
|
2013-03-14 11:21:17 +01:00
|
|
|
FT_Error error = FT_Err_Ok;
|
2005-10-20 17:33:34 +02:00
|
|
|
|
2005-10-23 21:25:41 +02:00
|
|
|
|
|
|
|
/* first, we skip what we can from the output buffer */
|
2005-10-20 17:33:34 +02:00
|
|
|
{
|
|
|
|
FT_ULong delta = (FT_ULong)( zip->limit - zip->cursor );
|
|
|
|
|
2005-10-23 21:25:41 +02:00
|
|
|
|
2005-10-20 17:33:34 +02:00
|
|
|
if ( delta >= count )
|
|
|
|
delta = count;
|
|
|
|
|
|
|
|
zip->cursor += delta;
|
|
|
|
zip->pos += delta;
|
|
|
|
|
|
|
|
count -= delta;
|
|
|
|
}
|
|
|
|
|
2005-10-23 21:25:41 +02:00
|
|
|
/* next, we skip as many bytes remaining as possible */
|
2005-10-20 17:33:34 +02:00
|
|
|
while ( count > 0 )
|
|
|
|
{
|
|
|
|
FT_ULong delta = FT_LZW_BUFFER_SIZE;
|
|
|
|
FT_ULong numread;
|
|
|
|
|
2005-10-23 21:25:41 +02:00
|
|
|
|
2005-10-20 17:33:34 +02:00
|
|
|
if ( delta > count )
|
|
|
|
delta = count;
|
|
|
|
|
|
|
|
numread = ft_lzwstate_io( &zip->lzw, NULL, delta );
|
|
|
|
if ( numread < delta )
|
|
|
|
{
|
|
|
|
/* not enough bytes */
|
2013-03-14 10:27:35 +01:00
|
|
|
error = FT_THROW( Invalid_Stream_Operation );
|
2005-10-20 17:33:34 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
zip->pos += delta;
|
|
|
|
count -= delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static FT_ULong
|
|
|
|
ft_lzw_file_io( FT_LZWFile zip,
|
|
|
|
FT_ULong pos,
|
|
|
|
FT_Byte* buffer,
|
|
|
|
FT_ULong count )
|
|
|
|
{
|
|
|
|
FT_ULong result = 0;
|
|
|
|
FT_Error error;
|
|
|
|
|
|
|
|
|
|
|
|
/* seeking backwards. */
|
|
|
|
if ( pos < zip->pos )
|
|
|
|
{
|
2005-10-23 21:25:41 +02:00
|
|
|
/* If the new position is within the output buffer, simply */
|
|
|
|
/* decrement pointers, otherwise we reset the stream completely! */
|
|
|
|
if ( ( zip->pos - pos ) <= (FT_ULong)( zip->cursor - zip->buffer ) )
|
2005-10-20 17:33:34 +02:00
|
|
|
{
|
2005-10-23 21:25:41 +02:00
|
|
|
zip->cursor -= zip->pos - pos;
|
2005-10-20 17:33:34 +02:00
|
|
|
zip->pos = pos;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
error = ft_lzw_file_reset( zip );
|
|
|
|
if ( error )
|
|
|
|
goto Exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* skip unwanted bytes */
|
|
|
|
if ( pos > zip->pos )
|
|
|
|
{
|
|
|
|
error = ft_lzw_file_skip_output( zip, (FT_ULong)( pos - zip->pos ) );
|
|
|
|
if ( error )
|
|
|
|
goto Exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( count == 0 )
|
|
|
|
goto Exit;
|
|
|
|
|
|
|
|
/* now read the data */
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
FT_ULong delta;
|
|
|
|
|
|
|
|
|
|
|
|
delta = (FT_ULong)( zip->limit - zip->cursor );
|
|
|
|
if ( delta >= count )
|
|
|
|
delta = count;
|
|
|
|
|
|
|
|
FT_MEM_COPY( buffer + result, zip->cursor, delta );
|
|
|
|
result += delta;
|
|
|
|
zip->cursor += delta;
|
|
|
|
zip->pos += delta;
|
|
|
|
|
|
|
|
count -= delta;
|
|
|
|
if ( count == 0 )
|
|
|
|
break;
|
|
|
|
|
|
|
|
error = ft_lzw_file_fill_output( zip );
|
|
|
|
if ( error )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Exit:
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***************************************************************************/
|
|
|
|
/***************************************************************************/
|
|
|
|
/***** *****/
|
|
|
|
/***** L Z W E M B E D D I N G S T R E A M *****/
|
|
|
|
/***** *****/
|
|
|
|
/***************************************************************************/
|
|
|
|
/***************************************************************************/
|
|
|
|
|
|
|
|
static void
|
|
|
|
ft_lzw_stream_close( FT_Stream stream )
|
|
|
|
{
|
|
|
|
FT_LZWFile zip = (FT_LZWFile)stream->descriptor.pointer;
|
|
|
|
FT_Memory memory = stream->memory;
|
|
|
|
|
|
|
|
|
|
|
|
if ( zip )
|
|
|
|
{
|
|
|
|
/* finalize lzw file descriptor */
|
|
|
|
ft_lzw_file_done( zip );
|
|
|
|
|
|
|
|
FT_FREE( zip );
|
|
|
|
|
|
|
|
stream->descriptor.pointer = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-26 15:19:54 +02:00
|
|
|
static unsigned long
|
|
|
|
ft_lzw_stream_io( FT_Stream stream,
|
|
|
|
unsigned long offset,
|
|
|
|
unsigned char* buffer,
|
|
|
|
unsigned long count )
|
2005-10-20 17:33:34 +02:00
|
|
|
{
|
|
|
|
FT_LZWFile zip = (FT_LZWFile)stream->descriptor.pointer;
|
|
|
|
|
|
|
|
|
2015-09-26 15:19:54 +02:00
|
|
|
return ft_lzw_file_io( zip, offset, buffer, count );
|
2005-10-20 17:33:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FT_EXPORT_DEF( FT_Error )
|
|
|
|
FT_Stream_OpenLZW( FT_Stream stream,
|
|
|
|
FT_Stream source )
|
|
|
|
{
|
|
|
|
FT_Error error;
|
2014-11-26 21:59:21 +01:00
|
|
|
FT_Memory memory;
|
2022-03-31 07:43:41 +02:00
|
|
|
FT_LZWFile zip = NULL;
|
2005-10-20 17:33:34 +02:00
|
|
|
|
|
|
|
|
2014-11-26 21:59:21 +01:00
|
|
|
if ( !stream || !source )
|
|
|
|
{
|
|
|
|
error = FT_THROW( Invalid_Stream_Handle );
|
|
|
|
goto Exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
memory = source->memory;
|
|
|
|
|
2005-10-20 17:33:34 +02:00
|
|
|
/*
|
2018-06-03 09:01:17 +02:00
|
|
|
* Check the header right now; this prevents allocation of a huge
|
|
|
|
* LZWFile object (400 KByte of heap memory) if not necessary.
|
2005-10-20 17:33:34 +02:00
|
|
|
*
|
2018-06-03 09:01:17 +02:00
|
|
|
* Did I mention that you should never use .Z compressed font
|
|
|
|
* files?
|
2005-10-20 17:33:34 +02:00
|
|
|
*/
|
|
|
|
error = ft_lzw_check_header( source );
|
|
|
|
if ( error )
|
|
|
|
goto Exit;
|
|
|
|
|
|
|
|
FT_ZERO( stream );
|
|
|
|
stream->memory = memory;
|
|
|
|
|
2022-01-13 15:45:48 +01:00
|
|
|
if ( !FT_QNEW( zip ) )
|
2005-10-20 17:33:34 +02:00
|
|
|
{
|
|
|
|
error = ft_lzw_file_init( zip, stream, source );
|
|
|
|
if ( error )
|
|
|
|
{
|
|
|
|
FT_FREE( zip );
|
|
|
|
goto Exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream->descriptor.pointer = zip;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream->size = 0x7FFFFFFFL; /* don't know the real size! */
|
|
|
|
stream->pos = 0;
|
2021-09-02 03:37:21 +02:00
|
|
|
stream->base = NULL;
|
2005-10-20 17:33:34 +02:00
|
|
|
stream->read = ft_lzw_stream_io;
|
|
|
|
stream->close = ft_lzw_stream_close;
|
|
|
|
|
|
|
|
Exit:
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2005-10-23 21:25:41 +02:00
|
|
|
|
2005-10-20 17:33:34 +02:00
|
|
|
#include "ftzopen.c"
|
|
|
|
|
|
|
|
|
|
|
|
#else /* !FT_CONFIG_OPTION_USE_LZW */
|
|
|
|
|
|
|
|
|
|
|
|
FT_EXPORT_DEF( FT_Error )
|
|
|
|
FT_Stream_OpenLZW( FT_Stream stream,
|
|
|
|
FT_Stream source )
|
|
|
|
{
|
|
|
|
FT_UNUSED( stream );
|
|
|
|
FT_UNUSED( source );
|
|
|
|
|
2013-03-14 10:27:35 +01:00
|
|
|
return FT_THROW( Unimplemented_Feature );
|
2005-10-20 17:33:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* !FT_CONFIG_OPTION_USE_LZW */
|
|
|
|
|
|
|
|
|
|
|
|
/* END */
|