1 /****************************************************************************
5 * Unix-specific FreeType low-level system interface (body).
7 * Copyright (C) 1996-2023 by
8 * David Turner, Robert Wilhelm, and Werner Lemberg.
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.
20 /* we use our special ftconfig.h file, not the standard one */
21 #include FT_CONFIG_CONFIG_H
22 #include <freetype/internal/ftdebug.h>
23 #include <freetype/ftsystem.h>
24 #include <freetype/fterrors.h>
25 #include <freetype/fttypes.h>
26 #include <freetype/internal/ftstream.h>
28 /* memory-mapping includes and definitions */
38 #ifdef MUNMAP_USES_VOIDP
39 #define MUNMAP_ARG_CAST void *
41 #define MUNMAP_ARG_CAST char *
44 #ifdef NEED_MUNMAP_DECL
55 #define MUNMAP_ARG_CAST char *
57 #endif /* NEED_DECLARATION_MUNMAP */
60 #include <sys/types.h>
73 /**************************************************************************
75 * MEMORY MANAGEMENT INTERFACE
80 /**************************************************************************
82 * It is not necessary to do any error checking for the
83 * allocation-related functions. This will be done by the higher level
84 * routines like ft_mem_alloc() or ft_mem_realloc().
89 /**************************************************************************
95 * The memory allocation function.
99 * A pointer to the memory object.
102 * The requested size in bytes.
105 * The address of newly allocated block.
107 FT_CALLBACK_DEF( void* )
108 ft_alloc( FT_Memory memory,
113 return malloc( size );
117 /**************************************************************************
123 * The memory reallocation function.
127 * A pointer to the memory object.
130 * The current size of the allocated memory block.
133 * The newly requested size in bytes.
136 * The current address of the block in memory.
139 * The address of the reallocated memory block.
141 FT_CALLBACK_DEF( void* )
142 ft_realloc( FT_Memory memory,
148 FT_UNUSED( cur_size );
150 return realloc( block, new_size );
154 /**************************************************************************
160 * The memory release function.
164 * A pointer to the memory object.
167 * The address of block in memory to be freed.
169 FT_CALLBACK_DEF( void )
170 ft_free( FT_Memory memory,
179 /**************************************************************************
181 * RESOURCE MANAGEMENT INTERFACE
186 /**************************************************************************
188 * The macro FT_COMPONENT is used in trace mode. It is an implicit
189 * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
190 * messages during execution.
193 #define FT_COMPONENT io
195 /* We use the macro STREAM_FILE for convenience to extract the */
196 /* system-specific stream handle from a given FreeType stream object */
197 #define STREAM_FILE( stream ) ( (FILE*)stream->descriptor.pointer )
200 /**************************************************************************
203 * ft_close_stream_by_munmap
206 * The function to close a stream which is opened by mmap.
209 * stream :: A pointer to the stream object.
211 FT_CALLBACK_DEF( void )
212 ft_close_stream_by_munmap( FT_Stream stream )
214 munmap( (MUNMAP_ARG_CAST)stream->descriptor.pointer, stream->size );
216 stream->descriptor.pointer = NULL;
222 /**************************************************************************
225 * ft_close_stream_by_free
228 * The function to close a stream which is created by ft_alloc.
231 * stream :: A pointer to the stream object.
233 FT_CALLBACK_DEF( void )
234 ft_close_stream_by_free( FT_Stream stream )
236 ft_free( stream->memory, stream->descriptor.pointer );
238 stream->descriptor.pointer = NULL;
244 /* documentation is in ftobjs.h */
246 FT_BASE_DEF( FT_Error )
247 FT_Stream_Open( FT_Stream stream,
248 const char* filepathname )
251 struct stat stat_buf;
255 return FT_THROW( Invalid_Stream_Handle );
258 file = open( filepathname, O_RDONLY );
261 FT_ERROR(( "FT_Stream_Open:" ));
262 FT_ERROR(( " could not open `%s'\n", filepathname ));
263 return FT_THROW( Cannot_Open_Resource );
266 /* Here we ensure that a "fork" will _not_ duplicate */
267 /* our opened input streams on Unix. This is critical */
268 /* since it avoids some (possible) access control */
269 /* issues and cleans up the kernel file table a bit. */
273 (void)fcntl( file, F_SETFD, FD_CLOEXEC );
275 (void)fcntl( file, F_SETFD, 1 );
276 #endif /* FD_CLOEXEC */
279 if ( fstat( file, &stat_buf ) < 0 )
281 FT_ERROR(( "FT_Stream_Open:" ));
282 FT_ERROR(( " could not `fstat' file `%s'\n", filepathname ));
286 /* XXX: TODO -- real 64bit platform support */
288 /* `stream->size' is typedef'd to unsigned long (in `ftsystem.h'); */
289 /* `stat_buf.st_size', however, is usually typedef'd to off_t */
290 /* (in sys/stat.h). */
291 /* On some platforms, the former is 32bit and the latter is 64bit. */
292 /* To avoid overflow caused by fonts in huge files larger than */
293 /* 2GB, do a test. Temporary fix proposed by Sean McBride. */
295 if ( stat_buf.st_size > LONG_MAX )
297 FT_ERROR(( "FT_Stream_Open: file is too big\n" ));
300 else if ( stat_buf.st_size == 0 )
302 FT_ERROR(( "FT_Stream_Open: zero-length file\n" ));
306 /* This cast potentially truncates a 64bit to 32bit! */
307 stream->size = (unsigned long)stat_buf.st_size;
309 stream->base = (unsigned char *)mmap( NULL,
312 MAP_FILE | MAP_PRIVATE,
316 if ( stream->base != MAP_FAILED )
317 stream->close = ft_close_stream_by_munmap;
320 ssize_t total_read_count;
323 FT_ERROR(( "FT_Stream_Open:" ));
324 FT_ERROR(( " could not `mmap' file `%s'\n", filepathname ));
326 stream->base = (unsigned char*)ft_alloc( stream->memory, stream->size );
330 FT_ERROR(( "FT_Stream_Open:" ));
331 FT_ERROR(( " could not `alloc' memory\n" ));
335 total_read_count = 0;
341 read_count = read( file,
342 stream->base + total_read_count,
343 stream->size - total_read_count );
345 if ( read_count <= 0 )
347 if ( read_count == -1 && errno == EINTR )
350 FT_ERROR(( "FT_Stream_Open:" ));
351 FT_ERROR(( " error while `read'ing file `%s'\n", filepathname ));
355 total_read_count += read_count;
357 } while ( (unsigned long)total_read_count != stream->size );
359 stream->close = ft_close_stream_by_free;
364 stream->descriptor.pointer = stream->base;
365 stream->pathname.pointer = (char*)filepathname;
369 FT_TRACE1(( "FT_Stream_Open:" ));
370 FT_TRACE1(( " opened `%s' (%ld bytes) successfully\n",
371 filepathname, stream->size ));
376 ft_free( stream->memory, stream->base );
385 return FT_THROW( Cannot_Open_Stream );
389 #ifdef FT_DEBUG_MEMORY
392 ft_mem_debug_init( FT_Memory memory );
395 ft_mem_debug_done( FT_Memory memory );
400 /* documentation is in ftobjs.h */
402 FT_BASE_DEF( FT_Memory )
403 FT_New_Memory( void )
408 memory = (FT_Memory)malloc( sizeof ( *memory ) );
412 memory->alloc = ft_alloc;
413 memory->realloc = ft_realloc;
414 memory->free = ft_free;
415 #ifdef FT_DEBUG_MEMORY
416 ft_mem_debug_init( memory );
424 /* documentation is in ftobjs.h */
427 FT_Done_Memory( FT_Memory memory )
429 #ifdef FT_DEBUG_MEMORY
430 ft_mem_debug_done( memory );
432 memory->free( memory, memory );