CrDatFrI.c revision 2e2dd055
1a966c04fSmrg/* 2a966c04fSmrg * Copyright (C) 1989-95 GROUPE BULL 3a966c04fSmrg * 4a966c04fSmrg * Permission is hereby granted, free of charge, to any person obtaining a copy 5a966c04fSmrg * of this software and associated documentation files (the "Software"), to 6a966c04fSmrg * deal in the Software without restriction, including without limitation the 7a966c04fSmrg * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 8a966c04fSmrg * sell copies of the Software, and to permit persons to whom the Software is 9a966c04fSmrg * furnished to do so, subject to the following conditions: 10a966c04fSmrg * 11a966c04fSmrg * The above copyright notice and this permission notice shall be included in 12a966c04fSmrg * all copies or substantial portions of the Software. 13a966c04fSmrg * 14a966c04fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15a966c04fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16a966c04fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17a966c04fSmrg * GROUPE BULL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 18a966c04fSmrg * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19a966c04fSmrg * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20a966c04fSmrg * 21a966c04fSmrg * Except as contained in this notice, the name of GROUPE BULL shall not be 22a966c04fSmrg * used in advertising or otherwise to promote the sale, use or other dealings 23a966c04fSmrg * in this Software without prior written authorization from GROUPE BULL. 24a966c04fSmrg */ 25a966c04fSmrg 26a966c04fSmrg/*****************************************************************************\ 27a966c04fSmrg* CrDataFI.c: * 28a966c04fSmrg* * 29a966c04fSmrg* XPM library * 30a966c04fSmrg* Scan an image and possibly its mask and create an XPM array * 31a966c04fSmrg* * 32a966c04fSmrg* Developed by Arnaud Le Hors * 33a966c04fSmrg\*****************************************************************************/ 34a966c04fSmrg/* $XFree86$ */ 35a966c04fSmrg 36a966c04fSmrg/* October 2004, source code review by Thomas Biege <thomas@suse.de> */ 37a966c04fSmrg 38a966c04fSmrg#ifdef HAVE_CONFIG_H 39a966c04fSmrg#include <config.h> 40a966c04fSmrg#endif 41a966c04fSmrg#include "XpmI.h" 42a966c04fSmrg 43a966c04fSmrgLFUNC(CreateColors, int, (char **dataptr, unsigned int *data_size, 44a966c04fSmrg XpmColor *colors, unsigned int ncolors, 45a966c04fSmrg unsigned int cpp)); 46a966c04fSmrg 47a966c04fSmrgLFUNC(CreatePixels, void, (char **dataptr, unsigned int data_size, 48a966c04fSmrg unsigned int width, 49a966c04fSmrg unsigned int height, unsigned int cpp, 50a966c04fSmrg unsigned int *pixels, XpmColor *colors)); 51a966c04fSmrg 52a966c04fSmrgLFUNC(CountExtensions, void, (XpmExtension *ext, unsigned int num, 53a966c04fSmrg unsigned int *ext_size, 54a966c04fSmrg unsigned int *ext_nlines)); 55a966c04fSmrg 56a966c04fSmrgLFUNC(CreateExtensions, void, (char **dataptr, unsigned int data_size, 57a966c04fSmrg unsigned int offset, 58a966c04fSmrg XpmExtension *ext, unsigned int num, 59a966c04fSmrg unsigned int ext_nlines)); 60a966c04fSmrg 61a966c04fSmrgint 622e2dd055SmrgXpmCreateDataFromImage( 632e2dd055Smrg Display *display, 642e2dd055Smrg char ***data_return, 652e2dd055Smrg XImage *image, 662e2dd055Smrg XImage *shapeimage, 672e2dd055Smrg XpmAttributes *attributes) 68a966c04fSmrg{ 69a966c04fSmrg XpmImage xpmimage; 70a966c04fSmrg XpmInfo info; 71a966c04fSmrg int ErrorStatus; 72a966c04fSmrg 73a966c04fSmrg /* initialize return value */ 74a966c04fSmrg if (data_return) 75a966c04fSmrg *data_return = NULL; 76a966c04fSmrg 77a966c04fSmrg /* create an XpmImage from the image */ 78a966c04fSmrg ErrorStatus = XpmCreateXpmImageFromImage(display, image, shapeimage, 79a966c04fSmrg &xpmimage, attributes); 80a966c04fSmrg if (ErrorStatus != XpmSuccess) 81a966c04fSmrg return (ErrorStatus); 82a966c04fSmrg 83a966c04fSmrg /* create the data from the XpmImage */ 84a966c04fSmrg if (attributes) { 85a966c04fSmrg xpmSetInfo(&info, attributes); 86a966c04fSmrg ErrorStatus = XpmCreateDataFromXpmImage(data_return, &xpmimage, &info); 87a966c04fSmrg } else 88a966c04fSmrg ErrorStatus = XpmCreateDataFromXpmImage(data_return, &xpmimage, NULL); 89a966c04fSmrg 90a966c04fSmrg /* free the XpmImage */ 91a966c04fSmrg XpmFreeXpmImage(&xpmimage); 92a966c04fSmrg 93a966c04fSmrg return (ErrorStatus); 94a966c04fSmrg} 95a966c04fSmrg 96a966c04fSmrg#undef RETURN 97a966c04fSmrg#define RETURN(status) \ 98a966c04fSmrgdo \ 99a966c04fSmrg{ \ 100a966c04fSmrg ErrorStatus = status; \ 101a966c04fSmrg goto exit; \ 102a966c04fSmrg} while(0) 103a966c04fSmrg 104a966c04fSmrgint 1052e2dd055SmrgXpmCreateDataFromXpmImage( 1062e2dd055Smrg char ***data_return, 1072e2dd055Smrg XpmImage *image, 1082e2dd055Smrg XpmInfo *info) 109a966c04fSmrg{ 110a966c04fSmrg /* calculation variables */ 111a966c04fSmrg int ErrorStatus; 112a966c04fSmrg char buf[BUFSIZ]; 113a966c04fSmrg char **header = NULL, **data, **sptr, **sptr2, *s; 114a966c04fSmrg unsigned int header_size, header_nlines; 115a966c04fSmrg unsigned int data_size, data_nlines; 116a966c04fSmrg unsigned int extensions = 0, ext_size = 0, ext_nlines = 0; 117a966c04fSmrg unsigned int offset, l, n; 118a966c04fSmrg 119a966c04fSmrg *data_return = NULL; 120a966c04fSmrg 121a966c04fSmrg extensions = info && (info->valuemask & XpmExtensions) 122a966c04fSmrg && info->nextensions; 123a966c04fSmrg 124a966c04fSmrg /* compute the number of extensions lines and size */ 125a966c04fSmrg if (extensions) 126a966c04fSmrg CountExtensions(info->extensions, info->nextensions, 127a966c04fSmrg &ext_size, &ext_nlines); 128a966c04fSmrg 129a966c04fSmrg /* 130a966c04fSmrg * alloc a temporary array of char pointer for the header section which 131a966c04fSmrg * is the hints line + the color table lines 132a966c04fSmrg */ 133a966c04fSmrg header_nlines = 1 + image->ncolors; /* this may wrap and/or become 0 */ 134a966c04fSmrg 135a966c04fSmrg /* 2nd check superfluous if we do not need header_nlines any further */ 136a966c04fSmrg if(header_nlines <= image->ncolors || 137a966c04fSmrg header_nlines >= UINT_MAX / sizeof(char *)) 138a966c04fSmrg return(XpmNoMemory); 139a966c04fSmrg 140a966c04fSmrg header_size = sizeof(char *) * header_nlines; 141a966c04fSmrg if (header_size >= UINT_MAX / sizeof(char *)) 142a966c04fSmrg return (XpmNoMemory); 143a966c04fSmrg header = (char **) XpmCalloc(header_size, sizeof(char *)); /* can we trust image->ncolors */ 144a966c04fSmrg if (!header) 145a966c04fSmrg return (XpmNoMemory); 146a966c04fSmrg 147a966c04fSmrg /* print the hints line */ 148a966c04fSmrg s = buf; 149a966c04fSmrg#ifndef VOID_SPRINTF 150a966c04fSmrg s += 151a966c04fSmrg#endif 152a966c04fSmrg sprintf(s, "%d %d %d %d", image->width, image->height, 153a966c04fSmrg image->ncolors, image->cpp); 154a966c04fSmrg#ifdef VOID_SPRINTF 155a966c04fSmrg s += strlen(s); 156a966c04fSmrg#endif 157a966c04fSmrg 158a966c04fSmrg if (info && (info->valuemask & XpmHotspot)) { 159a966c04fSmrg#ifndef VOID_SPRINTF 160a966c04fSmrg s += 161a966c04fSmrg#endif 162a966c04fSmrg sprintf(s, " %d %d", info->x_hotspot, info->y_hotspot); 163a966c04fSmrg#ifdef VOID_SPRINTF 164a966c04fSmrg s += strlen(s); 165a966c04fSmrg#endif 166a966c04fSmrg } 167a966c04fSmrg if (extensions) { 168a966c04fSmrg strcpy(s, " XPMEXT"); 169a966c04fSmrg s += 7; 170a966c04fSmrg } 171a966c04fSmrg l = s - buf + 1; 172a966c04fSmrg *header = (char *) XpmMalloc(l); 173a966c04fSmrg if (!*header) 174a966c04fSmrg RETURN(XpmNoMemory); 175a966c04fSmrg header_size += l; 176a966c04fSmrg strcpy(*header, buf); 177a966c04fSmrg 178a966c04fSmrg /* print colors */ 179a966c04fSmrg ErrorStatus = CreateColors(header + 1, &header_size, 180a966c04fSmrg image->colorTable, image->ncolors, image->cpp); 181a966c04fSmrg 182a966c04fSmrg if (ErrorStatus != XpmSuccess) 183a966c04fSmrg RETURN(ErrorStatus); 184a966c04fSmrg 185a966c04fSmrg /* now we know the size needed, alloc the data and copy the header lines */ 186a966c04fSmrg offset = image->width * image->cpp + 1; 187a966c04fSmrg 188a966c04fSmrg if(offset <= image->width || offset <= image->cpp) 189a966c04fSmrg RETURN(XpmNoMemory); 190a966c04fSmrg 191a966c04fSmrg if( (image->height + ext_nlines) >= UINT_MAX / sizeof(char *)) 192a966c04fSmrg RETURN(XpmNoMemory); 193a966c04fSmrg data_size = (image->height + ext_nlines) * sizeof(char *); 194a966c04fSmrg 195a966c04fSmrg if (image->height > UINT_MAX / offset || 196a966c04fSmrg image->height * offset > UINT_MAX - data_size) 197a966c04fSmrg RETURN(XpmNoMemory); 198a966c04fSmrg data_size += image->height * offset; 199a966c04fSmrg 200a966c04fSmrg if( (header_size + ext_size) >= (UINT_MAX - data_size) ) 201a966c04fSmrg RETURN(XpmNoMemory); 202a966c04fSmrg data_size += header_size + ext_size; 203a966c04fSmrg 204a966c04fSmrg data = (char **) XpmMalloc(data_size); 205a966c04fSmrg if (!data) 206a966c04fSmrg RETURN(XpmNoMemory); 207a966c04fSmrg 208a966c04fSmrg data_nlines = header_nlines + image->height + ext_nlines; 209a966c04fSmrg *data = (char *) (data + data_nlines); 210a966c04fSmrg 211a966c04fSmrg /* can header have less elements then n suggests? */ 212a966c04fSmrg n = image->ncolors; 213a966c04fSmrg for (l = 0, sptr = data, sptr2 = header; l <= n && sptr && sptr2; l++, sptr++, sptr2++) { 214a966c04fSmrg strcpy(*sptr, *sptr2); 215a966c04fSmrg *(sptr + 1) = *sptr + strlen(*sptr2) + 1; 216a966c04fSmrg } 217a966c04fSmrg 218a966c04fSmrg /* print pixels */ 219a966c04fSmrg data[header_nlines] = (char *) data + header_size 220a966c04fSmrg + (image->height + ext_nlines) * sizeof(char *); 221a966c04fSmrg 222a966c04fSmrg CreatePixels(data + header_nlines, data_size-header_nlines, image->width, image->height, 223a966c04fSmrg image->cpp, image->data, image->colorTable); 224a966c04fSmrg 225a966c04fSmrg /* print extensions */ 226a966c04fSmrg if (extensions) 227a966c04fSmrg CreateExtensions(data + header_nlines + image->height - 1, 228a966c04fSmrg data_size - header_nlines - image->height + 1, offset, 229a966c04fSmrg info->extensions, info->nextensions, 230a966c04fSmrg ext_nlines); 231a966c04fSmrg 232a966c04fSmrg *data_return = data; 233a966c04fSmrg ErrorStatus = XpmSuccess; 234a966c04fSmrg 235a966c04fSmrg/* exit point, free only locally allocated variables */ 236a966c04fSmrgexit: 237a966c04fSmrg if (header) { 238a966c04fSmrg for (l = 0; l < header_nlines; l++) 239a966c04fSmrg if (header[l]) 240a966c04fSmrg XpmFree(header[l]); 241a966c04fSmrg XpmFree(header); 242a966c04fSmrg } 243a966c04fSmrg return(ErrorStatus); 244a966c04fSmrg} 245a966c04fSmrg 246a966c04fSmrgstatic int 2472e2dd055SmrgCreateColors( 2482e2dd055Smrg char **dataptr, 2492e2dd055Smrg unsigned int *data_size, 2502e2dd055Smrg XpmColor *colors, 2512e2dd055Smrg unsigned int ncolors, 2522e2dd055Smrg unsigned int cpp) 253a966c04fSmrg{ 254a966c04fSmrg char buf[BUFSIZ]; 255a966c04fSmrg unsigned int a, key, l; 256a966c04fSmrg char *s, *s2; 257a966c04fSmrg char **defaults; 258a966c04fSmrg 259a966c04fSmrg /* can ncolors be trusted here? */ 260a966c04fSmrg for (a = 0; a < ncolors; a++, colors++, dataptr++) { 261a966c04fSmrg 262a966c04fSmrg defaults = (char **) colors; 263a966c04fSmrg if(sizeof(buf) <= cpp) 264a966c04fSmrg return(XpmNoMemory); 265a966c04fSmrg strncpy(buf, *defaults++, cpp); 266a966c04fSmrg s = buf + cpp; 267a966c04fSmrg 268a966c04fSmrg if(sizeof(buf) <= (s-buf)) 269a966c04fSmrg return XpmNoMemory; 270a966c04fSmrg 271a966c04fSmrg for (key = 1; key <= NKEYS; key++, defaults++) { 272a966c04fSmrg if ((s2 = *defaults)) { 273a966c04fSmrg#ifndef VOID_SPRINTF 274a966c04fSmrg s += 275a966c04fSmrg#endif 276a966c04fSmrg /* assume C99 compliance */ 277a966c04fSmrg snprintf(s, sizeof(buf)-(s-buf), "\t%s %s", xpmColorKeys[key - 1], s2); 278a966c04fSmrg#ifdef VOID_SPRINTF 279a966c04fSmrg s += strlen(s); 280a966c04fSmrg#endif 281a966c04fSmrg /* does s point out-of-bounds? */ 282a966c04fSmrg if(sizeof(buf) < (s-buf)) 283a966c04fSmrg return XpmNoMemory; 284a966c04fSmrg } 285a966c04fSmrg } 286a966c04fSmrg /* what about using strdup()? */ 287a966c04fSmrg l = s - buf + 1; 288a966c04fSmrg s = (char *) XpmMalloc(l); 289a966c04fSmrg if (!s) 290a966c04fSmrg return (XpmNoMemory); 291a966c04fSmrg *data_size += l; 292a966c04fSmrg *dataptr = strcpy(s, buf); 293a966c04fSmrg } 294a966c04fSmrg return (XpmSuccess); 295a966c04fSmrg} 296a966c04fSmrg 297a966c04fSmrgstatic void 2982e2dd055SmrgCreatePixels( 2992e2dd055Smrg char **dataptr, 3002e2dd055Smrg unsigned int data_size, 3012e2dd055Smrg unsigned int width, 3022e2dd055Smrg unsigned int height, 3032e2dd055Smrg unsigned int cpp, 3042e2dd055Smrg unsigned int *pixels, 3052e2dd055Smrg XpmColor *colors) 306a966c04fSmrg{ 307a966c04fSmrg char *s; 308a966c04fSmrg unsigned int x, y, h, offset; 309a966c04fSmrg 310a966c04fSmrg if(height <= 1) 311a966c04fSmrg return; 312a966c04fSmrg 313a966c04fSmrg h = height - 1; 314a966c04fSmrg 315a966c04fSmrg offset = width * cpp + 1; 316a966c04fSmrg 317a966c04fSmrg if(offset <= width || offset <= cpp) 318a966c04fSmrg return; 319a966c04fSmrg 320a966c04fSmrg /* why trust h? */ 321a966c04fSmrg for (y = 0; y < h; y++, dataptr++) { 322a966c04fSmrg s = *dataptr; 323a966c04fSmrg /* why trust width? */ 324a966c04fSmrg for (x = 0; x < width; x++, pixels++) { 325a966c04fSmrg if(cpp > (data_size - (s - *dataptr))) 326a966c04fSmrg return; 327a966c04fSmrg strncpy(s, colors[*pixels].string, cpp); /* why trust pixel? */ 328a966c04fSmrg s += cpp; 329a966c04fSmrg } 330a966c04fSmrg *s = '\0'; 331a966c04fSmrg if(offset > data_size) 332a966c04fSmrg return; 333a966c04fSmrg *(dataptr + 1) = *dataptr + offset; 334a966c04fSmrg } 335a966c04fSmrg /* duplicate some code to avoid a test in the loop */ 336a966c04fSmrg s = *dataptr; 337a966c04fSmrg /* why trust width? */ 338a966c04fSmrg for (x = 0; x < width; x++, pixels++) { 339a966c04fSmrg if(cpp > data_size - (s - *dataptr)) 340a966c04fSmrg return; 341a966c04fSmrg strncpy(s, colors[*pixels].string, cpp); /* why should we trust *pixel? */ 342a966c04fSmrg s += cpp; 343a966c04fSmrg } 344a966c04fSmrg *s = '\0'; 345a966c04fSmrg} 346a966c04fSmrg 347a966c04fSmrgstatic void 3482e2dd055SmrgCountExtensions( 3492e2dd055Smrg XpmExtension *ext, 3502e2dd055Smrg unsigned int num, 3512e2dd055Smrg unsigned int *ext_size, 3522e2dd055Smrg unsigned int *ext_nlines) 353a966c04fSmrg{ 354a966c04fSmrg unsigned int x, y, a, size, nlines; 355a966c04fSmrg char **line; 356a966c04fSmrg 357a966c04fSmrg size = 0; 358a966c04fSmrg nlines = 0; 359a966c04fSmrg for (x = 0; x < num; x++, ext++) { 360a966c04fSmrg /* 1 for the name */ 361a966c04fSmrg nlines += ext->nlines + 1; 362a966c04fSmrg /* 8 = 7 (for "XPMEXT ") + 1 (for 0) */ 363a966c04fSmrg size += strlen(ext->name) + 8; 364a966c04fSmrg a = ext->nlines; 365a966c04fSmrg for (y = 0, line = ext->lines; y < a; y++, line++) 366a966c04fSmrg size += strlen(*line) + 1; 367a966c04fSmrg } 368a966c04fSmrg /* 10 and 1 are for the ending "XPMENDEXT" */ 369a966c04fSmrg *ext_size = size + 10; 370a966c04fSmrg *ext_nlines = nlines + 1; 371a966c04fSmrg} 372a966c04fSmrg 373a966c04fSmrgstatic void 3742e2dd055SmrgCreateExtensions( 3752e2dd055Smrg char **dataptr, 3762e2dd055Smrg unsigned int data_size, 3772e2dd055Smrg unsigned int offset, 3782e2dd055Smrg XpmExtension *ext, 3792e2dd055Smrg unsigned int num, 3802e2dd055Smrg unsigned int ext_nlines) 381a966c04fSmrg{ 382a966c04fSmrg unsigned int x, y, a, b; 383a966c04fSmrg char **line; 384a966c04fSmrg 385a966c04fSmrg *(dataptr + 1) = *dataptr + offset; 386a966c04fSmrg dataptr++; 387a966c04fSmrg a = 0; 388a966c04fSmrg for (x = 0; x < num; x++, ext++) { 389a966c04fSmrg snprintf(*dataptr, data_size, "XPMEXT %s", ext->name); 390a966c04fSmrg a++; 391a966c04fSmrg if (a < ext_nlines) 392a966c04fSmrg *(dataptr + 1) = *dataptr + strlen(ext->name) + 8; 393a966c04fSmrg dataptr++; 394a966c04fSmrg b = ext->nlines; /* can we trust these values? */ 395a966c04fSmrg for (y = 0, line = ext->lines; y < b; y++, line++) { 396a966c04fSmrg strcpy(*dataptr, *line); 397a966c04fSmrg a++; 398a966c04fSmrg if (a < ext_nlines) 399a966c04fSmrg *(dataptr + 1) = *dataptr + strlen(*line) + 1; 400a966c04fSmrg dataptr++; 401a966c04fSmrg } 402a966c04fSmrg } 403a966c04fSmrg strcpy(*dataptr, "XPMENDEXT"); 404a966c04fSmrg} 405