Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / modules / freetype2 / src / base / ftstream.c
blob569e46c79bf8e574a0cfb6522041e35fff8a7d39
1 /***************************************************************************/
2 /* */
3 /* ftstream.c */
4 /* */
5 /* I/O stream support (body). */
6 /* */
7 /* Copyright 2000-2001, 2002, 2004, 2005, 2006, 2008 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 /***************************************************************************/
19 #include <ft2build.h>
20 #include FT_INTERNAL_STREAM_H
21 #include FT_INTERNAL_DEBUG_H
24 /*************************************************************************/
25 /* */
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. */
29 /* */
30 #undef FT_COMPONENT
31 #define FT_COMPONENT trace_stream
34 FT_BASE_DEF( void )
35 FT_Stream_OpenMemory( FT_Stream stream,
36 const FT_Byte* base,
37 FT_ULong size )
39 stream->base = (FT_Byte*) base;
40 stream->size = size;
41 stream->pos = 0;
42 stream->cursor = 0;
43 stream->read = 0;
44 stream->close = 0;
48 FT_BASE_DEF( void )
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,
58 FT_ULong pos )
60 FT_Error error = FT_Err_Ok;
63 stream->pos = pos;
65 if ( stream->read )
67 if ( stream->read( stream, pos, 0, 0 ) )
69 FT_ERROR(( "FT_Stream_Seek: invalid i/o; pos = 0x%lx, size = 0x%lx\n",
70 pos, stream->size ));
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",
79 pos, stream->size ));
81 error = FT_Err_Invalid_Stream_Operation;
84 return error;
88 FT_BASE_DEF( FT_Error )
89 FT_Stream_Skip( FT_Stream stream,
90 FT_Long distance )
92 if ( distance < 0 )
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 )
102 return stream->pos;
106 FT_BASE_DEF( FT_Error )
107 FT_Stream_Read( FT_Stream stream,
108 FT_Byte* buffer,
109 FT_ULong count )
111 return FT_Stream_ReadAt( stream, stream->pos, buffer, count );
115 FT_BASE_DEF( FT_Error )
116 FT_Stream_ReadAt( FT_Stream stream,
117 FT_ULong pos,
118 FT_Byte* buffer,
119 FT_ULong count )
121 FT_Error error = FT_Err_Ok;
122 FT_ULong read_bytes;
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;
133 if ( stream->read )
134 read_bytes = stream->read( stream, pos, buffer, count );
135 else
137 read_bytes = stream->size - pos;
138 if ( read_bytes > count )
139 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;
155 return error;
159 FT_BASE_DEF( FT_ULong )
160 FT_Stream_TryRead( FT_Stream stream,
161 FT_Byte* buffer,
162 FT_ULong count )
164 FT_ULong read_bytes = 0;
167 if ( stream->pos >= stream->size )
168 goto Exit;
170 if ( stream->read )
171 read_bytes = stream->read( stream, stream->pos, buffer, count );
172 else
174 read_bytes = stream->size - stream->pos;
175 if ( read_bytes > count )
176 read_bytes = count;
178 FT_MEM_COPY( buffer, stream->base + stream->pos, read_bytes );
181 stream->pos += read_bytes;
183 Exit:
184 return read_bytes;
188 FT_BASE_DEF( FT_Error )
189 FT_Stream_ExtractFrame( FT_Stream stream,
190 FT_ULong count,
191 FT_Byte** pbytes )
193 FT_Error error;
196 error = FT_Stream_EnterFrame( stream, count );
197 if ( !error )
199 *pbytes = (FT_Byte*)stream->cursor;
201 /* equivalent to FT_Stream_ExitFrame(), with no memory block release */
202 stream->cursor = 0;
203 stream->limit = 0;
206 return error;
210 FT_BASE_DEF( void )
211 FT_Stream_ReleaseFrame( FT_Stream stream,
212 FT_Byte** pbytes )
214 if ( stream->read )
216 FT_Memory memory = stream->memory;
218 #ifdef FT_DEBUG_MEMORY
219 ft_mem_free( memory, *pbytes );
220 *pbytes = NULL;
221 #else
222 FT_FREE( *pbytes );
223 #endif
225 *pbytes = 0;
229 FT_BASE_DEF( FT_Error )
230 FT_Stream_EnterFrame( FT_Stream stream,
231 FT_ULong count )
233 FT_Error error = FT_Err_Ok;
234 FT_ULong read_bytes;
237 /* check for nested frame access */
238 FT_ASSERT( stream && stream->cursor == 0 );
240 if ( stream->read )
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 );
248 if ( error )
249 goto Exit;
250 #else
251 if ( FT_QALLOC( stream->base, count ) )
252 goto Exit;
253 #endif
254 /* read it */
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;
270 else
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;
281 goto Exit;
284 /* set cursor */
285 stream->cursor = stream->base + stream->pos;
286 stream->limit = stream->cursor + count;
287 stream->pos += count;
290 Exit:
291 return error;
295 FT_BASE_DEF( void )
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). */
302 /* */
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. */
306 /* */
307 FT_ASSERT( stream );
309 if ( stream->read )
311 FT_Memory memory = stream->memory;
313 #ifdef FT_DEBUG_MEMORY
314 ft_mem_free( memory, stream->base );
315 stream->base = NULL;
316 #else
317 FT_FREE( stream->base );
318 #endif
320 stream->cursor = 0;
321 stream->limit = 0;
325 FT_BASE_DEF( FT_Char )
326 FT_Stream_GetChar( FT_Stream stream )
328 FT_Char result;
331 FT_ASSERT( stream && stream->cursor );
333 result = 0;
334 if ( stream->cursor < stream->limit )
335 result = *stream->cursor++;
337 return result;
341 FT_BASE_DEF( FT_Short )
342 FT_Stream_GetShort( FT_Stream stream )
344 FT_Byte* p;
345 FT_Short result;
348 FT_ASSERT( stream && stream->cursor );
350 result = 0;
351 p = stream->cursor;
352 if ( p + 1 < stream->limit )
353 result = FT_NEXT_SHORT( p );
354 stream->cursor = p;
356 return result;
360 FT_BASE_DEF( FT_Short )
361 FT_Stream_GetShortLE( FT_Stream stream )
363 FT_Byte* p;
364 FT_Short result;
367 FT_ASSERT( stream && stream->cursor );
369 result = 0;
370 p = stream->cursor;
371 if ( p + 1 < stream->limit )
372 result = FT_NEXT_SHORT_LE( p );
373 stream->cursor = p;
375 return result;
379 FT_BASE_DEF( FT_Long )
380 FT_Stream_GetOffset( FT_Stream stream )
382 FT_Byte* p;
383 FT_Long result;
386 FT_ASSERT( stream && stream->cursor );
388 result = 0;
389 p = stream->cursor;
390 if ( p + 2 < stream->limit )
391 result = FT_NEXT_OFF3( p );
392 stream->cursor = p;
393 return result;
397 FT_BASE_DEF( FT_Long )
398 FT_Stream_GetLong( FT_Stream stream )
400 FT_Byte* p;
401 FT_Long result;
404 FT_ASSERT( stream && stream->cursor );
406 result = 0;
407 p = stream->cursor;
408 if ( p + 3 < stream->limit )
409 result = FT_NEXT_LONG( p );
410 stream->cursor = p;
411 return result;
415 FT_BASE_DEF( FT_Long )
416 FT_Stream_GetLongLE( FT_Stream stream )
418 FT_Byte* p;
419 FT_Long result;
422 FT_ASSERT( stream && stream->cursor );
424 result = 0;
425 p = stream->cursor;
426 if ( p + 3 < stream->limit )
427 result = FT_NEXT_LONG_LE( p );
428 stream->cursor = p;
429 return result;
433 FT_BASE_DEF( FT_Char )
434 FT_Stream_ReadChar( FT_Stream stream,
435 FT_Error* error )
437 FT_Byte result = 0;
440 FT_ASSERT( stream );
442 *error = FT_Err_Ok;
444 if ( stream->read )
446 if ( stream->read( stream, stream->pos, &result, 1L ) != 1L )
447 goto Fail;
449 else
451 if ( stream->pos < stream->size )
452 result = stream->base[stream->pos];
453 else
454 goto Fail;
456 stream->pos++;
458 return result;
460 Fail:
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 ));
465 return 0;
469 FT_BASE_DEF( FT_Short )
470 FT_Stream_ReadShort( FT_Stream stream,
471 FT_Error* error )
473 FT_Byte reads[2];
474 FT_Byte* p = 0;
475 FT_Short result = 0;
478 FT_ASSERT( stream );
480 *error = FT_Err_Ok;
482 if ( stream->pos + 1 < stream->size )
484 if ( stream->read )
486 if ( stream->read( stream, stream->pos, reads, 2L ) != 2L )
487 goto Fail;
489 p = reads;
491 else
493 p = stream->base + stream->pos;
496 if ( p )
497 result = FT_NEXT_SHORT( p );
499 else
500 goto Fail;
502 stream->pos += 2;
504 return result;
506 Fail:
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 ));
512 return 0;
516 FT_BASE_DEF( FT_Short )
517 FT_Stream_ReadShortLE( FT_Stream stream,
518 FT_Error* error )
520 FT_Byte reads[2];
521 FT_Byte* p = 0;
522 FT_Short result = 0;
525 FT_ASSERT( stream );
527 *error = FT_Err_Ok;
529 if ( stream->pos + 1 < stream->size )
531 if ( stream->read )
533 if ( stream->read( stream, stream->pos, reads, 2L ) != 2L )
534 goto Fail;
536 p = reads;
538 else
540 p = stream->base + stream->pos;
543 if ( p )
544 result = FT_NEXT_SHORT_LE( p );
546 else
547 goto Fail;
549 stream->pos += 2;
551 return result;
553 Fail:
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 ));
559 return 0;
563 FT_BASE_DEF( FT_Long )
564 FT_Stream_ReadOffset( FT_Stream stream,
565 FT_Error* error )
567 FT_Byte reads[3];
568 FT_Byte* p = 0;
569 FT_Long result = 0;
572 FT_ASSERT( stream );
574 *error = FT_Err_Ok;
576 if ( stream->pos + 2 < stream->size )
578 if ( stream->read )
580 if (stream->read( stream, stream->pos, reads, 3L ) != 3L )
581 goto Fail;
583 p = reads;
585 else
587 p = stream->base + stream->pos;
590 if ( p )
591 result = FT_NEXT_OFF3( p );
593 else
594 goto Fail;
596 stream->pos += 3;
598 return result;
600 Fail:
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 ));
606 return 0;
610 FT_BASE_DEF( FT_Long )
611 FT_Stream_ReadLong( FT_Stream stream,
612 FT_Error* error )
614 FT_Byte reads[4];
615 FT_Byte* p = 0;
616 FT_Long result = 0;
619 FT_ASSERT( stream );
621 *error = FT_Err_Ok;
623 if ( stream->pos + 3 < stream->size )
625 if ( stream->read )
627 if ( stream->read( stream, stream->pos, reads, 4L ) != 4L )
628 goto Fail;
630 p = reads;
632 else
634 p = stream->base + stream->pos;
637 if ( p )
638 result = FT_NEXT_LONG( p );
640 else
641 goto Fail;
643 stream->pos += 4;
645 return result;
647 Fail:
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;
652 return 0;
656 FT_BASE_DEF( FT_Long )
657 FT_Stream_ReadLongLE( FT_Stream stream,
658 FT_Error* error )
660 FT_Byte reads[4];
661 FT_Byte* p = 0;
662 FT_Long result = 0;
665 FT_ASSERT( stream );
667 *error = FT_Err_Ok;
669 if ( stream->pos + 3 < stream->size )
671 if ( stream->read )
673 if ( stream->read( stream, stream->pos, reads, 4L ) != 4L )
674 goto Fail;
676 p = reads;
678 else
680 p = stream->base + stream->pos;
683 if ( p )
684 result = FT_NEXT_LONG_LE( p );
686 else
687 goto Fail;
689 stream->pos += 4;
691 return result;
693 Fail:
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;
699 return 0;
703 FT_BASE_DEF( FT_Error )
704 FT_Stream_ReadFields( FT_Stream stream,
705 const FT_Frame_Field* fields,
706 void* structure )
708 FT_Error error;
709 FT_Bool frame_accessed = 0;
710 FT_Byte* cursor = stream->cursor;
713 if ( !fields || !stream )
714 return FT_Err_Invalid_Argument;
716 error = FT_Err_Ok;
719 FT_ULong value;
720 FT_Int sign_shift;
721 FT_Byte* p;
724 switch ( fields->value )
726 case ft_frame_start: /* access a new frame */
727 error = FT_Stream_EnterFrame( stream, fields->offset );
728 if ( error )
729 goto Exit;
731 frame_accessed = 1;
732 cursor = stream->cursor;
733 fields++;
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;
745 goto Exit;
748 if ( fields->value == ft_frame_bytes )
750 p = (FT_Byte*)structure + fields->offset;
751 FT_MEM_COPY( p, cursor, len );
753 cursor += len;
754 fields++;
755 continue;
758 case ft_frame_byte:
759 case ft_frame_schar: /* read a single byte */
760 value = FT_NEXT_BYTE(cursor);
761 sign_shift = 24;
762 break;
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);
767 sign_shift = 16;
768 break;
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);
773 sign_shift = 16;
774 break;
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);
779 sign_shift = 0;
780 break;
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);
785 sign_shift = 0;
786 break;
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);
791 sign_shift = 8;
792 break;
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);
797 sign_shift = 8;
798 break;
800 default:
801 /* otherwise, exit the loop */
802 stream->cursor = cursor;
803 goto Exit;
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;
817 break;
819 case (16 / FT_CHAR_BIT):
820 *(FT_UShort*)p = (FT_UShort)value;
821 break;
823 case (32 / FT_CHAR_BIT):
824 *(FT_UInt32*)p = (FT_UInt32)value;
825 break;
827 default: /* for 64-bit systems */
828 *(FT_ULong*)p = (FT_ULong)value;
831 /* go to next field */
832 fields++;
834 while ( 1 );
836 Exit:
837 /* close the frame if it was opened by this read */
838 if ( frame_accessed )
839 FT_Stream_ExitFrame( stream );
841 return error;
845 /* END */