~ubuntu-branches/ubuntu/raring/libxfont/raring-security

« back to all changes in this revision

Viewing changes to src/FreeType/ftsystem.c

  • Committer: Bazaar Package Importer
  • Author(s): Julien Cristau
  • Date: 2008-03-07 13:32:43 UTC
  • mfrom: (1.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20080307133243-oe2mgbme0mqtwihp
Tags: 1:1.3.2-1
* New upstream release
* Drop CVE-2008-0006.diff, included upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************/
2
 
/*                                                                         */
3
 
/*  ftsystem.c                                                             */
4
 
/*                                                                         */
5
 
/*    ANSI-specific FreeType low-level system interface (body).            */
6
 
/*                                                                         */
7
 
/*  Copyright 1996-2001, 2002 by                                           */
8
 
/*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
9
 
/*                                                                         */
10
 
/*  Modified for XFree86.                                                  */
11
 
/*                                                                         */
12
 
/*  This file is part of the FreeType project, and may only be used,       */
13
 
/*  modified, and distributed under the terms of the FreeType project      */
14
 
/*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
15
 
/*  this file you indicate that you have read the license and              */
16
 
/*  understand and accept it fully.                                        */
17
 
/*                                                                         */
18
 
/***************************************************************************/
19
 
 
20
 
/* Modified for XFree86 */
21
 
/* $XFree86$ */
22
 
 
23
 
  /*************************************************************************/
24
 
  /*                                                                       */
25
 
  /* This file contains the default interface used by FreeType to access   */
26
 
  /* low-level, i.e. memory management, i/o access as well as thread       */
27
 
  /* synchronisation.  It can be replaced by user-specific routines if     */
28
 
  /* necessary.                                                            */
29
 
  /*                                                                       */
30
 
  /*************************************************************************/
31
 
 
32
 
 
33
 
#ifdef HAVE_CONFIG_H
34
 
#include <config.h>
35
 
#endif
36
 
#include <ft2build.h>
37
 
#include FT_CONFIG_CONFIG_H
38
 
#include FT_SYSTEM_H
39
 
#include FT_ERRORS_H
40
 
#include FT_TYPES_H
41
 
 
42
 
#ifndef FONTMODULE
43
 
#include <stdio.h>
44
 
#include <stdlib.h>
45
 
#else
46
 
#include "Xmd.h"
47
 
#define _XTYPEDEF_BOOL
48
 
#include "Xdefs.h"
49
 
#define DONT_DEFINE_WRAPPERS
50
 
#include "xf86_ansic.h"
51
 
#undef DONT_DEFINE_WRAPPERS
52
 
#define malloc(x) xf86malloc(x)
53
 
#define realloc(x, y) xf86realloc(x, y)
54
 
#define free(x) xf86free(x)
55
 
#define FILE XF86FILE
56
 
#define fopen(x, y) xf86fopen(x, y)
57
 
#define fclose(x) xf86fclose(x)
58
 
#define fseek(x, y, z) xf86fseek(x, y, z)
59
 
#define ftell(x) xf86ftell(x)
60
 
#define SEEK_SET XF86_SEEK_SET
61
 
#define SEEK_END XF86_SEEK_END
62
 
#define fread(x, y, z, t) xf86fread(x, y, z, t)
63
 
#endif
64
 
 
65
 
 
66
 
  /*************************************************************************/
67
 
  /*                                                                       */
68
 
  /*                       MEMORY MANAGEMENT INTERFACE                     */
69
 
  /*                                                                       */
70
 
  /*************************************************************************/
71
 
 
72
 
  /*************************************************************************/
73
 
  /*                                                                       */
74
 
  /* It is not necessary to do any error checking for the                  */
75
 
  /* allocation-related functions.  This will be done by the higher level  */
76
 
  /* routines like FT_Alloc() or FT_Realloc().                             */
77
 
  /*                                                                       */
78
 
  /*************************************************************************/
79
 
 
80
 
 
81
 
  /*************************************************************************/
82
 
  /*                                                                       */
83
 
  /* <Function>                                                            */
84
 
  /*    ft_alloc                                                           */
85
 
  /*                                                                       */
86
 
  /* <Description>                                                         */
87
 
  /*    The memory allocation function.                                    */
88
 
  /*                                                                       */
89
 
  /* <Input>                                                               */
90
 
  /*    memory :: A pointer to the memory object.                          */
91
 
  /*                                                                       */
92
 
  /*    size   :: The requested size in bytes.                             */
93
 
  /*                                                                       */
94
 
  /* <Return>                                                              */
95
 
  /*    The address of newly allocated block.                              */
96
 
  /*                                                                       */
97
 
  FT_CALLBACK_DEF( void* )
98
 
  ft_alloc( FT_Memory  memory,
99
 
            long       size )
100
 
  {
101
 
    FT_UNUSED( memory );
102
 
 
103
 
    return malloc( size );
104
 
  }
105
 
 
106
 
 
107
 
  /*************************************************************************/
108
 
  /*                                                                       */
109
 
  /* <Function>                                                            */
110
 
  /*    ft_realloc                                                         */
111
 
  /*                                                                       */
112
 
  /* <Description>                                                         */
113
 
  /*    The memory reallocation function.                                  */
114
 
  /*                                                                       */
115
 
  /* <Input>                                                               */
116
 
  /*    memory   :: A pointer to the memory object.                        */
117
 
  /*                                                                       */
118
 
  /*    cur_size :: The current size of the allocated memory block.        */
119
 
  /*                                                                       */
120
 
  /*    new_size :: The newly requested size in bytes.                     */
121
 
  /*                                                                       */
122
 
  /*    block    :: The current address of the block in memory.            */
123
 
  /*                                                                       */
124
 
  /* <Return>                                                              */
125
 
  /*    The address of the reallocated memory block.                       */
126
 
  /*                                                                       */
127
 
  FT_CALLBACK_DEF( void* )
128
 
  ft_realloc( FT_Memory  memory,
129
 
              long       cur_size,
130
 
              long       new_size,
131
 
              void*      block )
132
 
  {
133
 
    FT_UNUSED( memory );
134
 
    FT_UNUSED( cur_size );
135
 
 
136
 
    return realloc( block, new_size );
137
 
  }
138
 
 
139
 
 
140
 
  /*************************************************************************/
141
 
  /*                                                                       */
142
 
  /* <Function>                                                            */
143
 
  /*    ft_free                                                            */
144
 
  /*                                                                       */
145
 
  /* <Description>                                                         */
146
 
  /*    The memory release function.                                       */
147
 
  /*                                                                       */
148
 
  /* <Input>                                                               */
149
 
  /*    memory  :: A pointer to the memory object.                         */
150
 
  /*                                                                       */
151
 
  /*    block   :: The address of block in memory to be freed.             */
152
 
  /*                                                                       */
153
 
  FT_CALLBACK_DEF( void )
154
 
  ft_free( FT_Memory  memory,
155
 
           void*      block )
156
 
  {
157
 
    FT_UNUSED( memory );
158
 
 
159
 
    free( block );
160
 
  }
161
 
 
162
 
 
163
 
  /*************************************************************************/
164
 
  /*                                                                       */
165
 
  /*                     RESOURCE MANAGEMENT INTERFACE                     */
166
 
  /*                                                                       */
167
 
  /*************************************************************************/
168
 
 
169
 
 
170
 
  /*************************************************************************/
171
 
  /*                                                                       */
172
 
  /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
173
 
  /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
174
 
  /* messages during execution.                                            */
175
 
  /*                                                                       */
176
 
#undef  FT_COMPONENT
177
 
#define FT_COMPONENT  trace_io
178
 
 
179
 
  /* We use the macro STREAM_FILE for convenience to extract the       */
180
 
  /* system-specific stream handle from a given FreeType stream object */
181
 
#define STREAM_FILE( stream )  ( (FILE*)stream->descriptor.pointer )
182
 
 
183
 
 
184
 
  /*************************************************************************/
185
 
  /*                                                                       */
186
 
  /* <Function>                                                            */
187
 
  /*    ft_ansi_stream_close                                               */
188
 
  /*                                                                       */
189
 
  /* <Description>                                                         */
190
 
  /*    The function to close a stream.                                    */
191
 
  /*                                                                       */
192
 
  /* <Input>                                                               */
193
 
  /*    stream :: A pointer to the stream object.                          */
194
 
  /*                                                                       */
195
 
  FT_CALLBACK_DEF( void )
196
 
  ft_ansi_stream_close( FT_Stream  stream )
197
 
  {
198
 
    fclose( STREAM_FILE( stream ) );
199
 
 
200
 
    stream->descriptor.pointer = NULL;
201
 
    stream->size               = 0;
202
 
    stream->base               = 0;
203
 
  }
204
 
 
205
 
 
206
 
  /*************************************************************************/
207
 
  /*                                                                       */
208
 
  /* <Function>                                                            */
209
 
  /*    ft_ansi_stream_io                                                  */
210
 
  /*                                                                       */
211
 
  /* <Description>                                                         */
212
 
  /*    The function to open a stream.                                     */
213
 
  /*                                                                       */
214
 
  /* <Input>                                                               */
215
 
  /*    stream :: A pointer to the stream object.                          */
216
 
  /*                                                                       */
217
 
  /*    offset :: The position in the data stream to start reading.        */
218
 
  /*                                                                       */
219
 
  /*    buffer :: The address of buffer to store the read data.            */
220
 
  /*                                                                       */
221
 
  /*    count  :: The number of bytes to read from the stream.             */
222
 
  /*                                                                       */
223
 
  /* <Return>                                                              */
224
 
  /*    The number of bytes actually read.                                 */
225
 
  /*                                                                       */
226
 
  FT_CALLBACK_DEF( unsigned long )
227
 
  ft_ansi_stream_io( FT_Stream       stream,
228
 
                     unsigned long   offset,
229
 
                     unsigned char*  buffer,
230
 
                     unsigned long   count )
231
 
  {
232
 
    FILE*  file;
233
 
 
234
 
 
235
 
    file = STREAM_FILE( stream );
236
 
 
237
 
    fseek( file, offset, SEEK_SET );
238
 
 
239
 
    return (unsigned long)fread( buffer, 1, count, file );
240
 
  }
241
 
 
242
 
 
243
 
  /* documentation is in ftobjs.h */
244
 
 
245
 
  FT_EXPORT_DEF( FT_Error )
246
 
  FT_Stream_Open( FT_Stream    stream,
247
 
                  const char*  filepathname )
248
 
  {
249
 
    FILE*  file;
250
 
 
251
 
 
252
 
    if ( !stream )
253
 
      return FT_Err_Invalid_Stream_Handle;
254
 
 
255
 
    file = fopen( filepathname, "rb" );
256
 
    if ( !file )
257
 
    {
258
 
      FT_ERROR(( "FT_Stream_Open:" ));
259
 
      FT_ERROR(( " could not open `%s'\n", filepathname ));
260
 
 
261
 
      return FT_Err_Cannot_Open_Resource;
262
 
    }
263
 
 
264
 
    fseek( file, 0, SEEK_END );
265
 
    stream->size = ftell( file );
266
 
    fseek( file, 0, SEEK_SET );
267
 
 
268
 
    stream->descriptor.pointer = file;
269
 
    stream->pathname.pointer   = (char*)filepathname;
270
 
    stream->pos                = 0;
271
 
 
272
 
    stream->read  = ft_ansi_stream_io;
273
 
    stream->close = ft_ansi_stream_close;
274
 
 
275
 
    FT_TRACE1(( "FT_Stream_Open:" ));
276
 
    FT_TRACE1(( " opened `%s' (%d bytes) successfully\n",
277
 
                filepathname, stream->size ));
278
 
 
279
 
    return FT_Err_Ok;
280
 
  }
281
 
 
282
 
 
283
 
#ifdef FT_DEBUG_MEMORY
284
 
 
285
 
  extern FT_Int
286
 
  ft_mem_debug_init( FT_Memory  memory );
287
 
 
288
 
  extern void
289
 
  ft_mem_debug_done( FT_Memory  memory );
290
 
 
291
 
#endif
292
 
 
293
 
 
294
 
  /* documentation is in ftobjs.h */
295
 
 
296
 
  FT_EXPORT_DEF( FT_Memory )
297
 
  FT_New_Memory( void )
298
 
  {
299
 
    FT_Memory  memory;
300
 
 
301
 
 
302
 
    memory = (FT_Memory)malloc( sizeof ( *memory ) );
303
 
    if ( memory )
304
 
    {
305
 
      memory->user    = 0;
306
 
      memory->alloc   = ft_alloc;
307
 
      memory->realloc = ft_realloc;
308
 
      memory->free    = ft_free;
309
 
#ifdef FT_DEBUG_MEMORY
310
 
      ft_mem_debug_init( memory );
311
 
#endif
312
 
    }
313
 
 
314
 
    return memory;
315
 
  }
316
 
 
317
 
 
318
 
  /* documentation is in ftobjs.h */
319
 
 
320
 
  FT_EXPORT_DEF( void )
321
 
  FT_Done_Memory( FT_Memory  memory )
322
 
  {
323
 
#ifdef FT_DEBUG_MEMORY
324
 
    ft_mem_debug_done( memory );
325
 
#endif
326
 
#undef free
327
 
    memory->free( memory, memory );
328
 
  }
329
 
 
330
 
 
331
 
/* END */