psconv.c 12.1 KB
Newer Older
M
Ming, Bai 已提交
1 2 3 4 5 6
/***************************************************************************/
/*                                                                         */
/*  psconv.c                                                               */
/*                                                                         */
/*    Some convenience conversions (body).                                 */
/*                                                                         */
7
/*  Copyright 2006-2015 by                                                 */
M
Ming, Bai 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20
/*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
/*                                                                         */
/*  This file is part of the FreeType project, and may only be used,       */
/*  modified, and distributed under the terms of the FreeType project      */
/*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
/*  this file you indicate that you have read the license and              */
/*  understand and accept it fully.                                        */
/*                                                                         */
/***************************************************************************/


#include <ft2build.h>
#include FT_INTERNAL_POSTSCRIPT_AUX_H
G
Grissiom 已提交
21
#include FT_INTERNAL_DEBUG_H
M
Ming, Bai 已提交
22 23 24 25 26

#include "psconv.h"
#include "psauxerr.h"


G
Grissiom 已提交
27 28 29 30 31 32 33 34 35 36
  /*************************************************************************/
  /*                                                                       */
  /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
  /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
  /* messages during execution.                                            */
  /*                                                                       */
#undef  FT_COMPONENT
#define FT_COMPONENT  trace_psconv


M
Ming, Bai 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  /* The following array is used by various functions to quickly convert */
  /* digits (both decimal and non-decimal) into numbers.                 */

#if 'A' == 65
  /* ASCII */

  static const FT_Char  ft_char_table[128] =
  {
    /* 0x00 */
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     0,  1,  2,  3,  4,  5,  6,  7,  8,  9, -1, -1, -1, -1, -1, -1,
    -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
    25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1,
    -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
    25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1,
  };

  /* no character >= 0x80 can represent a valid number */
#define OP  >=

#endif /* 'A' == 65 */

#if 'A' == 193
  /* EBCDIC */

  static const FT_Char  ft_char_table[128] =
  {
    /* 0x80 */
    -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, -1, -1, -1, -1, -1, -1,
    -1, 19, 20, 21, 22, 23, 24, 25, 26, 27, -1, -1, -1, -1, -1, -1,
    -1, -1, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, -1, -1, -1, -1, -1, -1,
    -1, 19, 20, 21, 22, 23, 24, 25, 26, 27, -1, -1, -1, -1, -1, -1,
    -1, -1, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, -1,
     0,  1,  2,  3,  4,  5,  6,  7,  8,  9, -1, -1, -1, -1, -1, -1,
  };

  /* no character < 0x80 can represent a valid number */
#define OP  <

#endif /* 'A' == 193 */


G
Grissiom 已提交
83
  FT_LOCAL_DEF( FT_Long )
M
Ming, Bai 已提交
84 85
  PS_Conv_Strtol( FT_Byte**  cursor,
                  FT_Byte*   limit,
G
Grissiom 已提交
86
                  FT_Long    base )
M
Ming, Bai 已提交
87 88 89
  {
    FT_Byte*  p = *cursor;

G
Grissiom 已提交
90 91 92 93 94 95
    FT_Long   num           = 0;
    FT_Bool   sign          = 0;
    FT_Bool   have_overflow = 0;

    FT_Long   num_limit;
    FT_Char   c_limit;
M
Ming, Bai 已提交
96

G
Grissiom 已提交
97 98 99 100 101 102 103

    if ( p >= limit )
      goto Bad;

    if ( base < 2 || base > 36 )
    {
      FT_TRACE4(( "!!!INVALID BASE:!!!" ));
M
Ming, Bai 已提交
104
      return 0;
G
Grissiom 已提交
105
    }
M
Ming, Bai 已提交
106 107 108 109 110 111 112

    if ( *p == '-' || *p == '+' )
    {
      sign = FT_BOOL( *p == '-' );

      p++;
      if ( p == limit )
G
Grissiom 已提交
113
        goto Bad;
M
Ming, Bai 已提交
114 115
    }

G
Grissiom 已提交
116 117 118
    num_limit = 0x7FFFFFFFL / base;
    c_limit   = (FT_Char)( 0x7FFFFFFFL % base );

M
Ming, Bai 已提交
119 120 121 122 123 124 125 126
    for ( ; p < limit; p++ )
    {
      FT_Char  c;


      if ( IS_PS_SPACE( *p ) || *p OP 0x80 )
        break;

G
Grissiom 已提交
127
      c = ft_char_table[*p & 0x7F];
M
Ming, Bai 已提交
128 129 130 131

      if ( c < 0 || c >= base )
        break;

G
Grissiom 已提交
132 133 134 135 136 137 138 139 140 141 142 143
      if ( num > num_limit || ( num == num_limit && c > c_limit ) )
        have_overflow = 1;
      else
        num = num * base + c;
    }

    *cursor = p;

    if ( have_overflow )
    {
      num = 0x7FFFFFFFL;
      FT_TRACE4(( "!!!OVERFLOW:!!!" ));
M
Ming, Bai 已提交
144 145 146 147 148 149
    }

    if ( sign )
      num = -num;

    return num;
G
Grissiom 已提交
150 151 152 153

  Bad:
    FT_TRACE4(( "!!!END OF DATA:!!!" ));
    return 0;
M
Ming, Bai 已提交
154 155 156
  }


