| 1 |
/* |
| 2 |
* Copyright (C) 1989-95 GROUPE BULL |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 |
* of this software and associated documentation files (the "Software"), to |
| 6 |
* deal in the Software without restriction, including without limitation the |
| 7 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 8 |
* sell copies of the Software, and to permit persons to whom the Software is |
| 9 |
* furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice shall be included in |
| 12 |
* all copies or substantial portions of the Software. |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 |
* GROUPE BULL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 18 |
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 19 |
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 20 |
* |
| 21 |
* Except as contained in this notice, the name of GROUPE BULL shall not be |
| 22 |
* used in advertising or otherwise to promote the sale, use or other dealings |
| 23 |
* in this Software without prior written authorization from GROUPE BULL. |
| 24 |
*/ |
| 25 |
|
| 26 |
/*****************************************************************************\ |
| 27 |
* parse.c: * |
| 28 |
* * |
| 29 |
* XPM library * |
| 30 |
* Parse an XPM file or array and store the found information * |
| 31 |
* in the given XpmImage structure. * |
| 32 |
* * |
| 33 |
* Developed by Arnaud Le Hors * |
| 34 |
\*****************************************************************************/ |
| 35 |
|
| 36 |
/* October 2004, source code review by Thomas Biege <[email protected]> */ |
| 37 |
|
| 38 |
#ifdef HAVE_CONFIG_H |
| 39 |
#include <config.h> |
| 40 |
#endif |
| 41 |
#include "XpmI.h" |
| 42 |
#include <ctype.h> |
| 43 |
#include <string.h> |
| 44 |
|
| 45 |
/** |
| 46 |
* like strlcat() but returns true on success and false if the string got |
| 47 |
* truncated. |
| 48 |
*/ |
| 49 |
static inline Bool |
| 50 |
xstrlcat(char *dst, const char *src, size_t dstsize) |
| 51 |
{ |
| 52 |
#if defined(HAS_STRLCAT) || defined(HAVE_STRLCAT) |
| 53 |
return strlcat(dst, src, dstsize) < dstsize; |
| 54 |
#else |
| 55 |
if ((strlen(dst) + strlen(src)) < dstsize) { |
| 56 |
strcat(dst, src); |
| 57 |
return True; |
| 58 |
} else { |
| 59 |
return False; |
| 60 |
} |
| 61 |
#endif |
| 62 |
} |
| 63 |
|
| 64 |
LFUNC(ParsePixels, int, (xpmData *data, unsigned int width, |
| 65 |
unsigned int height, unsigned int ncolors, |
| 66 |
unsigned int cpp, XpmColor *colorTable, |
| 67 |
xpmHashTable *hashtable, unsigned int **pixels)); |
| 68 |
|
| 69 |
const char *xpmColorKeys[] = { |
| 70 |
"s", /* key #1: symbol */ |
| 71 |
"m", /* key #2: mono visual */ |
| 72 |
"g4", /* key #3: 4 grays visual */ |
| 73 |
"g", /* key #4: gray visual */ |
| 74 |
"c", /* key #5: color visual */ |
| 75 |
}; |
| 76 |
|
| 77 |
int |
| 78 |
xpmParseValues( |
| 79 |
xpmData *data, |
| 80 |
unsigned int *width, |
| 81 |
unsigned int *height, |
| 82 |
unsigned int *ncolors, |
| 83 |
unsigned int *cpp, |
| 84 |
unsigned int *x_hotspot, |
| 85 |
unsigned int *y_hotspot, |
| 86 |
unsigned int *hotspot, |
| 87 |
unsigned int *extensions) |
| 88 |
{ |
| 89 |
unsigned int l; |
| 90 |
char buf[BUFSIZ + 1]; |
| 91 |
|
| 92 |
if (!data->format) { /* XPM 2 or 3 */ |
| 93 |
|
| 94 |
/* |
| 95 |
* read values: width, height, ncolors, chars_per_pixel |
| 96 |
*/ |
| 97 |
if (!(xpmNextUI(data, width) && xpmNextUI(data, height) |
| 98 |
&& xpmNextUI(data, ncolors) && xpmNextUI(data, cpp))) |
| 99 |
return (XpmFileInvalid); |
| 100 |
|
| 101 |
/* |
| 102 |
* read optional information (hotspot and/or XPMEXT) if any |
| 103 |
*/ |
| 104 |
l = xpmNextWord(data, buf, BUFSIZ); |
| 105 |
if (l) { |
| 106 |
*extensions = (l == 6 && !strncmp("XPMEXT", buf, 6)); |
| 107 |
if (*extensions) |
| 108 |
*hotspot = (xpmNextUI(data, x_hotspot) |
| 109 |
&& xpmNextUI(data, y_hotspot)); |
| 110 |
else { |
| 111 |
*hotspot = (xpmatoui(buf, l, x_hotspot) |
| 112 |
&& xpmNextUI(data, y_hotspot)); |
| 113 |
l = xpmNextWord(data, buf, BUFSIZ); |
| 114 |
*extensions = (l == 6 && !strncmp("XPMEXT", buf, 6)); |
| 115 |
} |
| 116 |
} |
| 117 |
} else { |
| 118 |
|
| 119 |
/* |
| 120 |
* XPM 1 file read values: width, height, ncolors, chars_per_pixel |
| 121 |
*/ |
| 122 |
int i; |
| 123 |
char *ptr; |
| 124 |
Bool got_one, saw_width = False, saw_height = False; |
| 125 |
Bool saw_ncolors = False, saw_chars_per_pixel = False; |
| 126 |
|
| 127 |
for (i = 0; i < 4; i++) { |
| 128 |
l = xpmNextWord(data, buf, BUFSIZ); |
| 129 |
if (l != 7 || strncmp("#define", buf, 7)) |
| 130 |
return (XpmFileInvalid); |
| 131 |
l = xpmNextWord(data, buf, BUFSIZ); |
| 132 |
if (!l) |
| 133 |
return (XpmFileInvalid); |
| 134 |
buf[l] = '\0'; |
| 135 |
ptr = buf; |
| 136 |
got_one = False; |
| 137 |
while (!got_one) { |
| 138 |
ptr = strchr(ptr, '_'); |
| 139 |
if (!ptr) |
| 140 |
return (XpmFileInvalid); |
| 141 |
switch (l - (ptr - buf)) { |
| 142 |
case 6: |
| 143 |
if (saw_width || strncmp("_width", ptr, 6) |
| 144 |
|| !xpmNextUI(data, width)) |
| 145 |
return (XpmFileInvalid); |
| 146 |
else |
| 147 |
saw_width = True; |
| 148 |
got_one = True; |
| 149 |
break; |
| 150 |
case 7: |
| 151 |
if (saw_height || strncmp("_height", ptr, 7) |
| 152 |
|| !xpmNextUI(data, height)) |
| 153 |
return (XpmFileInvalid); |
| 154 |
else |
| 155 |
saw_height = True; |
| 156 |
got_one = True; |
| 157 |
break; |
| 158 |
case 8: |
| 159 |
if (saw_ncolors || strncmp("_ncolors", ptr, 8) |
| 160 |
|| !xpmNextUI(data, ncolors)) |
| 161 |
return (XpmFileInvalid); |
| 162 |
else |
| 163 |
saw_ncolors = True; |
| 164 |
got_one = True; |
| 165 |
break; |
| 166 |
case 16: |
| 167 |
if (saw_chars_per_pixel |
| 168 |
|| strncmp("_chars_per_pixel", ptr, 16) |
| 169 |
|| !xpmNextUI(data, cpp)) |
| 170 |
return (XpmFileInvalid); |
| 171 |
else |
| 172 |
saw_chars_per_pixel = True; |
| 173 |
got_one = True; |
| 174 |
break; |
| 175 |
default: |
| 176 |
ptr++; |
| 177 |
} |
| 178 |
} |
| 179 |
/* skip the end of line */ |
| 180 |
xpmNextString(data); |
| 181 |
} |
| 182 |
if (!saw_width || !saw_height || !saw_ncolors || !saw_chars_per_pixel) |
| 183 |
return (XpmFileInvalid); |
| 184 |
|
| 185 |
*hotspot = 0; |
| 186 |
*extensions = 0; |
| 187 |
} |
| 188 |
return (XpmSuccess); |
| 189 |
} |
| 190 |
|
| 191 |
int |
| 192 |
xpmParseColors( |
| 193 |
xpmData *data, |
| 194 |
unsigned int ncolors, |
| 195 |
unsigned int cpp, |
| 196 |
XpmColor **colorTablePtr, |
| 197 |
xpmHashTable *hashtable) |
| 198 |
{ |
| 199 |
unsigned int key = 0, l, a, b, len; |
| 200 |
unsigned int curkey; /* current color key */ |
| 201 |
unsigned int lastwaskey; /* key read */ |
| 202 |
char buf[BUFSIZ+1]; |
| 203 |
char curbuf[BUFSIZ]; /* current buffer */ |
| 204 |
const char **sptr; |
| 205 |
char *s; |
| 206 |
XpmColor *color; |
| 207 |
XpmColor *colorTable; |
| 208 |
char **defaults; |
| 209 |
int ErrorStatus; |
| 210 |
|
| 211 |
if (ncolors >= UINT_MAX / sizeof(XpmColor)) |
| 212 |
return (XpmNoMemory); |
| 213 |
colorTable = (XpmColor *) XpmCalloc(ncolors, sizeof(XpmColor)); |
| 214 |
if (!colorTable) |
| 215 |
return (XpmNoMemory); |
| 216 |
|
| 217 |
if (!data->format) { /* XPM 2 or 3 */ |
| 218 |
for (a = 0, color = colorTable; a < ncolors; a++, color++) { |
| 219 |
ErrorStatus = xpmNextString(data); /* skip the line */ |
| 220 |
if (ErrorStatus != XpmSuccess) |
| 221 |
goto error; |
| 222 |
|
| 223 |
/* |
| 224 |
* read pixel value |
| 225 |
*/ |
| 226 |
if (cpp >= UINT_MAX - 1) { |
| 227 |
ErrorStatus = XpmNoMemory; |
| 228 |
goto error; |
| 229 |
} |
| 230 |
color->string = (char *) XpmMalloc(cpp + 1); |
| 231 |
if (!color->string) { |
| 232 |
ErrorStatus = XpmNoMemory; |
| 233 |
goto error; |
| 234 |
} |
| 235 |
for (b = 0, s = color->string; b < cpp; b++, s++) { |
| 236 |
int c = xpmGetC(data); |
| 237 |
if (c < 0) { |
| 238 |
ErrorStatus = XpmFileInvalid; |
| 239 |
goto error; |
| 240 |
} |
| 241 |
*s = (char) c; |
| 242 |
} |
| 243 |
*s = '\0'; |
| 244 |
|
| 245 |
/* |
| 246 |
* store the string in the hashtable with its color index number |
| 247 |
*/ |
| 248 |
if (USE_HASHTABLE) { |
| 249 |
ErrorStatus = |
| 250 |
xpmHashIntern(hashtable, color->string, HashAtomData(a)); |
| 251 |
if (ErrorStatus != XpmSuccess) { |
| 252 |
goto error; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
/* |
| 257 |
* read color keys and values |
| 258 |
*/ |
| 259 |
defaults = (char **) color; |
| 260 |
curkey = 0; |
| 261 |
lastwaskey = 0; |
| 262 |
*curbuf = '\0'; /* init curbuf */ |
| 263 |
while ((l = xpmNextWord(data, buf, BUFSIZ))) { |
| 264 |
if (!lastwaskey) { |
| 265 |
for (key = 0, sptr = xpmColorKeys; key < NKEYS; key++, |
| 266 |
sptr++) |
| 267 |
if ((strlen(*sptr) == l) && (!strncmp(*sptr, buf, l))) |
| 268 |
break; |
| 269 |
} |
| 270 |
if (!lastwaskey && key < NKEYS) { /* open new key */ |
| 271 |
if (curkey) { /* flush string */ |
| 272 |
len = strlen(curbuf) + 1; |
| 273 |
s = (char *) XpmMalloc(len); |
| 274 |
if (!s) { |
| 275 |
ErrorStatus = XpmNoMemory; |
| 276 |
goto error; |
| 277 |
} |
| 278 |
defaults[curkey] = s; |
| 279 |
memcpy(s, curbuf, len); |
| 280 |
} |
| 281 |
curkey = key + 1; /* set new key */ |
| 282 |
*curbuf = '\0'; /* reset curbuf */ |
| 283 |
lastwaskey = 1; |
| 284 |
} else { |
| 285 |
if (!curkey) { /* key without value */ |
| 286 |
ErrorStatus = XpmFileInvalid; |
| 287 |
goto error; |
| 288 |
} |
| 289 |
if (!lastwaskey) { |
| 290 |
if (!xstrlcat(curbuf, " ", sizeof(curbuf))) { /* append space */ |
| 291 |
ErrorStatus = XpmFileInvalid; |
| 292 |
goto error; |
| 293 |
} |
| 294 |
} |
| 295 |
buf[l] = '\0'; |
| 296 |
if (!xstrlcat(curbuf, buf, sizeof(curbuf))) { /* append buf */ |
| 297 |
ErrorStatus = XpmFileInvalid; |
| 298 |
goto error; |
| 299 |
} |
| 300 |
lastwaskey = 0; |
| 301 |
} |
| 302 |
} |
| 303 |
if (!curkey) { /* key without value */ |
| 304 |
ErrorStatus = XpmFileInvalid; |
| 305 |
goto error; |
| 306 |
} |
| 307 |
len = strlen(curbuf) + 1; /* integer overflow just theoretically possible */ |
| 308 |
s = defaults[curkey] = (char *) XpmMalloc(len); |
| 309 |
if (!s) { |
| 310 |
ErrorStatus = XpmNoMemory; |
| 311 |
goto error; |
| 312 |
} |
| 313 |
memcpy(s, curbuf, len); |
| 314 |
} |
| 315 |
} else { /* XPM 1 */ |
| 316 |
/* get to the beginning of the first string */ |
| 317 |
data->Bos = '"'; |
| 318 |
data->Eos = '\0'; |
| 319 |
ErrorStatus = xpmNextString(data); |
| 320 |
if (ErrorStatus != XpmSuccess) |
| 321 |
goto error; |
| 322 |
data->Eos = '"'; |
| 323 |
for (a = 0, color = colorTable; a < ncolors; a++, color++) { |
| 324 |
|
| 325 |
/* |
| 326 |
* read pixel value |
| 327 |
*/ |
| 328 |
if (cpp >= UINT_MAX - 1) { |
| 329 |
ErrorStatus = XpmNoMemory; |
| 330 |
goto error; |
| 331 |
} |
| 332 |
color->string = (char *) XpmMalloc(cpp + 1); |
| 333 |
if (!color->string) { |
| 334 |
ErrorStatus = XpmNoMemory; |
| 335 |
goto error; |
| 336 |
} |
| 337 |
for (b = 0, s = color->string; b < cpp; b++, s++) { |
| 338 |
int c = xpmGetC(data); |
| 339 |
if (c < 0) { |
| 340 |
ErrorStatus = XpmFileInvalid; |
| 341 |
goto error; |
| 342 |
} |
| 343 |
*s = (char) c; |
| 344 |
} |
| 345 |
*s = '\0'; |
| 346 |
|
| 347 |
/* |
| 348 |
* store the string in the hashtable with its color index number |
| 349 |
*/ |
| 350 |
if (USE_HASHTABLE) { |
| 351 |
ErrorStatus = |
| 352 |
xpmHashIntern(hashtable, color->string, HashAtomData(a)); |
| 353 |
if (ErrorStatus != XpmSuccess) { |
| 354 |
goto error; |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
/* |
| 359 |
* read color values |
| 360 |
*/ |
| 361 |
ErrorStatus = xpmNextString(data); /* get to the next string */ |
| 362 |
if (ErrorStatus != XpmSuccess) |
| 363 |
goto error; |
| 364 |
*curbuf = '\0'; /* init curbuf */ |
| 365 |
while ((l = xpmNextWord(data, buf, BUFSIZ))) { |
| 366 |
if (*curbuf != '\0') { |
| 367 |
if (!xstrlcat(curbuf, " ", sizeof(curbuf))) { /* append space */ |
| 368 |
ErrorStatus = XpmFileInvalid; |
| 369 |
goto error; |
| 370 |
} |
| 371 |
} |
| 372 |
buf[l] = '\0'; |
| 373 |
if (!xstrlcat(curbuf, buf, sizeof(curbuf))) { /* append buf */ |
| 374 |
ErrorStatus = XpmFileInvalid; |
| 375 |
goto error; |
| 376 |
} |
| 377 |
} |
| 378 |
len = strlen(curbuf) + 1; |
| 379 |
s = (char *) XpmMalloc(len); |
| 380 |
if (!s) { |
| 381 |
ErrorStatus = XpmNoMemory; |
| 382 |
goto error; |
| 383 |
} |
| 384 |
memcpy(s, curbuf, len); |
| 385 |
color->c_color = s; |
| 386 |
*curbuf = '\0'; /* reset curbuf */ |
| 387 |
if (a < ncolors - 1) { /* can we trust ncolors -> leave data's bounds */ |
| 388 |
ErrorStatus = xpmNextString(data); /* get to the next string */ |
| 389 |
if (ErrorStatus != XpmSuccess) |
| 390 |
goto error; |
| 391 |
} |
| 392 |
} |
| 393 |
} |
| 394 |
*colorTablePtr = colorTable; |
| 395 |
return (XpmSuccess); |
| 396 |
|
| 397 |
error: |
| 398 |
xpmFreeColorTable(colorTable, ncolors); |
| 399 |
return ErrorStatus; |
| 400 |
} |
| 401 |
|
| 402 |
static int |
| 403 |
ParsePixels( |
| 404 |
xpmData *data, |
| 405 |
unsigned int width, |
| 406 |
unsigned int height, |
| 407 |
unsigned int ncolors, |
| 408 |
unsigned int cpp, |
| 409 |
XpmColor *colorTable, |
| 410 |
xpmHashTable *hashtable, |
| 411 |
unsigned int **pixels) |
| 412 |
{ |
| 413 |
unsigned int *iptr, *iptr2 = NULL; /* found by Egbert Eich */ |
| 414 |
unsigned int a, x, y; |
| 415 |
int ErrorStatus; |
| 416 |
|
| 417 |
if ((width == 0) && (height != 0)) |
| 418 |
return (XpmFileInvalid); |
| 419 |
|
| 420 |
if ((height == 0) && (width != 0)) |
| 421 |
return (XpmFileInvalid); |
| 422 |
|
| 423 |
if ((height > 0 && width >= UINT_MAX / height) || |
| 424 |
width * height >= UINT_MAX / sizeof(unsigned int)) |
| 425 |
return XpmNoMemory; |
| 426 |
|
| 427 |
iptr2 = (unsigned int *) XpmMalloc(sizeof(unsigned int) * width * height); |
| 428 |
if (!iptr2) |
| 429 |
return (XpmNoMemory); |
| 430 |
|
| 431 |
iptr = iptr2; |
| 432 |
|
| 433 |
switch (cpp) { |
| 434 |
|
| 435 |
case (1): /* Optimize for single character |
| 436 |
* colors */ |
| 437 |
{ |
| 438 |
unsigned short colidx[256]; |
| 439 |
|
| 440 |
if (ncolors > 256) { |
| 441 |
XpmFree(iptr2); /* found by Egbert Eich */ |
| 442 |
return (XpmFileInvalid); |
| 443 |
} |
| 444 |
|
| 445 |
bzero((char *)colidx, 256 * sizeof(short)); |
| 446 |
for (a = 0; a < ncolors; a++) |
| 447 |
colidx[(unsigned char)colorTable[a].string[0]] = a + 1; |
| 448 |
|
| 449 |
for (y = 0; y < height; y++) { |
| 450 |
ErrorStatus = xpmNextString(data); |
| 451 |
if (ErrorStatus != XpmSuccess) { |
| 452 |
XpmFree(iptr2); |
| 453 |
return (ErrorStatus); |
| 454 |
} |
| 455 |
for (x = 0; x < width; x++, iptr++) { |
| 456 |
int c = xpmGetC(data); |
| 457 |
|
| 458 |
if (c > 0 && c < 256 && colidx[c] != 0) |
| 459 |
*iptr = colidx[c] - 1; |
| 460 |
else { |
| 461 |
XpmFree(iptr2); |
| 462 |
return (XpmFileInvalid); |
| 463 |
} |
| 464 |
} |
| 465 |
} |
| 466 |
} |
| 467 |
break; |
| 468 |
|
| 469 |
case (2): /* Optimize for double character |
| 470 |
* colors */ |
| 471 |
{ |
| 472 |
|
| 473 |
/* free all allocated pointers at all exits */ |
| 474 |
#define FREE_CIDX \ |
| 475 |
do \ |
| 476 |
{ \ |
| 477 |
int f; for (f = 0; f < 256; f++) \ |
| 478 |
if (cidx[f]) XpmFree(cidx[f]); \ |
| 479 |
} while(0) |
| 480 |
|
| 481 |
/* array of pointers malloced by need */ |
| 482 |
unsigned short *cidx[256]; |
| 483 |
unsigned int char1; |
| 484 |
|
| 485 |
bzero((char *)cidx, 256 * sizeof(unsigned short *)); /* init */ |
| 486 |
for (a = 0; a < ncolors; a++) { |
| 487 |
char1 = (unsigned char) colorTable[a].string[0]; |
| 488 |
if (cidx[char1] == NULL) { /* get new memory */ |
| 489 |
cidx[char1] = (unsigned short *) |
| 490 |
XpmCalloc(256, sizeof(unsigned short)); |
| 491 |
if (cidx[char1] == NULL) { /* new block failed */ |
| 492 |
FREE_CIDX; |
| 493 |
XpmFree(iptr2); |
| 494 |
return (XpmNoMemory); |
| 495 |
} |
| 496 |
} |
| 497 |
cidx[char1][(unsigned char)colorTable[a].string[1]] = a + 1; |
| 498 |
} |
| 499 |
|
| 500 |
for (y = 0; y < height; y++) { |
| 501 |
ErrorStatus = xpmNextString(data); |
| 502 |
if (ErrorStatus != XpmSuccess) { |
| 503 |
FREE_CIDX; |
| 504 |
XpmFree(iptr2); |
| 505 |
return (ErrorStatus); |
| 506 |
} |
| 507 |
for (x = 0; x < width; x++, iptr++) { |
| 508 |
int cc1 = xpmGetC(data); |
| 509 |
if (cc1 > 0 && cc1 < 256) { |
| 510 |
int cc2 = xpmGetC(data); |
| 511 |
if (cc2 > 0 && cc2 < 256 && |
| 512 |
cidx[cc1] && cidx[cc1][cc2] != 0) |
| 513 |
*iptr = cidx[cc1][cc2] - 1; |
| 514 |
else { |
| 515 |
FREE_CIDX; |
| 516 |
XpmFree(iptr2); |
| 517 |
return (XpmFileInvalid); |
| 518 |
} |
| 519 |
} else { |
| 520 |
FREE_CIDX; |
| 521 |
XpmFree(iptr2); |
| 522 |
return (XpmFileInvalid); |
| 523 |
} |
| 524 |
} |
| 525 |
} |
| 526 |
FREE_CIDX; |
| 527 |
} |
| 528 |
break; |
| 529 |
|
| 530 |
default: /* Non-optimized case of long color |
| 531 |
* names */ |
| 532 |
{ |
| 533 |
char *s; |
| 534 |
char buf[BUFSIZ]; |
| 535 |
|
| 536 |
if (cpp >= sizeof(buf)) { |
| 537 |
XpmFree(iptr2); /* found by Egbert Eich */ |
| 538 |
return (XpmFileInvalid); |
| 539 |
} |
| 540 |
|
| 541 |
buf[cpp] = '\0'; |
| 542 |
if (USE_HASHTABLE) { |
| 543 |
xpmHashAtom *slot; |
| 544 |
|
| 545 |
for (y = 0; y < height; y++) { |
| 546 |
ErrorStatus = xpmNextString(data); |
| 547 |
if (ErrorStatus != XpmSuccess) { |
| 548 |
XpmFree(iptr2); |
| 549 |
return (ErrorStatus); |
| 550 |
} |
| 551 |
for (x = 0; x < width; x++, iptr++) { |
| 552 |
for (a = 0, s = buf; a < cpp; a++, s++) { |
| 553 |
int c = xpmGetC(data); |
| 554 |
if (c < 0) { |
| 555 |
XpmFree(iptr2); |
| 556 |
return (XpmFileInvalid); |
| 557 |
} |
| 558 |
*s = (char) c; |
| 559 |
} |
| 560 |
slot = xpmHashSlot(hashtable, buf); |
| 561 |
if (!*slot) { /* no color matches */ |
| 562 |
XpmFree(iptr2); |
| 563 |
return (XpmFileInvalid); |
| 564 |
} |
| 565 |
*iptr = HashColorIndex(slot); |
| 566 |
} |
| 567 |
} |
| 568 |
} else { |
| 569 |
for (y = 0; y < height; y++) { |
| 570 |
ErrorStatus = xpmNextString(data); |
| 571 |
if (ErrorStatus != XpmSuccess) { |
| 572 |
XpmFree(iptr2); |
| 573 |
return (ErrorStatus); |
| 574 |
} |
| 575 |
for (x = 0; x < width; x++, iptr++) { |
| 576 |
for (a = 0, s = buf; a < cpp; a++, s++) { |
| 577 |
int c = xpmGetC(data); |
| 578 |
if (c < 0) { |
| 579 |
XpmFree(iptr2); |
| 580 |
return (XpmFileInvalid); |
| 581 |
} |
| 582 |
*s = (char) c; |
| 583 |
} |
| 584 |
for (a = 0; a < ncolors; a++) |
| 585 |
if (!strcmp(colorTable[a].string, buf)) |
| 586 |
break; |
| 587 |
if (a == ncolors) { /* no color matches */ |
| 588 |
XpmFree(iptr2); |
| 589 |
return (XpmFileInvalid); |
| 590 |
} |
| 591 |
*iptr = a; |
| 592 |
} |
| 593 |
} |
| 594 |
} |
| 595 |
} |
| 596 |
break; |
| 597 |
} |
| 598 |
*pixels = iptr2; |
| 599 |
return (XpmSuccess); |
| 600 |
} |
| 601 |
|
| 602 |
int |
| 603 |
xpmParseExtensions( |
| 604 |
xpmData *data, |
| 605 |
XpmExtension **extensions, |
| 606 |
unsigned int *nextensions) |
| 607 |
{ |
| 608 |
XpmExtension *exts = NULL, *ext; |
| 609 |
unsigned int num = 0; |
| 610 |
unsigned int nlines, a, l, notstart, notend = 0; |
| 611 |
int status; |
| 612 |
char *string, *s, *s2, **sp; |
| 613 |
|
| 614 |
xpmNextString(data); |
| 615 |
exts = (XpmExtension *) XpmMalloc(sizeof(XpmExtension)); |
| 616 |
/* get the whole string */ |
| 617 |
status = xpmGetString(data, &string, &l); |
| 618 |
if (status != XpmSuccess) { |
| 619 |
XpmFree(exts); |
| 620 |
return (status); |
| 621 |
} |
| 622 |
/* look for the key word XPMEXT, skip lines before this */ |
| 623 |
while ((notstart = strncmp("XPMEXT", string, 6)) |
| 624 |
&& (notend = strncmp("XPMENDEXT", string, 9))) { |
| 625 |
XpmFree(string); |
| 626 |
xpmNextString(data); |
| 627 |
status = xpmGetString(data, &string, &l); |
| 628 |
if (status != XpmSuccess) { |
| 629 |
XpmFree(exts); |
| 630 |
return (status); |
| 631 |
} |
| 632 |
} |
| 633 |
if (!notstart) |
| 634 |
notend = strncmp("XPMENDEXT", string, 9); |
| 635 |
while (!notstart && notend) { |
| 636 |
/* there starts an extension */ |
| 637 |
ext = (XpmExtension *) |
| 638 |
XpmRealloc(exts, (num + 1) * sizeof(XpmExtension)); /* can the loop be forced to iterate often enough to make "(num + 1) * sizeof(XpmExtension)" wrapping? */ |
| 639 |
if (!ext) { |
| 640 |
XpmFree(string); |
| 641 |
XpmFreeExtensions(exts, num); |
| 642 |
return (XpmNoMemory); |
| 643 |
} |
| 644 |
exts = ext; |
| 645 |
ext += num; |
| 646 |
/* skip whitespace and store its name */ |
| 647 |
s2 = s = string + 6; |
| 648 |
while (isspace(*s2)) |
| 649 |
s2++; |
| 650 |
a = s2 - s; |
| 651 |
ext->name = (char *) XpmMalloc(l - a - 6); |
| 652 |
if (!ext->name) { |
| 653 |
XpmFree(string); |
| 654 |
ext->lines = NULL; |
| 655 |
ext->nlines = 0; |
| 656 |
XpmFreeExtensions(exts, num + 1); |
| 657 |
return (XpmNoMemory); |
| 658 |
} |
| 659 |
strncpy(ext->name, s + a, l - a - 6); |
| 660 |
XpmFree(string); |
| 661 |
/* now store the related lines */ |
| 662 |
xpmNextString(data); |
| 663 |
status = xpmGetString(data, &string, &l); |
| 664 |
if (status != XpmSuccess) { |
| 665 |
ext->lines = NULL; |
| 666 |
ext->nlines = 0; |
| 667 |
XpmFreeExtensions(exts, num + 1); |
| 668 |
return (status); |
| 669 |
} |
| 670 |
ext->lines = (char **) XpmMalloc(sizeof(char *)); |
| 671 |
nlines = 0; |
| 672 |
while ((notstart = strncmp("XPMEXT", string, 6)) |
| 673 |
&& (notend = strncmp("XPMENDEXT", string, 9))) { |
| 674 |
sp = (char **) |
| 675 |
XpmRealloc(ext->lines, (nlines + 1) * sizeof(char *)); /* can we iterate enough for a wrapping? */ |
| 676 |
if (!sp) { |
| 677 |
XpmFree(string); |
| 678 |
ext->nlines = nlines; |
| 679 |
XpmFreeExtensions(exts, num + 1); |
| 680 |
return (XpmNoMemory); |
| 681 |
} |
| 682 |
ext->lines = sp; |
| 683 |
ext->lines[nlines] = string; |
| 684 |
nlines++; |
| 685 |
xpmNextString(data); |
| 686 |
status = xpmGetString(data, &string, &l); |
| 687 |
if (status != XpmSuccess) { |
| 688 |
ext->nlines = nlines; |
| 689 |
XpmFreeExtensions(exts, num + 1); |
| 690 |
return (status); |
| 691 |
} |
| 692 |
} |
| 693 |
if (!nlines) { |
| 694 |
XpmFree(ext->lines); |
| 695 |
ext->lines = NULL; |
| 696 |
} |
| 697 |
ext->nlines = nlines; |
| 698 |
num++; |
| 699 |
} |
| 700 |
if (!num) { |
| 701 |
XpmFree(string); |
| 702 |
XpmFree(exts); |
| 703 |
exts = NULL; |
| 704 |
} else if (!notend) |
| 705 |
XpmFree(string); |
| 706 |
*nextensions = num; |
| 707 |
*extensions = exts; |
| 708 |
return (XpmSuccess); |
| 709 |
} |
| 710 |
|
| 711 |
|
| 712 |
/* function call in case of error */ |
| 713 |
#undef RETURN |
| 714 |
#define RETURN(status) \ |
| 715 |
do { \ |
| 716 |
goto error; \ |
| 717 |
} while(0) |
| 718 |
|
| 719 |
/* |
| 720 |
* This function parses an Xpm file or data and store the found information |
| 721 |
* in an an XpmImage structure which is returned. |
| 722 |
*/ |
| 723 |
int |
| 724 |
xpmParseData( |
| 725 |
xpmData *data, |
| 726 |
XpmImage *image, |
| 727 |
XpmInfo *info) |
| 728 |
{ |
| 729 |
/* variables to return */ |
| 730 |
unsigned int width, height, ncolors, cpp; |
| 731 |
unsigned int x_hotspot, y_hotspot, hotspot = 0, extensions = 0; |
| 732 |
XpmColor *colorTable = NULL; |
| 733 |
unsigned int *pixelindex = NULL; |
| 734 |
char *hints_cmt = NULL; |
| 735 |
char *colors_cmt = NULL; |
| 736 |
char *pixels_cmt = NULL; |
| 737 |
|
| 738 |
unsigned int cmts; |
| 739 |
int ErrorStatus; |
| 740 |
xpmHashTable hashtable; |
| 741 |
|
| 742 |
cmts = info && (info->valuemask & XpmReturnComments); |
| 743 |
|
| 744 |
/* |
| 745 |
* parse the header |
| 746 |
*/ |
| 747 |
ErrorStatus = xpmParseHeader(data); |
| 748 |
if (ErrorStatus != XpmSuccess) |
| 749 |
return (ErrorStatus); |
| 750 |
|
| 751 |
/* |
| 752 |
* read values |
| 753 |
*/ |
| 754 |
ErrorStatus = xpmParseValues(data, &width, &height, &ncolors, &cpp, |
| 755 |
&x_hotspot, &y_hotspot, &hotspot, |
| 756 |
&extensions); |
| 757 |
if (ErrorStatus != XpmSuccess) |
| 758 |
return (ErrorStatus); |
| 759 |
|
| 760 |
/* |
| 761 |
* store the hints comment line |
| 762 |
*/ |
| 763 |
if (cmts) |
| 764 |
xpmGetCmt(data, &hints_cmt); |
| 765 |
|
| 766 |
/* |
| 767 |
* init the hashtable |
| 768 |
*/ |
| 769 |
if (USE_HASHTABLE) { |
| 770 |
ErrorStatus = xpmHashTableInit(&hashtable); |
| 771 |
if (ErrorStatus != XpmSuccess) |
| 772 |
RETURN(ErrorStatus); |
| 773 |
} |
| 774 |
|
| 775 |
/* |
| 776 |
* read colors |
| 777 |
*/ |
| 778 |
ErrorStatus = xpmParseColors(data, ncolors, cpp, &colorTable, &hashtable); |
| 779 |
if (ErrorStatus != XpmSuccess) { |
| 780 |
if (USE_HASHTABLE) |
| 781 |
xpmHashTableFree(&hashtable); |
| 782 |
RETURN(ErrorStatus); |
| 783 |
} |
| 784 |
|
| 785 |
/* |
| 786 |
* store the colors comment line |
| 787 |
*/ |
| 788 |
if (cmts) |
| 789 |
xpmGetCmt(data, &colors_cmt); |
| 790 |
|
| 791 |
/* |
| 792 |
* read pixels and index them on color number |
| 793 |
*/ |
| 794 |
ErrorStatus = ParsePixels(data, width, height, ncolors, cpp, colorTable, |
| 795 |
&hashtable, &pixelindex); |
| 796 |
|
| 797 |
/* |
| 798 |
* free the hastable |
| 799 |
*/ |
| 800 |
if (USE_HASHTABLE) |
| 801 |
xpmHashTableFree(&hashtable); |
| 802 |
|
| 803 |
if (ErrorStatus != XpmSuccess) |
| 804 |
RETURN(ErrorStatus); |
| 805 |
|
| 806 |
/* |
| 807 |
* store the pixels comment line |
| 808 |
*/ |
| 809 |
if (cmts) |
| 810 |
xpmGetCmt(data, &pixels_cmt); |
| 811 |
|
| 812 |
/* |
| 813 |
* parse extensions |
| 814 |
*/ |
| 815 |
if (info && (info->valuemask & XpmReturnExtensions)) { |
| 816 |
if (extensions) { |
| 817 |
ErrorStatus = xpmParseExtensions(data, &info->extensions, |
| 818 |
&info->nextensions); |
| 819 |
if (ErrorStatus != XpmSuccess) |
| 820 |
RETURN(ErrorStatus); |
| 821 |
} else { |
| 822 |
info->extensions = NULL; |
| 823 |
info->nextensions = 0; |
| 824 |
} |
| 825 |
} |
| 826 |
|
| 827 |
/* |
| 828 |
* store found information in the XpmImage structure |
| 829 |
*/ |
| 830 |
image->width = width; |
| 831 |
image->height = height; |
| 832 |
image->cpp = cpp; |
| 833 |
image->ncolors = ncolors; |
| 834 |
image->colorTable = colorTable; |
| 835 |
image->data = pixelindex; |
| 836 |
|
| 837 |
if (info) { |
| 838 |
if (cmts) { |
| 839 |
info->hints_cmt = hints_cmt; |
| 840 |
info->colors_cmt = colors_cmt; |
| 841 |
info->pixels_cmt = pixels_cmt; |
| 842 |
} |
| 843 |
if (hotspot) { |
| 844 |
info->x_hotspot = x_hotspot; |
| 845 |
info->y_hotspot = y_hotspot; |
| 846 |
info->valuemask |= XpmHotspot; |
| 847 |
} |
| 848 |
} |
| 849 |
return (XpmSuccess); |
| 850 |
|
| 851 |
/* exit point in case of error, free only locally allocated variables */ |
| 852 |
error: |
| 853 |
if (colorTable) |
| 854 |
xpmFreeColorTable(colorTable, ncolors); |
| 855 |
if (pixelindex) |
| 856 |
XpmFree(pixelindex); |
| 857 |
if (hints_cmt) |
| 858 |
XpmFree(hints_cmt); |
| 859 |
if (colors_cmt) |
| 860 |
XpmFree(colors_cmt); |
| 861 |
if (pixels_cmt) |
| 862 |
XpmFree(pixels_cmt); |
| 863 |
|
| 864 |
return(ErrorStatus); |
| 865 |
} |