Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

BigNumber.cc

00001 /*
00002  * Copyright 2003 Michael A. Marsh, Cornell University. All rights reserved.
00003  * This software is released under the modified BSD license.
00004  * See the file LICENSE in the top-level directory for details.
00005  */
00006 //
00007 // $Id: BigNumber.cc,v 1.4 2004/05/19 15:56:37 mmarsh Exp $
00008 //
00009 // $Log: BigNumber.cc,v $
00010 // Revision 1.4  2004/05/19 15:56:37  mmarsh
00011 // Added copyright and license statements.
00012 //
00013 // Revision 1.3  2003/11/06 16:57:33  mmarsh
00014 // m_value was not being tested before trying to convert it to m_asn1.
00015 //
00016 // Revision 1.2  2003/11/04 22:31:46  mmarsh
00017 // *** empty log message ***
00018 //
00019 //
00020 
00021 #include "BigNumber.h"
00022 
00023 using namespace CODEX_ASN1;
00024 
00025 BigNumber::BigNumber() :
00026    Base( false ),
00027    m_value( 0 ),
00028    m_asn1( 0 )
00029 {
00030 }
00031 
00032 BigNumber::BigNumber( BIGNUM * pBN ) :
00033    Base( true ),
00034    m_value( pBN ),
00035    m_asn1( 0 )
00036 {
00037 //   if ( 0 != m_value )
00038 //   {
00039 //      m_asn1 = BN_to_ASN1_INTEGER( m_value, m_asn1 );
00040 //   }
00041 }
00042 
00043 BigNumber::BigNumber( const ASN1_INTEGER* asn1 ) :
00044    Base( true ),
00045    m_value( 0 ),
00046    m_asn1( 0 )
00047 {
00048    m_asn1 = M_ASN1_INTEGER_dup( asn1 );
00049    if ( 0 != m_asn1 )
00050    {
00051       m_value = ASN1_INTEGER_to_BN( m_asn1, m_value );
00052    }
00053 }
00054 
00055 BigNumber::BigNumber( const BigNumber& aBN ) :
00056    Base( aBN.m_initialized ),
00057    m_value( 0 ),
00058    m_asn1( 0 )
00059 {
00060    m_value = BN_dup( aBN.m_value );
00061 //   m_asn1 = M_ASN1_INTEGER_dup( aBN.m_asn1 );
00062 }
00063 
00064 BigNumber::~BigNumber()
00065 {
00066    if ( m_initialized )
00067    {
00068       if ( 0 != m_value ) BN_free( m_value );
00069       if ( 0 != m_asn1  ) ASN1_INTEGER_free( m_asn1 );
00070    }
00071 }
00072 
00073 void
00074 BigNumber::operator=( const BigNumber& aBN )
00075 {
00076    m_initialized = aBN.m_initialized;
00077    if ( 0 != m_value )
00078    {
00079       BN_clear_free( m_value );
00080       m_value = 0;
00081    }
00082    if ( 0 != aBN.m_value )
00083    {
00084       m_value = BN_dup( aBN.m_value );
00085    }
00086    if ( 0 != m_asn1 )
00087    {
00088       ASN1_INTEGER_free( m_asn1 );
00089       m_asn1 = 0;
00090    }
00091 //   if ( 0 != aBN.m_asn1 )
00092 //   {
00093 //      m_asn1 = M_ASN1_INTEGER_dup( aBN.m_asn1 );
00094 //   }
00095 }
00096 
00097 void
00098 BigNumber::operator=( BIGNUM * pBN )
00099 {
00100    if ( 0 != m_value ) BN_clear_free( m_value );
00101    if ( 0 != m_asn1  ) ASN1_INTEGER_free( m_asn1 );
00102    m_value = pBN;
00103    m_asn1 = 0;
00104    m_initialized = true;
00105 }
00106 
00107 bool
00108 BigNumber::operator==( const BigNumber& aBN ) const
00109 {
00110    return ( 0 == BN_cmp( m_value , aBN.m_value ) );
00111 }
00112 
00113 bool
00114 BigNumber::operator!=( const BigNumber& aBN ) const
00115 {
00116    return ( 0 != BN_cmp( m_value , aBN.m_value ) );
00117 }
00118 
00119 bool
00120 BigNumber::operator<( const BigNumber& aBN ) const
00121 {
00122    return ( 0 > BN_cmp( m_value , aBN.m_value ) );
00123 }
00124 
00125 bool
00126 BigNumber::operator>( const BigNumber& aBN ) const
00127 {
00128    return ( 0 < BN_cmp( m_value , aBN.m_value ) );
00129 }
00130 
00131 const ASN1_INTEGER *
00132 BigNumber::asn1() const
00133 {
00134    if ( 0 == m_asn1 )
00135    {
00136       if ( 0 != m_value )
00137       {
00138          m_asn1 = BN_to_ASN1_INTEGER( m_value, m_asn1 );
00139       }
00140    }
00141    return m_asn1;
00142 }
00143 
00144 int
00145 BigNumber::marshal( unsigned char ** pp ) const
00146 {
00147    asn1();
00148    return i2d_ASN1_INTEGER( m_asn1, pp );
00149 }
00150 
00151 void*
00152 BigNumber::unmarshal( void* bogus, unsigned char** pp, long length )
00153 {
00154    if ( m_initialized )
00155    {
00156       return 0;
00157    }
00158    if ( 0 == d2i_ASN1_INTEGER( &m_asn1, (unsigned char**)pp, length ) )
00159    {
00160       return 0;
00161    }
00162    m_value = ASN1_INTEGER_to_BN( m_asn1, m_value );
00163    m_initialized = true;
00164    return this;
00165 }

Generated on Fri May 6 17:38:34 2005 for COrnell Data EXchange (CODEX) by  doxygen 1.4.1