FT_Stream_TryRead( FT_Stream stream, FT_Byte* buffer, FT_ULong count ) { FT_ULong read_bytes = 0; if ( stream->pos >= stream->size ) goto Exit; if ( stream->read ) read_bytes = stream->read( stream, stream->pos, buffer, count ); else { read_bytes = stream->size - stream->pos; if ( read_bytes > count ) read_bytes = count; FT_MEM_COPY( buffer, stream->base + stream->pos, read_bytes ); } stream->pos += read_bytes; Exit: return read_bytes; }
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_THROW( 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_Stream_Seek( FT_Stream stream, FT_ULong pos ) { FT_Error error = FT_Err_Ok; 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_THROW( 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_THROW( Invalid_Stream_Operation ); } if ( !error ) stream->pos = pos; return error; }
FT_Stream_Close( FT_Stream stream ) { if ( stream && stream->close ) { stream->close( stream ); stream->close = NULL; } }
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_QALLOC( 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; }
/* load a typeface from a URI */ FT_Error FT_New_Face_From_URI (FT_Library library, const gchar* uri, FT_Long face_index, FT_Face *aface) { FT_Open_Args args; FT_Stream stream; FT_Error error; stream = calloc (1, sizeof (*stream)); if (stream == NULL) return FT_Err_Out_Of_Memory; error = vfs_stream_open (stream, uri); if (error != FT_Err_Ok) { free (stream); return error; } args.flags = FT_OPEN_STREAM; args.stream = stream; error = FT_Open_Face (library, &args, face_index, aface); if (error != FT_Err_Ok) { if (stream->close != NULL) stream->close(stream); free (stream); return error; } /* so that freetype will free the stream */ (*aface)->face_flags &= ~FT_FACE_FLAG_EXTERNAL_STREAM; return error; }
static FT_Error ft_bzip2_file_fill_input( FT_BZip2File zip ) { bz_stream* bzstream = &zip->bzstream; FT_Stream stream = zip->source; FT_ULong size; if ( stream->read ) { size = stream->read( stream, stream->pos, zip->input, FT_BZIP2_BUFFER_SIZE ); if ( size == 0 ) { zip->limit = zip->cursor; return FT_THROW( Invalid_Stream_Operation ); } } else { size = stream->size - stream->pos; if ( size > FT_BZIP2_BUFFER_SIZE ) size = FT_BZIP2_BUFFER_SIZE; if ( size == 0 ) { zip->limit = zip->cursor; return FT_THROW( Invalid_Stream_Operation ); } FT_MEM_COPY( zip->input, stream->base + stream->pos, size ); } stream->pos += size; bzstream->next_in = (char*)zip->input; bzstream->avail_in = size; return FT_Err_Ok; }
BASE_FUNC(FT_Long) FT_Read_LongLE(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 = NEXT_LongLE(p); } } else { goto Fail; } stream->pos += 4; return result; Fail: FT_ERROR(("FT_Read_Long:")); FT_ERROR((" invalid i/o; pos = 0x%lx, size = 0x%lx\n", stream->pos, stream->size)); *error = FT_Err_Invalid_Stream_Operation; return 0; }
BASE_FUNC(FT_Short) FT_Read_Short(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 = NEXT_Short(p); } } else { goto Fail; } stream->pos += 2; return result; Fail: *error = FT_Err_Invalid_Stream_Operation; FT_ERROR(("FT_Read_Short:")); FT_ERROR((" invalid i/o; pos = 0x%lx, size = 0x%lx\n", stream->pos, stream->size)); return 0; }
BASE_FUNC(FT_Error) FT_Read_Stream_At(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_Read_Stream_At:")); FT_ERROR((" 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; } MEM_Copy(buffer, stream->base + pos, read_bytes); } stream->pos = pos + read_bytes; if(read_bytes < count) { FT_ERROR(("FT_Read_Stream_At:")); FT_ERROR((" invalid read; expected %lu bytes, got %lu\n", count, read_bytes)); error = FT_Err_Invalid_Stream_Operation; } return error; }
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_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_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; }
BASE_FUNC(FT_Char) FT_Read_Char(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_Read_Char:")); FT_ERROR((" invalid i/o; pos = 0x%lx, size = 0x%lx\n", stream->pos, stream->size)); return 0; }
BASE_FUNC(FT_Error) FT_Seek_Stream(FT_Stream stream, FT_ULong pos) { FT_Error error; stream->pos = pos; if(stream->read) { if(stream->read(stream, pos, 0, 0)) { FT_ERROR(("FT_Seek_Stream:")); FT_ERROR((" invalid i/o; pos = 0x%lx, size = 0x%lx\n", pos, stream->size)); error = FT_Err_Invalid_Stream_Operation; } else { error = FT_Err_Ok; } } /* note that seeking to the first position after the file is valid */ else if(pos > stream->size) { FT_ERROR(("FT_Seek_Stream:")); FT_ERROR((" invalid i/o; pos = 0x%lx, size = 0x%lx\n", pos, stream->size)); error = FT_Err_Invalid_Stream_Operation; } else { error = FT_Err_Ok; } return error; }
FT_Stream_Close( FT_Stream stream ) { if ( stream && stream->close ) stream->close( stream ); }
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; #ifdef FT_DEBUG_MEMORY /* assume _ft_debug_file and _ft_debug_lineno are already set */ stream->base = (unsigned char*)ft_mem_qalloc( memory, count, &error ); if ( error ) goto Exit; #else if ( FT_QALLOC( stream->base, count ) ) goto Exit; #endif /* 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; }