/* WOFF 1.0 -> sfnt. A WOFF file is an sfnt whose tables were each * zlib-compressed on their own, behind a 44-byte header and a 20-byte * directory entry per table; rebuilding the TrueType file only means * writing a new 12-byte offset table, 16-byte directory entries and the * inflated tables, 4-byte aligned. WOFF2 (Brotli plus glyf/loca * transforms) is not handled. * * Separate from ufont.c so only the browser (and the host tests) link it * together with inflate. */ #include "ufont.h" #include "inflate.h" #include #include #define WOFF_HDR 44u #define WOFF_DIR 20u #define WOFF_MAX_TABLES 64u static unsigned int be16(const unsigned char *p) { return ((unsigned int)p[0] << 8) | p[1]; } static unsigned int be32(const unsigned char *p) { return ((unsigned int)p[0] << 24) | ((unsigned int)p[1] << 16) | ((unsigned int)p[2] << 8) | p[3]; } static void put16(unsigned char *p, unsigned int v) { p[0] = (unsigned char)(v >> 8); p[1] = (unsigned char)v; } static void put32(unsigned char *p, unsigned int v) { p[0] = (unsigned char)(v >> 24); p[1] = (unsigned char)(v >> 16); p[2] = (unsigned char)(v >> 8); p[3] = (unsigned char)v; } int ufont_is_woff(const void *data, unsigned int len) { const unsigned char *p = (const unsigned char *)data; return len >= WOFF_HDR && p[0] == 'w' && p[1] == 'O' && p[2] == 'F' && p[3] == 'F'; } int ufont_is_woff2(const void *data, unsigned int len) { const unsigned char *p = (const unsigned char *)data; return len >= 4 && p[0] == 'w' && p[1] == 'O' && p[2] == 'F' && p[3] == '2'; } unsigned char *ufont_woff_decode(const void *data, unsigned int len, unsigned int *out_len) { const unsigned char *src = (const unsigned char *)data; unsigned int ntables, total, i, need, off, sel, pow2; unsigned char *out, *ws = 0; if (out_len) *out_len = 0; if (!ufont_is_woff(src, len) || be32(src + 8) != len) return 0; ntables = be16(src + 12); total = be32(src + 16); if (ntables == 0 || ntables > WOFF_MAX_TABLES || WOFF_HDR + ntables * WOFF_DIR > len || total > UFONT_MAX_FILE) return 0; /* totalSfntSize is advisory; recompute the real need and refuse a * file whose tables would not fit in it. */ need = 12u + ntables * 16u; for (i = 0; i < ntables; i++) { const unsigned char *d = src + WOFF_HDR + i * WOFF_DIR; unsigned int orig = be32(d + 12); if (orig > UFONT_MAX_FILE) return 0; need += (orig + 3u) & ~3u; if (need > UFONT_MAX_FILE) return 0; } out = (unsigned char *)malloc(need); if (!out) return 0; memset(out, 0, need); put32(out, be32(src + 4)); /* sfnt flavor */ put16(out + 4, ntables); for (sel = 0, pow2 = 1; pow2 * 2u <= ntables; sel++) pow2 *= 2u; put16(out + 6, pow2 * 16u); /* searchRange */ put16(out + 8, sel); /* entrySelector */ put16(out + 10, ntables * 16u - pow2 * 16u); off = 12u + ntables * 16u; for (i = 0; i < ntables; i++) { const unsigned char *d = src + WOFF_HDR + i * WOFF_DIR; unsigned char *r = out + 12u + i * 16u; unsigned int toff = be32(d + 4); unsigned int comp = be32(d + 8); unsigned int orig = be32(d + 12); if (toff > len || comp > len - toff || comp > orig) goto fail; memcpy(r, d, 4); /* tag */ put32(r + 4, be32(d + 16)); /* checksum */ put32(r + 8, off); put32(r + 12, orig); if (comp == orig) { memcpy(out + off, src + toff, orig); } else { unsigned int got = 0; if (!ws) { ws = (unsigned char *)malloc( sizeof(struct inflate)); if (!ws) goto fail; } if (inflate_zlib_mem(src + toff, comp, out + off, orig, &got, ws) != 0 || got != orig) goto fail; } off += (orig + 3u) & ~3u; } free(ws); if (out_len) *out_len = off; return out; fail: free(ws); free(out); return 0; }