Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright 2012 Google Inc. |
michael@0 | 3 | * |
michael@0 | 4 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 5 | * found in the LICENSE file. |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | #include "SkData.h" |
michael@0 | 9 | #include "SkEndian.h" |
michael@0 | 10 | #include "SkSFNTHeader.h" |
michael@0 | 11 | #include "SkStream.h" |
michael@0 | 12 | #include "SkOTTable_head.h" |
michael@0 | 13 | #include "SkOTTable_name.h" |
michael@0 | 14 | #include "SkOTTableTypes.h" |
michael@0 | 15 | #include "SkOTUtils.h" |
michael@0 | 16 | |
michael@0 | 17 | extern const uint8_t SK_OT_GlyphData_NoOutline[] = { |
michael@0 | 18 | 0x0,0x0, //SkOTTableGlyphData::numberOfContours |
michael@0 | 19 | 0x0,0x0, //SkOTTableGlyphData::xMin |
michael@0 | 20 | 0x0,0x0, //SkOTTableGlyphData::yMin |
michael@0 | 21 | 0x0,0x0, //SkOTTableGlyphData::xMax |
michael@0 | 22 | 0x0,0x0, //SkOTTableGlyphData::yMax |
michael@0 | 23 | |
michael@0 | 24 | 0x0,0x0, //SkOTTableGlyphDataInstructions::length |
michael@0 | 25 | }; |
michael@0 | 26 | |
michael@0 | 27 | uint32_t SkOTUtils::CalcTableChecksum(SK_OT_ULONG *data, size_t length) { |
michael@0 | 28 | uint32_t sum = 0; |
michael@0 | 29 | SK_OT_ULONG *dataEnd = data + ((length + 3) & ~3) / sizeof(SK_OT_ULONG); |
michael@0 | 30 | for (; data < dataEnd; ++data) { |
michael@0 | 31 | sum += SkEndian_SwapBE32(*data); |
michael@0 | 32 | } |
michael@0 | 33 | return sum; |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | SkData* SkOTUtils::RenameFont(SkStream* fontData, const char* fontName, int fontNameLen) { |
michael@0 | 37 | |
michael@0 | 38 | // Get the sfnt header. |
michael@0 | 39 | SkSFNTHeader sfntHeader; |
michael@0 | 40 | if (fontData->read(&sfntHeader, sizeof(sfntHeader)) < sizeof(sfntHeader)) { |
michael@0 | 41 | return NULL; |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | // Find the existing 'name' table. |
michael@0 | 45 | int tableIndex; |
michael@0 | 46 | SkSFNTHeader::TableDirectoryEntry tableEntry; |
michael@0 | 47 | int numTables = SkEndian_SwapBE16(sfntHeader.numTables); |
michael@0 | 48 | for (tableIndex = 0; tableIndex < numTables; ++tableIndex) { |
michael@0 | 49 | if (fontData->read(&tableEntry, sizeof(tableEntry)) < sizeof(tableEntry)) { |
michael@0 | 50 | return NULL; |
michael@0 | 51 | } |
michael@0 | 52 | if (SkOTTableName::TAG == tableEntry.tag) { |
michael@0 | 53 | break; |
michael@0 | 54 | } |
michael@0 | 55 | } |
michael@0 | 56 | if (tableIndex == numTables) { |
michael@0 | 57 | return NULL; |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | if (!fontData->rewind()) { |
michael@0 | 61 | return NULL; |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | // The required 'name' record types: Family, Style, Unique, Full and PostScript. |
michael@0 | 65 | const SkOTTableName::Record::NameID::Predefined::Value namesToCreate[] = { |
michael@0 | 66 | SkOTTableName::Record::NameID::Predefined::FontFamilyName, |
michael@0 | 67 | SkOTTableName::Record::NameID::Predefined::FontSubfamilyName, |
michael@0 | 68 | SkOTTableName::Record::NameID::Predefined::UniqueFontIdentifier, |
michael@0 | 69 | SkOTTableName::Record::NameID::Predefined::FullFontName, |
michael@0 | 70 | SkOTTableName::Record::NameID::Predefined::PostscriptName, |
michael@0 | 71 | }; |
michael@0 | 72 | const int namesCount = SK_ARRAY_COUNT(namesToCreate); |
michael@0 | 73 | |
michael@0 | 74 | // Copy the data, leaving out the old name table. |
michael@0 | 75 | // In theory, we could also remove the DSIG table if it exists. |
michael@0 | 76 | size_t nameTableLogicalSize = sizeof(SkOTTableName) + (namesCount * sizeof(SkOTTableName::Record)) + (fontNameLen * sizeof(wchar_t)); |
michael@0 | 77 | size_t nameTablePhysicalSize = (nameTableLogicalSize + 3) & ~3; // Rounded up to a multiple of 4. |
michael@0 | 78 | |
michael@0 | 79 | size_t oldNameTablePhysicalSize = (SkEndian_SwapBE32(tableEntry.logicalLength) + 3) & ~3; // Rounded up to a multiple of 4. |
michael@0 | 80 | size_t oldNameTableOffset = SkEndian_SwapBE32(tableEntry.offset); |
michael@0 | 81 | |
michael@0 | 82 | //originalDataSize is the size of the original data without the name table. |
michael@0 | 83 | size_t originalDataSize = fontData->getLength() - oldNameTablePhysicalSize; |
michael@0 | 84 | size_t newDataSize = originalDataSize + nameTablePhysicalSize; |
michael@0 | 85 | |
michael@0 | 86 | SK_OT_BYTE* data = static_cast<SK_OT_BYTE*>(sk_malloc_throw(newDataSize)); |
michael@0 | 87 | SkAutoTUnref<SkData> rewrittenFontData(SkData::NewFromMalloc(data, newDataSize)); |
michael@0 | 88 | |
michael@0 | 89 | if (fontData->read(data, oldNameTableOffset) < oldNameTableOffset) { |
michael@0 | 90 | return NULL; |
michael@0 | 91 | } |
michael@0 | 92 | if (fontData->skip(oldNameTablePhysicalSize) < oldNameTablePhysicalSize) { |
michael@0 | 93 | return NULL; |
michael@0 | 94 | } |
michael@0 | 95 | if (fontData->read(data + oldNameTableOffset, originalDataSize - oldNameTableOffset) < originalDataSize - oldNameTableOffset) { |
michael@0 | 96 | return NULL; |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | //Fix up the offsets of the directory entries after the old 'name' table entry. |
michael@0 | 100 | SkSFNTHeader::TableDirectoryEntry* currentEntry = reinterpret_cast<SkSFNTHeader::TableDirectoryEntry*>(data + sizeof(SkSFNTHeader)); |
michael@0 | 101 | SkSFNTHeader::TableDirectoryEntry* endEntry = currentEntry + numTables; |
michael@0 | 102 | SkSFNTHeader::TableDirectoryEntry* headTableEntry = NULL; |
michael@0 | 103 | for (; currentEntry < endEntry; ++currentEntry) { |
michael@0 | 104 | uint32_t oldOffset = SkEndian_SwapBE32(currentEntry->offset); |
michael@0 | 105 | if (oldOffset > oldNameTableOffset) { |
michael@0 | 106 | currentEntry->offset = SkEndian_SwapBE32(oldOffset - oldNameTablePhysicalSize); |
michael@0 | 107 | } |
michael@0 | 108 | if (SkOTTableHead::TAG == currentEntry->tag) { |
michael@0 | 109 | headTableEntry = currentEntry; |
michael@0 | 110 | } |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | // Make the table directory entry point to the new 'name' table. |
michael@0 | 114 | SkSFNTHeader::TableDirectoryEntry* nameTableEntry = reinterpret_cast<SkSFNTHeader::TableDirectoryEntry*>(data + sizeof(SkSFNTHeader)) + tableIndex; |
michael@0 | 115 | nameTableEntry->logicalLength = SkEndian_SwapBE32(nameTableLogicalSize); |
michael@0 | 116 | nameTableEntry->offset = SkEndian_SwapBE32(originalDataSize); |
michael@0 | 117 | |
michael@0 | 118 | // Write the new 'name' table after the original font data. |
michael@0 | 119 | SkOTTableName* nameTable = reinterpret_cast<SkOTTableName*>(data + originalDataSize); |
michael@0 | 120 | unsigned short stringOffset = sizeof(SkOTTableName) + (namesCount * sizeof(SkOTTableName::Record)); |
michael@0 | 121 | nameTable->format = SkOTTableName::format_0; |
michael@0 | 122 | nameTable->count = SkEndian_SwapBE16(namesCount); |
michael@0 | 123 | nameTable->stringOffset = SkEndian_SwapBE16(stringOffset); |
michael@0 | 124 | |
michael@0 | 125 | SkOTTableName::Record* nameRecords = reinterpret_cast<SkOTTableName::Record*>(data + originalDataSize + sizeof(SkOTTableName)); |
michael@0 | 126 | for (int i = 0; i < namesCount; ++i) { |
michael@0 | 127 | nameRecords[i].platformID.value = SkOTTableName::Record::PlatformID::Windows; |
michael@0 | 128 | nameRecords[i].encodingID.windows.value = SkOTTableName::Record::EncodingID::Windows::UnicodeBMPUCS2; |
michael@0 | 129 | nameRecords[i].languageID.windows.value = SkOTTableName::Record::LanguageID::Windows::English_UnitedStates; |
michael@0 | 130 | nameRecords[i].nameID.predefined.value = namesToCreate[i]; |
michael@0 | 131 | nameRecords[i].offset = SkEndian_SwapBE16(0); |
michael@0 | 132 | nameRecords[i].length = SkEndian_SwapBE16(fontNameLen * sizeof(wchar_t)); |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | SK_OT_USHORT* nameString = reinterpret_cast<SK_OT_USHORT*>(data + originalDataSize + stringOffset); |
michael@0 | 136 | for (int i = 0; i < fontNameLen; ++i) { |
michael@0 | 137 | nameString[i] = SkEndian_SwapBE16(fontName[i]); |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | unsigned char* logical = data + originalDataSize + nameTableLogicalSize; |
michael@0 | 141 | unsigned char* physical = data + originalDataSize + nameTablePhysicalSize; |
michael@0 | 142 | for (; logical < physical; ++logical) { |
michael@0 | 143 | *logical = 0; |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | // Update the table checksum in the directory entry. |
michael@0 | 147 | nameTableEntry->checksum = SkEndian_SwapBE32(SkOTUtils::CalcTableChecksum(reinterpret_cast<SK_OT_ULONG*>(nameTable), nameTableLogicalSize)); |
michael@0 | 148 | |
michael@0 | 149 | // Update the checksum adjustment in the head table. |
michael@0 | 150 | if (headTableEntry) { |
michael@0 | 151 | size_t headTableOffset = SkEndian_SwapBE32(headTableEntry->offset); |
michael@0 | 152 | if (headTableOffset + sizeof(SkOTTableHead) < originalDataSize) { |
michael@0 | 153 | SkOTTableHead* headTable = reinterpret_cast<SkOTTableHead*>(data + headTableOffset); |
michael@0 | 154 | headTable->checksumAdjustment = SkEndian_SwapBE32(0); |
michael@0 | 155 | uint32_t unadjustedFontChecksum = SkOTUtils::CalcTableChecksum(reinterpret_cast<SK_OT_ULONG*>(data), originalDataSize + nameTablePhysicalSize); |
michael@0 | 156 | headTable->checksumAdjustment = SkEndian_SwapBE32(SkOTTableHead::fontChecksum - unadjustedFontChecksum); |
michael@0 | 157 | } |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | return rewrittenFontData.detach(); |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | |
michael@0 | 164 | SkOTUtils::LocalizedStrings_NameTable* |
michael@0 | 165 | SkOTUtils::LocalizedStrings_NameTable::CreateForFamilyNames(const SkTypeface& typeface) { |
michael@0 | 166 | static const SkFontTableTag nameTag = SkSetFourByteTag('n','a','m','e'); |
michael@0 | 167 | size_t nameTableSize = typeface.getTableSize(nameTag); |
michael@0 | 168 | if (0 == nameTableSize) { |
michael@0 | 169 | return NULL; |
michael@0 | 170 | } |
michael@0 | 171 | SkAutoTDeleteArray<uint8_t> nameTableData(new uint8_t[nameTableSize]); |
michael@0 | 172 | size_t copied = typeface.getTableData(nameTag, 0, nameTableSize, nameTableData.get()); |
michael@0 | 173 | if (copied != nameTableSize) { |
michael@0 | 174 | return NULL; |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | return new SkOTUtils::LocalizedStrings_NameTable((SkOTTableName*)nameTableData.detach(), |
michael@0 | 178 | SkOTUtils::LocalizedStrings_NameTable::familyNameTypes, |
michael@0 | 179 | SK_ARRAY_COUNT(SkOTUtils::LocalizedStrings_NameTable::familyNameTypes)); |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | bool SkOTUtils::LocalizedStrings_NameTable::next(SkTypeface::LocalizedString* localizedString) { |
michael@0 | 183 | do { |
michael@0 | 184 | SkOTTableName::Iterator::Record record; |
michael@0 | 185 | if (fFamilyNameIter.next(record)) { |
michael@0 | 186 | localizedString->fString = record.name; |
michael@0 | 187 | localizedString->fLanguage = record.language; |
michael@0 | 188 | return true; |
michael@0 | 189 | } |
michael@0 | 190 | if (fTypesCount == fTypesIndex + 1) { |
michael@0 | 191 | return false; |
michael@0 | 192 | } |
michael@0 | 193 | ++fTypesIndex; |
michael@0 | 194 | fFamilyNameIter.reset(fTypes[fTypesIndex]); |
michael@0 | 195 | } while (true); |
michael@0 | 196 | } |
michael@0 | 197 | |
michael@0 | 198 | SkOTTableName::Record::NameID::Predefined::Value |
michael@0 | 199 | SkOTUtils::LocalizedStrings_NameTable::familyNameTypes[3] = { |
michael@0 | 200 | SkOTTableName::Record::NameID::Predefined::FontFamilyName, |
michael@0 | 201 | SkOTTableName::Record::NameID::Predefined::PreferredFamily, |
michael@0 | 202 | SkOTTableName::Record::NameID::Predefined::WWSFamilyName, |
michael@0 | 203 | }; |