#ifdef TRUSTEES // -*-c++-*-

#ifndef _NSRTRUST_
#define _NSRTRUST_

// $Source: /projects/HMS/nsr/udfCore/sys/nsrTrust.h,v $
// $Revision: 1.16 $    $Author: hmgr $
// $State: Exp $        $Locker:  $
// $Date: 96/05/21 11:40:41 $

//      Unpublished Confidential Information of Hewlett-Packard
//      Company. Do not disclose.  Copyright (c) Hewlett-Packard
//      Company 1995. All rights reserved
// 
// The contents of this software are proprietary and confidential to
// the Hewlett- Packard Company, and are limited in distribution to
// those with a direct need to know.  Individuals having access to
// this software are responsible for maintaining the confidentiality
// of the content and for keeping the software secure when not in use.
// Transfer to any party is strictly forbidden other than as expressly
// permitted in writing by Hewlett-Packard Company.  Unauthorized
// transfer to or possession by any unauthorized party may be a
// criminal offense.
//
//                  RESTRICTED RIGHTS LEGEND
//
//      Use,  duplication,  or disclosure by the U. S. Government is
//      subject to restrictions as set forth in subparagraph (c) (1)
//      (ii) in the Rights in Technical Data and Computer Software
//      clause in DFARS 252.227-7013 or any other successor clause.
//
//                      HEWLETT-PACKARD COMPANY
//                      3000 Hanover St.
//                      Palo Alto, CA  94304 U.S.A.
//
//      Rights for non-DOD U.S. Government Departments and Agencies
//      are as set forth in FAR 52.227-19 (c) (1,2) or any other
//      successor clause.
//
//
// CLASS: NetwareTrusteeInfo
//
// METHOD: NetwareTrusteeInfo::NetwareTrusteeInfo()
//
// NetwareTrusteeInfo Constructors
//
//    (void)           - empty trustee with no Assignment elements
//    (UINT32 size)    - a trustee with 'size' Assignment elements allocated

// METHOD: NetwareTrusteeInfo::set_inh_rights()
// METHOD: NetwareTrusteeInfo::getCount()
// METHOD: NetwareTrusteeInfo::getPath()
// METHOD: NetwareTrusteeInfo::setPath()
// METHOD: NetwareTrusteeInfo::empty()
// METHOD: NetwareTrusteeInfo::get_inh_rights()
// METHOD: NetwareTrusteeInfo::load()
// METHOD: NetwareTrusteeInfo::store()
// METHOD: NetwareTrusteeInfo::clear()
// METHOD: NetwareTrusteeInfo::resize()
// METHOD: NetwareTrusteeInfo::error()
// METHOD: NetwareTrusteeInfo::debug_print()
// METHOD: NetwareTrusteeInfo::operator[]
// METHOD: NetwareTrusteeInfo::operator=()
// 
// NetwareTrusteeInfo Method Overview
//
// Operator support for NetwareTrusteeInfo:
//
//    =  (assignment)
//    [] (array reference - returns reference to element # requested)
//
// Special member functions:
//
//    set_inh_rights()  -  sets the inhereted rights to the Rights passed in
//    set_empty_flag()  -  sets the empty flag to the state passed in
//    getCount()        -  returns the number of elements in the 
//    setPath()         -  assigns the unix path for the Trustee
//    getPath()         -  returns the unix path for the Trustee
//                         NetwareTrusteeInfo
//    empty()           -  returns True if NetwareTrusteeInfo is empty else 
//                         return False
//    get_inh_rights()  -  returns the inhereted rights 
//    load()            -  loads the NetwareTrusteeInfo with the inhereted 
//                         rights and 
//                         all the Assignment elements.  Frees any previous
//                         Assignment elements in the NetwareTrusteeInfo and 
//                         allocates
//                         new to load into.
//    store()           -  writes the NetwareTrusteeInfo to its path
//    clear()           -  clears all the fields in the NetwareTrusteeInfo
//    resize()          -  resizes the Trustee object to the size specified
//    error()           -  returns the error value
//    print()           -  Prints the trustee to stdout.  
//
// Special considerations:
//   
//    None
//


class NetwareTrusteeInfo {

public:

    // NOTE: this is VERY compiler dependant !!!
    
    class Inh_Rights {
        
    private:

        UINT16 data;

