]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/io-xpm.c
Check the number of scanned items. (#168906, Morten Welinder)
[~andy/gtk] / gdk-pixbuf / io-xpm.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* GdkPixbuf library - XPM image loader
3  *
4  * Copyright (C) 1999 Mark Crichton
5  * Copyright (C) 1999 The Free Software Foundation
6  *
7  * Authors: Mark Crichton <crichton@gimp.org>
8  *          Federico Mena-Quintero <federico@gimp.org>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */
25
26 #include <config.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <glib.h>
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h> /* for unlink */
33 #endif
34 #include <errno.h>
35 #include "gdk-pixbuf-private.h"
36 #include "gdk-pixbuf-io.h"
37 #include <glib/gstdio.h>
38 \f
39
40 /* I have must have done something to deserve this.
41  * XPM is such a crappy format to handle.
42  * This code is an ugly hybred from gdkpixmap.c
43  * modified to respect transparent colors.
44  * It's still a mess, though.
45  */
46
47 enum buf_op {
48         op_header,
49         op_cmap,
50         op_body
51 };
52
53 typedef struct {
54         gchar *color_string;
55         guint16 red;
56         guint16 green;
57         guint16 blue;
58         gint transparent;
59 } XPMColor;
60
61 struct file_handle {
62         FILE *infile;
63         gchar *buffer;
64         guint buffer_size;
65 };
66
67 struct mem_handle {
68         const gchar **data;
69         int offset;
70 };
71
72 /* The following 2 routines (parse_color, find_color) come from Tk, via the Win32
73  * port of GDK. The licensing terms on these (longer than the functions) is:
74  *
75  * This software is copyrighted by the Regents of the University of
76  * California, Sun Microsystems, Inc., and other parties.  The following
77  * terms apply to all files associated with the software unless explicitly
78  * disclaimed in individual files.
79  * 
80  * The authors hereby grant permission to use, copy, modify, distribute,
81  * and license this software and its documentation for any purpose, provided
82  * that existing copyright notices are retained in all copies and that this
83  * notice is included verbatim in any distributions. No written agreement,
84  * license, or royalty fee is required for any of the authorized uses.
85  * Modifications to this software may be copyrighted by their authors
86  * and need not follow the licensing terms described here, provided that
87  * the new terms are clearly indicated on the first page of each file where
88  * they apply.
89  * 
90  * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY
91  * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
92  * ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
93  * DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE
94  * POSSIBILITY OF SUCH DAMAGE.
95  * 
96  * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
97  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
98  * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE
99  * IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE
100  * NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
101  * MODIFICATIONS.
102  * 
103  * GOVERNMENT USE: If you are acquiring this software on behalf of the
104  * U.S. government, the Government shall have only "Restricted Rights"
105  * in the software and related documentation as defined in the Federal 
106  * Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2).  If you
107  * are acquiring the software on behalf of the Department of Defense, the
108  * software shall be classified as "Commercial Computer Software" and the
109  * Government shall have only "Restricted Rights" as defined in Clause
110  * 252.227-7013 (c) (1) of DFARs.  Notwithstanding the foregoing, the
111  * authors grant the U.S. Government and others acting in its behalf
112  * permission to use and distribute the software in accordance with the
113  * terms specified in this license.
114  */
115
116 typedef struct {
117     const char *name;
118     unsigned char red;
119     unsigned char green;
120     unsigned char blue;
121 } XPMColorEntry;
122
123 static XPMColorEntry xColors[] = {
124     { "alice blue", 240, 248, 255 },
125     { "AliceBlue", 240, 248, 255 },
126     { "antique white", 250, 235, 215 },
127     { "AntiqueWhite", 250, 235, 215 },
128     { "AntiqueWhite1", 255, 239, 219 },
129     { "AntiqueWhite2", 238, 223, 204 },
130     { "AntiqueWhite3", 205, 192, 176 },
131     { "AntiqueWhite4", 139, 131, 120 },
132     { "aquamarine", 127, 255, 212 },
133     { "aquamarine1", 127, 255, 212 },
134     { "aquamarine2", 118, 238, 198 },
135     { "aquamarine3", 102, 205, 170 },
136     { "aquamarine4", 69, 139, 116 },
137     { "azure", 240, 255, 255 },
138     { "azure1", 240, 255, 255 },
139     { "azure2", 224, 238, 238 },
140     { "azure3", 193, 205, 205 },
141     { "azure4", 131, 139, 139 },
142     { "beige", 245, 245, 220 },
143     { "bisque", 255, 228, 196 },
144     { "bisque1", 255, 228, 196 },
145     { "bisque2", 238, 213, 183 },
146     { "bisque3", 205, 183, 158 },
147     { "bisque4", 139, 125, 107 },
148     { "black", 0, 0, 0 },
149     { "blanched almond", 255, 235, 205 },
150     { "BlanchedAlmond", 255, 235, 205 },
151     { "blue", 0, 0, 255 },
152     { "blue violet", 138, 43, 226 },
153     { "blue1", 0, 0, 255 },
154     { "blue2", 0, 0, 238 },
155     { "blue3", 0, 0, 205 },
156     { "blue4", 0, 0, 139 },
157     { "BlueViolet", 138, 43, 226 },
158     { "brown", 165, 42, 42 },
159     { "brown1", 255, 64, 64 },
160     { "brown2", 238, 59, 59 },
161     { "brown3", 205, 51, 51 },
162     { "brown4", 139, 35, 35 },
163     { "burlywood", 222, 184, 135 },
164     { "burlywood1", 255, 211, 155 },
165     { "burlywood2", 238, 197, 145 },
166     { "burlywood3", 205, 170, 125 },
167     { "burlywood4", 139, 115, 85 },
168     { "cadet blue", 95, 158, 160 },
169     { "CadetBlue", 95, 158, 160 },
170     { "CadetBlue1", 152, 245, 255 },
171     { "CadetBlue2", 142, 229, 238 },
172     { "CadetBlue3", 122, 197, 205 },
173     { "CadetBlue4", 83, 134, 139 },
174     { "chartreuse", 127, 255, 0 },
175     { "chartreuse1", 127, 255, 0 },
176     { "chartreuse2", 118, 238, 0 },
177     { "chartreuse3", 102, 205, 0 },
178     { "chartreuse4", 69, 139, 0 },
179     { "chocolate", 210, 105, 30 },
180     { "chocolate1", 255, 127, 36 },
181     { "chocolate2", 238, 118, 33 },
182     { "chocolate3", 205, 102, 29 },
183     { "chocolate4", 139, 69, 19 },
184     { "coral", 255, 127, 80 },
185     { "coral1", 255, 114, 86 },
186     { "coral2", 238, 106, 80 },
187     { "coral3", 205, 91, 69 },
188     { "coral4", 139, 62, 47 },
189     { "cornflower blue", 100, 149, 237 },
190     { "CornflowerBlue", 100, 149, 237 },
191     { "cornsilk", 255, 248, 220 },
192     { "cornsilk1", 255, 248, 220 },
193     { "cornsilk2", 238, 232, 205 },
194     { "cornsilk3", 205, 200, 177 },
195     { "cornsilk4", 139, 136, 120 },
196     { "cyan", 0, 255, 255 },
197     { "cyan1", 0, 255, 255 },
198     { "cyan2", 0, 238, 238 },
199     { "cyan3", 0, 205, 205 },
200     { "cyan4", 0, 139, 139 },
201     { "dark blue", 0, 0, 139 },
202     { "dark cyan", 0, 139, 139 },
203     { "dark goldenrod", 184, 134, 11 },
204     { "dark gray", 169, 169, 169 },
205     { "dark green", 0, 100, 0 },
206     { "dark grey", 169, 169, 169 },
207     { "dark khaki", 189, 183, 107 },
208     { "dark magenta", 139, 0, 139 },
209     { "dark olive green", 85, 107, 47 },
210     { "dark orange", 255, 140, 0 },
211     { "dark orchid", 153, 50, 204 },
212     { "dark red", 139, 0, 0 },
213     { "dark salmon", 233, 150, 122 },
214     { "dark sea green", 143, 188, 143 },
215     { "dark slate blue", 72, 61, 139 },
216     { "dark slate gray", 47, 79, 79 },
217     { "dark slate grey", 47, 79, 79 },
218     { "dark turquoise", 0, 206, 209 },
219     { "dark violet", 148, 0, 211 },
220     { "DarkBlue", 0, 0, 139 },
221     { "DarkCyan", 0, 139, 139 },
222     { "DarkGoldenrod", 184, 134, 11 },
223     { "DarkGoldenrod1", 255, 185, 15 },
224     { "DarkGoldenrod2", 238, 173, 14 },
225     { "DarkGoldenrod3", 205, 149, 12 },
226     { "DarkGoldenrod4", 139, 101, 8 },
227     { "DarkGray", 169, 169, 169 },
228     { "DarkGreen", 0, 100, 0 },
229     { "DarkGrey", 169, 169, 169 },
230     { "DarkKhaki", 189, 183, 107 },
231     { "DarkMagenta", 139, 0, 139 },
232     { "DarkOliveGreen", 85, 107, 47 },
233     { "DarkOliveGreen1", 202, 255, 112 },
234     { "DarkOliveGreen2", 188, 238, 104 },
235     { "DarkOliveGreen3", 162, 205, 90 },
236     { "DarkOliveGreen4", 110, 139, 61 },
237     { "DarkOrange", 255, 140, 0 },
238     { "DarkOrange1", 255, 127, 0 },
239     { "DarkOrange2", 238, 118, 0 },
240     { "DarkOrange3", 205, 102, 0 },
241     { "DarkOrange4", 139, 69, 0 },
242     { "DarkOrchid", 153, 50, 204 },
243     { "DarkOrchid1", 191, 62, 255 },
244     { "DarkOrchid2", 178, 58, 238 },
245     { "DarkOrchid3", 154, 50, 205 },
246     { "DarkOrchid4", 104, 34, 139 },
247     { "DarkRed", 139, 0, 0 },
248     { "DarkSalmon", 233, 150, 122 },
249     { "DarkSeaGreen", 143, 188, 143 },
250     { "DarkSeaGreen1", 193, 255, 193 },
251     { "DarkSeaGreen2", 180, 238, 180 },
252     { "DarkSeaGreen3", 155, 205, 155 },
253     { "DarkSeaGreen4", 105, 139, 105 },
254     { "DarkSlateBlue", 72, 61, 139 },
255     { "DarkSlateGray", 47, 79, 79 },
256     { "DarkSlateGray1", 151, 255, 255 },
257     { "DarkSlateGray2", 141, 238, 238 },
258     { "DarkSlateGray3", 121, 205, 205 },
259     { "DarkSlateGray4", 82, 139, 139 },
260     { "DarkSlateGrey", 47, 79, 79 },
261     { "DarkTurquoise", 0, 206, 209 },
262     { "DarkViolet", 148, 0, 211 },
263     { "deep pink", 255, 20, 147 },
264     { "deep sky blue", 0, 191, 255 },
265     { "DeepPink", 255, 20, 147 },
266     { "DeepPink1", 255, 20, 147 },
267     { "DeepPink2", 238, 18, 137 },
268     { "DeepPink3", 205, 16, 118 },
269     { "DeepPink4", 139, 10, 80 },
270     { "DeepSkyBlue", 0, 191, 255 },
271     { "DeepSkyBlue1", 0, 191, 255 },
272     { "DeepSkyBlue2", 0, 178, 238 },
273     { "DeepSkyBlue3", 0, 154, 205 },
274     { "DeepSkyBlue4", 0, 104, 139 },
275     { "dim gray", 105, 105, 105 },
276     { "dim grey", 105, 105, 105 },
277     { "DimGray", 105, 105, 105 },
278     { "DimGrey", 105, 105, 105 },
279     { "dodger blue", 30, 144, 255 },
280     { "DodgerBlue", 30, 144, 255 },
281     { "DodgerBlue1", 30, 144, 255 },
282     { "DodgerBlue2", 28, 134, 238 },
283     { "DodgerBlue3", 24, 116, 205 },
284     { "DodgerBlue4", 16, 78, 139 },
285     { "firebrick", 178, 34, 34 },
286     { "firebrick1", 255, 48, 48 },
287     { "firebrick2", 238, 44, 44 },
288     { "firebrick3", 205, 38, 38 },
289     { "firebrick4", 139, 26, 26 },
290     { "floral white", 255, 250, 240 },
291     { "FloralWhite", 255, 250, 240 },
292     { "forest green", 34, 139, 34 },
293     { "ForestGreen", 34, 139, 34 },
294     { "gainsboro", 220, 220, 220 },
295     { "ghost white", 248, 248, 255 },
296     { "GhostWhite", 248, 248, 255 },
297     { "gold", 255, 215, 0 },
298     { "gold1", 255, 215, 0 },
299     { "gold2", 238, 201, 0 },
300     { "gold3", 205, 173, 0 },
301     { "gold4", 139, 117, 0 },
302     { "goldenrod", 218, 165, 32 },
303     { "goldenrod1", 255, 193, 37 },
304     { "goldenrod2", 238, 180, 34 },
305     { "goldenrod3", 205, 155, 29 },
306     { "goldenrod4", 139, 105, 20 },
307     { "gray", 190, 190, 190 },
308     { "gray0", 0, 0, 0 },
309     { "gray1", 3, 3, 3 },
310     { "gray10", 26, 26, 26 },
311     { "gray100", 255, 255, 255 },
312     { "gray11", 28, 28, 28 },
313     { "gray12", 31, 31, 31 },
314     { "gray13", 33, 33, 33 },
315     { "gray14", 36, 36, 36 },
316     { "gray15", 38, 38, 38 },
317     { "gray16", 41, 41, 41 },
318     { "gray17", 43, 43, 43 },
319     { "gray18", 46, 46, 46 },
320     { "gray19", 48, 48, 48 },
321     { "gray2", 5, 5, 5 },
322     { "gray20", 51, 51, 51 },
323     { "gray21", 54, 54, 54 },
324     { "gray22", 56, 56, 56 },
325     { "gray23", 59, 59, 59 },
326     { "gray24", 61, 61, 61 },
327     { "gray25", 64, 64, 64 },
328     { "gray26", 66, 66, 66 },
329     { "gray27", 69, 69, 69 },
330     { "gray28", 71, 71, 71 },
331     { "gray29", 74, 74, 74 },
332     { "gray3", 8, 8, 8 },
333     { "gray30", 77, 77, 77 },
334     { "gray31", 79, 79, 79 },
335     { "gray32", 82, 82, 82 },
336     { "gray33", 84, 84, 84 },
337     { "gray34", 87, 87, 87 },
338     { "gray35", 89, 89, 89 },
339     { "gray36", 92, 92, 92 },
340     { "gray37", 94, 94, 94 },
341     { "gray38", 97, 97, 97 },
342     { "gray39", 99, 99, 99 },
343     { "gray4", 10, 10, 10 },
344     { "gray40", 102, 102, 102 },
345     { "gray41", 105, 105, 105 },
346     { "gray42", 107, 107, 107 },
347     { "gray43", 110, 110, 110 },
348     { "gray44", 112, 112, 112 },
349     { "gray45", 115, 115, 115 },
350     { "gray46", 117, 117, 117 },
351     { "gray47", 120, 120, 120 },
352     { "gray48", 122, 122, 122 },
353     { "gray49", 125, 125, 125 },
354     { "gray5", 13, 13, 13 },
355     { "gray50", 127, 127, 127 },
356     { "gray51", 130, 130, 130 },
357     { "gray52", 133, 133, 133 },
358     { "gray53", 135, 135, 135 },
359     { "gray54", 138, 138, 138 },
360     { "gray55", 140, 140, 140 },
361     { "gray56", 143, 143, 143 },
362     { "gray57", 145, 145, 145 },
363     { "gray58", 148, 148, 148 },
364     { "gray59", 150, 150, 150 },
365     { "gray6", 15, 15, 15 },
366     { "gray60", 153, 153, 153 },
367     { "gray61", 156, 156, 156 },
368     { "gray62", 158, 158, 158 },
369     { "gray63", 161, 161, 161 },
370     { "gray64", 163, 163, 163 },
371     { "gray65", 166, 166, 166 },
372     { "gray66", 168, 168, 168 },
373     { "gray67", 171, 171, 171 },
374     { "gray68", 173, 173, 173 },
375     { "gray69", 176, 176, 176 },
376     { "gray7", 18, 18, 18 },
377     { "gray70", 179, 179, 179 },
378     { "gray71", 181, 181, 181 },
379     { "gray72", 184, 184, 184 },
380     { "gray73", 186, 186, 186 },
381     { "gray74", 189, 189, 189 },
382     { "gray75", 191, 191, 191 },
383     { "gray76", 194, 194, 194 },
384     { "gray77", 196, 196, 196 },
385     { "gray78", 199, 199, 199 },
386     { "gray79", 201, 201, 201 },
387     { "gray8", 20, 20, 20 },
388     { "gray80", 204, 204, 204 },
389     { "gray81", 207, 207, 207 },
390     { "gray82", 209, 209, 209 },
391     { "gray83", 212, 212, 212 },
392     { "gray84", 214, 214, 214 },
393     { "gray85", 217, 217, 217 },
394     { "gray86", 219, 219, 219 },
395     { "gray87", 222, 222, 222 },
396     { "gray88", 224, 224, 224 },
397     { "gray89", 227, 227, 227 },
398     { "gray9", 23, 23, 23 },
399     { "gray90", 229, 229, 229 },
400     { "gray91", 232, 232, 232 },
401     { "gray92", 235, 235, 235 },
402     { "gray93", 237, 237, 237 },
403     { "gray94", 240, 240, 240 },
404     { "gray95", 242, 242, 242 },
405     { "gray96", 245, 245, 245 },
406     { "gray97", 247, 247, 247 },
407     { "gray98", 250, 250, 250 },
408     { "gray99", 252, 252, 252 },
409     { "green", 0, 255, 0 },
410     { "green yellow", 173, 255, 47 },
411     { "green1", 0, 255, 0 },
412     { "green2", 0, 238, 0 },
413     { "green3", 0, 205, 0 },
414     { "green4", 0, 139, 0 },
415     { "GreenYellow", 173, 255, 47 },
416     { "grey", 190, 190, 190 },
417     { "grey0", 0, 0, 0 },
418     { "grey1", 3, 3, 3 },
419     { "grey10", 26, 26, 26 },
420     { "grey100", 255, 255, 255 },
421     { "grey11", 28, 28, 28 },
422     { "grey12", 31, 31, 31 },
423     { "grey13", 33, 33, 33 },
424     { "grey14", 36, 36, 36 },
425     { "grey15", 38, 38, 38 },
426     { "grey16", 41, 41, 41 },
427     { "grey17", 43, 43, 43 },
428     { "grey18", 46, 46, 46 },
429     { "grey19", 48, 48, 48 },
430     { "grey2", 5, 5, 5 },
431     { "grey20", 51, 51, 51 },
432     { "grey21", 54, 54, 54 },
433     { "grey22", 56, 56, 56 },
434     { "grey23", 59, 59, 59 },
435     { "grey24", 61, 61, 61 },
436     { "grey25", 64, 64, 64 },
437     { "grey26", 66, 66, 66 },
438     { "grey27", 69, 69, 69 },
439     { "grey28", 71, 71, 71 },
440     { "grey29", 74, 74, 74 },
441     { "grey3", 8, 8, 8 },
442     { "grey30", 77, 77, 77 },
443     { "grey31", 79, 79, 79 },
444     { "grey32", 82, 82, 82 },
445     { "grey33", 84, 84, 84 },
446     { "grey34", 87, 87, 87 },
447     { "grey35", 89, 89, 89 },
448     { "grey36", 92, 92, 92 },
449     { "grey37", 94, 94, 94 },
450     { "grey38", 97, 97, 97 },
451     { "grey39", 99, 99, 99 },
452     { "grey4", 10, 10, 10 },
453     { "grey40", 102, 102, 102 },
454     { "grey41", 105, 105, 105 },
455     { "grey42", 107, 107, 107 },
456     { "grey43", 110, 110, 110 },
457     { "grey44", 112, 112, 112 },
458     { "grey45", 115, 115, 115 },
459     { "grey46", 117, 117, 117 },
460     { "grey47", 120, 120, 120 },
461     { "grey48", 122, 122, 122 },
462     { "grey49", 125, 125, 125 },
463     { "grey5", 13, 13, 13 },
464     { "grey50", 127, 127, 127 },
465     { "grey51", 130, 130, 130 },
466     { "grey52", 133, 133, 133 },
467     { "grey53", 135, 135, 135 },
468     { "grey54", 138, 138, 138 },
469     { "grey55", 140, 140, 140 },
470     { "grey56", 143, 143, 143 },
471     { "grey57", 145, 145, 145 },
472     { "grey58", 148, 148, 148 },
473     { "grey59", 150, 150, 150 },
474     { "grey6", 15, 15, 15 },
475     { "grey60", 153, 153, 153 },
476     { "grey61", 156, 156, 156 },
477     { "grey62", 158, 158, 158 },
478     { "grey63", 161, 161, 161 },
479     { "grey64", 163, 163, 163 },
480     { "grey65", 166, 166, 166 },
481     { "grey66", 168, 168, 168 },
482     { "grey67", 171, 171, 171 },
483     { "grey68", 173, 173, 173 },
484     { "grey69", 176, 176, 176 },
485     { "grey7", 18, 18, 18 },
486     { "grey70", 179, 179, 179 },
487     { "grey71", 181, 181, 181 },
488     { "grey72", 184, 184, 184 },
489     { "grey73", 186, 186, 186 },
490     { "grey74", 189, 189, 189 },
491     { "grey75", 191, 191, 191 },
492     { "grey76", 194, 194, 194 },
493     { "grey77", 196, 196, 196 },
494     { "grey78", 199, 199, 199 },
495     { "grey79", 201, 201, 201 },
496     { "grey8", 20, 20, 20 },
497     { "grey80", 204, 204, 204 },
498     { "grey81", 207, 207, 207 },
499     { "grey82", 209, 209, 209 },
500     { "grey83", 212, 212, 212 },
501     { "grey84", 214, 214, 214 },
502     { "grey85", 217, 217, 217 },
503     { "grey86", 219, 219, 219 },
504     { "grey87", 222, 222, 222 },
505     { "grey88", 224, 224, 224 },
506     { "grey89", 227, 227, 227 },
507     { "grey9", 23, 23, 23 },
508     { "grey90", 229, 229, 229 },
509     { "grey91", 232, 232, 232 },
510     { "grey92", 235, 235, 235 },
511     { "grey93", 237, 237, 237 },
512     { "grey94", 240, 240, 240 },
513     { "grey95", 242, 242, 242 },
514     { "grey96", 245, 245, 245 },
515     { "grey97", 247, 247, 247 },
516     { "grey98", 250, 250, 250 },
517     { "grey99", 252, 252, 252 },
518     { "honeydew", 240, 255, 240 },
519     { "honeydew1", 240, 255, 240 },
520     { "honeydew2", 224, 238, 224 },
521     { "honeydew3", 193, 205, 193 },
522     { "honeydew4", 131, 139, 131 },
523     { "hot pink", 255, 105, 180 },
524     { "HotPink", 255, 105, 180 },
525     { "HotPink1", 255, 110, 180 },
526     { "HotPink2", 238, 106, 167 },
527     { "HotPink3", 205, 96, 144 },
528     { "HotPink4", 139, 58, 98 },
529     { "indian red", 205, 92, 92 },
530     { "IndianRed", 205, 92, 92 },
531     { "IndianRed1", 255, 106, 106 },
532     { "IndianRed2", 238, 99, 99 },
533     { "IndianRed3", 205, 85, 85 },
534     { "IndianRed4", 139, 58, 58 },
535     { "ivory", 255, 255, 240 },
536     { "ivory1", 255, 255, 240 },
537     { "ivory2", 238, 238, 224 },
538     { "ivory3", 205, 205, 193 },
539     { "ivory4", 139, 139, 131 },
540     { "khaki", 240, 230, 140 },
541     { "khaki1", 255, 246, 143 },
542     { "khaki2", 238, 230, 133 },
543     { "khaki3", 205, 198, 115 },
544     { "khaki4", 139, 134, 78 },
545     { "lavender", 230, 230, 250 },
546     { "lavender blush", 255, 240, 245 },
547     { "LavenderBlush", 255, 240, 245 },
548     { "LavenderBlush1", 255, 240, 245 },
549     { "LavenderBlush2", 238, 224, 229 },
550     { "LavenderBlush3", 205, 193, 197 },
551     { "LavenderBlush4", 139, 131, 134 },
552     { "lawn green", 124, 252, 0 },
553     { "LawnGreen", 124, 252, 0 },
554     { "lemon chiffon", 255, 250, 205 },
555     { "LemonChiffon", 255, 250, 205 },
556     { "LemonChiffon1", 255, 250, 205 },
557     { "LemonChiffon2", 238, 233, 191 },
558     { "LemonChiffon3", 205, 201, 165 },
559     { "LemonChiffon4", 139, 137, 112 },
560     { "light blue", 173, 216, 230 },
561     { "light coral", 240, 128, 128 },
562     { "light cyan", 224, 255, 255 },
563     { "light goldenrod", 238, 221, 130 },
564     { "light goldenrod yellow", 250, 250, 210 },
565     { "light gray", 211, 211, 211 },
566     { "light green", 144, 238, 144 },
567     { "light grey", 211, 211, 211 },
568     { "light pink", 255, 182, 193 },
569     { "light salmon", 255, 160, 122 },
570     { "light sea green", 32, 178, 170 },
571     { "light sky blue", 135, 206, 250 },
572     { "light slate blue", 132, 112, 255 },
573     { "light slate gray", 119, 136, 153 },
574     { "light slate grey", 119, 136, 153 },
575     { "light steel blue", 176, 196, 222 },
576     { "light yellow", 255, 255, 224 },
577     { "LightBlue", 173, 216, 230 },
578     { "LightBlue1", 191, 239, 255 },
579     { "LightBlue2", 178, 223, 238 },
580     { "LightBlue3", 154, 192, 205 },
581     { "LightBlue4", 104, 131, 139 },
582     { "LightCoral", 240, 128, 128 },
583     { "LightCyan", 224, 255, 255 },
584     { "LightCyan1", 224, 255, 255 },
585     { "LightCyan2", 209, 238, 238 },
586     { "LightCyan3", 180, 205, 205 },
587     { "LightCyan4", 122, 139, 139 },
588     { "LightGoldenrod", 238, 221, 130 },
589     { "LightGoldenrod1", 255, 236, 139 },
590     { "LightGoldenrod2", 238, 220, 130 },
591     { "LightGoldenrod3", 205, 190, 112 },
592     { "LightGoldenrod4", 139, 129, 76 },
593     { "LightGoldenrodYellow", 250, 250, 210 },
594     { "LightGray", 211, 211, 211 },
595     { "LightGreen", 144, 238, 144 },
596     { "LightGrey", 211, 211, 211 },
597     { "LightPink", 255, 182, 193 },
598     { "LightPink1", 255, 174, 185 },
599     { "LightPink2", 238, 162, 173 },
600     { "LightPink3", 205, 140, 149 },
601     { "LightPink4", 139, 95, 101 },
602     { "LightSalmon", 255, 160, 122 },
603     { "LightSalmon1", 255, 160, 122 },
604     { "LightSalmon2", 238, 149, 114 },
605     { "LightSalmon3", 205, 129, 98 },
606     { "LightSalmon4", 139, 87, 66 },
607     { "LightSeaGreen", 32, 178, 170 },
608     { "LightSkyBlue", 135, 206, 250 },
609     { "LightSkyBlue1", 176, 226, 255 },
610     { "LightSkyBlue2", 164, 211, 238 },
611     { "LightSkyBlue3", 141, 182, 205 },
612     { "LightSkyBlue4", 96, 123, 139 },
613     { "LightSlateBlue", 132, 112, 255 },
614     { "LightSlateGray", 119, 136, 153 },
615     { "LightSlateGrey", 119, 136, 153 },
616     { "LightSteelBlue", 176, 196, 222 },
617     { "LightSteelBlue1", 202, 225, 255 },
618     { "LightSteelBlue2", 188, 210, 238 },
619     { "LightSteelBlue3", 162, 181, 205 },
620     { "LightSteelBlue4", 110, 123, 139 },
621     { "LightYellow", 255, 255, 224 },
622     { "LightYellow1", 255, 255, 224 },
623     { "LightYellow2", 238, 238, 209 },
624     { "LightYellow3", 205, 205, 180 },
625     { "LightYellow4", 139, 139, 122 },
626     { "lime green", 50, 205, 50 },
627     { "LimeGreen", 50, 205, 50 },
628     { "linen", 250, 240, 230 },
629     { "magenta", 255, 0, 255 },
630     { "magenta1", 255, 0, 255 },
631     { "magenta2", 238, 0, 238 },
632     { "magenta3", 205, 0, 205 },
633     { "magenta4", 139, 0, 139 },
634     { "maroon", 176, 48, 96 },
635     { "maroon1", 255, 52, 179 },
636     { "maroon2", 238, 48, 167 },
637     { "maroon3", 205, 41, 144 },
638     { "maroon4", 139, 28, 98 },
639     { "medium aquamarine", 102, 205, 170 },
640     { "medium blue", 0, 0, 205 },
641     { "medium orchid", 186, 85, 211 },
642     { "medium purple", 147, 112, 219 },
643     { "medium sea green", 60, 179, 113 },
644     { "medium slate blue", 123, 104, 238 },
645     { "medium spring green", 0, 250, 154 },
646     { "medium turquoise", 72, 209, 204 },
647     { "medium violet red", 199, 21, 133 },
648     { "MediumAquamarine", 102, 205, 170 },
649     { "MediumBlue", 0, 0, 205 },
650     { "MediumOrchid", 186, 85, 211 },
651     { "MediumOrchid1", 224, 102, 255 },
652     { "MediumOrchid2", 209, 95, 238 },
653     { "MediumOrchid3", 180, 82, 205 },
654     { "MediumOrchid4", 122, 55, 139 },
655     { "MediumPurple", 147, 112, 219 },
656     { "MediumPurple1", 171, 130, 255 },
657     { "MediumPurple2", 159, 121, 238 },
658     { "MediumPurple3", 137, 104, 205 },
659     { "MediumPurple4", 93, 71, 139 },
660     { "MediumSeaGreen", 60, 179, 113 },
661     { "MediumSlateBlue", 123, 104, 238 },
662     { "MediumSpringGreen", 0, 250, 154 },
663     { "MediumTurquoise", 72, 209, 204 },
664     { "MediumVioletRed", 199, 21, 133 },
665     { "midnight blue", 25, 25, 112 },
666     { "MidnightBlue", 25, 25, 112 },
667     { "mint cream", 245, 255, 250 },
668     { "MintCream", 245, 255, 250 },
669     { "misty rose", 255, 228, 225 },
670     { "MistyRose", 255, 228, 225 },
671     { "MistyRose1", 255, 228, 225 },
672     { "MistyRose2", 238, 213, 210 },
673     { "MistyRose3", 205, 183, 181 },
674     { "MistyRose4", 139, 125, 123 },
675     { "moccasin", 255, 228, 181 },
676     { "navajo white", 255, 222, 173 },
677     { "NavajoWhite", 255, 222, 173 },
678     { "NavajoWhite1", 255, 222, 173 },
679     { "NavajoWhite2", 238, 207, 161 },
680     { "NavajoWhite3", 205, 179, 139 },
681     { "NavajoWhite4", 139, 121, 94 },
682     { "navy", 0, 0, 128 },
683     { "navy blue", 0, 0, 128 },
684     { "NavyBlue", 0, 0, 128 },
685     { "old lace", 253, 245, 230 },
686     { "OldLace", 253, 245, 230 },
687     { "olive drab", 107, 142, 35 },
688     { "OliveDrab", 107, 142, 35 },
689     { "OliveDrab1", 192, 255, 62 },
690     { "OliveDrab2", 179, 238, 58 },
691     { "OliveDrab3", 154, 205, 50 },
692     { "OliveDrab4", 105, 139, 34 },
693     { "orange", 255, 165, 0 },
694     { "orange red", 255, 69, 0 },
695     { "orange1", 255, 165, 0 },
696     { "orange2", 238, 154, 0 },
697     { "orange3", 205, 133, 0 },
698     { "orange4", 139, 90, 0 },
699     { "OrangeRed", 255, 69, 0 },
700     { "OrangeRed1", 255, 69, 0 },
701     { "OrangeRed2", 238, 64, 0 },
702     { "OrangeRed3", 205, 55, 0 },
703     { "OrangeRed4", 139, 37, 0 },
704     { "orchid", 218, 112, 214 },
705     { "orchid1", 255, 131, 250 },
706     { "orchid2", 238, 122, 233 },
707     { "orchid3", 205, 105, 201 },
708     { "orchid4", 139, 71, 137 },
709     { "pale goldenrod", 238, 232, 170 },
710     { "pale green", 152, 251, 152 },
711     { "pale turquoise", 175, 238, 238 },
712     { "pale violet red", 219, 112, 147 },
713     { "PaleGoldenrod", 238, 232, 170 },
714     { "PaleGreen", 152, 251, 152 },
715     { "PaleGreen1", 154, 255, 154 },
716     { "PaleGreen2", 144, 238, 144 },
717     { "PaleGreen3", 124, 205, 124 },
718     { "PaleGreen4", 84, 139, 84 },
719     { "PaleTurquoise", 175, 238, 238 },
720     { "PaleTurquoise1", 187, 255, 255 },
721     { "PaleTurquoise2", 174, 238, 238 },
722     { "PaleTurquoise3", 150, 205, 205 },
723     { "PaleTurquoise4", 102, 139, 139 },
724     { "PaleVioletRed", 219, 112, 147 },
725     { "PaleVioletRed1", 255, 130, 171 },
726     { "PaleVioletRed2", 238, 121, 159 },
727     { "PaleVioletRed3", 205, 104, 137 },
728     { "PaleVioletRed4", 139, 71, 93 },
729     { "papaya whip", 255, 239, 213 },
730     { "PapayaWhip", 255, 239, 213 },
731     { "peach puff", 255, 218, 185 },
732     { "PeachPuff", 255, 218, 185 },
733     { "PeachPuff1", 255, 218, 185 },
734     { "PeachPuff2", 238, 203, 173 },
735     { "PeachPuff3", 205, 175, 149 },
736     { "PeachPuff4", 139, 119, 101 },
737     { "peru", 205, 133, 63 },
738     { "pink", 255, 192, 203 },
739     { "pink1", 255, 181, 197 },
740     { "pink2", 238, 169, 184 },
741     { "pink3", 205, 145, 158 },
742     { "pink4", 139, 99, 108 },
743     { "plum", 221, 160, 221 },
744     { "plum1", 255, 187, 255 },
745     { "plum2", 238, 174, 238 },
746     { "plum3", 205, 150, 205 },
747     { "plum4", 139, 102, 139 },
748     { "powder blue", 176, 224, 230 },
749     { "PowderBlue", 176, 224, 230 },
750     { "purple", 160, 32, 240 },
751     { "purple1", 155, 48, 255 },
752     { "purple2", 145, 44, 238 },
753     { "purple3", 125, 38, 205 },
754     { "purple4", 85, 26, 139 },
755     { "red", 255, 0, 0 },
756     { "red1", 255, 0, 0 },
757     { "red2", 238, 0, 0 },
758     { "red3", 205, 0, 0 },
759     { "red4", 139, 0, 0 },
760     { "rosy brown", 188, 143, 143 },
761     { "RosyBrown", 188, 143, 143 },
762     { "RosyBrown1", 255, 193, 193 },
763     { "RosyBrown2", 238, 180, 180 },
764     { "RosyBrown3", 205, 155, 155 },
765     { "RosyBrown4", 139, 105, 105 },
766     { "royal blue", 65, 105, 225 },
767     { "RoyalBlue", 65, 105, 225 },
768     { "RoyalBlue1", 72, 118, 255 },
769     { "RoyalBlue2", 67, 110, 238 },
770     { "RoyalBlue3", 58, 95, 205 },
771     { "RoyalBlue4", 39, 64, 139 },
772     { "saddle brown", 139, 69, 19 },
773     { "SaddleBrown", 139, 69, 19 },
774     { "salmon", 250, 128, 114 },
775     { "salmon1", 255, 140, 105 },
776     { "salmon2", 238, 130, 98 },
777     { "salmon3", 205, 112, 84 },
778     { "salmon4", 139, 76, 57 },
779     { "sandy brown", 244, 164, 96 },
780     { "SandyBrown", 244, 164, 96 },
781     { "sea green", 46, 139, 87 },
782     { "SeaGreen", 46, 139, 87 },
783     { "SeaGreen1", 84, 255, 159 },
784     { "SeaGreen2", 78, 238, 148 },
785     { "SeaGreen3", 67, 205, 128 },
786     { "SeaGreen4", 46, 139, 87 },
787     { "seashell", 255, 245, 238 },
788     { "seashell1", 255, 245, 238 },
789     { "seashell2", 238, 229, 222 },
790     { "seashell3", 205, 197, 191 },
791     { "seashell4", 139, 134, 130 },
792     { "sienna", 160, 82, 45 },
793     { "sienna1", 255, 130, 71 },
794     { "sienna2", 238, 121, 66 },
795     { "sienna3", 205, 104, 57 },
796     { "sienna4", 139, 71, 38 },
797     { "sky blue", 135, 206, 235 },
798     { "SkyBlue", 135, 206, 235 },
799     { "SkyBlue1", 135, 206, 255 },
800     { "SkyBlue2", 126, 192, 238 },
801     { "SkyBlue3", 108, 166, 205 },
802     { "SkyBlue4", 74, 112, 139 },
803     { "slate blue", 106, 90, 205 },
804     { "slate gray", 112, 128, 144 },
805     { "slate grey", 112, 128, 144 },
806     { "SlateBlue", 106, 90, 205 },
807     { "SlateBlue1", 131, 111, 255 },
808     { "SlateBlue2", 122, 103, 238 },
809     { "SlateBlue3", 105, 89, 205 },
810     { "SlateBlue4", 71, 60, 139 },
811     { "SlateGray", 112, 128, 144 },
812     { "SlateGray1", 198, 226, 255 },
813     { "SlateGray2", 185, 211, 238 },
814     { "SlateGray3", 159, 182, 205 },
815     { "SlateGray4", 108, 123, 139 },
816     { "SlateGrey", 112, 128, 144 },
817     { "snow", 255, 250, 250 },
818     { "snow1", 255, 250, 250 },
819     { "snow2", 238, 233, 233 },
820     { "snow3", 205, 201, 201 },
821     { "snow4", 139, 137, 137 },
822     { "spring green", 0, 255, 127 },
823     { "SpringGreen", 0, 255, 127 },
824     { "SpringGreen1", 0, 255, 127 },
825     { "SpringGreen2", 0, 238, 118 },
826     { "SpringGreen3", 0, 205, 102 },
827     { "SpringGreen4", 0, 139, 69 },
828     { "steel blue", 70, 130, 180 },
829     { "SteelBlue", 70, 130, 180 },
830     { "SteelBlue1", 99, 184, 255 },
831     { "SteelBlue2", 92, 172, 238 },
832     { "SteelBlue3", 79, 148, 205 },
833     { "SteelBlue4", 54, 100, 139 },
834     { "tan", 210, 180, 140 },
835     { "tan1", 255, 165, 79 },
836     { "tan2", 238, 154, 73 },
837     { "tan3", 205, 133, 63 },
838     { "tan4", 139, 90, 43 },
839     { "thistle", 216, 191, 216 },
840     { "thistle1", 255, 225, 255 },
841     { "thistle2", 238, 210, 238 },
842     { "thistle3", 205, 181, 205 },
843     { "thistle4", 139, 123, 139 },
844     { "tomato", 255, 99, 71 },
845     { "tomato1", 255, 99, 71 },
846     { "tomato2", 238, 92, 66 },
847     { "tomato3", 205, 79, 57 },
848     { "tomato4", 139, 54, 38 },
849     { "turquoise", 64, 224, 208 },
850     { "turquoise1", 0, 245, 255 },
851     { "turquoise2", 0, 229, 238 },
852     { "turquoise3", 0, 197, 205 },
853     { "turquoise4", 0, 134, 139 },
854     { "violet", 238, 130, 238 },
855     { "violet red", 208, 32, 144 },
856     { "VioletRed", 208, 32, 144 },
857     { "VioletRed1", 255, 62, 150 },
858     { "VioletRed2", 238, 58, 140 },
859     { "VioletRed3", 205, 50, 120 },
860     { "VioletRed4", 139, 34, 82 },
861     { "wheat", 245, 222, 179 },
862     { "wheat1", 255, 231, 186 },
863     { "wheat2", 238, 216, 174 },
864     { "wheat3", 205, 186, 150 },
865     { "wheat4", 139, 126, 102 },
866     { "white", 255, 255, 255 },
867     { "white smoke", 245, 245, 245 },
868     { "WhiteSmoke", 245, 245, 245 },
869     { "yellow", 255, 255, 0 },
870     { "yellow green", 154, 205, 50 },
871     { "yellow1", 255, 255, 0 },
872     { "yellow2", 238, 238, 0 },
873     { "yellow3", 205, 205, 0 },
874     { "yellow4", 139, 139, 0 },
875     { "YellowGreen", 154, 205, 50 }
876 };
877  
878 #define numXColors (sizeof (xColors) / sizeof (*xColors))
879  
880 /*
881  *----------------------------------------------------------------------
882  *
883  * find_color --
884  *
885  *      This routine finds the color entry that corresponds to the
886  *      specified color.
887  *
888  * Results:
889  *      Returns non-zero on success.  The RGB values of the XColor
890  *      will be initialized to the proper values on success.
891  *
892  * Side effects:
893  *      None.
894  *
895  *----------------------------------------------------------------------
896  */
897
898 static int
899 compare_xcolor_entries (const void *a, const void *b)
900 {
901   return g_ascii_strcasecmp ((const char *) a, 
902                              ((const XPMColorEntry *) b)->name);
903 }
904
905 static gboolean
906 find_color(const char *name,
907            XPMColor   *colorPtr)
908 {
909         XPMColorEntry *found;
910
911         found = bsearch (name, xColors, numXColors, sizeof (XPMColorEntry),
912                          compare_xcolor_entries);
913         if (found == NULL)
914           return FALSE;
915         
916         colorPtr->red = (found->red * 65535) / 255;
917         colorPtr->green = (found->green * 65535) / 255;
918         colorPtr->blue = (found->blue * 65535) / 255;
919         
920         return TRUE;
921 }
922
923 /*
924  *----------------------------------------------------------------------
925  *
926  * parse_color --
927  *
928  *      Partial implementation of X color name parsing interface.
929  *
930  * Results:
931  *      Returns TRUE on success.
932  *
933  * Side effects:
934  *      None.
935  *
936  *----------------------------------------------------------------------
937  */
938
939 static gboolean
940 parse_color (const char *spec,
941              XPMColor   *colorPtr)
942 {
943         if (spec[0] == '#') {
944                 char fmt[16];
945                 int i, red, green, blue;
946
947                 if ((i = strlen (spec + 1)) % 3) {
948                         return FALSE;
949                 }
950                 i /= 3;
951
952                 g_snprintf (fmt, 16, "%%%dx%%%dx%%%dx", i, i, i);
953
954                 if (sscanf (spec + 1, fmt, &red, &green, &blue) != 3) {
955                         return FALSE;
956                 }
957                 if (i == 4) {
958                         colorPtr->red = red;
959                         colorPtr->green = green;
960                         colorPtr->blue = blue;
961                 } else if (i == 1) {
962                         colorPtr->red = (red * 65535) / 15;
963                         colorPtr->green = (green * 65535) / 15;
964                         colorPtr->blue = (blue * 65535) / 15;
965                 } else if (i == 2)
966                 {
967                         colorPtr->red = (red * 65535) / 255;
968                         colorPtr->green = (green * 65535) / 255;
969                         colorPtr->blue = (blue * 65535) / 255;
970                 } else /* if (i == 3) */ {
971                         colorPtr->red = (red * 65535) / 4095;
972                         colorPtr->green = (green * 65535) / 4095;
973                         colorPtr->blue = (blue * 65535) / 4095;
974                 }
975         } else {
976                 if (!find_color(spec, colorPtr))
977                         return FALSE;
978         }
979         return TRUE;
980 }
981
982 static gint
983 xpm_seek_string (FILE *infile, const gchar *str)
984 {
985         char instr[1024];
986
987         while (!feof (infile)) {
988                 if (fscanf (infile, "%1023s", instr) < 0)
989                         return FALSE;
990                 if (strcmp (instr, str) == 0)
991                         return TRUE;
992         }
993
994         return FALSE;
995 }
996
997 static gint
998 xpm_seek_char (FILE *infile, gchar c)
999 {
1000         gint b, oldb;
1001
1002         while ((b = getc (infile)) != EOF) {
1003                 if (c != b && b == '/') {
1004                         b = getc (infile);
1005                         if (b == EOF)
1006                                 return FALSE;
1007
1008                         else if (b == '*') {    /* we have a comment */
1009                                 b = -1;
1010                                 do {
1011                                         oldb = b;
1012                                         b = getc (infile);
1013                                         if (b == EOF)
1014                                                 return FALSE;
1015                                 } while (!(oldb == '*' && b == '/'));
1016                         }
1017                 } else if (c == b)
1018                         return TRUE;
1019         }
1020
1021         return FALSE;
1022 }
1023
1024 static gint
1025 xpm_read_string (FILE *infile, gchar **buffer, guint *buffer_size)
1026 {
1027         gint c;
1028         guint cnt = 0, bufsiz, ret = FALSE;
1029         gchar *buf;
1030
1031         buf = *buffer;
1032         bufsiz = *buffer_size;
1033         if (buf == NULL) {
1034                 bufsiz = 10 * sizeof (gchar);
1035                 buf = g_new (gchar, bufsiz);
1036         }
1037
1038         do {
1039                 c = getc (infile);
1040         } while (c != EOF && c != '"');
1041
1042         if (c != '"')
1043                 goto out;
1044
1045         while ((c = getc (infile)) != EOF) {
1046                 if (cnt == bufsiz) {
1047                         guint new_size = bufsiz * 2;
1048
1049                         if (new_size > bufsiz)
1050                                 bufsiz = new_size;
1051                         else
1052                                 goto out;
1053
1054                         buf = g_realloc (buf, bufsiz);
1055                         buf[bufsiz - 1] = '\0';
1056                 }
1057
1058                 if (c != '"')
1059                         buf[cnt++] = c;
1060                 else {
1061                         buf[cnt] = 0;
1062                         ret = TRUE;
1063                         break;
1064                 }
1065         }
1066
1067  out:
1068         buf[bufsiz - 1] = '\0'; /* ensure null termination for errors */
1069         *buffer = buf;
1070         *buffer_size = bufsiz;
1071         return ret;
1072 }
1073
1074 static gchar *
1075 xpm_extract_color (const gchar *buffer)
1076 {
1077         const gchar *p = &buffer[0];
1078         gint new_key = 0;
1079         gint key = 0;
1080         gint current_key = 1;
1081         gint space = 128;
1082         gchar word[129], color[129], current_color[129];
1083         gchar *r; 
1084         
1085         word[0] = '\0';
1086         color[0] = '\0';
1087         current_color[0] = '\0';
1088         while (1) {
1089                 /* skip whitespace */
1090                 for (; *p != '\0' && g_ascii_isspace (*p); p++) {
1091                 } 
1092                 /* copy word */
1093                 for (r = word; *p != '\0' && !g_ascii_isspace (*p) && r - word < sizeof (word) - 1; p++, r++) {
1094                         *r = *p;
1095                 }
1096                 *r = '\0';
1097                 if (*word == '\0') {
1098                         if (color[0] == '\0')  /* incomplete colormap entry */
1099                                 return NULL;                            
1100                         else  /* end of entry, still store the last color */
1101                                 new_key = 1;
1102                 } 
1103                 else if (key > 0 && color[0] == '\0')  /* next word must be a color name part */
1104                         new_key = 0;
1105                 else {
1106                         if (strcmp (word, "c") == 0)
1107                                 new_key = 5;
1108                         else if (strcmp (word, "g") == 0)
1109                                 new_key = 4;
1110                         else if (strcmp (word, "g4") == 0)
1111                                 new_key = 3;
1112                         else if (strcmp (word, "m") == 0)
1113                                 new_key = 2;
1114                         else if (strcmp (word, "s") == 0)
1115                                 new_key = 1;
1116                         else 
1117                                 new_key = 0;
1118                 }
1119                 if (new_key == 0) {  /* word is a color name part */
1120                         if (key == 0)  /* key expected */
1121                                 return NULL;
1122                         /* accumulate color name */
1123                         if (color[0] != '\0') {
1124                                 strncat (color, " ", space);
1125                                 space -= MIN (space, 1);
1126                         }
1127                         strncat (color, word, space);
1128                         space -= MIN (space, strlen (word));
1129                 }
1130                 else {  /* word is a key */
1131                         if (key > current_key) {
1132                                 current_key = key;
1133                                 strcpy (current_color, color);
1134                         }
1135                         space = 128;
1136                         color[0] = '\0';
1137                         key = new_key;
1138                         if (*p == '\0') break;
1139                 }
1140                 
1141         }
1142         if (current_key > 1)
1143                 return g_strdup (current_color);
1144         else
1145                 return NULL; 
1146 }
1147
1148 /* (almost) direct copy from gdkpixmap.c... loads an XPM from a file */
1149
1150 static const gchar *
1151 file_buffer (enum buf_op op, gpointer handle)
1152 {
1153         struct file_handle *h = handle;
1154
1155         switch (op) {
1156         case op_header:
1157                 if (xpm_seek_string (h->infile, "XPM") != TRUE)
1158                         break;
1159
1160                 if (xpm_seek_char (h->infile, '{') != TRUE)
1161                         break;
1162                 /* Fall through to the next xpm_seek_char. */
1163
1164         case op_cmap:
1165                 xpm_seek_char (h->infile, '"');
1166                 fseek (h->infile, -1, SEEK_CUR);
1167                 /* Fall through to the xpm_read_string. */
1168
1169         case op_body:
1170                 xpm_read_string (h->infile, &h->buffer, &h->buffer_size);
1171                 return h->buffer;
1172
1173         default:
1174                 g_assert_not_reached ();
1175         }
1176
1177         return NULL;
1178 }
1179
1180 /* This reads from memory */
1181 static const gchar *
1182 mem_buffer (enum buf_op op, gpointer handle)
1183 {
1184         struct mem_handle *h = handle;
1185         switch (op) {
1186         case op_header:
1187         case op_cmap:
1188         case op_body:
1189                 if (h->data[h->offset]) {
1190                         const gchar* retval;
1191
1192                         retval = h->data[h->offset];
1193                         h->offset += 1;
1194                         return retval;
1195                 }
1196                 break;
1197
1198         default:
1199                 g_assert_not_reached ();
1200                 break;
1201         }
1202
1203         return NULL;
1204 }
1205
1206 /* This function does all the work. */
1207 static GdkPixbuf *
1208 pixbuf_create_from_xpm (const gchar * (*get_buf) (enum buf_op op, gpointer handle), gpointer handle,
1209                         GError **error)
1210 {
1211         gint w, h, n_col, cpp, x_hot, y_hot, items;
1212         gint cnt, xcnt, ycnt, wbytes, n;
1213         gint is_trans = FALSE;
1214         const gchar *buffer;
1215         gchar *name_buf;
1216         gchar pixel_str[32];
1217         GHashTable *color_hash;
1218         XPMColor *colors, *color, *fallbackcolor;
1219         guchar *pixtmp;
1220         GdkPixbuf *pixbuf;
1221
1222         fallbackcolor = NULL;
1223
1224         buffer = (*get_buf) (op_header, handle);
1225         if (!buffer) {
1226                 g_set_error (error,
1227                              GDK_PIXBUF_ERROR,
1228                              GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1229                              _("No XPM header found"));
1230                 return NULL;
1231         }
1232         items = sscanf (buffer, "%d %d %d %d %d %d", &w, &h, &n_col, &cpp, &x_hot, &y_hot);
1233
1234         if (items != 4 && items != 6) {
1235                 g_set_error (error,
1236                              GDK_PIXBUF_ERROR,
1237                              GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1238                              _("Invalid XPM header"));
1239                 return NULL;
1240         }
1241
1242         if (w <= 0) {
1243                 g_set_error (error,
1244                              GDK_PIXBUF_ERROR,
1245                              GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1246                              _("XPM file has image width <= 0"));
1247                 return NULL;
1248
1249         }
1250         if (h <= 0) {
1251                 g_set_error (error,
1252                              GDK_PIXBUF_ERROR,
1253                              GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1254                              _("XPM file has image height <= 0"));
1255                 return NULL;
1256
1257         }
1258         if (cpp <= 0 || cpp >= 32) {
1259                 g_set_error (error,
1260                              GDK_PIXBUF_ERROR,
1261                              GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1262                              _("XPM has invalid number of chars per pixel"));
1263                 return NULL;
1264         }
1265         if (n_col <= 0 || n_col >= G_MAXINT / (cpp + 1)) {
1266                 g_set_error (error,
1267                              GDK_PIXBUF_ERROR,
1268                              GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1269                              _("XPM file has invalid number of colors"));
1270                 return NULL;
1271         }
1272
1273         /* The hash is used for fast lookups of color from chars */
1274         color_hash = g_hash_table_new (g_str_hash, g_str_equal);
1275
1276         name_buf = g_try_malloc (n_col * (cpp + 1));
1277         if (!name_buf) {
1278                 g_set_error (error,
1279                              GDK_PIXBUF_ERROR,
1280                              GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
1281                              _("Cannot allocate memory for loading XPM image"));
1282                 g_hash_table_destroy (color_hash);
1283                 return NULL;
1284         }
1285         colors = (XPMColor *) g_try_malloc (sizeof (XPMColor) * n_col);
1286         if (!colors) {
1287                 g_set_error (error,
1288                              GDK_PIXBUF_ERROR,
1289                              GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
1290                              _("Cannot allocate memory for loading XPM image"));
1291                 g_hash_table_destroy (color_hash);
1292                 g_free (name_buf);
1293                 return NULL;
1294         }
1295
1296         for (cnt = 0; cnt < n_col; cnt++) {
1297                 gchar *color_name;
1298
1299                 buffer = (*get_buf) (op_cmap, handle);
1300                 if (!buffer) {
1301                         g_set_error (error,
1302                                      GDK_PIXBUF_ERROR,
1303                                      GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1304                                      _("Cannot read XPM colormap"));
1305                         g_hash_table_destroy (color_hash);
1306                         g_free (name_buf);
1307                         g_free (colors);
1308                         return NULL;
1309                 }
1310
1311                 color = &colors[cnt];
1312                 color->color_string = &name_buf[cnt * (cpp + 1)];
1313                 strncpy (color->color_string, buffer, cpp);
1314                 color->color_string[cpp] = 0;
1315                 buffer += strlen (color->color_string);
1316                 color->transparent = FALSE;
1317
1318                 color_name = xpm_extract_color (buffer);
1319
1320                 if ((color_name == NULL) || (g_ascii_strcasecmp (color_name, "None") == 0)
1321                     || (parse_color (color_name, color) == FALSE)) {
1322                         color->transparent = TRUE;
1323                         color->red = 0;
1324                         color->green = 0;
1325                         color->blue = 0;
1326                         is_trans = TRUE;
1327                 }
1328
1329                 g_free (color_name);
1330                 g_hash_table_insert (color_hash, color->color_string, color);
1331
1332                 if (cnt == 0)
1333                         fallbackcolor = color;
1334         }
1335
1336         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, is_trans, 8, w, h);
1337
1338         if (!pixbuf) {
1339                 g_set_error (error,
1340                              GDK_PIXBUF_ERROR,
1341                              GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
1342                              _("Cannot allocate memory for loading XPM image"));
1343                 g_hash_table_destroy (color_hash);
1344                 g_free (colors);
1345                 g_free (name_buf);
1346                 return NULL;
1347         }
1348
1349         wbytes = w * cpp;
1350
1351         for (ycnt = 0; ycnt < h; ycnt++) {
1352                 pixtmp = pixbuf->pixels + ycnt * pixbuf->rowstride;
1353
1354                 buffer = (*get_buf) (op_body, handle);
1355                 if ((!buffer) || (strlen (buffer) < wbytes))
1356                         continue;
1357
1358                 for (n = 0, cnt = 0, xcnt = 0; n < wbytes; n += cpp, xcnt++) {
1359                         strncpy (pixel_str, &buffer[n], cpp);
1360                         pixel_str[cpp] = 0;
1361
1362                         color = g_hash_table_lookup (color_hash, pixel_str);
1363
1364                         /* Bad XPM...punt */
1365                         if (!color)
1366                                 color = fallbackcolor;
1367
1368                         *pixtmp++ = color->red >> 8;
1369                         *pixtmp++ = color->green >> 8;
1370                         *pixtmp++ = color->blue >> 8;
1371
1372                         if (is_trans && color->transparent)
1373                                 *pixtmp++ = 0;
1374                         else if (is_trans)
1375                                 *pixtmp++ = 0xFF;
1376                 }
1377         }
1378
1379         g_hash_table_destroy (color_hash);
1380         g_free (colors);
1381         g_free (name_buf);
1382
1383         if (items == 6) {
1384                 gchar hot[10];
1385                 g_snprintf (hot, 10, "%d", x_hot);
1386                 gdk_pixbuf_set_option (pixbuf, "x_hot", hot);
1387                 g_snprintf (hot, 10, "%d", y_hot);
1388                 gdk_pixbuf_set_option (pixbuf, "y_hot", hot);
1389
1390         }
1391
1392         return pixbuf;
1393 }
1394
1395 /* Shared library entry point for file loading */
1396 static GdkPixbuf *
1397 gdk_pixbuf__xpm_image_load (FILE *f,
1398                             GError **error)
1399 {
1400         GdkPixbuf *pixbuf;
1401         struct file_handle h;
1402
1403         memset (&h, 0, sizeof (h));
1404         h.infile = f;
1405         pixbuf = pixbuf_create_from_xpm (file_buffer, &h, error);
1406         g_free (h.buffer);
1407
1408         return pixbuf;
1409 }
1410
1411 /* Shared library entry point for memory loading */
1412 static GdkPixbuf *
1413 gdk_pixbuf__xpm_image_load_xpm_data (const gchar **data)
1414 {
1415         GdkPixbuf *pixbuf;
1416         struct mem_handle h;
1417         GError *error = NULL;
1418         
1419         h.data = data;
1420         h.offset = 0;
1421         
1422         pixbuf = pixbuf_create_from_xpm (mem_buffer, &h, &error);
1423
1424         if (error) {
1425                 g_warning ("Inline XPM data is broken: %s", error->message);
1426                 g_error_free (error);
1427                 error = NULL;
1428         }
1429         
1430         return pixbuf;
1431 }
1432
1433 /* Progressive loader */
1434 typedef struct _XPMContext XPMContext;
1435 struct _XPMContext
1436 {
1437        GdkPixbufModulePreparedFunc prepare_func;
1438        GdkPixbufModuleUpdatedFunc update_func;
1439        gpointer user_data;
1440
1441        gchar *tempname;
1442        FILE *file;
1443        gboolean all_okay;
1444 };
1445
1446 /*
1447  * FIXME xpm loading progressively is not properly implemented.
1448  * Instead we will buffer to a file then load that file when done.
1449  * This is very broken but it should be relayively simple to fix
1450  * in the future.
1451  */
1452 static gpointer
1453 gdk_pixbuf__xpm_image_begin_load (GdkPixbufModuleSizeFunc size_func,
1454                                   GdkPixbufModulePreparedFunc prepare_func,
1455                                   GdkPixbufModuleUpdatedFunc update_func,
1456                                   gpointer user_data,
1457                                   GError **error)
1458 {
1459        XPMContext *context;
1460        gint fd;
1461
1462        context = g_new (XPMContext, 1);
1463        context->prepare_func = prepare_func;
1464        context->update_func = update_func;
1465        context->user_data = user_data;
1466        context->all_okay = TRUE;
1467        fd = g_file_open_tmp ("gdkpixbuf-xpm-tmp.XXXXXX", &context->tempname,
1468                              NULL);
1469        if (fd < 0) {
1470                g_free (context);
1471                return NULL;
1472        }
1473
1474        context->file = fdopen (fd, "w+");
1475        if (context->file == NULL) {
1476                g_free (context->tempname);
1477                g_free (context);
1478                return NULL;
1479        }
1480
1481        return context;
1482 }
1483
1484 static gboolean
1485 gdk_pixbuf__xpm_image_stop_load (gpointer data,
1486                                  GError **error)
1487 {
1488        XPMContext *context = (XPMContext*) data;
1489        GdkPixbuf *pixbuf;
1490        gboolean retval = FALSE;
1491        
1492        g_return_val_if_fail (data != NULL, FALSE);
1493
1494        fflush (context->file);
1495        rewind (context->file);
1496        if (context->all_okay) {
1497                pixbuf = gdk_pixbuf__xpm_image_load (context->file, error);
1498
1499                if (pixbuf != NULL) {
1500                        (* context->prepare_func) (pixbuf,
1501                                                   NULL,
1502                                                   context->user_data);
1503                        (* context->update_func) (pixbuf, 0, 0, pixbuf->width, pixbuf->height, context->user_data);
1504                        g_object_unref (pixbuf);
1505
1506                        retval = TRUE;
1507                }
1508        }
1509
1510        fclose (context->file);
1511        g_unlink (context->tempname);
1512        g_free (context->tempname);
1513        g_free ((XPMContext *) context);
1514
1515        return retval;
1516 }
1517
1518 static gboolean
1519 gdk_pixbuf__xpm_image_load_increment (gpointer data,
1520                                       const guchar *buf,
1521                                       guint    size,
1522                                       GError **error)
1523 {
1524        XPMContext *context = (XPMContext *) data;
1525
1526        g_return_val_if_fail (data != NULL, FALSE);
1527
1528        if (fwrite (buf, sizeof (guchar), size, context->file) != size) {
1529                context->all_okay = FALSE;
1530                g_set_error (error,
1531                             G_FILE_ERROR,
1532                             g_file_error_from_errno (errno),
1533                             _("Failed to write to temporary file when loading XPM image"));
1534                return FALSE;
1535        }
1536
1537        return TRUE;
1538 }
1539
1540 void
1541 MODULE_ENTRY (xpm, fill_vtable) (GdkPixbufModule *module)
1542 {
1543         module->load = gdk_pixbuf__xpm_image_load;
1544         module->load_xpm_data = gdk_pixbuf__xpm_image_load_xpm_data;
1545         module->begin_load = gdk_pixbuf__xpm_image_begin_load;
1546         module->stop_load = gdk_pixbuf__xpm_image_stop_load;
1547         module->load_increment = gdk_pixbuf__xpm_image_load_increment;
1548 }
1549
1550 void
1551 MODULE_ENTRY (xpm, fill_info) (GdkPixbufFormat *info)
1552 {
1553         static GdkPixbufModulePattern signature[] = {
1554                 { "/* XPM */", NULL, 100 },
1555                 { NULL, NULL, 0 }
1556         };
1557         static gchar * mime_types[] = {
1558                 "image/x-xpixmap",
1559                 NULL
1560         };
1561         static gchar * extensions[] = {
1562                 "xpm",
1563                 NULL
1564         };
1565
1566         info->name = "xpm";
1567         info->signature = signature;
1568         info->description = N_("The XPM image format");
1569         info->mime_types = mime_types;
1570         info->extensions = extensions;
1571         info->flags = GDK_PIXBUF_FORMAT_THREADSAFE;
1572         info->license = "LGPL";
1573 }