1 2 3 4 5 6 7 8 9 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 36 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
|
/* Copyright (C) 2001-2021 Artifex Software, Inc.
All Rights Reserved.
This software is provided AS-IS with no warranty, either express or
implied.
This software is distributed under license and may not be copied,
modified or distributed except as expressly authorized under the terms
of the license contained in the file LICENSE in this distribution.
Refer to licensing information at http://www.artifex.com or contact
Artifex Software, Inc., 1305 Grant Avenue - Suite 200, Novato,
CA 94945, U.S.A., +1(415)492-9861, for further information.
*/
/* Utilities for Ghostscript library */
#include "string_.h"
#include "memory_.h"
#include "gstypes.h"
#include "gserrors.h"
#include "gsmemory.h" /* for init procedure */
#include "gsrect.h" /* for prototypes */
#include "gsuid.h"
#include "gsutil.h" /* for prototypes */
#include "gxsync.h" /* for gx_monitor_t */
/* ------ Unique IDs ------ */
/* This is used by multiple threads, so lock around updates */
ulong
gs_next_ids(const gs_memory_t *mem, uint count)
{
gs_lib_ctx_core_t *core = mem->gs_lib_ctx->core;
ulong id;
gx_monitor_enter((gx_monitor_t *)(core->monitor));
id = core->gs_next_id;
core->gs_next_id += count;
gx_monitor_leave((gx_monitor_t *)(core->monitor));
return id;
}
/* ------ Memory utilities ------ */
/* Transpose an 8 x 8 block of bits. line_size is the raster of */
/* the input data. dist is the distance between output bytes. */
void
memflip8x8(const byte * inp, int line_size, byte * outp, int dist)
{
uint aceg, bdfh;
{
const byte *ptr4 = inp + (line_size << 2);
const int ls2 = line_size << 1;
aceg = ((uint)*inp) | ((uint)inp[ls2] << 8) |
((uint)*ptr4 << 16) | ((uint)ptr4[ls2] << 24);
inp += line_size, ptr4 += line_size;
bdfh = ((uint)*inp) | ((uint)inp[ls2] << 8) |
((uint)*ptr4 << 16) | ((uint)ptr4[ls2] << 24);
}
/* Check for all 8 bytes being the same. */
/* This is especially worth doing for the case where all are zero. */
if (aceg == bdfh && (aceg >> 8) == (aceg & 0xffffff)) {
if (aceg == 0 || aceg == 0xffffffff)
goto store;
*outp = (byte)-(int)((aceg >> 7) & 1);
outp[dist] = (byte)-(int)((aceg >> 6) & 1);
outp += dist << 1;
*outp = (byte)-(int)((aceg >> 5) & 1);
outp[dist] = (byte)-(int)((aceg >> 4) & 1);
outp += dist << 1;
*outp = (byte)-(int)((aceg >> 3) & 1);
outp[dist] = (byte)-(int)((aceg >> 2) & 1);
outp += dist << 1;
*outp = (byte)-(int)((aceg >> 1) & 1);
outp[dist] = (byte)-(int)(aceg & 1);
return;
} {
register uint temp;
/* Transpose a block of bits between registers. */
#define TRANSPOSE(r,s,mask,shift)\
(r ^= (temp = ((s >> shift) ^ r) & mask),\
s ^= temp << shift)
/* Transpose blocks of 4 x 4 */
TRANSPOSE(aceg, aceg, 0x00000f0f, 20);
TRANSPOSE(bdfh, bdfh, 0x00000f0f, 20);
/* Transpose blocks of 2 x 2 */
TRANSPOSE(aceg, aceg, 0x00330033, 10);
TRANSPOSE(bdfh, bdfh, 0x00330033, 10);
/* Transpose blocks of 1 x 1 */
TRANSPOSE(aceg, bdfh, 0x55555555, 1);
#undef TRANSPOSE
}
store:
*outp = (byte)aceg;
outp[dist] = (byte)bdfh;
outp += dist << 1;
*outp = (byte)(aceg >>= 8);
outp[dist] = (byte)(bdfh >>= 8);
outp += dist << 1;
*outp = (byte)(aceg >>= 8);
outp[dist] = (byte)(bdfh >>= 8);
outp += dist << 1;
*outp = (byte)(aceg >> 8);
outp[dist] = (byte)(bdfh >> 8);
}
#ifdef PACIFY_VALGRIND
void
memflip8x8_eol(const byte * inp, int line_size, byte * outp, int dist, int bits)
{
uint aceg, bdfh;
uint mask;
{
const byte *ptr4 = inp + (line_size << 2);
const int ls2 = line_size << 1;
aceg = ((uint)*inp) | ((uint)inp[ls2] << 8) |
((uint)*ptr4 << 16) | ((uint)ptr4[ls2] << 24);
inp += line_size, ptr4 += line_size;
bdfh = ((uint)*inp) | ((uint)inp[ls2] << 8) |
((uint)*ptr4 << 16) | ((uint)ptr4[ls2] << 24);
}
/* Keep just the defined bits */
mask = (0xff00>>(bits&7)) & 0xff;
mask |= mask<<8;
mask |= mask<<16;
aceg &= mask;
bdfh &= mask;
/* Check for all 8 bytes being the same. */
/* This is especially worth doing for the case where all are zero. */
if (aceg == bdfh && (aceg >> 8) == (aceg & 0xffffff)) {
if (aceg == 0 || aceg == 0xffffffff)
goto store;
*outp = (byte)-(int)((aceg >> 7) & 1);
outp[dist] = (byte)-(int)((aceg >> 6) & 1);
outp += dist << 1;
*outp = (byte)-(int)((aceg >> 5) & 1);
outp[dist] = (byte)-(int)((aceg >> 4) & 1);
outp += dist << 1;
*outp = (byte)-(int)((aceg >> 3) & 1);
outp[dist] = (byte)-(int)((aceg >> 2) & 1);
outp += dist << 1;
*outp = (byte)-(int)((aceg >> 1) & 1);
outp[dist] = (byte)-(int)(aceg & 1);
return;
} {
register uint temp;
/* Transpose a block of bits between registers. */
#define TRANSPOSE(r,s,mask,shift)\
(r ^= (temp = ((s >> shift) ^ r) & mask),\
s ^= temp << shift)
/* Transpose blocks of 4 x 4 */
TRANSPOSE(aceg, aceg, 0x00000f0f, 20);
TRANSPOSE(bdfh, bdfh, 0x00000f0f, 20);
/* Transpose blocks of 2 x 2 */
TRANSPOSE(aceg, aceg, 0x00330033, 10);
TRANSPOSE(bdfh, bdfh, 0x00330033, 10);
/* Transpose blocks of 1 x 1 */
TRANSPOSE(aceg, bdfh, 0x55555555, 1);
#undef TRANSPOSE
}
store:
*outp = (byte)aceg;
outp[dist] = (byte)bdfh;
outp += dist << 1;
*outp = (byte)(aceg >>= 8);
outp[dist] = (byte)(bdfh >>= 8);
outp += dist << 1;
*outp = (byte)(aceg >>= 8);
outp[dist] = (byte)(bdfh >>= 8);
outp += dist << 1;
*outp = (byte)(aceg >> 8);
outp[dist] = (byte)(bdfh >> 8);
}
#endif
/* Get an unsigned, big-endian 32-bit value. */
ulong
get_u32_msb(const byte *p)
{
return ((uint)p[0] << 24) + ((uint)p[1] << 16) + ((uint)p[2] << 8) + p[3];
}
/* Put an unsigned, big-endian 32-bit value. */
void
put_u32_msb(byte *p, const ulong n, const int offs)
{
(p + offs)[0] = n >> 24 & 255;
(p + offs)[1] = n >> 16 & 255;
(p + offs)[2] = n >> 8 & 255;
(p + offs)[3] = n & 255;
}
/* ------ String utilities ------ */
/* Compare two strings, returning -1 if the first is less, */
/* 0 if they are equal, and 1 if first is greater. */
/* We can't use memcmp, because we always use unsigned characters. */
int
bytes_compare(const byte * s1, uint len1, const byte * s2, uint len2)
{
register uint len = len1;
if (len2 < len)
len = len2;
{
register const byte *p1 = s1;
register const byte *p2 = s2;
while (len--)
if (*p1++ != *p2++)
return (p1[-1] < p2[-1] ? -1 : 1);
}
/* Now check for differing lengths */
return (len1 == len2 ? 0 : len1 < len2 ? -1 : 1);
}
/* Test whether a string matches a pattern with wildcards. */
/* '*' = any substring, '?' = any character, '\' quotes next character. */
const string_match_params string_match_params_default = {
'*', '?', '\\', false, false
};
bool
string_match(const byte * str, uint len, const byte * pstr, uint plen,
register const string_match_params * psmp)
{
const byte *pback = 0;
const byte *spback = 0; /* initialized only to pacify gcc */
const byte *p = pstr, *pend = pstr + plen;
const byte *sp = str, *spend = str + len;
if (psmp == 0)
psmp = &string_match_params_default;
again:while (p < pend) {
register byte ch = *p;
if (ch == psmp->any_substring) {
pback = ++p, spback = sp;
continue;
} else if (ch == psmp->any_char) {
if (sp == spend)
return false; /* str too short */
p++, sp++;
continue;
} else if (ch == psmp->quote_next) {
if (++p == pend)
return true; /* bad pattern */
ch = *p;
}
if (sp == spend)
return false; /* str too short */
if (*sp == ch ||
(psmp->ignore_case && (*sp ^ ch) == 0x20 &&
(ch &= ~0x20) >= 0x41 && ch <= 0x5a) ||
(psmp->slash_equiv && ((ch == '\\' && *sp == '/') ||
(ch == '/' && *sp == '\\')))
)
p++, sp++;
else if (pback == 0)
return false; /* no * to back up to */
else {
sp = ++spback;
p = pback;
}
}
if (sp < spend) { /* We got a match, but there are chars left over. */
/* If we can back up, back up to the only place that */
/* could produce a complete match, otherwise fail. */
if (pback == 0)
return false;
p = pback;
pback = 0;
sp = spend - (pend - p);
goto again;
}
return true;
}
/* ------ UID utilities ------ */
/* Compare two UIDs for equality. */
/* We know that at least one of them is valid. */
bool
uid_equal(register const gs_uid * puid1, register const gs_uid * puid2)
{
if (puid1->id != puid2->id)
return false;
if (puid1->id >= 0)
return true; /* UniqueID */
return
!memcmp((const char *)puid1->xvalues,
(const char *)puid2->xvalues,
(uint) - (puid1->id) * sizeof(long));
}
/* Copy the XUID data for a uid, if needed, updating the uid in place. */
int
uid_copy(gs_uid *puid, gs_memory_t *mem, client_name_t cname)
{
if (uid_is_XUID(puid)) {
uint xsize = uid_XUID_size(puid);
long *xvalues = (long *)
gs_alloc_byte_array(mem, xsize, sizeof(long), cname);
if (xvalues == 0)
return_error(gs_error_VMerror);
memcpy(xvalues, uid_XUID_values(puid), xsize * sizeof(long));
puid->xvalues = xvalues;
}
return 0;
}
/* ------ Rectangle utilities ------ */
/*
* Calculate the difference of two rectangles, a list of up to 4 rectangles.
* Return the number of rectangles in the list, and set the first rectangle
* to the intersection.
*/
int
int_rect_difference(gs_int_rect * outer, const gs_int_rect * inner,
gs_int_rect * diffs /*[4] */ )
{
int x0 = outer->p.x, y0 = outer->p.y;
int x1 = outer->q.x, y1 = outer->q.y;
int count = 0;
if (y0 < inner->p.y) {
diffs[0].p.x = x0, diffs[0].p.y = y0;
diffs[0].q.x = x1, diffs[0].q.y = min(y1, inner->p.y);
outer->p.y = y0 = diffs[0].q.y;
++count;
}
if (y1 > inner->q.y) {
diffs[count].p.x = x0, diffs[count].p.y = max(y0, inner->q.y);
diffs[count].q.x = x1, diffs[count].q.y = y1;
outer->q.y = y1 = diffs[count].p.y;
++count;
}
if (x0 < inner->p.x) {
diffs[0].p.x = x0, diffs[0].p.y = y0;
diffs[0].q.x = min(x1, inner->p.x), diffs[0].q.y = y1;
outer->p.x = x0 = diffs[count].q.x;
++count;
}
if (x1 > inner->q.x) {
diffs[count].p.x = max(x0, inner->q.x), diffs[count].p.y = y0;
diffs[count].q.x = x1, diffs[count].q.y = y1;
outer->q.x = x1 = diffs[count].p.x;
++count;
}
return count;
}
|