void
nsUrlClassifierUtils::CanonicalNum(const nsACString& num,
                                   uint32_t bytes,
                                   bool allowOctal,
                                   nsACString& _retval)
{
  _retval.Truncate();

  if (num.Length() < 1) {
    return;
  }

  uint32_t val;
  if (allowOctal && IsOctal(num)) {
    if (PR_sscanf(PromiseFlatCString(num).get(), "%o", &val) != 1) {
      return;
    }
  } else if (IsDecimal(num)) {
    if (PR_sscanf(PromiseFlatCString(num).get(), "%u", &val) != 1) {
      return;
    }
  } else if (IsHex(num)) {
  if (PR_sscanf(PromiseFlatCString(num).get(), num[1] == 'X' ? "0X%x" : "0x%x",
                &val) != 1) {
      return;
    }
  } else {
    return;
  }

  while (bytes--) {
    char buf[20];
    PR_snprintf(buf, sizeof(buf), "%u", val & 0xff);
    if (_retval.IsEmpty()) {
      _retval.Assign(buf);
    } else {
      _retval = nsDependentCString(buf) + NS_LITERAL_CSTRING(".") + _retval;
    }
    val >>= 8;
  }
}
Example #2
0
/* Parse character (possibly an escape sequence). Return the unicode value of
   the character. *code will be assigned the opcode corresponding to the
   character or EMPTY if the character has no special meaning. */
static int ParseChar(ParseInfo *info, AReOpcode *code)
{
    unsigned char c;

    *code = A_EMPTY;

    /* Assumption: info->bufInd < info->bufLen. */

    /* Escape sequence? */
    if (*info->str != '\\')
        /* No, plain character. */
        return *info->str++;

    /* Skip '\\'. */
    info->str++;

    /* Check end of line. */
    if (info->str == info->strEnd) {
        AGenerateError(info, ErrTrailingBackslash);
        return 0;
    }

    /* Translate an escape sequence. */

    c = *info->str++;

    if (c == 'x') {
        /* Hexadecimal ascii value? */

        int digit1 = GetHexDigit(info);
        int digit2;

        if (digit1 < 0)
            return 'x';

        digit2 = GetHexDigit(info);

        if (digit2 < 0)
            return digit1;
        else
            return digit1 * 16 + digit2;
    }

    if (c >= 'a' && c <= 'z') {
        int conv = CharConv[c - 'a'];
        if (conv >= CC)
            *code = conv;
        else if (conv == 0)
            conv = c;
        return conv;
    }

    if (c == '<') {
        *code = A_BOW;
        return '<';
    }

    if (c == '>') {
        *code = A_EOW;
        return '>';
    }

    if (c == 'D') {
        *code = CC + CC_COMP;
        return 0;
    }

    if (c == 'S') {
        *code = CC + 1 + CC_COMP;
        return 0;
    }

    if (c == 'W') {
        *code = CC + 2 + CC_COMP;
        return 0;
    }

    if (ReIsDigit(c)) {
        /* Octal ascii value / back reference */

        if (c != '0' && (c > '7' || info->str == info->strEnd
                || !IsOctal(*info->str))) {
            /* Back reference */

            *code = info->flags & A_RE_NOCASE ? A_BACKREF_I : A_BACKREF;

            return c > '7' ? c : c - '0';
        } else {
            /* Octal value */

            int number = c - '0';

            if (info->str < info->strEnd && IsOctal(*info->str)) {
                number = number * 8 + (*info->str++ - '0');
                if (info->str < info->strEnd && IsOctal(*info->str))
                    number = number * 8 + (*info->str++ - '0');
            }

            return number;
        }
    }

    /* No special interpretion */
    return c;
}