27#ifdef FT_CONFIG_OPTION_USE_ZLIB
37#define FT_COMPONENT sfwoff
40#define WRITE_USHORT( p, v ) \
43 *(p)++ = (FT_Byte)( (v) >> 8 ); \
44 *(p)++ = (FT_Byte)( (v) >> 0 ); \
48#define WRITE_ULONG( p, v ) \
51 *(p)++ = (FT_Byte)( (v) >> 24 ); \
52 *(p)++ = (FT_Byte)( (v) >> 16 ); \
53 *(p)++ = (FT_Byte)( (v) >> 8 ); \
54 *(p)++ = (FT_Byte)( (v) >> 0 ); \
60 sfnt_stream_close( FT_Stream stream )
62 FT_Memory memory = stream->memory;
65 FT_FREE( stream->base );
73 compare_offsets(
const void* a,
76 WOFF_Table table1 = *(WOFF_Table*)a;
77 WOFF_Table table2 = *(WOFF_Table*)b;
79 FT_ULong offset1 = table1->Offset;
80 FT_ULong offset2 = table2->Offset;
83 if ( offset1 > offset2 )
85 else if ( offset1 < offset2 )
95 FT_LOCAL_DEF( FT_Error )
96 woff_open_font( FT_Stream stream,
99 FT_Memory memory = stream->memory;
100 FT_Error error = FT_Err_Ok;
103 WOFF_Table tables = NULL;
104 WOFF_Table* indices = NULL;
106 FT_ULong woff_offset;
108 FT_Byte* sfnt = NULL;
109 FT_Stream sfnt_stream = NULL;
111 FT_Byte* sfnt_header;
112 FT_ULong sfnt_offset;
117 static const FT_Frame_Field woff_header_fields[] =
120#define FT_STRUCTURE WOFF_HeaderRec
122 FT_FRAME_START( 44 ),
123 FT_FRAME_ULONG ( signature ),
124 FT_FRAME_ULONG ( flavor ),
125 FT_FRAME_ULONG ( length ),
126 FT_FRAME_USHORT( num_tables ),
127 FT_FRAME_USHORT( reserved ),
128 FT_FRAME_ULONG ( totalSfntSize ),
129 FT_FRAME_USHORT( majorVersion ),
130 FT_FRAME_USHORT( minorVersion ),
131 FT_FRAME_ULONG ( metaOffset ),
132 FT_FRAME_ULONG ( metaLength ),
133 FT_FRAME_ULONG ( metaOrigLength ),
134 FT_FRAME_ULONG ( privOffset ),
135 FT_FRAME_ULONG ( privLength ),
140 FT_ASSERT( stream == face->root.stream );
141 FT_ASSERT( FT_STREAM_POS() == 0 );
143 if ( FT_STREAM_READ_FIELDS( woff_header_fields, &woff ) )
147 if ( woff.flavor == TTAG_wOFF || woff.flavor == TTAG_ttcf )
148 return FT_THROW( Invalid_Table );
151 if ( woff.length != stream->size ||
152 woff.num_tables == 0 ||
153 woff.num_tables > 0xFFFU ||
154 44 + woff.num_tables * 20UL >= woff.length ||
155 12 + woff.num_tables * 16UL >= woff.totalSfntSize ||
156 ( woff.totalSfntSize & 3 ) != 0 ||
157 ( woff.metaOffset == 0 && ( woff.metaLength != 0 ||
158 woff.metaOrigLength != 0 ) ) ||
159 ( woff.metaLength != 0 && woff.metaOrigLength == 0 ) ||
160 ( woff.privOffset == 0 && woff.privLength != 0 ) )
162 FT_ERROR((
"woff_font_open: invalid WOFF header\n" ));
163 return FT_THROW( Invalid_Table );
167 if ( FT_QALLOC( sfnt, 12 ) || FT_NEW( sfnt_stream ) )
174 FT_Int entrySelector = FT_MSB( woff.num_tables );
175 FT_Int searchRange = ( 1 << entrySelector ) * 16;
176 FT_Int rangeShift = woff.num_tables * 16 - searchRange;
179 WRITE_ULONG ( sfnt_header, woff.flavor );
180 WRITE_USHORT( sfnt_header, woff.num_tables );
181 WRITE_USHORT( sfnt_header, searchRange );
182 WRITE_USHORT( sfnt_header, entrySelector );
183 WRITE_USHORT( sfnt_header, rangeShift );
190 if ( FT_QNEW_ARRAY( tables, woff.num_tables ) ||
191 FT_QNEW_ARRAY( indices, woff.num_tables ) )
195 FT_TRACE2((
" tag offset compLen origLen checksum\n" ));
196 FT_TRACE2((
" -------------------------------------------\n" ));
198 if ( FT_FRAME_ENTER( 20L * woff.num_tables ) )
201 for ( nn = 0; nn < woff.num_tables; nn++ )
203 WOFF_Table table = tables + nn;
205 table->Tag = FT_GET_TAG4();
206 table->Offset = FT_GET_ULONG();
207 table->CompLength = FT_GET_ULONG();
208 table->OrigLength = FT_GET_ULONG();
209 table->CheckSum = FT_GET_ULONG();
211 FT_TRACE2((
" %c%c%c%c %08lx %08lx %08lx %08lx\n",
212 (FT_Char)( table->Tag >> 24 ),
213 (FT_Char)( table->Tag >> 16 ),
214 (FT_Char)( table->Tag >> 8 ),
215 (FT_Char)( table->Tag ),
221 if ( table->Tag <= old_tag )
225 FT_ERROR((
"woff_font_open: table tags are not sorted\n" ));
226 error = FT_THROW( Invalid_Table );
230 old_tag = table->Tag;
240 sizeof ( WOFF_Table ),
245 woff_offset = 44 + woff.num_tables * 20L;
246 sfnt_offset = 12 + woff.num_tables * 16L;
248 for ( nn = 0; nn < woff.num_tables; nn++ )
250 WOFF_Table table = indices[nn];
253 if ( table->Offset != woff_offset ||
254 table->CompLength > woff.length ||
255 table->Offset > woff.length - table->CompLength ||
256 table->OrigLength > woff.totalSfntSize ||
257 sfnt_offset > woff.totalSfntSize - table->OrigLength ||
258 table->CompLength > table->OrigLength )
260 FT_ERROR((
"woff_font_open: invalid table offsets\n" ));
261 error = FT_THROW( Invalid_Table );
265 table->OrigOffset = sfnt_offset;
268 woff_offset += ( table->CompLength + 3 ) & ~3U;
269 sfnt_offset += ( table->OrigLength + 3 ) & ~3U;
281 if ( woff.metaOffset )
283 if ( woff.metaOffset != woff_offset ||
284 woff.metaOffset + woff.metaLength > woff.length )
286 FT_ERROR((
"woff_font_open:"
287 " invalid `metadata' offset or length\n" ));
288 error = FT_THROW( Invalid_Table );
293 woff_offset += woff.metaLength;
296 if ( woff.privOffset )
299 woff_offset = ( woff_offset + 3 ) & ~3U;
301 if ( woff.privOffset != woff_offset ||
302 woff.privOffset + woff.privLength > woff.length )
304 FT_ERROR((
"woff_font_open: invalid `private' offset or length\n" ));
305 error = FT_THROW( Invalid_Table );
310 woff_offset += woff.privLength;
313 if ( sfnt_offset != woff.totalSfntSize ||
314 woff_offset != woff.length )
316 FT_ERROR((
"woff_font_open: invalid `sfnt' table structure\n" ));
317 error = FT_THROW( Invalid_Table );
322 if ( FT_QREALLOC( sfnt, 12, woff.totalSfntSize ) )
325 sfnt_header = sfnt + 12;
329 for ( nn = 0; nn < woff.num_tables; nn++ )
331 WOFF_Table table = tables + nn;
335 WRITE_ULONG( sfnt_header, table->Tag );
336 WRITE_ULONG( sfnt_header, table->CheckSum );
337 WRITE_ULONG( sfnt_header, table->OrigOffset );
338 WRITE_ULONG( sfnt_header, table->OrigLength );
341 if ( FT_STREAM_SEEK( table->Offset ) ||
342 FT_FRAME_ENTER( table->CompLength ) )
345 if ( table->CompLength == table->OrigLength )
348 ft_memcpy( sfnt + table->OrigOffset,
355 FT_ULong output_len = table->OrigLength;
358 error = FT_Gzip_Uncompress( memory,
359 sfnt + table->OrigOffset, &output_len,
360 stream->cursor, table->CompLength );
363 if ( output_len != table->OrigLength )
365 FT_ERROR((
"woff_font_open: compressed table length mismatch\n" ));
366 error = FT_THROW( Invalid_Table );
375 sfnt_offset = table->OrigOffset + table->OrigLength;
376 while ( sfnt_offset & 3 )
378 sfnt[sfnt_offset] =
'\0';
384 FT_Stream_OpenMemory( sfnt_stream, sfnt, woff.totalSfntSize );
385 sfnt_stream->memory = stream->memory;
386 sfnt_stream->close = sfnt_stream_close;
390 ( face->root.face_flags & FT_FACE_FLAG_EXTERNAL_STREAM ) != 0 );
392 face->root.stream = sfnt_stream;
394 face->root.face_flags &= ~FT_FACE_FLAG_EXTERNAL_STREAM;
403 FT_Stream_Close( sfnt_stream );
404 FT_FREE( sfnt_stream );
421 typedef int sfwoff_dummy_;