comparison gtk/dw.c @ 2201:ae6626a4331f

GTK: Same change from tmpnam() to mkstemp() for GTK2 plus extra safety checks on both GTK2 and GTK3.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Tue, 27 Oct 2020 00:52:13 +0000
parents 8ee74cf778cb
children b86ae2abb3b3
comparison
equal deleted inserted replaced
2200:319eeecb411e 2201:ae6626a4331f
4952 #else 4952 #else
4953 GdkBitmap *bitmap = NULL; 4953 GdkBitmap *bitmap = NULL;
4954 GdkPixmap *tmp = NULL; 4954 GdkPixmap *tmp = NULL;
4955 #endif 4955 #endif
4956 int _locked_by_me = FALSE; 4956 int _locked_by_me = FALSE;
4957 char *file; 4957
4958 FILE *fp; 4958 if(!id && !data)
4959
4960 if (!id && !data)
4961 return; 4959 return;
4962 4960
4963 DW_MUTEX_LOCK; 4961 DW_MUTEX_LOCK;
4964 if(data) 4962 if(data)
4965 { 4963 {
4968 #endif 4966 #endif
4969 /* 4967 /*
4970 * A real hack; create a temporary file and write the contents 4968 * A real hack; create a temporary file and write the contents
4971 * of the data to the file 4969 * of the data to the file
4972 */ 4970 */
4973 file = tmpnam( NULL ); 4971 char template[] = "/tmp/dwpixmapXXXXXX";
4974 fp = fopen( file, "wb" ); 4972 int written = -1, fd = mkstemp(template);
4975 if ( fp ) 4973
4976 { 4974 if(fd != -1)
4977 fwrite( data, len, 1, fp ); 4975 {
4978 fclose( fp ); 4976 written = write(fd, data, len);
4979 } 4977 close(fd);
4980 else 4978 }
4979 /* Bail if we couldn't write full file */
4980 if(fd == -1 || written != len)
4981 { 4981 {
4982 DW_MUTEX_UNLOCK; 4982 DW_MUTEX_UNLOCK;
4983 return; 4983 return;
4984 } 4984 }
4985 #if GTK_MAJOR_VERSION > 1 4985 #if GTK_MAJOR_VERSION > 1
4986 pixbuf = gdk_pixbuf_new_from_file(file, NULL ); 4986 pixbuf = gdk_pixbuf_new_from_file(template, NULL);
4987 #elif defined(USE_IMLIB) 4987 #elif defined(USE_IMLIB)
4988 image = gdk_imlib_load_image(file); 4988 image = gdk_imlib_load_image(template);
4989 gdk_imlib_render(image, image->rgb_width, image->rgb_height); 4989 gdk_imlib_render(image, image->rgb_width, image->rgb_height);
4990 tmp = gdk_imlib_copy_image(image); 4990 tmp = gdk_imlib_copy_image(image);
4991 bitmap = gdk_imlib_copy_mask(image); 4991 bitmap = gdk_imlib_copy_mask(image);
4992 gdk_imlib_destroy_image(image); 4992 gdk_imlib_destroy_image(image);
4993 #else 4993 #else
4994 tmp = gdk_pixmap_create_from_xpm_d(handle->window, &bitmap, &_colors[DW_CLR_PALEGRAY], mydata); 4994 tmp = gdk_pixmap_create_from_xpm_d(handle->window, &bitmap, &_colors[DW_CLR_PALEGRAY], mydata);
4995 #endif 4995 #endif
4996 /* remove our temporary file */ 4996 /* remove our temporary file */
4997 unlink (file ); 4997 unlink(template);
4998 } 4998 }
4999 else if (id) 4999 else if (id)
5000 #if GTK_MAJOR_VERSION > 1 5000 #if GTK_MAJOR_VERSION > 1
5001 pixbuf = _find_pixbuf((HICN)id); 5001 pixbuf = _find_pixbuf((HICN)id);
5002 #else 5002 #else
7136 * data: Source of data for image. 7136 * data: Source of data for image.
7137 * len: length of data 7137 * len: length of data
7138 */ 7138 */
7139 HICN API dw_icon_load_from_data(const char *data, int len) 7139 HICN API dw_icon_load_from_data(const char *data, int len)
7140 { 7140 {
7141 int found = -1, _locked_by_me = FALSE; 7141 int fd, written = -1, found = -1, _locked_by_me = FALSE;
7142 char *file; 7142 char template[] = "/tmp/dwiconXXXXXX";
7143 FILE *fp;
7144 #if GTK_MAJOR_VERSION > 1 7143 #if GTK_MAJOR_VERSION > 1
7145 GdkPixbuf *pixbuf; 7144 GdkPixbuf *pixbuf;
7146 #elif defined(USE_IMLIB) 7145 #elif defined(USE_IMLIB)
7147 GdkImlibImage *image; 7146 GdkImlibImage *image;
7148 #endif 7147 #endif
7151 DW_MUTEX_LOCK; 7150 DW_MUTEX_LOCK;
7152 /* 7151 /*
7153 * A real hack; create a temporary file and write the contents 7152 * A real hack; create a temporary file and write the contents
7154 * of the data to the file 7153 * of the data to the file
7155 */ 7154 */
7156 file = tmpnam( NULL ); 7155 if((fd = mkstemp(template)) != -1)
7157 fp = fopen( file, "wb" ); 7156 {
7158 if ( fp ) 7157 written = write(fd, data, len);
7159 { 7158 close(fd);
7160 fwrite( data, len, 1, fp ); 7159 }
7161 fclose( fp ); 7160 /* Bail if we couldn't write full file */
7162 } 7161 if(fd == -1 || written != len)
7163 else
7164 {
7165 DW_MUTEX_UNLOCK;
7166 return 0; 7162 return 0;
7167 } 7163
7168 /* Find a free entry in the array */ 7164 /* Find a free entry in the array */
7169 for (z=0;z<_PixmapCount;z++) 7165 for(z=0;z<_PixmapCount;z++)
7170 { 7166 {
7171 if(!_PixmapArray[z].used) 7167 if(!_PixmapArray[z].used)
7172 { 7168 {
7173 ret = found = z; 7169 ret = found = z;
7174 break; 7170 break;
7176 } 7172 }
7177 7173
7178 /* If there are no free entries, expand the 7174 /* If there are no free entries, expand the
7179 * array. 7175 * array.
7180 */ 7176 */
7181 if (found == -1) 7177 if(found == -1)
7182 { 7178 {
7183 DWPrivatePixmap *old = _PixmapArray; 7179 DWPrivatePixmap *old = _PixmapArray;
7184 7180
7185 ret = found = _PixmapCount; 7181 ret = found = _PixmapCount;
7186 _PixmapCount++; 7182 _PixmapCount++;
7194 _PixmapArray[found].used = 1; 7190 _PixmapArray[found].used = 1;
7195 _PixmapArray[found].pixmap = _PixmapArray[found].mask = NULL; 7191 _PixmapArray[found].pixmap = _PixmapArray[found].mask = NULL;
7196 } 7192 }
7197 7193
7198 #if GTK_MAJOR_VERSION > 1 7194 #if GTK_MAJOR_VERSION > 1
7199 pixbuf = _icon_resize(gdk_pixbuf_new_from_file(file, NULL)); 7195 pixbuf = _icon_resize(gdk_pixbuf_new_from_file(template, NULL));
7200 if (pixbuf) 7196
7197 if(pixbuf)
7201 { 7198 {
7202 _PixmapArray[found].pixbuf = pixbuf; 7199 _PixmapArray[found].pixbuf = pixbuf;
7203 _PixmapArray[found].width = gdk_pixbuf_get_width(pixbuf); 7200 _PixmapArray[found].width = gdk_pixbuf_get_width(pixbuf);
7204 _PixmapArray[found].height = gdk_pixbuf_get_height(pixbuf); 7201 _PixmapArray[found].height = gdk_pixbuf_get_height(pixbuf);
7205 7202
7206 gdk_pixbuf_render_pixmap_and_mask(pixbuf, &_PixmapArray[found].pixmap, &_PixmapArray[found].mask, 1); 7203 gdk_pixbuf_render_pixmap_and_mask(pixbuf, &_PixmapArray[found].pixmap, &_PixmapArray[found].mask, 1);
7207 } 7204 }
7208 #elif defined(USE_IMLIB) 7205 #elif defined(USE_IMLIB)
7209 image = gdk_imlib_load_image(file); 7206 image = gdk_imlib_load_image(template);
7210 7207
7211 if (image) 7208 if(image)
7212 { 7209 {
7213 _PixmapArray[found].width = image->rgb_width; 7210 _PixmapArray[found].width = image->rgb_width;
7214 _PixmapArray[found].height = image->rgb_height; 7211 _PixmapArray[found].height = image->rgb_height;
7215 7212
7216 gdk_imlib_render(image, image->rgb_width, image->rgb_height); 7213 gdk_imlib_render(image, image->rgb_width, image->rgb_height);
7217 _PixmapArray[found].pixmap = gdk_imlib_copy_image(image); 7214 _PixmapArray[found].pixmap = gdk_imlib_copy_image(image);
7218 _PixmapArray[found].mask = gdk_imlib_copy_mask(image); 7215 _PixmapArray[found].mask = gdk_imlib_copy_mask(image);
7219 gdk_imlib_destroy_image(image); 7216 gdk_imlib_destroy_image(image);
7220 } 7217 }
7221 #else 7218 #else
7222 if (last_window) 7219 if(last_window)
7223 _PixmapArray[found].pixmap = gdk_pixmap_create_from_xpm_d(last_window->window, &_PixmapArray[found].mask, &_colors[DW_CLR_PALEGRAY], data); 7220 _PixmapArray[found].pixmap = gdk_pixmap_create_from_xpm_d(last_window->window, &_PixmapArray[found].mask, &_colors[DW_CLR_PALEGRAY], data);
7224 #endif 7221 #endif
7225 /* remove our temporary file */ 7222 /* remove our temporary file */
7226 unlink (file ); 7223 unlink(template);
7227 DW_MUTEX_UNLOCK; 7224 DW_MUTEX_UNLOCK;
7228 if (!_PixmapArray[found].pixmap || !_PixmapArray[found].mask) 7225 if(!_PixmapArray[found].pixmap || !_PixmapArray[found].mask)
7229 { 7226 {
7230 _PixmapArray[found].used = 0; 7227 _PixmapArray[found].used = 0;
7231 _PixmapArray[found].pixmap = _PixmapArray[found].mask = NULL; 7228 _PixmapArray[found].pixmap = _PixmapArray[found].mask = NULL;
7232 return 0; 7229 return 0;
7233 } 7230 }
9063 * Returns: 9060 * Returns:
9064 * A handle to a pixmap or NULL on failure. 9061 * A handle to a pixmap or NULL on failure.
9065 */ 9062 */
9066 HPIXMAP dw_pixmap_new_from_data(HWND handle, const char *data, int len) 9063 HPIXMAP dw_pixmap_new_from_data(HWND handle, const char *data, int len)
9067 { 9064 {
9068 int _locked_by_me = FALSE; 9065 int fd, written = -1, _locked_by_me = FALSE;
9069 char *file;
9070 FILE *fp;
9071 HPIXMAP pixmap; 9066 HPIXMAP pixmap;
9072 #if GTK_MAJOR_VERSION > 1 9067 #if GTK_MAJOR_VERSION > 1
9073 GdkPixbuf *pixbuf; 9068 GdkPixbuf *pixbuf;
9074 #elif defined(USE_IMLIB) 9069 #elif defined(USE_IMLIB)
9075 GdkImlibImage *image; 9070 GdkImlibImage *image;
9076 #endif 9071 #endif
9077 9072 char template[] = "/tmp/dwpixmapXXXXXX";
9078 if (!data || !(pixmap = calloc(1,sizeof(struct _hpixmap)))) 9073
9074 if(!data || !(pixmap = calloc(1,sizeof(struct _hpixmap))))
9079 return NULL; 9075 return NULL;
9080 9076
9081 DW_MUTEX_LOCK; 9077 DW_MUTEX_LOCK;
9082 /* 9078 /*
9083 * A real hack; create a temporary file and write the contents 9079 * A real hack; create a temporary file and write the contents
9084 * of the data to the file 9080 * of the data to the file
9085 */ 9081 */
9086 file = tmpnam( NULL ); 9082 if((fd = mkstemp(template)) != -1)
9087 fp = fopen( file, "wb" ); 9083 {
9088 if ( fp ) 9084 written = write(fd, data, len);
9089 { 9085 close(fd);
9090 fwrite( data, len, 1, fp ); 9086 }
9091 fclose( fp ); 9087 /* Bail if we couldn't write full file */
9092 } 9088 if(fd == -1 || written != len)
9093 else
9094 { 9089 {
9095 DW_MUTEX_UNLOCK; 9090 DW_MUTEX_UNLOCK;
9096 return 0; 9091 return 0;
9097 } 9092 }
9098 #if GTK_MAJOR_VERSION > 1 9093 #if GTK_MAJOR_VERSION > 1
9099 pixbuf = gdk_pixbuf_new_from_file(file, NULL); 9094 pixbuf = gdk_pixbuf_new_from_file(template, NULL);
9100 pixmap->width = gdk_pixbuf_get_width(pixbuf); 9095 pixmap->width = gdk_pixbuf_get_width(pixbuf);
9101 pixmap->height = gdk_pixbuf_get_height(pixbuf); 9096 pixmap->height = gdk_pixbuf_get_height(pixbuf);
9102 gdk_pixbuf_render_pixmap_and_mask(pixbuf, &pixmap->pixmap, &pixmap->bitmap, 1); 9097 gdk_pixbuf_render_pixmap_and_mask(pixbuf, &pixmap->pixmap, &pixmap->bitmap, 1);
9103 g_object_unref(pixbuf); 9098 g_object_unref(pixbuf);
9104 #elif defined(USE_IMLIB) 9099 #elif defined(USE_IMLIB)
9105 image = gdk_imlib_load_image(file); 9100 image = gdk_imlib_load_image(template);
9106 9101
9107 pixmap->width = image->rgb_width; 9102 pixmap->width = image->rgb_width;
9108 pixmap->height = image->rgb_height; 9103 pixmap->height = image->rgb_height;
9109 9104
9110 gdk_imlib_render(image, pixmap->width, pixmap->height); 9105 gdk_imlib_render(image, pixmap->width, pixmap->height);
9112 gdk_imlib_destroy_image(image); 9107 gdk_imlib_destroy_image(image);
9113 #else 9108 #else
9114 pixmap->pixmap = gdk_pixmap_create_from_xpm_d(handle->window, &pixmap->bitmap, &_colors[DW_CLR_PALEGRAY], data); 9109 pixmap->pixmap = gdk_pixmap_create_from_xpm_d(handle->window, &pixmap->bitmap, &_colors[DW_CLR_PALEGRAY], data);
9115 #endif 9110 #endif
9116 /* remove our temporary file */ 9111 /* remove our temporary file */
9117 unlink (file ); 9112 unlink(template);
9118 pixmap->handle = handle; 9113 pixmap->handle = handle;
9119 DW_MUTEX_UNLOCK; 9114 DW_MUTEX_UNLOCK;
9120 return pixmap; 9115 return pixmap;
9121 } 9116 }
9122 9117