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

#include <botan/asn1.h>

namespace Botan {

namespace {

/*************************************************
* BER decode an ASN.1 type tag                   *
*************************************************/
ASN1_Tag decode_tag(DataSource* ber)
   {
   byte b;
   if(!ber->read_byte(b))
      return NO_OBJECT;

   if((b & 0x1F) != 0x1F)
      return ASN1_Tag(b);

   throw Internal_Error("BER decoding of high-tag form unimplemented");
   }

/*************************************************
* BER decode an ASN.1 length field               *
*************************************************/
u32bit decode_length(DataSource* ber)
   {
   byte b;
   if(!ber->read_byte(b))
      throw BER_Decoding_Error("Length field not found");
   if((b & 0x80) == 0)
      return b;

   u32bit field_size = b & 0x7F;
   if(field_size == 0)
      throw Internal_Error("Indefinite length BER constructs not supported");
   if(field_size >= 5)
      throw Internal_Error("BER field size is too large");

   u32bit length = 0;
   while(field_size--)
      {
      if(get_byte(0, length) != 0)
         throw BER_Decoding_Error("Field length overflow");
      if(!ber->read_byte(b))
         throw BER_Decoding_Error("Corrupted length field");
      length = (length << 8) | b;
      }
   return length;
   }

}

/*************************************************
* Check if more objects are there                *
*************************************************/
bool BER_Decoder::more_items() const
   {
   if(source->end_of_data() && (pushed.tag == NO_OBJECT))
      return false;
   return true;
   }

/*************************************************
* Verify that no bytes remain in the source      *
*************************************************/
void BER_Decoder::verify_end() const
   {
   if(!source->end_of_data() || (pushed.tag != NO_OBJECT))
      throw Invalid_State("BER_Decoder::verify_end called, but data remains");
   }

/*************************************************
* Return all the bytes remaining in the source   *
*************************************************/
SecureVector<byte> BER_Decoder::get_remaining()
   {
   SecureVector<byte> out;
   byte buf;
   while(source->read_byte(buf))
      out.append(buf);
   return out;
   }

/*************************************************
* Discard all the bytes remaining in the source  *
*************************************************/
void BER_Decoder::discard_remaining()
   {
   byte buf;
   while(source->read_byte(buf))
      ;
   }

/*************************************************
* Return the BER encoding of the next object     *
*************************************************/
BER_Object BER_Decoder::get_next_object()
   {
   BER_Object next;

   if(pushed.tag != NO_OBJECT)
      {
      next = pushed;
      pushed.tag = NO_OBJECT;
      return next;
      }

   next.tag = decode_tag(source);
   if(next.tag == NO_OBJECT)
      return next;

   u32bit length = decode_length(source);
   next.value.resize(length);
   if(source->read(next.value, length) != length)
      throw BER_Decoding_Error("Value truncated");
   return next;
   }

/*************************************************
* Push a object back into the stream             *
*************************************************/
void BER_Decoder::push_back(const BER_Object& obj)
   {
   if(pushed.tag != NO_OBJECT)
      throw Invalid_State("BER_Decoder: Only one push back is allowed");
   pushed = obj;
   }

/*************************************************
* BER_Decoder Constructor                        *
*************************************************/
BER_Decoder::BER_Decoder(DataSource& src)
   {
   source = &src;
   owns = false;
   pushed.tag = NO_OBJECT;
   }

/*************************************************
* BER_Decoder Constructor                        *
 *************************************************/
BER_Decoder::BER_Decoder(const byte data[], u32bit length)
   {
   source = new DataSource_Memory(data, length);
   owns = true;
   pushed.tag = NO_OBJECT;
   }

/*************************************************
* BER_Decoder Constructor                        *
*************************************************/
BER_Decoder::BER_Decoder(const SecureVector<byte>& data)
   {
   source = new DataSource_Memory(data);
   owns = true;
   pushed.tag = NO_OBJECT;
   }

/*************************************************
* BER_Decoder Copy Constructor                   *
*************************************************/
BER_Decoder::BER_Decoder(const BER_Decoder& other)
   {
   source = other.source;
   owns = false;
   if(other.owns)
      {
      other.owns = false;
      owns = true;
      }
   pushed.tag = NO_OBJECT;
   }

/*************************************************
* BER_Decoder Destructor                         *
*************************************************/
BER_Decoder::~BER_Decoder()
   {
   if(owns)
      delete source;
   source = 0;
   }

}