G
Grissiom 已提交
157
  FT_LOCAL_DEF( FT_Long )
M
Ming, Bai 已提交
158 159 160 161
  PS_Conv_ToInt( FT_Byte**  cursor,
                 FT_Byte*   limit )

  {
G
Grissiom 已提交
162 163 164 165
    FT_Byte*  p = *cursor;
    FT_Byte*  curp;

    FT_Long   num;
M
Ming, Bai 已提交
166 167


G
Grissiom 已提交
168 169 170 171 172
    curp = p;
    num  = PS_Conv_Strtol( &p, limit, 10 );

    if ( p == curp )
      return 0;
M
Ming, Bai 已提交
173 174 175

    if ( p < limit && *p == '#' )
    {
G
Grissiom 已提交
176
      p++;
M
Ming, Bai 已提交
177

G
Grissiom 已提交
178 179 180 181 182
      curp = p;
      num  = PS_Conv_Strtol( &p, limit, num );

      if ( p == curp )
        return 0;
M
Ming, Bai 已提交
183
    }
G
Grissiom 已提交
184 185 186 187

    *cursor = p;

    return num;
M
Ming, Bai 已提交
188 189 190 191 192 193
  }


  FT_LOCAL_DEF( FT_Fixed )
  PS_Conv_ToFixed( FT_Byte**  cursor,
                   FT_Byte*   limit,
G
Grissiom 已提交
194
                   FT_Long    power_ten )
