font_table_linux.cc 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "pdf/font_table_linux.h"
  5. #include <sys/stat.h>
  6. #include <unistd.h>
  7. #include <algorithm>
  8. #include <limits>
  9. #include <memory>
  10. #include "base/numerics/safe_conversions.h"
  11. #include "base/posix/eintr_wrapper.h"
  12. #include "base/sys_byteorder.h"
  13. namespace pdf {
  14. // TODO(drott): This should be should be replaced with using FreeType for the
  15. // purpose instead of reimplementing table parsing.
  16. bool GetFontTable(int fd,
  17. uint32_t table_tag,
  18. off_t offset,
  19. uint8_t* output,
  20. size_t* output_length) {
  21. if (offset < 0)
  22. return false;
  23. size_t data_length = 0; // the length of the file data.
  24. off_t data_offset = 0; // the offset of the data in the file.
  25. if (table_tag == 0) {
  26. // Get the entire font file.
  27. struct stat st;
  28. if (fstat(fd, &st) < 0)
  29. return false;
  30. data_length = base::checked_cast<size_t>(st.st_size);
  31. } else {
  32. // Get a font table. Read the header to find its offset in the file.
  33. uint16_t num_tables;
  34. ssize_t n = HANDLE_EINTR(
  35. pread(fd, &num_tables, sizeof(num_tables), 4 /* skip the font type */));
  36. if (n != sizeof(num_tables))
  37. return false;
  38. // Font data is stored in net (big-endian) order.
  39. num_tables = base::NetToHost16(num_tables);
  40. // Read the table directory.
  41. static const size_t kTableEntrySize = 16;
  42. const size_t directory_size = num_tables * kTableEntrySize;
  43. std::unique_ptr<uint8_t[]> table_entries(new uint8_t[directory_size]);
  44. n = HANDLE_EINTR(pread(fd, table_entries.get(), directory_size,
  45. 12 /* skip the SFNT header */));
  46. if (n != base::checked_cast<ssize_t>(directory_size))
  47. return false;
  48. for (uint16_t i = 0; i < num_tables; ++i) {
  49. uint8_t* entry = table_entries.get() + i * kTableEntrySize;
  50. uint32_t tag = *reinterpret_cast<uint32_t*>(entry);
  51. if (tag == table_tag) {
  52. // Font data is stored in net (big-endian) order.
  53. data_offset =
  54. base::NetToHost32(*reinterpret_cast<uint32_t*>(entry + 8));
  55. data_length =
  56. base::NetToHost32(*reinterpret_cast<uint32_t*>(entry + 12));
  57. break;
  58. }
  59. }
  60. }
  61. if (!data_length)
  62. return false;
  63. // Clamp |offset| inside the allowable range. This allows the read to succeed
  64. // but return 0 bytes.
  65. offset = std::min(offset, base::checked_cast<off_t>(data_length));
  66. // Make sure it's safe to add the data offset and the caller's logical offset.
  67. // Define the maximum positive offset on 32 bit systems.
  68. static const off_t kMaxPositiveOffset32 = 0x7FFFFFFF; // 2 GB - 1.
  69. if ((offset > kMaxPositiveOffset32 / 2) ||
  70. (data_offset > kMaxPositiveOffset32 / 2))
  71. return false;
  72. data_offset += offset;
  73. data_length -= offset;
  74. if (output) {
  75. // 'output_length' holds the maximum amount of data the caller can accept.
  76. data_length = std::min(data_length, *output_length);
  77. ssize_t n = HANDLE_EINTR(pread(fd, output, data_length, data_offset));
  78. if (n != base::checked_cast<ssize_t>(data_length))
  79. return false;
  80. }
  81. *output_length = data_length;
  82. return true;
  83. }
  84. } // namespace pdf