summaryrefslogtreecommitdiff
path: root/src/encparse.c
blob: 2e24475579c16e07fe7f5f804a3726d33558c3af (plain)
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
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
/*
Copyright (c) 1998-2001 by Juliusz Chroboczek

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

/* $XFree86: xc/lib/font/fontfile/encparse.c,v 1.20 2004/02/11 21:11:19 dawes Exp $ */

/* Parser for encoding files */

/* This code assumes that we are using ASCII.  We don't use the ctype
   functions, as they depend on the current locale.  On the other
   hand, we do use strcasecmp, but only on strings that we've checked
   to be pure ASCII.  Bloody ``Code Set Independence''. */

#include <string.h>
#include <stdio.h>

#ifdef SCO325
#include <strings.h>
#endif

#ifndef FONTENC_NO_LIBFONT

#include "fntfilio.h"
#include "fntfilst.h"

#else

#include <stdlib.h>
#define xalloc(n) malloc(n)
#define xrealloc(p, n) realloc(p, n)
#define xfree(p) free(p)

#include "zlib.h"
typedef gzFile FontFilePtr;
#define FontFileGetc(f) gzgetc(f)
#define FontFileOpen(filename) gzopen(filename, "rb")
#define FontFileClose(f) gzclose(f)

#define MAXFONTFILENAMELEN 1024
#define MAXFONTNAMELEN 1024

#endif

#include "fontenc.h"
#include "fontencI.h"

#define MAXALIASES 20

#define EOF_TOKEN -1
#define ERROR_TOKEN -2
#define EOL_TOKEN 0
#define NUMBER_TOKEN 1
#define KEYWORD_TOKEN 2

#define EOF_LINE -1
#define ERROR_LINE -2
#define STARTENCODING_LINE 1
#define STARTMAPPING_LINE 2
#define ENDMAPPING_LINE 3
#define CODE_LINE 4
#define CODE_RANGE_LINE 5
#define CODE_UNDEFINE_LINE 6
#define NAME_LINE 7
#define SIZE_LINE 8
#define ALIAS_LINE 9
#define FIRSTINDEX_LINE 10

/* Return from lexer */
#define MAXKEYWORDLEN 100

static long number_value;
static char keyword_value[MAXKEYWORDLEN+1];

static long value1, value2, value3;

/* Lexer code */

/* Skip to the beginning of new line */
static void
skipEndOfLine(FontFilePtr f, int c)
{
    if(c == 0)
        c = FontFileGetc(f);
  
    for(;;)
        if(c <= 0 || c == '\n')
            return;
        else
            c = FontFileGetc(f);
}

/* Get a number; we're at the first digit. */
static unsigned
getnum(FontFilePtr f, int c, int *cp)
{
    unsigned n = 0;
    int base = 10;

    /* look for `0' or `0x' prefix */
    if(c == '0') {
        c = FontFileGetc(f);
        base = 8;
        if(c == 'x' || c == 'X') {
            base = 16;
            c = FontFileGetc(f);
        }
    }

    /* accumulate digits */
    for(;;) {
        if ('0' <= c && c <= '9') {
            n *= base; n += c - '0';
        } else if('a' <= c && c <= 'f') {
            n *= base; n += c - 'a' + 10;
        } else if('A' <=c && c <= 'F') {
            n *= base; n += c - 'A' + 10;
        } else
            break;
        c = FontFileGetc(f);
    }

    *cp = c; return n;
}
 
/* Skip to beginning of new line; return 1 if only whitespace was found. */
static int
endOfLine(FontFilePtr f, int c)
{
    if(c == 0)
        c = FontFileGetc(f);

    for(;;) {
        if(c <= 0 || c == '\n')
            return 1;
        else if(c == '#') {
            skipEndOfLine(f,c);
            return 1;
        }
        else if(c == ' ' || c == '\t') {
            skipEndOfLine(f,c);
            return 0;
        }
        c = FontFileGetc(f);
    }
}

