1 /***************************************************************************/
5 /* I/O stream support (body). */
7 /* Copyright 2000-2001, 2002, 2004, 2005, 2006, 2008 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
,
93 return FT_Err_Invalid_Stream_Operation
;
95 return FT_Stream_Seek( stream
, (FT_ULong
)( stream
->pos
+ distance
) );
99 FT_BASE_DEF( FT_Long
)
100 FT_Stream_Pos( FT_Stream stream
)
106 FT_BASE_DEF( FT_Error
)
107 FT_Stream_Read( FT_Stream stream
,
111 return FT_Stream_ReadAt( stream
, stream
->pos
, buffer
, count
);
115 FT_BASE_DEF( FT_Error
)
116 FT_Stream_ReadAt( FT_Stream stream
,
121 FT_Error error
= FT_Err_Ok
;
125 if ( pos
>= stream
->size
)
127 FT_ERROR(( "FT_Stream_ReadAt: invalid i/o; pos = 0x%lx, size = 0x%lx\n",
128 pos
, stream
->size
));
130 return FT_Err_Invalid_Stream_Operation
;
134 read_bytes
= stream
->read( stream
, pos
, buffer
, count
);
137 read_bytes
= stream
->size
- pos
;
138 if ( read_bytes
> count
)
141 FT_MEM_COPY( buffer
, stream
->base
+ pos
, read_bytes
);
144 stream
->pos
= pos
+ read_bytes
;
146 if ( read_bytes
< count
)
148 FT_ERROR(( "FT_Stream_ReadAt:" ));
149 FT_ERROR(( " invalid read; expected %lu bytes, got %lu\n",
150 count
, read_bytes
));
152 error
= FT_Err_Invalid_Stream_Operation
;
159 FT_BASE_DEF( FT_ULong
)
160 FT_Stream_TryRead( FT_Stream stream
,
164 FT_ULong read_bytes
= 0;
167 if ( stream
->pos
>= stream
->size
)
171 read_bytes
= stream
->read( stream
, stream
->pos
, buffer
, count
);
174 read_bytes
= stream
->size
- stream
->pos
;
175 if ( read_bytes
> count
)
178 FT_MEM_COPY( buffer
, stream
->base
+ stream
->pos
, read_bytes
);
181 stream
->pos
+= read_bytes
;
188 FT_BASE_DEF( FT_Error
)
189 FT_Stream_ExtractFrame( FT_Stream stream
,
196 error
= FT_Stream_EnterFrame( stream
, count
);
199 *pbytes
= (FT_Byte
*)stream
->cursor
;
201 /* equivalent to FT_Stream_ExitFrame(), with no memory block release */
211 FT_Stream_ReleaseFrame( FT_Stream stream
,
216 FT_Memory memory
= stream
->memory
;
218 #ifdef FT_DEBUG_MEMORY
219 ft_mem_free( memory
, *pbytes
);
229 FT_BASE_DEF( FT_Error
)
230 FT_Stream_EnterFrame( FT_Stream stream
,
233 FT_Error error
= FT_Err_Ok
;
237 /* check for nested frame access */
238 FT_ASSERT( stream
&& stream
->cursor
== 0 );
242 /* allocate the frame in memory */
243 FT_Memory memory
= stream
->memory
;
245 #ifdef FT_DEBUG_MEMORY
246 /* assume _ft_debug_file and _ft_debug_lineno are already set */
247 stream
->base
= (unsigned char*)ft_mem_qalloc( memory
, count
, &error
);
251 if ( FT_QALLOC( stream
->base
, count
) )
255 read_bytes
= stream
->read( stream
, stream
->pos
,
256 stream
->base
, count
);
257 if ( read_bytes
< count
)
259 FT_ERROR(( "FT_Stream_EnterFrame:" ));
260 FT_ERROR(( " invalid read; expected %lu bytes, got %lu\n",
261 count
, read_bytes
));
263 FT_FREE( stream
->base
);
264 error
= FT_Err_Invalid_Stream_Operation
;
266 stream
->cursor
= stream
->base
;
267 stream
->limit
= stream
->cursor
+ count
;
268 stream
->pos
+= read_bytes
;
272 /* check current and new position */
273 if ( stream
->pos
>= stream
->size
||
274 stream
->pos
+ count
> stream
->size
)
276 FT_ERROR(( "FT_Stream_EnterFrame:" ));
277 FT_ERROR(( " invalid i/o; pos = 0x%lx, count = %lu, size = 0x%lx\n",
278 stream
->pos
, count
, stream
->size
));
280 error
= FT_Err_Invalid_Stream_Operation
;
285 stream
->cursor
= stream
->base
+ stream
->pos
;
286 stream
->limit
= stream
->cursor
+ count
;
287 stream
->pos
+= count
;
296 FT_Stream_ExitFrame( FT_Stream stream
)
298 /* IMPORTANT: The assertion stream->cursor != 0 was removed, given */
299 /* that it is possible to access a frame of length 0 in */
300 /* some weird fonts (usually, when accessing an array of */
301 /* 0 records, like in some strange kern tables). */
303 /* In this case, the loader code handles the 0-length table */
304 /* gracefully; however, stream.cursor is really set to 0 by the */
305 /* FT_Stream_EnterFrame() call, and this is not an error. */
311 FT_Memory memory
= stream
->memory
;
313 #ifdef FT_DEBUG_MEMORY
314 ft_mem_free( memory
, stream
->base
);
317 FT_FREE( stream
->base
);
325 FT_BASE_DEF( FT_Char
)
326 FT_Stream_GetChar( FT_Stream stream
)
331 FT_ASSERT( stream
&& stream
->cursor
);
334 if ( stream
->cursor
< stream
->limit
)
335 result
= *stream
->cursor
++;
341 FT_BASE_DEF( FT_Short
)
342 FT_Stream_GetShort( FT_Stream stream
)
348 FT_ASSERT( stream
&& stream
->cursor
);
352 if ( p
+ 1 < stream
->limit
)
353 result
= FT_NEXT_SHORT( p
);
360 FT_BASE_DEF( FT_Short
)
361 FT_Stream_GetShortLE( FT_Stream stream
)
367 FT_ASSERT( stream
&& stream
->cursor
);
371 if ( p
+ 1 < stream
->limit
)
372 result
= FT_NEXT_SHORT_LE( p
);
379 FT_BASE_DEF( FT_Long
)
380 FT_Stream_GetOffset( FT_Stream stream
)
386 FT_ASSERT( stream
&& stream
->cursor
);
390 if ( p
+ 2 < stream
->limit
)
391 result
= FT_NEXT_OFF3( p
);
397 FT_BASE_DEF( FT_Long
)
398 FT_Stream_GetLong( FT_Stream stream
)
404 FT_ASSERT( stream
&& stream
->cursor
);
408 if ( p
+ 3 < stream
->limit
)
409 result
= FT_NEXT_LONG( p
);
415 FT_BASE_DEF( FT_Long
)
416 FT_Stream_GetLongLE( FT_Stream stream
)
422 FT_ASSERT( stream
&& stream
->cursor
);
426 if ( p
+ 3 < stream
->limit
)
427 result
= FT_NEXT_LONG_LE( p
);
433 FT_BASE_DEF( FT_Char
)
434 FT_Stream_ReadChar( FT_Stream stream
,
446 if ( stream
->read( stream
, stream
->pos
, &result
, 1L ) != 1L )
451 if ( stream
->pos
< stream
->size
)
452 result
= stream
->base
[stream
->pos
];
461 *error
= FT_Err_Invalid_Stream_Operation
;
462 FT_ERROR(( "FT_Stream_ReadChar: invalid i/o; pos = 0x%lx, size = 0x%lx\n",
463 stream
->pos
, stream
->size
));
469 FT_BASE_DEF( FT_Short
)
470 FT_Stream_ReadShort( FT_Stream stream
,
482 if ( stream
->pos
+ 1 < stream
->size
)
486 if ( stream
->read( stream
, stream
->pos
, reads
, 2L ) != 2L )
493 p
= stream
->base
+ stream
->pos
;
497 result
= FT_NEXT_SHORT( p
);
507 *error
= FT_Err_Invalid_Stream_Operation
;
508 FT_ERROR(( "FT_Stream_ReadShort:" ));
509 FT_ERROR(( " invalid i/o; pos = 0x%lx, size = 0x%lx\n",
510 stream
->pos
, stream
->size
));
516 FT_BASE_DEF( FT_Short
)
517 FT_Stream_ReadShortLE( FT_Stream stream
,
529 if ( stream
->pos
+ 1 < stream
->size
)
533 if ( stream
->read( stream
, stream
->pos
, reads
, 2L ) != 2L )
540 p
= stream
->base
+ stream
->pos
;
544 result
= FT_NEXT_SHORT_LE( p
);
554 *error
= FT_Err_Invalid_Stream_Operation
;
555 FT_ERROR(( "FT_Stream_ReadShortLE:" ));
556 FT_ERROR(( " invalid i/o; pos = 0x%lx, size = 0x%lx\n",
557 stream
->pos
, stream
->size
));
563 FT_BASE_DEF( FT_Long
)
564 FT_Stream_ReadOffset( FT_Stream stream
,
576 if ( stream
->pos
+ 2 < stream
->size
)
580 if (stream
->read( stream
, stream
->pos
, reads
, 3L ) != 3L )
587 p
= stream
->base
+ stream
->pos
;
591 result
= FT_NEXT_OFF3( p
);
601 *error
= FT_Err_Invalid_Stream_Operation
;
602 FT_ERROR(( "FT_Stream_ReadOffset:" ));
603 FT_ERROR(( " invalid i/o; pos = 0x%lx, size = 0x%lx\n",
604 stream
->pos
, stream
->size
));
610 FT_BASE_DEF( FT_Long
)
611 FT_Stream_ReadLong( FT_Stream stream
,
623 if ( stream
->pos
+ 3 < stream
->size
)
627 if ( stream
->read( stream
, stream
->pos
, reads
, 4L ) != 4L )
634 p
= stream
->base
+ stream
->pos
;
638 result
= FT_NEXT_LONG( p
);
648 FT_ERROR(( "FT_Stream_ReadLong: invalid i/o; pos = 0x%lx, size = 0x%lx\n",
649 stream
->pos
, stream
->size
));
650 *error
= FT_Err_Invalid_Stream_Operation
;
656 FT_BASE_DEF( FT_Long
)
657 FT_Stream_ReadLongLE( FT_Stream stream
,
669 if ( stream
->pos
+ 3 < stream
->size
)
673 if ( stream
->read( stream
, stream
->pos
, reads
, 4L ) != 4L )
680 p
= stream
->base
+ stream
->pos
;
684 result
= FT_NEXT_LONG_LE( p
);
694 FT_ERROR(( "FT_Stream_ReadLongLE:" ));
695 FT_ERROR(( " invalid i/o; pos = 0x%lx, size = 0x%lx\n",
696 stream
->pos
, stream
->size
));
697 *error
= FT_Err_Invalid_Stream_Operation
;
703 FT_BASE_DEF( FT_Error
)
704 FT_Stream_ReadFields( FT_Stream stream
,
705 const FT_Frame_Field
* fields
,
709 FT_Bool frame_accessed
= 0;
710 FT_Byte
* cursor
= stream
->cursor
;
713 if ( !fields
|| !stream
)
714 return FT_Err_Invalid_Argument
;
724 switch ( fields
->value
)
726 case ft_frame_start
: /* access a new frame */
727 error
= FT_Stream_EnterFrame( stream
, fields
->offset
);
732 cursor
= stream
->cursor
;
734 continue; /* loop! */
736 case ft_frame_bytes
: /* read a byte sequence */
737 case ft_frame_skip
: /* skip some bytes */
739 FT_UInt len
= fields
->size
;
742 if ( cursor
+ len
> stream
->limit
)
744 error
= FT_Err_Invalid_Stream_Operation
;
748 if ( fields
->value
== ft_frame_bytes
)
750 p
= (FT_Byte
*)structure
+ fields
->offset
;
751 FT_MEM_COPY( p
, cursor
, len
);
759 case ft_frame_schar
: /* read a single byte */
760 value
= FT_NEXT_BYTE(cursor
);
764 case ft_frame_short_be
:
765 case ft_frame_ushort_be
: /* read a 2-byte big-endian short */
766 value
= FT_NEXT_USHORT(cursor
);
770 case ft_frame_short_le
:
771 case ft_frame_ushort_le
: /* read a 2-byte little-endian short */
772 value
= FT_NEXT_USHORT_LE(cursor
);
776 case ft_frame_long_be
:
777 case ft_frame_ulong_be
: /* read a 4-byte big-endian long */
778 value
= FT_NEXT_ULONG(cursor
);
782 case ft_frame_long_le
:
783 case ft_frame_ulong_le
: /* read a 4-byte little-endian long */
784 value
= FT_NEXT_ULONG_LE(cursor
);
788 case ft_frame_off3_be
:
789 case ft_frame_uoff3_be
: /* read a 3-byte big-endian long */
790 value
= FT_NEXT_UOFF3(cursor
);
794 case ft_frame_off3_le
:
795 case ft_frame_uoff3_le
: /* read a 3-byte little-endian long */
796 value
= FT_NEXT_UOFF3_LE(cursor
);
801 /* otherwise, exit the loop */
802 stream
->cursor
= cursor
;
806 /* now, compute the signed value is necessary */
807 if ( fields
->value
& FT_FRAME_OP_SIGNED
)
808 value
= (FT_ULong
)( (FT_Int32
)( value
<< sign_shift
) >> sign_shift
);
810 /* finally, store the value in the object */
812 p
= (FT_Byte
*)structure
+ fields
->offset
;
813 switch ( fields
->size
)
815 case (8 / FT_CHAR_BIT
):
816 *(FT_Byte
*)p
= (FT_Byte
)value
;
819 case (16 / FT_CHAR_BIT
):
820 *(FT_UShort
*)p
= (FT_UShort
)value
;
823 case (32 / FT_CHAR_BIT
):
824 *(FT_UInt32
*)p
= (FT_UInt32
)value
;
827 default: /* for 64-bit systems */
828 *(FT_ULong
*)p
= (FT_ULong
)value
;
831 /* go to next field */
837 /* close the frame if it was opened by this read */
838 if ( frame_accessed
)
839 FT_Stream_ExitFrame( stream
);