PageRenderTime 85ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/gfx/ots/src/ots.h

http://github.com/zpao/v8monkey
C Header | 230 lines | 182 code | 30 blank | 18 comment | 10 complexity | f78f0c17af7ee4e8172bebfe6887f169 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, AGPL-1.0, LGPL-2.1, BSD-3-Clause, GPL-2.0, JSON, Apache-2.0, 0BSD
  1. // Copyright (c) 2009 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef OTS_H_
  5. #define OTS_H_
  6. #include <stddef.h>
  7. #include <cstdarg>
  8. #include <cstddef>
  9. #include <cstdio>
  10. #include <cstdlib>
  11. #include <cstring>
  12. #include "opentype-sanitiser.h"
  13. namespace ots {
  14. #if defined(_MSC_VER) || !defined(OTS_DEBUG)
  15. #define OTS_FAILURE() false
  16. #else
  17. #define OTS_FAILURE() ots::Failure(__FILE__, __LINE__, __PRETTY_FUNCTION__)
  18. bool Failure(const char *f, int l, const char *fn);
  19. #endif
  20. #if defined(_MSC_VER)
  21. // MSVC supports C99 style variadic macros.
  22. #define OTS_WARNING(format, ...)
  23. #else
  24. // GCC
  25. #if defined(OTS_DEBUG)
  26. #define OTS_WARNING(format, args...) \
  27. ots::Warning(__FILE__, __LINE__, format, ##args)
  28. void Warning(const char *f, int l, const char *format, ...)
  29. __attribute__((format(printf, 3, 4)));
  30. #else
  31. #define OTS_WARNING(format, args...)
  32. #endif
  33. #endif
  34. // Define OTS_NO_TRANSCODE_HINTS (i.e., g++ -DOTS_NO_TRANSCODE_HINTS) if you
  35. // want to omit TrueType hinting instructions and variables in glyf, fpgm, prep,
  36. // and cvt tables.
  37. #if defined(OTS_NO_TRANSCODE_HINTS)
  38. const bool g_transcode_hints = false;
  39. #else
  40. const bool g_transcode_hints = true;
  41. #endif
  42. // -----------------------------------------------------------------------------
  43. // Buffer helper class
  44. //
  45. // This class perform some trival buffer operations while checking for
  46. // out-of-bounds errors. As a family they return false if anything is amiss,
  47. // updating the current offset otherwise.
  48. // -----------------------------------------------------------------------------
  49. class Buffer {
  50. public:
  51. Buffer(const uint8_t *buffer, size_t len)
  52. : buffer_(buffer),
  53. length_(len),
  54. offset_(0) { }
  55. bool Skip(size_t n_bytes) {
  56. return Read(NULL, n_bytes);
  57. }
  58. bool Read(uint8_t *buffer, size_t n_bytes) {
  59. if (n_bytes > 1024 * 1024 * 1024) {
  60. return OTS_FAILURE();
  61. }
  62. if ((offset_ + n_bytes > length_) ||
  63. (offset_ > length_ - n_bytes)) {
  64. return OTS_FAILURE();
  65. }
  66. if (buffer) {
  67. std::memcpy(buffer, buffer_ + offset_, n_bytes);
  68. }
  69. offset_ += n_bytes;
  70. return true;
  71. }
  72. inline bool ReadU8(uint8_t *value) {
  73. if (offset_ + 1 > length_) {
  74. return OTS_FAILURE();
  75. }
  76. *value = buffer_[offset_];
  77. ++offset_;
  78. return true;
  79. }
  80. bool ReadU16(uint16_t *value) {
  81. if (offset_ + 2 > length_) {
  82. return OTS_FAILURE();
  83. }
  84. std::memcpy(value, buffer_ + offset_, sizeof(uint16_t));
  85. *value = ntohs(*value);
  86. offset_ += 2;
  87. return true;
  88. }
  89. bool ReadS16(int16_t *value) {
  90. return ReadU16(reinterpret_cast<uint16_t*>(value));
  91. }
  92. bool ReadU24(uint32_t *value) {
  93. if (offset_ + 3 > length_) {
  94. return OTS_FAILURE();
  95. }
  96. *value = static_cast<uint32_t>(buffer_[offset_]) << 16 |
  97. static_cast<uint32_t>(buffer_[offset_ + 1]) << 8 |
  98. static_cast<uint32_t>(buffer_[offset_ + 2]);
  99. offset_ += 3;
  100. return true;
  101. }
  102. bool ReadU32(uint32_t *value) {
  103. if (offset_ + 4 > length_) {
  104. return OTS_FAILURE();
  105. }
  106. std::memcpy(value, buffer_ + offset_, sizeof(uint32_t));
  107. *value = ntohl(*value);
  108. offset_ += 4;
  109. return true;
  110. }
  111. bool ReadS32(int32_t *value) {
  112. return ReadU32(reinterpret_cast<uint32_t*>(value));
  113. }
  114. bool ReadTag(uint32_t *value) {
  115. if (offset_ + 4 > length_) {
  116. return OTS_FAILURE();
  117. }
  118. std::memcpy(value, buffer_ + offset_, sizeof(uint32_t));
  119. offset_ += 4;
  120. return true;
  121. }
  122. bool ReadR64(uint64_t *value) {
  123. if (offset_ + 8 > length_) {
  124. return OTS_FAILURE();
  125. }
  126. std::memcpy(value, buffer_ + offset_, sizeof(uint64_t));
  127. offset_ += 8;
  128. return true;
  129. }
  130. const uint8_t *buffer() const { return buffer_; }
  131. size_t offset() const { return offset_; }
  132. size_t length() const { return length_; }
  133. void set_offset(size_t newoffset) { offset_ = newoffset; }
  134. private:
  135. const uint8_t * const buffer_;
  136. const size_t length_;
  137. size_t offset_;
  138. };
  139. #define FOR_EACH_TABLE_TYPE \
  140. F(cff, CFF) \
  141. F(cmap, CMAP) \
  142. F(cvt, CVT) \
  143. F(fpgm, FPGM) \
  144. F(gasp, GASP) \
  145. F(gdef, GDEF) \
  146. F(glyf, GLYF) \
  147. F(gpos, GPOS) \
  148. F(gsub, GSUB) \
  149. F(hdmx, HDMX) \
  150. F(head, HEAD) \
  151. F(hhea, HHEA) \
  152. F(hmtx, HMTX) \
  153. F(kern, KERN) \
  154. F(loca, LOCA) \
  155. F(ltsh, LTSH) \
  156. F(maxp, MAXP) \
  157. F(name, NAME) \
  158. F(os2, OS2) \
  159. F(post, POST) \
  160. F(prep, PREP) \
  161. F(vdmx, VDMX) \
  162. F(vorg, VORG) \
  163. F(vhea, VHEA) \
  164. F(vmtx, VMTX) \
  165. F(silf, SILF) \
  166. F(sill, SILL) \
  167. F(glat, GLAT) \
  168. F(gloc, GLOC) \
  169. F(feat, FEAT)
  170. #define F(name, capname) struct OpenType##capname;
  171. FOR_EACH_TABLE_TYPE
  172. #undef F
  173. struct OpenTypeFile {
  174. OpenTypeFile() {
  175. #define F(name, capname) name = NULL;
  176. FOR_EACH_TABLE_TYPE
  177. #undef F
  178. }
  179. uint32_t version;
  180. uint16_t num_tables;
  181. uint16_t search_range;
  182. uint16_t entry_selector;
  183. uint16_t range_shift;
  184. // This is used to tell the relevant parsers whether to preserve the
  185. // Graphite layout tables (currently _without_ any checking)
  186. bool preserve_graphite;
  187. #define F(name, capname) OpenType##capname *name;
  188. FOR_EACH_TABLE_TYPE
  189. #undef F
  190. };
  191. #define F(name, capname) \
  192. bool ots_##name##_parse(OpenTypeFile *f, const uint8_t *d, size_t l); \
  193. bool ots_##name##_should_serialise(OpenTypeFile *f); \
  194. bool ots_##name##_serialise(OTSStream *s, OpenTypeFile *f); \
  195. void ots_##name##_free(OpenTypeFile *f);
  196. // TODO(yusukes): change these function names to follow Chromium coding rule.
  197. FOR_EACH_TABLE_TYPE
  198. #undef F
  199. } // namespace ots
  200. #endif // OTS_H_