/* Get a token; we're at first char */
static int
gettoken(FontFilePtr f, int c, int *cp)
{
    char *p;

    if(c <= 0)
    c = FontFileGetc(f);

    if(c <= 0) {
        return EOF_TOKEN;
    }

    while(c == ' ' || c == '\t')
        c = FontFileGetc(f);

    if(c=='\n') {
        return EOL_TOKEN;
    } else if(c == '#') {
        skipEndOfLine(f,c);
        return EOL_TOKEN;
    } else if(c >= '0' && c <= '9') {
        number_value = getnum(f,c,cp);
        return NUMBER_TOKEN;
    } else if((c >= 'A' && c <= 'Z') ||
              (c >= 'a' && c <= 'z') ||
              c == '/' || c == '_' || c == '-' || c == '.') {
        p = keyword_value;
        *p++ = c;
        while(p-keyword_value < MAXKEYWORDLEN) {
            c = FontFileGetc(f);
            if(c <= ' ' || c > '~' || c == '#')
                break;
            *p++ = c;
        }
        *cp = c;
        *p = '\0';
        return KEYWORD_TOKEN;
    } else {
        *cp = c;
        return ERROR_TOKEN;
    }
}

/* Parse a line.
 * Always skips to the beginning of a new line, even if an error occurs */
