1 // Copyright 2017 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "pdf/font_table_linux.h"
14 #include "base/numerics/safe_conversions.h"
15 #include "base/posix/eintr_wrapper.h"
16 #include "base/sys_byteorder.h"
20 // TODO(drott): This should be should be replaced with using FreeType for the
21 // purpose instead of reimplementing table parsing.
22 bool GetFontTable(int fd,
26 size_t* output_length) {
30 size_t data_length = 0; // the length of the file data.
31 off_t data_offset = 0; // the offset of the data in the file.
33 // Get the entire font file.
35 if (fstat(fd, &st) < 0)
37 data_length = base::checked_cast<size_t>(st.st_size);
39 // Get a font table. Read the header to find its offset in the file.
41 ssize_t n = HANDLE_EINTR(
42 pread(fd, &num_tables, sizeof(num_tables), 4 /* skip the font type */));
43 if (n != sizeof(num_tables))
45 // Font data is stored in net (big-endian) order.
46 num_tables = base::NetToHost16(num_tables);
48 // Read the table directory.
49 static const size_t kTableEntrySize = 16;
50 const size_t directory_size = num_tables * kTableEntrySize;
51 std::unique_ptr<uint8_t[]> table_entries(new uint8_t[directory_size]);
52 n = HANDLE_EINTR(pread(fd, table_entries.get(), directory_size,
53 12 /* skip the SFNT header */));
54 if (n != base::checked_cast<ssize_t>(directory_size))
57 for (uint16_t i = 0; i < num_tables; ++i) {
58 uint8_t* entry = table_entries.get() + i * kTableEntrySize;
59 uint32_t tag = *reinterpret_cast<uint32_t*>(entry);
60 if (tag == table_tag) {
61 // Font data is stored in net (big-endian) order.
63 base::NetToHost32(*reinterpret_cast<uint32_t*>(entry + 8));
65 base::NetToHost32(*reinterpret_cast<uint32_t*>(entry + 12));
73 // Clamp |offset| inside the allowable range. This allows the read to succeed
74 // but return 0 bytes.
75 offset = std::min(offset, base::checked_cast<off_t>(data_length));
76 // Make sure it's safe to add the data offset and the caller's logical offset.
77 // Define the maximum positive offset on 32 bit systems.
78 static const off_t kMaxPositiveOffset32 = 0x7FFFFFFF; // 2 GB - 1.
79 if ((offset > kMaxPositiveOffset32 / 2) ||
80 (data_offset > kMaxPositiveOffset32 / 2))
82 data_offset += offset;
83 data_length -= offset;
86 // 'output_length' holds the maximum amount of data the caller can accept.
87 data_length = std::min(data_length, *output_length);
88 ssize_t n = HANDLE_EINTR(pread(fd, output, data_length, data_offset));
89 if (n != base::checked_cast<ssize_t>(data_length))
92 *output_length = data_length;