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

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