static int
getnextline(FontFilePtr f)
{
    int c, token;
    c = FontFileGetc(f);
    if(c <= 0)
        return EOF_LINE;

  again:
    token=gettoken(f,c,&c);

    switch(token) {
    case EOF_TOKEN:
        return EOF_LINE;
    case EOL_TOKEN:
        /* empty line */
        c = FontFileGetc(f);
        goto again;
    case NUMBER_TOKEN:
        value1 = number_value;
        token = gettoken(f,c,&c);
        switch(token) {
        case NUMBER_TOKEN:
            value2 = number_value;
            token = gettoken(f,c,&c);
            switch(token) {
            case NUMBER_TOKEN:
                value3 = number_value;
                return CODE_RANGE_LINE;
            case EOL_TOKEN:
                return CODE_LINE;
            default:
                skipEndOfLine(f,c);
                return ERROR_LINE;
            }
        case KEYWORD_TOKEN:
            if(!endOfLine(f,c))
                return ERROR_LINE;
            else
                return NAME_LINE;
        default:
            skipEndOfLine(f,c);
            return ERROR_LINE;
        }
    case KEYWORD_TOKEN:
        if(!strcasecmp(keyword_value, "STARTENCODING")) {
            token = gettoken(f,c,&c);
            if(token == KEYWORD_TOKEN) {
                if(endOfLine(f,c))
                    return STARTENCODING_LINE;
                else
                    return ERROR_LINE;
            } else {
                skipEndOfLine(f,c);
                return ERROR_LINE;
            }
        } else if(!strcasecmp(keyword_value, "ALIAS")) {
            token = gettoken(f,c,&c);
            if(token == KEYWORD_TOKEN) {
                if(endOfLine(f,c))
                    return ALIAS_LINE;
                else
                    return ERROR_LINE;
            } else {
                skipEndOfLine(f,c);
                return ERROR_LINE;
            }
        } else if(!strcasecmp(keyword_value, "SIZE")) {
            token = gettoken(f,c,&c);
            if(token == NUMBER_TOKEN) {
                value1 = number_value;
                token = gettoken(f,c,&c);
                switch(token) {
                case NUMBER_TOKEN:
                    value2 = number_value;
                    return SIZE_LINE;
                case EOL_TOKEN:
                    value2=0;
                    return SIZE_LINE;
                default:
                    skipEndOfLine(f,c);
                    return ERROR_LINE;
                }
            } else {
                skipEndOfLine(f,c);
                return ERROR_LINE;
            }
        } else if(!strcasecmp(keyword_value, "FIRSTINDEX")) {
            token = gettoken(f,c,&c);
            if(token == NUMBER_TOKEN) {
                value1 = number_value;
                token = gettoken(f,c,&c);
                switch(token) {
                case NUMBER_TOKEN:
                    value2 = number_value;
                    return FIRSTINDEX_LINE;
                case EOL_TOKEN:
                    value2 = 0;
                    return FIRSTINDEX_LINE;
                default:
                    skipEndOfLine(f,c);
                    return ERROR_LINE;
                }
            } else {
                skipEndOfLine(f,c);
                return ERROR_LINE;
            }
        } else if(!strcasecmp(keyword_value, "STARTMAPPING")) {
            keyword_value[0] = 0;
            value1 = 0; value1 = 0;
            /* first a keyword */
            token = gettoken(f,c,&c);
            if(token != KEYWORD_TOKEN) {
                skipEndOfLine(f, c);
                return ERROR_LINE;
            }

            /* optional first integer */
            token = gettoken(f,c,&c);
            if(token == NUMBER_TOKEN) {
                value1 = number_value;
            } else if(token == EOL_TOKEN) {
                return STARTMAPPING_LINE;
            } else {
                skipEndOfLine(f, c);
                return ERROR_LINE;
            }

            /* optional second integer */
            token = gettoken(f,c,&c);
            if(token == NUMBER_TOKEN) {
                value2 = number_value;
            } else if(token == EOL_TOKEN) {
                return STARTMAPPING_LINE;
            } else {
                skipEndOfLine(f, c);
                return ERROR_LINE;
            }

            if(!endOfLine(f,c))
                return ERROR_LINE;
            else {
                return STARTMAPPING_LINE;
            }
        } else if(!strcasecmp(keyword_value, "UNDEFINE")) {
            /* first integer */
            token = gettoken(f,c,&c);
            if(token != NUMBER_TOKEN) {
                skipEndOfLine(f,c);
                return ERROR_LINE;
            }
            value1 = number_value;
            /* optional second integer */
            token = gettoken(f,c,&c);
            if(token == EOL_TOKEN) {
                value2 = value1;
                return CODE_UNDEFINE_LINE;
            } else if(token == NUMBER_TOKEN) {
                value2 = number_value;
                if(endOfLine(f,c)) {
                    return CODE_UNDEFINE_LINE;
                } else
                    return ERROR_LINE;
            } else {
                skipEndOfLine(f,c);
                return ERROR_LINE;
            }
        } else if(!strcasecmp(keyword_value, "ENDENCODING")) {
            if(endOfLine(f,c)) 
                return EOF_LINE;
            else
                return ERROR_LINE;
        } else if(!strcasecmp(keyword_value, "ENDMAPPING")) {
            if(endOfLine(f,c))
                return ENDMAPPING_LINE;
            else
                return ERROR_LINE;
        } else {
            skipEndOfLine(f,c);
            return ERROR_LINE;
        }
    default:
        return ERROR_LINE;
    }
}

static void 
install_mapping(FontEncPtr encoding, FontMapPtr mapping)
{
    FontMapPtr m;

    if(encoding->mappings == NULL)
        encoding->mappings = mapping;
    else {
        m = encoding->mappings;
        while(m->next != NULL)
            m = m->next;
        m->next = mapping;
    }
    mapping->next = NULL;
    mapping->encoding = encoding;
}