        #define NIR_S_MASK     0x0001
        #define NIR_R_MASK     0x0100
        #define NIR_W_MASK     0x0200
        #define NIR_ALWAYS_SET 0x0400
        #define NIR_C_MASK     0x0800
        #define NIR_E_MASK     0x1000
        #define NIR_A_MASK     0x2000
        #define NIR_F_MASK     0x4000
        #define NIR_M_MASK     0x8000

    public:

        // Constructors
        Inh_Rights() : data(NIR_ALWAYS_SET) {}

        // Modifiers
        void S(const Boolean bit) { (bit == NSR::True) ? (data |= NIR_S_MASK) :
                                                   (data &= ~(NIR_S_MASK)); }
        void R(const Boolean bit) { (bit == NSR::True) ? (data |= NIR_R_MASK) :
                                                   (data &= ~(NIR_R_MASK)); }
        void M(const Boolean bit) { (bit == NSR::True) ? (data |= NIR_M_MASK) :
                                                   (data &= ~(NIR_M_MASK)); }
        void C(const Boolean bit) { (bit == NSR::True) ? (data |= NIR_C_MASK) :
                                                   (data &= ~(NIR_C_MASK)); }
        void E(const Boolean bit) { (bit == NSR::True) ? (data |= NIR_E_MASK) :
                                                   (data &= ~(NIR_E_MASK)); }
        void A(const Boolean bit) { (bit == NSR::True) ? (data |= NIR_A_MASK) :
                                                   (data &= ~(NIR_A_MASK)); }
        void F(const Boolean bit) { (bit == NSR::True) ? (data |= NIR_F_MASK) :
                                                   (data &= ~(NIR_F_MASK)); }
        void W(const Boolean bit) { (bit == NSR::True) ? (data |= NIR_W_MASK) :
                                                   (data &= ~(NIR_W_MASK)); }


        // Selectors
        image_size() const {return sizeof(UINT16);}

        Boolean S() const {return (((data & NIR_S_MASK) == 0) ? 
                                        NSR::False : NSR::True);}
        Boolean W() const {return (((data & NIR_W_MASK) == 0) ? 
                                        NSR::False : NSR::True);}
        Boolean F() const {return (((data & NIR_F_MASK) == 0) ? 
                                        NSR::False : NSR::True);}
        Boolean A() const {return (((data & NIR_A_MASK) == 0) ? 
                                        NSR::False : NSR::True);}
        Boolean E() const {return (((data & NIR_E_MASK) == 0) ? 
                                        NSR::False : NSR::True);}
        Boolean C() const {return (((data & NIR_C_MASK) == 0) ? 
                                        NSR::False : NSR::True);}
        Boolean M() const {return (((data & NIR_M_MASK) == 0) ? 
                                        NSR::False : NSR::True);}
        Boolean R() const {return (((data & NIR_R_MASK) == 0) ? 
                                        NSR::False : NSR::True);}


    };

    
    class Rights {

    private:
        
        UINT32 data;

        #define NTR_S_MASK 0x00000100
        #define NTR_R_MASK 0x00000001
        #define NTR_W_MASK 0x00000002
        #define NTR_C_MASK 0x00000008
        #define NTR_E_MASK 0x00000010
        #define NTR_A_MASK 0x00000020
        #define NTR_F_MASK 0x00000040
        #define NTR_M_MASK 0x00000080


    public:
        // Constructors
        Rights() : data(0) {}

        // Modifiers
        void S(const Boolean bit) { (bit == NSR::True) ? (data |= NTR_S_MASK) :
                                                   (data &= ~(NTR_S_MASK)); }
        void R(const Boolean bit) { (bit == NSR::True) ? (data |= NTR_R_MASK) :
                                                   (data &= ~(NTR_R_MASK)); }
        void M(const Boolean bit) { (bit == NSR::True) ? (data |= NTR_M_MASK) :
                                                   (data &= ~(NTR_M_MASK)); }
        void C(const Boolean bit) { (bit == NSR::True) ? (data |= NTR_C_MASK) :
                                                   (data &= ~(NTR_C_MASK)); }
        void E(const Boolean bit) { (bit == NSR::True) ? (data |= NTR_E_MASK) :
                                                   (data &= ~(NTR_E_MASK)); }
        void A(const Boolean bit) { (bit == NSR::True) ? (data |= NTR_A_MASK) :
                                                   (data &= ~(NTR_A_MASK)); }
        void F(const Boolean bit) { (bit == NSR::True) ? (data |= NTR_F_MASK) :
                                                   (data &= ~(NTR_F_MASK)); }
        void W(const Boolean bit) { (bit == NSR::True) ? (data |= NTR_W_MASK) :
                                                   (data &= ~(NTR_W_MASK)); }

