File: gifbuild.c

package info (click to toggle)
giflib 5.1.4-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 3,952 kB
  • sloc: sh: 11,565; ansic: 6,901; xml: 2,401; makefile: 278
file content (936 lines) | stat: -rw-r--r-- 25,070 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
/*****************************************************************************

gifbuild - dump GIF data in a textual format, or undump it to a GIF

*****************************************************************************/

#include <stdlib.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>

#include "gif_lib.h"
#include "getarg.h"

#define PROGRAM_NAME	"gifbuild"

static char
    *VersionStr =
	PROGRAM_NAME
	VERSION_COOKIE
	"	Eric Raymond,	"
	__DATE__ ",   " __TIME__ "\n"
	"(C) Copyright 1992 Eric Raymond.\n";
static char
    *CtrlStr =
	PROGRAM_NAME
	" v%- d%- t%-Characters!s h%- GifFile(s)!*s";

static char KeyLetters[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&'()*+,-./:<=>?@[\\]^_`{|}~";
#define PRINTABLES	(sizeof(KeyLetters) - 1)

static void Icon2Gif(char *FileName, FILE *txtin, int fdout);
static void Gif2Icon(char *FileName,
		     int fdin, int fdout,
		     char NameTable[]);
static int EscapeString(char *cp, char *tp);

/******************************************************************************
 Main sequence
******************************************************************************/
int main(int argc, char **argv)
{
    int	NumFiles;
    bool Error,	DisasmFlag = false, HelpFlag = false, TextLineFlag = false;
    char **FileNames = NULL;
    char *TextLines[1];

    if ((Error = GAGetArgs(argc, argv, CtrlStr,
		&GifNoisyPrint, &DisasmFlag, &TextLineFlag, &TextLines[0],
		&HelpFlag, &NumFiles, &FileNames)) != false) {
	GAPrintErrMsg(Error);
	GAPrintHowTo(CtrlStr);
	exit(EXIT_FAILURE);
    }

    if (HelpFlag) {
	(void)fprintf(stderr, VersionStr, GIFLIB_MAJOR, GIFLIB_MINOR);
	GAPrintHowTo(CtrlStr);
	exit(EXIT_SUCCESS);
    }

    if (!DisasmFlag && NumFiles > 1) {
	GIF_MESSAGE("Error in command line parsing - one  text input please.");
	GAPrintHowTo(CtrlStr);
	exit(EXIT_FAILURE);
    }

    if (!DisasmFlag && TextLineFlag) {
	GIF_MESSAGE("Error in command line parsing - -t invalid without -d.");
	GAPrintHowTo(CtrlStr);
	exit(EXIT_FAILURE);
    }


    if (NumFiles == 0)
    {
	if (DisasmFlag)
	    Gif2Icon("Stdin", 0, 1, TextLineFlag ? TextLines[0] : KeyLetters);
	else
	    Icon2Gif("Stdin", stdin, 1);
    }
    else 
    {
	int i;
	for (i = 0; i < NumFiles; i++)
	{
	    FILE	*fp;

	    if ((fp = fopen(FileNames[i], "r")) == (FILE *)NULL)
	    {
		(void) fprintf(stderr, "Can't open %s\n", FileNames[i]);
		exit(EXIT_FAILURE);
	    }

	    if (DisasmFlag)
	    {
		printf("#\n# GIF information from %s\n", FileNames[i]);
		Gif2Icon(FileNames[i], -1, 1, TextLineFlag ? TextLines[0] : KeyLetters);
	    }
	    else
	    {
		Icon2Gif(FileNames[i], fp, 1);
	    }

	    (void) fclose(fp);
	}
    }

    return 0;
}

/******************************************************************************
 Parse image directives
******************************************************************************/
#define PARSE_ERROR(str)  (void) fprintf(stderr,"%s:%d: %s\n",FileName,LineNum,str);

static void Icon2Gif(char *FileName, FILE *txtin, int fdout)
{
    unsigned int	ColorMapSize = 0;
    GifColorType GlobalColorMap[256], LocalColorMap[256],
	*ColorMap = GlobalColorMap;
    char GlobalColorKeys[PRINTABLES], LocalColorKeys[PRINTABLES],
	*KeyTable = GlobalColorKeys;
    bool SortFlag = false;
    unsigned int ExtCode, intval;
    int red, green, blue, n;
    char buf[BUFSIZ * 2], InclusionFile[64];
    GifFileType *GifFileOut;
    SavedImage *NewImage = NULL;
    int LeadingExtensionBlockCount = 0;
    ExtensionBlock *LeadingExtensionBlocks = NULL;
    int ErrorCode, LineNum = 0;

    if ((GifFileOut = EGifOpenFileHandle(fdout, &ErrorCode)) == NULL) {
	PrintGifError(ErrorCode);
	exit(EXIT_FAILURE);
    }

    /* OK, interpret directives */
    /* coverity[tainted_data_transitive] */
    while (fgets(buf, sizeof(buf), txtin) != (char *)NULL)
    {
	char	*cp;

	++LineNum;

	/*
	 * Skip lines consisting only of whitespace and comments
	 */
	for (cp = buf; isspace((int)(*cp)); cp++)
	    continue;
	if (*cp == '#' || *cp == '\0')
	    continue;

	/*
	 * If there's a trailing comment, nuke it and all preceding whitespace.
	 * But preserve the EOL.
	 */
	if ((cp = strchr(buf, '#')) && (cp == strrchr(cp, '#')))
	{
	    while (isspace((int)(*--cp)))
		continue;
	    *++cp = '\n';
	    *++cp = '\0';
	}

	/*
	 * Explicit header declarations
	 */

	// cppcheck-suppress invalidscanf 
	if (sscanf(buf, "screen width %d\n", &GifFileOut->SWidth) == 1)
	    continue;

	// cppcheck-suppress invalidscanf 
	else if (sscanf(buf, "screen height %d\n", &GifFileOut->SHeight) == 1)
	    continue;

	// cppcheck-suppress invalidscanf 
	else if (sscanf(buf, "screen colors %d\n", &n) == 1)
	{
	    int	ResBits = GifBitSize(n);

	    if (n > 256 || n < 0 || n != (1 << ResBits))
	    {
		PARSE_ERROR("Invalid color resolution value.");
		exit(EXIT_FAILURE);
	    }

	    GifFileOut->SColorResolution = ResBits;
	    continue;
	}

	// cppcheck-suppress invalidscanf 
	else if (sscanf(buf,
			"screen background %d\n",
			&GifFileOut->SBackGroundColor) == 1)
	    continue;

	// cppcheck-suppress invalidscanf 
	else if (sscanf(buf, "pixel aspect byte %u\n", &intval) == 1) {
	    GifFileOut->AspectByte = (GifByteType)(intval & 0xff);
	    continue;
	}

	/*
	 * Color table parsing
	 */

	else if (strcmp(buf, "screen map\n") == 0)
	{
	    if (GifFileOut->SColorMap != NULL)
	    {
		PARSE_ERROR("You've already declared a global color map.");
		exit(EXIT_FAILURE);
	    }

	    ColorMapSize = 0;
	    ColorMap = GlobalColorMap;
	    SortFlag = false;
	    KeyTable = GlobalColorKeys;
	    memset(GlobalColorKeys, '\0', sizeof(GlobalColorKeys));
	}

	else if (strcmp(buf, "image map\n") == 0)
	{
	    if (NewImage == NULL)
	    {
		PARSE_ERROR("No previous image declaration.");
		exit(EXIT_FAILURE);
	    }

	    ColorMapSize = 0;
	    ColorMap = LocalColorMap;
	    KeyTable = LocalColorKeys;
	    memset(LocalColorKeys, '\0', sizeof(LocalColorKeys));
	}

	// cppcheck-suppress invalidscanf 
	else if (sscanf(buf, "	rgb %d %d %d is %c",
		   &red, &green, &blue, &KeyTable[ColorMapSize]) == 4)
	{
	    ColorMap[ColorMapSize].Red = red;
	    ColorMap[ColorMapSize].Green = green;
	    ColorMap[ColorMapSize].Blue = blue;
	    ColorMapSize++;
	}

	// cppcheck-suppress invalidscanf 
	else if (sscanf(buf, "	rgb %d %d %d", &red, &green, &blue) == 3)
	{
	    ColorMap[ColorMapSize].Red = red;
	    ColorMap[ColorMapSize].Green = green;
	    ColorMap[ColorMapSize].Blue = blue;
	    ColorMapSize++;
	}

	else if (strcmp(buf, "	sort flag on\n") == 0)
	    SortFlag = true;

	else if (strcmp(buf, "	sort flag off\n") == 0)
	    SortFlag = false;

	else if (strcmp(buf, "end\n") == 0)
	{
	    ColorMapObject	*NewMap;

	    NewMap = GifMakeMapObject(1 << GifBitSize(ColorMapSize), ColorMap);
	    if (NewMap == (ColorMapObject *)NULL)
	    {
		PARSE_ERROR("Out of memory while allocating new color map.");
		exit(EXIT_FAILURE);
	    }

	    NewMap->SortFlag = SortFlag;

	    if (NewImage)
		NewImage->ImageDesc.ColorMap = NewMap;
	    else
		GifFileOut->SColorMap = NewMap;
	}

	/* GIF inclusion */
	// cppcheck-suppress invalidscanf 
	else if (sscanf(buf, "include %s", InclusionFile) == 1)
	{
	    int		ErrorCode;
	    bool	DoTranslation;
	    GifPixelType	Translation[256];

	    GifFileType	*Inclusion;
	    SavedImage	*NewImage, *CopyFrom;

	    if ((Inclusion = DGifOpenFileName(InclusionFile, &ErrorCode)) == NULL) {
		PrintGifError(ErrorCode);
		exit(EXIT_FAILURE);
	    }

	    if (DGifSlurp(Inclusion) == GIF_ERROR)
	    {
		PARSE_ERROR("Inclusion read failed.");
		if (Inclusion != NULL) {
		    PrintGifError(Inclusion->Error);
		    DGifCloseFile(Inclusion, NULL);
		}
		if (GifFileOut != NULL) {
		    EGifCloseFile(GifFileOut, NULL);
		};
		exit(EXIT_FAILURE);
	    }

	    if ((DoTranslation = (GifFileOut->SColorMap!=(ColorMapObject*)NULL)))
	    {
		ColorMapObject	*UnionMap;

		UnionMap = GifUnionColorMap(GifFileOut->SColorMap,
					 Inclusion->SColorMap, Translation);

		if (UnionMap == NULL)
		{
		    PARSE_ERROR("Inclusion failed --- global map conflict.");
		    PrintGifError(GifFileOut->Error);
		    if (Inclusion != NULL) DGifCloseFile(Inclusion, NULL);
		    if (GifFileOut != NULL) EGifCloseFile(GifFileOut, NULL);
		    exit(EXIT_FAILURE);
		}

		GifFreeMapObject(GifFileOut->SColorMap);
		GifFileOut->SColorMap = UnionMap;
	    }

	    for (CopyFrom = Inclusion->SavedImages;
		 CopyFrom < Inclusion->SavedImages + Inclusion->ImageCount;
		 CopyFrom++)
	    {
		if ((NewImage = GifMakeSavedImage(GifFileOut, CopyFrom)) == NULL)
		{
		    PARSE_ERROR("Inclusion failed --- out of memory.");
		    PrintGifError(GifFileOut->Error);
		    if (Inclusion != NULL) DGifCloseFile(Inclusion, NULL);
		    if (GifFileOut != NULL) EGifCloseFile(GifFileOut, NULL);
		    exit(EXIT_FAILURE);
		}
		else if (DoTranslation)
		    GifApplyTranslation(NewImage, Translation);

		GifQprintf(
		        "%s: Image %d at (%d, %d) [%dx%d]: from %s\n",
			PROGRAM_NAME, GifFileOut->ImageCount,
			NewImage->ImageDesc.Left, NewImage->ImageDesc.Top,
			NewImage->ImageDesc.Width, NewImage->ImageDesc.Height,
			InclusionFile);
	    }

	    (void) DGifCloseFile(Inclusion, NULL);
	}

	/*
	 * Extension blocks.
	 */
	else if (strcmp(buf, "comment\n") == 0)
	{
	    int bc = 0;
	    while (fgets(buf, sizeof(buf), txtin) != (char *)NULL)
		if (strcmp(buf, "end\n") == 0)
		    break;
	        else
		{
		    int Len;

		    buf[strlen(buf) - 1] = '\0';
		    Len = EscapeString(buf, buf);
		    if (GifAddExtensionBlock(&LeadingExtensionBlockCount,
					     &LeadingExtensionBlocks,
					     bc++ == CONTINUE_EXT_FUNC_CODE ? COMMENT_EXT_FUNC_CODE : 0,
					     Len,
					     (unsigned char *)buf) == GIF_ERROR) {
			PARSE_ERROR("out of memory while adding comment block.");
			exit(EXIT_FAILURE);
		    }
		}
	}
	else if (strcmp(buf, "plaintext\n") == 0)
	{
	    int bc = 0;
	    while (fgets(buf, sizeof(buf), txtin) != (char *)NULL)
		if (strcmp(buf, "end\n") == 0)
		    break;
	        else
		{
		    int Len;

		    buf[strlen(buf) - 1] = '\0';
		    Len = EscapeString(buf, buf);
		    if (GifAddExtensionBlock(&LeadingExtensionBlockCount,
					     &LeadingExtensionBlocks,
					     bc++ == CONTINUE_EXT_FUNC_CODE ? PLAINTEXT_EXT_FUNC_CODE : 0,
					     Len, 
					     (unsigned char *)buf) == GIF_ERROR) {
			PARSE_ERROR("out of memory while adding plaintext block.");
			exit(EXIT_FAILURE);
		    }
		}
	}
	else if (strcmp(buf, "graphics control\n") == 0)
	{
	    GraphicsControlBlock gcb;
	    size_t Len;

	    memset(&gcb, '\0', sizeof(gcb));
	    gcb.TransparentColor = NO_TRANSPARENT_COLOR;
	    while (fgets(buf, sizeof(buf), txtin) != (char *)NULL)
		if (strcmp(buf, "end\n") == 0)
		    break;
	        else
		{
		    char *tp = buf;

		    while (isspace(*tp))
			tp++;
		    // cppcheck-suppress invalidscanf 
		    if (sscanf(tp, "disposal mode %d\n", &gcb.DisposalMode))
			continue;
		    if (strcmp(tp, "user input flag on\n") == 0) {
			gcb.UserInputFlag = true;
			continue;
		    }
		    if (strcmp(tp, "user input flag off\n") == 0) {
			gcb.UserInputFlag = false;
			continue;
		    }
		    // cppcheck-suppress invalidscanf 
		    if (sscanf(tp, "delay %d\n", &gcb.DelayTime))
			continue;
		    // cppcheck-suppress invalidscanf 
		    if (sscanf(tp, "transparent index %d\n",
			       &gcb.TransparentColor))
			continue;
		    (void) fputs(tp, stderr);
		    PARSE_ERROR("unrecognized directive in GCB block.");
		    exit(EXIT_FAILURE);
		}
	    Len = EGifGCBToExtension(&gcb, (GifByteType *)buf);
	    if (GifAddExtensionBlock(&LeadingExtensionBlockCount,
				     &LeadingExtensionBlocks,
				     GRAPHICS_EXT_FUNC_CODE,
				     Len,
				     (unsigned char *)buf) == GIF_ERROR) {
		PARSE_ERROR("out of memory while adding GCB.");
		exit(EXIT_FAILURE);
	    }

	}
	// cppcheck-suppress invalidscanf 
	else if (sscanf(buf, "netscape loop %u", &intval))
	{
	    unsigned char params[3] = {1, 0, 0};
	    /* Create a Netscape 2.0 loop block */
	    if (GifAddExtensionBlock(&LeadingExtensionBlockCount,
				     &LeadingExtensionBlocks,
				     APPLICATION_EXT_FUNC_CODE,
				     11,
				     (unsigned char *)"NETSCAPE2.0")==GIF_ERROR) {
		PARSE_ERROR("out of memory while adding loop block.");
		exit(EXIT_FAILURE);
	    }
	    params[1] = (intval & 0xff);
	    params[2] = (intval >> 8) & 0xff;
	    if (GifAddExtensionBlock(&LeadingExtensionBlockCount,
				     &LeadingExtensionBlocks,
				     0, sizeof(params), params) == GIF_ERROR) {
		PARSE_ERROR("out of memory while adding loop continuation.");
		exit(EXIT_FAILURE);
	    }
	    
	}
	// cppcheck-suppress invalidscanf 
	else if (sscanf(buf, "extension %x", &ExtCode))
	{
	    int bc = 0;
	    while (fgets(buf, sizeof(buf), txtin) != (char *)NULL)
		if (strcmp(buf, "end\n") == 0)
		    break;
	        else
		{
		    int Len;

		    buf[strlen(buf) - 1] = '\0';
		    Len = EscapeString(buf, buf);
		    if (GifAddExtensionBlock(&LeadingExtensionBlockCount,
					     &LeadingExtensionBlocks,
					     bc++ == CONTINUE_EXT_FUNC_CODE ? ExtCode : 0, 
					     Len,
					     (unsigned char *)buf) == GIF_ERROR) {
			PARSE_ERROR("out of memory while adding extension block.");
			exit(EXIT_FAILURE);
		    }
		}
	}

	/*
	 * Explicit image declarations 
	 */

	else if (strcmp(buf, "image\n") == 0)
	{
	    if ((NewImage = GifMakeSavedImage(GifFileOut, NULL)) == (SavedImage *)NULL)
	    {
		PARSE_ERROR("Out of memory while allocating image block.");
		exit(EXIT_FAILURE);
	    }

	    /* use global table unless user specifies a local one */
	    ColorMap = GlobalColorMap;
	    KeyTable = GlobalColorKeys;

	    /* connect leading extension blocks */
	    NewImage->ExtensionBlockCount = LeadingExtensionBlockCount;
	    NewImage->ExtensionBlocks = LeadingExtensionBlocks;
	    LeadingExtensionBlockCount = 0;
	    LeadingExtensionBlocks = NULL;
	}

	/*
	 * Nothing past this point is valid unless we've seen a previous
	 * image declaration.
	 */
	else if (NewImage == (SavedImage *)NULL)
	{
	    (void) fputs(buf, stderr);
	    PARSE_ERROR("Syntax error in header block.");
	    exit(EXIT_FAILURE);
	}

	/*
	 * Accept image attributes
	 */
	// cppcheck-suppress invalidscanf 
	else if (sscanf(buf, "image top %d\n", &NewImage->ImageDesc.Top) == 1)
	    continue;

	// cppcheck-suppress invalidscanf 
	else if (sscanf(buf, "image left %d\n", &NewImage->ImageDesc.Left)== 1)
	    continue;

	else if (strcmp(buf, "image interlaced\n") == 0)
	{
	    NewImage->ImageDesc.Interlace = true;
	    continue;
	}

	// cppcheck-suppress invalidscanf 
	else if (sscanf(buf,
			"image bits %d by %d",
			&NewImage->ImageDesc.Width,
			&NewImage->ImageDesc.Height) == 2)
	{
	    int i, j;
	    static GifPixelType *Raster, *cp;
	    int c;
	    bool hex = (strstr(buf, "hex") != NULL);

	    /* coverity[overflow_sink] */
	    if ((Raster = (GifPixelType *) malloc(sizeof(GifPixelType) * NewImage->ImageDesc.Width * NewImage->ImageDesc.Height))
		== NULL) {
		PARSE_ERROR("Failed to allocate raster block, aborted.");
		exit(EXIT_FAILURE);
	    }

	    GifQprintf("%s: Image %d at (%d, %d) [%dx%d]:     ",
		       PROGRAM_NAME, GifFileOut->ImageCount,
		       NewImage->ImageDesc.Left, NewImage->ImageDesc.Top,
		       NewImage->ImageDesc.Width, NewImage->ImageDesc.Height);

	    cp = Raster;
	    for (i = 0; i < NewImage->ImageDesc.Height; i++) {

		char	*dp;

		for (j = 0; j < NewImage->ImageDesc.Width; j++)
		    if ((c = fgetc(txtin)) == EOF) {
			PARSE_ERROR("input file ended prematurely.");
			exit(EXIT_FAILURE);
		    }
		    else if (c == '\n')
		    {
			--j;
			++LineNum;
		    }
		    else if (isspace(c))
			--j;
		    else if (hex) 
		    {
			const static char *hexdigits = "0123456789ABCDEF";
			unsigned char hi, lo;
			dp = strchr(hexdigits, toupper(c));
			if (dp == NULL) {
			    PARSE_ERROR("Invalid hex high byte.");
			    exit(EXIT_FAILURE);
			}
			hi = (dp - hexdigits);
			if ((c = fgetc(txtin)) == EOF) {
			    PARSE_ERROR("input file ended prematurely.");
			    exit(EXIT_FAILURE);
			}
			dp = strchr(hexdigits, toupper(c));
			if (dp == NULL) {
			    PARSE_ERROR("Invalid hex low byte.");
			    exit(EXIT_FAILURE);
			}
			lo = (dp - hexdigits);
			*cp++ = (hi << 4) | lo;
		    }
		    else if ((dp = strchr(KeyTable, c)))
			*cp++ = (dp - KeyTable);
		    else {
			PARSE_ERROR("Invalid ASCII pixel key.");
			exit(EXIT_FAILURE);
		    }

		if (GifNoisyPrint)
		    fprintf(stderr, "\b\b\b\b%-4d", i);
	    }

	    if (GifNoisyPrint)
		putc('\n', stderr);

	    NewImage->RasterBits = (unsigned char *) Raster;
	}
	else
	{
	    (void) fputs(buf, stderr);
	    PARSE_ERROR("Syntax error in image description.");
	    exit(EXIT_FAILURE);
	}
    }

    /* connect trailing extension blocks */
    GifFileOut->ExtensionBlockCount = LeadingExtensionBlockCount;
    GifFileOut->ExtensionBlocks = LeadingExtensionBlocks;
    //LeadingExtensionBlockCount = 0;
    LeadingExtensionBlocks = NULL;
 
    EGifSpew(GifFileOut);
}

static void VisibleDumpBuffer(GifByteType *buf, int len)
/* Visibilize a given string */
{
    GifByteType	*cp;

    for (cp = buf; cp < buf + len; cp++)
    {
	if (isprint((int)(*cp)) || *cp == ' ')
	    putchar(*cp);
	else if (*cp == '\n')
	{
	    putchar('\\'); putchar('n');
	}
	else if (*cp == '\r')
	{
	    putchar('\\'); putchar('r');
	}
	else if (*cp == '\b')
	{
	    putchar('\\'); putchar('b');
	}
	else if (*cp < ' ')
	{
	    putchar('\\'); putchar('^'); putchar('@' + *cp);
	}
	else
	    printf("\\0x%02x", *cp);
    }
}

static void DumpExtensions(GifFileType *GifFileOut, 
			   int ExtensionBlockCount,
			   ExtensionBlock *ExtensionBlocks)
{
    ExtensionBlock *ep;

    for (ep = ExtensionBlocks; 
	 ep < ExtensionBlocks + ExtensionBlockCount;
	 ep++) {
	bool last = (ep - ExtensionBlocks == (ExtensionBlockCount - 1));
	if (ep->Function == COMMENT_EXT_FUNC_CODE) {
	    printf("comment\n");
	    VisibleDumpBuffer(ep->Bytes, ep->ByteCount);
	    putchar('\n');
	    while (!last && ep[1].Function == CONTINUE_EXT_FUNC_CODE) {
		++ep;
		last = (ep - ExtensionBlocks == (ExtensionBlockCount - 1));
		VisibleDumpBuffer(ep->Bytes, ep->ByteCount);
		putchar('\n');
	    }
	    printf("end\n\n");
	}
	else if (ep->Function == PLAINTEXT_EXT_FUNC_CODE) {
	    printf("plaintext\n");
	    VisibleDumpBuffer(ep->Bytes, ep->ByteCount);
	    putchar('\n');
	    while (!last && ep[1].Function == CONTINUE_EXT_FUNC_CODE) {
		++ep;
		last = (ep - ExtensionBlocks == (ExtensionBlockCount - 1));
		VisibleDumpBuffer(ep->Bytes, ep->ByteCount);
		putchar('\n');
	    }
	    printf("end\n\n");
	}
	else if (ep->Function == GRAPHICS_EXT_FUNC_CODE)
	{
	    GraphicsControlBlock gcb;
	    printf("graphics control\n");
	    if (DGifExtensionToGCB(ep->ByteCount, ep->Bytes, &gcb) == GIF_ERROR) {
		GIF_MESSAGE("invalid graphics control block");
		exit(EXIT_FAILURE);
	    }
	    printf("\tdisposal mode %d\n", gcb.DisposalMode);
	    printf("\tuser input flag %s\n", 
		   gcb.UserInputFlag ? "on" : "off");
	    printf("\tdelay %d\n", gcb.DelayTime);
	    printf("\ttransparent index %d\n", gcb.TransparentColor);
	    printf("end\n\n");
	}
	else if (!last
		 && ep->Function == APPLICATION_EXT_FUNC_CODE
		 && ep->ByteCount >= 11
		 && (ep+1)->ByteCount >= 3
		 && memcmp(ep->Bytes, "NETSCAPE2.0", 11) == 0) {
	    unsigned char *params = (++ep)->Bytes;
	    unsigned int loopcount = params[1] | (params[2] << 8);
	    printf("netscape loop %u\n\n", loopcount);
	}
	else {
	    printf("extension 0x%02x\n", ep->Function);
	    VisibleDumpBuffer(ep->Bytes, ep->ByteCount);
	    while (!last && ep[1].Function == CONTINUE_EXT_FUNC_CODE) {
		++ep;
		last = (ep - ExtensionBlocks == (ExtensionBlockCount - 1));
		VisibleDumpBuffer(ep->Bytes, ep->ByteCount);
		putchar('\n');
	    }
	    printf("end\n\n");
	}
    }
}

static void Gif2Icon(char *FileName,
		     int fdin, int fdout,
		     char NameTable[])
{
    int ErrorCode, im, i, j, ColorCount = 0;
    GifFileType *GifFile;

    if (fdin == -1) {
	if ((GifFile = DGifOpenFileName(FileName, &ErrorCode)) == NULL) {
	    PrintGifError(ErrorCode);
	    exit(EXIT_FAILURE);
	}
    }
    else {
	/* Use stdin instead: */
	if ((GifFile = DGifOpenFileHandle(fdin, &ErrorCode)) == NULL) {
	    PrintGifError(ErrorCode);
	    exit(EXIT_FAILURE);
	}
    }

    if (DGifSlurp(GifFile) == GIF_ERROR) {
	PrintGifError(GifFile->Error);
	exit(EXIT_FAILURE);
    }

    printf("screen width %d\nscreen height %d\n",
	   GifFile->SWidth, GifFile->SHeight);

    printf("screen colors %d\nscreen background %d\npixel aspect byte %u\n\n",
	   1 << GifFile->SColorResolution,
	   GifFile->SBackGroundColor,
	   (unsigned)GifFile->AspectByte);

    if (GifFile->SColorMap)
    {
	printf("screen map\n");

	printf("\tsort flag %s\n", GifFile->SColorMap->SortFlag ? "on" : "off");

	for (i = 0; i < GifFile->SColorMap->ColorCount; i++)
	    if (GifFile->SColorMap->ColorCount < PRINTABLES)
		printf("\trgb %03d %03d %03d is %c\n",
		       GifFile->SColorMap ->Colors[i].Red,
		       GifFile->SColorMap ->Colors[i].Green,
		       GifFile->SColorMap ->Colors[i].Blue,
		       NameTable[i]);
	    else
		printf("\trgb %03d %03d %03d\n",
		       GifFile->SColorMap ->Colors[i].Red,
		       GifFile->SColorMap ->Colors[i].Green,
		       GifFile->SColorMap ->Colors[i].Blue);
	printf("end\n\n");
    }

    for (im = 0; im < GifFile->ImageCount; im++) {
	SavedImage *image = &GifFile->SavedImages[im];

	DumpExtensions(GifFile, 
		       image->ExtensionBlockCount, image->ExtensionBlocks);

	printf("image # %d\nimage left %d\nimage top %d\n",
	       im+1, image->ImageDesc.Left, image->ImageDesc.Top);
	if (image->ImageDesc.Interlace)
	    printf("image interlaced\n");

	if (image->ImageDesc.ColorMap)
	{
	    printf("image map\n");

	    printf("\tsort flag %s\n", 
		   image->ImageDesc.ColorMap->SortFlag ? "on" : "off");

	    if (image->ImageDesc.ColorMap->ColorCount < PRINTABLES)
		for (i = 0; i < image->ImageDesc.ColorMap->ColorCount; i++)
		    printf("\trgb %03d %03d %03d is %c\n",
			   image->ImageDesc.ColorMap ->Colors[i].Red,
			   image->ImageDesc.ColorMap ->Colors[i].Green,
			   image->ImageDesc.ColorMap ->Colors[i].Blue,
			   NameTable[i]);
	    else
		for (i = 0; i < image->ImageDesc.ColorMap->ColorCount; i++)
		    printf("\trgb %03d %03d %03d\n",
			   image->ImageDesc.ColorMap ->Colors[i].Red,
			   image->ImageDesc.ColorMap ->Colors[i].Green,
			   image->ImageDesc.ColorMap ->Colors[i].Blue);
	    printf("end\n\n");
	}

	/* one of these conditions has to be true */
	if (image->ImageDesc.ColorMap)
	    ColorCount = image->ImageDesc.ColorMap->ColorCount;
	else if (GifFile->SColorMap)
	    ColorCount = GifFile->SColorMap->ColorCount;

	if (ColorCount < PRINTABLES)
	    printf("image bits %d by %d\n",
		   image->ImageDesc.Width, image->ImageDesc.Height);
	else
	    printf("image bits %d by %d hex\n",
		   image->ImageDesc.Width, image->ImageDesc.Height);
	for (i = 0; i < image->ImageDesc.Height; i++) {
	    for (j = 0; j < image->ImageDesc.Width; j++) {
		GifByteType ch = image->RasterBits[i*image->ImageDesc.Width + j];
		if (ColorCount < PRINTABLES)
		    putchar(NameTable[ch]);
		else
		    printf("%02x", ch);
	    }
	    putchar('\n');
	}
	putchar('\n');
    }

    DumpExtensions(GifFile, 
		   GifFile->ExtensionBlockCount, GifFile->ExtensionBlocks);

    /* Tell EMACS this is a picture... */
    printf("# The following sets edit modes for GNU EMACS\n");
    printf("# Local ");		/* ...break this up, so that EMACS doesn't */
    printf("Variables:\n");	/* get confused when visiting *this* file! */
    printf("# mode:picture\n");
    printf("# truncate-lines:t\n");
    printf("# End:\n");

    if (fdin == -1)
	(void) printf("# End of %s dump\n", FileName);

    if (DGifCloseFile(GifFile, &ErrorCode) == GIF_ERROR) {
	PrintGifError(ErrorCode);
	exit(EXIT_FAILURE);
    }
}

static int EscapeString(char *cp, char *tp)
/* process standard C-style escape sequences in a string */
{
    char *StartAddr = tp;

    while (*cp)
    {
	int	cval = 0;

	if (*cp == '\\' && strchr("0123456789xX", cp[1]))
	{
	    int dcount = 0;

	    if (*++cp == 'x' || *cp == 'X') {
		char *dp, *hex = "00112233445566778899aAbBcCdDeEfF";
		for (++cp; (dp = strchr(hex, *cp)) && (dcount++ < 2); cp++)
		    cval = (cval * 16) + (dp - hex) / 2;
	    } else if (*cp == '0')
		while (strchr("01234567",*cp) != (char*)NULL && (dcount++ < 3))
		    cval = (cval * 8) + (*cp++ - '0');
	    else
		while ((strchr("0123456789",*cp)!=(char*)NULL)&&(dcount++ < 3))
		    cval = (cval * 10) + (*cp++ - '0');
	}
	else if (*cp == '\\')		/* C-style character escapes */
	{
	    switch (*++cp)
	    {
	    case '\\': cval = '\\'; break;
	    case 'n': cval = '\n'; break;
	    case 't': cval = '\t'; break;
	    case 'b': cval = '\b'; break;
	    case 'r': cval = '\r'; break;
	    default: cval = *cp;
	    }
	    cp++;
	}
	else if (*cp == '^')		/* expand control-character syntax */
	{
	    cval = (*++cp & 0x1f);
	    cp++;
	}
	else
	    cval = *cp++;
	*tp++ = cval;
    }

    return(tp - StartAddr);
}

/* end */