1 /****************************************************************************
5 * Stream handling (specification).
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.
24 #include <freetype/ftsystem.h>
25 #include <freetype/internal/ftobjs.h>
31 /* format of an 8-bit frame_op value: */
36 /* s is set to 1 if the value is signed. */
37 /* e is set to 1 if the value is little-endian. */
38 /* xxx is a command. */
40 #define FT_FRAME_OP_SHIFT 2
41 #define FT_FRAME_OP_SIGNED 1
42 #define FT_FRAME_OP_LITTLE 2
43 #define FT_FRAME_OP_COMMAND( x ) ( x >> FT_FRAME_OP_SHIFT )
45 #define FT_MAKE_FRAME_OP( command, little, sign ) \
46 ( ( command << FT_FRAME_OP_SHIFT ) | ( little << 1 ) | sign )
48 #define FT_FRAME_OP_END 0
49 #define FT_FRAME_OP_START 1 /* start a new frame */
50 #define FT_FRAME_OP_BYTE 2 /* read 1-byte value */
51 #define FT_FRAME_OP_SHORT 3 /* read 2-byte value */
52 #define FT_FRAME_OP_LONG 4 /* read 4-byte value */
53 #define FT_FRAME_OP_OFF3 5 /* read 3-byte value */
54 #define FT_FRAME_OP_BYTES 6 /* read a bytes sequence */
57 typedef enum FT_Frame_Op_
60 ft_frame_start = FT_MAKE_FRAME_OP( FT_FRAME_OP_START, 0, 0 ),
62 ft_frame_byte = FT_MAKE_FRAME_OP( FT_FRAME_OP_BYTE, 0, 0 ),
63 ft_frame_schar = FT_MAKE_FRAME_OP( FT_FRAME_OP_BYTE, 0, 1 ),
65 ft_frame_ushort_be = FT_MAKE_FRAME_OP( FT_FRAME_OP_SHORT, 0, 0 ),
66 ft_frame_short_be = FT_MAKE_FRAME_OP( FT_FRAME_OP_SHORT, 0, 1 ),
67 ft_frame_ushort_le = FT_MAKE_FRAME_OP( FT_FRAME_OP_SHORT, 1, 0 ),
68 ft_frame_short_le = FT_MAKE_FRAME_OP( FT_FRAME_OP_SHORT, 1, 1 ),
70 ft_frame_ulong_be = FT_MAKE_FRAME_OP( FT_FRAME_OP_LONG, 0, 0 ),
71 ft_frame_long_be = FT_MAKE_FRAME_OP( FT_FRAME_OP_LONG, 0, 1 ),
72 ft_frame_ulong_le = FT_MAKE_FRAME_OP( FT_FRAME_OP_LONG, 1, 0 ),
73 ft_frame_long_le = FT_MAKE_FRAME_OP( FT_FRAME_OP_LONG, 1, 1 ),
75 ft_frame_uoff3_be = FT_MAKE_FRAME_OP( FT_FRAME_OP_OFF3, 0, 0 ),
76 ft_frame_off3_be = FT_MAKE_FRAME_OP( FT_FRAME_OP_OFF3, 0, 1 ),
77 ft_frame_uoff3_le = FT_MAKE_FRAME_OP( FT_FRAME_OP_OFF3, 1, 0 ),
78 ft_frame_off3_le = FT_MAKE_FRAME_OP( FT_FRAME_OP_OFF3, 1, 1 ),
80 ft_frame_bytes = FT_MAKE_FRAME_OP( FT_FRAME_OP_BYTES, 0, 0 ),
81 ft_frame_skip = FT_MAKE_FRAME_OP( FT_FRAME_OP_BYTES, 0, 1 )
86 typedef struct FT_Frame_Field_
95 /* Construct an FT_Frame_Field out of a structure type and a field name. */
96 /* The structure type must be set in the FT_STRUCTURE macro before */
97 /* calling the FT_FRAME_START() macro. */
99 #define FT_FIELD_SIZE( f ) \
100 (FT_Byte)sizeof ( ((FT_STRUCTURE*)0)->f )
102 #define FT_FIELD_SIZE_DELTA( f ) \
103 (FT_Byte)sizeof ( ((FT_STRUCTURE*)0)->f[0] )
105 #define FT_FIELD_OFFSET( f ) \
106 (FT_UShort)( offsetof( FT_STRUCTURE, f ) )
108 #define FT_FRAME_FIELD( frame_op, field ) \
111 FT_FIELD_SIZE( field ), \
112 FT_FIELD_OFFSET( field ) \
115 #define FT_MAKE_EMPTY_FIELD( frame_op ) { frame_op, 0, 0 }
117 #define FT_FRAME_START( size ) { ft_frame_start, 0, size }
118 #define FT_FRAME_END { ft_frame_end, 0, 0 }
120 #define FT_FRAME_LONG( f ) FT_FRAME_FIELD( ft_frame_long_be, f )
121 #define FT_FRAME_ULONG( f ) FT_FRAME_FIELD( ft_frame_ulong_be, f )
122 #define FT_FRAME_SHORT( f ) FT_FRAME_FIELD( ft_frame_short_be, f )
123 #define FT_FRAME_USHORT( f ) FT_FRAME_FIELD( ft_frame_ushort_be, f )
124 #define FT_FRAME_OFF3( f ) FT_FRAME_FIELD( ft_frame_off3_be, f )
125 #define FT_FRAME_UOFF3( f ) FT_FRAME_FIELD( ft_frame_uoff3_be, f )
126 #define FT_FRAME_BYTE( f ) FT_FRAME_FIELD( ft_frame_byte, f )
127 #define FT_FRAME_CHAR( f ) FT_FRAME_FIELD( ft_frame_schar, f )
129 #define FT_FRAME_LONG_LE( f ) FT_FRAME_FIELD( ft_frame_long_le, f )
130 #define FT_FRAME_ULONG_LE( f ) FT_FRAME_FIELD( ft_frame_ulong_le, f )
131 #define FT_FRAME_SHORT_LE( f ) FT_FRAME_FIELD( ft_frame_short_le, f )
132 #define FT_FRAME_USHORT_LE( f ) FT_FRAME_FIELD( ft_frame_ushort_le, f )
133 #define FT_FRAME_OFF3_LE( f ) FT_FRAME_FIELD( ft_frame_off3_le, f )
134 #define FT_FRAME_UOFF3_LE( f ) FT_FRAME_FIELD( ft_frame_uoff3_le, f )
136 #define FT_FRAME_SKIP_LONG { ft_frame_long_be, 0, 0 }
137 #define FT_FRAME_SKIP_SHORT { ft_frame_short_be, 0, 0 }
138 #define FT_FRAME_SKIP_BYTE { ft_frame_byte, 0, 0 }
140 #define FT_FRAME_BYTES( field, count ) \
144 FT_FIELD_OFFSET( field ) \
147 #define FT_FRAME_SKIP_BYTES( count ) { ft_frame_skip, count, 0 }
150 /**************************************************************************
152 * Integer extraction macros -- the 'buffer' parameter must ALWAYS be of
153 * type 'char*' or equivalent (1-byte elements).
156 #define FT_BYTE_( p, i ) ( ((const FT_Byte*)(p))[(i)] )
158 #define FT_INT16( x ) ( (FT_Int16)(x) )
159 #define FT_UINT16( x ) ( (FT_UInt16)(x) )
160 #define FT_INT32( x ) ( (FT_Int32)(x) )
161 #define FT_UINT32( x ) ( (FT_UInt32)(x) )
164 #define FT_BYTE_U16( p, i, s ) ( FT_UINT16( FT_BYTE_( p, i ) ) << (s) )
165 #define FT_BYTE_U32( p, i, s ) ( FT_UINT32( FT_BYTE_( p, i ) ) << (s) )
169 * function acts on increases does range for emits
170 * pointer checking frames error
171 * -------------------------------------------------------------------
172 * FT_PEEK_XXX buffer pointer no no no no
173 * FT_NEXT_XXX buffer pointer yes no no no
174 * FT_GET_XXX stream->cursor yes yes yes no
175 * FT_READ_XXX stream->pos yes yes no yes
180 * `FT_PEEK_XXX' are generic macros to get data from a buffer position. No
181 * safety checks are performed.
183 #define FT_PEEK_SHORT( p ) FT_INT16( FT_BYTE_U16( p, 0, 8 ) | \
184 FT_BYTE_U16( p, 1, 0 ) )
186 #define FT_PEEK_USHORT( p ) FT_UINT16( FT_BYTE_U16( p, 0, 8 ) | \
187 FT_BYTE_U16( p, 1, 0 ) )
189 #define FT_PEEK_LONG( p ) FT_INT32( FT_BYTE_U32( p, 0, 24 ) | \
190 FT_BYTE_U32( p, 1, 16 ) | \
191 FT_BYTE_U32( p, 2, 8 ) | \
192 FT_BYTE_U32( p, 3, 0 ) )
194 #define FT_PEEK_ULONG( p ) FT_UINT32( FT_BYTE_U32( p, 0, 24 ) | \
195 FT_BYTE_U32( p, 1, 16 ) | \
196 FT_BYTE_U32( p, 2, 8 ) | \
197 FT_BYTE_U32( p, 3, 0 ) )
199 #define FT_PEEK_OFF3( p ) ( FT_INT32( FT_BYTE_U32( p, 0, 24 ) | \
200 FT_BYTE_U32( p, 1, 16 ) | \
201 FT_BYTE_U32( p, 2, 8 ) ) >> 8 )
203 #define FT_PEEK_UOFF3( p ) FT_UINT32( FT_BYTE_U32( p, 0, 16 ) | \
204 FT_BYTE_U32( p, 1, 8 ) | \
205 FT_BYTE_U32( p, 2, 0 ) )
207 #define FT_PEEK_SHORT_LE( p ) FT_INT16( FT_BYTE_U16( p, 1, 8 ) | \
208 FT_BYTE_U16( p, 0, 0 ) )
210 #define FT_PEEK_USHORT_LE( p ) FT_UINT16( FT_BYTE_U16( p, 1, 8 ) | \
211 FT_BYTE_U16( p, 0, 0 ) )
213 #define FT_PEEK_LONG_LE( p ) FT_INT32( FT_BYTE_U32( p, 3, 24 ) | \
214 FT_BYTE_U32( p, 2, 16 ) | \
215 FT_BYTE_U32( p, 1, 8 ) | \
216 FT_BYTE_U32( p, 0, 0 ) )
218 #define FT_PEEK_ULONG_LE( p ) FT_UINT32( FT_BYTE_U32( p, 3, 24 ) | \
219 FT_BYTE_U32( p, 2, 16 ) | \
220 FT_BYTE_U32( p, 1, 8 ) | \
221 FT_BYTE_U32( p, 0, 0 ) )
223 #define FT_PEEK_OFF3_LE( p ) ( FT_INT32( FT_BYTE_U32( p, 2, 24 ) | \
224 FT_BYTE_U32( p, 1, 16 ) | \
225 FT_BYTE_U32( p, 0, 8 ) ) >> 8 )
227 #define FT_PEEK_UOFF3_LE( p ) FT_UINT32( FT_BYTE_U32( p, 2, 16 ) | \
228 FT_BYTE_U32( p, 1, 8 ) | \
229 FT_BYTE_U32( p, 0, 0 ) )
232 * `FT_NEXT_XXX' are generic macros to get data from a buffer position
233 * which is then increased appropriately. No safety checks are performed.
235 #define FT_NEXT_CHAR( buffer ) \
236 ( (signed char)*buffer++ )
238 #define FT_NEXT_BYTE( buffer ) \
239 ( (unsigned char)*buffer++ )
241 #define FT_NEXT_SHORT( buffer ) \
242 ( buffer += 2, FT_PEEK_SHORT( buffer - 2 ) )
244 #define FT_NEXT_USHORT( buffer ) \
245 ( buffer += 2, FT_PEEK_USHORT( buffer - 2 ) )
247 #define FT_NEXT_OFF3( buffer ) \
248 ( buffer += 3, FT_PEEK_OFF3( buffer - 3 ) )
250 #define FT_NEXT_UOFF3( buffer ) \
251 ( buffer += 3, FT_PEEK_UOFF3( buffer - 3 ) )
253 #define FT_NEXT_LONG( buffer ) \
254 ( buffer += 4, FT_PEEK_LONG( buffer - 4 ) )
256 #define FT_NEXT_ULONG( buffer ) \
257 ( buffer += 4, FT_PEEK_ULONG( buffer - 4 ) )
260 #define FT_NEXT_SHORT_LE( buffer ) \
261 ( buffer += 2, FT_PEEK_SHORT_LE( buffer - 2 ) )
263 #define FT_NEXT_USHORT_LE( buffer ) \
264 ( buffer += 2, FT_PEEK_USHORT_LE( buffer - 2 ) )
266 #define FT_NEXT_OFF3_LE( buffer ) \
267 ( buffer += 3, FT_PEEK_OFF3_LE( buffer - 3 ) )
269 #define FT_NEXT_UOFF3_LE( buffer ) \
270 ( buffer += 3, FT_PEEK_UOFF3_LE( buffer - 3 ) )
272 #define FT_NEXT_LONG_LE( buffer ) \
273 ( buffer += 4, FT_PEEK_LONG_LE( buffer - 4 ) )
275 #define FT_NEXT_ULONG_LE( buffer ) \
276 ( buffer += 4, FT_PEEK_ULONG_LE( buffer - 4 ) )
279 /**************************************************************************
281 * The `FT_GET_XXX` macros use an implicit 'stream' variable.
283 * Note that a call to `FT_STREAM_SEEK` or `FT_STREAM_POS` has **no**
284 * effect on `FT_GET_XXX`! They operate on `stream->pos`, while
285 * `FT_GET_XXX` use `stream->cursor`.
288 #define FT_GET_MACRO( type ) FT_NEXT_ ## type ( stream->cursor )
290 #define FT_GET_CHAR() FT_GET_MACRO( CHAR )
291 #define FT_GET_BYTE() FT_GET_MACRO( BYTE )
292 #define FT_GET_SHORT() FT_GET_MACRO( SHORT )
293 #define FT_GET_USHORT() FT_GET_MACRO( USHORT )
294 #define FT_GET_OFF3() FT_GET_MACRO( OFF3 )
295 #define FT_GET_UOFF3() FT_GET_MACRO( UOFF3 )
296 #define FT_GET_LONG() FT_GET_MACRO( LONG )
297 #define FT_GET_ULONG() FT_GET_MACRO( ULONG )
298 #define FT_GET_TAG4() FT_GET_MACRO( ULONG )
300 #define FT_GET_SHORT_LE() FT_GET_MACRO( SHORT_LE )
301 #define FT_GET_USHORT_LE() FT_GET_MACRO( USHORT_LE )
302 #define FT_GET_LONG_LE() FT_GET_MACRO( LONG_LE )
303 #define FT_GET_ULONG_LE() FT_GET_MACRO( ULONG_LE )
306 #define FT_GET_MACRO( func, type ) ( (type)func( stream ) )
308 #define FT_GET_CHAR() FT_GET_MACRO( FT_Stream_GetByte, FT_Char )
309 #define FT_GET_BYTE() FT_GET_MACRO( FT_Stream_GetByte, FT_Byte )
310 #define FT_GET_SHORT() FT_GET_MACRO( FT_Stream_GetUShort, FT_Int16 )
311 #define FT_GET_USHORT() FT_GET_MACRO( FT_Stream_GetUShort, FT_UInt16 )
312 #define FT_GET_UOFF3() FT_GET_MACRO( FT_Stream_GetUOffset, FT_UInt32 )
313 #define FT_GET_LONG() FT_GET_MACRO( FT_Stream_GetULong, FT_Int32 )
314 #define FT_GET_ULONG() FT_GET_MACRO( FT_Stream_GetULong, FT_UInt32 )
315 #define FT_GET_TAG4() FT_GET_MACRO( FT_Stream_GetULong, FT_UInt32 )
317 #define FT_GET_SHORT_LE() FT_GET_MACRO( FT_Stream_GetUShortLE, FT_Int16 )
318 #define FT_GET_USHORT_LE() FT_GET_MACRO( FT_Stream_GetUShortLE, FT_UInt16 )
319 #define FT_GET_LONG_LE() FT_GET_MACRO( FT_Stream_GetULongLE, FT_Int32 )
320 #define FT_GET_ULONG_LE() FT_GET_MACRO( FT_Stream_GetULongLE, FT_UInt32 )
324 #define FT_READ_MACRO( func, type, var ) \
325 ( var = (type)func( stream, &error ), \
329 * The `FT_READ_XXX' macros use implicit `stream' and `error' variables.
331 * `FT_READ_XXX' can be controlled with `FT_STREAM_SEEK' and
332 * `FT_STREAM_POS'. They use the full machinery to check whether a read is
335 #define FT_READ_BYTE( var ) FT_READ_MACRO( FT_Stream_ReadByte, FT_Byte, var )
336 #define FT_READ_CHAR( var ) FT_READ_MACRO( FT_Stream_ReadByte, FT_Char, var )
337 #define FT_READ_SHORT( var ) FT_READ_MACRO( FT_Stream_ReadUShort, FT_Int16, var )
338 #define FT_READ_USHORT( var ) FT_READ_MACRO( FT_Stream_ReadUShort, FT_UInt16, var )
339 #define FT_READ_UOFF3( var ) FT_READ_MACRO( FT_Stream_ReadUOffset, FT_UInt32, var )
340 #define FT_READ_LONG( var ) FT_READ_MACRO( FT_Stream_ReadULong, FT_Int32, var )
341 #define FT_READ_ULONG( var ) FT_READ_MACRO( FT_Stream_ReadULong, FT_UInt32, var )
343 #define FT_READ_SHORT_LE( var ) FT_READ_MACRO( FT_Stream_ReadUShortLE, FT_Int16, var )
344 #define FT_READ_USHORT_LE( var ) FT_READ_MACRO( FT_Stream_ReadUShortLE, FT_UInt16, var )
345 #define FT_READ_LONG_LE( var ) FT_READ_MACRO( FT_Stream_ReadULongLE, FT_Int32, var )
346 #define FT_READ_ULONG_LE( var ) FT_READ_MACRO( FT_Stream_ReadULongLE, FT_UInt32, var )
349 #ifndef FT_CONFIG_OPTION_NO_DEFAULT_SYSTEM
351 /* initialize a stream for reading a regular system stream */
353 FT_Stream_Open( FT_Stream stream,
354 const char* filepathname );
356 #endif /* FT_CONFIG_OPTION_NO_DEFAULT_SYSTEM */
359 /* create a new (input) stream from an FT_Open_Args structure */
361 FT_Stream_New( FT_Library library,
362 const FT_Open_Args* args,
363 FT_Stream *astream );
367 FT_Stream_Free( FT_Stream stream,
370 /* initialize a stream for reading in-memory data */
372 FT_Stream_OpenMemory( FT_Stream stream,
376 /* close a stream (does not destroy the stream structure) */
378 FT_Stream_Close( FT_Stream stream );
381 /* seek within a stream. position is relative to start of stream */
383 FT_Stream_Seek( FT_Stream stream,
386 /* skip bytes in a stream */
388 FT_Stream_Skip( FT_Stream stream,
391 /* return current stream position */
393 FT_Stream_Pos( FT_Stream stream );
395 /* read bytes from a stream into a user-allocated buffer, returns an */
396 /* error if not all bytes could be read. */
398 FT_Stream_Read( FT_Stream stream,
402 /* read bytes from a stream at a given position */
404 FT_Stream_ReadAt( FT_Stream stream,
409 /* try to read bytes at the end of a stream; return number of bytes */
410 /* really available */
412 FT_Stream_TryRead( FT_Stream stream,
416 /* Enter a frame of `count' consecutive bytes in a stream. Returns an */
417 /* error if the frame could not be read/accessed. The caller can use */
418 /* the `FT_Stream_GetXXX' functions to retrieve frame data without */
421 /* You must _always_ call `FT_Stream_ExitFrame' once you have entered */
422 /* a stream frame! */
424 /* Nested frames are not permitted. */
427 FT_Stream_EnterFrame( FT_Stream stream,
430 /* exit a stream frame */
432 FT_Stream_ExitFrame( FT_Stream stream );
435 /* Extract a stream frame. If the stream is disk-based, a heap block */
436 /* is allocated and the frame bytes are read into it. If the stream */
437 /* is memory-based, this function simply sets a pointer to the data. */
439 /* Useful to optimize access to memory-based streams transparently. */
441 /* `FT_Stream_GetXXX' functions can't be used. */
443 /* An extracted frame must be `freed' with a call to the function */
444 /* `FT_Stream_ReleaseFrame'. */
447 FT_Stream_ExtractFrame( FT_Stream stream,
451 /* release an extract frame (see `FT_Stream_ExtractFrame') */
453 FT_Stream_ReleaseFrame( FT_Stream stream,
457 /* read a byte from an entered frame */
459 FT_Stream_GetByte( FT_Stream stream );
461 /* read a 16-bit big-endian unsigned integer from an entered frame */
463 FT_Stream_GetUShort( FT_Stream stream );
465 /* read a 24-bit big-endian unsigned integer from an entered frame */
467 FT_Stream_GetUOffset( FT_Stream stream );
469 /* read a 32-bit big-endian unsigned integer from an entered frame */
471 FT_Stream_GetULong( FT_Stream stream );
473 /* read a 16-bit little-endian unsigned integer from an entered frame */
475 FT_Stream_GetUShortLE( FT_Stream stream );
477 /* read a 32-bit little-endian unsigned integer from an entered frame */
479 FT_Stream_GetULongLE( FT_Stream stream );
482 /* read a byte from a stream */
484 FT_Stream_ReadByte( FT_Stream stream,
487 /* read a 16-bit big-endian unsigned integer from a stream */
489 FT_Stream_ReadUShort( FT_Stream stream,
492 /* read a 24-bit big-endian unsigned integer from a stream */
494 FT_Stream_ReadUOffset( FT_Stream stream,
497 /* read a 32-bit big-endian integer from a stream */
499 FT_Stream_ReadULong( FT_Stream stream,
502 /* read a 16-bit little-endian unsigned integer from a stream */
504 FT_Stream_ReadUShortLE( FT_Stream stream,
507 /* read a 32-bit little-endian unsigned integer from a stream */
509 FT_Stream_ReadULongLE( FT_Stream stream,
512 /* Read a structure from a stream. The structure must be described */
513 /* by an array of FT_Frame_Field records. */
515 FT_Stream_ReadFields( FT_Stream stream,
516 const FT_Frame_Field* fields,
520 #define FT_STREAM_POS() \
521 FT_Stream_Pos( stream )
523 #define FT_STREAM_SEEK( position ) \
524 FT_SET_ERROR( FT_Stream_Seek( stream, \
525 (FT_ULong)(position) ) )
527 #define FT_STREAM_SKIP( distance ) \
528 FT_SET_ERROR( FT_Stream_Skip( stream, \
529 (FT_Long)(distance) ) )
531 #define FT_STREAM_READ( buffer, count ) \
532 FT_SET_ERROR( FT_Stream_Read( stream, \
533 (FT_Byte*)(buffer), \
534 (FT_ULong)(count) ) )
536 #define FT_STREAM_READ_AT( position, buffer, count ) \
537 FT_SET_ERROR( FT_Stream_ReadAt( stream, \
538 (FT_ULong)(position), \
539 (FT_Byte*)(buffer), \
540 (FT_ULong)(count) ) )
542 #define FT_STREAM_READ_FIELDS( fields, object ) \
543 FT_SET_ERROR( FT_Stream_ReadFields( stream, fields, object ) )
546 #define FT_FRAME_ENTER( size ) \
548 FT_DEBUG_INNER( FT_Stream_EnterFrame( stream, \
549 (FT_ULong)(size) ) ) )
551 #define FT_FRAME_EXIT() \
552 FT_DEBUG_INNER( FT_Stream_ExitFrame( stream ) )
554 #define FT_FRAME_EXTRACT( size, bytes ) \
556 FT_DEBUG_INNER( FT_Stream_ExtractFrame( stream, \
558 (FT_Byte**)&(bytes) ) ) )
560 #define FT_FRAME_RELEASE( bytes ) \
561 FT_DEBUG_INNER( FT_Stream_ReleaseFrame( stream, \
562 (FT_Byte**)&(bytes) ) )
567 #endif /* FTSTREAM_H_ */