]> git.saurik.com Git - wxWidgets.git/blame - src/xpm/RdFToI.c
case insensitive HHP files and fixed buffer overflow vulnerability
[wxWidgets.git] / src / xpm / RdFToI.c
CommitLineData
cfbe03c9 1/*
e6ed776f 2 * Copyright (C) 1989-95 GROUPE BULL
cfbe03c9
JS
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/*****************************************************************************\
e6ed776f 27* RdFToI.c: *
cfbe03c9
JS
28* *
29* XPM library *
30* Parse an XPM file and create the image and possibly its mask *
31* *
32* Developed by Arnaud Le Hors *
33\*****************************************************************************/
34
e6ed776f
GRG
35#include "XpmI.h"
36#include <sys/stat.h>
37#if !defined(NO_ZPIPE) && defined(WIN32)
38# define popen _popen
39# define pclose _pclose
40# if defined(STAT_ZFILE)
41# include <io.h>
42# define stat _stat
43# define fstat _fstat
44# endif
45#endif
cfbe03c9 46
e6ed776f
GRG
47LFUNC(OpenReadFile, int, (char *filename, xpmData *mdata));
48LFUNC(xpmDataClose, void, (xpmData *mdata));
49
50#ifndef CXPMPROG
ea258ad3
DW
51#ifdef __OS2__
52/* Visual Age cannot deal with old, non-ansi, code */
53int XpmReadFileToImage(
54 Display* display
55, char* filename
56, XImage** image_return
57, XImage** shapeimage_return
58, XpmAttributes* attributes
59)
60#else
cfbe03c9 61int
e6ed776f
GRG
62XpmReadFileToImage(display, filename,
63 image_return, shapeimage_return, attributes)
64 Display *display;
65 char *filename;
66 XImage **image_return;
67 XImage **shapeimage_return;
68 XpmAttributes *attributes;
ea258ad3 69#endif
cfbe03c9
JS
70{
71 XpmImage image;
72 XpmInfo info;
73 int ErrorStatus;
e6ed776f 74 xpmData mdata;
cfbe03c9 75
e6ed776f
GRG
76 xpmInitXpmImage(&image);
77 xpmInitXpmInfo(&info);
78
79 /* open file to read */
80 if ((ErrorStatus = OpenReadFile(filename, &mdata)) != XpmSuccess)
81 return (ErrorStatus);
82
83 /* create the XImage from the XpmData */
cfbe03c9
JS
84 if (attributes) {
85 xpmInitAttributes(attributes);
86 xpmSetInfoMask(&info, attributes);
e6ed776f
GRG
87 ErrorStatus = xpmParseDataAndCreate(display, &mdata,
88 image_return, shapeimage_return,
89 &image, &info, attributes);
cfbe03c9 90 } else
e6ed776f
GRG
91 ErrorStatus = xpmParseDataAndCreate(display, &mdata,
92 image_return, shapeimage_return,
93 &image, NULL, attributes);
cfbe03c9
JS
94 if (attributes) {
95 if (ErrorStatus >= 0) /* no fatal error */
96 xpmSetAttributes(attributes, &image, &info);
97 XpmFreeXpmInfo(&info);
98 }
e6ed776f
GRG
99
100 xpmDataClose(&mdata);
cfbe03c9
JS
101 /* free the XpmImage */
102 XpmFreeXpmImage(&image);
103
104 return (ErrorStatus);
105}
106
ea258ad3
DW
107#ifdef __OS2__
108#define popen fopen
109#define pclose fclose
110/* Visual Age cannot deal with old, non-ansi, code */
111int XpmReadFileToXpmImage(
112 char* filename
113, XpmImage* image
114, XpmInfo* info
115)
116#else
cfbe03c9 117int
e6ed776f
GRG
118XpmReadFileToXpmImage(filename, image, info)
119 char *filename;
120 XpmImage *image;
121 XpmInfo *info;
ea258ad3 122#endif
cfbe03c9
JS
123{
124 xpmData mdata;
125 int ErrorStatus;
126
127 /* init returned values */
128 xpmInitXpmImage(image);
129 xpmInitXpmInfo(info);
130
131 /* open file to read */
e6ed776f 132 if ((ErrorStatus = OpenReadFile(filename, &mdata)) != XpmSuccess)
cfbe03c9
JS
133 return (ErrorStatus);
134
135 /* create the XpmImage from the XpmData */
136 ErrorStatus = xpmParseData(&mdata, image, info);
137
138 xpmDataClose(&mdata);
139
140 return (ErrorStatus);
141}
e6ed776f
GRG
142#endif /* CXPMPROG */
143
144/*
145 * open the given file to be read as an xpmData which is returned.
146 */
ea258ad3
DW
147#ifdef __OS2__
148/* Visual Age cannot deal with old, non-ansi, code */
149static int OpenReadFile(char* filename, xpmData* mdata)
150#else
e6ed776f
GRG
151static int
152OpenReadFile(filename, mdata)
153 char *filename;
154 xpmData *mdata;
ea258ad3 155#endif
e6ed776f
GRG
156{
157#ifndef NO_ZPIPE
3ca6a5f0 158 char buf[BUFSIZ];
e6ed776f 159# ifdef STAT_ZFILE
3ca6a5f0 160 char *compressfile;
e6ed776f
GRG
161 struct stat status;
162# endif
163#endif
164
165 if (!filename) {
166 mdata->stream.file = (stdin);
167 mdata->type = XPMFILE;
168 } else {
169#ifndef NO_ZPIPE
170 int len = strlen(filename);
171 if ((len > 2) && !strcmp(".Z", filename + (len - 2))) {
172 mdata->type = XPMPIPE;
173 sprintf(buf, "uncompress -c \"%s\"", filename);
174 if (!(mdata->stream.file = popen(buf, "r")))
175 return (XpmOpenFailed);
176
177 } else if ((len > 3) && !strcmp(".gz", filename + (len - 3))) {
178 mdata->type = XPMPIPE;
179 sprintf(buf, "gunzip -qc \"%s\"", filename);
180 if (!(mdata->stream.file = popen(buf, "r")))
181 return (XpmOpenFailed);
182
183 } else {
184# ifdef STAT_ZFILE
185 if (!(compressfile = (char *) XpmMalloc(len + 4)))
186 return (XpmNoMemory);
187
188 sprintf(compressfile, "%s.Z", filename);
189 if (!stat(compressfile, &status)) {
190 sprintf(buf, "uncompress -c \"%s\"", compressfile);
191 if (!(mdata->stream.file = popen(buf, "r"))) {
192 XpmFree(compressfile);
193 return (XpmOpenFailed);
194 }
195 mdata->type = XPMPIPE;
196 } else {
197 sprintf(compressfile, "%s.gz", filename);
198 if (!stat(compressfile, &status)) {
199 sprintf(buf, "gunzip -c \"%s\"", compressfile);
200 if (!(mdata->stream.file = popen(buf, "r"))) {
201 XpmFree(compressfile);
202 return (XpmOpenFailed);
203 }
204 mdata->type = XPMPIPE;
205 } else {
206# endif
207#endif
208 if (!(mdata->stream.file = fopen(filename, "r"))) {
209#if !defined(NO_ZPIPE) && defined(STAT_ZFILE)
210 XpmFree(compressfile);
211#endif
212 return (XpmOpenFailed);
213 }
214 mdata->type = XPMFILE;
215#ifndef NO_ZPIPE
216# ifdef STAT_ZFILE
217 }
218 }
219 XpmFree(compressfile);
220# endif
221 }
222#endif
223 }
224 mdata->CommentLength = 0;
225#ifdef CXPMPROG
226 mdata->lineNum = 0;
227 mdata->charNum = 0;
228#endif
229 return (XpmSuccess);
230}
231
232/*
233 * close the file related to the xpmData if any
234 */
ea258ad3
DW
235#ifdef __OS2__
236/* Visual Age cannot deal with old, non-ansi, code */
237static void
238xpmDataClose(xpmData* mdata)
239#else
e6ed776f
GRG
240static void
241xpmDataClose(mdata)
242 xpmData *mdata;
ea258ad3 243#endif
e6ed776f
GRG
244{
245 switch (mdata->type) {
246 case XPMFILE:
247 if (mdata->stream.file != (stdin))
248 fclose(mdata->stream.file);
249 break;
250#ifndef NO_ZPIPE
251 case XPMPIPE:
252 pclose(mdata->stream.file);
253 break;
254#endif
255 }
256}