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 *  Image.c:                                                                   *
28 *                                                                             *
29 *  XPM library                                                                *
30 *  Functions to init and free the XpmImage structure.                         *
31 *                                                                             *
32 *  Developed by Arnaud Le Hors                                                *
33 \*****************************************************************************/
34 
35 #include <LTconfig.h>
36 
37 /* amai: we can't include DebugUtil.h before system headers.
38    So we assume that system headers are idempotent, #include
39    all of those used below here and then try with our
40    DebugUtil.h! */
41 #include <stdio.h>
42 #include <string.h>
43 
44 #include <ctype.h>
45 #ifdef HAVE_SYS_TYPES_H
46 #include <sys/types.h>
47 #endif
48 #ifdef HAVE_SYS_STAT_H
49 #include <sys/stat.h>
50 #endif
51 #ifdef HAVE_UNISTD_H
52 #include <unistd.h>
53 #endif
54 #ifdef HAVE_FCNTL_H
55 #include <fcntl.h>
56 #endif
57 #if defined(FOR_MSW) || defined(WIN32)
58 #include <io.h>
59 #endif
60 
61 #include <X11/Intrinsic.h> /* Avoid re-definition of Pixel-type */
62 
63 #include <Xm/XpmP.h>
64 #include <XmI/XpmI.h>
65 
66 /* #if !defined(WITH_DBMALLOC) && !defined(WITH_DMALLOC) */
67 #include <XmI/DebugUtil.h>
68 
69 #ifdef HAVE_STRLCAT
70 # define STRLCAT(dst, src, dstsize) { \
71  	if (strlcat(dst, src, dstsize) >= (dstsize)) \
72 	    return (XpmFileInvalid); }
73 # define STRLCPY(dst, src, dstsize) { \
74   	if (strlcpy(dst, src, dstsize) >= (dstsize)) \
75 	    return (XpmFileInvalid); }
76 #else
77 # define STRLCAT(dst, src, dstsize) { \
78 	if ((strlen(dst) + strlen(src)) < (dstsize)) \
79  	    strcat(dst, src); \
80 	else return (XpmFileInvalid); }
81 # define STRLCPY(dst, src, dstsize) { \
82 	if (strlen(src) < (dstsize)) \
83  	    strcpy(dst, src); \
84 	else return (XpmFileInvalid); }
85 #endif
86 
87 /*
88  * Init returned data to free safely later on
89  */
90 void
xpmInitXpmImage(image)91 xpmInitXpmImage(image)
92     XpmImage *image;
93 {
94     image->ncolors = 0;
95     image->colorTable = NULL;
96     image->data = NULL;
97 }
98 
99 /*
100  * Free the XpmImage data which have been allocated
101  */
102 void
XpmFreeXpmImage(image)103 XpmFreeXpmImage(image)
104     XpmImage *image;
105 {
106     if (image->colorTable)
107 	xpmFreeColorTable(image->colorTable, image->ncolors);
108     if (image->data)
109 	XpmFree(image->data);
110     image->data = NULL;
111 }
112