/*************************************************
* BER Decoding Source File                       *
* (C) 1999-2003 The Botan Project                *
*************************************************/

#include <botan/asn1.h>

namespace Botan {

namespace {

/*************************************************
* Check an object's type and size                *
*************************************************/
void check_object(const BER_Object& object,
                  ASN1_Tag type_tag, ASN1_Tag class_tag,
                  u32bit length = 0, bool check_length = false)
   {
   if(object.tag != ASN1_Tag(type_tag | class_tag))
      throw BER_Decoding_Error("Tag mismatch when decoding");
   if(check_length && object.value.size() != length)
      throw BER_Decoding_Error("Incorrect size for type");
   }

}

namespace BER {

/*************************************************
* Decode a BER encoded NULL                      *
*************************************************/
void decode_null(BER_Decoder& decoder)
   {
   BER_Object object = decoder.get_next_object();
   check_object(object, NULL_TAG, UNIVERSAL, 0, true);
   }

/*************************************************
* Decode a BER encoded BOOLEAN                   *
*************************************************/
void decode(BER_Decoder& decoder, bool& out)
   {
   decode(decoder, out, BOOLEAN, UNIVERSAL);
   }

/*************************************************
* Decode a small BER encoded INTEGER             *
*************************************************/
void decode(BER_Decoder& decoder, u32bit& out)
   {
   decode(decoder, out, INTEGER, UNIVERSAL);
   }

/*************************************************
* Decode a BER encoded INTEGER                   *
*************************************************/
void decode(BER_Decoder& decoder, BigInt& out)
   {
   decode(decoder, out, INTEGER, UNIVERSAL);
   }

/*************************************************
* BER decode a BIT STRING or OCTET STRING        *
*************************************************/
void decode(BER_Decoder& decoder, SecureVector<byte>& out, ASN1_Tag real_type)
   {
   decode(decoder, out, real_type, real_type, UNIVERSAL);
   }

/*************************************************
* Decode a BER encoded BOOLEAN                   *
*************************************************/
void decode(BER_Decoder& decoder, bool& out,
            ASN1_Tag type_tag, ASN1_Tag class_tag)
   {
   BER_Object object = decoder.get_next_object();
   check_object(object, type_tag, class_tag, 1, true);
   out = (object.value[0]) ? true : false;
   }

/*************************************************
* Decode a small BER encoded INTEGER             *
*************************************************/
void decode(BER_Decoder& decoder, u32bit& out,
            ASN1_Tag type_tag, ASN1_Tag class_tag)
   {
   BigInt integer;
   decode(decoder, integer, type_tag, class_tag);
   out = integer.to_u32bit();
   }

/*************************************************
* Decode a BER encoded INTEGER                   *
*************************************************/
void decode(BER_Decoder& decoder, BigInt& out,
            ASN1_Tag type_tag, ASN1_Tag class_tag)
   {
   BER_Object object = decoder.get_next_object();
   check_object(object, type_tag, class_tag);

   out = 0;
   if(object.value.is_empty())
      return;

   const bool negative = (object.value[0] & 0x80) ? true : false;

   if(negative)
      {
      for(u32bit j = object.value.size(); j > 0; j--)
         if(object.value[j-1]--)
            break;
      for(u32bit j = 0; j != object.value.size(); j++)
         object.value[j] = ~object.value[j];
      }

   out = BigInt(object.value, object.value.size());

   if(negative)
      out.flip_sign();
   }

/*************************************************
* BER decode a BIT STRING or OCTET STRING        *
*************************************************/
void decode(BER_Decoder& decoder, SecureVector<byte>& buffer,
            ASN1_Tag real_type, ASN1_Tag type_tag, ASN1_Tag class_tag)
   {
   if(real_type != OCTET_STRING && real_type != BIT_STRING)
      throw BER_Bad_Tag("Bad tag for {bit,octet} string", real_type);

   BER_Object object = decoder.get_next_object();
   check_object(object, type_tag, class_tag);

   if(real_type == OCTET_STRING)
      buffer = object.value;
   else
      {
      if(object.value[0])
         throw BER_Decoding_Error("BIT STRING is not a multiple of 8 bits");
      buffer.set(object.value + 1, object.value.size() - 1);
      }
   }

/*************************************************
* Decode and return a BER encoded SEQUENCE       *
*************************************************/
BER_Decoder get_subsequence(BER_Decoder& decoder)
   {
   return get_subsequence(decoder, SEQUENCE, CONSTRUCTED);
   }

/*************************************************
* Decode and return a BER encoded SET            *
*************************************************/
BER_Decoder get_subset(BER_Decoder& decoder)
   {
   return get_subset(decoder, SET, CONSTRUCTED);
   }

/*************************************************
* Decode and return a BER encoded SEQUENCE       *
*************************************************/
BER_Decoder get_subsequence(BER_Decoder& decoder,
                            ASN1_Tag type_tag, ASN1_Tag class_tag)
   {
   BER_Object object = decoder.get_next_object();
   check_object(object, type_tag, class_tag);
   return BER_Decoder(object.value, object.value.size());
   }

/*************************************************
* Decode and return a BER encoded SET            *
*************************************************/
BER_Decoder get_subset(BER_Decoder& decoder,
                       ASN1_Tag type_tag, ASN1_Tag class_tag)
   {
   BER_Object object = decoder.get_next_object();
   check_object(object, type_tag, class_tag);
   return BER_Decoder(object.value, object.value.size());
   }

/*************************************************
* Convert a BER object into a string object      *
*************************************************/
std::string to_string(const BER_Object& obj)
   {
   const char* str = (const char*)obj.value.ptr();
   std::string str_rep(str, obj.value.size());
   return str_rep;
   }

/*************************************************
* Decode an OPTIONAL string type                 *
*************************************************/
bool decode_optional_string(BER_Decoder& in, SecureVector<byte>& out,
                            ASN1_Tag real_type,
                            ASN1_Tag type_tag, ASN1_Tag class_tag)
   {
   BER_Object obj = in.get_next_object();

   if(obj.tag == class_tag)
      {
      if(class_tag & CONSTRUCTED)
         {
         BER_Decoder stored_value(obj.value);
         BER::decode(stored_value, out, real_type);
         stored_value.verify_end();
         }
      else
         {
         in.push_back(obj);
         BER::decode(in, out, real_type, type_tag, class_tag);
         }
      return true;
      }
   else
      {
      out.clear();
      in.push_back(obj);
      return false;
      }
   }

/*************************************************
* Do heuristic tests for BER data                *
*************************************************/
bool maybe_BER(DataSource& source)
   {
   byte first_byte;
   if(!source.peek_byte(first_byte))
      throw Stream_IO_Error("BER::maybe_BER: Source was empty");

   if(first_byte == (SEQUENCE | CONSTRUCTED))
      return true;
   return false;
   }

}

}
