Avi Drissman | db497b3 | 2022-09-15 19:47:28 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors |
jam | cc96c42 | 2017-02-10 18:50:20 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Lei Zhang | aee2ce9 | 2021-06-21 18:47:54 | [diff] [blame] | 5 | #include "pdf/font_table_linux.h" |
jam | cc96c42 | 2017-02-10 18:50:20 | [diff] [blame] | 6 | |
jam | cc96c42 | 2017-02-10 18:50:20 | [diff] [blame] | 7 | #include <sys/stat.h> |
Lei Zhang | 76a0ca2 | 2021-12-11 15:10:54 | [diff] [blame] | 8 | #include <unistd.h> |
jam | cc96c42 | 2017-02-10 18:50:20 | [diff] [blame] | 9 | |
Tom Sepez | b03f3f8 | 2021-12-09 23:43:38 | [diff] [blame] | 10 | #include <algorithm> |
jam | cc96c42 | 2017-02-10 18:50:20 | [diff] [blame] | 11 | #include <limits> |
| 12 | #include <memory> |
| 13 | |
| 14 | #include "base/numerics/safe_conversions.h" |
| 15 | #include "base/posix/eintr_wrapper.h" |
| 16 | #include "base/sys_byteorder.h" |
jam | cc96c42 | 2017-02-10 18:50:20 | [diff] [blame] | 17 | |
Lei Zhang | aee2ce9 | 2021-06-21 18:47:54 | [diff] [blame] | 18 | namespace pdf { |
jam | cc96c42 | 2017-02-10 18:50:20 | [diff] [blame] | 19 | |
Lei Zhang | aee2ce9 | 2021-06-21 18:47:54 | [diff] [blame] | 20 | // TODO(drott): This should be should be replaced with using FreeType for the |
| 21 | // purpose instead of reimplementing table parsing. |
jam | cc96c42 | 2017-02-10 18:50:20 | [diff] [blame] | 22 | bool GetFontTable(int fd, |
| 23 | uint32_t table_tag, |
| 24 | off_t offset, |
| 25 | uint8_t* output, |
| 26 | size_t* output_length) { |
| 27 | if (offset < 0) |
| 28 | return false; |
| 29 | |
| 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. |
| 32 | if (table_tag == 0) { |
| 33 | // Get the entire font file. |
| 34 | struct stat st; |
| 35 | if (fstat(fd, &st) < 0) |
| 36 | return false; |
| 37 | data_length = base::checked_cast<size_t>(st.st_size); |
| 38 | } else { |
| 39 | // Get a font table. Read the header to find its offset in the file. |
| 40 | uint16_t num_tables; |
| 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)) |
| 44 | return false; |
| 45 | // Font data is stored in net (big-endian) order. |
| 46 | num_tables = base::NetToHost16(num_tables); |
| 47 | |
| 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)) |
| 55 | return false; |
| 56 | |
| 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. |
| 62 | data_offset = |
| 63 | base::NetToHost32(*reinterpret_cast<uint32_t*>(entry + 8)); |
| 64 | data_length = |
| 65 | base::NetToHost32(*reinterpret_cast<uint32_t*>(entry + 12)); |
| 66 | break; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | if (!data_length) |
| 72 | return false; |
| 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)) |
| 81 | return false; |
| 82 | data_offset += offset; |
| 83 | data_length -= offset; |
| 84 | |
| 85 | if (output) { |
| 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)) |
| 90 | return false; |
| 91 | } |
| 92 | *output_length = data_length; |
| 93 | |
| 94 | return true; |
| 95 | } |
| 96 | |
Lei Zhang | aee2ce9 | 2021-06-21 18:47:54 | [diff] [blame] | 97 | } // namespace pdf |