2
* Copyright (C) 1989-95 GROUPE BULL
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:
11
* The above copyright notice and this permission notice shall be included in
12
* all copies or substantial portions of the Software.
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.
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.
26
/*****************************************************************************\
30
* Scan an image and possibly its mask and create an XPM array *
32
* Developed by Arnaud Le Hors *
33
\*****************************************************************************/
36
/* October 2004, source code review by Thomas Biege <thomas@suse.de> */
43
LFUNC(CreateColors, int, (char **dataptr, unsigned int *data_size,
44
XpmColor *colors, unsigned int ncolors,
47
LFUNC(CreatePixels, void, (char **dataptr, unsigned int data_size,
49
unsigned int height, unsigned int cpp,
50
unsigned int *pixels, XpmColor *colors));
52
LFUNC(CountExtensions, void, (XpmExtension *ext, unsigned int num,
53
unsigned int *ext_size,
54
unsigned int *ext_nlines));
56
LFUNC(CreateExtensions, void, (char **dataptr, unsigned int data_size,
58
XpmExtension *ext, unsigned int num,
59
unsigned int ext_nlines));
62
XpmCreateDataFromImage(display, data_return, image, shapeimage, attributes)
67
XpmAttributes *attributes;
73
/* initialize return value */
77
/* create an XpmImage from the image */
78
ErrorStatus = XpmCreateXpmImageFromImage(display, image, shapeimage,
79
&xpmimage, attributes);
80
if (ErrorStatus != XpmSuccess)
83
/* create the data from the XpmImage */
85
xpmSetInfo(&info, attributes);
86
ErrorStatus = XpmCreateDataFromXpmImage(data_return, &xpmimage, &info);
88
ErrorStatus = XpmCreateDataFromXpmImage(data_return, &xpmimage, NULL);
90
/* free the XpmImage */
91
XpmFreeXpmImage(&xpmimage);
97
#define RETURN(status) \
100
ErrorStatus = status; \
105
XpmCreateDataFromXpmImage(data_return, image, info)
110
/* calculation variables */
113
char **header = NULL, **data, **sptr, **sptr2, *s;
114
unsigned int header_size, header_nlines;
115
unsigned int data_size, data_nlines;
116
unsigned int extensions = 0, ext_size = 0, ext_nlines = 0;
117
unsigned int offset, l, n;
121
extensions = info && (info->valuemask & XpmExtensions)
122
&& info->nextensions;
124
/* compute the number of extensions lines and size */
126
CountExtensions(info->extensions, info->nextensions,
127
&ext_size, &ext_nlines);
130
* alloc a temporary array of char pointer for the header section which
131
* is the hints line + the color table lines
133
header_nlines = 1 + image->ncolors; /* this may wrap and/or become 0 */
135
/* 2nd check superfluous if we do not need header_nlines any further */
136
if(header_nlines <= image->ncolors ||
137
header_nlines >= UINT_MAX / sizeof(char *))
140
header_size = sizeof(char *) * header_nlines;
141
if (header_size >= UINT_MAX / sizeof(char *))
142
return (XpmNoMemory);
143
header = (char **) XpmCalloc(header_size, sizeof(char *)); /* can we trust image->ncolors */
145
return (XpmNoMemory);
147
/* print the hints line */
152
sprintf(s, "%d %d %d %d", image->width, image->height,
153
image->ncolors, image->cpp);
158
if (info && (info->valuemask & XpmHotspot)) {
162
sprintf(s, " %d %d", info->x_hotspot, info->y_hotspot);
168
strcpy(s, " XPMEXT");
172
*header = (char *) XpmMalloc(l);
176
strcpy(*header, buf);
179
ErrorStatus = CreateColors(header + 1, &header_size,
180
image->colorTable, image->ncolors, image->cpp);
182
if (ErrorStatus != XpmSuccess)
185
/* now we know the size needed, alloc the data and copy the header lines */
186
offset = image->width * image->cpp + 1;
188
if(offset <= image->width || offset <= image->cpp)
191
if( (image->height + ext_nlines) >= UINT_MAX / sizeof(char *))
193
data_size = (image->height + ext_nlines) * sizeof(char *);
195
if (image->height > UINT_MAX / offset ||
196
image->height * offset > UINT_MAX - data_size)
198
data_size += image->height * offset;
200
if( (header_size + ext_size) >= (UINT_MAX - data_size) )
202
data_size += header_size + ext_size;
204
data = (char **) XpmMalloc(data_size);
208
data_nlines = header_nlines + image->height + ext_nlines;
209
*data = (char *) (data + data_nlines);
211
/* can header have less elements then n suggests? */
213
for (l = 0, sptr = data, sptr2 = header; l <= n && sptr && sptr2; l++, sptr++, sptr2++) {
214
strcpy(*sptr, *sptr2);
215
*(sptr + 1) = *sptr + strlen(*sptr2) + 1;
219
data[header_nlines] = (char *) data + header_size
220
+ (image->height + ext_nlines) * sizeof(char *);
222
CreatePixels(data + header_nlines, data_size-header_nlines, image->width, image->height,
223
image->cpp, image->data, image->colorTable);
225
/* print extensions */
227
CreateExtensions(data + header_nlines + image->height - 1,
228
data_size - header_nlines - image->height + 1, offset,
229
info->extensions, info->nextensions,
233
ErrorStatus = XpmSuccess;
235
/* exit point, free only locally allocated variables */
238
for (l = 0; l < header_nlines; l++)
247
CreateColors(dataptr, data_size, colors, ncolors, cpp)
249
unsigned int *data_size;
251
unsigned int ncolors;
255
unsigned int a, key, l;
259
/* can ncolors be trusted here? */
260
for (a = 0; a < ncolors; a++, colors++, dataptr++) {
262
defaults = (char **) colors;
263
if(sizeof(buf) <= cpp)
265
strncpy(buf, *defaults++, cpp);
268
if(sizeof(buf) <= (s-buf))
271
for (key = 1; key <= NKEYS; key++, defaults++) {
272
if ((s2 = *defaults)) {
276
/* assume C99 compliance */
277
snprintf(s, sizeof(buf)-(s-buf), "\t%s %s", xpmColorKeys[key - 1], s2);
281
/* does s point out-of-bounds? */
282
if(sizeof(buf) < (s-buf))
286
/* what about using strdup()? */
288
s = (char *) XpmMalloc(l);
290
return (XpmNoMemory);
292
*dataptr = strcpy(s, buf);
298
CreatePixels(dataptr, data_size, width, height, cpp, pixels, colors)
300
unsigned int data_size;
304
unsigned int *pixels;
308
unsigned int x, y, h, offset;
315
offset = width * cpp + 1;
317
if(offset <= width || offset <= cpp)
321
for (y = 0; y < h; y++, dataptr++) {
323
/* why trust width? */
324
for (x = 0; x < width; x++, pixels++) {
325
if(cpp > (data_size - (s - *dataptr)))
327
strncpy(s, colors[*pixels].string, cpp); /* why trust pixel? */
331
if(offset > data_size)
333
*(dataptr + 1) = *dataptr + offset;
335
/* duplicate some code to avoid a test in the loop */
337
/* why trust width? */
338
for (x = 0; x < width; x++, pixels++) {
339
if(cpp > data_size - (s - *dataptr))
341
strncpy(s, colors[*pixels].string, cpp); /* why should we trust *pixel? */
348
CountExtensions(ext, num, ext_size, ext_nlines)
351
unsigned int *ext_size;
352
unsigned int *ext_nlines;
354
unsigned int x, y, a, size, nlines;
359
for (x = 0; x < num; x++, ext++) {
361
nlines += ext->nlines + 1;
362
/* 8 = 7 (for "XPMEXT ") + 1 (for 0) */
363
size += strlen(ext->name) + 8;
365
for (y = 0, line = ext->lines; y < a; y++, line++)
366
size += strlen(*line) + 1;
368
/* 10 and 1 are for the ending "XPMENDEXT" */
369
*ext_size = size + 10;
370
*ext_nlines = nlines + 1;
374
CreateExtensions(dataptr, data_size, offset, ext, num, ext_nlines)
376
unsigned int data_size;
380
unsigned int ext_nlines;
382
unsigned int x, y, a, b;
385
*(dataptr + 1) = *dataptr + offset;
388
for (x = 0; x < num; x++, ext++) {
389
snprintf(*dataptr, data_size, "XPMEXT %s", ext->name);
392
*(dataptr + 1) = *dataptr + strlen(ext->name) + 8;
394
b = ext->nlines; /* can we trust these values? */
395
for (y = 0, line = ext->lines; y < b; y++, line++) {
396
strcpy(*dataptr, *line);
399
*(dataptr + 1) = *dataptr + strlen(*line) + 1;
403
strcpy(*dataptr, "XPMENDEXT");