        // Selectors
        image_size() const {return sizeof(UINT32);}

        Boolean S() const {return (((data & NTR_S_MASK) == 0) ? 
                                        NSR::False : NSR::True);}
        Boolean W() const {return (((data & NTR_W_MASK) == 0) ? 
                                        NSR::False : NSR::True);}
        Boolean F() const {return (((data & NTR_F_MASK) == 0) ? 
                                        NSR::False : NSR::True);}
        Boolean A() const {return (((data & NTR_A_MASK) == 0) ? 
                                        NSR::False : NSR::True);}
        Boolean E() const {return (((data & NTR_E_MASK) == 0) ? 
                                        NSR::False : NSR::True);}
        Boolean C() const {return (((data & NTR_C_MASK) == 0) ? 
                                        NSR::False : NSR::True);}
        Boolean M() const {return (((data & NTR_M_MASK) == 0) ? 
                                        NSR::False : NSR::True);}
        Boolean R() const {return (((data & NTR_R_MASK) == 0) ? 
                                        NSR::False : NSR::True);}

    };




    class Assignment {

        // this structure is designed to directly lay over the Puzzle file
        // format.  This way reading and writing are very quick and simple
        // because no AND/OR operations need to be done on an iteration
        // through the elements.  The drawback here is that this is very
        // compiler dependant.  We must watch this if we switch to another
        // compiler.

    private:

        UINT32 user_;
        Rights rights_;

    public:

        // Constructors
        Assignment() : user_(0) {}

        // Modifiers
        void user(const UINT32 info) {user_ = info; }
        void rights(const Rights info) {rights_ = info; }

        // Selectors
        UINT32 user() const { return user_; }
        const Rights & rights() const { return rights_; };
        image_size() { return (sizeof(UINT32) + rights_.image_size()); }

    };

private:

    Inh_Rights inh_rights;
    Assignment *element;
    Assignment nullAssignment;
    RWCString unix_path;        
    UINT32 size;
    Boolean empty_flag; 

    Error err;

    void copy( const NetwareTrusteeInfo & );
    void free() { delete [] element; element = 0; size = 0;  }

    Boolean empty_element( const Assignment & el) {
        if ((memcmp (&(el), &nullAssignment, 
                             nullAssignment.image_size())) == 0) {
            return True;
        } else {
            return False;
        }
    }

public:

    // Constructors
    NetwareTrusteeInfo() : size(0), element(NULL), empty_flag(True) {}

    NetwareTrusteeInfo( UINT32 sz ) : size (sz), empty_flag (False) { 
        if (size != 0) {
            element = new Assignment [size];
            if (element == NULL) {
                // allocation failed
                size = 0;
                empty_flag = True;
                err = error_No_free_store;
            }
        } else {
            element = NULL;
            empty_flag = True;
        }
    }

    // Destructor
    ~NetwareTrusteeInfo() { free(); }

    // Modifiers 
    void load(RWCString);
    void store(RWCString);
    void setPath(RWCString path) { unix_path = path; }
    void clear() {Inh_Rights blank;  
    
                    unix_path = "";
                    empty_flag = True;
                    free();
                    size = 0;
                    element = 0;
                    err = error_None;
                    inh_rights = (blank); }

    void resize( UINT32 sz );

    Assignment &operator[]( UINT32 i ) {

        if (i < size)
            return element[i];
        else {
            err = error_Index_out_of_bounds;
            return nullAssignment;
        }
    }

    const Assignment &operator[]( UINT32 i ) const {

        if (i < size)
            return element[i];
        else 
            return nullAssignment;
    }

    NetwareTrusteeInfo &operator=( const NetwareTrusteeInfo &obj ) {

        if (this != &obj) {
            free();
            copy(obj);
        }
        return *this;

    }

    void set_inh_rights ( const Inh_Rights & info ) { inh_rights = info; }

    void set_empty_flag (Boolean flag) { empty_flag = flag; }