static int
setCode(unsigned from, unsigned to, unsigned row_size,
        unsigned *first, unsigned *last,
        unsigned *encsize, unsigned short **enc)
{
    unsigned index, i;
    unsigned short *newenc;

    if(from>0xFFFF)
        return 0;               /* success */

    if(row_size==0)
        index=from;
    else {
        if((value1 & 0xFF) >= row_size)
            return 0;           /* ignore out of range mappings */
        index = (from>>8) * row_size + (from&0xFF);
    }

    /* Optimize away useless identity mappings.  This is only expected
       to be useful with linear encodings. */
    if(index == to && (index < *first || index > *last))
        return 0;
    if(*encsize == 0) {
        *encsize = (index < 256) ? 256 : 0x10000;
        *enc = (unsigned short*)xalloc((*encsize) * sizeof(unsigned short));
        if(*enc == NULL) {
            *encsize = 0;
            return 1;
        }
    } else if(*encsize <= index) {
        *encsize = 0x10000;
        if((newenc = (unsigned short*)xrealloc(enc, *encsize))==NULL)
            return 1;
        *enc = newenc;
    }
    if(*first > *last) {
        *first = *last = index;
    }
    if(index < *first) {
        for(i = index; i < *first; i++)
            (*enc)[i] = i;
        *first = index;
    }
    if(index > *last) {
        for(i = *last + 1; i <= index; i++)
            (*enc)[i] = i;
        *last = index;
    }
    (*enc)[index] = to;
    return 0;
}

/* Parser.  If headerOnly is true, we're only interested in the
   data contained in the encoding file's header. */

/* As font encodings are currently never freed, the allocations done
   by this function are mostly its private business.  Note, however,
   that FontEncIdentify needs to free the header fields -- so if you
   change this function, you may need to change FontEncIdentify. */

/* I want a garbage collector. */

