Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   Related Pages  

SubshareLabel.h

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: SubshareLabel.h,v 1.3 2004/05/19 15:57:01 mmarsh Exp $
00008 //
00009 // $Log: SubshareLabel.h,v $
00010 // Revision 1.3  2004/05/19 15:57:01  mmarsh
00011 // *** empty log message ***
00012 //
00013 // Revision 1.2  2003/11/04 22:23:08  mmarsh
00014 // General code cleanup.
00015 //
00016 //
00017 
00018 #ifndef __CODEX_VSS_SUBSHARELABEL_H__
00019 #define __CODEX_VSS_SUBSHARELABEL_H__
00020 
00021 #include "ShareLabel.h"
00022 
00023 namespace CODEX_VSS
00024 {
00037    template< class _ShareLabel >
00038    class SubshareLabel : public CODEX_ASN1::Base
00039    {
00040       public :
00042          typedef _ShareLabel  ShareLabel;
00043 
00049          typedef typename ShareLabel::VType  VType;
00050 
00052          typedef typename ShareLabel::ShareType  ShareType;
00053 
00055          typedef typename ShareLabel::OneWay  OneWay;
00056 
00058          const static unsigned int  NumShares = ShareLabel::NumShares;
00059 
00061          SubshareLabel() :
00062             CODEX_ASN1::Base( false )
00063          {
00064          }
00065 
00067          SubshareLabel( const ShareLabel& label,
00068                         int id,
00069                         const ShareSet< ShareType >& shareSet,
00070                         const OneWay& func ) :
00071             CODEX_ASN1::Base( true ),
00072             m_label( label ),
00073             m_id( id )
00074          {
00075             for ( unsigned int i = 0 ; i < NumShares ; ++i )
00076             {
00077                func( shareSet(i), m_vc[i] );
00078             }
00079          }
00080 
00081 //         /// Constructor taking a share label and ID
00082 //         SubshareLabel( const ShareLabel& label, int id ) :
00083 //            CODEX_ASN1::Base( true ),
00084 //            m_label( label ),
00085 //            m_id( id )
00086 //         {
00087 //         }
00088 
00090          SubshareLabel( const ShareLabel& label,
00091                         int id,
00092                         const typename VType::ValueType vc[NumShares] ) :
00093             CODEX_ASN1::Base( true ),
00094             m_label( label ),
00095             m_id( id )
00096          {
00097             for ( unsigned int i = 0 ; i < NumShares ; ++i )
00098             {
00099                m_vc[i] = vc[i];
00100             }
00101          }
00102 
00104          SubshareLabel( const SubshareLabel< _ShareLabel >& aOther ) :
00105             CODEX_ASN1::Base( aOther.m_initialized ),
00106             m_label( aOther.m_label ),
00107             m_id( aOther.m_id )
00108          {
00109             for ( unsigned int i = 0 ; i < NumShares ; ++i )
00110             {
00111                m_vc[i] = aOther.m_vc[i];
00112             }
00113          }
00114 
00116          virtual ~SubshareLabel()
00117          {
00118          }
00119 
00121          void operator=( const SubshareLabel< _ShareLabel >& aOther )
00122          {
00123             m_initialized = aOther.m_initialized;
00124             m_label = aOther.m_label;
00125             m_id = aOther.m_id;
00126             for ( unsigned int i = 0 ; i < NumShares ; ++i )
00127             {
00128                m_vc[i] = aOther.m_vc[i];
00129             }
00130          }
00131 
00146          int cmp( const SubshareLabel< _ShareLabel >& aOther ) const
00147          {
00148             int lc = m_label.cmp( aOther.m_label );
00149             if ( lc < 0 ) return -1;
00150             if ( lc > 0 ) return 1;
00151 
00152             int i1 = m_id.value();
00153             int i2 = aOther.m_id.value();
00154             if ( i1 < i2 ) return -1;
00155             if ( i1 > i2 ) return 1;
00156 
00157             for ( unsigned int i = 0 ; i < NumShares ; ++i )
00158             {
00159                if ( m_vc[i] < aOther.m_vc[i] ) return -1;
00160                if ( m_vc[i] > aOther.m_vc[i] ) return 1;
00161             }
00162 
00163             // At this point they're equal.
00164             return 0;
00165          }
00166          
00168          bool operator<( const SubshareLabel< _ShareLabel >& aOther )
00169             const
00170          {
00171             return ( cmp(aOther) < 0 );
00172          }
00173 
00175          bool operator>( const SubshareLabel< _ShareLabel >& aOther )
00176             const
00177          {
00178             return ( cmp(aOther) > 0 );
00179          }
00180 
00182          bool operator==( const SubshareLabel< _ShareLabel >& aOther )
00183             const
00184          {
00185             return ( cmp(aOther) == 0 );
00186          }
00187 
00189          bool operator!=( const SubshareLabel< _ShareLabel >& aOther )
00190             const
00191          {
00192             return ( cmp(aOther) != 0 );
00193          }
00194 
00196          bool operator<=( const SubshareLabel< _ShareLabel >& aOther )
00197             const
00198          {
00199             return ( cmp(aOther) <= 0 );
00200          }
00201 
00203          bool operator>=( const SubshareLabel< _ShareLabel >& aOther )
00204             const
00205          {
00206             return ( cmp(aOther) >= 0 );
00207          }
00208 
00210          const ShareLabel& label() const { return m_label; }
00211 
00213          const CODEX_ASN1::Integer& id() const { return m_id; }
00214 
00216          const typename VType::ValueType* vc() const { return m_vc; }
00217 
00219          const typename VType::ValueType& vc( unsigned int i ) const
00220          {
00221             if ( i >= NumShares )
00222             {
00223                throw CODEX_Exceptions::IllegalIndexException( __FILE__ ,
00224                                                               __LINE__ );
00225             }
00226             return m_vc[i];
00227          }
00228 
00236          bool verify( const OneWay& func ) const
00237          {
00238             VType vr( func );
00239             typename VType::ValueType res;
00240             vr( m_vc, res );
00241             // We have the relevant check in the label.
00242             if ( res != m_label.vc( m_id.value() ) ) return false;
00243             return true;
00244          }
00245 
00252          bool check( const ShareType& share, const OneWay& func ) const
00253          {
00254             VType vr( func );
00255             return vr.checkShare( m_vc, share, func );
00256          }
00257 
00258          virtual int marshal( unsigned char ** pp ) const
00259          {
00260             // For starters, we will not agree to marshal an incomplete label.
00261             if ( ! m_label.initialized() ) return 0;
00262             if ( ! m_id.initialized() ) return 0;
00263             for ( unsigned int i = 0 ; i < NumShares ; ++i )
00264             {
00265                if ( ! m_vc[i].initialized() ) return 0;
00266             }
00267             int r=0;
00268             int ret=0;
00269             unsigned char * p;
00270 
00271             ret += m_label.marshal(0);
00272             ret += m_id.marshal(0);
00273             for ( unsigned int i = 0 ; i < NumShares ; ++i )
00274             {
00275                ret += m_vc[i].marshal(0);
00276             }
00277             M_ASN1_I2D_seq_total();
00278             m_label.marshal(&p);
00279             m_id.marshal(&p);
00280             for ( unsigned int i = 0 ; i < NumShares ; ++i )
00281             {
00282                m_vc[i].marshal(&p);
00283             }
00284             M_ASN1_I2D_finish();
00285          }
00286 
00287          virtual void* unmarshal( void* bogus,
00288                                   unsigned char** pp,
00289                                   long length )
00290          {
00291             if ( m_initialized )
00292             {
00293                return 0;
00294             }
00295             if ( (0 == pp) || (0 == *pp) )
00296             {
00297                return 0;
00298             }
00299 
00300             ASN1_CTX c;
00301             c.pp = pp;
00302             c.q = *pp;
00303             c.error = ERR_R_NESTED_ASN1_ERROR;
00304             int i;
00305 
00306             M_ASN1_D2I_Init();
00307             M_ASN1_D2I_start_sequence();
00308             M_ASN1_D2I_get(i, m_label.unmarshal);
00309             M_ASN1_D2I_get(i, m_id.unmarshal);
00310             for ( unsigned int count = 0 ; count < NumShares ; ++count )
00311             {
00312                M_ASN1_D2I_get(i, m_vc[count].unmarshal);
00313             }
00314             if ( !asn1_Finish(&c) )
00315             {
00316                return 0;
00317             }
00318             *pp=c.p;
00319             m_initialized = true;
00320             return this;
00321            err: // needed by ASN.1 macros
00322             return 0;
00323          }
00324 
00325       private :
00326          ShareLabel                 m_label;
00327          CODEX_ASN1::Integer        m_id;
00328          typename VType::ValueType  m_vc[ NumShares ];
00329    };
00330 
00331 }
00332 
00333 #endif /* __CODEX_VSS_SUBSHARELABEL_H__ */

Generated on Wed Jun 2 16:32:56 2004 for COrnell Data EXchange (CODEX) by doxygen1.2.18