    // Selectors
    UINT32 getCount() const { return size; }
    CONST_CHAR_PTR getPath() const { return unix_path; }
    Boolean empty() const { return empty_flag; }
    Inh_Rights get_inh_rights() const {return inh_rights; }
    Error error() const { return err; }
    void print();

};





// CLASS: NetwareTrusteeInfoList
//
// METHOD: NetwareTrusteeInfoList::NetwareTrusteeInfoList()
//
// NetwareTrusteeInfoList Constructors
//
//    (CONST_CHAR_PTR vg, CONST_CHAR_PTR tp, CONST_CHAR_PTR itp)     
//              - stores the passed in strings for future need
//
// METHOD: NetwareTrusteeInfo::byPath_trustee()
// METHOD: NetwareTrusteeInfo::first_trustee()
// METHOD: NetwareTrusteeInfo::next_trustee()
// METHOD: NetwareTrusteeInfo::rename_trustee()
// METHOD: NetwareTrusteeInfo::add_trustee()
// METHOD: NetwareTrusteeInfo::error()
// METHOD: NetwareTrusteeInfo::operator=()
// 
// NetwareTrusteeInfo Method Overview
//
// Operator support for NetwareTrusteeInfo:
//
//    =  (assignment)
//
// Special member functions:
//
//    byPath_trustee()  -  loads a Trustee for the file specified by the path
//    first_trustee()   -  loads the first Trustee for the volume set  
//                         indicated by the trustee_path
//    next_trustee()    -  loads the next Trustee in sequence for the volume set
//                         indicated by the trustee_path
//    rename_trustee()  -  renames the trustee as specified
//    add_trustee()     -  adds the Trustee under the temporary name by
//    error()           -  returns the error value
//
// Special considerations:
//   
//    byPath(), first(), and next() will all return a reference to a 
//    NetwareTrusteeInfo.  The NetwareTrusteeInfoList object will ensure the 
//    NetwareTrusteeInfo referenced will be valid and stable until the next 
//    time one of these three methods is used, or until the 
//    NetwareTrusteeInfoList goes out of scope. 
//



class NetwareTrusteeInfoList {

    DIR *dir_desc;   // the file descriptor for the directory

    const RWCString rid_prefix;

    RWCString vgroot;

    RWCString export_trustee_path;

    RWCString import_trustee_path;

    Error err;            
    
    NetwareTrusteeInfo working_trustee;

    enum PathType {
        path_Import = 0,
        path_Export
    };

    NetwareTrusteeInfoList & operator=( const NetwareTrusteeInfoList &) {
        return *this;
    }
    NetwareTrusteeInfoList ( const NetwareTrusteeInfoList &) {}
    RWCString conv_unix_to_trustee_path (const RWCString &, const PathType);
    RWCString conv_trustee_to_unix_path (const RWCString &);
    RWCString conv_rid_to_trustee_path (const ino_t);

public:

    // Constructors
    NetwareTrusteeInfoList( CONST_CHAR_PTR vg, 
                 CONST_CHAR_PTR etp,
                 CONST_CHAR_PTR itp) : rid_prefix("/+"),
                                    vgroot(vg),
                                    export_trustee_path(etp),
                                    import_trustee_path(itp),
                                    dir_desc(0) {}

    // Destructor
    ~NetwareTrusteeInfoList() { if (dir_desc != 0)
                                    closedir (dir_desc); }

    // Modifiers

    NetwareTrusteeInfo &byPath_trustee( CONST_CHAR_PTR path );

    NetwareTrusteeInfo &first_trustee();
    NetwareTrusteeInfo &next_trustee();

    void rename_trustee( ino_t rid, CONST_CHAR_PTR path );

    void add_trustee( ino_t rid, NetwareTrusteeInfo &new_trustee );

    // Selectors
    Error error() const { return err; }

};


#ifdef NETWARE_ATTR
// CLASS: NetwareAttr
//
class NetwareAttr {

public:

    // Constructors
    NetwareAttr(Boolean isShareable = True) {}
    
    // Destructor
    ~NetwareAttr() {}

    // Modifiers
    void markShareable(Boolean val) { isShareable = val; }
    Boolean isShareable() { return isShareable_; }
private:
    Boolean isShareable_;
};
#endif /* NETWARE_ATTR */

#endif /* _NSRTRUST_ */

#endif /* TRUSTEES */
