1 /***************************************************************************/
5 /* I/O stream support (body). */
7 /* Copyright 2000-2001, 2002, 2004, 2005, 2006, 2008, 2009 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
;
65 if ( stream
->read( stream
, pos
, 0, 0 ) )
67 FT_ERROR(( "FT_Stream_Seek:"
68 " invalid i/o; pos = 0x%lx, size = 0x%lx\n",
71 error
= FT_Err_Invalid_Stream_Operation
;
74 /* note that seeking to the first position after the file is valid */
75 else if ( pos
> stream
->size
)
77 FT_ERROR(( "FT_Stream_Seek:"
78 " invalid i/o; pos = 0x%lx, size = 0x%lx\n",
81 error
= FT_Err_Invalid_Stream_Operation
;
91 FT_BASE_DEF( FT_Error
)
92 FT_Stream_Skip( FT_Stream stream
,
96 return FT_Err_Invalid_Stream_Operation
;
98 return FT_Stream_Seek( stream
, (FT_ULong
)( stream
->pos
+ distance
) );
102 FT_BASE_DEF( FT_Long
)
103 FT_Stream_Pos( FT_Stream stream
)
109 FT_BASE_DEF( FT_Error
)
110 FT_Stream_Read( FT_Stream stream
,
114 return FT_Stream_ReadAt( stream
, stream
->pos
, buffer
, count
);
118 FT_BASE_DEF( FT_Error
)
119 FT_Stream_ReadAt( FT_Stream stream
,
124 FT_Error error
= FT_Err_Ok
;
128 if ( pos
>= stream
->size
)
130 FT_ERROR(( "FT_Stream_ReadAt:"
131 " invalid i/o; pos = 0x%lx, size = 0x%lx\n",
132 pos
, stream
->size
));
134 return FT_Err_Invalid_Stream_Operation
;
138 read_bytes
= stream
->read( stream
, pos
, buffer
, count
);
141 read_bytes
= stream
->size
- pos
;
142 if ( read_bytes
> count
)
145 FT_MEM_COPY( buffer
, stream
->base
+ pos
, read_bytes
);
148 stream
->pos
= pos
+ read_bytes
;
150 if ( read_bytes
< count
)
152 FT_ERROR(( "FT_Stream_ReadAt:"
153 " invalid read; expected %lu bytes, got %lu\n",
154 count
, read_bytes
));
156 error
= FT_Err_Invalid_Stream_Operation
;
163 FT_BASE_DEF( FT_ULong
)
164 FT_Stream_TryRead( FT_Stream stream
,
168 FT_ULong read_bytes
= 0;
171 if ( stream
->pos
>= stream
->size
)
175 read_bytes
= stream
->read( stream
, stream
->pos
, buffer
, count
);
178 read_bytes
= stream
->size
- stream
->pos
;
179 if ( read_bytes
> count
)
182 FT_MEM_COPY( buffer
, stream
->base
+ stream
->pos
, read_bytes
);
185 stream
->pos
+= read_bytes
;
192 FT_BASE_DEF( FT_Error
)
193 FT_Stream_ExtractFrame( FT_Stream stream
,
200 error
= FT_Stream_EnterFrame( stream
, count
);
203 *pbytes
= (FT_Byte
*)stream
->cursor
;
205 /* equivalent to FT_Stream_ExitFrame(), with no memory block release */
215 FT_Stream_ReleaseFrame( FT_Stream stream
,
218 if ( stream
&& stream
->read
)
220 FT_Memory memory
= stream
->memory
;
222 #ifdef FT_DEBUG_MEMORY
223 ft_mem_free( memory
, *pbytes
);
233 FT_BASE_DEF( FT_Error
)
234 FT_Stream_EnterFrame( FT_Stream stream
,
237 FT_Error error
= FT_Err_Ok
;
241 /* check for nested frame access */
242 FT_ASSERT( stream
&& stream
->cursor
== 0 );
246 /* allocate the frame in memory */
247 FT_Memory memory
= stream
->memory
;
249 #ifdef FT_DEBUG_MEMORY
250 /* assume _ft_debug_file and _ft_debug_lineno are already set */
251 stream
->base
= (unsigned char*)ft_mem_qalloc( memory
, count
, &error
);
255 if ( FT_QALLOC( stream
->base
, count
) )
259 read_bytes
= stream
->read( stream
, stream
->pos
,
260 stream
->base
, count
);
261 if ( read_bytes
< count
)
263 FT_ERROR(( "FT_Stream_EnterFrame:"
264 " invalid read; expected %lu bytes, got %lu\n",
265 count
, read_bytes
));
267 FT_FREE( stream
->base
);
268 error
= FT_Err_Invalid_Stream_Operation
;
270 stream
->cursor
= stream
->base
;
271 stream
->limit
= stream
->cursor
+ count
;
272 stream
->pos
+= read_bytes
;
276 /* check current and new position */
277 if ( stream
->pos
>= stream
->size
||
278 stream
->pos
+ count
> stream
->size
)
280 FT_ERROR(( "FT_Stream_EnterFrame:"
281 " invalid i/o; pos = 0x%lx, count = %lu, size = 0x%lx\n",
282 stream
->pos
, count
, stream
->size
));
284 error
= FT_Err_Invalid_Stream_Operation
;
289 stream
->cursor
= stream
->base
+ stream
->pos
;
290 stream
->limit
= stream
->cursor
+ count
;
291 stream
->pos
+= count
;
300 FT_Stream_ExitFrame( FT_Stream stream
)
302 /* IMPORTANT: The assertion stream->cursor != 0 was removed, given */
303 /* that it is possible to access a frame of length 0 in */
304 /* some weird fonts (usually, when accessing an array of */
305 /* 0 records, like in some strange kern tables). */
307 /* In this case, the loader code handles the 0-length table */
308 /* gracefully; however, stream.cursor is really set to 0 by the */
309 /* FT_Stream_EnterFrame() call, and this is not an error. */
315 FT_Memory memory
= stream
->memory
;
317 #ifdef FT_DEBUG_MEMORY
318 ft_mem_free( memory
, stream
->base
);
321 FT_FREE( stream
->base
);
329 FT_BASE_DEF( FT_Char
)
330 FT_Stream_GetChar( FT_Stream stream
)
335 FT_ASSERT( stream
&& stream
->cursor
);
338 if ( stream
->cursor
< stream
->limit
)
339 result
= *stream
->cursor
++;
345 FT_BASE_DEF( FT_Short
)
346 FT_Stream_GetShort( FT_Stream stream
)
352 FT_ASSERT( stream
&& stream
->cursor
);
356 if ( p
+ 1 < stream
->limit
)
357 result
= FT_NEXT_SHORT( p
);
364 FT_BASE_DEF( FT_Short
)
365 FT_Stream_GetShortLE( FT_Stream stream
)
371 FT_ASSERT( stream
&& stream
->cursor
);
375 if ( p
+ 1 < stream
->limit
)
376 result
= FT_NEXT_SHORT_LE( p
);
383 FT_BASE_DEF( FT_Long
)
384 FT_Stream_GetOffset( FT_Stream stream
)
390 FT_ASSERT( stream
&& stream
->cursor
);
394 if ( p
+ 2 < stream
->limit
)
395 result
= FT_NEXT_OFF3( p
);
401 FT_BASE_DEF( FT_Long
)
402 FT_Stream_GetLong( FT_Stream stream
)
408 FT_ASSERT( stream
&& stream
->cursor
);
412 if ( p
+ 3 < stream
->limit
)
413 result
= FT_NEXT_LONG( p
);
419 FT_BASE_DEF( FT_Long
)
420 FT_Stream_GetLongLE( FT_Stream stream
)
426 FT_ASSERT( stream
&& stream
->cursor
);
430 if ( p
+ 3 < stream
->limit
)
431 result
= FT_NEXT_LONG_LE( p
);
437 FT_BASE_DEF( FT_Char
)
438 FT_Stream_ReadChar( FT_Stream stream
,
450 if ( stream
->read( stream
, stream
->pos
, &result
, 1L ) != 1L )
455 if ( stream
->pos
< stream
->size
)
456 result
= stream
->base
[stream
->pos
];
465 *error
= FT_Err_Invalid_Stream_Operation
;
466 FT_ERROR(( "FT_Stream_ReadChar:"
467 " invalid i/o; pos = 0x%lx, size = 0x%lx\n",
468 stream
->pos
, stream
->size
));
474 FT_BASE_DEF( FT_Short
)
475 FT_Stream_ReadShort( FT_Stream stream
,
487 if ( stream
->pos
+ 1 < stream
->size
)
491 if ( stream
->read( stream
, stream
->pos
, reads
, 2L ) != 2L )
498 p
= stream
->base
+ stream
->pos
;
502 result
= FT_NEXT_SHORT( p
);
512 *error
= FT_Err_Invalid_Stream_Operation
;
513 FT_ERROR(( "FT_Stream_ReadShort:"
514 " invalid i/o; pos = 0x%lx, size = 0x%lx\n",
515 stream
->pos
, stream
->size
));
521 FT_BASE_DEF( FT_Short
)
522 FT_Stream_ReadShortLE( FT_Stream stream
,
534 if ( stream
->pos
+ 1 < stream
->size
)
538 if ( stream
->read( stream
, stream
->pos
, reads
, 2L ) != 2L )
545 p
= stream
->base
+ stream
->pos
;
549 result
= FT_NEXT_SHORT_LE( p
);
559 *error
= FT_Err_Invalid_Stream_Operation
;
560 FT_ERROR(( "FT_Stream_ReadShortLE:"
561 " invalid i/o; pos = 0x%lx, size = 0x%lx\n",
562 stream
->pos
, stream
->size
));
568 FT_BASE_DEF( FT_Long
)
569 FT_Stream_ReadOffset( FT_Stream stream
,
581 if ( stream
->pos
+ 2 < stream
->size
)
585 if (stream
->read( stream
, stream
->pos
, reads
, 3L ) != 3L )
592 p
= stream
->base
+ stream
->pos
;
596 result
= FT_NEXT_OFF3( p
);
606 *error
= FT_Err_Invalid_Stream_Operation
;
607 FT_ERROR(( "FT_Stream_ReadOffset:"
608 " invalid i/o; pos = 0x%lx, size = 0x%lx\n",
609 stream
->pos
, stream
->size
));
615 FT_BASE_DEF( FT_Long
)
616 FT_Stream_ReadLong( FT_Stream stream
,
628 if ( stream
->pos
+ 3 < stream
->size
)
632 if ( stream
->read( stream
, stream
->pos
, reads
, 4L ) != 4L )
639 p
= stream
->base
+ stream
->pos
;
643 result
= FT_NEXT_LONG( p
);
653 *error
= FT_Err_Invalid_Stream_Operation
;
654 FT_ERROR(( "FT_Stream_ReadLong:"
655 " invalid i/o; pos = 0x%lx, size = 0x%lx\n",
656 stream
->pos
, stream
->size
));
662 FT_BASE_DEF( FT_Long
)
663 FT_Stream_ReadLongLE( FT_Stream stream
,
675 if ( stream
->pos
+ 3 < stream
->size
)
679 if ( stream
->read( stream
, stream
->pos
, reads
, 4L ) != 4L )
686 p
= stream
->base
+ stream
->pos
;
690 result
= FT_NEXT_LONG_LE( p
);
700 *error
= FT_Err_Invalid_Stream_Operation
;
701 FT_ERROR(( "FT_Stream_ReadLongLE:"
702 " invalid i/o; pos = 0x%lx, size = 0x%lx\n",
703 stream
->pos
, stream
->size
));
709 FT_BASE_DEF( FT_Error
)
710 FT_Stream_ReadFields( FT_Stream stream
,
711 const FT_Frame_Field
* fields
,
715 FT_Bool frame_accessed
= 0;
718 if ( !fields
|| !stream
)
719 return FT_Err_Invalid_Argument
;
721 cursor
= stream
->cursor
;
731 switch ( fields
->value
)
733 case ft_frame_start
: /* access a new frame */
734 error
= FT_Stream_EnterFrame( stream
, fields
->offset
);
739 cursor
= stream
->cursor
;
741 continue; /* loop! */
743 case ft_frame_bytes
: /* read a byte sequence */
744 case ft_frame_skip
: /* skip some bytes */
746 FT_UInt len
= fields
->size
;
749 if ( cursor
+ len
> stream
->limit
)
751 error
= FT_Err_Invalid_Stream_Operation
;
755 if ( fields
->value
== ft_frame_bytes
)
757 p
= (FT_Byte
*)structure
+ fields
->offset
;
758 FT_MEM_COPY( p
, cursor
, len
);
766 case ft_frame_schar
: /* read a single byte */
767 value
= FT_NEXT_BYTE(cursor
);
771 case ft_frame_short_be
:
772 case ft_frame_ushort_be
: /* read a 2-byte big-endian short */
773 value
= FT_NEXT_USHORT(cursor
);
777 case ft_frame_short_le
:
778 case ft_frame_ushort_le
: /* read a 2-byte little-endian short */
779 value
= FT_NEXT_USHORT_LE(cursor
);
783 case ft_frame_long_be
:
784 case ft_frame_ulong_be
: /* read a 4-byte big-endian long */
785 value
= FT_NEXT_ULONG(cursor
);
789 case ft_frame_long_le
:
790 case ft_frame_ulong_le
: /* read a 4-byte little-endian long */
791 value
= FT_NEXT_ULONG_LE(cursor
);
795 case ft_frame_off3_be
:
796 case ft_frame_uoff3_be
: /* read a 3-byte big-endian long */
797 value
= FT_NEXT_UOFF3(cursor
);
801 case ft_frame_off3_le
:
802 case ft_frame_uoff3_le
: /* read a 3-byte little-endian long */
803 value
= FT_NEXT_UOFF3_LE(cursor
);
808 /* otherwise, exit the loop */
809 stream
->cursor
= cursor
;
813 /* now, compute the signed value is necessary */
814 if ( fields
->value
& FT_FRAME_OP_SIGNED
)
815 value
= (FT_ULong
)( (FT_Int32
)( value
<< sign_shift
) >> sign_shift
);
817 /* finally, store the value in the object */
819 p
= (FT_Byte
*)structure
+ fields
->offset
;
820 switch ( fields
->size
)
822 case (8 / FT_CHAR_BIT
):
823 *(FT_Byte
*)p
= (FT_Byte
)value
;
826 case (16 / FT_CHAR_BIT
):
827 *(FT_UShort
*)p
= (FT_UShort
)value
;
830 case (32 / FT_CHAR_BIT
):
831 *(FT_UInt32
*)p
= (FT_UInt32
)value
;
834 default: /* for 64-bit systems */
835 *(FT_ULong
*)p
= (FT_ULong
)value
;
838 /* go to next field */
844 /* close the frame if it was opened by this read */
845 if ( frame_accessed
)
846 FT_Stream_ExitFrame( stream
);