home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / SSTRING.C < prev    next >
C/C++ Source or Header  |  1994-07-27  |  9KB  |  361 lines

  1. /* Copyright (C) 1993, 1994 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* sstring.c */
  20. /* String and hexstring streams for Ghostscript */
  21. #include "stdio_.h"        /* includes std.h */
  22. #include "memory_.h"
  23. #include "strimpl.h"
  24. #include "sfilter.h"
  25. #include "scanchar.h"
  26.  
  27. /* ------ ASCIIHexEncode ------ */
  28.  
  29. /* Process a buffer */
  30. private int
  31. s_AXE_process(stream_state *st, stream_cursor_read *pr,
  32.   stream_cursor_write *pw, bool last)
  33. {    register const byte *p = pr->ptr;
  34.     register byte *q = pw->ptr;
  35.     int rcount = pr->limit - p;
  36.     int wcount = pw->limit - q;
  37.     register int count;
  38.     register const char _ds *hex_digits = "0123456789abcdef";
  39.     int status = 0;
  40.     if ( last )
  41.         wcount--;        /* leave room for '>' */
  42.     wcount -= (wcount + 64) / 65;    /* leave room for \n */
  43.     wcount >>= 1;            /* 2 chars per input byte */
  44.     count = (wcount < rcount ? (status = 1, wcount) : rcount);
  45.     while ( --count >= 0 )
  46.     {    *++q = hex_digits[*++p >> 4];
  47.         *++q = hex_digits[*p & 0xf];
  48.         if ( !(count & 31) )
  49.             *++q = '\n';
  50.     }
  51.     if ( last && status == 0 )
  52.     {    *++q = '>';
  53.     }
  54.     pr->ptr = p;
  55.     pw->ptr = q;
  56.     return status;
  57. }
  58.  
  59. /* Stream template */
  60. const stream_template s_AXE_template =
  61. {    &st_stream_state, NULL, s_AXE_process, 1, 3
  62. };
  63.  
  64. /* ------ ASCIIHexDecode ------ */
  65.  
  66. private_st_AXD_state();
  67.  
  68. #define ss ((stream_AXD_state *)st)
  69.  
  70. /* Initialize the state */
  71. private int
  72. s_AXD_init(stream_state *st)
  73. {    return s_AXD_init_inline(ss);
  74. }
  75.  
  76. /* Process a buffer */
  77. private int
  78. s_AXD_process(stream_state *st, stream_cursor_read *pr,
  79.   stream_cursor_write *pw, bool last)
  80. {    int code = s_hex_process(pr, pw, &ss->odd, false);
  81.     switch ( code )
  82.     {
  83.     case 0:
  84.         if ( ss->odd >= 0 && last )
  85.         {    if ( pw->ptr == pw->limit )
  86.               return 1;
  87.             *++(pw->ptr) = ss->odd << 4;
  88.         }
  89.         /* falls through */
  90.     default:
  91.         return code;
  92.     case ERRC:
  93.         ;
  94.     }
  95.     /* Check for EOD.  ERRC implies at least one more character */
  96.     /* was read. */
  97.     if ( *pr->ptr != '>' )        /* EOD */
  98.       return ERRC;
  99.     if ( ss->odd >= 0 )
  100.     { if ( pw->ptr == pw->limit )
  101.         return 1;
  102.       *++(pw->ptr) = ss->odd << 4;
  103.     }
  104.     return EOFC;
  105. }
  106.  
  107. #undef ss
  108.  
  109. /* Stream template */
  110. const stream_template s_AXD_template =
  111. {    &st_AXD_state, s_AXD_init, s_AXD_process, 2, 1
  112. };
  113.  
  114. /* ------ PSStringEncode ------ */
  115.  
  116. /* Process a buffer */
  117. private int
  118. s_PSSE_process(stream_state *st, stream_cursor_read *pr,
  119.   stream_cursor_write *pw, bool last)
  120. {    register const byte *p = pr->ptr;
  121.     const byte *rlimit = pr->limit;
  122.     register byte *q = pw->ptr;
  123.     byte *wlimit = pw->limit;
  124.     int status = 0;
  125.     /* This doesn't have to be very efficient. */
  126.     while ( p < rlimit )
  127.       {    register int c;
  128.         if ( q == wlimit )
  129.           {    status = 1;
  130.             break;
  131.           }
  132.         c = *++p;
  133.         if ( c < 32 || c >= 127 || c == '(' || c == ')' || c == '\\' )
  134.           {    if ( q + 1 == wlimit )
  135.               {    status = 1;
  136.                 break;
  137.               }
  138.             *++q = '\\';
  139.           }
  140.         *++q = c;
  141.       }
  142.     if ( last && status == 0 )
  143.       {    if ( q == wlimit )
  144.           status = 1;
  145.         else
  146.           *++q = ')';
  147.       }
  148.     pr->ptr = p;
  149.     pw->ptr = q;
  150.     return status;
  151. }
  152.  
  153. /* Stream template */
  154. const stream_template s_PSSE_template =
  155. {    &st_stream_state, NULL, s_PSSE_process, 1, 4
  156. };
  157.  
  158. /* ------ PSStringDecode ------ */
  159.  
  160. private_st_PSSD_state();
  161.  
  162. #define ss ((stream_PSSD_state *)st)
  163.  
  164. /* Initialize the state */
  165. private int
  166. s_PSSD_init(stream_state *st)
  167. {    return s_PSSD_init_inline(ss);
  168. }
  169.  
  170. /* Process a buffer */
  171. private int
  172. s_PSSD_process(stream_state *st, stream_cursor_read *pr,
  173.   stream_cursor_write *pw, bool last)
  174. {    register const byte *p = pr->ptr;
  175.     const byte *rlimit = pr->limit;
  176.     register byte *q = pw->ptr;
  177.     byte *wlimit = pw->limit;
  178.     int status = 0;
  179.     register int c;
  180. #define check_p(n)\
  181.   if ( p == rlimit ) { p -= n; goto out; }
  182. #define check_q(n)\
  183.   if ( q == wlimit ) { p -= n; status = 1; goto out; }
  184.     while ( p < rlimit )
  185.       {    c = *++p;
  186.         if ( c == '\\' && !ss->from_string )
  187.           {    check_p(1);
  188.             switch ( (c = *++p) )
  189.             {
  190.             case 'n':
  191.                 c = '\n';
  192.                 goto put;
  193.             case 'r':
  194.                 c = '\r';
  195.                 goto put;
  196.             case 't':
  197.                 c = '\t';
  198.                 goto put;
  199.             case 'b':
  200.                 c = '\b';
  201.                 goto put;
  202.             case 'f':
  203.                 c = '\f';
  204.                 goto put;
  205.             default:        /* ignore the \ */
  206. put:                check_q(2);
  207.                 *++q = c;
  208.                 continue;
  209.             case char_CR:    /* ignore, check for following \n */
  210.                 check_p(2);
  211.                 if ( p[1] == char_EOL )
  212.                     p++;
  213.                 continue;
  214.             case char_EOL:        /* ignore */
  215.                 continue;
  216.             case '0': case '1': case '2': case '3':
  217.             case '4': case '5': case '6': case '7':
  218.               {    int d;
  219.                 check_p(2);
  220.                 d = p[1];
  221.                 c -= '0';
  222.                 if ( d >= '0' && d <= '7' )
  223.                 {    if ( p + 1 == rlimit )
  224.                       {    p -= 2;
  225.                         goto out;
  226.                       }
  227.                     check_q(2);
  228.                     c = (c << 3) + d - '0';
  229.                     d = p[2];
  230.                     if ( d >= '0' && d <= '7' )
  231.                     {    c = (c << 3) + d - '0';
  232.                         p += 2;
  233.                     }
  234.                     else
  235.                         p++;
  236.                 }
  237.                 else
  238.                     check_q(2);
  239.                 *++q = c;
  240.                 continue;
  241.               }
  242.             }
  243.           }
  244.         else
  245.           switch ( c )
  246.         {
  247.         case '(':
  248.             check_q(1);
  249.             ss->depth++;
  250.             break;
  251.         case ')':
  252.             if ( ss->depth == 0 )
  253.               {    status = EOFC;
  254.                 goto out;
  255.               }
  256.             check_q(1);
  257.             ss->depth--;
  258.             break;
  259.         case char_CR:        /* convert to \n */
  260.             check_p(1);
  261.             check_q(1);
  262.             if ( p[1] == char_EOL )
  263.                 p++;
  264.             *++q = '\n';
  265.             continue;
  266.         case char_EOL:
  267.             c = '\n';
  268.         default:
  269.             check_q(1);
  270.             break;
  271.         }
  272.         *++q = c;
  273.       }
  274. #undef check_p
  275. #undef check_q
  276. out:    pr->ptr = p;
  277.     pw->ptr = q;
  278.     if ( last && status == 0 && p != rlimit )
  279.       status = ERRC;
  280.     return status;
  281. }
  282.  
  283. #undef ss
  284.  
  285. /* Stream template */
  286. const stream_template s_PSSD_template =
  287. {    &st_PSSD_state, s_PSSD_init, s_PSSD_process, 4, 1
  288. };
  289.  
  290. /* ------ Utilities ------ */
  291.  
  292. /*
  293.  * Convert hex data to binary.  Return 1 if we filled the string, 0 if
  294.  * we ran out of input data before filling the string, or ERRC on error.
  295.  * The caller must set *odd_digit to -1 before the first call;
  296.  * after each call, if an odd number of hex digits has been read (total),
  297.  * *odd_digit is the odd digit value, otherwise *odd_digit = -1.
  298.  * If ignore_garbage is true, ignore characters other than hex digits;
  299.  * if ignore_garbage is false, return ERRC on characters other than hex
  300.  * digits or whitespace.
  301.  */
  302. int
  303. s_hex_process(stream_cursor_read *pr, stream_cursor_write *pw,
  304.   int *odd_digit, bool ignore_garbage)
  305. {    const byte *p = pr->ptr;
  306.     const byte *rlimit = pr->limit;
  307.     byte *q = pw->ptr;
  308.     byte *wlimit = pw->limit;
  309.     byte val1 = (byte)*odd_digit;
  310.     byte val2;
  311.     uint rcount;
  312.     byte *flimit;
  313.     register const byte _ds *decoder = scan_char_decoder;
  314.     int code = 0;
  315.     if ( q >= wlimit )
  316.       return 1;
  317.     if ( val1 <= 0xf )
  318.       goto d2;
  319. d1:    if ( (rcount = (rlimit - p) >> 1) == 0 )
  320.       goto x1;
  321.     /* Set up a fast end-of-loop check, so we don't have to test */
  322.     /* both p and q against their respective limits. */
  323.     flimit = (rcount < wlimit - q ? q + rcount : wlimit);
  324. f1:    if ( (val1 = decoder[p[1]]) <= 0xf &&
  325.          (val2 = decoder[p[2]]) <= 0xf
  326.        )
  327.     {    p += 2;
  328.         *++q = (val1 << 4) + val2;
  329.         if ( q < flimit )
  330.           goto f1;
  331.         if ( q >= wlimit )
  332.           goto px;
  333.     }
  334. x1:    if ( p >= rlimit )
  335.       goto end1;
  336.     if ( (val1 = decoder[*++p]) > 0xf )
  337.     {    if ( val1 == ctype_space || ignore_garbage )
  338.           goto x1;
  339.         code = ERRC;
  340.         goto end1;
  341.     }
  342. d2:    if ( p >= rlimit )
  343.     {    *odd_digit = val1;
  344.         goto ended;
  345.     }
  346.     if ( (val2 = decoder[*++p]) > 0xf )
  347.     {    if ( val2 == ctype_space || ignore_garbage )
  348.             goto d2;
  349.         *odd_digit = val1;
  350.         code = ERRC;
  351.         goto ended;
  352.     }
  353.     *++q = (val1 << 4) + val2;
  354.     if ( q < wlimit ) goto d1;
  355. px:    code = 1;
  356. end1:    *odd_digit = -1;
  357. ended:    pr->ptr = p;
  358.     pw->ptr = q;
  359.     return code;
  360. }
  361.