Commit c3d21f50 authored by Florin Malita's avatar Florin Malita Committed by Commit Bot

Refactor OpenType::ValidateTable() to avoid SharedBuffer data flattening

There's no need to go through an intermediate SharedBuffer, because we
start with a flat Vector<char> in FontPlatformData::OpenTypeTable().

Refactor the validation code to use Vector<char> instead of
SharedBuffer.

BUG=728627

Change-Id: I81892c0c5c42b4f2e2abdeeabaca20979cd60eeb
Reviewed-on: https://chromium-review.googlesource.com/544317Reviewed-by: default avatarBen Wagner <bungeman@google.com>
Reviewed-by: default avatarHajime Hoshi <hajimehoshi@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Commit-Queue: Florin Malita <fmalita@chromium.org>
Cr-Commit-Position: refs/heads/master@{#481867}
parent 9c2b7965
......@@ -329,17 +329,15 @@ PassRefPtr<OpenTypeVerticalData> FontPlatformData::VerticalData() const {
*this);
}
PassRefPtr<SharedBuffer> FontPlatformData::OpenTypeTable(
SkFontTableTag tag) const {
RefPtr<SharedBuffer> buffer;
Vector<char> FontPlatformData::OpenTypeTable(SkFontTableTag tag) const {
Vector<char> table_buffer;
const size_t table_size = typeface_->getTableSize(tag);
if (table_size) {
Vector<char> table_buffer(table_size);
table_buffer.resize(table_size);
typeface_->getTableData(tag, 0, table_size, &table_buffer[0]);
buffer = SharedBuffer::AdoptVector(table_buffer);
}
return buffer.Release();
return table_buffer;
}
} // namespace blink
......@@ -34,7 +34,6 @@
#include "SkPaint.h"
#include "SkTypeface.h"
#include "platform/PlatformExport.h"
#include "platform/SharedBuffer.h"
#include "platform/fonts/FontDescription.h"
#include "platform/fonts/FontOrientation.h"
#include "platform/fonts/SmallCapsIterator.h"
......@@ -43,6 +42,7 @@
#include "platform/wtf/Forward.h"
#include "platform/wtf/HashTableDeletedValueType.h"
#include "platform/wtf/RefPtr.h"
#include "platform/wtf/Vector.h"
#include "platform/wtf/text/CString.h"
#include "platform/wtf/text/StringImpl.h"
#include "third_party/skia/include/core/SkRefCnt.h"
......@@ -149,7 +149,7 @@ class PLATFORM_EXPORT FontPlatformData {
bool FontContainsCharacter(UChar32 character);
PassRefPtr<OpenTypeVerticalData> VerticalData() const;
PassRefPtr<SharedBuffer> OpenTypeTable(SkFontTableTag) const;
Vector<char> OpenTypeTable(SkFontTableTag) const;
#if OS(LINUX) || OS(ANDROID)
// The returned styles are all actual styles without
......
......@@ -25,8 +25,8 @@
#ifndef OpenTypeTypes_h
#define OpenTypeTypes_h
#include "platform/SharedBuffer.h"
#include "platform/wtf/ByteOrder.h"
#include "platform/wtf/Vector.h"
namespace blink {
namespace OpenType {
......@@ -64,35 +64,34 @@ typedef UInt16 Offset;
typedef UInt16 GlyphID;
template <typename T>
static const T* ValidateTable(const RefPtr<SharedBuffer>& buffer,
size_t count = 1) {
if (!buffer || buffer->size() < sizeof(T) * count)
return 0;
return reinterpret_cast<const T*>(buffer->Data());
static const T* ValidateTable(const Vector<char>& buffer, size_t count = 1) {
if (buffer.size() < sizeof(T) * count)
return nullptr;
return reinterpret_cast<const T*>(buffer.data());
}
struct TableBase {
DISALLOW_NEW();
protected:
static bool IsValidEnd(const SharedBuffer& buffer, const void* position) {
if (position < buffer.Data())
static bool IsValidEnd(const Vector<char>& buffer, const void* position) {
if (position < buffer.data())
return false;
size_t offset = reinterpret_cast<const char*>(position) - buffer.Data();
size_t offset = reinterpret_cast<const char*>(position) - buffer.data();
return offset <= buffer.size(); // "<=" because end is included as valid
}
template <typename T>
static const T* ValidatePtr(const SharedBuffer& buffer,
static const T* ValidatePtr(const Vector<char>& buffer,
const void* position) {
const T* casted = reinterpret_cast<const T*>(position);
if (!IsValidEnd(buffer, &casted[1]))
return 0;
return nullptr;
return casted;
}
template <typename T>
const T* ValidateOffset(const SharedBuffer& buffer, uint16_t offset) const {
const T* ValidateOffset(const Vector<char>& buffer, uint16_t offset) const {
return ValidatePtr<T>(buffer,
reinterpret_cast<const int8_t*>(this) + offset);
}
......
......@@ -25,7 +25,6 @@
#include "platform/fonts/opentype/OpenTypeVerticalData.h"
#include "SkTypeface.h"
#include "platform/SharedBuffer.h"
#include "platform/fonts/SimpleFontData.h"
#include "platform/fonts/opentype/OpenTypeTypes.h"
#include "platform/geometry/FloatRect.h"
......@@ -132,7 +131,7 @@ OpenTypeVerticalData::OpenTypeVerticalData(
void OpenTypeVerticalData::LoadMetrics(const FontPlatformData& platform_data) {
// Load hhea and hmtx to get x-component of vertical origins.
// If these tables are missing, it's not an OpenType font.
RefPtr<SharedBuffer> buffer = platform_data.OpenTypeTable(OpenType::kHheaTag);
Vector<char> buffer = platform_data.OpenTypeTable(OpenType::kHheaTag);
const OpenType::HheaTable* hhea =
OpenType::ValidateTable<OpenType::HheaTable>(buffer);
if (!hhea)
......@@ -171,7 +170,7 @@ void OpenTypeVerticalData::LoadMetrics(const FontPlatformData& platform_data) {
buffer = platform_data.OpenTypeTable(OpenType::kVORGTag);
const OpenType::VORGTable* vorg =
OpenType::ValidateTable<OpenType::VORGTable>(buffer);
if (vorg && buffer->size() >= vorg->RequiredSize()) {
if (vorg && buffer.size() >= vorg->RequiredSize()) {
default_vert_origin_y_ = vorg->default_vert_origin_y;
uint16_t count_vert_origin_y_metrics = vorg->num_vert_origin_y_metrics;
if (!count_vert_origin_y_metrics) {
......@@ -205,7 +204,7 @@ void OpenTypeVerticalData::LoadMetrics(const FontPlatformData& platform_data) {
return;
size_t size_extra =
buffer->size() - sizeof(OpenType::VmtxTable::Entry) * count_vmtx_entries;
buffer.size() - sizeof(OpenType::VmtxTable::Entry) * count_vmtx_entries;
if (size_extra % sizeof(OpenType::Int16)) {
DLOG(ERROR) << "vmtx has incorrect tsb count";
return;
......
......@@ -22,7 +22,6 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "platform/SharedBuffer.h"
#include "platform/fonts/opentype/OpenTypeTypes.h"
#include "platform/wtf/RefPtr.h"
#include "testing/gtest/include/gtest/gtest.h"
......@@ -34,43 +33,43 @@ struct TestTable : OpenType::TableBase {
OpenType::Int16 ascender;
template <typename T>
const T* ValidateOffset(const SharedBuffer& buffer, uint16_t offset) const {
const T* ValidateOffset(const Vector<char>& buffer, uint16_t offset) const {
return TableBase::ValidateOffset<T>(buffer, offset);
}
};
TEST(OpenTypeVerticalDataTest, ValidateTableTest) {
RefPtr<SharedBuffer> buffer = SharedBuffer::Create(sizeof(TestTable));
Vector<char> buffer(sizeof(TestTable));
const TestTable* table = OpenType::ValidateTable<TestTable>(buffer);
EXPECT_TRUE(table);
buffer = SharedBuffer::Create(sizeof(TestTable) - 1);
buffer = Vector<char>(sizeof(TestTable) - 1);
table = OpenType::ValidateTable<TestTable>(buffer);
EXPECT_FALSE(table);
buffer = SharedBuffer::Create(sizeof(TestTable) + 1);
buffer = Vector<char>(sizeof(TestTable) + 1);
table = OpenType::ValidateTable<TestTable>(buffer);
EXPECT_TRUE(table);
}
TEST(OpenTypeVerticalDataTest, ValidateOffsetTest) {
RefPtr<SharedBuffer> buffer = SharedBuffer::Create(sizeof(TestTable));
Vector<char> buffer(sizeof(TestTable));
const TestTable* table = OpenType::ValidateTable<TestTable>(buffer);
ASSERT_TRUE(table);
// Test overflow
EXPECT_FALSE(table->ValidateOffset<uint8_t>(*buffer, 0xFFFF));
EXPECT_FALSE(table->ValidateOffset<uint8_t>(buffer, 0xFFFF));
// uint8_t is valid for all offsets
for (uint16_t offset = 0; offset < sizeof(TestTable); offset++)
EXPECT_TRUE(table->ValidateOffset<uint8_t>(*buffer, offset));
EXPECT_FALSE(table->ValidateOffset<uint8_t>(*buffer, sizeof(TestTable)));
EXPECT_FALSE(table->ValidateOffset<uint8_t>(*buffer, sizeof(TestTable) + 1));
EXPECT_TRUE(table->ValidateOffset<uint8_t>(buffer, offset));
EXPECT_FALSE(table->ValidateOffset<uint8_t>(buffer, sizeof(TestTable)));
EXPECT_FALSE(table->ValidateOffset<uint8_t>(buffer, sizeof(TestTable) + 1));
// For uint16_t, the last byte is invalid
for (uint16_t offset = 0; offset < sizeof(TestTable) - 1; offset++)
EXPECT_TRUE(table->ValidateOffset<uint16_t>(*buffer, offset));
EXPECT_FALSE(table->ValidateOffset<uint16_t>(*buffer, sizeof(TestTable) - 1));
EXPECT_TRUE(table->ValidateOffset<uint16_t>(buffer, offset));
EXPECT_FALSE(table->ValidateOffset<uint16_t>(buffer, sizeof(TestTable) - 1));
}
} // namespace blink
......@@ -4,6 +4,7 @@
#include "platform/testing/FontTestHelpers.h"
#include "platform/SharedBuffer.h"
#include "platform/fonts/Font.h"
#include "platform/fonts/FontCustomPlatformData.h"
#include "platform/fonts/FontDescription.h"
......
......@@ -31,6 +31,8 @@
#ifndef WTF_ByteOrder_h
#define WTF_ByteOrder_h
#include "platform/wtf/build_config.h"
#if OS(POSIX)
#include <arpa/inet.h>
#endif
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment