Debian Codegraph @

src/core/SkFontStream.cpp

raw
1 /*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7 #include "src/core/SkFontStream.h"
8
9 #include "include/core/SkFourByteTag.h"
10 #include "include/core/SkStream.h"
11 #include "include/core/SkTypes.h"
12 #include "include/private/base/SkMalloc.h"
13 #include "src/base/SkAutoMalloc.h"
14 #include "src/base/SkEndian.h"
15
16 #include <algorithm>
17 #include <cstdint>
18
19 struct SkSFNTHeader {
20 uint32_t fVersion;
21 uint16_t fNumTables;
22 uint16_t fSearchRange;
23 uint16_t fEntrySelector;
24 uint16_t fRangeShift;
25 };
26
27 struct SkTTCFHeader {
28 uint32_t fTag;
29 uint32_t fVersion;
30 uint32_t fNumOffsets;
31 uint32_t fOffset0; // the first of N (fNumOffsets)
32 };
33
34 union SkSharedTTHeader {
35 SkSFNTHeader fSingle;
36 SkTTCFHeader fCollection;
37 };
38
39 struct SkSFNTDirEntry {
40 uint32_t fTag;
41 uint32_t fChecksum;
42 uint32_t fOffset;
43 uint32_t fLength;
44 };
45
46 static bool read(SkStream* stream, void* buffer, size_t amount) {
47 return stream->read(buffer, amount) == amount;
48 }
49
50 static bool skip(SkStream* stream, size_t amount) {
51 return stream->skip(amount) == amount;
52 }
53
54 /** Return the number of tables, or if this is a TTC (collection), return the
55 number of tables in the first element of the collection. In either case,
56 if offsetToDir is not-null, set it to the offset to the beginning of the
57 table headers (SkSFNTDirEntry), relative to the start of the stream.
58
59 On an error, return 0 for number of tables, and ignore offsetToDir
60 */
61 static int count_tables(SkStream* stream, int ttcIndex, size_t* offsetToDir) {
62 SkASSERT(ttcIndex >= 0);
63
64 SkAutoSMalloc<1024> storage(sizeof(SkSharedTTHeader));
65 SkSharedTTHeader* header = (SkSharedTTHeader*)storage.get();
66
67 if (!read(stream, header, sizeof(SkSharedTTHeader))) {
68 return 0;
69 }
70
71 // by default, SkSFNTHeader is at the start of the stream
72 size_t offset = 0;
73
74 // if we're really a collection, the first 4-bytes will be 'ttcf'
75 uint32_t tag = SkEndian_SwapBE32(header->fCollection.fTag);
76 if (SkSetFourByteTag('t', 't', 'c', 'f') == tag) {
77 unsigned count = SkEndian_SwapBE32(header->fCollection.fNumOffsets);
78 if ((unsigned)ttcIndex >= count) {
79 return 0;
80 }
81
82 if (ttcIndex > 0) { // need to read more of the shared header
83 stream->rewind();
84 size_t amount = sizeof(SkSharedTTHeader) + ttcIndex * sizeof(uint32_t);
85 header = (SkSharedTTHeader*)storage.reset(amount);
86 if (!read(stream, header, amount)) {
87 return 0;
88 }
89 }
90 // this is the offset to the local SkSFNTHeader
91 offset = SkEndian_SwapBE32((&header->fCollection.fOffset0)[ttcIndex]);
92 stream->rewind();
93 if (!skip(stream, offset)) {
94 return 0;
95 }
96 if (!read(stream, header, sizeof(SkSFNTHeader))) {
97 return 0;
98 }
99 }
100
101 if (offsetToDir) {
102 // add the size of the header, so we will point to the DirEntries
103 *offsetToDir = offset + sizeof(SkSFNTHeader);
104 }
105 return SkEndian_SwapBE16(header->fSingle.fNumTables);
106 }
107
108 ///////////////////////////////////////////////////////////////////////////////
109
110 struct SfntHeader {
111 SfntHeader() : fCount(0), fDir(nullptr) {}
112 ~SfntHeader() { sk_free(fDir); }
113
114 /** If it returns true, then fCount and fDir are properly initialized.
115 Note: fDir will point to the raw array of SkSFNTDirEntry values,
116 meaning they will still be in the file's native endianness (BE).
117
118 fDir will be automatically freed when this object is destroyed
119 */
120 bool init(SkStream* stream, int ttcIndex) {
121 stream->rewind();
122
123 size_t offsetToDir;
124 fCount = count_tables(stream, ttcIndex, &offsetToDir);
125 if (0 == fCount) {
126 return false;
127 }
128
129 stream->rewind();
130 if (!skip(stream, offsetToDir)) {
131 return false;
132 }
133
134 size_t size = fCount * sizeof(SkSFNTDirEntry);
135 fDir = reinterpret_cast<SkSFNTDirEntry*>(sk_malloc_throw(size));
136 return read(stream, fDir, size);
137 }
138
139 int fCount;
140 SkSFNTDirEntry* fDir;
141 };
142
143 ///////////////////////////////////////////////////////////////////////////////
144
145 int SkFontStream::CountTTCEntries(SkStream* stream) {
146 stream->rewind();
147
148 SkSharedTTHeader shared;
149 if (!read(stream, &shared, sizeof(shared))) {
150 return 0;
151 }
152
153 // if we're really a collection, the first 4-bytes will be 'ttcf'
154 uint32_t tag = SkEndian_SwapBE32(shared.fCollection.fTag);
155 if (SkSetFourByteTag('t', 't', 'c', 'f') == tag) {
156 return SkEndian_SwapBE32(shared.fCollection.fNumOffsets);
157 } else {
158 return 1; // normal 'sfnt' has 1 dir entry
159 }
160 }
161
162 int SkFontStream::GetTableTags(SkStream* stream, int ttcIndex, SkSpan<SkFontTableTag> tags) {
163 SfntHeader header;
164 if (!header.init(stream, ttcIndex)) {
165 return 0;
166 }
167
168 const size_t n = std::min((size_t)header.fCount, tags.size());
169 for (size_t i = 0; i < n; i++) {
170 tags[i] = SkEndian_SwapBE32(header.fDir[i].fTag);
171 }
172 return header.fCount;
173 }
174
175 size_t SkFontStream::GetTableData(SkStream* stream, int ttcIndex,
176 SkFontTableTag tag,
177 size_t offset, size_t length, void* data) {
178 SfntHeader header;
179 if (!header.init(stream, ttcIndex)) {
180 return 0;
181 }
182
183 for (int i = 0; i < header.fCount; i++) {
184 if (SkEndian_SwapBE32(header.fDir[i].fTag) == tag) {
185 size_t realOffset = SkEndian_SwapBE32(header.fDir[i].fOffset);
186 size_t realLength = SkEndian_SwapBE32(header.fDir[i].fLength);
187 if (offset >= realLength) {
188 // invalid
189 return 0;
190 }
191 // if the caller is trusting the length from the file, then a
192 // hostile file might choose a value which would overflow offset +
193 // length.
194 if (offset + length < offset) {
195 return 0;
196 }
197 if (length > realLength - offset) {
198 length = realLength - offset;
199 }
200 if (data) {
201 // skip the stream to the part of the table we want to copy from
202 stream->rewind();
203 size_t bytesToSkip = realOffset + offset;
204 if (!skip(stream, bytesToSkip)) {
205 return 0;
206 }
207 if (!read(stream, data, length)) {
208 return 0;
209 }
210 }
211 return length;
212 }
213 }
214 return 0;
215 }