static FontEncPtr
parseEncodingFile(FontFilePtr f, int headerOnly)
{
    int line;

    unsigned short *enc=NULL;
    char **nam = NULL, **newnam;
    unsigned i, first = 0xFFFF, last=0, encsize=0, namsize=0;
    FontEncPtr encoding = NULL;
    FontMapPtr mapping = NULL;
    FontEncSimpleMapPtr sm;
    FontEncSimpleNamePtr sn;
    char *aliases[MAXALIASES];
    int numaliases=0;

#if 0
    /* GCC complains about unused labels.  Please fix GCC rather than
       obfuscating my code. */
  no_encoding:
#endif
    line = getnextline(f);
    switch(line) {
    case EOF_LINE:
        goto error;
    case STARTENCODING_LINE:
        encoding = (FontEncPtr)xalloc(sizeof(FontEncRec));
        if(encoding == NULL)
            goto error;
        encoding->name = (char*)xalloc(strlen(keyword_value)+1);
        if(encoding->name == NULL)
            goto error;
        strcpy(encoding->name, keyword_value);
        encoding->size = 256;
        encoding->row_size = 0;
        encoding->mappings = NULL;
        encoding->next = NULL;
        encoding->first = encoding->first_col=0;
        goto no_mapping;
    default:
        goto error;
    }

  no_mapping:
    line = getnextline(f);
    switch(line) {
    case EOF_LINE: goto done;
    case ALIAS_LINE:
        if(numaliases < MAXALIASES) {
            aliases[numaliases] = (char*)xalloc(strlen(keyword_value)+1);
            if(aliases[numaliases] == NULL)
                goto error;
            strcpy(aliases[numaliases], keyword_value);
            numaliases++;
        }
        goto no_mapping;
    case SIZE_LINE:
        encoding->size = value1;
        encoding->row_size = value2;
        goto no_mapping;
    case FIRSTINDEX_LINE:
        encoding->first = value1;
        encoding->first_col = value2;
        goto no_mapping;
    case STARTMAPPING_LINE:
        if(headerOnly)
            goto done;
        if(!strcasecmp(keyword_value, "unicode")) {
            mapping = (FontMapPtr)xalloc(sizeof(FontMapRec));
            if(mapping == NULL)
                goto error;
            mapping->type = FONT_ENCODING_UNICODE;
            mapping->pid = 0;
            mapping->eid = 0;
            mapping->recode = 0;
            mapping->name = 0;
            mapping->client_data = 0;
            mapping->next = 0;
            goto mapping;
        } else if(!strcasecmp(keyword_value, "cmap")) {
            mapping = (FontMapPtr)xalloc(sizeof(FontMapRec));
            if(mapping == NULL)
                goto error;
            mapping->type = FONT_ENCODING_TRUETYPE;
            mapping->pid = value1;
            mapping->eid = value2;
            mapping->recode = 0;
            mapping->name = 0;
            mapping->client_data = 0;
            mapping->next = 0;
            goto mapping;
        } else if(!strcasecmp(keyword_value, "postscript")) {
            mapping = (FontMapPtr)xalloc(sizeof(FontMapRec));
            if(mapping == NULL)
                goto error;
            mapping->type = FONT_ENCODING_POSTSCRIPT;
            mapping->pid = 0;
            mapping->eid = 0;
            mapping->recode = 0;
            mapping->name = 0;
            mapping->client_data = 0;
            mapping->next = 0;
            goto string_mapping;
        } else {                /* unknown mapping type -- ignore */
            goto skipmapping;
        }
        /* NOTREACHED */
        goto error;
    default: goto no_mapping;   /* ignore unknown lines */
    }

  skipmapping:
    line = getnextline(f);
    switch(line) {
    case ENDMAPPING_LINE:
        goto no_mapping;
    case EOF_LINE:
        goto error;
    default:
        goto skipmapping;
    }

  mapping:
    line = getnextline(f);
    switch(line) {
    case EOF_LINE: goto error;
    case ENDMAPPING_LINE:
        mapping->recode = FontEncSimpleRecode;
        mapping->name = FontEncUndefinedName;
        mapping->client_data = sm =
            (FontEncSimpleMapPtr)xalloc(sizeof(FontEncSimpleMapRec));
        if(sm == NULL)
            goto error;
        sm->row_size = encoding->row_size;
        if(first <= last) {
            sm->first = first;
            sm->len=last-first+1;
            sm->map = 
                (unsigned short*)xalloc(sm->len * sizeof(unsigned short));
            if(sm->map == NULL) {
                xfree(sm);
                mapping->client_data = sm = NULL;
                goto error;
            }
        } else {
            sm->first = 0;
            sm->len = 0;
            sm->map = 0;
        }
        for(i=0; i < sm->len; i++)
            sm -> map[i] = enc[first+i];
        install_mapping(encoding, mapping);
        mapping = 0;
        first = 0xFFFF; last=0;
        goto no_mapping;

    case CODE_LINE:
        if(setCode(value1, value2, encoding->row_size,
                   &first, &last, &encsize, &enc))
            goto error;
        goto mapping;

    case CODE_RANGE_LINE:
        if(value1 > 0x10000)
            value1 = 0x10000;
        if(value2 > 0x10000)
            value2 = 0x10000;
        if(value2 < value1)
            goto mapping;
        /* Do the last value first to avoid having to realloc() */
        if(setCode(value2, value3+(value2-value1), encoding->row_size,
                   &first, &last, &encsize, &enc))
            goto error;
        for(i=value1; i<value2; i++) {
            if(setCode(i, value3+(i-value1), encoding->row_size,
                       &first, &last, &encsize, &enc))
                goto error;
        }
        goto mapping;
    
    case CODE_UNDEFINE_LINE:
        if(value1 > 0x10000)
            value1 = 0x10000;
        if(value2 > 0x10000)
            value2 = 0x10000;
        if(value2 < value1)
            goto mapping;
        /* Do the last value first to avoid having to realloc() */
        if(setCode(value2, 0, encoding->row_size,
                   &first, &last, &encsize, &enc))
            goto error;
        for(i = value1; i < value2; i++) {
            if(setCode(i, 0, encoding->row_size,
                       &first, &last, &encsize, &enc))
                goto error;
        }
        goto mapping;

    default: goto mapping;      /* ignore unknown lines */
    }
    
  string_mapping:
    line = getnextline(f);
    switch(line) {
    case EOF_LINE: goto error;
    case ENDMAPPING_LINE:
        mapping->recode = FontEncUndefinedRecode;
        mapping->name = FontEncSimpleName;
        mapping->client_data = sn =
            (FontEncSimpleNamePtr)xalloc(sizeof(FontEncSimpleNameRec));
        if(sn == NULL)
            goto error;
        if(first > last) {
            xfree(sn);
            mapping->client_data = sn = NULL;
            goto error;
        }
        sn->first = first;
        sn->len = last - first + 1;
        sn->map = (char**)xalloc(sn->len*sizeof(char*));
        if(sn->map == NULL) {
            xfree(sn);
            mapping->client_data = sn = NULL;
            goto error;
        }
        for(i = 0; i < sn->len; i++)
            sn->map[i] = nam[first+i];
        install_mapping(encoding,mapping);
        mapping = 0;
        first = 0xFFFF; last=0;
        goto no_mapping;
    case NAME_LINE:
        if(value1 >= 0x10000) goto string_mapping;
        if(namsize == 0) {
            namsize = (value1) < 256 ? 256 : 0x10000;
            nam = (char**)xalloc(namsize * sizeof(char*));
            if(nam == NULL) {
                namsize=0;
                goto error;
            }
        } else if(namsize <= value1) {
            namsize = 0x10000;
            if((newnam = (char**)xrealloc(nam, namsize)) == NULL)
                goto error;
            nam = newnam;
        }
        if(first > last) {
            first = last = value1;
        }
        if(value1 < first) {
            for(i = value1; i < first; i++)
                nam[i] = NULL;
            first = value1;
        }
        if(value1 > last) {
            for(i=last+1; i <= value1; i++)
                nam[i]=NULL;
            last = value1;
        }
        nam[value1] = (char*)xalloc(strlen(keyword_value)+1);
        if(nam[value1] == NULL) {
            goto error;
        }
        strcpy(nam[value1], keyword_value);
        goto string_mapping;

    default: goto string_mapping; /* ignore unknown lines */
    }

  done:
    if(encsize) xfree(enc); encsize=0; enc = NULL;
    if(namsize) xfree(nam); namsize=0; nam = NULL; /* don't free entries! */

    encoding->aliases=NULL;
    if(numaliases) {
        encoding->aliases = (char**)xalloc((numaliases+1)*sizeof(char*));
        if(encoding->aliases == NULL)
            goto error;
        for(i=0; i<numaliases; i++)
            encoding->aliases[i] = aliases[i];
        encoding->aliases[numaliases]=NULL;
    }

    return encoding;

error:
    if(encsize) xfree(enc); encsize=0;
    if(namsize) {
        for(i = first; i <= last; i++)
            if(nam[i])
                xfree(nam[i]);
        xfree(nam);
        namsize = 0;
    }
    if(mapping) {
        if(mapping->client_data) xfree(mapping->client_data);
        xfree(mapping);
    }
    if(encoding) {
        if(encoding->name) xfree(encoding->name);
    for(mapping = encoding->mappings; mapping; mapping = mapping->next) {
        if(mapping->client_data) xfree(mapping->client_data);
        xfree(mapping);
    }
    xfree(encoding);
    }
    for(i = 0; i < numaliases; i++)
        xfree(aliases[i]);
    /* We don't need to free sn and sm as they handled locally in the body.*/
    return 0;
}

