Debian Codegraph @

podofo/3rdparty/adobe/afdko/source/sfntread/sfntread.c

raw
1 /* Copyright 2014 Adobe Systems Incorporated (http://www.adobe.com/). All Rights Reserved.
2 This software is licensed as OpenSource, under the Apache License, Version 2.0.
3 This license is available at: http://opensource.org/licenses/Apache-2.0. */
4
5 /*
6 * sfnt Format Table Handler.
7 */
8
9 #include "sfntread.h"
10 #include "supportexcept.h"
11
12 #include <string.h>
13
14 #define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0]))
15 #define MAX_NUM_TTC_TABLES 512 /* Arbitrary limit to avoid large memory allocations */
16
17 /* Library context */
18 struct sfrCtx_ /* Context */
19 {
20 long flags; /* Status flags */
21 #define USED_DEFAULT_STM (1 << 0) /* Default stream in use */
22 #define USED_CLIENT_STM (1 << 1) /* Client stream in use */
23 #define TTC_STM (1 << 2) /* TrueType Collection stream */
24 struct /* sfnt data */
25 {
26 ctlTag version; /* Header version */
27 long numTables; /* Table count */
28 sfrTable *directory; /* Table directory */
29 long index; /* Iterator index */
30 } sfnt;
31 struct /* TrueType Collection data */
32 {
33 long DirectoryCount;
34 long *TableDirectory;
35 long index; /* Iterator index */
36 long origin;
37 } TTC;
38 struct /* Client callbacks */
39 {
40 ctlMemoryCallbacks mem;
41 ctlStreamCallbacks stm;
42 } cb;
43 struct /* Source stream */
44 {
45 void *stm;
46 char *buf; /* Buffer beginning */
47 size_t length; /* Buffer length */
48 char *end; /* Buffer end */
49 char *next; /* Next byte available (buf <= next < end) */
50 } src;
51 struct /* Error handling */
52 {
53 _Exc_Buf env;
54 } err;
55 };
56
57 /* ----------------------------- Error Handling ---------------------------- */
58
59 /* Handle fatal error. */
60 static void fatal(sfrCtx h, int err_code) {
61 RAISE(&h->err.env, err_code, NULL);
62 }
63
64 /* --------------------------- Memory Management --------------------------- */
65
66 /* Resize memory. */
67 static void *memResize(sfrCtx h, void *old, size_t size) {
68 void *_new = h->cb.mem.manage(&h->cb.mem, old, size);
69 if (_new == NULL)
70 fatal(h, sfrErrNoMemory);
71 return _new;
72 }
73
74 /* Free memory. */
75 static void memFree(sfrCtx h, void *ptr) {
76 (void)h->cb.mem.manage(&h->cb.mem, ptr, 0);
77 }
78
79 /* --------------------------- Context Management -------------------------- */
80
81 /* Validate client and create context. */
82 sfrCtx sfrNew(ctlMemoryCallbacks *mem_cb, ctlStreamCallbacks *stm_cb,
83 CTL_CHECK_ARGS_DCL) {
84 sfrCtx h;
85
86 /* Check client/library compatibility */
87 if (CTL_CHECK_ARGS_TEST(SFR_VERSION))
88 return NULL;
89
90 /* Allocate context */
91 h = (sfrCtx)mem_cb->manage(mem_cb, NULL, sizeof(struct sfrCtx_));
92 if (h == NULL)
93 return NULL;
94
95 /* Safety initialization */
96 memset(h, 0, sizeof(*h));
97
98 /* Copy callbacks */
99 h->cb.mem = *mem_cb;
100 h->cb.stm = *stm_cb;
101
102 h->flags = 0;
103
104 return h;
105 }
106
107 /* Free library context. */
108 void sfrFree(sfrCtx h) {
109 if (h == NULL)
110 return;
111
112 memFree(h, h->sfnt.directory);
113 memFree(h, h->TTC.TableDirectory);
114 memFree(h, h);
115 }
116
117 /* ----------------------------- Source Stream ----------------------------- */
118
119 /* Fill source buffer. */
120 static void fillbuf(sfrCtx h) {
121 h->src.length = h->cb.stm.read(&h->cb.stm, h->src.stm, &h->src.buf);
122 if (h->src.length == 0)
123 fatal(h, sfrErrSrcStream);
124 h->src.next = h->src.buf;
125 h->src.end = h->src.buf + h->src.length;
126 }
127
128 /* Read next sequential source buffer; update offset and return first byte. */
129 static char nextbuf(sfrCtx h) {
130 fillbuf(h);
131 return *h->src.next++;
132 }
133
134 /* Read 1-byte unsigned number. */
135 #define read1(h) \
136 ((unsigned char)((h->src.next == h->src.end) ? nextbuf(h) : *h->src.next++))
137
138 /* Read 2-byte number. */
139 static unsigned short read2(sfrCtx h) {
140 unsigned short value = (unsigned short)read1(h) << 8;
141 return value | (unsigned short)read1(h);
142 }
143
144 /* Read 4-byte unsigned number. */
145 static unsigned long read4(sfrCtx h) {
146 unsigned long value = (unsigned long)read1(h) << 24;
147 value |= (unsigned long)read1(h) << 16;
148 value |= (unsigned long)read1(h) << 8;
149 return value | (unsigned long)read1(h);
150 }
151
152 /* ------------------------------ sfnt Parsing ----------------------------- */
153
154 /* Read sfnt table directory. */
155 static void readSfntDirectory(sfrCtx h, long origin) {
156 long i;
157 /* Read rest of header */
158 h->sfnt.numTables = read2(h);
159 (void)read2(h);
160 (void)read2(h);
161 (void)read2(h);
162
163 /* Read directory */
164 h->sfnt.directory = (sfrTable *)memResize(h, h->sfnt.directory,
165 h->sfnt.numTables * sizeof(sfrTable));
166 for (i = 0; i < h->sfnt.numTables; i++) {
167 sfrTable *table = &h->sfnt.directory[i];
168 table->tag = read4(h);
169 table->checksum = read4(h);
170 table->offset = read4(h) + origin;
171 table->length = read4(h);
172 }
173
174 h->sfnt.index = 0;
175 }
176
177 /* Read TrueType Collection TableDirectory. */
178 static void readTTCDirectory(sfrCtx h, long origin) {
179 long i;
180 size_t size;
181 long dir_count = 0;
182
183 /* Skip version since we are only interested in version 1.0 fields*/
184 (void)read4(h);
185
186 /* Read directory */
187 dir_count = h->TTC.DirectoryCount = read4(h);
188 if (dir_count > MAX_NUM_TTC_TABLES)
189 {
190 /* NOTE: this limit is not actually part of the spec; it was arbitrarily
191 chosen to avoid massive memory allocations when dir_count is ridiculously
192 large. Ideally we'd check the stream's size, but that's not possible for
193 the stdin input case. */
194 fprintf(stderr, "tx: TTC directory count %ld > limit %i\n", dir_count, MAX_NUM_TTC_TABLES);
195 fprintf(stderr, "tx: fatal error\n");
196 fatal(h, sfrErrTables);
197 }
198
199 size = (size_t)dir_count * sizeof(long);
200
201 if (dir_count <= 0 || size / sizeof(long) != dir_count) /* overflow check */
202 {
203 h->TTC.DirectoryCount = 0;
204 return;
205 }
206 h->TTC.TableDirectory = (long *)memResize(h, h->TTC.TableDirectory, size);
207 h->flags |= TTC_STM; /* readSfntDirectory( reads in to h->TTC.TableDirectory[i].directory if TTC_STM is set.*/
208
209 for (i = 0; i < dir_count; i++) {
210 h->TTC.TableDirectory[i] = read4(h) + origin;
211 }
212 h->TTC.origin = origin;
213 h->TTC.index = 0;
214 }
215
216 /* Begin reading new font. */
217 int sfrBegFont(sfrCtx h, void *stm, long origin, ctlTag *sfnt_tag) {
218 if (h->flags & (USED_CLIENT_STM | USED_DEFAULT_STM))
219 ; /* Stream already set */
220 else if (stm == NULL) {
221 /* Open default source stream */
222 h->src.stm = h->cb.stm.open(&h->cb.stm, SFR_SRC_STREAM_ID, 0);
223 if (h->src.stm == NULL)
224 return sfrErrSrcStream;
225 h->flags |= USED_DEFAULT_STM;
226 } else {
227 /* Use client-supplied stream */
228 h->src.stm = stm;
229 h->flags |= USED_CLIENT_STM;
230 }
231
232 /* Seek to sfnt header and fill buffer */
233 if (h->cb.stm.seek(&h->cb.stm, h->src.stm, origin))
234 return sfrErrSrcStream;
235
236 /* Set error handler */
237 DURING_EX(h->err.env)
238
239 fillbuf(h);
240
241 /* Read and validate sfnt version */
242 *sfnt_tag = read4(h);
243 switch (*sfnt_tag) {
244 case sfr_v1_0_tag:
245 case sfr_true_tag:
246 case sfr_OTTO_tag:
247 case sfr_typ1_tag:
248 readSfntDirectory(h, (h->flags & TTC_STM) ? h->TTC.origin : origin);
249 break;
250 case sfr_ttcf_tag:
251 readTTCDirectory(h, origin);
252 break;
253 default:
254 return sfrErrBadSfnt;
255 }
256
257 HANDLER
258 return Exception.Code;
259 END_HANDLER
260
261 return sfrSuccess;
262 }
263
264 /* Get the next offset from the TTC table directory. */
265 long sfrGetNextTTCOffset(sfrCtx h) {
266 if (!(h->flags & TTC_STM))
267 return 0; /* Invalid call */
268
269 if (h->TTC.index == h->TTC.DirectoryCount) {
270 /* At end of list; reset iterator */
271 h->TTC.index = 0;
272 return 0;
273 }
274
275 /* Iterate list */
276 return h->TTC.TableDirectory[h->TTC.index++];
277 }
278
279 /* Get the next table element from the sfnt table directory. */
280 sfrTable *sfrGetNextTable(sfrCtx h) {
281 if (h->sfnt.directory == NULL)
282 return 0; /* Invalid call */
283
284 if (h->sfnt.index == h->sfnt.numTables) {
285 /* At end of list; reset iterator */
286 h->sfnt.index = 0;
287 return 0;
288 }
289
290 /* Iterate list */
291 return &h->sfnt.directory[h->sfnt.index++];
292 }
293
294 /* Get table from sfnt by its tag. */
295 sfrTable *sfrGetTableByTag(sfrCtx h, ctlTag tag) {
296 long i;
297
298 if (h->sfnt.directory == NULL)
299 return NULL; /* Invalid call */
300
301 /* Table count is small so use non-optimized linear search. This search
302 isn't optimized because although the TrueType specification says the
303 sfnt directory entries must be sorted by ascending tag order there are
304 shipping fonts that don't do this - sigh! */
305 for (i = 0; i < h->sfnt.numTables; i++) {
306 sfrTable *table = &h->sfnt.directory[i];
307 if (tag == table->tag)
308 return table;
309 }
310
311 return NULL; /* Not found */
312 }
313
314 /* Finish reading font. */
315 int sfrEndFont(sfrCtx h) {
316 if (h->flags & USED_DEFAULT_STM) {
317 /* Close default source stream */
318 if (h->cb.stm.close(&h->cb.stm, h->src.stm) == -1)
319 return sfrErrSrcStream;
320 }
321 h->flags = 0;
322
323 return sfrSuccess;
324 }
325
326 /* Get version numbers of libraries. */
327 void sfrGetVersion(ctlVersionCallbacks *cb) {
328 if (cb->called & 1 << SFR_LIB_ID)
329 return; /* Already enumerated */
330
331 /* This library */
332 cb->getversion(cb, SFR_VERSION, "sfntread");
333
334 /* Record this call */
335 cb->called |= 1 << SFR_LIB_ID;
336 }
337
338 /* Convert error code to error string. */
339 char *sfrErrStr(int err_code) {
340 static char *errstrs[] =
341 {
342 #undef CTL_DCL_ERR
343 #define CTL_DCL_ERR(name, string) string,
344 #include "sfrerr.h"
345 };
346 return (err_code < 0 || err_code >= (int)ARRAY_LEN(errstrs)) ? "unknown error" : errstrs[err_code];
347 }