-
Notifications
You must be signed in to change notification settings - Fork 169
/
Copy pathmain.c
1247 lines (1077 loc) · 38.6 KB
/
main.c
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
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright 2014 Adobe Systems Incorporated (http://www.adobe.com/). All Rights Reserved.
This software is licensed as OpenSource, under the Apache License, Version 2.0.
This license is available at: http://opensource.org/licenses/Apache-2.0. */
/* sfnt table edit utility. */
#ifdef __MWERKS__
#include <SIOUX.h>
#include <console.h>
#include <unix.h>
#endif
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <signal.h>
#include "Eglobal.h"
#include "Efile.h"
#include "Esys.h"
#include "otter.h"
#include "otftableeditor.h"
#include "setjmp.h"
#define MAX_ARGS 200
jmp_buf mark;
#if SUNOS
/* Fixup SUNOS libc */
#include <sys/unistd.h> /* For SEEK_* macros */
#ifndef FILENAME_MAX
/* SunOS doesn't define this ANSI macro anywhere therefore derive it */
#include <sys/param.h>
#define FILENAME_MAX MAXPATHLEN
#endif /* FILENAME_MAX */
#endif /* SUNOS */
#define VERSION "1.4"
/* Data type sizes (bytes) */
#define uint16_ 2
#define int16_ 2
#define uint32_ 4
#define int32_ 4
/* Tag support */
typedef Card32 Tag;
#define TAG(a, b, c, d) ((Tag)(a) << 24 | (Tag)(b) << 16 | (c) << 8 | (d))
#define TAG_ARG(t) (char)((t) >> 24 & 0xff), (char)((t) >> 16 & 0xff), \
(char)((t) >> 8 & 0xff), (char)((t)&0xff)
typedef struct /* sfnt table directory entry */
{
Card32 tag;
Card32 checksum;
Card32 offset;
Card32 length;
Card16 flags; /* Option flags */
#define TBL_SRC (1 << 10) /* Flags table in source sfnt */
#define TBL_DST (1 << 11) /* Flags table in destination sfnt */
Int16 order; /* Table ordering */
char *xfilename; /* Extract filename */
char *afilename; /* Add filename */
} Table;
#define ENTRY_SIZE (uint32_ * 4) /* Size of written fields */
#define MAX_TABLES 60
typedef struct /* sfnt header */
{
Fixed version;
Card16 numTables;
Card16 searchRange;
Card16 entrySelector;
Card16 rangeShift;
Table directory[MAX_TABLES]; /* [numTables] */
} sfntHdr;
#define DIR_HDR_SIZE (int32_ + uint16_ * 4) /* Size of written fields */
/* head.checkSumAdjustment offset within head table */
#define HEAD_ADJUST_OFFSET (2 * int32_)
/* Recommended table data order for OTF fonts */
static Tag otfOrder[] =
{
TAG('h', 'e', 'a', 'd'),
TAG('h', 'h', 'e', 'a'),
TAG('m', 'a', 'x', 'p'),
TAG('O', 'S', '/', '2'),
TAG('n', 'a', 'm', 'e'),
TAG('c', 'm', 'a', 'p'),
TAG('p', 'o', 's', 't'),
TAG('f', 'v', 'a', 'r'),
TAG('M', 'M', 'S', 'D'),
TAG('C', 'F', 'F', ' '),
0, /* Sentinel */
};
/* Recommended table data order for TTF fonts */
static Tag ttfOrder[] =
{
TAG('h', 'e', 'a', 'd'),
TAG('h', 'h', 'e', 'a'),
TAG('m', 'a', 'x', 'p'),
TAG('O', 'S', '/', '2'),
TAG('h', 'm', 't', 'x'),
TAG('L', 'T', 'S', 'H'),
TAG('V', 'D', 'M', 'X'),
TAG('h', 'd', 'm', 'x'),
TAG('c', 'm', 'a', 'p'),
TAG('f', 'p', 'g', 'm'),
TAG('p', 'r', 'e', 'p'),
TAG('c', 'v', 't', ' '),
TAG('l', 'o', 'c', 'a'),
TAG('g', 'l', 'y', 'f'),
TAG('k', 'e', 'r', 'n'),
TAG('n', 'a', 'm', 'e'),
TAG('p', 'o', 's', 't'),
TAG('g', 'a', 's', 'p'),
TAG('P', 'C', 'L', 'T'),
TAG('D', 'S', 'I', 'G'),
0, /* Sentinel */
};
static sfntHdr sfnt;
static File srcfile;
static File dstfile;
char *tmpname = "sfntedit.tmp"; /* Temporary filename */
#define BACKUPNAME "sfntedit.BAK"
static long options; /* Options seen */
#define OPT_EXTRACT (1 << 0)
#define OPT_DELETE (1 << 1)
#define OPT_ADD (1 << 2)
#define OPT_LIST (1 << 3)
#define OPT_CHECK (1 << 4)
#define OPT_FIX (1 << 5)
volatile int doingScripting = 0;
int foundXswitch = 0;
static char scriptfilename[256];
char *sourcepath = "";
typedef struct _cmdlinetype {
da_DCL(char *, args); /* arg list */
} cmdlinetype;
static struct
{
char *buf; /* input buffer */
da_DCL(cmdlinetype, cmdline);
} script;
char *MakeFullPath(char *source) {
char *dest;
dest = (char *)malloc(256);
if (sourcepath[0] == '\0' || strchr(source, '\\') != NULL)
sprintf(dest, "%s", source);
else
sprintf(dest, "%s\\%s", sourcepath, source);
return dest;
}
/* ----------------------------- Error Handling ---------------------------- */
/* Cleanup on abnormal program termination */
static void cleanup(int code) {
fileClose(&srcfile);
if (dstfile.fp != NULL) {
fileClose(&dstfile);
(void)remove(dstfile.name);
}
fclose(stderr);
longjmp(mark, 1);
}
/* ----------------------------- Usage and Help ---------------------------- */
/* Print usage information */
static void printUsage(void) {
fprintf(stdout,
"Usage:\n"
" %s [options] <srcfile> [<dstfile>]\n"
"OR: %s -X <scriptfile>\n\n"
"Options:\n"
" -x <tag>[=<file>][,<tag>[=<file>]]+ extract table to file\n"
" -d <tag>[,<tag>]+ delete table\n"
" -a <tag>=<file>[,<tag>=<file>]+ add (or replace) table\n"
" -l list sfnt directory (default)\n"
" -c check checksums\n"
" -f fix checksums (implies -c)\n"
" -u print usage\n"
" -h print help\n"
" -X execute command-lines from <scriptfile> [default: sfntedit.scr]\n"
"Build:\n"
" Version: %s\n",
global.progname,
global.progname,
VERSION);
}
/* Show usage information */
static void showUsage(void) {
printUsage();
}
/* Show usage and help information */
static void showHelp(void) {
printUsage();
fprintf(stdout,
"Notes:\n"
" This program supports table-editing, listing, and checksumming options\n"
"on sfnt-formatted files such as OpenType Format (OTF) or TrueType. The\n"
"mandatory source file is specified as an argument to the program. An\n"
"optional destination file may also be specified which receives the edited\n"
"data otherwise the source data is edited in-place thus modifying the source\n");
fprintf(stdout,
"file. In-place editing is achieved by the use of a temporary file called\n"
"%s that is created in the directory of execution (requiring you\n"
"to have write permission to that directory).\n"
" The target table of an editing option (-x, -d, and -a) is specified\n"
"with a table tag argument that is nominally 4 characters long. If fewer\n"
"than 4 characters are specified the tag is padded with spaces (more than 4\n",
tmpname);
fprintf(stdout,
"characters is a fatal error). Multiple tables may be specified as a single\n"
"argument composed from a comma-separated list of tags.\n"
" The extract option (-x) copies the table data into a file whose default\n"
"name is the concatenation of the source filename (less its .otf or .ttf\n"
"extension), a period character (.), and the table tag. If the tag contains\n"
"non-alphanumeric characters they are replaced by underscore characters (_)\n");
fprintf(stdout,
"and finally trailing underscores are removed. The default filename may be\n"
"overridden by appending an equals character (=) followed by an alternate\n"
"filename to the table tag argument. The delete option (-d) deletes a table.\n"
"Unlike the -x option no files may be specified in the table tag list. The\n"
"add option (-a) adds a table or replaces one if the table already exists.\n"
"The source file containing the table data is specified by appending an\n");
fprintf(stdout,
"equals character (=) followed by a filename to the table tag.\n"
" The 3 editing options may be specified together as acting on the same\n"
"table. In such cases the -x option is applied before the -d option which is\n"
"applied before the -a option. (The -d option applied to the same table as a\n"
"subsequent -a option is permitted but redundant.) The -d and -a options\n"
"change the contents of the sfnt and cause the table checksums and the head\n");
fprintf(stdout,
"table's checksum adjustment field to be recomputed.\n"
" The list option (-l) simply lists the contents of the sfnt table\n"
"directory. This is the default action if no other options are specified.\n"
"The check checksum option (-c) performs a check of all the table checksums\n"
"and the head table's checksum adjustment field and reports any errors. The\n"
"fix checksum option (-f) fixes any checksum errors.\n");
fprintf(stdout,
" The -d, -a, and -f options create a new sfnt file by copying tables\n"
"from the source file to the destination file. The tables are copied in the\n"
"order recommended in the OpenType specification. A side effect of copying\n"
"is that all table information including checksums and sfnt search fields\n"
"is recalculated.\n"
"Examples:\n");
fprintf(stdout,
"o Extract GPOS and GSUB tables to files minion.GPOS and minion.GSUB.\n"
" sfntedit -x GPOS,GSUB minion.otf\n"
"o Add tables extracted previously to different font.\n"
" sfntedit -a GPOS=minion.GPOS,GSUB=minion.GSUB minion.ttf\n"
"o Delete temporary tables from font.\n"
" sfntedit -d TR01,TR02,TR03 pala.ttf\n"
"o Copy font to new file fixing checksums and reordering tables.\n"
" sfntedit -f helv.ttf newhelv.ttf\n");
}
static void makeArgs(char *filename) {
int state;
long i;
long length;
File file;
char *start = NULL; /* Suppress optimizer warning */
cmdlinetype *cmdl;
/* Read whole file into buffer */
fileOpenRead(filename, &file);
length = fileLength(&file);
if (length < 1)
fatal(SFED_MSG_BADSCRIPTFILE, filename);
script.buf = memNew(length + 2);
fileSeek(&file, 0, 0);
fileReadN(&file, length, script.buf);
fileClose(&file);
script.buf[length] = '\n'; /* Ensure termination */
script.buf[length + 1] = '\0'; /* Ensure termination */
/* Parse buffer into args */
state = 0;
da_INIT(script.cmdline, 10, 10);
cmdl = da_NEXT(script.cmdline);
da_INIT(cmdl->args, 10, 10);
*da_NEXT(cmdl->args) = global.progname;
for (i = 0; i < length + 1; i++) {
char c = script.buf[i];
switch (state) {
case 0:
switch ((int)c) {
case '\n':
case '\r':
cmdl = da_NEXT(script.cmdline);
da_INIT(cmdl->args, 10, 10);
*da_NEXT(cmdl->args) = global.progname;
break;
case '\f':
case '\t':
break;
case ' ':
break;
case '#':
state = 1;
break;
case '"':
start = &script.buf[i + 1];
state = 2;
break;
default:
start = &script.buf[i];
state = 3;
break;
}
break;
case 1: /* Comment */
if (c == '\n' || c == '\r')
state = 0;
break;
case 2: /* Quoted string */
if (c == '"') {
script.buf[i] = '\0'; /* Terminate string */
*da_NEXT(cmdl->args) = start;
state = 0;
}
break;
case 3: /* Space-delimited string */
if (isspace((int)c)) {
script.buf[i] = '\0'; /* Terminate string */
*da_NEXT(cmdl->args) = start;
state = 0;
if ((c == '\n') || (c == '\r')) {
cmdl = da_NEXT(script.cmdline);
da_INIT(cmdl->args, 10, 10);
*da_NEXT(cmdl->args) = global.progname;
}
}
break;
}
}
}
/* ---------------------------- Argument Parsing --------------------------- */
/* Find table with given tag and return its index if found otherwise return
index of insert position */
static int getTableIndex(Tag tag) {
int i;
for (i = 0; i < sfnt.numTables; i++)
if (tag <= sfnt.directory[i].tag)
break;
return i;
}
/* Insert table into sfnt directory in tag order */
static Table *insertTable(Tag tag) {
int index = getTableIndex(tag);
Table *tbl = &sfnt.directory[index];
if (tbl->tag != tag) {
/* Insert table */
int i;
if (++sfnt.numTables > MAX_TABLES)
fatal(SFED_MSG_TABLELIMIT, MAX_TABLES);
for (i = sfnt.numTables - 2; i >= index; i--)
sfnt.directory[i + 1] = sfnt.directory[i];
tbl->flags = 0;
tbl->xfilename = NULL;
tbl->afilename = NULL;
}
return tbl;
}
/* Parse table tag list */
static void parseTagList(char *arg, int option, int flag) {
char *p = arg;
for (p = strtok(arg, ","); p != NULL; p = strtok(NULL, ",")) {
int i;
int taglen;
Tag tag;
char *filename;
Table *tbl;
/* Find filename separator and set to null if present */
filename = strchr(p, '=');
if (filename != NULL) {
*filename++ = '\0';
if (strlen(filename) == 0)
fatal(SFED_MSG_BADFNAMOPT, option);
}
/* Validate tag length */
taglen = strlen(p);
if (taglen == 0 || taglen > 4)
fatal(SFED_MSG_BADTAGOPT, p, option);
/* Make tag */
tag = 0;
for (i = 0; i < taglen; i++)
tag = tag << 8 | *p++;
/* Pad tag with space */
for (; i < 4; i++)
tag = tag << 8 | ' ';
/* Add option */
if (option == 'd' && filename != NULL) {
warning(SFED_MSG_FILENIGNORED);
filename = NULL;
}
if (option == 'a' && filename == NULL)
fatal(SFED_MSG_FILENMISSING);
tbl = insertTable(tag);
/* Check tag not already in list */
if (tbl->flags & flag)
warning(SFED_MSG_DUPTAGOPT, TAG_ARG(tag), option);
/* Save values */
tbl->tag = tag;
tbl->flags |= flag;
if (option == 'x') {
if (sourcepath[0] != '\0' && filename != NULL)
tbl->xfilename = MakeFullPath(filename);
else
tbl->xfilename = filename;
} else if (option == 'a') {
if (sourcepath[0] != '\0' && filename != NULL)
tbl->afilename = MakeFullPath(filename);
else
tbl->afilename = filename;
}
}
}
/* Count bits in word */
static int countbits(long value) {
int count;
for (count = 0; value; count++)
value &= value - 1;
return count;
}
/* Process options */
static int parseArgs(int argc, char *argv[]) {
int i;
options = 0; /* reset options */
for (i = 0; i < argc; i++) {
char *arg = argv[i];
int argsleft = argc - i - 1;
switch (arg[0]) {
case '-':
switch (arg[1]) {
case 'X': /* script file to execute */
foundXswitch = 1;
if ((argsleft > 0) && argv[i + 1][0] != '-') {
strcpy(scriptfilename, argv[++i]);
if (doingScripting) /* disallow nesting */
{
foundXswitch = 0;
scriptfilename[0] = '\0';
}
}
break;
case 'x': /* Extract table */
if (arg[2] != '\0')
fatal(SFED_MSG_BADOPTION, arg);
else if (argsleft == 0)
fatal(SFED_MSG_NOARG, arg[1]);
parseTagList(argv[++i], 'x', OPT_EXTRACT);
options |= OPT_EXTRACT;
break;
case 'd': /* Delete table */
if (arg[2] != '\0')
fatal(SFED_MSG_BADOPTION, arg);
else if (argsleft == 0)
fatal(SFED_MSG_NOARG, arg[1]);
parseTagList(argv[++i], 'd', OPT_DELETE);
options |= OPT_DELETE;
break;
case 'a': /* Add table */
if (arg[2] != '\0')
fatal(SFED_MSG_BADOPTION, arg);
else if (argsleft == 0)
fatal(SFED_MSG_NOARG, arg[1]);
parseTagList(argv[++i], 'a', OPT_ADD);
options |= OPT_ADD;
break;
case 'l': /* List sfnt table directory */
options |= OPT_LIST;
break;
case 'c': /* Check checksum */
options |= OPT_CHECK;
break;
case 'f': /* Fix checksum */
options |= OPT_FIX;
break;
case 'u':
showUsage();
exit(0);
case 'h':
showHelp();
exit(0);
default:
fatal(SFED_MSG_UNRECOGOPT, arg);
}
break;
default: /* Non-option arg is taken to be filename */
{
int writefile = options & (OPT_DELETE | OPT_ADD | OPT_FIX);
/* Validate options */
if (options & (OPT_LIST | OPT_CHECK | OPT_FIX) &&
countbits(options) > 1)
fatal(SFED_MSG_OPTCONFLICT);
if (options == 0)
options |= OPT_LIST; /* No options set; apply default */
/* Set up filenames */
if (sourcepath[0] != '\0') {
srcfile.name = MakeFullPath(arg);
} else {
srcfile.name = arg;
}
if (argsleft == 0) {
if (writefile) { /* Use temporary file; but check doesn't exist first */
char *fulltemp;
fulltemp = MakeFullPath(tmpname);
dstfile.name = fulltemp;
}
} else if (argsleft == 1) {
if (writefile) {
if (sourcepath[0] != '\0') {
dstfile.name = MakeFullPath(argv[i + 1]);
} else {
dstfile.name = argv[i + 1];
}
} else
warning(SFED_MSG_OUTFIGNORED);
} else
fatal(SFED_MSG_TOOMANYFILES);
}
return (argsleft + 1);
}
}
/* No files provided */
if ((options & (OPT_DELETE | OPT_ADD | OPT_FIX)) &&
(dstfile.name == NULL)) {
dstfile.name = tmpname;
}
return 0;
}
/* ------------------------- sfnt Table Processing ------------------------- */
/* Read sfnt header */
static void sfntReadHdr(void) {
int i;
Int16 numTables = 0;
/* Read and validate version */
fileReadObject(&srcfile, 4, &sfnt.version);
switch (sfnt.version) {
case 0x00010000: /* 1.0 */
case TAG('t', 'r', 'u', 'e'):
case TAG('t', 'y', 'p', '1'):
case TAG('O', 'T', 'T', 'O'):
case TAG('b', 'i', 't', 's'):
break;
case TAG('t', 't', 'c', 'f'):
default:
fileClose(&srcfile);
fatal(SFED_MSG_UNRECFILE, srcfile.name);
}
/* Read rest of header */
fileReadObject(&srcfile, 2, &numTables);
fileReadObject(&srcfile, 2, &sfnt.searchRange);
fileReadObject(&srcfile, 2, &sfnt.entrySelector);
fileReadObject(&srcfile, 2, &sfnt.rangeShift);
for (i = 0; i < numTables; i++) {
Tag tag;
Table *tbl;
fileReadObject(&srcfile, sizeof(Tag), &tag);
tbl = insertTable(tag);
tbl->tag = tag;
fileReadObject(&srcfile, 4, &tbl->checksum);
fileReadObject(&srcfile, 4, &tbl->offset);
fileReadObject(&srcfile, 4, &tbl->length);
tbl->flags |= TBL_SRC;
}
/* Check options have corresponding tables */
for (i = 0; i < sfnt.numTables; i++) {
Table *tbl = &sfnt.directory[i];
if (tbl->flags & (OPT_EXTRACT | OPT_DELETE) && !(tbl->flags & TBL_SRC))
fatal(SFED_MSG_TABLEMISSING, TAG_ARG(tbl->tag));
}
}
/* Dump sfnt header */
static void sfntDumpHdr(void) {
int i;
fprintf(stdout, "--- sfnt header [%s]\n", srcfile.name);
if (sfnt.version == 0x00010000)
fprintf(stdout, "version =1.0 (00010000)\n");
else
fprintf(stdout, "version =%c%c%c%c (%08x)\n",
TAG_ARG(sfnt.version), sfnt.version);
fprintf(stdout, "numTables =%hu\n", sfnt.numTables);
fprintf(stdout, "searchRange =%hu\n", sfnt.searchRange);
fprintf(stdout, "entrySelector=%hu\n", sfnt.entrySelector);
fprintf(stdout, "rangeShift =%hu\n", sfnt.rangeShift);
fprintf(stdout, "--- table directory [index]={tag,checksum,offset,length}\n");
for (i = 0; i < sfnt.numTables; i++) {
Table *tbl = &sfnt.directory[i];
fprintf(stdout, "[%2d]={%c%c%c%c,%08x,%08x,%08x}\n", i,
TAG_ARG(tbl->tag), tbl->checksum, tbl->offset, tbl->length);
}
}
/* Calculate values of binary search table parameters */
static void calcSearchParams(unsigned nUnits,
Card16 *searchRange,
Card16 *entrySelector,
Card16 *rangeShift) {
unsigned log2;
unsigned pwr2;
pwr2 = 2;
for (log2 = 0; pwr2 <= nUnits; log2++)
pwr2 *= 2;
pwr2 /= 2;
*searchRange = ENTRY_SIZE * pwr2;
*entrySelector = log2;
*rangeShift = ENTRY_SIZE * (nUnits - pwr2);
}
/* Check that the table checksums and the head adjustment checksums are
calculated correctly. Also validate the sfnt search fields */
static void checkChecksums(void) {
int i;
long nLongs;
int fail = 0;
Card16 searchRange;
Card16 entrySelector;
Card16 rangeShift;
Card32 checkSumAdjustment;
Card32 totalsum = 0;
/* Validate sfnt search fields */
calcSearchParams(sfnt.numTables, &searchRange, &entrySelector, &rangeShift);
if (sfnt.searchRange != searchRange) {
warning(SFED_MSG_BADSEARCH, sfnt.searchRange, searchRange);
fail = 1;
}
if (sfnt.entrySelector != entrySelector) {
warning(SFED_MSG_BADSELECT, sfnt.entrySelector, entrySelector);
fail = 1;
}
if (sfnt.rangeShift != rangeShift) {
warning(SFED_MSG_BADRSHIFT, sfnt.rangeShift, rangeShift);
fail = 1;
}
/* Read directory header */
fileSeek(&srcfile, 0, SEEK_SET);
nLongs = (DIR_HDR_SIZE + ENTRY_SIZE * sfnt.numTables) / 4;
while (nLongs--) {
Card32 amt;
fileReadObject(&srcfile, 4, &amt);
totalsum += amt;
}
for (i = 0; i < sfnt.numTables; i++) {
Card32 checksum = 0;
Card32 amt;
Table *entry = &sfnt.directory[i];
fileSeek(&srcfile, entry->offset, SEEK_SET);
nLongs = (entry->length + 3) / 4;
while (nLongs--) {
fileReadObject(&srcfile, 4, &amt);
checksum += amt;
}
if (entry->tag == TAG('h', 'e', 'a', 'd')) {
/* Adjust sum to ignore head.checkSumAdjustment field */
fileSeek(&srcfile, entry->offset + HEAD_ADJUST_OFFSET, SEEK_SET);
fileReadObject(&srcfile, 4, &checkSumAdjustment);
checksum -= checkSumAdjustment;
}
if (entry->checksum != checksum) {
warning(SFED_MSG_BADCHECKSUM, TAG_ARG(entry->tag), entry->checksum, checksum);
fail = 1;
}
totalsum += checksum;
}
totalsum = 0xb1b0afba - totalsum;
if (!fail && totalsum != checkSumAdjustment) {
warning(SFED_MSG_BADCKSUMADJ, checkSumAdjustment, totalsum);
fail = 1;
}
message(fail ? SFED_MSG_CHECKFAILED : SFED_MSG_CHECKPASSED, srcfile.name);
}
/* Return tail component of path */
static char *tail(char *path) {
char *p;
p = strrchr(path, '/');
if (p == NULL)
p = strrchr(path, '\\');
p = strrchr(path, '\\');
return (p == NULL) ? path : p + 1;
}
/* Make extract filename from option filename or src filename plus table tag */
static char *makexFilename(Table *tbl) {
if (tbl->xfilename != NULL)
return tbl->xfilename;
else {
static char filename[FILENAME_MAX];
int i;
char *p;
char *q;
char tag[5];
tag[0] = (char)(tbl->tag >> 24);
tag[1] = (char)(tbl->tag >> 16);
tag[2] = (char)(tbl->tag >> 8);
tag[3] = (char)(tbl->tag);
tag[4] = '\0';
/* Replace potentially troublesome filename chars by underscores */
for (i = 0; i < 4; i++)
if (!isalnum(tag[i]))
tag[i] = '_';
/* Trim trailing underscores */
for (i = 3; i > 0; i--)
if (tag[i] == '_')
tag[i] = '\0';
else
break;
p = tail(srcfile.name);
q = strstr(p, ".otf");
if (q == NULL)
q = strstr(p, ".ttf");
if (q == NULL)
sprintf(filename, "%s.%s", p, tag);
else
sprintf(filename, "%.*s.%s", (int)(q - p), p, tag);
return filename;
}
}
/* Extract (-x) tables from source file */
static void extractTables(void) {
int i;
for (i = 0; i < sfnt.numTables; i++) {
File file;
char *filename;
Table *tbl = &sfnt.directory[i];
if (!(tbl->flags & OPT_EXTRACT))
continue;
filename = makexFilename(tbl);
fileOpenWrite(filename, &file);
fileSeek(&srcfile, tbl->offset, SEEK_SET);
fileCopy(&srcfile, &file, tbl->length);
fileClose(&file);
}
}
/* Compare order fields */
static int cmpOrder(const void *first, const void *second) {
const Table *a = first;
const Table *b = second;
if (a->order < b->order)
return -1;
else if (a->order > b->order)
return 1;
else
return 0;
}
/* Copy table and compute its checksum */
static Card32 tableCopy(File *src, File *dst, long offset, long length) {
int i;
Card32 value;
Card32 checksum = 0;
fileSeek(src, offset, SEEK_SET);
for (; length > 3; length -= 4) {
fileReadObject(src, 4, &value);
fileWriteObject(dst, 4, value);
checksum += value;
}
if (length == 0)
return checksum;
/* Read remaining bytes and pad to 4-byte boundary */
value = 0;
for (i = 0; i < length; i++) {
Card8 b;
fileReadN(src, 1, &b);
value = value << 8 | b;
}
value <<= (4 - length) * 8;
fileWriteObject(dst, 4, value);
return checksum + value;
}
/* Add table from file */
static Card32 addTable(Table *tbl, Card32 *length) {
File file;
Card32 checksum;
fileOpenRead(tbl->afilename, &file);
fileSeek(&file, 0, SEEK_END);
*length = fileTell(&file);
checksum = tableCopy(&file, &dstfile, 0, *length);
fileClose(&file);
return checksum;
}
/* Compare tag fields */
static int cmpTags(const void *first, const void *second) {
const Table *a = first;
const Table *b = second;
if (a->tag < b->tag)
return -1;
else if (a->tag > b->tag)
return 1;
else
return 0;
}
/* Copy tables from source file to destination file applying (-d, -a, and -f)
options */
static void sfntCopy(void) {
int i;
int nLongs;
Tag *tags;
Card16 numDstTables;
Card32 checksum;
Card32 offset;
Card32 length;
Card32 adjustOff;
Card32 totalsum;
int headSeen = 0;
char outputfilename[FILENAME_MAX];
char *dstfilename = dstfile.name;
FILE *f;
strcpy(outputfilename, dstfile.name);
f = freopen(outputfilename, "r+b", dstfile.fp);
if (f == NULL) {
fatal(SFED_MSG_sysFERRORSTR, strerror(errno), dstfile.name);
}
/* Count destination tables */
numDstTables = 0;
for (i = 0; i < sfnt.numTables; i++)
if (!(sfnt.directory[i].flags & OPT_DELETE))
numDstTables++;
/* Assign table order */
tags = (sfnt.version == TAG('O', 'T', 'T', 'O')) ? otfOrder : ttfOrder;
for (i = 0; i < sfnt.numTables; i++) {
Tag *tagp = tags;
Table *tbl = &sfnt.directory[i];
for (tagp = tags; *tagp != 0; tagp++)
if (*tagp == tbl->tag) {
tbl->order = tagp - tags;
goto found;
}
/* Tag not found; sort to end */
tbl->order = 100;
found:;
}
/* Sort tables into recommended order */
qsort(sfnt.directory, sfnt.numTables, sizeof(Table), cmpOrder);
/* Skip directory */
fileSeek(&dstfile, DIR_HDR_SIZE + ENTRY_SIZE * numDstTables, SEEK_SET);
totalsum = 0;
for (i = 0; i < sfnt.numTables; i++) {
Table *tbl = &sfnt.directory[i];
offset = fileTell(&dstfile);
if (tbl->flags & OPT_ADD)
checksum = addTable(tbl, &length);
else {
if (tbl->flags & OPT_DELETE)
continue; /* Skip deleted table */
else {
length = tbl->length;
checksum =
tableCopy(&srcfile, &dstfile, tbl->offset, length);
}
}
if (tbl->tag == TAG('h', 'e', 'a', 'd')) {
Card32 b;
/* Adjust sum to ignore head.checkSumAdjustment field */
adjustOff = offset + HEAD_ADJUST_OFFSET;
fileSeek(&dstfile, adjustOff, SEEK_SET);
fileReadObject(&dstfile, 4, &b);
checksum -= b;
fileSeek(&dstfile, 0, SEEK_END);
headSeen = 1;
}
/* Update table entry */
tbl->checksum = checksum;
tbl->offset = offset;
tbl->length = length;
tbl->flags |= TBL_DST;
totalsum += checksum;
}
/* Initialize sfnt header */
calcSearchParams(numDstTables, &sfnt.searchRange,
&sfnt.entrySelector, &sfnt.rangeShift);
/* Write sfnt header */
fileSeek(&dstfile, 0, SEEK_SET);
fileWriteObject(&dstfile, 4, sfnt.version);
fileWriteObject(&dstfile, 2, numDstTables);
fileWriteObject(&dstfile, 2, sfnt.searchRange);
fileWriteObject(&dstfile, 2, sfnt.entrySelector);
fileWriteObject(&dstfile, 2, sfnt.rangeShift);
/* Sort directory into tag order */
qsort(sfnt.directory, sfnt.numTables, sizeof(Table), cmpTags);
/* Write sfnt directory */
for (i = 0; i < sfnt.numTables; i++) {
Table *tbl = &sfnt.directory[i];
if (tbl->flags & TBL_DST) {
fileWriteObject(&dstfile, 4, tbl->tag);