1727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/***************************************************************************/
2727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*                                                                         */
3727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  cf2read.c                                                              */
4727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*                                                                         */
5727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*    Adobe's code for stream handling (body).                             */
6727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*                                                                         */
7727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  Copyright 2007-2013 Adobe Systems Incorporated.                        */
8727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*                                                                         */
9727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  This software, and all works of authorship, whether in source or       */
10727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  object code form as indicated by the copyright notice(s) included      */
11727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  herein (collectively, the "Work") is made available, and may only be   */
12727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  used, modified, and distributed under the FreeType Project License,    */
13727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  LICENSE.TXT.  Additionally, subject to the terms and conditions of the */
14727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  FreeType Project License, each contributor to the Work hereby grants   */
15727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  to any individual or legal entity exercising permissions granted by    */
16727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  the FreeType Project License and this section (hereafter, "You" or     */
17727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  "Your") a perpetual, worldwide, non-exclusive, no-charge,              */
18727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  royalty-free, irrevocable (except as stated in this section) patent    */
19727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  license to make, have made, use, offer to sell, sell, import, and      */
20727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  otherwise transfer the Work, where such license applies only to those  */
21727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  patent claims licensable by such contributor that are necessarily      */
22727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  infringed by their contribution(s) alone or by combination of their    */
23727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  contribution(s) with the Work to which such contribution(s) was        */
24727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  submitted.  If You institute patent litigation against any entity      */
25727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  (including a cross-claim or counterclaim in a lawsuit) alleging that   */
26727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  the Work or a contribution incorporated within the Work constitutes    */
27727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  direct or contributory patent infringement, then any patent licenses   */
28727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  granted to You under this License for that Work shall terminate as of  */
29727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  the date such litigation is filed.                                     */
30727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*                                                                         */
31727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  By using, modifying, or distributing the Work you indicate that you    */
32727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  have read and understood the terms and conditions of the               */
33727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  FreeType Project License as well as those provided in this section,    */
34727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  and you accept them fully.                                             */
35727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*                                                                         */
36727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/***************************************************************************/
37727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
38727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
39727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease#include "cf2ft.h"
40727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease#include FT_INTERNAL_DEBUG_H
41727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
42727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease#include "cf2glue.h"
43727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
44727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease#include "cf2error.h"
45727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
46727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
47727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  /* Define CF2_IO_FAIL as 1 to enable random errors and random */
48727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  /* value errors in I/O.                                       */
49727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease#define CF2_IO_FAIL  0
50727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
51727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
52727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease#if CF2_IO_FAIL
53727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
54727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  /* set the .00 value to a nonzero probability */
55727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  static int
56727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  randomError2( void )
57727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  {
58727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    /* for region buffer ReadByte (interp) function */
59727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    return (double)rand() / RAND_MAX < .00;
60727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  }
61727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
62727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  /* set the .00 value to a nonzero probability */
63727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  static CF2_Int
64727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  randomValue()
65727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  {
66727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    return (double)rand() / RAND_MAX < .00 ? rand() : 0;
67727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  }
68727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
69727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease#endif /* CF2_IO_FAIL */
70727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
71727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
72727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  /* Region Buffer                                      */
73727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  /*                                                    */
74727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  /* Can be constructed from a copied buffer managed by */
75727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  /* `FCM_getDatablock'.                                */
76727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  /* Reads bytes with check for end of buffer.          */
77727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
78727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  /* reading past the end of the buffer sets error and returns zero */
79727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  FT_LOCAL_DEF( CF2_Int )
80727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  cf2_buf_readByte( CF2_Buffer  buf )
81727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  {
82727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    if ( buf->ptr < buf->end )
83727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    {
84727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease#if CF2_IO_FAIL
85727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease      if ( randomError2() )
86727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease      {
87727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease        CF2_SET_ERROR( buf->error, Invalid_Stream_Operation );
88727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease        return 0;
89727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease      }
90727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
91727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease      return *(buf->ptr)++ + randomValue();
92727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease#else
93727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease      return *(buf->ptr)++;
94727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease#endif
95727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    }
96727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    else
97727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    {
98727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease      CF2_SET_ERROR( buf->error, Invalid_Stream_Operation );
99727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease      return 0;
100727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    }
101727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  }
102727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
103727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
104727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  /* note: end condition can occur without error */
105727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  FT_LOCAL_DEF( FT_Bool )
106727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  cf2_buf_isEnd( CF2_Buffer  buf )
107727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  {
108ec0bab5697bb31ba980810145f62e3799946ec60Victoria Lease    return (FT_Bool)( buf->ptr >= buf->end );
109727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  }
110727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
111727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
112727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/* END */
113