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