tagging audio streams and changing audio sink to pulseaudio
[profile/ivi/webkit-efl.git] / Source / WebCore / platform / graphics / WOFFFileFormat.cpp
1 /*
2  * Copyright (C) 2010 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
24  */
25
26 #include "config.h"
27 #include "WOFFFileFormat.h"
28 #include <zlib.h>
29
30 #if !USE(OPENTYPE_SANITIZER)
31
32 #include "SharedBuffer.h"
33
34 #if OS(UNIX)
35 #include <netinet/in.h>
36 #endif
37
38 #if OS(WINDOWS)
39 #if CPU(BIG_ENDIAN)
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)
49 #else
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)
55 #endif
56 #endif // OS(WINDOWS)
57
58 namespace WebCore {
59
60 static bool readUInt32(SharedBuffer* buffer, size_t& offset, uint32_t& value)
61 {
62     ASSERT_ARG(offset, offset <= buffer->size());
63     if (buffer->size() - offset < sizeof(value))
64         return false;
65
66     value = ntohl(*reinterpret_cast_ptr<const uint32_t*>(buffer->data() + offset));
67     offset += sizeof(value);
68
69     return true;
70 }
71
72 static bool readUInt16(SharedBuffer* buffer, size_t& offset, uint16_t& value)
73 {
74     ASSERT_ARG(offset, offset <= buffer->size());
75     if (buffer->size() - offset < sizeof(value))
76         return false;
77
78     value = ntohs(*reinterpret_cast_ptr<const uint16_t*>(buffer->data() + offset));
79     offset += sizeof(value);
80
81     return true;
82 }
83
84 static bool writeUInt32(Vector<char>& vector, uint32_t value)
85 {
86     uint32_t bigEndianValue = htonl(value);
87     return vector.tryAppend(reinterpret_cast_ptr<char*>(&bigEndianValue), sizeof(bigEndianValue));
88 }
89
90 static bool writeUInt16(Vector<char>& vector, uint16_t value)
91 {
92     uint16_t bigEndianValue = htons(value);
93     return vector.tryAppend(reinterpret_cast_ptr<char*>(&bigEndianValue), sizeof(bigEndianValue));
94 }
95
96 static const uint32_t woffSignature = 0x774f4646; /* 'wOFF' */
97
98 bool isWOFF(SharedBuffer* buffer)
99 {
100     size_t offset = 0;
101     uint32_t signature;
102
103     return readUInt32(buffer, offset, signature) && signature == woffSignature;
104 }
105
106 bool convertWOFFToSfnt(SharedBuffer* woff, Vector<char>& sfnt)
107 {
108     ASSERT_ARG(sfnt, sfnt.isEmpty());
109
110     size_t offset = 0;
111
112     // Read the WOFF header.
113     uint32_t signature;
114     if (!readUInt32(woff, offset, signature) || signature != woffSignature) {
115         ASSERT_NOT_REACHED();
116         return false;
117     }
118
119     uint32_t flavor;
120     if (!readUInt32(woff, offset, flavor))
121         return false;
122
123     uint32_t length;
124     if (!readUInt32(woff, offset, length) || length != woff->size())
125         return false;
126
127     uint16_t numTables;
128     if (!readUInt16(woff, offset, numTables))
129         return false;
130
131     if (!numTables || numTables > 0x0fff)
132         return false;
133
134     uint16_t reserved;
135     if (!readUInt16(woff, offset, reserved) || reserved)
136         return false;
137
138     uint32_t totalSfntSize;
139     if (!readUInt32(woff, offset, totalSfntSize))
140         return false;
141
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))
143         return false;
144
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
152
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))
155         return false;
156
157     // Write the sfnt offset subtable.
158     uint16_t entrySelector = 0;
159     uint16_t searchRange = 1;
160     while (searchRange < numTables >> 1) {
161         entrySelector++;
162         searchRange <<= 1;
163     }
164     searchRange <<= 4;
165     uint16_t rangeShift = (numTables << 4) - searchRange;
166
167     if (!writeUInt32(sfnt, flavor)
168         || !writeUInt16(sfnt, numTables)
169         || !writeUInt16(sfnt, searchRange)
170         || !writeUInt16(sfnt, entrySelector)
171         || !writeUInt16(sfnt, rangeShift))
172         return false;
173
174     if (sfnt.size() > totalSfntSize)
175         return false;
176
177     if (totalSfntSize - sfnt.size() < numTables * 4 * sizeof(uint32_t))
178         return false;
179
180     size_t sfntTableDirectoryCursor = sfnt.size();
181     sfnt.grow(sfnt.size() + numTables * 4 * sizeof(uint32_t));
182
183     // Process tables.
184     for (uint16_t i = 0; i < numTables; ++i) {
185         // Read a WOFF table directory entry.
186         uint32_t tableTag;
187         if (!readUInt32(woff, offset, tableTag))
188             return false;
189
190         uint32_t tableOffset;
191         if (!readUInt32(woff, offset, tableOffset))
192             return false;
193
194         uint32_t tableCompLength;
195         if (!readUInt32(woff, offset, tableCompLength))
196             return false;
197
198         if (tableOffset > woff->size() || tableCompLength > woff->size() - tableOffset)
199             return false;
200
201         uint32_t tableOrigLength;
202         if (!readUInt32(woff, offset, tableOrigLength) || tableCompLength > tableOrigLength)
203             return false;
204
205         if (tableOrigLength > totalSfntSize || sfnt.size() > totalSfntSize - tableOrigLength)
206             return false;
207
208         uint32_t tableOrigChecksum;
209         if (!readUInt32(woff, offset, tableOrigChecksum))
210             return false;
211
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);
219
220         if (tableCompLength == tableOrigLength) {
221             // The table is not compressed.
222             if (!sfnt.tryAppend(woff->data() + tableOffset, tableCompLength))
223                 return false;
224         } else {
225             uLongf destLen = tableOrigLength;
226             if (!sfnt.tryReserveCapacity(sfnt.size() + tableOrigLength))
227                 return false;
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)
231                 return false;
232             if (destLen != tableOrigLength)
233                 return false;
234         }
235
236         // Pad to a multiple of 4 bytes.
237         while (sfnt.size() % 4)
238             sfnt.append(0);
239     }
240
241     return sfnt.size() == totalSfntSize;
242 }
243     
244 } // namespace WebCore
245
246 #endif // !USE(OPENTYPE_SANITIZER)