1 /***************************************************************************/ 2 /* */ 3 /* ftsystem.c */ 4 /* */ 5 /* ANSI-specific FreeType low-level system interface (body). */ 6 /* */ 7 /* Copyright 1996-2018 by */ 8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */ 9 /* */ 10 /* This file is part of the FreeType project, and may only be used, */ 11 /* modified, and distributed under the terms of the FreeType project */ 12 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */ 13 /* this file you indicate that you have read the license and */ 14 /* understand and accept it fully. */ 15 /* */ 16 /***************************************************************************/ 17 18 /*************************************************************************/ 19 /* */ 20 /* This file contains the default interface used by FreeType to access */ 21 /* low-level, i.e. memory management, i/o access as well as thread */ 22 /* synchronisation. It can be replaced by user-specific routines if */ 23 /* necessary. */ 24 /* */ 25 /*************************************************************************/ 26 27 28 #include <ft2build.h> 29 #include FT_CONFIG_CONFIG_H 30 #include FT_INTERNAL_DEBUG_H 31 #include FT_INTERNAL_STREAM_H 32 #include FT_SYSTEM_H 33 #include FT_ERRORS_H 34 #include FT_TYPES_H 35 36 37 /*************************************************************************/ 38 /* */ 39 /* MEMORY MANAGEMENT INTERFACE */ 40 /* */ 41 /*************************************************************************/ 42 43 /*************************************************************************/ 44 /* */ 45 /* It is not necessary to do any error checking for the */ 46 /* allocation-related functions. This will be done by the higher level */ 47 /* routines like ft_mem_alloc() or ft_mem_realloc(). */ 48 /* */ 49 /*************************************************************************/ 50 51 52 /*************************************************************************/ 53 /* */ 54 /* <Function> */ 55 /* ft_alloc */ 56 /* */ 57 /* <Description> */ 58 /* The memory allocation function. */ 59 /* */ 60 /* <Input> */ 61 /* memory :: A pointer to the memory object. */ 62 /* */ 63 /* size :: The requested size in bytes. */ 64 /* */ 65 /* <Return> */ 66 /* The address of newly allocated block. */ 67 /* */ 68 FT_CALLBACK_DEF( void* ) 69 ft_alloc( FT_Memory memory, 70 long size ) 71 { 72 FT_UNUSED( memory ); 73 74 return ft_smalloc( (size_t)size ); 75 } 76 77 78 /*************************************************************************/ 79 /* */ 80 /* <Function> */ 81 /* ft_realloc */ 82 /* */ 83 /* <Description> */ 84 /* The memory reallocation function. */ 85 /* */ 86 /* <Input> */ 87 /* memory :: A pointer to the memory object. */ 88 /* */ 89 /* cur_size :: The current size of the allocated memory block. */ 90 /* */ 91 /* new_size :: The newly requested size in bytes. */ 92 /* */ 93 /* block :: The current address of the block in memory. */ 94 /* */ 95 /* <Return> */ 96 /* The address of the reallocated memory block. */ 97 /* */ 98 FT_CALLBACK_DEF( void* ) 99 ft_realloc( FT_Memory memory, 100 long cur_size, 101 long new_size, 102 void* block ) 103 { 104 FT_UNUSED( memory ); 105 FT_UNUSED( cur_size ); 106 107 return ft_srealloc( block, (size_t)new_size ); 108 } 109 110 111 /*************************************************************************/ 112 /* */ 113 /* <Function> */ 114 /* ft_free */ 115 /* */ 116 /* <Description> */ 117 /* The memory release function. */ 118 /* */ 119 /* <Input> */ 120 /* memory :: A pointer to the memory object. */ 121 /* */ 122 /* block :: The address of block in memory to be freed. */ 123 /* */ 124 FT_CALLBACK_DEF( void ) 125 ft_free( FT_Memory memory, 126 void* block ) 127 { 128 FT_UNUSED( memory ); 129 130 ft_sfree( block ); 131 } 132 133 134 /*************************************************************************/ 135 /* */ 136 /* RESOURCE MANAGEMENT INTERFACE */ 137 /* */ 138 /*************************************************************************/ 139 140 #ifndef FT_CONFIG_OPTION_DISABLE_STREAM_SUPPORT 141 142 /*************************************************************************/ 143 /* */ 144 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */ 145 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ 146 /* messages during execution. */ 147 /* */ 148 #undef FT_COMPONENT 149 #define FT_COMPONENT trace_io 150 151 /* We use the macro STREAM_FILE for convenience to extract the */ 152 /* system-specific stream handle from a given FreeType stream object */ 153 #define STREAM_FILE( stream ) ( (FT_FILE*)stream->descriptor.pointer ) 154 155 156 /*************************************************************************/ 157 /* */ 158 /* <Function> */ 159 /* ft_ansi_stream_close */ 160 /* */ 161 /* <Description> */ 162 /* The function to close a stream. */ 163 /* */ 164 /* <Input> */ 165 /* stream :: A pointer to the stream object. */ 166 /* */ 167 FT_CALLBACK_DEF( void ) 168 ft_ansi_stream_close( FT_Stream stream ) 169 { 170 ft_fclose( STREAM_FILE( stream ) ); 171 172 stream->descriptor.pointer = NULL; 173 stream->size = 0; 174 stream->base = NULL; 175 } 176 177 178 /*************************************************************************/ 179 /* */ 180 /* <Function> */ 181 /* ft_ansi_stream_io */ 182 /* */ 183 /* <Description> */ 184 /* The function to open a stream. */ 185 /* */ 186 /* <Input> */ 187 /* stream :: A pointer to the stream object. */ 188 /* */ 189 /* offset :: The position in the data stream to start reading. */ 190 /* */ 191 /* buffer :: The address of buffer to store the read data. */ 192 /* */ 193 /* count :: The number of bytes to read from the stream. */ 194 /* */ 195 /* <Return> */ 196 /* The number of bytes actually read. If `count' is zero (this is, */ 197 /* the function is used for seeking), a non-zero return value */ 198 /* indicates an error. */ 199 /* */ 200 FT_CALLBACK_DEF( unsigned long ) 201 ft_ansi_stream_io( FT_Stream stream, 202 unsigned long offset, 203 unsigned char* buffer, 204 unsigned long count ) 205 { 206 FT_FILE* file; 207 208 209 if ( !count && offset > stream->size ) 210 return 1; 211 212 file = STREAM_FILE( stream ); 213 214 if ( stream->pos != offset ) 215 ft_fseek( file, (long)offset, SEEK_SET ); 216 217 return (unsigned long)ft_fread( buffer, 1, count, file ); 218 } 219 | 1 /**************************************************************************** 2 * 3 * ftsystem.c 4 * 5 * ANSI-specific FreeType low-level system interface (body). 6 * 7 * Copyright (C) 1996-2019 by 8 * David Turner, Robert Wilhelm, and Werner Lemberg. 9 * 10 * This file is part of the FreeType project, and may only be used, 11 * modified, and distributed under the terms of the FreeType project 12 * license, LICENSE.TXT. By continuing to use, modify, or distribute 13 * this file you indicate that you have read the license and 14 * understand and accept it fully. 15 * 16 */ 17 18 /************************************************************************** 19 * 20 * This file contains the default interface used by FreeType to access 21 * low-level, i.e. memory management, i/o access as well as thread 22 * synchronisation. It can be replaced by user-specific routines if 23 * necessary. 24 * 25 */ 26 27 28 #include <ft2build.h> 29 #include FT_CONFIG_CONFIG_H 30 #include FT_INTERNAL_DEBUG_H 31 #include FT_INTERNAL_STREAM_H 32 #include FT_SYSTEM_H 33 #include FT_ERRORS_H 34 #include FT_TYPES_H 35 36 37 /************************************************************************** 38 * 39 * MEMORY MANAGEMENT INTERFACE 40 * 41 */ 42 43 /************************************************************************** 44 * 45 * It is not necessary to do any error checking for the 46 * allocation-related functions. This will be done by the higher level 47 * routines like ft_mem_alloc() or ft_mem_realloc(). 48 * 49 */ 50 51 52 /************************************************************************** 53 * 54 * @Function: 55 * ft_alloc 56 * 57 * @Description: 58 * The memory allocation function. 59 * 60 * @Input: 61 * memory :: 62 * A pointer to the memory object. 63 * 64 * size :: 65 * The requested size in bytes. 66 * 67 * @Return: 68 * The address of newly allocated block. 69 */ 70 FT_CALLBACK_DEF( void* ) 71 ft_alloc( FT_Memory memory, 72 long size ) 73 { 74 FT_UNUSED( memory ); 75 76 return ft_smalloc( (size_t)size ); 77 } 78 79 80 /************************************************************************** 81 * 82 * @Function: 83 * ft_realloc 84 * 85 * @Description: 86 * The memory reallocation function. 87 * 88 * @Input: 89 * memory :: 90 * A pointer to the memory object. 91 * 92 * cur_size :: 93 * The current size of the allocated memory block. 94 * 95 * new_size :: 96 * The newly requested size in bytes. 97 * 98 * block :: 99 * The current address of the block in memory. 100 * 101 * @Return: 102 * The address of the reallocated memory block. 103 */ 104 FT_CALLBACK_DEF( void* ) 105 ft_realloc( FT_Memory memory, 106 long cur_size, 107 long new_size, 108 void* block ) 109 { 110 FT_UNUSED( memory ); 111 FT_UNUSED( cur_size ); 112 113 return ft_srealloc( block, (size_t)new_size ); 114 } 115 116 117 /************************************************************************** 118 * 119 * @Function: 120 * ft_free 121 * 122 * @Description: 123 * The memory release function. 124 * 125 * @Input: 126 * memory :: 127 * A pointer to the memory object. 128 * 129 * block :: 130 * The address of block in memory to be freed. 131 */ 132 FT_CALLBACK_DEF( void ) 133 ft_free( FT_Memory memory, 134 void* block ) 135 { 136 FT_UNUSED( memory ); 137 138 ft_sfree( block ); 139 } 140 141 142 /************************************************************************** 143 * 144 * RESOURCE MANAGEMENT INTERFACE 145 * 146 */ 147 148 #ifndef FT_CONFIG_OPTION_DISABLE_STREAM_SUPPORT 149 150 /************************************************************************** 151 * 152 * The macro FT_COMPONENT is used in trace mode. It is an implicit 153 * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log 154 * messages during execution. 155 */ 156 #undef FT_COMPONENT 157 #define FT_COMPONENT io 158 159 /* We use the macro STREAM_FILE for convenience to extract the */ 160 /* system-specific stream handle from a given FreeType stream object */ 161 #define STREAM_FILE( stream ) ( (FT_FILE*)stream->descriptor.pointer ) 162 163 164 /************************************************************************** 165 * 166 * @Function: 167 * ft_ansi_stream_close 168 * 169 * @Description: 170 * The function to close a stream. 171 * 172 * @Input: 173 * stream :: 174 * A pointer to the stream object. 175 */ 176 FT_CALLBACK_DEF( void ) 177 ft_ansi_stream_close( FT_Stream stream ) 178 { 179 ft_fclose( STREAM_FILE( stream ) ); 180 181 stream->descriptor.pointer = NULL; 182 stream->size = 0; 183 stream->base = NULL; 184 } 185 186 187 /************************************************************************** 188 * 189 * @Function: 190 * ft_ansi_stream_io 191 * 192 * @Description: 193 * The function to open a stream. 194 * 195 * @Input: 196 * stream :: 197 * A pointer to the stream object. 198 * 199 * offset :: 200 * The position in the data stream to start reading. 201 * 202 * buffer :: 203 * The address of buffer to store the read data. 204 * 205 * count :: 206 * The number of bytes to read from the stream. 207 * 208 * @Return: 209 * The number of bytes actually read. If `count' is zero (this is, 210 * the function is used for seeking), a non-zero return value 211 * indicates an error. 212 */ 213 FT_CALLBACK_DEF( unsigned long ) 214 ft_ansi_stream_io( FT_Stream stream, 215 unsigned long offset, 216 unsigned char* buffer, 217 unsigned long count ) 218 { 219 FT_FILE* file; 220 221 222 if ( !count && offset > stream->size ) 223 return 1; 224 225 file = STREAM_FILE( stream ); 226 227 if ( stream->pos != offset ) 228 ft_fseek( file, (long)offset, SEEK_SET ); 229 230 return (unsigned long)ft_fread( buffer, 1, count, file ); 231 } 232 |