M
Ming, Bai 已提交
195 196
  {
    FT_Byte*  p = *cursor;
G
Grissiom 已提交
197
    FT_Byte*  curp;
M
Ming, Bai 已提交
198

G
Grissiom 已提交
199 200 201
    FT_Fixed  integral = 0;
    FT_Long   decimal  = 0;
    FT_Long   divider  = 1;
M
Ming, Bai 已提交
202

G
Grissiom 已提交
203 204 205 206 207 208 209
    FT_Bool   sign           = 0;
    FT_Bool   have_overflow  = 0;
    FT_Bool   have_underflow = 0;


    if ( p >= limit )
      goto Bad;
M
Ming, Bai 已提交
210 211 212 213 214 215 216

    if ( *p == '-' || *p == '+' )
    {
      sign = FT_BOOL( *p == '-' );

      p++;
      if ( p == limit )
G
Grissiom 已提交
217
        goto Bad;
M
Ming, Bai 已提交
218 219
    }

G
Grissiom 已提交
220
    /* read the integer part */
M
Ming, Bai 已提交
221
    if ( *p != '.' )
G
Grissiom 已提交
222 223 224 225 226 227 228 229 230 231 232 233
    {
      curp     = p;
      integral = PS_Conv_ToInt( &p, limit );

      if ( p == curp )
        return 0;

      if ( integral > 0x7FFF )
        have_overflow = 1;
      else
        integral = (FT_Fixed)( (FT_UInt32)integral << 16 );
    }
M
Ming, Bai 已提交
234 235 236 237 238 239 240 241 242 243 244 245 246 247

    /* read the decimal part */
    if ( p < limit && *p == '.' )
    {
      p++;

      for ( ; p < limit; p++ )
      {
        FT_Char  c;


        if ( IS_PS_SPACE( *p ) || *p OP 0x80 )
          break;

G
Grissiom 已提交
248
        c = ft_char_table[*p & 0x7F];
M
Ming, Bai 已提交
249 250 251 252

        if ( c < 0 || c >= 10 )
          break;

G
Grissiom 已提交
253 254
        /* only add digit if we don't overflow */
        if ( divider < 0xCCCCCCCL && decimal < 0xCCCCCCCL )
M
Ming, Bai 已提交
255 256
        {
          decimal = decimal * 10 + c;
G
Grissiom 已提交
257 258 259 260

          if ( !integral && power_ten > 0 )
            power_ten--;
          else
M
Ming, Bai 已提交
261 262 263 264 265 266 267 268
            divider *= 10;
        }
      }
    }

    /* read exponent, if any */
    if ( p + 1 < limit && ( *p == 'e' || *p == 'E' ) )
    {
G
Grissiom 已提交
269 270 271
      FT_Long  exponent;


M
Ming, Bai 已提交
272
      p++;
G
Grissiom 已提交
273 274 275 276 277 278 279 280 281 282 283 284 285 286

      curp     = p;
      exponent = PS_Conv_ToInt( &p, limit );

      if ( curp == p )
        return 0;

      /* arbitrarily limit exponent */
      if ( exponent > 1000 )
        have_overflow = 1;
      else if ( exponent < -1000 )
        have_underflow = 1;
      else
        power_ten += exponent;
M
Ming, Bai 已提交
287 288
    }

G
Grissiom 已提交
289 290 291 292 293 294 295 296 297 298
    *cursor = p;

    if ( !integral && !decimal )
      return 0;

    if ( have_overflow )
      goto Overflow;
    if ( have_underflow )
      goto Underflow;

M
Ming, Bai 已提交
299 300
    while ( power_ten > 0 )
    {
G
Grissiom 已提交
301 302
      if ( integral >= 0xCCCCCCCL )
        goto Overflow;
M
Ming, Bai 已提交
303
      integral *= 10;
G
Grissiom 已提交
304 305 306 307 308 309 310 311 312 313

      if ( decimal >= 0xCCCCCCCL )
      {
        if ( divider == 1 )
          goto Overflow;
        divider /= 10;
      }
      else
        decimal *= 10;

M
Ming, Bai 已提交
314 315 316 317 318 319
      power_ten--;
    }

    while ( power_ten < 0 )
    {
      integral /= 10;
G
Grissiom 已提交
320 321 322 323 324 325 326 327
      if ( divider < 0xCCCCCCCL )
        divider *= 10;
      else
        decimal /= 10;

      if ( !integral && !decimal )
        goto Underflow;

M
Ming, Bai 已提交
328 329 330 331
      power_ten++;
    }

    if ( decimal )
G
Grissiom 已提交
332 333 334 335 336 337
    {
      decimal = FT_DivFix( decimal, divider );
      /* it's not necessary to check this addition for overflow */
      /* due to the structure of the real number representation */
      integral += decimal;
    }
M
Ming, Bai 已提交
338

G
Grissiom 已提交
339
  Exit:
M
Ming, Bai 已提交
340 341 342 343
    if ( sign )
      integral = -integral;

    return integral;
G
Grissiom 已提交
344 345 346 347 348 349 350 351 352 353 354 355 356

  Bad:
    FT_TRACE4(( "!!!END OF DATA:!!!" ));
    return 0;

  Overflow:
    integral = 0x7FFFFFFFL;
    FT_TRACE4(( "!!!OVERFLOW:!!!" ));
    goto Exit;

  Underflow:
    FT_TRACE4(( "!!!UNDERFLOW:!!!" ));
    return 0;
M
Ming, Bai 已提交
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
  }


