| 1 |
|
| 2 |
#include <stdio.h> |
| 3 |
#include <string.h> |
| 4 |
#include <stdlib.h> |
| 5 |
#include <zlib.h> |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#define CHR(ch1,ch2,ch3,ch4) (((ch1)<<24)|((ch2)<<16)|((ch3)<<8)|(ch4)) |
| 12 |
|
| 13 |
static int getushort(FILE *ttf) { |
| 14 |
int ch1 = getc(ttf); |
| 15 |
int ch2 = getc(ttf); |
| 16 |
if ( ch2==EOF ) |
| 17 |
return( EOF ); |
| 18 |
return( (ch1<<8)|ch2 ); |
| 19 |
} |
| 20 |
|
| 21 |
static int getlong(FILE *ttf) { |
| 22 |
int ch1 = getc(ttf); |
| 23 |
int ch2 = getc(ttf); |
| 24 |
int ch3 = getc(ttf); |
| 25 |
int ch4 = getc(ttf); |
| 26 |
if ( ch4==EOF ) |
| 27 |
return( EOF ); |
| 28 |
return( (ch1<<24)|(ch2<<16)|(ch3<<8)|ch4 ); |
| 29 |
} |
| 30 |
|
| 31 |
static void putshort(FILE *file,int sval) { |
| 32 |
putc((sval>>8)&0xff,file); |
| 33 |
putc(sval&0xff,file); |
| 34 |
} |
| 35 |
|
| 36 |
static void putlong(FILE *file,int val) { |
| 37 |
putc((val>>24)&0xff,file); |
| 38 |
putc((val>>16)&0xff,file); |
| 39 |
putc((val>>8)&0xff,file); |
| 40 |
putc(val&0xff,file); |
| 41 |
} |
| 42 |
|
| 43 |
static int filecheck(FILE *file) { |
| 44 |
unsigned int sum = 0, chunk; |
| 45 |
|
| 46 |
rewind(file); |
| 47 |
while ( 1 ) { |
| 48 |
chunk = getlong(file); |
| 49 |
if ( feof(file) || ferror(file)) |
| 50 |
break; |
| 51 |
sum += chunk; |
| 52 |
} |
| 53 |
return( sum ); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
enum woff_errs { we_good, we_cantopen, we_cantopenout, we_badsignature, |
| 58 |
we_badwofflength, we_mbz, we_badtablen, we_decompressfailed, |
| 59 |
we_baddecompresslen }; |
| 60 |
static void copydata(FILE *to,int off_to,FILE *from,int off_from, int len) { |
| 61 |
int ch, i; |
| 62 |
|
| 63 |
fseek(to ,off_to ,SEEK_SET); |
| 64 |
fseek(from,off_from,SEEK_SET); |
| 65 |
for ( i=0; i<len; ++i ) { |
| 66 |
ch = getc(from); |
| 67 |
putc(ch,to); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
#define CHUNK (128*1024) |
| 72 |
|
| 73 |
static enum woff_errs decompressdata(FILE *to,int off_to,FILE *from,int off_from, |
| 74 |
int len, int uncomplen ) { |
| 75 |
char in[CHUNK]; |
| 76 |
char out[CHUNK]; |
| 77 |
z_stream strm; |
| 78 |
int ret; |
| 79 |
int amount; |
| 80 |
|
| 81 |
fseek(to ,off_to ,SEEK_SET); |
| 82 |
fseek(from,off_from,SEEK_SET); |
| 83 |
memset(&strm,0,sizeof(strm)); |
| 84 |
ret = inflateInit(&strm); |
| 85 |
if ( ret!=Z_OK ) |
| 86 |
return( we_decompressfailed ); |
| 87 |
|
| 88 |
do { |
| 89 |
if ( len<=0 ) { |
| 90 |
(void)inflateEnd(&strm); |
| 91 |
return( we_decompressfailed ); |
| 92 |
} |
| 93 |
amount = len; |
| 94 |
if ( amount>CHUNK ) |
| 95 |
amount = CHUNK; |
| 96 |
strm.avail_in = fread(in, 1, amount, from); |
| 97 |
len -= strm.avail_in; |
| 98 |
if (ferror(from)) { |
| 99 |
(void)inflateEnd(&strm); |
| 100 |
return( we_decompressfailed ); |
| 101 |
} |
| 102 |
if (strm.avail_in == 0) |
| 103 |
break; |
| 104 |
strm.next_in = (Bytef*)in; |
| 105 |
do { |
| 106 |
strm.avail_out = CHUNK; |
| 107 |
strm.next_out = (Bytef*)out; |
| 108 |
ret = inflate(&strm, Z_NO_FLUSH); |
| 109 |
if ( ret!=Z_OK && ret!=Z_STREAM_END ) { |
| 110 |
(void)inflateEnd(&strm); |
| 111 |
return( we_decompressfailed ); |
| 112 |
} |
| 113 |
amount = CHUNK - strm.avail_out; |
| 114 |
if ( fwrite(out,1,amount,to)!= amount || ferror(to) ) { |
| 115 |
(void)inflateEnd(&strm); |
| 116 |
return( we_decompressfailed ); |
| 117 |
} |
| 118 |
} while ( strm.avail_out==0 ); |
| 119 |
} while ( ret!=Z_STREAM_END ); |
| 120 |
(void)inflateEnd(&strm); |
| 121 |
if ( uncomplen!=strm.total_out ) |
| 122 |
return( we_baddecompresslen ); |
| 123 |
|
| 124 |
return( we_good ); |
| 125 |
} |
| 126 |
|
| 127 |
static int dewoff(char *filename) { |
| 128 |
int flavour; |
| 129 |
int iscff; |
| 130 |
char *outname; |
| 131 |
int len, len_stated; |
| 132 |
int num_tabs; |
| 133 |
int metaOffset, metaLenCompressed, metaLenUncompressed; |
| 134 |
int privOffset, privLength; |
| 135 |
int i,j,err; |
| 136 |
int tag, offset, compLen, uncompLen, checksum; |
| 137 |
FILE *woff, *sfnt, *meta, *priv; |
| 138 |
char *start, *ext; |
| 139 |
int here, next, tab_start; |
| 140 |
int head_pos = -1; |
| 141 |
|
| 142 |
woff = fopen( filename,"rb"); |
| 143 |
if ( woff==NULL ) |
| 144 |
return( we_cantopen ); |
| 145 |
|
| 146 |
start = strrchr(filename,'/'); |
| 147 |
if ( start==NULL ) |
| 148 |
start = filename; |
| 149 |
else |
| 150 |
++start; |
| 151 |
ext = strrchr(start,'.'); |
| 152 |
if ( ext==NULL ) |
| 153 |
ext = start+strlen(start); |
| 154 |
|
| 155 |
fseek(woff,0,SEEK_END); |
| 156 |
len = ftell(woff); |
| 157 |
rewind(woff); |
| 158 |
if ( getlong(woff)!=CHR('w','O','F','F') ) |
| 159 |
return( we_badsignature ); |
| 160 |
flavour = getlong(woff); |
| 161 |
iscff = (flavour==CHR('O','T','T','O')); |
| 162 |
len_stated = getlong(woff); |
| 163 |
if ( len!=len_stated ) |
| 164 |
return( we_badwofflength ); |
| 165 |
num_tabs = getushort(woff); |
| 166 |
if ( getushort(woff)!=0 ) |
| 167 |
return( we_mbz ); |
| 168 |
getlong(woff); |
| 169 |
getushort(woff); |
| 170 |
getushort(woff); |
| 171 |
metaOffset = getlong(woff); |
| 172 |
metaLenCompressed = getlong(woff); |
| 173 |
metaLenUncompressed = getlong(woff); |
| 174 |
privOffset = getlong(woff); |
| 175 |
privLength = getlong(woff); |
| 176 |
|
| 177 |
outname = malloc(strlen(start)+20); |
| 178 |
strcpy(outname,start); |
| 179 |
strcpy(outname+(ext-start),iscff ? ".otf" : ".ttf" ); |
| 180 |
sfnt = fopen( outname,"wb+" ); |
| 181 |
if ( sfnt==NULL ) { |
| 182 |
free(outname); |
| 183 |
fclose(woff); |
| 184 |
return( we_cantopenout ); |
| 185 |
} |
| 186 |
|
| 187 |
putlong(sfnt,flavour); |
| 188 |
putshort(sfnt,num_tabs); |
| 189 |
for ( i=1, j=0; 2*i<=num_tabs; i<<=1, ++j ); |
| 190 |
putshort(sfnt,i*16); |
| 191 |
putshort(sfnt,j); |
| 192 |
putshort(sfnt,(num_tabs-i)*16); |
| 193 |
|
| 194 |
|
| 195 |
tab_start = ftell(sfnt); |
| 196 |
for ( i=0; i<4*num_tabs; ++i ) |
| 197 |
putlong(sfnt,0); |
| 198 |
|
| 199 |
for ( i=0; i<num_tabs; ++i ) { |
| 200 |
tag = getlong(woff); |
| 201 |
offset = getlong(woff); |
| 202 |
compLen = getlong(woff); |
| 203 |
uncompLen = getlong(woff); |
| 204 |
checksum = getlong(woff); |
| 205 |
if ( compLen>uncompLen || offset+compLen>len ) { |
| 206 |
fclose(sfnt); |
| 207 |
fclose(woff); |
| 208 |
free(outname); |
| 209 |
return( we_badtablen ); |
| 210 |
} |
| 211 |
here = ftell(woff); |
| 212 |
next = ftell(sfnt); |
| 213 |
fseek(sfnt,tab_start,SEEK_SET); |
| 214 |
putlong(sfnt,tag); |
| 215 |
putlong(sfnt,checksum); |
| 216 |
putlong(sfnt,next); |
| 217 |
putlong(sfnt,uncompLen); |
| 218 |
if ( tag==CHR('h','e','a','d')) |
| 219 |
head_pos = next; |
| 220 |
tab_start = ftell(sfnt); |
| 221 |
fseek(sfnt,next,SEEK_SET); |
| 222 |
if ( compLen==uncompLen ) { |
| 223 |
|
| 224 |
copydata(sfnt,next,woff,offset,compLen); |
| 225 |
} else { |
| 226 |
err = decompressdata(sfnt,next,woff,offset,compLen,uncompLen); |
| 227 |
if ( err!=we_good ) { |
| 228 |
fclose(sfnt); |
| 229 |
fclose(woff); |
| 230 |
free(outname); |
| 231 |
return( err ); |
| 232 |
} |
| 233 |
} |
| 234 |
if ( (ftell(sfnt)&3)!=0 ) { |
| 235 |
|
| 236 |
if ( ftell(sfnt)&1 ) |
| 237 |
putc('\0',sfnt); |
| 238 |
if ( ftell(sfnt)&2 ) |
| 239 |
putshort(sfnt,0); |
| 240 |
} |
| 241 |
fseek(woff,here,SEEK_SET); |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
if ( head_pos!=-1 ) { |
| 247 |
fseek(sfnt,head_pos+8,SEEK_SET); |
| 248 |
putlong(sfnt,0); |
| 249 |
checksum = filecheck(sfnt); |
| 250 |
checksum = 0xb1b0afba-checksum; |
| 251 |
fseek(sfnt,head_pos+8,SEEK_SET); |
| 252 |
putlong(sfnt,checksum); |
| 253 |
} |
| 254 |
fclose(sfnt); |
| 255 |
|
| 256 |
if ( metaOffset!=0 ) { |
| 257 |
strcpy(outname+(ext-start), "_meta.xml" ); |
| 258 |
meta = fopen( outname,"wb" ); |
| 259 |
if ( meta==NULL ) { |
| 260 |
fclose(woff); |
| 261 |
free(outname); |
| 262 |
return( we_cantopenout ); |
| 263 |
} |
| 264 |
err = decompressdata(meta,0,woff,metaOffset,metaLenCompressed,metaLenUncompressed); |
| 265 |
fclose(meta); |
| 266 |
if ( err!=we_good ) { |
| 267 |
fclose(woff); |
| 268 |
free(outname); |
| 269 |
return( err ); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
if ( privOffset!=0 ) { |
| 274 |
strcpy(outname+(ext-start), ".priv" ); |
| 275 |
priv = fopen( outname,"wb" ); |
| 276 |
if ( priv==NULL ) { |
| 277 |
fclose(woff); |
| 278 |
return( we_cantopenout ); |
| 279 |
} |
| 280 |
copydata(priv,0,woff,privOffset,privLength); |
| 281 |
fclose(priv); |
| 282 |
} |
| 283 |
|
| 284 |
fclose(woff); |
| 285 |
free(outname); |
| 286 |
return( we_good ); |
| 287 |
} |
| 288 |
|
| 289 |
static void usage(char *prog) { |
| 290 |
printf( "%s {woff-filename}\n", prog ); |
| 291 |
printf( " Takes one (or several woff files, and converts them into their equivalent\n" ); |
| 292 |
printf( "ttf or otf forms. The output files will be placed in the current directory\n" ); |
| 293 |
printf( "with the same name as the original file and an extension of either \".otf\" or \".ttf\"\n" ); |
| 294 |
printf( "(as is appropriate). If the woff file contains metadata that will be stored\n" ); |
| 295 |
printf( "with the extension \"_meta.xml\", and a private table will have \".priv\"\n" ); |
| 296 |
} |
| 297 |
|
| 298 |
int main(int argc, char **argv) { |
| 299 |
int i, err; |
| 300 |
|
| 301 |
for ( i=1; i<argc; ++i ) { |
| 302 |
if ( *argv[i]=='-' ) { |
| 303 |
usage(argv[0]); |
| 304 |
continue; |
| 305 |
} |
| 306 |
err = dewoff(argv[i]); |
| 307 |
switch ( err ) { |
| 308 |
case we_good: |
| 309 |
break; |
| 310 |
case we_cantopen: |
| 311 |
fprintf( stderr, "Cannot open %s\n", argv[i]); |
| 312 |
break; |
| 313 |
case we_cantopenout: |
| 314 |
fprintf( stderr, "Cannot open output file(s)\n" ); |
| 315 |
break; |
| 316 |
case we_badsignature: |
| 317 |
fprintf( stderr, "This (%s) does not look like a WOFF file.\n", argv[i] ); |
| 318 |
break; |
| 319 |
case we_badwofflength: |
| 320 |
fprintf( stderr, "Actual file length does not match expected length in %s.\n", argv[i] ); |
| 321 |
break; |
| 322 |
case we_badtablen: |
| 323 |
fprintf( stderr, "Table stretches beyond end of file, or impossible values for\n compressed and uncompressed table lengths in %s.\n", argv[i] ); |
| 324 |
break; |
| 325 |
case we_mbz: |
| 326 |
fprintf( stderr, "A field in the WOFF header must be zero, but is not in %s.\n", argv[i] ); |
| 327 |
break; |
| 328 |
case we_decompressfailed: |
| 329 |
fprintf( stderr, "Table decompress failed in %s.\n", argv[i] ); |
| 330 |
break; |
| 331 |
case we_baddecompresslen: |
| 332 |
fprintf( stderr, "Length of decompressed table does not match expected length in %s.\n", argv[i] ); |
| 333 |
break; |
| 334 |
default: |
| 335 |
fprintf( stderr, "Unexpected error: %d on %s\n", err, argv[i]); |
| 336 |
} |
| 337 |
} |
| 338 |
return( 0 ); |
| 339 |
} |