Пример #1
0
PRInt32
nsHTMLEntities::EntityToUnicode(const nsCString& aEntity)
{
    NS_ASSERTION(gEntityToUnicode.ops, "no lookup table, needs addref");
    if (!gEntityToUnicode.ops)
        return -1;

    //this little piece of code exists because entities may or may not have the terminating ';'.
    //if we see it, strip if off for this test...

    if(';'==aEntity.Last()) {
        nsCAutoString temp(aEntity);
        temp.Truncate(aEntity.Length()-1);
        return EntityToUnicode(temp);
    }

    EntityNodeEntry* entry =
        static_cast<EntityNodeEntry*>
        (PL_DHashTableOperate(&gEntityToUnicode, aEntity.get(), PL_DHASH_LOOKUP));

    if (!entry || PL_DHASH_ENTRY_IS_FREE(entry))
        return -1;

    return entry->node->mUnicode;
}
Пример #2
0
int32_t
nsHTMLEntities::EntityToUnicode(const nsCString& aEntity)
{
  NS_ASSERTION(gEntityToUnicode, "no lookup table, needs addref");
  if (!gEntityToUnicode) {
    return -1;
  }

  //this little piece of code exists because entities may or may not have the terminating ';'.
  //if we see it, strip if off for this test...

  if(';'==aEntity.Last()) {
    nsAutoCString temp(aEntity);
    temp.Truncate(aEntity.Length()-1);
    return EntityToUnicode(temp);
  }

  auto entry =
    static_cast<EntityNodeEntry*>(gEntityToUnicode->Search(aEntity.get()));

  return entry ? entry->node->mUnicode : -1;
}
Пример #3
0
nsresult
nsMorkReader::ReadLine(nsCString &aLine)
{
    PRBool res;
    nsresult rv = mStream->ReadLine(aLine, &res);
    NS_ENSURE_SUCCESS(rv, rv);
    if (!res) {
        return NS_ERROR_NOT_AVAILABLE;
    }

    while (!aLine.IsEmpty() &&  aLine.Last() == '\\') {
        // There is a continuation for this line.  Read it and append.
        nsCLineString line2;
        rv = mStream->ReadLine(line2, &res);
        NS_ENSURE_SUCCESS(rv, rv);
        if (!res) {
            return NS_ERROR_NOT_AVAILABLE;
        }
        aLine.Truncate(aLine.Length() - 1);
        aLine.Append(line2);
    }

    return NS_OK;
}
// Finds the base domain for a host, with requested number of additional parts.
// This will fail, generating an error, if the host is an IPv4/IPv6 address,
// if more subdomain parts are requested than are available, or if the hostname
// includes characters that are not valid in a URL. Normalization is performed
// on the host string and the result will be in UTF8.
nsresult
nsEffectiveTLDService::GetBaseDomainInternal(nsCString  &aHostname,
                                             int32_t    aAdditionalParts,
                                             nsACString &aBaseDomain)
{
  if (aHostname.IsEmpty())
    return NS_ERROR_INSUFFICIENT_DOMAIN_LEVELS;

  // chomp any trailing dot, and keep track of it for later
  bool trailingDot = aHostname.Last() == '.';
  if (trailingDot)
    aHostname.Truncate(aHostname.Length() - 1);

  // check the edge cases of the host being '.' or having a second trailing '.',
  // since subsequent checks won't catch it.
  if (aHostname.IsEmpty() || aHostname.Last() == '.')
    return NS_ERROR_INVALID_ARG;

  // Check if we're dealing with an IPv4/IPv6 hostname, and return
  PRNetAddr addr;
  PRStatus result = PR_StringToNetAddr(aHostname.get(), &addr);
  if (result == PR_SUCCESS)
    return NS_ERROR_HOST_IS_IP_ADDRESS;

  // Walk up the domain tree, most specific to least specific,
  // looking for matches at each level.  Note that a given level may
  // have multiple attributes (e.g. IsWild() and IsNormal()).
  const char *prevDomain = nullptr;
  const char *currDomain = aHostname.get();
  const char *nextDot = strchr(currDomain, '.');
  const char *end = currDomain + aHostname.Length();
  // Default value of *eTLD is currDomain as set in the while loop below
  const char *eTLD = nullptr;
  while (1) {
    // sanity check the string we're about to look up: it should not begin with
    // a '.'; this would mean the hostname began with a '.' or had an
    // embedded '..' sequence.
    if (*currDomain == '.')
      return NS_ERROR_INVALID_ARG;

    // Perform the lookup.
    const ETLDEntry* entry = ETLDEntry::GetEntry(currDomain);
    if (entry) {
      if (entry->IsWild() && prevDomain) {
        // wildcard rules imply an eTLD one level inferior to the match.
        eTLD = prevDomain;
        break;

      } else if (entry->IsNormal() || !nextDot) {
        // specific match, or we've hit the top domain level
        eTLD = currDomain;
        break;

      } else if (entry->IsException()) {
        // exception rules imply an eTLD one level superior to the match.
        eTLD = nextDot + 1;
        break;
      }
    }

    if (!nextDot) {
      // we've hit the top domain level; use it by default.
      eTLD = currDomain;
      break;
    }

    prevDomain = currDomain;
    currDomain = nextDot + 1;
    nextDot = strchr(currDomain, '.');
  }

  const char *begin, *iter;
  if (aAdditionalParts < 0) {
    NS_ASSERTION(aAdditionalParts == -1,
                 "aAdditionalParts can't be negative and different from -1");

    for (iter = aHostname.get(); iter != eTLD && *iter != '.'; iter++);

    if (iter != eTLD) {
      iter++;
    }
    if (iter != eTLD) {
      aAdditionalParts = 0;
    }
  } else {
    // count off the number of requested domains.
    begin = aHostname.get();
    iter = eTLD;

    while (1) {
      if (iter == begin)
        break;

      if (*(--iter) == '.' && aAdditionalParts-- == 0) {
        ++iter;
        ++aAdditionalParts;
        break;
      }
    }
  }

  if (aAdditionalParts != 0)
    return NS_ERROR_INSUFFICIENT_DOMAIN_LEVELS;

  aBaseDomain = Substring(iter, end);
  // add on the trailing dot, if applicable
  if (trailingDot)
    aBaseDomain.Append('.');

  return NS_OK;
}