1 /***************************************************************************/
5 /* I/O stream support (body). */
7 /* Copyright 2000-2001, 2002, 2004, 2005, 2006 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. */
16 /***************************************************************************/
20 #include FT_INTERNAL_STREAM_H
21 #include FT_INTERNAL_DEBUG_H
24 /*************************************************************************/
26 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
27 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
28 /* messages during execution. */
31 #define FT_COMPONENT trace_stream
35 FT_Stream_OpenMemory( FT_Stream stream
,
39 stream
->base
= (FT_Byte
*) base
;
49 FT_Stream_Close( FT_Stream stream
)
51 if ( stream
&& stream
->close
)
52 stream
->close( stream
);
56 FT_BASE_DEF( FT_Error
)
57 FT_Stream_Seek( FT_Stream stream
,
60 FT_Error error
= FT_Err_Ok
;
67 if ( stream
->read( stream
, pos
, 0, 0 ) )
69 FT_ERROR(( "FT_Stream_Seek: invalid i/o; pos = 0x%lx, size = 0x%lx\n",
72 error
= FT_Err_Invalid_Stream_Operation
;
75 /* note that seeking to the first position after the file is valid */
76 else if ( pos
> stream
->size
)
78 FT_ERROR(( "FT_Stream_Seek: invalid i/o; pos = 0x%lx, size = 0x%lx\n",
81 error
= FT_Err_Invalid_Stream_Operation
;
88 FT_BASE_DEF( FT_Error
)
89 FT_Stream_Skip( FT_Stream stream
,
92 return FT_Stream_Seek( stream
, (FT_ULong
)( stream
->pos
+ distance
) );
96 FT_BASE_DEF( FT_Long
)
97 FT_Stream_Pos( FT_Stream stream
)
103 FT_BASE_DEF( FT_Error
)
104 FT_Stream_Read( FT_Stream stream
,
108 return FT_Stream_ReadAt( stream
, stream
->pos
, buffer
, count
);
112 FT_BASE_DEF( FT_Error
)
113 FT_Stream_ReadAt( FT_Stream stream
,
118 FT_Error error
= FT_Err_Ok
;
122 if ( pos
>= stream
->size
)
124 FT_ERROR(( "FT_Stream_ReadAt: invalid i/o; pos = 0x%lx, size = 0x%lx\n",
125 pos
, stream
->size
));
127 return FT_Err_Invalid_Stream_Operation
;
131 read_bytes
= stream
->read( stream
, pos
, buffer
, count
);
134 read_bytes
= stream
->size
- pos
;
135 if ( read_bytes
> count
)
138 FT_MEM_COPY( buffer
, stream
->base
+ pos
, read_bytes
);
141 stream
->pos
= pos
+ read_bytes
;
143 if ( read_bytes
< count
)
145 FT_ERROR(( "FT_Stream_ReadAt:" ));
146 FT_ERROR(( " invalid read; expected %lu bytes, got %lu\n",
147 count
, read_bytes
));
149 error
= FT_Err_Invalid_Stream_Operation
;
156 FT_BASE_DEF( FT_ULong
)
157 FT_Stream_TryRead( FT_Stream stream
,
161 FT_ULong read_bytes
= 0;
164 if ( stream
->pos
>= stream
->size
)
168 read_bytes
= stream
->read( stream
, stream
->pos
, buffer
, count
);
171 read_bytes
= stream
->size
- stream
->pos
;
172 if ( read_bytes
> count
)
175 FT_MEM_COPY( buffer
, stream
->base
+ stream
->pos
, read_bytes
);
178 stream
->pos
+= read_bytes
;
185 FT_BASE_DEF( FT_Error
)
186 FT_Stream_ExtractFrame( FT_Stream stream
,
193 error
= FT_Stream_EnterFrame( stream
, count
);
196 *pbytes
= (FT_Byte
*)stream
->cursor
;
198 /* equivalent to FT_Stream_ExitFrame(), with no memory block release */
208 FT_Stream_ReleaseFrame( FT_Stream stream
,
213 FT_Memory memory
= stream
->memory
;
215 #ifdef FT_DEBUG_MEMORY
216 ft_mem_free( memory
, *pbytes
);
226 FT_BASE_DEF( FT_Error
)
227 FT_Stream_EnterFrame( FT_Stream stream
,
230 FT_Error error
= FT_Err_Ok
;
234 /* check for nested frame access */
235 FT_ASSERT( stream
&& stream
->cursor
== 0 );
239 /* allocate the frame in memory */
240 FT_Memory memory
= stream
->memory
;
242 #ifdef FT_DEBUG_MEMORY
243 /* assume _ft_debug_file and _ft_debug_lineno are already set */
244 stream
->base
= (unsigned char*)ft_mem_qalloc( memory
, count
, &error
);
248 if ( FT_QALLOC( stream
->base
, count
) )
252 read_bytes
= stream
->read( stream
, stream
->pos
,
253 stream
->base
, count
);
254 if ( read_bytes
< count
)
256 FT_ERROR(( "FT_Stream_EnterFrame:" ));
257 FT_ERROR(( " invalid read; expected %lu bytes, got %lu\n",
258 count
, read_bytes
));
260 FT_FREE( stream
->base
);
261 error
= FT_Err_Invalid_Stream_Operation
;
263 stream
->cursor
= stream
->base
;
264 stream
->limit
= stream
->cursor
+ count
;
265 stream
->pos
+= read_bytes
;
269 /* check current and new position */
270 if ( stream
->pos
>= stream
->size
||
271 stream
->pos
+ count
> stream
->size
)
273 FT_ERROR(( "FT_Stream_EnterFrame:" ));
274 FT_ERROR(( " invalid i/o; pos = 0x%lx, count = %lu, size = 0x%lx\n",
275 stream
->pos
, count
, stream
->size
));
277 error
= FT_Err_Invalid_Stream_Operation
;
282 stream
->cursor
= stream
->base
+ stream
->pos
;
283 stream
->limit
= stream
->cursor
+ count
;
284 stream
->pos
+= count
;
293 FT_Stream_ExitFrame( FT_Stream stream
)
295 /* IMPORTANT: The assertion stream->cursor != 0 was removed, given */
296 /* that it is possible to access a frame of length 0 in */
297 /* some weird fonts (usually, when accessing an array of */
298 /* 0 records, like in some strange kern tables). */
300 /* In this case, the loader code handles the 0-length table */
301 /* gracefully; however, stream.cursor is really set to 0 by the */
302 /* FT_Stream_EnterFrame() call, and this is not an error. */
308 FT_Memory memory
= stream
->memory
;
310 #ifdef FT_DEBUG_MEMORY
311 ft_mem_free( memory
, stream
->base
);
314 FT_FREE( stream
->base
);
322 FT_BASE_DEF( FT_Char
)
323 FT_Stream_GetChar( FT_Stream stream
)
328 FT_ASSERT( stream
&& stream
->cursor
);
331 if ( stream
->cursor
< stream
->limit
)
332 result
= *stream
->cursor
++;
338 FT_BASE_DEF( FT_Short
)
339 FT_Stream_GetShort( FT_Stream stream
)
345 FT_ASSERT( stream
&& stream
->cursor
);
349 if ( p
+ 1 < stream
->limit
)
350 result
= FT_NEXT_SHORT( p
);
357 FT_BASE_DEF( FT_Short
)
358 FT_Stream_GetShortLE( FT_Stream stream
)
364 FT_ASSERT( stream
&& stream
->cursor
);
368 if ( p
+ 1 < stream
->limit
)
369 result
= FT_NEXT_SHORT_LE( p
);
376 FT_BASE_DEF( FT_Long
)
377 FT_Stream_GetOffset( FT_Stream stream
)
383 FT_ASSERT( stream
&& stream
->cursor
);
387 if ( p
+ 2 < stream
->limit
)
388 result
= FT_NEXT_OFF3( p
);
394 FT_BASE_DEF( FT_Long
)
395 FT_Stream_GetLong( FT_Stream stream
)
401 FT_ASSERT( stream
&& stream
->cursor
);
405 if ( p
+ 3 < stream
->limit
)
406 result
= FT_NEXT_LONG( p
);
412 FT_BASE_DEF( FT_Long
)
413 FT_Stream_GetLongLE( FT_Stream stream
)
419 FT_ASSERT( stream
&& stream
->cursor
);
423 if ( p
+ 3 < stream
->limit
)
424 result
= FT_NEXT_LONG_LE( p
);
430 FT_BASE_DEF( FT_Char
)
431 FT_Stream_ReadChar( FT_Stream stream
,
443 if ( stream
->read( stream
, stream
->pos
, &result
, 1L ) != 1L )
448 if ( stream
->pos
< stream
->size
)
449 result
= stream
->base
[stream
->pos
];
458 *error
= FT_Err_Invalid_Stream_Operation
;
459 FT_ERROR(( "FT_Stream_ReadChar: invalid i/o; pos = 0x%lx, size = 0x%lx\n",
460 stream
->pos
, stream
->size
));
466 FT_BASE_DEF( FT_Short
)
467 FT_Stream_ReadShort( FT_Stream stream
,
479 if ( stream
->pos
+ 1 < stream
->size
)
483 if ( stream
->read( stream
, stream
->pos
, reads
, 2L ) != 2L )
490 p
= stream
->base
+ stream
->pos
;
494 result
= FT_NEXT_SHORT( p
);
504 *error
= FT_Err_Invalid_Stream_Operation
;
505 FT_ERROR(( "FT_Stream_ReadShort:" ));
506 FT_ERROR(( " invalid i/o; pos = 0x%lx, size = 0x%lx\n",
507 stream
->pos
, stream
->size
));
513 FT_BASE_DEF( FT_Short
)
514 FT_Stream_ReadShortLE( FT_Stream stream
,
526 if ( stream
->pos
+ 1 < stream
->size
)
530 if ( stream
->read( stream
, stream
->pos
, reads
, 2L ) != 2L )
537 p
= stream
->base
+ stream
->pos
;
541 result
= FT_NEXT_SHORT_LE( p
);
551 *error
= FT_Err_Invalid_Stream_Operation
;
552 FT_ERROR(( "FT_Stream_ReadShortLE:" ));
553 FT_ERROR(( " invalid i/o; pos = 0x%lx, size = 0x%lx\n",
554 stream
->pos
, stream
->size
));
560 FT_BASE_DEF( FT_Long
)
561 FT_Stream_ReadOffset( FT_Stream stream
,
573 if ( stream
->pos
+ 2 < stream
->size
)
577 if (stream
->read( stream
, stream
->pos
, reads
, 3L ) != 3L )
584 p
= stream
->base
+ stream
->pos
;
588 result
= FT_NEXT_OFF3( p
);
598 *error
= FT_Err_Invalid_Stream_Operation
;
599 FT_ERROR(( "FT_Stream_ReadOffset:" ));
600 FT_ERROR(( " invalid i/o; pos = 0x%lx, size = 0x%lx\n",
601 stream
->pos
, stream
->size
));
607 FT_BASE_DEF( FT_Long
)
608 FT_Stream_ReadLong( FT_Stream stream
,
620 if ( stream
->pos
+ 3 < stream
->size
)
624 if ( stream
->read( stream
, stream
->pos
, reads
, 4L ) != 4L )
631 p
= stream
->base
+ stream
->pos
;
635 result
= FT_NEXT_LONG( p
);
645 FT_ERROR(( "FT_Stream_ReadLong: invalid i/o; pos = 0x%lx, size = 0x%lx\n",
646 stream
->pos
, stream
->size
));
647 *error
= FT_Err_Invalid_Stream_Operation
;
653 FT_BASE_DEF( FT_Long
)
654 FT_Stream_ReadLongLE( FT_Stream stream
,
666 if ( stream
->pos
+ 3 < stream
->size
)
670 if ( stream
->read( stream
, stream
->pos
, reads
, 4L ) != 4L )
677 p
= stream
->base
+ stream
->pos
;
681 result
= FT_NEXT_LONG_LE( p
);
691 FT_ERROR(( "FT_Stream_ReadLongLE:" ));
692 FT_ERROR(( " invalid i/o; pos = 0x%lx, size = 0x%lx\n",
693 stream
->pos
, stream
->size
));
694 *error
= FT_Err_Invalid_Stream_Operation
;
700 FT_BASE_DEF( FT_Error
)
701 FT_Stream_ReadFields( FT_Stream stream
,
702 const FT_Frame_Field
* fields
,
706 FT_Bool frame_accessed
= 0;
707 FT_Byte
* cursor
= stream
->cursor
;
710 if ( !fields
|| !stream
)
711 return FT_Err_Invalid_Argument
;
721 switch ( fields
->value
)
723 case ft_frame_start
: /* access a new frame */
724 error
= FT_Stream_EnterFrame( stream
, fields
->offset
);
729 cursor
= stream
->cursor
;
731 continue; /* loop! */
733 case ft_frame_bytes
: /* read a byte sequence */
734 case ft_frame_skip
: /* skip some bytes */
736 FT_UInt len
= fields
->size
;
739 if ( cursor
+ len
> stream
->limit
)
741 error
= FT_Err_Invalid_Stream_Operation
;
745 if ( fields
->value
== ft_frame_bytes
)
747 p
= (FT_Byte
*)structure
+ fields
->offset
;
748 FT_MEM_COPY( p
, cursor
, len
);
756 case ft_frame_schar
: /* read a single byte */
757 value
= FT_NEXT_BYTE(cursor
);
761 case ft_frame_short_be
:
762 case ft_frame_ushort_be
: /* read a 2-byte big-endian short */
763 value
= FT_NEXT_USHORT(cursor
);
767 case ft_frame_short_le
:
768 case ft_frame_ushort_le
: /* read a 2-byte little-endian short */
769 value
= FT_NEXT_USHORT_LE(cursor
);
773 case ft_frame_long_be
:
774 case ft_frame_ulong_be
: /* read a 4-byte big-endian long */
775 value
= FT_NEXT_ULONG(cursor
);
779 case ft_frame_long_le
:
780 case ft_frame_ulong_le
: /* read a 4-byte little-endian long */
781 value
= FT_NEXT_ULONG_LE(cursor
);
785 case ft_frame_off3_be
:
786 case ft_frame_uoff3_be
: /* read a 3-byte big-endian long */
787 value
= FT_NEXT_UOFF3(cursor
);
791 case ft_frame_off3_le
:
792 case ft_frame_uoff3_le
: /* read a 3-byte little-endian long */
793 value
= FT_NEXT_UOFF3_LE(cursor
);
798 /* otherwise, exit the loop */
799 stream
->cursor
= cursor
;
803 /* now, compute the signed value is necessary */
804 if ( fields
->value
& FT_FRAME_OP_SIGNED
)
805 value
= (FT_ULong
)( (FT_Int32
)( value
<< sign_shift
) >> sign_shift
);
807 /* finally, store the value in the object */
809 p
= (FT_Byte
*)structure
+ fields
->offset
;
810 switch ( fields
->size
)
812 case (8 / FT_CHAR_BIT
):
813 *(FT_Byte
*)p
= (FT_Byte
)value
;
816 case (16 / FT_CHAR_BIT
):
817 *(FT_UShort
*)p
= (FT_UShort
)value
;
820 case (32 / FT_CHAR_BIT
):
821 *(FT_UInt32
*)p
= (FT_UInt32
)value
;
824 default: /* for 64-bit systems */
825 *(FT_ULong
*)p
= (FT_ULong
)value
;
828 /* go to next field */
834 /* close the frame if it was opened by this read */
835 if ( frame_accessed
)
836 FT_Stream_ExitFrame( stream
);