123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803 |
- /***************************************************************************/
- /* */
- /* ftstream.c */
- /* */
- /* I/O stream support (body). */
- /* */
- /* Copyright 2000-2001, 2002 by */
- /* David Turner, Robert Wilhelm, and Werner Lemberg. */
- /* */
- /* 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. */
- /* */
- /***************************************************************************/
- #include <ft2build.h>
- #include FT_INTERNAL_STREAM_H
- #include FT_INTERNAL_DEBUG_H
- /*************************************************************************/
- /* */
- /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
- /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
- /* messages during execution. */
- /* */
- #undef FT_COMPONENT
- #define FT_COMPONENT trace_stream
- FT_BASE_DEF( void )
- FT_Stream_OpenMemory( FT_Stream stream,
- const FT_Byte* base,
- FT_ULong size )
- {
- stream->base = (FT_Byte*) base;
- stream->size = size;
- stream->pos = 0;
- stream->cursor = 0;
- stream->read = 0;
- stream->close = 0;
- }
- FT_BASE_DEF( void )
- FT_Stream_Close( FT_Stream stream )
- {
- if ( stream && stream->close )
- {
- stream->close( stream );
- stream->close = NULL;
- }
- }
- FT_BASE_DEF( FT_Error )
- FT_Stream_Seek( FT_Stream stream,
- FT_ULong pos )
- {
- FT_Error error = FT_Err_Ok;
- stream->pos = pos;
- if ( stream->read )
- {
- if ( stream->read( stream, pos, 0, 0 ) )
- {
- FT_ERROR(( "FT_Stream_Seek: invalid i/o; pos = 0x%lx, size = 0x%lx\n",
- pos, stream->size ));
- error = FT_Err_Invalid_Stream_Operation;
- }
- }
- /* note that seeking to the first position after the file is valid */
- else if ( pos > stream->size )
- {
- FT_ERROR(( "FT_Stream_Seek: invalid i/o; pos = 0x%lx, size = 0x%lx\n",
- pos, stream->size ));
- error = FT_Err_Invalid_Stream_Operation;
- }
- return error;
- }
- FT_BASE_DEF( FT_Error )
- FT_Stream_Skip( FT_Stream stream,
- FT_Long distance )
- {
- return FT_Stream_Seek( stream, (FT_ULong)( stream->pos + distance ) );
- }
- FT_BASE_DEF( FT_Long )
- FT_Stream_Pos( FT_Stream stream )
- {
- return stream->pos;
- }
- FT_BASE_DEF( FT_Error )
- FT_Stream_Read( FT_Stream stream,
- FT_Byte* buffer,
- FT_ULong count )
- {
- return FT_Stream_ReadAt( stream, stream->pos, buffer, count );
- }
- FT_BASE_DEF( FT_Error )
- FT_Stream_ReadAt( FT_Stream stream,
- FT_ULong pos,
- FT_Byte* buffer,
- FT_ULong count )
- {
- FT_Error error = FT_Err_Ok;
- FT_ULong read_bytes;
- if ( pos >= stream->size )
- {
- FT_ERROR(( "FT_Stream_ReadAt: invalid i/o; pos = 0x%lx, size = 0x%lx\n",
- pos, stream->size ));
- return FT_Err_Invalid_Stream_Operation;
- }
- if ( stream->read )
- read_bytes = stream->read( stream, pos, buffer, count );
- else
- {
- read_bytes = stream->size - pos;
- if ( read_bytes > count )
- read_bytes = count;
- FT_MEM_COPY( buffer, stream->base + pos, read_bytes );
- }
- stream->pos = pos + read_bytes;
- if ( read_bytes < count )
- {
- FT_ERROR(( "FT_Stream_ReadAt:" ));
- FT_ERROR(( " invalid read; expected %lu bytes, got %lu\n",
- count, read_bytes ));
- error = FT_Err_Invalid_Stream_Operation;
- }
- return error;
- }
- FT_BASE_DEF( FT_Error )
- FT_Stream_ExtractFrame( FT_Stream stream,
- FT_ULong count,
- FT_Byte** pbytes )
- {
- FT_Error error;
- error = FT_Stream_EnterFrame( stream, count );
- if ( !error )
- {
- *pbytes = (FT_Byte*)stream->cursor;
- /* equivalent to FT_Stream_ExitFrame(), with no memory block release */
- stream->cursor = 0;
- stream->limit = 0;
- }
- return error;
- }
- FT_BASE_DEF( void )
- FT_Stream_ReleaseFrame( FT_Stream stream,
- FT_Byte** pbytes )
- {
- if ( stream->read )
- {
- FT_Memory memory = stream->memory;
- FT_FREE( *pbytes );
- }
- *pbytes = 0;
- }
- FT_BASE_DEF( FT_Error )
- FT_Stream_EnterFrame( FT_Stream stream,
- FT_ULong count )
- {
- FT_Error error = FT_Err_Ok;
- FT_ULong read_bytes;
- /* check for nested frame access */
- FT_ASSERT( stream && stream->cursor == 0 );
- if ( stream->read )
- {
- /* allocate the frame in memory */
- FT_Memory memory = stream->memory;
- if ( FT_ALLOC( stream->base, count ) )
- goto Exit;
- /* read it */
- read_bytes = stream->read( stream, stream->pos,
- stream->base, count );
- if ( read_bytes < count )
- {
- FT_ERROR(( "FT_Stream_EnterFrame:" ));
- FT_ERROR(( " invalid read; expected %lu bytes, got %lu\n",
- count, read_bytes ));
- FT_FREE( stream->base );
- error = FT_Err_Invalid_Stream_Operation;
- }
- stream->cursor = stream->base;
- stream->limit = stream->cursor + count;
- stream->pos += read_bytes;
- }
- else
- {
- /* check current and new position */
- if ( stream->pos >= stream->size ||
- stream->pos + count > stream->size )
- {
- FT_ERROR(( "FT_Stream_EnterFrame:" ));
- FT_ERROR(( " invalid i/o; pos = 0x%lx, count = %lu, size = 0x%lx\n",
- stream->pos, count, stream->size ));
- error = FT_Err_Invalid_Stream_Operation;
- goto Exit;
- }
- /* set cursor */
- stream->cursor = stream->base + stream->pos;
- stream->limit = stream->cursor + count;
- stream->pos += count;
- }
- Exit:
- return error;
- }
- FT_BASE_DEF( void )
- FT_Stream_ExitFrame( FT_Stream stream )
- {
- /* IMPORTANT: The assertion stream->cursor != 0 was removed, given */
- /* that it is possible to access a frame of length 0 in */
- /* some weird fonts (usually, when accessing an array of */
- /* 0 records, like in some strange kern tables). */
- /* */
- /* In this case, the loader code handles the 0-length table */
- /* gracefully; however, stream.cursor is really set to 0 by the */
- /* FT_Stream_EnterFrame() call, and this is not an error. */
- /* */
- FT_ASSERT( stream );
- if ( stream->read )
- {
- FT_Memory memory = stream->memory;
- FT_FREE( stream->base );
- }
- stream->cursor = 0;
- stream->limit = 0;
- }
- FT_BASE_DEF( FT_Char )
- FT_Stream_GetChar( FT_Stream stream )
- {
- FT_Char result;
- FT_ASSERT( stream && stream->cursor );
- result = 0;
- if ( stream->cursor < stream->limit )
- result = *stream->cursor++;
- return result;
- }
- FT_BASE_DEF( FT_Short )
- FT_Stream_GetShort( FT_Stream stream )
- {
- FT_Byte* p;
- FT_Short result;
- FT_ASSERT( stream && stream->cursor );
- result = 0;
- p = stream->cursor;
- if ( p + 1 < stream->limit )
- result = FT_NEXT_SHORT( p );
- stream->cursor = p;
- return result;
- }
- FT_BASE_DEF( FT_Short )
- FT_Stream_GetShortLE( FT_Stream stream )
- {
- FT_Byte* p;
- FT_Short result;
- FT_ASSERT( stream && stream->cursor );
- result = 0;
- p = stream->cursor;
- if ( p + 1 < stream->limit )
- result = FT_NEXT_SHORT_LE( p );
- stream->cursor = p;
- return result;
- }
- FT_BASE_DEF( FT_Long )
- FT_Stream_GetOffset( FT_Stream stream )
- {
- FT_Byte* p;
- FT_Long result;
- FT_ASSERT( stream && stream->cursor );
- result = 0;
- p = stream->cursor;
- if ( p + 2 < stream->limit )
- result = FT_NEXT_OFF3( p );
- stream->cursor = p;
- return result;
- }
- FT_BASE_DEF( FT_Long )
- FT_Stream_GetLong( FT_Stream stream )
- {
- FT_Byte* p;
- FT_Long result;
- FT_ASSERT( stream && stream->cursor );
- result = 0;
- p = stream->cursor;
- if ( p + 3 < stream->limit )
- result = FT_NEXT_LONG( p );
- stream->cursor = p;
- return result;
- }
- FT_BASE_DEF( FT_Long )
- FT_Stream_GetLongLE( FT_Stream stream )
- {
- FT_Byte* p;
- FT_Long result;
- FT_ASSERT( stream && stream->cursor );
- result = 0;
- p = stream->cursor;
- if ( p + 3 < stream->limit )
- result = FT_NEXT_LONG_LE( p );
- stream->cursor = p;
- return result;
- }
- FT_BASE_DEF( FT_Char )
- FT_Stream_ReadChar( FT_Stream stream,
- FT_Error* error )
- {
- FT_Byte result = 0;
- FT_ASSERT( stream );
- *error = FT_Err_Ok;
- if ( stream->read )
- {
- if ( stream->read( stream, stream->pos, &result, 1L ) != 1L )
- goto Fail;
- }
- else
- {
- if ( stream->pos < stream->size )
- result = stream->base[stream->pos];
- else
- goto Fail;
- }
- stream->pos++;
- return result;
- Fail:
- *error = FT_Err_Invalid_Stream_Operation;
- FT_ERROR(( "FT_Stream_ReadChar: invalid i/o; pos = 0x%lx, size = 0x%lx\n",
- stream->pos, stream->size ));
- return 0;
- }
- FT_BASE_DEF( FT_Short )
- FT_Stream_ReadShort( FT_Stream stream,
- FT_Error* error )
- {
- FT_Byte reads[2];
- FT_Byte* p = 0;
- FT_Short result = 0;
- FT_ASSERT( stream );
- *error = FT_Err_Ok;
- if ( stream->pos + 1 < stream->size )
- {
- if ( stream->read )
- {
- if ( stream->read( stream, stream->pos, reads, 2L ) != 2L )
- goto Fail;
- p = reads;
- }
- else
- {
- p = stream->base + stream->pos;
- }
- if ( p )
- result = FT_NEXT_SHORT( p );
- }
- else
- goto Fail;
- stream->pos += 2;
- return result;
- Fail:
- *error = FT_Err_Invalid_Stream_Operation;
- FT_ERROR(( "FT_Stream_ReadShort:" ));
- FT_ERROR(( " invalid i/o; pos = 0x%lx, size = 0x%lx\n",
- stream->pos, stream->size ));
- return 0;
- }
- FT_BASE_DEF( FT_Short )
- FT_Stream_ReadShortLE( FT_Stream stream,
- FT_Error* error )
- {
- FT_Byte reads[2];
- FT_Byte* p = 0;
- FT_Short result = 0;
- FT_ASSERT( stream );
- *error = FT_Err_Ok;
- if ( stream->pos + 1 < stream->size )
- {
- if ( stream->read )
- {
- if ( stream->read( stream, stream->pos, reads, 2L ) != 2L )
- goto Fail;
- p = reads;
- }
- else
- {
- p = stream->base + stream->pos;
- }
- if ( p )
- result = FT_NEXT_SHORT_LE( p );
- }
- else
- goto Fail;
- stream->pos += 2;
- return result;
- Fail:
- *error = FT_Err_Invalid_Stream_Operation;
- FT_ERROR(( "FT_Stream_ReadShortLE:" ));
- FT_ERROR(( " invalid i/o; pos = 0x%lx, size = 0x%lx\n",
- stream->pos, stream->size ));
- return 0;
- }
- FT_BASE_DEF( FT_Long )
- FT_Stream_ReadOffset( FT_Stream stream,
- FT_Error* error )
- {
- FT_Byte reads[3];
- FT_Byte* p = 0;
- FT_Long result = 0;
- FT_ASSERT( stream );
- *error = FT_Err_Ok;
- if ( stream->pos + 2 < stream->size )
- {
- if ( stream->read )
- {
- if (stream->read( stream, stream->pos, reads, 3L ) != 3L )
- goto Fail;
- p = reads;
- }
- else
- {
- p = stream->base + stream->pos;
- }
- if ( p )
- result = FT_NEXT_OFF3( p );
- }
- else
- goto Fail;
- stream->pos += 3;
- return result;
- Fail:
- *error = FT_Err_Invalid_Stream_Operation;
- FT_ERROR(( "FT_Stream_ReadOffset:" ));
- FT_ERROR(( " invalid i/o; pos = 0x%lx, size = 0x%lx\n",
- stream->pos, stream->size ));
- return 0;
- }
- FT_BASE_DEF( FT_Long )
- FT_Stream_ReadLong( FT_Stream stream,
- FT_Error* error )
- {
- FT_Byte reads[4];
- FT_Byte* p = 0;
- FT_Long result = 0;
- FT_ASSERT( stream );
- *error = FT_Err_Ok;
- if ( stream->pos + 3 < stream->size )
- {
- if ( stream->read )
- {
- if ( stream->read( stream, stream->pos, reads, 4L ) != 4L )
- goto Fail;
- p = reads;
- }
- else
- {
- p = stream->base + stream->pos;
- }
- if ( p )
- result = FT_NEXT_LONG( p );
- }
- else
- goto Fail;
- stream->pos += 4;
- return result;
- Fail:
- FT_ERROR(( "FT_Stream_ReadLong: invalid i/o; pos = 0x%lx, size = 0x%lx\n",
- stream->pos, stream->size ));
- *error = FT_Err_Invalid_Stream_Operation;
- return 0;
- }
- FT_BASE_DEF( FT_Long )
- FT_Stream_ReadLongLE( FT_Stream stream,
- FT_Error* error )
- {
- FT_Byte reads[4];
- FT_Byte* p = 0;
- FT_Long result = 0;
- FT_ASSERT( stream );
- *error = FT_Err_Ok;
- if ( stream->pos + 3 < stream->size )
- {
- if ( stream->read )
- {
- if ( stream->read( stream, stream->pos, reads, 4L ) != 4L )
- goto Fail;
- p = reads;
- }
- else
- {
- p = stream->base + stream->pos;
- }
- if ( p )
- result = FT_NEXT_LONG_LE( p );
- }
- else
- goto Fail;
- stream->pos += 4;
- return result;
- Fail:
- FT_ERROR(( "FT_Stream_ReadLongLE:" ));
- FT_ERROR(( " invalid i/o; pos = 0x%lx, size = 0x%lx\n",
- stream->pos, stream->size ));
- *error = FT_Err_Invalid_Stream_Operation;
- return 0;
- }
- FT_BASE_DEF( FT_Error )
- FT_Stream_ReadFields( FT_Stream stream,
- const FT_Frame_Field* fields,
- void* structure )
- {
- FT_Error error;
- FT_Bool frame_accessed = 0;
- FT_Byte* cursor = stream->cursor;
- if ( !fields || !stream )
- return FT_Err_Invalid_Argument;
- error = FT_Err_Ok;
- do
- {
- FT_ULong value;
- FT_Int sign_shift;
- FT_Byte* p;
- switch ( fields->value )
- {
- case ft_frame_start: /* access a new frame */
- error = FT_Stream_EnterFrame( stream, fields->offset );
- if ( error )
- goto Exit;
- frame_accessed = 1;
- cursor = stream->cursor;
- fields++;
- continue; /* loop! */
- case ft_frame_bytes: /* read a byte sequence */
- case ft_frame_skip: /* skip some bytes */
- {
- FT_UInt len = fields->size;
- if ( cursor + len > stream->limit )
- {
- error = FT_Err_Invalid_Stream_Operation;
- goto Exit;
- }
- if ( fields->value == ft_frame_bytes )
- {
- p = (FT_Byte*)structure + fields->offset;
- FT_MEM_COPY( p, cursor, len );
- }
- cursor += len;
- fields++;
- continue;
- }
- case ft_frame_byte:
- case ft_frame_schar: /* read a single byte */
- value = FT_NEXT_BYTE(cursor);
- sign_shift = 24;
- break;
- case ft_frame_short_be:
- case ft_frame_ushort_be: /* read a 2-byte big-endian short */
- value = FT_NEXT_USHORT(cursor);
- sign_shift = 16;
- break;
- case ft_frame_short_le:
- case ft_frame_ushort_le: /* read a 2-byte little-endian short */
- value = FT_NEXT_USHORT_LE(cursor);
- sign_shift = 16;
- break;
- case ft_frame_long_be:
- case ft_frame_ulong_be: /* read a 4-byte big-endian long */
- value = FT_NEXT_ULONG(cursor);
- sign_shift = 0;
- break;
- case ft_frame_long_le:
- case ft_frame_ulong_le: /* read a 4-byte little-endian long */
- value = FT_NEXT_ULONG_LE(cursor);
- sign_shift = 0;
- break;
- case ft_frame_off3_be:
- case ft_frame_uoff3_be: /* read a 3-byte big-endian long */
- value = FT_NEXT_UOFF3(cursor);
- sign_shift = 8;
- break;
- case ft_frame_off3_le:
- case ft_frame_uoff3_le: /* read a 3-byte little-endian long */
- value = FT_NEXT_UOFF3_LE(cursor);
- sign_shift = 8;
- break;
- default:
- /* otherwise, exit the loop */
- stream->cursor = cursor;
- goto Exit;
- }
- /* now, compute the signed value is necessary */
- if ( fields->value & FT_FRAME_OP_SIGNED )
- value = (FT_ULong)( (FT_Int32)( value << sign_shift ) >> sign_shift );
- /* finally, store the value in the object */
- p = (FT_Byte*)structure + fields->offset;
- switch ( fields->size )
- {
- case 1:
- *(FT_Byte*)p = (FT_Byte)value;
- break;
- case 2:
- *(FT_UShort*)p = (FT_UShort)value;
- break;
- case 4:
- *(FT_UInt32*)p = (FT_UInt32)value;
- break;
- default: /* for 64-bit systems */
- *(FT_ULong*)p = (FT_ULong)value;
- }
- /* go to next field */
- fields++;
- }
- while ( 1 );
- Exit:
- /* close the frame if it was opened by this read */
- if ( frame_accessed )
- FT_Stream_ExitFrame( stream );
- return error;
- }
- /* END */
|