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

  1. /* Copyright (C) 1992, 1993 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. /* gsutil.c */
  20. /* Utilities for Ghostscript library */
  21. #include "std.h"
  22. #include "string_.h"
  23. #include "memory_.h"
  24. #include "gsuid.h"
  25. #include "gsutil.h"            /* for prototype checking */
  26.  
  27. /* ------ Unique IDs ------ */
  28.  
  29. /* Generate a block of unique IDs. */
  30. static ulong gs_next_id = 0;
  31. ulong
  32. gs_next_ids(uint count)
  33. {    ulong id;
  34.     if ( gs_next_id == 0 ) gs_next_id++;
  35.     id = gs_next_id;
  36.     gs_next_id += count;
  37.     return id;
  38. }
  39.  
  40. /* ------ Memory utilities ------ */
  41.  
  42. /* Transpose an 8 x 8 block of bits.  line_size is the raster of */
  43. /* the input data.  dist is the distance between output bytes. */
  44. /* This routine may be supplanted by assembly code. */
  45. #if !USE_ASM
  46.  
  47. void
  48. memflip8x8(const byte *inp, int line_size, byte *outp, int dist)
  49. {    register uint ae, bf, cg, dh;
  50.        {    const byte *ptr4 = inp + (line_size << 2);
  51.         ae = ((uint)*inp << 8) + *ptr4;
  52.         inp += line_size, ptr4 += line_size;
  53.         bf = ((uint)*inp << 8) + *ptr4;
  54.         inp += line_size, ptr4 += line_size;
  55.         cg = ((uint)*inp << 8) + *ptr4;
  56.         inp += line_size, ptr4 += line_size;
  57.         dh = ((uint)*inp << 8) + *ptr4;
  58.        }
  59.  
  60.     /* Check for all 8 bytes being the same. */
  61.     /* This is especially worth doing for the case where all are zero. */
  62.     if ( ae == bf && ae == cg && ae == dh && (ae >> 8) == (ae & 0xff) )
  63.        {    if ( ae == 0 ) goto store;
  64.         *outp = -((ae >> 7) & 1);
  65.         outp += dist;
  66.         *outp = -((ae >> 6) & 1);
  67.         outp += dist;
  68.         *outp = -((ae >> 5) & 1);
  69.         outp += dist;
  70.         *outp = -((ae >> 4) & 1);
  71.         outp += dist;
  72.         *outp = -((ae >> 3) & 1);
  73.         outp += dist;
  74.         *outp = -((ae >> 2) & 1);
  75.         outp += dist;
  76.         *outp = -((ae >> 1) & 1);
  77.         outp += dist;
  78.         *outp = -(ae & 1);
  79.         return;
  80.        }
  81.  
  82.        {    register uint temp;
  83.  
  84. /* Transpose a block of bits between registers. */
  85. #define transpose(r,s,mask,shift)\
  86.   r ^= (temp = ((s >> shift) ^ r) & mask);\
  87.   s ^= temp << shift
  88.  
  89. /* Transpose blocks of 4 x 4 */
  90. #define transpose4(r) transpose(r,r,0x00f0,4)
  91.     transpose4(ae);
  92.     transpose4(bf);
  93.     transpose4(cg);
  94.     transpose4(dh);
  95.  
  96. /* Transpose blocks of 2 x 2 */
  97.     transpose(ae, cg, 0x3333, 2);
  98.     transpose(bf, dh, 0x3333, 2);
  99.  
  100. /* Transpose blocks of 1 x 1 */
  101.     transpose(ae, bf, 0x5555, 1);
  102.     transpose(cg, dh, 0x5555, 1);
  103.  
  104.        }
  105.  
  106. store:    *outp = ae >> 8;
  107.     outp += dist;
  108.     *outp = bf >> 8;
  109.     outp += dist;
  110.     *outp = cg >> 8;
  111.     outp += dist;
  112.     *outp = dh >> 8;
  113.     outp += dist;
  114.     *outp = (byte)ae;
  115.     outp += dist;
  116.     *outp = (byte)bf;
  117.     outp += dist;
  118.     *outp = (byte)cg;
  119.     outp += dist;
  120.     *outp = (byte)dh;
  121. }
  122.  
  123. #endif                    /* !USE_ASM */
  124.  
  125. /* ------ String utilities ------ */
  126.  
  127. /* Compare two strings, returning -1 if the first is less, */
  128. /* 0 if they are equal, and 1 if first is greater. */
  129. /* We can't use memcmp, because we always use unsigned characters. */
  130. int
  131. bytes_compare(const byte *s1, uint len1, const byte *s2, uint len2)
  132. {    register uint len = len1;
  133.     if ( len2 < len ) len = len2;
  134.        {    register const byte *p1 = s1;
  135.         register const byte *p2 = s2;
  136.         while ( len-- )
  137.             if ( *p1++ != *p2++ )
  138.                 return (p1[-1] < p2[-1] ? -1 : 1);
  139.        }
  140.     /* Now check for differing lengths */
  141.     return (len1 == len2 ? 0 : len1 < len2 ? -1 : 1);
  142. }
  143.  
  144. /* Test whether a string matches a pattern with wildcards. */
  145. /* '*' = any substring, '?' = any character, '\' quotes next character. */
  146. private const string_match_params smp_default = { '*', '?', '\\', false };
  147. bool
  148. string_match(const byte *str, uint len, const byte *pstr, uint plen,
  149.   register const string_match_params *psmp)
  150. {    const byte *pback = 0;
  151.     const byte *spback;
  152.     const byte *p = pstr, *pend = pstr + plen;
  153.     const byte *sp = str, *spend = str + len;
  154.     if ( psmp == 0 )
  155.         psmp = &smp_default;
  156. again:    while ( p < pend )
  157.     {    register byte ch = *p;
  158.         if ( ch == psmp->any_substring )
  159.         {    pback = ++p, spback = sp;
  160.             continue;
  161.         }
  162.         else if ( ch == psmp->any_char )
  163.         {    if ( sp == spend )
  164.               return false;    /* str too short */
  165.             p++, sp++;
  166.             continue;
  167.         }
  168.         else if ( ch == psmp->quote_next )
  169.         {    if ( ++p == pend )
  170.               return true;    /* bad pattern */
  171.             ch = *p;
  172.         }
  173.         if ( sp == spend )
  174.             return false;    /* str too short */
  175.         if ( *sp == ch ||
  176.              (psmp->ignore_case && (*sp ^ ch) == 0x20 &&
  177.               (ch &= ~0x20) >= 0x41 && ch <= 0x5a)
  178.            )
  179.             p++, sp++;
  180.         else if ( pback == 0 )
  181.             return false;    /* no * to back up to */
  182.         else
  183.            {    sp = ++spback;
  184.             p = pback;
  185.            }
  186.     }
  187.     if ( sp < spend )
  188.     {    /* We got a match, but there are chars left over. */
  189.         /* If we can back up, back up to the only place that */
  190.         /* could produce a complete match, otherwise fail. */
  191.         if ( pback == 0 )
  192.             return false;
  193.         p = pback;
  194.         pback = 0;
  195.         sp = spend - (pend - p);
  196.         goto again;
  197.     }
  198.     return true;
  199. }
  200.  
  201. /* ------ UID utilities ------ */
  202.  
  203. /* Compare two UIDs for equality. */
  204. /* We know that at least one of them is valid. */
  205. bool
  206. uid_equal(register const gs_uid *puid1, register const gs_uid *puid2)
  207. {    if ( puid1->id != puid2->id )
  208.         return false;
  209.     if ( puid1->id >= 0 )
  210.         return true;        /* UniqueID */
  211.     return
  212.         !memcmp((const char *)puid1->xvalues,
  213.             (const char *)puid2->xvalues,
  214.             (uint)-(puid1->id) * sizeof(long));
  215. }
  216.