char*
FontEncDirectory()
{
    static char* dir = NULL;

    if(dir == NULL) {
        char *c = getenv("FONT_ENCODINGS_DIRECTORY");
        if(c) {
            dir = malloc(strlen(c) + 1);
            if(!dir)
                return NULL;
            strcpy(dir, c);
        } else {
            dir = FONT_ENCODINGS_DIRECTORY;
        }
    }
    return dir;
}

static void
parseFontFileName(const char *fontFileName, char *buf, char *dir)
{
    const char *p;
    char *q, *lastslash;
    
    for(p = fontFileName, q = dir, lastslash = NULL; *p; p++, q++) {
        *q = *p;
        if(*p == '/')
            lastslash = q+1;
    }
    
    if(!lastslash)
        lastslash = dir;
    
    *lastslash = '\0';

    if(buf && strlen(dir) + 14 < MAXFONTFILENAMELEN) {
        strcpy(buf, dir);
        strcat(buf, "encodings.dir");
    }
}

static FontEncPtr
FontEncReallyReallyLoad(const char *charset, 
                        const char *dirname, const char *dir)
{
    FontFilePtr f;
    FILE *file;
    FontEncPtr encoding;
    char file_name[MAXFONTFILENAMELEN], encoding_name[MAXFONTNAMELEN],
        buf[MAXFONTFILENAMELEN];
    int count, n;
    static char format[24] = "";
    
    /* As we don't really expect to open encodings that often, we don't
       take the trouble of caching encodings directories. */

    if((file = fopen(dirname, "r")) == NULL) {
        return NULL;
    }
    
    count = fscanf(file, "%d\n", &n);
    if(count == EOF || count != 1) {
        fclose(file);
        return NULL;
    }

    encoding = NULL;
    if (!format[0]) {
	sprintf(format, "%%%ds %%%d[^\n]\n", sizeof(encoding_name) - 1,
		sizeof(file_name) - 1);
    }
    for(;;) {
        count = fscanf(file, format, encoding_name, file_name);
        if(count == EOF)
            break;
        if(count != 2)
            break;

        if(!strcasecmp(encoding_name, charset)) {
            /* Found it */
            if(file_name[0] != '/') {
                if(strlen(dir) + strlen(file_name) >= MAXFONTFILENAMELEN)
                    return NULL;
                strcpy(buf, dir);
                strcat(buf, file_name);
            } else {
                strcpy(buf , file_name);
            }

            f = FontFileOpen(buf);
            if(f == NULL) {
                return NULL;
            }
            encoding = parseEncodingFile(f, 0);
            FontFileClose(f);
            break;
        }
    }

    fclose(file);

    return encoding;
}

