Debian Codegraph @

src/3rdparty/freetype/builds/unix/ftsystem.c

raw
1 /****************************************************************************
2 *
3 * ftsystem.c
4 *
5 * Unix-specific FreeType low-level system interface (body).
6 *
7 * Copyright (C) 1996-2023 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 #include <ft2build.h>
20 /* we use our special ftconfig.h file, not the standard one */
21 #include <ftconfig.h>
22 //#include FT_CONFIG_CONFIG_H
23 #include <freetype/internal/ftdebug.h>
24 #include <freetype/ftsystem.h>
25 #include <freetype/fterrors.h>
26 #include <freetype/fttypes.h>
27 #include <freetype/internal/ftstream.h>
28
29 /* memory-mapping includes and definitions */
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33
34 #include <sys/mman.h>
35 #ifndef MAP_FILE
36 #define MAP_FILE 0x00
37 #endif
38
39 #ifdef MUNMAP_USES_VOIDP
40 #define MUNMAP_ARG_CAST void *
41 #else
42 #define MUNMAP_ARG_CAST char *
43 #endif
44
45 #ifdef NEED_MUNMAP_DECL
46
47 #ifdef __cplusplus
48 extern "C"
49 #else
50 extern
51 #endif
52 int
53 munmap( char* addr,
54 int len );
55
56 #define MUNMAP_ARG_CAST char *
57
58 #endif /* NEED_DECLARATION_MUNMAP */
59
60
61 #include <sys/types.h>
62 #include <sys/stat.h>
63
64 #ifdef HAVE_FCNTL_H
65 #include <fcntl.h>
66 #endif
67
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <string.h>
71 #include <errno.h>
72
73
74 /**************************************************************************
75 *
76 * MEMORY MANAGEMENT INTERFACE
77 *
78 */
79
80
81 /**************************************************************************
82 *
83 * It is not necessary to do any error checking for the
84 * allocation-related functions. This will be done by the higher level
85 * routines like ft_mem_alloc() or ft_mem_realloc().
86 *
87 */
88
89
90 /**************************************************************************
91 *
92 * @Function:
93 * ft_alloc
94 *
95 * @Description:
96 * The memory allocation function.
97 *
98 * @Input:
99 * memory ::
100 * A pointer to the memory object.
101 *
102 * size ::
103 * The requested size in bytes.
104 *
105 * @Return:
106 * The address of newly allocated block.
107 */
108 FT_CALLBACK_DEF( void* )
109 ft_alloc( FT_Memory memory,
110 long size )
111 {
112 FT_UNUSED( memory );
113
114 return malloc( size );
115 }
116
117
118 /**************************************************************************
119 *
120 * @Function:
121 * ft_realloc
122 *
123 * @Description:
124 * The memory reallocation function.
125 *
126 * @Input:
127 * memory ::
128 * A pointer to the memory object.
129 *
130 * cur_size ::
131 * The current size of the allocated memory block.
132 *
133 * new_size ::
134 * The newly requested size in bytes.
135 *
136 * block ::
137 * The current address of the block in memory.
138 *
139 * @Return:
140 * The address of the reallocated memory block.
141 */
142 FT_CALLBACK_DEF( void* )
143 ft_realloc( FT_Memory memory,
144 long cur_size,
145 long new_size,
146 void* block )
147 {
148 FT_UNUSED( memory );
149 FT_UNUSED( cur_size );
150
151 return realloc( block, new_size );
152 }
153
154
155 /**************************************************************************
156 *
157 * @Function:
158 * ft_free
159 *
160 * @Description:
161 * The memory release function.
162 *
163 * @Input:
164 * memory ::
165 * A pointer to the memory object.
166 *
167 * block ::
168 * The address of block in memory to be freed.
169 */
170 FT_CALLBACK_DEF( void )
171 ft_free( FT_Memory memory,
172 void* block )
173 {
174 FT_UNUSED( memory );
175
176 free( block );
177 }
178
179
180 /**************************************************************************
181 *
182 * RESOURCE MANAGEMENT INTERFACE
183 *
184 */
185
186
187 /**************************************************************************
188 *
189 * The macro FT_COMPONENT is used in trace mode. It is an implicit
190 * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
191 * messages during execution.
192 */
193 #undef FT_COMPONENT
194 #define FT_COMPONENT io
195
196 /* We use the macro STREAM_FILE for convenience to extract the */
197 /* system-specific stream handle from a given FreeType stream object */
198 #define STREAM_FILE( stream ) ( (FILE*)stream->descriptor.pointer )
199
200
201 /**************************************************************************
202 *
203 * @Function:
204 * ft_close_stream_by_munmap
205 *
206 * @Description:
207 * The function to close a stream which is opened by mmap.
208 *
209 * @Input:
210 * stream :: A pointer to the stream object.
211 */
212 FT_CALLBACK_DEF( void )
213 ft_close_stream_by_munmap( FT_Stream stream )
214 {
215 munmap( (MUNMAP_ARG_CAST)stream->descriptor.pointer, stream->size );
216
217 stream->descriptor.pointer = NULL;
218 stream->size = 0;
219 stream->base = NULL;
220 }
221
222
223 /**************************************************************************
224 *
225 * @Function:
226 * ft_close_stream_by_free
227 *
228 * @Description:
229 * The function to close a stream which is created by ft_alloc.
230 *
231 * @Input:
232 * stream :: A pointer to the stream object.
233 */
234 FT_CALLBACK_DEF( void )
235 ft_close_stream_by_free( FT_Stream stream )
236 {
237 ft_free( stream->memory, stream->descriptor.pointer );
238
239 stream->descriptor.pointer = NULL;
240 stream->size = 0;
241 stream->base = NULL;
242 }
243
244
245 /* documentation is in ftobjs.h */
246
247 FT_BASE_DEF( FT_Error )
248 FT_Stream_Open( FT_Stream stream,
249 const char* filepathname )
250 {
251 int file;
252 struct stat stat_buf;
253
254
255 if ( !stream )
256 return FT_THROW( Invalid_Stream_Handle );
257
258 /* open the file */
259 file = open( filepathname, O_RDONLY );
260 if ( file < 0 )
261 {
262 FT_ERROR(( "FT_Stream_Open:" ));
263 FT_ERROR(( " could not open `%s'\n", filepathname ));
264 return FT_THROW( Cannot_Open_Resource );
265 }
266
267 /* Here we ensure that a "fork" will _not_ duplicate */
268 /* our opened input streams on Unix. This is critical */
269 /* since it avoids some (possible) access control */
270 /* issues and cleans up the kernel file table a bit. */
271 /* */
272 #ifdef F_SETFD
273 #ifdef FD_CLOEXEC
274 (void)fcntl( file, F_SETFD, FD_CLOEXEC );
275 #else
276 (void)fcntl( file, F_SETFD, 1 );
277 #endif /* FD_CLOEXEC */
278 #endif /* F_SETFD */
279
280 if ( fstat( file, &stat_buf ) < 0 )
281 {
282 FT_ERROR(( "FT_Stream_Open:" ));
283 FT_ERROR(( " could not `fstat' file `%s'\n", filepathname ));
284 goto Fail_Map;
285 }
286
287 /* XXX: TODO -- real 64bit platform support */
288 /* */
289 /* `stream->size' is typedef'd to unsigned long (in `ftsystem.h'); */
290 /* `stat_buf.st_size', however, is usually typedef'd to off_t */
291 /* (in sys/stat.h). */
292 /* On some platforms, the former is 32bit and the latter is 64bit. */
293 /* To avoid overflow caused by fonts in huge files larger than */
294 /* 2GB, do a test. Temporary fix proposed by Sean McBride. */
295 /* */
296 if ( stat_buf.st_size > LONG_MAX )
297 {
298 FT_ERROR(( "FT_Stream_Open: file is too big\n" ));
299 goto Fail_Map;
300 }
301 else if ( stat_buf.st_size == 0 )
302 {
303 FT_ERROR(( "FT_Stream_Open: zero-length file\n" ));
304 goto Fail_Map;
305 }
306
307 /* This cast potentially truncates a 64bit to 32bit! */
308 stream->size = (unsigned long)stat_buf.st_size;
309 stream->pos = 0;
310 stream->base = (unsigned char *)mmap( NULL,
311 stream->size,
312 PROT_READ,
313 MAP_FILE | MAP_PRIVATE,
314 file,
315 0 );
316
317 if ( stream->base != MAP_FAILED )
318 stream->close = ft_close_stream_by_munmap;
319 else
320 {
321 ssize_t total_read_count;
322
323
324 FT_ERROR(( "FT_Stream_Open:" ));
325 FT_ERROR(( " could not `mmap' file `%s'\n", filepathname ));
326
327 stream->base = (unsigned char*)ft_alloc( stream->memory, stream->size );
328
329 if ( !stream->base )
330 {
331 FT_ERROR(( "FT_Stream_Open:" ));
332 FT_ERROR(( " could not `alloc' memory\n" ));
333 goto Fail_Map;
334 }
335
336 total_read_count = 0;
337 do
338 {
339 ssize_t read_count;
340
341
342 read_count = read( file,
343 stream->base + total_read_count,
344 stream->size - total_read_count );
345
346 if ( read_count <= 0 )
347 {
348 if ( read_count == -1 && errno == EINTR )
349 continue;
350
351 FT_ERROR(( "FT_Stream_Open:" ));
352 FT_ERROR(( " error while `read'ing file `%s'\n", filepathname ));
353 goto Fail_Read;
354 }
355
356 total_read_count += read_count;
357
358 } while ( (unsigned long)total_read_count != stream->size );
359
360 stream->close = ft_close_stream_by_free;
361 }
362
363 close( file );
364
365 stream->descriptor.pointer = stream->base;
366 stream->pathname.pointer = (char*)filepathname;
367
368 stream->read = NULL;
369
370 FT_TRACE1(( "FT_Stream_Open:" ));
371 FT_TRACE1(( " opened `%s' (%ld bytes) successfully\n",
372 filepathname, stream->size ));
373
374 return FT_Err_Ok;
375
376 Fail_Read:
377 ft_free( stream->memory, stream->base );
378
379 Fail_Map:
380 close( file );
381
382 stream->base = NULL;
383 stream->size = 0;
384 stream->pos = 0;
385
386 return FT_THROW( Cannot_Open_Stream );
387 }
388
389
390 #ifdef FT_DEBUG_MEMORY
391
392 extern FT_Int
393 ft_mem_debug_init( FT_Memory memory );
394
395 extern void
396 ft_mem_debug_done( FT_Memory memory );
397
398 #endif
399
400
401 /* documentation is in ftobjs.h */
402
403 FT_BASE_DEF( FT_Memory )
404 FT_New_Memory( void )
405 {
406 FT_Memory memory;
407
408
409 memory = (FT_Memory)malloc( sizeof ( *memory ) );
410 if ( memory )
411 {
412 memory->user = NULL;
413 memory->alloc = ft_alloc;
414 memory->realloc = ft_realloc;
415 memory->free = ft_free;
416 #ifdef FT_DEBUG_MEMORY
417 ft_mem_debug_init( memory );
418 #endif
419 }
420
421 return memory;
422 }
423
424
425 /* documentation is in ftobjs.h */
426
427 FT_BASE_DEF( void )
428 FT_Done_Memory( FT_Memory memory )
429 {
430 #ifdef FT_DEBUG_MEMORY
431 ft_mem_debug_done( memory );
432 #endif
433 memory->free( memory, memory );
434 }
435
436
437 /* END */