ftstream.h revision 7da4824dfb05ab88413b9cb52dff515283f32e52
1/***************************************************************************/
2/*                                                                         */
3/*  ftstream.h                                                             */
4/*                                                                         */
5/*    Stream handling(specification).                                      */
6/*                                                                         */
7/*  Copyright 1996-2001 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#ifndef __FTSTREAM_H__
20#define __FTSTREAM_H__
21
22
23#include <ft2build.h>
24#include FT_INTERNAL_OBJECTS_H
25
26
27FT_BEGIN_HEADER
28
29
30  /* format of an 8-bit frame_op value = [ xxxxx | e | s ] */
31  /* s is set to 1 if the value is signed,                 */
32  /* e is set to 1 if the value is little-endian           */
33  /* xxxxx is a command                                    */
34
35#define FT_FRAME_OP_SHIFT         2
36#define FT_FRAME_OP_SIGNED        1
37#define FT_FRAME_OP_LITTLE        2
38#define FT_FRAME_OP_COMMAND( x )  ( x >> FT_FRAME_OP_SHIFT )
39
40#define FT_MAKE_FRAME_OP( command, little, sign ) \
41          ( ( command << FT_FRAME_OP_SHIFT ) | ( little << 1 ) | sign )
42
43#define FT_FRAME_OP_END   0
44#define FT_FRAME_OP_START 1  /* start a new frame     */
45#define FT_FRAME_OP_BYTE  2  /* read 1-byte value     */
46#define FT_FRAME_OP_SHORT 3  /* read 2-byte value     */
47#define FT_FRAME_OP_LONG  4  /* read 4-byte value     */
48#define FT_FRAME_OP_OFF3  5  /* read 3-byte value     */
49#define FT_FRAME_OP_BYTES 6  /* read a bytes sequence */
50
51
52  typedef enum  FT_Frame_Op_
53  {
54    ft_frame_end       = 0,
55    ft_frame_start     = FT_MAKE_FRAME_OP( FT_FRAME_OP_START, 0, 0 ),
56
57    ft_frame_byte      = FT_MAKE_FRAME_OP( FT_FRAME_OP_BYTE,  0, 0 ),
58    ft_frame_schar     = FT_MAKE_FRAME_OP( FT_FRAME_OP_BYTE,  0, 1 ),
59
60    ft_frame_ushort_be = FT_MAKE_FRAME_OP( FT_FRAME_OP_SHORT, 0, 0 ),
61    ft_frame_short_be  = FT_MAKE_FRAME_OP( FT_FRAME_OP_SHORT, 0, 1 ),
62    ft_frame_ushort_le = FT_MAKE_FRAME_OP( FT_FRAME_OP_SHORT, 1, 0 ),
63    ft_frame_short_le  = FT_MAKE_FRAME_OP( FT_FRAME_OP_SHORT, 1, 1 ),
64
65    ft_frame_ulong_be  = FT_MAKE_FRAME_OP( FT_FRAME_OP_LONG, 0, 0 ),
66    ft_frame_long_be   = FT_MAKE_FRAME_OP( FT_FRAME_OP_LONG, 0, 1 ),
67    ft_frame_ulong_le  = FT_MAKE_FRAME_OP( FT_FRAME_OP_LONG, 1, 0 ),
68    ft_frame_long_le   = FT_MAKE_FRAME_OP( FT_FRAME_OP_LONG, 1, 1 ),
69
70    ft_frame_uoff3_be  = FT_MAKE_FRAME_OP( FT_FRAME_OP_OFF3, 0, 0 ),
71    ft_frame_off3_be   = FT_MAKE_FRAME_OP( FT_FRAME_OP_OFF3, 0, 1 ),
72    ft_frame_uoff3_le  = FT_MAKE_FRAME_OP( FT_FRAME_OP_OFF3, 1, 0 ),
73    ft_frame_off3_le   = FT_MAKE_FRAME_OP( FT_FRAME_OP_OFF3, 1, 1 ),
74
75    ft_frame_bytes     = FT_MAKE_FRAME_OP( FT_FRAME_OP_BYTES, 0, 0 ),
76    ft_frame_skip      = FT_MAKE_FRAME_OP( FT_FRAME_OP_BYTES, 0, 1 )
77
78  } FT_Frame_Op;
79
80
81  typedef struct  FT_Frame_Field_
82  {
83    FT_Byte      value;
84    FT_Byte      size;
85    FT_UShort    offset;
86
87  } FT_Frame_Field;
88
89
90  /* Construct an FT_Frame_Field out of a structure type and a field name. */
91  /* The structure type must be set in the FT_STRUCTURE macro before       */
92  /* calling the FT_FRAME_START() macro.                                   */
93#define FT_FIELD_SIZE( f ) \
94          (FT_Byte)sizeof ( ((FT_STRUCTURE*)0)->f )
95
96#define FT_FIELD_SIZE_DELTA( f ) \
97          (FT_Byte)sizeof ( ((FT_STRUCTURE*)0)->f[0] )
98
99#define FT_FIELD_OFFSET( f ) \
100          (FT_UShort)( offsetof( FT_STRUCTURE, f ) )
101
102#define FT_FRAME_FIELD( frame_op, field ) \
103          {                               \
104            frame_op,                     \
105            FT_FIELD_SIZE( field ),       \
106            FT_FIELD_OFFSET( field )      \
107          }
108
109#define FT_MAKE_EMPTY_FIELD( frame_op )  { frame_op, 0, 0 }
110
111#define FT_FRAME_START( size )   { ft_frame_start, 0, size }
112#define FT_FRAME_END             { ft_frame_end, 0, 0 }
113
114#define FT_FRAME_LONG( f )       FT_FRAME_FIELD( ft_frame_long_be, f )
115#define FT_FRAME_ULONG( f )      FT_FRAME_FIELD( ft_frame_ulong_be, f )
116#define FT_FRAME_SHORT( f )      FT_FRAME_FIELD( ft_frame_short_be, f )
117#define FT_FRAME_USHORT( f )     FT_FRAME_FIELD( ft_frame_ushort_be, f )
118#define FT_FRAME_BYTE( f )       FT_FRAME_FIELD( ft_frame_byte, f )
119#define FT_FRAME_CHAR( f )       FT_FRAME_FIELD( ft_frame_schar, f )
120
121#define FT_FRAME_LONG_LE( f )    FT_FRAME_FIELD( ft_frame_long_le, f )
122#define FT_FRAME_ULONG_LE( f )   FT_FRAME_FIELD( ft_frame_ulong_le, f )
123#define FT_FRAME_SHORT_LE( f )   FT_FRAME_FIELD( ft_frame_short_le, f )
124#define FT_FRAME_USHORT_LE( f )  FT_FRAME_FIELD( ft_frame_ushort_le, f )
125
126#define FT_FRAME_SKIP_LONG       { ft_frame_long_be, 0, 0 }
127#define FT_FRAME_SKIP_SHORT      { ft_frame_short_be, 0, 0 }
128#define FT_FRAME_SKIP_BYTE       { ft_frame_byte, 0, 0 }
129
130#define FT_FRAME_BYTES( field, count ) \
131          {                            \
132            ft_frame_bytes,            \
133            count,                     \
134            FT_FIELD_OFFSET( field )   \
135          }
136
137#define FT_FRAME_SKIP_BYTES( count )  { ft_frame_skip, count, 0 }
138
139
140
141  /*************************************************************************/
142  /*                                                                       */
143  /* integer extraction macros -- the `buffer' parameter must ALWAYS be of */
144  /* type `char*' or equivalent (1-byte elements).                         */
145  /*                                                                       */
146
147#define FT_GET_SHORT_BE( p )                                   \
148          ((FT_Int16)( ( (FT_Int16)(FT_Char)(p)[0] <<  8 ) |   \
149                         (FT_Int16)(FT_Byte)(p)[1]         ) )
150
151#define FT_GET_USHORT_BE( p )                                   \
152          ((FT_Int16)( ( (FT_UInt16)(FT_Byte)(p)[0] <<  8 ) |   \
153                         (FT_UInt16)(FT_Byte)(p)[1]         ) )
154
155#define FT_GET_OFF3_BE( p )                                      \
156          ( (FT_Int32) ( ( (FT_Int32)(FT_Char)(p)[0] << 16 ) |   \
157                         ( (FT_Int32)(FT_Byte)(p)[1] <<  8 ) |   \
158                           (FT_Int32)(FT_Byte)(p)[2]         ) )
159
160#define FT_GET_UOFF3_BE( p )                                      \
161          ( (FT_Int32) ( ( (FT_UInt32)(FT_Byte)(p)[0] << 16 ) |   \
162                         ( (FT_UInt32)(FT_Byte)(p)[1] <<  8 ) |   \
163                           (FT_UInt32)(FT_Byte)(p)[2]         ) )
164
165#define FT_GET_LONG_BE( p )                                      \
166          ( (FT_Int32) ( ( (FT_Int32)(FT_Char)(p)[0] << 24 ) |   \
167                         ( (FT_Int32)(FT_Byte)(p)[1] << 16 ) |   \
168                         ( (FT_Int32)(FT_Byte)(p)[2] <<  8 ) |   \
169                           (FT_Int32)(FT_Byte)(p)[3]         ) )
170
171#define FT_GET_ULONG_BE( p )                                      \
172          ( (FT_Int32) ( ( (FT_UInt32)(FT_Byte)(p)[0] << 24 ) |   \
173                         ( (FT_UInt32)(FT_Byte)(p)[1] << 16 ) |   \
174                         ( (FT_UInt32)(FT_Byte)(p)[2] <<  8 ) |   \
175                           (FT_UInt32)(FT_Byte)(p)[3]         ) )
176
177#define FT_GET_SHORT_LE( p )                                   \
178          ((FT_Int16)( ( (FT_Int16)(FT_Char)(p)[1] <<  8 ) |   \
179                         (FT_Int16)(FT_Byte)(p)[0]         ) )
180
181#define FT_GET_USHORT_LE( p )                                   \
182          ((FT_Int16)( ( (FT_UInt16)(FT_Byte)(p)[1] <<  8 ) |   \
183                         (FT_UInt16)(FT_Byte)(p)[0]         ) )
184
185#define FT_GET_OFF3_LE( p )                                      \
186          ( (FT_Int32) ( ( (FT_Int32)(FT_Char)(p)[2] << 16 ) |   \
187                         ( (FT_Int32)(FT_Byte)(p)[1] <<  8 ) |   \
188                           (FT_Int32)(FT_Byte)(p)[0]         ) )
189
190#define FT_GET_UOFF3_LE( p )                                      \
191          ( (FT_Int32) ( ( (FT_UInt32)(FT_Byte)(p)[2] << 16 ) |   \
192                         ( (FT_UInt32)(FT_Byte)(p)[1] <<  8 ) |   \
193                           (FT_UInt32)(FT_Byte)(p)[0]         ) )
194
195#define FT_GET_LONG_LE( p )                                      \
196          ( (FT_Int32) ( ( (FT_Int32)(FT_Char)(p)[3] << 24 ) |   \
197                         ( (FT_Int32)(FT_Byte)(p)[2] << 16 ) |   \
198                         ( (FT_Int32)(FT_Byte)(p)[1] <<  8 ) |   \
199                           (FT_Int32)(FT_Byte)(p)[0]         ) )
200
201#define FT_GET_ULONG_LE( p )                                      \
202          ( (FT_Int32) ( ( (FT_UInt32)(FT_Byte)(p)[3] << 24 ) |   \
203                         ( (FT_UInt32)(FT_Byte)(p)[2] << 16 ) |   \
204                         ( (FT_UInt32)(FT_Byte)(p)[1] <<  8 ) |   \
205                           (FT_UInt32)(FT_Byte)(p)[0]         ) )
206
207
208#define NEXT_Char( buffer )          \
209          ( (signed char)*buffer++ )
210
211#define NEXT_Byte( buffer )            \
212          ( (unsigned char)*buffer++ )
213
214#define NEXT_Short( buffer )                                        \
215          ( (short)( buffer += 2, FT_GET_SHORT_BE( buffer - 2 ) ) )
216
217#define NEXT_UShort( buffer )                                                 \
218          ( (unsigned short)( buffer += 2, FT_GET_USHORT_BE( buffer - 2 ) ) )
219
220#define NEXT_Offset( buffer )                                     \
221          ( (long)( buffer += 3, FT_GET_OFF3_BE( buffer - 3 ) ) )
222
223#define NEXT_UOffset( buffer )                                              \
224          ( (unsigned long)( buffer += 3, FT_GET_UOFF3_BE( buffer - 3 ) ) )
225
226#define NEXT_Long( buffer )                                       \
227          ( (long)( buffer += 4, FT_GET_LONG_BE( buffer - 4 ) ) )
228
229#define NEXT_ULong( buffer )                                                \
230          ( (unsigned long)( buffer += 4, FT_GET_ULONG_BE( buffer - 4 ) ) )
231
232
233#define NEXT_ShortLE( buffer )                                      \
234          ( (short)( buffer += 2, FT_GET_SHORT_LE( buffer - 2 ) ) )
235
236#define NEXT_UShortLE( buffer )                                               \
237          ( (unsigned short)( buffer += 2, FT_GET_USHORT_LE( buffer - 2 ) ) )
238
239#define NEXT_OffsetLE( buffer )                                   \
240          ( (long)( buffer += 3, FT_GET_OFF3_LE( buffer - 3 ) ) )
241
242#define NEXT_UOffsetLE( buffer )                                            \
243          ( (unsigned long)( buffer += 3, FT_GET_UOFF3_LE( buffer - 3 ) ) )
244
245
246#define NEXT_LongLE( buffer )                                     \
247          ( (long)( buffer += 4, FT_GET_LONG_LE( buffer - 4 ) ) )
248
249#define NEXT_ULongLE( buffer )                                              \
250          ( (unsigned long)( buffer += 4, FT_GET_ULONG_LE( buffer - 4 ) ) )
251
252
253  /*************************************************************************/
254  /*                                                                       */
255  /* Each GET_xxxx() macro uses an implicit `stream' variable.             */
256  /*                                                                       */
257#define FT_GET_MACRO( func, type )        ( (type)func( stream ) )
258
259#define GET_Char()      FT_GET_MACRO( FT_Get_Char, FT_Char )
260#define GET_Byte()      FT_GET_MACRO( FT_Get_Char, FT_Byte )
261#define GET_Short()     FT_GET_MACRO( FT_Get_Short, FT_Short )
262#define GET_UShort()    FT_GET_MACRO( FT_Get_Short, FT_UShort )
263#define GET_Offset()    FT_GET_MACRO( FT_Get_Offset, FT_Long )
264#define GET_UOffset()   FT_GET_MACRO( FT_Get_Offset, FT_ULong )
265#define GET_Long()      FT_GET_MACRO( FT_Get_Long, FT_Long )
266#define GET_ULong()     FT_GET_MACRO( FT_Get_Long, FT_ULong )
267#define GET_Tag4()      FT_GET_MACRO( FT_Get_Long, FT_ULong )
268
269#define GET_ShortLE()   FT_GET_MACRO( FT_Get_ShortLE, FT_Short )
270#define GET_UShortLE()  FT_GET_MACRO( FT_Get_ShortLE, FT_UShort )
271#define GET_LongLE()    FT_GET_MACRO( FT_Get_LongLE, FT_Long )
272#define GET_ULongLE()   FT_GET_MACRO( FT_Get_LongLE, FT_ULong )
273
274#define FT_READ_MACRO( func, type, var )        \
275          ( var = (type)func( stream, &error ), \
276            error != FT_Err_Ok )
277
278#define READ_Byte( var )      FT_READ_MACRO( FT_Read_Char, FT_Byte, var )
279#define READ_Char( var )      FT_READ_MACRO( FT_Read_Char, FT_Char, var )
280#define READ_Short( var )     FT_READ_MACRO( FT_Read_Short, FT_Short, var )
281#define READ_UShort( var )    FT_READ_MACRO( FT_Read_Short, FT_UShort, var )
282#define READ_Offset( var )    FT_READ_MACRO( FT_Read_Offset, FT_Long, var )
283#define READ_UOffset( var )   FT_READ_MACRO( FT_Read_Offset, FT_ULong, var )
284#define READ_Long( var )      FT_READ_MACRO( FT_Read_Long, FT_Long, var )
285#define READ_ULong( var )     FT_READ_MACRO( FT_Read_Long, FT_ULong, var )
286
287#define READ_ShortLE( var )   FT_READ_MACRO( FT_Read_ShortLE, FT_Short, var )
288#define READ_UShortLE( var )  FT_READ_MACRO( FT_Read_ShortLE, FT_UShort, var )
289#define READ_LongLE( var )    FT_READ_MACRO( FT_Read_LongLE, FT_Long, var )
290#define READ_ULongLE( var )   FT_READ_MACRO( FT_Read_LongLE, FT_ULong, var )
291
292
293  FT_BASE( void )
294  FT_New_Memory_Stream( FT_Library  library,
295                        FT_Byte*    base,
296                        FT_ULong    size,
297                        FT_Stream   stream );
298
299  FT_BASE( FT_Error )
300  FT_Seek_Stream( FT_Stream  stream,
301                  FT_ULong   pos );
302
303  FT_BASE( FT_Error )
304  FT_Skip_Stream( FT_Stream  stream,
305                  FT_Long    distance );
306
307  FT_BASE( FT_Long )
308  FT_Stream_Pos( FT_Stream  stream );
309
310
311  FT_BASE( FT_Error )
312  FT_Read_Stream( FT_Stream  stream,
313                  FT_Byte*   buffer,
314                  FT_ULong   count );
315
316  FT_BASE( FT_Error )
317  FT_Read_Stream_At( FT_Stream  stream,
318                     FT_ULong   pos,
319                     FT_Byte*   buffer,
320                     FT_ULong   count );
321
322  FT_BASE( FT_Error )
323  FT_Access_Frame( FT_Stream  stream,
324                   FT_ULong   count );
325
326  FT_BASE( void )
327  FT_Forget_Frame( FT_Stream  stream );
328
329  FT_BASE( FT_Error )
330  FT_Extract_Frame( FT_Stream  stream,
331                    FT_ULong   count,
332                    FT_Byte**  pbytes );
333
334  FT_BASE( void )
335  FT_Release_Frame( FT_Stream  stream,
336                    FT_Byte**  pbytes );
337
338  FT_BASE( FT_Char )
339  FT_Get_Char( FT_Stream  stream );
340
341  FT_BASE( FT_Short )
342  FT_Get_Short( FT_Stream  stream );
343
344  FT_BASE( FT_Long )
345  FT_Get_Offset( FT_Stream  stream );
346
347  FT_BASE( FT_Long )
348  FT_Get_Long( FT_Stream  stream );
349
350  FT_BASE( FT_Short )
351  FT_Get_ShortLE( FT_Stream  stream );
352
353  FT_BASE( FT_Long )
354  FT_Get_LongLE( FT_Stream  stream );
355
356
357  FT_BASE( FT_Char )
358  FT_Read_Char( FT_Stream  stream,
359                FT_Error*  error );
360
361  FT_BASE( FT_Short )
362  FT_Read_Short( FT_Stream  stream,
363                 FT_Error*  error );
364
365  FT_BASE( FT_Long )
366  FT_Read_Offset( FT_Stream  stream,
367                  FT_Error*  error );
368
369  FT_BASE( FT_Long )
370  FT_Read_Long( FT_Stream  stream,
371                FT_Error*  error );
372
373  FT_BASE( FT_Short )
374  FT_Read_ShortLE( FT_Stream  stream,
375                   FT_Error*  error );
376
377  FT_BASE( FT_Long )
378  FT_Read_LongLE( FT_Stream  stream,
379                  FT_Error*  error );
380
381  FT_BASE( FT_Error )
382  FT_Read_Fields( FT_Stream              stream,
383                  const FT_Frame_Field*  fields,
384                  void*                  structure );
385
386
387#define USE_Stream( resource, stream )                       \
388          FT_SET_ERROR( FT_Open_Stream( resource, stream ) )
389
390#define DONE_Stream( stream )      \
391          FT_Done_Stream( stream )
392
393
394#define ACCESS_Frame( size )                              \
395          FT_SET_ERROR( FT_Access_Frame( stream, size ) )
396
397#define FORGET_Frame()              \
398          FT_Forget_Frame( stream )
399
400#define EXTRACT_Frame( size, bytes )                              \
401          FT_SET_ERROR( FT_Extract_Frame( stream, size,           \
402                                          (FT_Byte**)&(bytes) ) )
403
404#define RELEASE_Frame( bytes )                            \
405          FT_Release_Frame( stream, (FT_Byte**)&(bytes) )
406
407#define FILE_Seek( position )                                \
408          FT_SET_ERROR( FT_Seek_Stream( stream, position ) )
409
410#define FILE_Skip( distance )                                \
411          FT_SET_ERROR( FT_Skip_Stream( stream, distance ) )
412
413#define FILE_Pos()                \
414          FT_Stream_Pos( stream )
415
416#define FILE_Read( buffer, count )                        \
417          FT_SET_ERROR( FT_Read_Stream( stream,           \
418                                        (FT_Byte*)buffer, \
419                                        count ) )
420
421#define FILE_Read_At( position, buffer, count )              \
422          FT_SET_ERROR( FT_Read_Stream_At( stream,           \
423                                           position,         \
424                                           (FT_Byte*)buffer, \
425                                           count ) )
426
427#define READ_Fields( fields, object )  \
428        ( ( error = FT_Read_Fields( stream, fields, object ) ) != FT_Err_Ok )
429
430
431FT_END_HEADER
432
433#endif /* __FTSTREAM_H__ */
434
435
436/* END */
437