/* Parser ntrypoint -- used by FontEncLoad */  
FontEncPtr
FontEncReallyLoad(const char *charset, const char *fontFileName)
{
    FontEncPtr encoding;
    char dir[MAXFONTFILENAMELEN], dirname[MAXFONTFILENAMELEN];
    char *d;

    if(fontFileName) {
        parseFontFileName(fontFileName, dirname, dir);
        encoding = FontEncReallyReallyLoad(charset, dirname, dir);
        if(encoding)
            return(encoding);
    }
  
    d = FontEncDirectory();
    if(d) {
        parseFontFileName(d, NULL, dir);
        encoding = FontEncReallyReallyLoad(charset, d, dir);
        return encoding;
    }
    
    return NULL;
}

/* Return a NULL-terminated array of encoding names.  Note that this
 * function has incestuous knowledge of the allocations done by
 * parseEncodingFile. */

char **
FontEncIdentify(const char *fileName)
{
    FontFilePtr f;
    FontEncPtr encoding;
    char **names, **name, **alias;
    int numaliases;
    
    if((f = FontFileOpen(fileName))==NULL) {
        return NULL;
    }
    encoding = parseEncodingFile(f, 1);
    FontFileClose(f);

    if(!encoding)
        return NULL;

    numaliases = 0;
    if(encoding->aliases)
        for(alias = encoding->aliases; *alias; alias++)
            numaliases++;

    names = (char**)xalloc((numaliases+2)*sizeof(char*));
    if(names == NULL) {
        if(encoding->aliases)
            xfree(encoding->aliases);
        xfree(encoding);
        return NULL;
    }

    name = names;
    *(name++) = encoding->name;
    if(numaliases > 0)
    for(alias = encoding->aliases; *alias; alias++, name++)
        *name = *alias;

    *name = 0;
    xfree(encoding->aliases);
    xfree(encoding);

    return names;
}