ufont_woff.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* WOFF 1.0 -> sfnt. A WOFF file is an sfnt whose tables were each
  2. * zlib-compressed on their own, behind a 44-byte header and a 20-byte
  3. * directory entry per table; rebuilding the TrueType file only means
  4. * writing a new 12-byte offset table, 16-byte directory entries and the
  5. * inflated tables, 4-byte aligned. WOFF2 (Brotli plus glyf/loca
  6. * transforms) is not handled.
  7. *
  8. * Separate from ufont.c so only the browser (and the host tests) link it
  9. * together with inflate. */
  10. #include "ufont.h"
  11. #include "inflate.h"
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #define WOFF_HDR 44u
  15. #define WOFF_DIR 20u
  16. #define WOFF_MAX_TABLES 64u
  17. static unsigned int be16(const unsigned char *p)
  18. {
  19. return ((unsigned int)p[0] << 8) | p[1];
  20. }
  21. static unsigned int be32(const unsigned char *p)
  22. {
  23. return ((unsigned int)p[0] << 24) | ((unsigned int)p[1] << 16) |
  24. ((unsigned int)p[2] << 8) | p[3];
  25. }
  26. static void put16(unsigned char *p, unsigned int v)
  27. {
  28. p[0] = (unsigned char)(v >> 8);
  29. p[1] = (unsigned char)v;
  30. }
  31. static void put32(unsigned char *p, unsigned int v)
  32. {
  33. p[0] = (unsigned char)(v >> 24);
  34. p[1] = (unsigned char)(v >> 16);
  35. p[2] = (unsigned char)(v >> 8);
  36. p[3] = (unsigned char)v;
  37. }
  38. int ufont_is_woff(const void *data, unsigned int len)
  39. {
  40. const unsigned char *p = (const unsigned char *)data;
  41. return len >= WOFF_HDR && p[0] == 'w' && p[1] == 'O' &&
  42. p[2] == 'F' && p[3] == 'F';
  43. }
  44. int ufont_is_woff2(const void *data, unsigned int len)
  45. {
  46. const unsigned char *p = (const unsigned char *)data;
  47. return len >= 4 && p[0] == 'w' && p[1] == 'O' && p[2] == 'F' &&
  48. p[3] == '2';
  49. }
  50. unsigned char *ufont_woff_decode(const void *data, unsigned int len,
  51. unsigned int *out_len)
  52. {
  53. const unsigned char *src = (const unsigned char *)data;
  54. unsigned int ntables, total, i, need, off, sel, pow2;
  55. unsigned char *out, *ws = 0;
  56. if (out_len)
  57. *out_len = 0;
  58. if (!ufont_is_woff(src, len) || be32(src + 8) != len)
  59. return 0;
  60. ntables = be16(src + 12);
  61. total = be32(src + 16);
  62. if (ntables == 0 || ntables > WOFF_MAX_TABLES ||
  63. WOFF_HDR + ntables * WOFF_DIR > len || total > UFONT_MAX_FILE)
  64. return 0;
  65. /* totalSfntSize is advisory; recompute the real need and refuse a
  66. * file whose tables would not fit in it. */
  67. need = 12u + ntables * 16u;
  68. for (i = 0; i < ntables; i++) {
  69. const unsigned char *d = src + WOFF_HDR + i * WOFF_DIR;
  70. unsigned int orig = be32(d + 12);
  71. if (orig > UFONT_MAX_FILE)
  72. return 0;
  73. need += (orig + 3u) & ~3u;
  74. if (need > UFONT_MAX_FILE)
  75. return 0;
  76. }
  77. out = (unsigned char *)malloc(need);
  78. if (!out)
  79. return 0;
  80. memset(out, 0, need);
  81. put32(out, be32(src + 4)); /* sfnt flavor */
  82. put16(out + 4, ntables);
  83. for (sel = 0, pow2 = 1; pow2 * 2u <= ntables; sel++)
  84. pow2 *= 2u;
  85. put16(out + 6, pow2 * 16u); /* searchRange */
  86. put16(out + 8, sel); /* entrySelector */
  87. put16(out + 10, ntables * 16u - pow2 * 16u);
  88. off = 12u + ntables * 16u;
  89. for (i = 0; i < ntables; i++) {
  90. const unsigned char *d = src + WOFF_HDR + i * WOFF_DIR;
  91. unsigned char *r = out + 12u + i * 16u;
  92. unsigned int toff = be32(d + 4);
  93. unsigned int comp = be32(d + 8);
  94. unsigned int orig = be32(d + 12);
  95. if (toff > len || comp > len - toff || comp > orig)
  96. goto fail;
  97. memcpy(r, d, 4); /* tag */
  98. put32(r + 4, be32(d + 16)); /* checksum */
  99. put32(r + 8, off);
  100. put32(r + 12, orig);
  101. if (comp == orig) {
  102. memcpy(out + off, src + toff, orig);
  103. } else {
  104. unsigned int got = 0;
  105. if (!ws) {
  106. ws = (unsigned char *)malloc(
  107. sizeof(struct inflate));
  108. if (!ws)
  109. goto fail;
  110. }
  111. if (inflate_zlib_mem(src + toff, comp, out + off, orig,
  112. &got, ws) != 0 || got != orig)
  113. goto fail;
  114. }
  115. off += (orig + 3u) & ~3u;
  116. }
  117. free(ws);
  118. if (out_len)
  119. *out_len = off;
  120. return out;
  121. fail:
  122. free(ws);
  123. free(out);
  124. return 0;
  125. }