SkFontStream.cpp revision 90ee4488e9c6b8ec4cb1137250fed43b5919ce2c
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
8#include "SkEndian.h"
9#include "SkFontStream.h"
10#include "SkStream.h"
11
12struct SkSFNTHeader {
13    uint32_t    fVersion;
14    uint16_t    fNumTables;
15    uint16_t    fSearchRange;
16    uint16_t    fEntrySelector;
17    uint16_t    fRangeShift;
18};
19
20struct SkTTCFHeader {
21    uint32_t    fTag;
22    uint32_t    fVersion;
23    uint32_t    fNumOffsets;
24    uint32_t    fOffset0;   // the first of N (fNumOffsets)
25};
26
27union SkSharedTTHeader {
28    SkSFNTHeader    fSingle;
29    SkTTCFHeader    fCollection;
30};
31
32struct SkSFNTDirEntry {
33    uint32_t    fTag;
34    uint32_t    fChecksum;
35    uint32_t    fOffset;
36    uint32_t    fLength;
37};
38
39/** Return the number of tables, or if this is a TTC (collection), return the
40    number of tables in the first element of the collection. In either case,
41    if offsetToDir is not-null, set it to the offset to the beginning of the
42    table headers (SkSFNTDirEntry), relative to the start of the stream.
43
44    On an error, return 0 for number of tables, and ignore offsetToDir
45 */
46static int count_tables(SkStream* stream, size_t* offsetToDir = NULL) {
47    SkSharedTTHeader shared;
48    if (stream->read(&shared, sizeof(shared)) != sizeof(shared)) {
49        return 0;
50    }
51
52    // by default, SkSFNTHeader is at the start of the stream
53    size_t offset = 0;
54
55    // if we're really a collection, the first 4-bytes will be 'ttcf'
56    uint32_t tag = SkEndian_SwapBE32(shared.fCollection.fTag);
57    if (SkSetFourByteTag('t', 't', 'c', 'f') == tag) {
58        if (shared.fCollection.fNumOffsets == 0) {
59            return 0;
60        }
61        // this is the offset to the first local SkSFNTHeader
62        offset = SkEndian_SwapBE32(shared.fCollection.fOffset0);
63        stream->rewind();
64        if (stream->skip(offset) != offset) {
65            return 0;
66        }
67        if (stream->read(&shared, sizeof(shared)) != sizeof(shared)) {
68            return 0;
69        }
70    }
71
72    if (offsetToDir) {
73        // add the size of the header, so we will point to the DirEntries
74        *offsetToDir = offset + sizeof(SkSFNTHeader);
75    }
76    return SkEndian_SwapBE16(shared.fSingle.fNumTables);
77}
78
79///////////////////////////////////////////////////////////////////////////////
80
81struct SfntHeader {
82    SfntHeader() : fCount(0), fDir(NULL) {}
83    ~SfntHeader() { sk_free(fDir); }
84
85    /** If it returns true, then fCount and fDir are properly initialized.
86        Note: fDir will point to the raw array of SkSFNTDirEntry values,
87        meaning they will still be in the file's native endianness (BE).
88
89        fDir will be automatically freed when this object is destroyed
90     */
91    bool init(SkStream* stream) {
92        stream->rewind();
93
94        size_t offsetToDir;
95        fCount = count_tables(stream, &offsetToDir);
96        if (0 == fCount) {
97            return false;
98        }
99
100        stream->rewind();
101        if (stream->skip(offsetToDir) != offsetToDir) {
102            return false;
103        }
104
105        size_t size = fCount * sizeof(SkSFNTDirEntry);
106        fDir = reinterpret_cast<SkSFNTDirEntry*>(sk_malloc_throw(size));
107        return stream->read(fDir, size) == size;
108    }
109
110    int             fCount;
111    SkSFNTDirEntry* fDir;
112};
113
114///////////////////////////////////////////////////////////////////////////////
115
116int SkFontStream::GetTableTags(SkStream* stream, SkFontTableTag tags[]) {
117    SfntHeader  header;
118    if (!header.init(stream)) {
119        return 0;
120    }
121
122    if (tags) {
123        for (int i = 0; i < header.fCount; i++) {
124            tags[i] = SkEndian_SwapBE32(header.fDir[i].fTag);
125        }
126    }
127    return header.fCount;
128}
129
130size_t SkFontStream::GetTableData(SkStream* stream, SkFontTableTag tag,
131                                  size_t offset, size_t length, void* data) {
132    SfntHeader  header;
133    if (!header.init(stream)) {
134        return 0;
135    }
136
137    for (int i = 0; i < header.fCount; i++) {
138        if (SkEndian_SwapBE32(header.fDir[i].fTag) == tag) {
139            size_t realOffset = SkEndian_SwapBE32(header.fDir[i].fOffset);
140            size_t realLength = SkEndian_SwapBE32(header.fDir[i].fLength);
141            // now sanity check the caller's offset/length
142            if (offset >= realLength) {
143                return 0;
144            }
145            // if the caller is trusting the length from the file, then a
146            // hostile file might choose a value which would overflow offset +
147            // length.
148            if (offset + length < offset) {
149                return 0;
150            }
151            if (length > realLength - offset) {
152                length = realLength - offset;
153            }
154            if (data) {
155                // skip the stream to the part of the table we want to copy from
156                stream->rewind();
157                size_t bytesToSkip = realOffset + offset;
158                if (stream->skip(bytesToSkip) != bytesToSkip) {
159                    return 0;
160                }
161                if (stream->read(data, length) != length) {
162                    return 0;
163                }
164            }
165            return length;
166        }
167    }
168    return 0;
169}
170