ASN1CBitStr.h
Go to the documentation of this file.00001 /* 00002 * Copyright (c) 1997-2009 Objective Systems, Inc. 00003 * 00004 * This software is furnished under a license and may be used and copied 00005 * only in accordance with the terms of such license and with the 00006 * inclusion of the above copyright notice. This software or any other 00007 * copies thereof may not be provided or otherwise made available to any 00008 * other person. No title to and ownership of the software is hereby 00009 * transferred. 00010 * 00011 * The information in this software is subject to change without notice 00012 * and should not be construed as a commitment by Objective Systems, Inc. 00013 * 00014 * PROPRIETARY NOTICE 00015 * 00016 * This software is an unpublished work subject to a confidentiality agreement 00017 * and is protected by copyright and trade secret law. Unauthorized copying, 00018 * redistribution or other use of this work is prohibited. 00019 * 00020 * The above notice of copyright on this source code product does not indicate 00021 * any actual or intended publication of such source code. 00022 * 00023 *****************************************************************************/ 00024 00025 #ifndef _ASN1CBITSTR_H_ 00026 #define _ASN1CBITSTR_H_ 00027 00028 #include "rtsrc/asn1CppTypes.h" 00037 00038 // 00039 // ASN1CBitStr 00040 // 00041 // Bit string implementation. This class provides all functionalty 00042 // necessary for bit strings. 00043 // 00044 // Author Artem Bolgar. 00045 // version 1.52 21 Feb, 2003 00046 // 00047 #ifndef _NO_UTILS_CLASSES 00048 00058 class EXTRTCLASS ASN1CBitStr : public ASN1CType { 00059 private: 00060 OSUINT32 _numbits; // placeholder 00061 OSOCTET* _units; // placeholder 00062 protected: 00063 OSOCTET** mpUnits; 00064 OSUINT32 mMaxNumBits; 00065 OSUINT32* mpNumBits; 00066 int mUnitsUsed; 00067 int mUnitsAllocated; 00068 OSBOOL mDynAlloc; 00069 private: 00070 EXTRTMETHOD OSOCTET* allocateMemory(int sz); 00071 00072 EXTRTMETHOD OSOCTET* reallocateMemory(OSOCTET* old, int oldBufSz, int newBufSz); 00073 00074 EXTRTMETHOD void freeMemory(OSOCTET* mem); 00075 /* 00076 * Set the field mUnitsUsed with the logical size in units of the bit 00077 * set. WARNING:This function assumes that the number of units actually 00078 * in use is less than or equal to the current value of mUnitsUsed! 00079 */ 00080 EXTRTMETHOD void recalculateUnitsUsed(); 00081 00082 /* 00083 * Ensures that the bit string can hold enough units. 00084 * param unitsRequired the minimum acceptable number of units. 00085 */ 00086 EXTRTMETHOD int checkCapacity(int unitsRequired); 00087 00088 00089 /* 00090 * Returns the unit of this bitset at index j as if this bit string had an 00091 * infinite amount of storage. 00092 */ 00093 EXTRTMETHOD OSOCTET getBits(int j); 00094 00095 EXTRTMETHOD void privateInit (OSUINT32 nbits); 00096 EXTRTMETHOD void privateInit (OSOCTET* bitStr, OSUINT32 maxNumbits_); 00097 EXTRTMETHOD void privateInit (ASN1TDynBitStr& bitStr); 00098 protected: 00099 /* 00100 * Creates an empty bit string. The string should be additionaly initialized 00101 * by call to 'init' method. 00102 */ 00103 EXTRTMETHOD ASN1CBitStr (OSRTMessageBufferIF& msgBuf); 00104 00105 /* 00106 * Default protected ctor. mpContext should be set and the string should be 00107 * additionaly initialized by call to 'init' method. 00108 */ 00109 EXTRTMETHOD ASN1CBitStr(); 00110 00111 /* 00112 * This constructor creates an empty bit string. 00113 * 00114 * @param ctxt - Reference to reference-counted context object. 00115 */ 00116 EXTRTMETHOD ASN1CBitStr (OSRTContext& ctxt); 00117 00118 /* 00119 * This constructor initializes the internal bit string to the 00120 * given value. 00121 * 00122 * @param pBits - Pointer to binary bit string data. 00123 * @param numbits - Reference to counter to hold number of bits. 00124 * @param maxNumbits - Maximum size (in bits) of this bit string. 00125 */ 00126 EXTRTMETHOD ASN1CBitStr (OSOCTET* pBits, OSUINT32& numbits, OSUINT32 maxNumbits); 00127 00128 /* 00129 * This constructor initializes the internal bit string to the 00130 * given value. 00131 * 00132 * @param bitstr - Reference to a bit string variable. 00133 */ 00134 EXTRTMETHOD ASN1CBitStr (ASN1TDynBitStr& bitStr); 00135 00136 EXTRTMETHOD void init (OSOCTET* pBits, OSUINT32& numbits, OSUINT32 maxNumbits); 00137 EXTRTMETHOD void init (ASN1TDynBitStr& bitStr); 00138 00139 public: 00149 EXTRTMETHOD ASN1CBitStr (OSRTMessageBufferIF& msgbuf, OSUINT32 nbits); 00150 00162 EXTRTMETHOD ASN1CBitStr (OSRTMessageBufferIF& msgbuf, OSOCTET* bitStr, 00163 OSUINT32& numbits, OSUINT32 maxNumbits_); 00164 00165 /* 00166 * Creates bits string from ASN1TDynBitStr. Ctor does not copy bytes, 00167 * just assign array (data member of ASN1TDynBitStr) to units. 00168 * This string will be extendable. 00169 */ 00170 EXTRTMETHOD ASN1CBitStr(OSRTMessageBufferIF& msgBuf, ASN1TDynBitStr& bitStr); 00171 00172 /* 00173 * Creates empty bit string. If nbits == 0 then bit string is dynamic; 00174 * else capacity will be fixed to nbits. 00175 */ 00176 EXTRTMETHOD ASN1CBitStr(OSRTContext& ctxt, OSUINT32 nbits); 00177 00178 /* 00179 * Creates bits string from array of bits. Ctor does not copy bytes, 00180 * just assign passed array to units. 00181 * Param bitStr - pointer to static byte array 00182 * Param numbits - reference to length of bit string (in bits); 00183 * Param maxNumbits_ - sets maximum length in bits 00184 */ 00185 EXTRTMETHOD ASN1CBitStr(OSRTContext& ctxt, OSOCTET* bitStr, 00186 OSUINT32& octsNumbits, OSUINT32 maxNumbits_); 00187 00188 /* 00189 * Creates bits string from ASN1TDynBitStr. Ctor does not copy bytes, 00190 * just assign array (data member of ASN1TDynBitStr) to units. 00191 * This string will be extendable. 00192 */ 00193 EXTRTMETHOD ASN1CBitStr(OSRTContext& ctxt, ASN1TDynBitStr& bitStr); 00194 00195 /* 00196 * Copy constructor. 00197 */ 00198 EXTRTMETHOD ASN1CBitStr(const ASN1CBitStr& bitStr); 00199 00200 /* 00201 * Copy constructor, but allows to specify 'extendable' parameter 00202 */ 00203 EXTRTMETHOD ASN1CBitStr(const ASN1CBitStr& bitStr, OSBOOL extendable); 00204 00205 00206 EXTRTMETHOD ~ASN1CBitStr(); 00218 /* 00219 * Sets the bit at the specified index to 1. 00220 * 00221 * Returns: 0 - if succeed, or 00222 * RTERR_OUTOFBND - if 'bitIndex' is out of bounds, or 00223 * other error codes (see asn1type.h). 00224 */ 00225 EXTRTMETHOD int set(OSUINT32 bitIndex); 00226 00243 /* 00244 * Sets the bits from the specified fromIndex(inclusive) to the 00245 * specified toIndex(exclusive) to 1. 00246 * 00247 * Returns: 0 - if succeed, or 00248 * RTERR_OUTOFBND - if one of indexes is out of bounds, or 00249 * RTERR_RANGERR - if fromIndex > toIndex, or 00250 * other error codes (see asn1type.h). 00251 */ 00252 EXTRTMETHOD int set(OSUINT32 fromIndex, OSUINT32 toIndex); 00253 00266 /*Returns (details): RTERR_OUTOFBND - if 'bitIndex' is out of bounds, or 00267 * other error codes (see asn1type.h). 00268 */ 00269 inline int change(OSUINT32 bitIndex, OSBOOL value) { 00270 if(value) 00271 return set(bitIndex); 00272 else 00273 return clear(bitIndex); 00274 } 00275 00288 /* 00289 * Sets the bit specified by the index to 0. 00290 * 00291 * Returns: 0 - if succeed, or 00292 * RTERR_OUTOFBND - if 'bitIndex' is out of bounds, or 00293 * other error codes (see asn1type.h). 00294 */ 00295 EXTRTMETHOD int clear(OSUINT32 bitIndex); 00296 00313 /* 00314 * Sets the bits from the specified fromIndex(inclusive) to the 00315 * specified toIndex(exclusive) to 0. 00316 * 00317 * Returns: 0 - if succeed, or 00318 * RTERR_OUTOFBND - if one of indexes is out of bounds, or 00319 * RTERR_RANGERR - if fromIndex > toIndex, or 00320 * other error codes (see asn1type.h). 00321 */ 00322 EXTRTMETHOD int clear(OSUINT32 fromIndex, OSUINT32 toIndex); 00323 00332 /* 00333 * Sets all of the bits in this bit string to 0. 00334 */ 00335 EXTRTMETHOD void clear(); 00336 00352 /* 00353 * Sets the bit at the specified index to to the complement of its 00354 * current value. 00355 * 00356 * Returns: 0 - if succeed, or 00357 * RTERR_OUTOFBND - if 'bitIndex' is out of bounds, or 00358 * other error codes (see asn1type.h). 00359 */ 00360 EXTRTMETHOD int invert(OSUINT32 bitIndex); 00361 00381 /* 00382 * Sets each bit from the specified fromIndex(inclusive) to the 00383 * specified toIndex(exclusive) to the complement of its current 00384 * value. 00385 * 00386 * Returns: 0 - if succeed, or 00387 * RTERR_OUTOFBND - if one of indexes is out of bounds, or 00388 * RTERR_RANGERR - if fromIndex > toIndex, or 00389 * other error codes (see asn1type.h). 00390 */ 00391 EXTRTMETHOD int invert(OSUINT32 fromIndex, OSUINT32 toIndex); 00392 00404 /* 00405 * Returns the value of the bit with the specified index. 00406 */ 00407 EXTRTMETHOD OSBOOL get(OSUINT32 bitIndex); 00408 00414 /* 00415 * Returns TRUE if the value of the bit with the specified index is set to 1. 00416 */ 00417 inline OSBOOL isSet(OSUINT32 bitIndex) { return get(bitIndex); } 00418 00427 /* 00428 * Returns true if this bit string contains no bits that are set 00429 * to 1. 00430 */ 00431 inline OSBOOL isEmpty() { return (mUnitsUsed == 0); } 00432 00441 /* 00442 * Returns the number of bytes of space actually in use by this 00443 * bit string to represent bit values. 00444 */ 00445 EXTRTMETHOD int size() const; 00446 00458 /* 00459 * Returns the "logical size" of this bit string: the index of 00460 * the highest set bit in the bit string plus one. Returns zero 00461 * if the bit string contains no set bits. 00462 */ 00463 EXTRTMETHOD OSUINT32 length() const; 00464 00474 /* 00475 * Returns the number of bits set to 1 in this 00476 * bit string. 00477 */ 00478 EXTRTMETHOD int cardinality() const; 00479 00493 /* 00494 * Copies bit string to buffer (pBuf). 00495 */ 00496 EXTRTMETHOD int getBytes(OSOCTET* pBuf, int bufSz); 00497 00523 EXTRTMETHOD int get(OSUINT32 fromIndex, OSUINT32 toIndex, OSOCTET* pBuf, int bufSz); 00524 00536 /* 00537 * Performs a logical AND of this target bit set with the 00538 * argument bit set. 00539 * 00540 * Returns: 0 - if succeed, or 00541 * ASN_E_INVLEN - if 'octsNumbits' is negative, or 00542 * RTERR_INVPARAM - if pOctstr is the same bit string as this or null, or 00543 * other error codes (see asn1type.h). 00544 */ 00545 EXTRTMETHOD int doAnd(const OSOCTET* pOctstr, OSUINT32 octsNumbits); 00546 00558 inline int doAnd(const ASN1TDynBitStr& bitStr) { 00559 return doAnd(bitStr.data, bitStr.numbits); 00560 } 00561 00572 inline int doAnd(const ASN1CBitStr& bitStr) { 00573 return doAnd(*bitStr.mpUnits, bitStr.length()); 00574 } 00575 00576 00588 /* 00589 * Performs a logical OR of this target bit set with the 00590 * argument bit set. 00591 * 00592 * Returns: 0 - if succeed, or 00593 * ASN_E_INVLEN - if 'octsNumbits' is negative, or 00594 * RTERR_INVPARAM - if pOctstr is the same bit string as this or null, or 00595 * other error codes (see asn1type.h). 00596 */ 00597 EXTRTMETHOD int doOr(const OSOCTET* pOctstr, OSUINT32 octsNumbits); 00598 00610 inline int doOr(const ASN1TDynBitStr& bitStr) { 00611 return doOr(bitStr.data, bitStr.numbits); 00612 } 00613 00624 inline int doOr(const ASN1CBitStr& bitStr) { 00625 return doOr(*bitStr.mpUnits, bitStr.length()); 00626 } 00627 00639 /* 00640 * Performs a logical XOR of this target bit set with the 00641 * argument bit set. 00642 * 00643 * Returns: 0 - if succeed, or 00644 * ASN_E_INVLEN - if 'octsNumbits' is negative, or 00645 * RTERR_INVPARAM - if pOctstr is null, or 00646 * other error codes (see asn1type.h). 00647 */ 00648 EXTRTMETHOD int doXor(const OSOCTET* pOctstr, OSUINT32 octsNumbits); 00649 00661 inline int doXor(const ASN1TDynBitStr& bitStr) { 00662 return doXor(bitStr.data, bitStr.numbits); 00663 } 00664 00675 inline int doXor(const ASN1CBitStr& bitStr) { 00676 return doXor(*bitStr.mpUnits, bitStr.length()); 00677 } 00678 00693 /* 00694 * Clears all of the bits in this bit string whose corresponding 00695 * bit is set in the specified bit string. 00696 * 00697 * Returns: 0 - if succeed, or 00698 * ASN_E_INVLEN - if 'octsNumbits' is negative, or 00699 * RTERR_INVPARAM - if pOctstr is null, or 00700 * other error codes (see asn1type.h). 00701 */ 00702 EXTRTMETHOD int doAndNot(const OSOCTET* pOctstr, OSUINT32 octsNumbits); 00703 00718 inline int doAndNot(const ASN1TDynBitStr& bitStr) { 00719 return doAndNot(bitStr.data, bitStr.numbits); 00720 } 00721 00735 inline int doAndNot(const ASN1CBitStr& bitStr) { 00736 return doAndNot(*bitStr.mpUnits, bitStr.length()); 00737 } 00738 00752 /* 00753 * Shifts all bits to the left by 'shift' bits. 00754 */ 00755 EXTRTMETHOD int shiftLeft(OSUINT32 shift); 00756 00770 /* 00771 * Shifts all bits to the right by 'shift' bits. 00772 */ 00773 EXTRTMETHOD int shiftRight(OSUINT32 shift); 00774 00782 /* 00783 * Returns number of unused bits in last unit. 00784 */ 00785 EXTRTMETHOD int unusedBitsInLastUnit(); 00786 00797 /* 00798 * Returns filled ASN1TDynBitStr. Memory for data is not 00799 * reallocated, thus, this ASN1TDynBitStr will be eligible while 00800 * this ASN1CBitStr is in scope. 00801 */ 00802 EXTRTMETHOD operator ASN1TDynBitStr(); 00803 00813 /* 00814 * Returns pointer to filled ASN1TDynBitStr. In this method, 00815 * memory is allocated, and bits are copied to it. 00816 */ 00817 EXTRTMETHOD operator ASN1TDynBitStr*(); 00818 } ; 00819 #else 00820 typedef class _ASN1CBitStr : public ASN1CType { 00821 public: 00822 _ASN1CBitStr (OSRTMessageBufferIF& msgBuf, OSUINT32 nbits) : 00823 ASN1CType (msgBuf) {} 00824 00825 _ASN1CBitStr (OSRTMessageBufferIF& msgBuf, OSOCTET* bitStr, 00826 OSUINT32& octsNumbits, OSUINT32 maxNumbits_) : 00827 ASN1CType (msgBuf) {} 00828 00829 _ASN1CBitStr (OSRTMessageBufferIF& msgBuf, ASN1TDynBitStr& bitStr) : 00830 ASN1CType (msgBuf) {} 00831 } ASN1CBitStr; 00832 #endif // _NO_UTILS_CLASSES 00833 #endif // _ASN1CBITSTR_H_ 00834