tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

feat.cc (6287B)


      1 // Copyright (c) 2009-2017 The OTS 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 
      5 #include "feat.h"
      6 
      7 #include "name.h"
      8 
      9 namespace ots {
     10 
     11 bool OpenTypeFEAT::Parse(const uint8_t* data, size_t length) {
     12  Buffer table(data, length);
     13 
     14  if (!table.ReadU32(&this->version)) {
     15    return DropGraphite("Failed to read version");
     16  }
     17  if (this->version >> 16 != 1 && this->version >> 16 != 2) {
     18    return DropGraphite("Unsupported table version: %u", this->version >> 16);
     19  }
     20  if (!table.ReadU16(&this->numFeat)) {
     21    return DropGraphite("Failed to read numFeat");
     22  }
     23  if (!table.ReadU16(&this->reserved)) {
     24    return DropGraphite("Failed to read reserved");
     25  }
     26  if (this->reserved != 0) {
     27    Warning("Nonzero reserved");
     28  }
     29  if (!table.ReadU32(&this->reserved2)) {
     30    return DropGraphite("Failed to read valid reserved2");
     31  }
     32  if (this->reserved2 != 0) {
     33    Warning("Nonzero reserved2");
     34  }
     35 
     36  std::unordered_set<size_t> unverified;
     37  //this->features.resize(this->numFeat, this);
     38  for (unsigned i = 0; i < this->numFeat; ++i) {
     39    this->features.emplace_back(this);
     40    FeatureDefn& feature = this->features[i];
     41    if (!feature.ParsePart(table)) {
     42      return DropGraphite("Failed to read features[%u]", i);
     43    }
     44    this->feature_ids.insert(feature.id);
     45    for (unsigned j = 0; j < feature.numSettings; ++j) {
     46      size_t offset = feature.offset + j * 4;
     47      if (offset < feature.offset || offset > length) {
     48        return DropGraphite("Invalid FeatSettingDefn offset %zu/%zu",
     49                            offset, length);
     50      }
     51      unverified.insert(offset);
     52        // need to verify that this FeatureDefn points to valid
     53        // FeatureSettingDefn
     54    }
     55  }
     56 
     57  while (table.remaining()) {
     58    bool used = unverified.erase(table.offset());
     59    FeatureSettingDefn featSetting(this);
     60    if (!featSetting.ParsePart(table, used)) {
     61      return DropGraphite("Failed to read a FeatureSettingDefn");
     62    }
     63    featSettings.push_back(featSetting);
     64  }
     65 
     66  if (!unverified.empty()) {
     67    return DropGraphite("%zu incorrect offsets into featSettings",
     68                        unverified.size());
     69  }
     70  if (table.remaining()) {
     71    return Warning("%zu bytes unparsed", table.remaining());
     72  }
     73  return true;
     74 }
     75 
     76 bool OpenTypeFEAT::Serialize(OTSStream* out) {
     77  if (!out->WriteU32(this->version) ||
     78      !out->WriteU16(this->numFeat) ||
     79      !out->WriteU16(this->reserved) ||
     80      !out->WriteU32(this->reserved2) ||
     81      !SerializeParts(this->features, out) ||
     82      !SerializeParts(this->featSettings, out)) {
     83    return Error("Failed to write table");
     84  }
     85  return true;
     86 }
     87 
     88 bool OpenTypeFEAT::IsValidFeatureId(uint32_t id) const {
     89  return feature_ids.count(id);
     90 }
     91 
     92 bool OpenTypeFEAT::FeatureDefn::ParsePart(Buffer& table) {
     93  OpenTypeNAME* name = static_cast<OpenTypeNAME*>(
     94      parent->GetFont()->GetTypedTable(OTS_TAG_NAME));
     95  if (!name) {
     96    return parent->Error("FeatureDefn: Required name table is missing");
     97  }
     98 
     99  if (parent->version >> 16 >= 2 && !table.ReadU32(&this->id)) {
    100    return parent->Error("FeatureDefn: Failed to read id");
    101  }
    102  if (parent->version >> 16 == 1)  {
    103    uint16_t id;
    104    if (!table.ReadU16(&id)) {
    105      return parent->Error("FeatureDefn: Failed to read id");
    106    }
    107    this->id = id;
    108  }
    109  if (!table.ReadU16(&this->numSettings)) {
    110    return parent->Error("FeatureDefn: Failed to read numSettings");
    111  }
    112  if (parent->version >> 16 >= 2) {
    113    if (!table.ReadU16(&this->reserved)) {
    114      return parent->Error("FeatureDefn: Failed to read reserved");
    115    }
    116    if (this->reserved != 0) {
    117      parent->Warning("FeatureDefn: Nonzero reserved");
    118    }
    119  }
    120  if (!table.ReadU32(&this->offset)) {
    121    return parent->Error("FeatureDefn: Failed to read offset");
    122  }  // validity of offset verified in OpenTypeFEAT::Parse
    123  if (!table.ReadU16(&this->flags)) {
    124    return parent->Error("FeatureDefn: Failed to read flags");
    125  }
    126  if ((this->flags & RESERVED) != 0) {
    127    this->flags &= ~RESERVED;
    128    parent->Warning("FeatureDefn: Nonzero (flags & 0x%x) repaired", RESERVED);
    129  }
    130  if (this->flags & HAS_DEFAULT_SETTING &&
    131      (this->flags & DEFAULT_SETTING) >= this->numSettings) {
    132    return parent->Error("FeatureDefn: (flags & 0x%x) is set but (flags & 0x%x "
    133                         "is not a valid setting index", HAS_DEFAULT_SETTING,
    134                         DEFAULT_SETTING);
    135  }
    136  if (!table.ReadU16(&this->label)) {
    137    return parent->Error("FeatureDefn: Failed to read label");
    138  }
    139  if (!name->IsValidNameId(this->label)) {
    140    if (this->id == 1 && name->IsValidNameId(this->label, true)) {
    141      parent->Warning("FeatureDefn: Missing NameRecord repaired for feature"
    142                      " with id=%u, label=%u", this->id, this->label);
    143    }
    144    else {
    145      return parent->Error("FeatureDefn: Invalid label");
    146    }
    147  }
    148  return true;
    149 }
    150 
    151 bool OpenTypeFEAT::FeatureDefn::SerializePart(OTSStream* out) const {
    152  if ((parent->version >> 16 >= 2 && !out->WriteU32(this->id)) ||
    153      (parent->version >> 16 == 1 &&
    154       !out->WriteU16(static_cast<uint16_t>(this->id))) ||
    155      !out->WriteU16(this->numSettings) ||
    156      (parent->version >> 16 >= 2 && !out->WriteU16(this->reserved)) ||
    157      !out->WriteU32(this->offset) ||
    158      !out->WriteU16(this->flags) ||
    159      !out->WriteU16(this->label)) {
    160    return parent->Error("FeatureDefn: Failed to write");
    161  }
    162  return true;
    163 }
    164 
    165 bool OpenTypeFEAT::FeatureSettingDefn::ParsePart(Buffer& table, bool used) {
    166  OpenTypeNAME* name = static_cast<OpenTypeNAME*>(
    167      parent->GetFont()->GetTypedTable(OTS_TAG_NAME));
    168  if (!name) {
    169    return parent->Error("FeatureSettingDefn: Required name table is missing");
    170  }
    171 
    172  if (!table.ReadS16(&this->value)) {
    173    return parent->Error("FeatureSettingDefn: Failed to read value");
    174  }
    175  if (!table.ReadU16(&this->label) ||
    176      (used && !name->IsValidNameId(this->label))) {
    177    return parent->Error("FeatureSettingDefn: Failed to read valid label");
    178  }
    179  return true;
    180 }
    181 
    182 bool OpenTypeFEAT::FeatureSettingDefn::SerializePart(OTSStream* out) const {
    183  if (!out->WriteS16(this->value) ||
    184      !out->WriteU16(this->label)) {
    185    return parent->Error("FeatureSettingDefn: Failed to write");
    186  }
    187  return true;
    188 }
    189 
    190 }  // namespace ots