2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
27 #include "WOFFFileFormat.h"
30 #if !USE(OPENTYPE_SANITIZER)
32 #include "SharedBuffer.h"
35 #include <netinet/in.h>
40 #define ntohs(x) ((uint16_t)(x))
41 #define htons(x) ((uint16_t)(x))
42 #define ntohl(x) ((uint32_t)(x))
43 #define htonl(x) ((uint32_t)(x))
44 #elif CPU(MIDDLE_ENDIAN)
45 #define ntohs(x) ((unit16_t)(x))
46 #define htons(x) ((uint16_t)(x))
47 #define ntohl(x) ((uint32_t)((((uint32_t)(x) & 0xffff0000) >> 16) | (((uint32_t)(x) & 0xffff) << 16))
48 #define htonl(x) ntohl(x)
50 #define ntohs(x) ((uint16_t)((((uint16_t)(x) & 0xff00) >> 8) | (((uint16_t)(x) & 0x00ff) << 8)))
51 #define htons(x) ntohs(x)
52 #define ntohl(x) ((uint32_t)((((uint32_t)(x) & 0xff000000) >> 24) | (((uint32_t)(x) & 0x00ff0000) >> 8) | \
53 (((uint32_t)(x) & 0x0000ff00) << 8) | (((uint32_t)(x) & 0x000000ff) << 24)))
54 #define htonl(x) ntohl(x)
60 static bool readUInt32(SharedBuffer* buffer, size_t& offset, uint32_t& value)
62 ASSERT_ARG(offset, offset <= buffer->size());
63 if (buffer->size() - offset < sizeof(value))
66 value = ntohl(*reinterpret_cast_ptr<const uint32_t*>(buffer->data() + offset));
67 offset += sizeof(value);
72 static bool readUInt16(SharedBuffer* buffer, size_t& offset, uint16_t& value)
74 ASSERT_ARG(offset, offset <= buffer->size());
75 if (buffer->size() - offset < sizeof(value))
78 value = ntohs(*reinterpret_cast_ptr<const uint16_t*>(buffer->data() + offset));
79 offset += sizeof(value);
84 static bool writeUInt32(Vector<char>& vector, uint32_t value)
86 uint32_t bigEndianValue = htonl(value);
87 return vector.tryAppend(reinterpret_cast_ptr<char*>(&bigEndianValue), sizeof(bigEndianValue));
90 static bool writeUInt16(Vector<char>& vector, uint16_t value)
92 uint16_t bigEndianValue = htons(value);
93 return vector.tryAppend(reinterpret_cast_ptr<char*>(&bigEndianValue), sizeof(bigEndianValue));
96 static const uint32_t woffSignature = 0x774f4646; /* 'wOFF' */
98 bool isWOFF(SharedBuffer* buffer)
103 return readUInt32(buffer, offset, signature) && signature == woffSignature;
106 bool convertWOFFToSfnt(SharedBuffer* woff, Vector<char>& sfnt)
108 ASSERT_ARG(sfnt, sfnt.isEmpty());
112 // Read the WOFF header.
114 if (!readUInt32(woff, offset, signature) || signature != woffSignature) {
115 ASSERT_NOT_REACHED();
120 if (!readUInt32(woff, offset, flavor))
124 if (!readUInt32(woff, offset, length) || length != woff->size())
128 if (!readUInt16(woff, offset, numTables))
131 if (!numTables || numTables > 0x0fff)
135 if (!readUInt16(woff, offset, reserved) || reserved)
138 uint32_t totalSfntSize;
139 if (!readUInt32(woff, offset, totalSfntSize))
142 if (woff->size() - offset < sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t) + sizeof(uint32_t) + sizeof(uint32_t) + sizeof(uint32_t) + sizeof(uint32_t))
145 offset += sizeof(uint16_t); // majorVersion
146 offset += sizeof(uint16_t); // minorVersion
147 offset += sizeof(uint32_t); // metaOffset
148 offset += sizeof(uint32_t); // metaLength
149 offset += sizeof(uint32_t); // metaOrigLength
150 offset += sizeof(uint32_t); // privOffset
151 offset += sizeof(uint32_t); // privLength
153 // Check if the WOFF can supply as many tables as it claims it has.
154 if (woff->size() - offset < numTables * 5 * sizeof(uint32_t))
157 // Write the sfnt offset subtable.
158 uint16_t entrySelector = 0;
159 uint16_t searchRange = 1;
160 while (searchRange < numTables >> 1) {
165 uint16_t rangeShift = (numTables << 4) - searchRange;
167 if (!writeUInt32(sfnt, flavor)
168 || !writeUInt16(sfnt, numTables)
169 || !writeUInt16(sfnt, searchRange)
170 || !writeUInt16(sfnt, entrySelector)
171 || !writeUInt16(sfnt, rangeShift))
174 if (sfnt.size() > totalSfntSize)
177 if (totalSfntSize - sfnt.size() < numTables * 4 * sizeof(uint32_t))
180 size_t sfntTableDirectoryCursor = sfnt.size();
181 sfnt.grow(sfnt.size() + numTables * 4 * sizeof(uint32_t));
184 for (uint16_t i = 0; i < numTables; ++i) {
185 // Read a WOFF table directory entry.
187 if (!readUInt32(woff, offset, tableTag))
190 uint32_t tableOffset;
191 if (!readUInt32(woff, offset, tableOffset))
194 uint32_t tableCompLength;
195 if (!readUInt32(woff, offset, tableCompLength))
198 if (tableOffset > woff->size() || tableCompLength > woff->size() - tableOffset)
201 uint32_t tableOrigLength;
202 if (!readUInt32(woff, offset, tableOrigLength) || tableCompLength > tableOrigLength)
205 if (tableOrigLength > totalSfntSize || sfnt.size() > totalSfntSize - tableOrigLength)
208 uint32_t tableOrigChecksum;
209 if (!readUInt32(woff, offset, tableOrigChecksum))
212 // Write an sfnt table directory entry.
213 uint32_t* sfntTableDirectoryPtr = reinterpret_cast_ptr<uint32_t*>(sfnt.data() + sfntTableDirectoryCursor);
214 *sfntTableDirectoryPtr++ = htonl(tableTag);
215 *sfntTableDirectoryPtr++ = htonl(tableOrigChecksum);
216 *sfntTableDirectoryPtr++ = htonl(sfnt.size());
217 *sfntTableDirectoryPtr++ = htonl(tableOrigLength);
218 sfntTableDirectoryCursor += 4 * sizeof(uint32_t);
220 if (tableCompLength == tableOrigLength) {
221 // The table is not compressed.
222 if (!sfnt.tryAppend(woff->data() + tableOffset, tableCompLength))
225 uLongf destLen = tableOrigLength;
226 if (!sfnt.tryReserveCapacity(sfnt.size() + tableOrigLength))
228 Bytef* dest = reinterpret_cast<Bytef*>(sfnt.end());
229 sfnt.grow(sfnt.size() + tableOrigLength);
230 if (uncompress(dest, &destLen, reinterpret_cast<const Bytef*>(woff->data() + tableOffset), tableCompLength) != Z_OK)
232 if (destLen != tableOrigLength)
236 // Pad to a multiple of 4 bytes.
237 while (sfnt.size() % 4)
241 return sfnt.size() == totalSfntSize;
244 } // namespace WebCore
246 #endif // !USE(OPENTYPE_SANITIZER)