Exemplo n.º 1
0
/*
* Decode a BigInt
*/
BigInt BigInt::decode(const uint8_t buf[], size_t length, Base base)
   {
   BigInt r;
   if(base == Binary)
      {
      r.binary_decode(buf, length);
      }
   else if(base == Hexadecimal)
      {
      secure_vector<uint8_t> binary;

      if(length % 2)
         {
         // Handle lack of leading 0
         const char buf0_with_leading_0[2] =
            { '0', static_cast<char>(buf[0]) };

         binary = hex_decode_locked(buf0_with_leading_0, 2);

         binary += hex_decode_locked(cast_uint8_ptr_to_char(&buf[1]),
                                     length - 1,
                                     false);
         }
      else
         binary = hex_decode_locked(cast_uint8_ptr_to_char(buf),
                                    length, false);

      r.binary_decode(binary.data(), binary.size());
      }
   else if(base == Decimal)
      {
      for(size_t i = 0; i != length; ++i)
         {
         if(Charset::is_space(buf[i]))
            continue;

         if(!Charset::is_digit(buf[i]))
            throw Invalid_Argument("BigInt::decode: "
                                   "Invalid character in decimal input");

         const uint8_t x = Charset::char2digit(buf[i]);

         if(x >= 10)
            throw Invalid_Argument("BigInt: Invalid decimal string");

         r *= 10;
         r += x;
         }
      }
   else
      throw Invalid_Argument("Unknown BigInt decoding method");
   return r;
   }
Exemplo n.º 2
0
/*************************************************
* Decode a BigInt                                *
*************************************************/
BigInt BigInt::decode(const byte buf[], u32bit length, Base base)
{
    BigInt r;
    if(base == Binary)
        r.binary_decode(buf, length);
#ifndef BOTAN_MINIMAL_BIGINT
    else if(base == Hexadecimal)
    {
        SecureVector<byte> hex;
        for(u32bit j = 0; j != length; ++j)
            if(Hex_Decoder::is_valid(buf[j]))
                hex.append(buf[j]);

        u32bit offset = (hex.size() % 2);
        SecureVector<byte> binary(hex.size() / 2 + offset);

        if(offset)
        {
            byte temp[2] = { '0', hex[0] };
            binary[0] = Hex_Decoder::decode(temp);
        }

        for(u32bit j = offset; j != binary.size(); ++j)
            binary[j] = Hex_Decoder::decode(hex+2*j-offset);
        r.binary_decode(binary, binary.size());
    }
#endif
    else if(base == Decimal || base == Octal)
    {
        const u32bit RADIX = ((base == Decimal) ? 10 : 8);
        for(u32bit j = 0; j != length; ++j)
        {
            byte x = Charset::char2digit(buf[j]);
            if(x >= RADIX)
            {
                if(RADIX == 10)
                    throw Invalid_Argument("BigInt: Invalid decimal string");
                else
                    throw Invalid_Argument("BigInt: Invalid octal string");
            }

            r *= RADIX;
            r += x;
        }
    }
    else
        throw Invalid_Argument("Unknown BigInt decoding method");
    return r;
}