| 1 |
#include <stdbool.h> |
| 2 |
#include <stdint.h> |
| 3 |
#include <stdio.h> |
| 4 |
#include <stdlib.h> |
| 5 |
#include <string.h> |
| 6 |
|
| 7 |
#define CHR(ch1,ch2,ch3,ch4) (((ch1)<<24)|((ch2)<<16)|((ch3)<<8)|(ch4)) |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
static int getushort(FILE *ttf) { |
| 13 |
int ch1 = getc(ttf); |
| 14 |
int ch2 = getc(ttf); |
| 15 |
if ( ch2==EOF ) |
| 16 |
return( EOF ); |
| 17 |
return( (ch1<<8)|ch2 ); |
| 18 |
} |
| 19 |
|
| 20 |
static int32_t getlong(FILE *ttf) { |
| 21 |
int ch1 = getc(ttf); |
| 22 |
int ch2 = getc(ttf); |
| 23 |
int ch3 = getc(ttf); |
| 24 |
int ch4 = getc(ttf); |
| 25 |
if ( ch4==EOF ) |
| 26 |
return( EOF ); |
| 27 |
return( (ch1<<24)|(ch2<<16)|(ch3<<8)|ch4 ); |
| 28 |
} |
| 29 |
|
| 30 |
static int readttfheader(FILE *ttf, int search_tag) { |
| 31 |
int i, numtables; |
| 32 |
int tag; uint32_t v; |
| 33 |
|
| 34 |
|
| 35 |
v = getlong(ttf); |
| 36 |
if ( v==CHR('t','t','c','f')) { |
| 37 |
int ttccnt, *ttcoffsets; |
| 38 |
v = getlong(ttf); |
| 39 |
ttccnt = getlong(ttf); |
| 40 |
ttcoffsets = calloc(ttccnt,sizeof(int)); |
| 41 |
for ( i = 0; i<ttccnt; ++i ) { |
| 42 |
ttcoffsets[i] = getlong(ttf); |
| 43 |
} |
| 44 |
for ( i=0; i<ttccnt; ++i ) { |
| 45 |
fseek(ttf,ttcoffsets[i],SEEK_SET); |
| 46 |
if ( readttfheader(ttf,search_tag)) { |
| 47 |
free(ttcoffsets); |
| 48 |
return(true); |
| 49 |
} |
| 50 |
} |
| 51 |
free(ttcoffsets); |
| 52 |
return( false ); |
| 53 |
} |
| 54 |
|
| 55 |
numtables = getushort(ttf); |
| 56 |
getushort(ttf), |
| 57 |
getushort(ttf), |
| 58 |
getushort(ttf); |
| 59 |
if ( v==CHR('O','T','T','O')) |
| 60 |
; |
| 61 |
else if ( v==CHR('t','r','u','e')) |
| 62 |
; |
| 63 |
else if ( v==CHR('S','p','l','i')) |
| 64 |
return( false ); |
| 65 |
else if ( v==CHR('%','!','P','S')) |
| 66 |
return( false ); |
| 67 |
else if ( v>=0x80000000 && numtables==0 ) |
| 68 |
return( false ); |
| 69 |
else if ( (v>>24)==1 && ((v>>16)&0xff)==0 && ((v>>8)&0xff)==4 ) |
| 70 |
return( false ); |
| 71 |
else if ( v==CHR('t','y','p','1')) { |
| 72 |
} else { } |
| 73 |
; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
for ( i=0; i<numtables; ++i ) { |
| 80 |
tag = getlong(ttf); |
| 81 |
getlong(ttf); |
| 82 |
getlong(ttf); |
| 83 |
getlong(ttf); |
| 84 |
if ( tag == search_tag ) |
| 85 |
return( true ); |
| 86 |
} |
| 87 |
return( false ); |
| 88 |
} |
| 89 |
|
| 90 |
int main(int argc, char **argv) { |
| 91 |
int search_tag, i, any; |
| 92 |
char stag[4]; |
| 93 |
|
| 94 |
if ( argc<=1 ) { |
| 95 |
fprintf( stderr, "Usage: tag files\n" ); |
| 96 |
return( 2 ); |
| 97 |
} |
| 98 |
if ( strlen(argv[1])>4 || argv[1][0]=='\0' ) { |
| 99 |
fprintf( stderr, "Bad tag, must contain at least one and at most 4 characters.\n" ); |
| 100 |
return( 2 ); |
| 101 |
} |
| 102 |
memset(stag,' ',sizeof(stag)); |
| 103 |
stag[0] = argv[1][0]; |
| 104 |
if ( argv[1][1]!='\0' ) { |
| 105 |
stag[1] = argv[1][1]; |
| 106 |
if ( argv[1][2]!='\0' ) { |
| 107 |
stag[2] = argv[1][2]; |
| 108 |
if ( argv[1][3]!='\0' ) |
| 109 |
stag[3] = argv[1][3]; |
| 110 |
} |
| 111 |
} |
| 112 |
search_tag = CHR(stag[0],stag[1],stag[2],stag[3]); |
| 113 |
|
| 114 |
any = false; |
| 115 |
for ( i=2; i<argc; ++i ) { |
| 116 |
FILE *ttf = fopen( argv[i],"rb"); |
| 117 |
if ( ttf==NULL ) |
| 118 |
fprintf( stderr, "Failed to open %s\n", argv[i]); |
| 119 |
else { |
| 120 |
if ( readttfheader(ttf,search_tag)) { |
| 121 |
printf ( "%s contains table %s\n", argv[i], argv[1]); |
| 122 |
any = true; |
| 123 |
} |
| 124 |
fclose(ttf); |
| 125 |
} |
| 126 |
} |
| 127 |
return( !any ); |
| 128 |
} |
| 129 |
|