#if 0
  FT_LOCAL_DEF( FT_UInt )
  PS_Conv_StringDecode( FT_Byte**  cursor,
                        FT_Byte*   limit,
                        FT_Byte*   buffer,
                        FT_Offset  n )
  {
    FT_Byte*  p;
    FT_UInt   r = 0;


    for ( p = *cursor; r < n && p < limit; p++ )
    {
      FT_Byte  b;


      if ( *p != '\\' )
      {
        buffer[r++] = *p;

        continue;
      }

      p++;

      switch ( *p )
      {
      case 'n':
        b = '\n';
        break;
      case 'r':
        b = '\r';
        break;
      case 't':
        b = '\t';
        break;
      case 'b':
        b = '\b';
        break;
      case 'f':
        b = '\f';
        break;
      case '\r':
        p++;
        if ( *p != '\n' )
        {
          b = *p;

          break;
        }
        /* no break */
      case '\n':
        continue;
        break;
      default:
        if ( IS_PS_DIGIT( *p ) )
        {
          b = *p - '0';

          p++;

          if ( IS_PS_DIGIT( *p ) )
          {
            b = b * 8 + *p - '0';

            p++;

            if ( IS_PS_DIGIT( *p ) )
              b = b * 8 + *p - '0';
            else
            {
              buffer[r++] = b;
              b = *p;
            }
          }
          else
          {
            buffer[r++] = b;
            b = *p;
          }
        }
        else
          b = *p;
        break;
      }

      buffer[r++] = b;
    }

    *cursor = p;

    return r;
  }
#endif /* 0 */


  FT_LOCAL_DEF( FT_UInt )
  PS_Conv_ASCIIHexDecode( FT_Byte**  cursor,
                          FT_Byte*   limit,
                          FT_Byte*   buffer,
                          FT_Offset  n )
  {
    FT_Byte*  p;
    FT_UInt   r   = 0;
    FT_UInt   w   = 0;
    FT_UInt   pad = 0x01;


    n *= 2;

#if 1

G
Grissiom 已提交
472 473 474 475 476
    p = *cursor;

    if ( p >= limit )
      return 0;

M
Ming, Bai 已提交
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
    if ( n > (FT_UInt)( limit - p ) )
      n = (FT_UInt)( limit - p );

    /* we try to process two nibbles at a time to be as fast as possible */
    for ( ; r < n; r++ )
    {
      FT_UInt  c = p[r];


      if ( IS_PS_SPACE( c ) )
        continue;

      if ( c OP 0x80 )
        break;

492 493
      c = (FT_UInt)ft_char_table[c & 0x7F];
      if ( c >= 16 )
M
Ming, Bai 已提交
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
        break;

      pad = ( pad << 4 ) | c;
      if ( pad & 0x100 )
      {
        buffer[w++] = (FT_Byte)pad;
        pad         = 0x01;
      }
    }

    if ( pad != 0x01 )
      buffer[w++] = (FT_Byte)( pad << 4 );

    *cursor = p + r;

    return w;

#else /* 0 */

    for ( r = 0; r < n; r++ )
    {
      FT_Char  c;


      if ( IS_PS_SPACE( *p ) )
        continue;

      if ( *p OP 0x80 )
        break;

G
Grissiom 已提交
524
      c = ft_char_table[*p & 0x7F];
M
Ming, Bai 已提交
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563

      if ( (unsigned)c >= 16 )
        break;

      if ( r & 1 )
      {
        *buffer = (FT_Byte)(*buffer + c);
        buffer++;
      }
      else
        *buffer = (FT_Byte)(c << 4);

      r++;
    }

    *cursor = p;

    return ( r + 1 ) / 2;

#endif /* 0 */

  }


  FT_LOCAL_DEF( FT_UInt )
  PS_Conv_EexecDecode( FT_Byte**   cursor,
                       FT_Byte*    limit,
                       FT_Byte*    buffer,
                       FT_Offset   n,
                       FT_UShort*  seed )
  {
    FT_Byte*  p;
    FT_UInt   r;
    FT_UInt   s = *seed;


#if 1

    p = *cursor;
G
Grissiom 已提交
564 565 566 567

    if ( p >= limit )
      return 0;

M
Ming, Bai 已提交
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
    if ( n > (FT_UInt)(limit - p) )
      n = (FT_UInt)(limit - p);

    for ( r = 0; r < n; r++ )
    {
      FT_UInt  val = p[r];
      FT_UInt  b   = ( val ^ ( s >> 8 ) );


      s         = ( (val + s)*52845U + 22719 ) & 0xFFFFU;
      buffer[r] = (FT_Byte) b;
    }

    *cursor = p + n;
    *seed   = (FT_UShort)s;

#else /* 0 */

    for ( r = 0, p = *cursor; r < n && p < limit; r++, p++ )
    {
      FT_Byte  b = (FT_Byte)( *p ^ ( s >> 8 ) );


      s = (FT_UShort)( ( *p + s ) * 52845U + 22719 );
      *buffer++ = b;
    }
    *cursor = p;
    *seed   = s;

#endif /* 0 */

    return r;
  }


/* END */