ASN1C C/C++ Common Runtime  ASN1C v7.2.x
ASN1CBitStr.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 1997-2018 Objective Systems, Inc.
3  *
4  * This software is furnished under a license and may be used and copied
5  * only in accordance with the terms of such license and with the
6  * inclusion of the above copyright notice. This software or any other
7  * copies thereof may not be provided or otherwise made available to any
8  * other person. No title to and ownership of the software is hereby
9  * transferred.
10  *
11  * The information in this software is subject to change without notice
12  * and should not be construed as a commitment by Objective Systems, Inc.
13  *
14  * PROPRIETARY NOTICE
15  *
16  * This software is an unpublished work subject to a confidentiality agreement
17  * and is protected by copyright and trade secret law. Unauthorized copying,
18  * redistribution or other use of this work is prohibited.
19  *
20  * The above notice of copyright on this source code product does not indicate
21  * any actual or intended publication of such source code.
22  *
23  *****************************************************************************/
24 
25 #ifndef _ASN1CBITSTR_H_
26 #define _ASN1CBITSTR_H_
27 
28 #include "rtsrc/asn1CppTypes.h"
37 //
39 // ASN1CBitStr
40 //
41 // Bit string implementation. This class provides all functionalty
42 // necessary for bit strings.
43 //
44 // Author Artem Bolgar.
45 // version 1.52 21 Feb, 2003
46 //
47 #ifndef _NO_UTILS_CLASSES
48 
55  public:
56  virtual ASN1CBitStrSizeHolder* clone () = 0;
57  virtual OSUINT32 getValue () const = 0;
58  virtual int setValue (OSUINT32 value) = 0;
59 
60  virtual ~ASN1CBitStrSizeHolder() {}
61 } ;
62 
67  protected:
68  OSUINT8& mSize;
69 
70  public:
71  ASN1CBitStrSizeHolder8 (OSUINT8& value) : mSize(value) {}
72  virtual ASN1CBitStrSizeHolder* clone () {
73  return new ASN1CBitStrSizeHolder8 (mSize);
74  }
75  virtual OSUINT32 getValue () const { return mSize; }
76  virtual int setValue (OSUINT32 value);
77 
78  virtual ~ASN1CBitStrSizeHolder8() {}
79 
80  private:
82  return *this;
83  }
84 } ;
85 
90  protected:
91  OSUINT16& mSize;
92 
93  public:
94  ASN1CBitStrSizeHolder16 (OSUINT16& value) : mSize(value) {}
95  virtual ASN1CBitStrSizeHolder* clone () {
96  return new ASN1CBitStrSizeHolder16 (mSize);
97  }
98  virtual OSUINT32 getValue () const { return mSize; }
99  virtual int setValue (OSUINT32 value);
100 
101  virtual ~ASN1CBitStrSizeHolder16() {}
102 
103  private:
105  return *this;
106  }
107 } ;
108 
113  protected:
114  OSUINT32& mSize;
115 
116  public:
117  ASN1CBitStrSizeHolder32 (OSUINT32& value) : mSize(value) {}
118  virtual ASN1CBitStrSizeHolder* clone () {
119  return new ASN1CBitStrSizeHolder32 (mSize);
120  }
121  virtual OSUINT32 getValue () const { return mSize; }
122  virtual int setValue (OSUINT32 value);
123 
124  virtual ~ASN1CBitStrSizeHolder32() {}
125 
126  private:
128  return *this;
129  }
130 } ;
131 
141 class EXTRTCLASS ASN1CBitStr : public ASN1CType {
142  private:
143  OSUINT32 _numbits; // placeholder
144  OSOCTET* _units; // placeholder
145 
146  protected:
147  OSOCTET** mpUnits;
148  OSUINT32 mMaxNumBits;
149  ASN1CBitStrSizeHolder* mpNumBits;
150  OSUINT32 mUnitsUsed;
151  OSUINT32 mUnitsAllocated;
152  OSBOOL mDynAlloc;
153 
154  private:
155  OSOCTET* allocateMemory (OSUINT32 sz);
156  OSOCTET* reallocateMemory
157  (OSOCTET* old, OSUINT32 oldBufSz, OSUINT32 newBufSz);
158  void freeMemory (OSOCTET* mem);
159 
160  /*
161  * Set the field mUnitsUsed with the logical size in units of the bit
162  * set. WARNING:This function assumes that the number of units actually
163  * in use is less than or equal to the current value of mUnitsUsed!
164  */
165  void recalculateUnitsUsed();
166 
167  /*
168  * Ensures that the bit string can hold enough units.
169  * param unitsRequired the minimum acceptable number of units.
170  */
171  int checkCapacity (OSUINT32 unitsRequired);
172 
173  /*
174  * Returns the unit of this bitset at index j as if this bit string had an
175  * infinite amount of storage.
176  */
177  OSOCTET getBits (OSUINT32 j);
178 
179  void privateInit (OSUINT32 nbits);
180  void privateInit (OSOCTET* bitStr, OSUINT32 maxNumbits_);
181  void privateInit (ASN1TDynBitStr& bitStr);
182 
183  protected:
184  /*
185  * Creates an empty bit string. The string should be additionaly initialized
186  * by call to 'init' method.
187  */
188  EXTRTMETHOD ASN1CBitStr (OSRTMessageBufferIF& msgBuf);
189 
190  /*
191  * Default protected ctor. mpContext should be set and the string should be
192  * additionaly initialized by call to 'init' method.
193  */
194  EXTRTMETHOD ASN1CBitStr();
195 
196  /*
197  * This constructor creates an empty bit string.
198  *
199  * @param ctxt - Reference to reference-counted context object.
200  */
201  EXTRTMETHOD ASN1CBitStr (OSRTContext& ctxt);
202 
203  /*
204  * This constructor initializes the internal bit string to the
205  * given value.
206  *
207  * @param pBits - Pointer to binary bit string data.
208  * @param numbits - Reference to counter to hold number of bits.
209  * @param maxNumbits - Maximum size (in bits) of this bit string.
210  */
211  EXTRTMETHOD ASN1CBitStr
212  (OSOCTET* pBits, OSUINT32& numbits, OSUINT32 maxNumbits);
213 
214  EXTRTMETHOD ASN1CBitStr
215  (OSOCTET* pBits, OSUINT8& numbits, OSUINT32 maxNumbits);
216 
217  EXTRTMETHOD ASN1CBitStr
218  (OSOCTET* pBits, OSUINT16& numbits, OSUINT32 maxNumbits);
219 
220  // Fixed-size bit string
221  EXTRTMETHOD ASN1CBitStr (OSOCTET* pBits, OSUINT32 maxNumbits);
222 
223  /*
224  * This constructor initializes the internal bit string to the
225  * given value.
226  *
227  * @param bitstr - Reference to a bit string variable.
228  */
229  EXTRTMETHOD ASN1CBitStr (ASN1TDynBitStr& bitStr);
230 
231  void initBase (OSOCTET* pBits, OSUINT32 numbits, OSUINT32 maxNumbits);
232 
233  EXTRTMETHOD void init
234  (OSOCTET* pBits, OSUINT32& numbits, OSUINT32 maxNumbits);
235 
236  EXTRTMETHOD void init
237  (OSOCTET* pBits, OSUINT8& numbits, OSUINT32 maxNumbits);
238 
239  EXTRTMETHOD void init
240  (OSOCTET* pBits, OSUINT16& numbits, OSUINT32 maxNumbits);
241 
242  EXTRTMETHOD void init (ASN1TDynBitStr& bitStr);
243 
244  public:
254  EXTRTMETHOD ASN1CBitStr (OSRTMessageBufferIF& msgbuf, OSUINT32 nbits);
255 
267  EXTRTMETHOD ASN1CBitStr
268  (OSRTMessageBufferIF& msgbuf, OSOCTET* bitStr,
269  OSUINT32& numbits, OSUINT32 maxNumbits_);
270 
271  EXTRTMETHOD ASN1CBitStr
272  (OSRTMessageBufferIF& msgbuf, OSOCTET* bitStr,
273  OSUINT8& numbits, OSUINT32 maxNumbits_);
274 
275  EXTRTMETHOD ASN1CBitStr
276  (OSRTMessageBufferIF& msgbuf, OSOCTET* bitStr,
277  OSUINT16& numbits, OSUINT32 maxNumbits_);
278 
279  EXTRTMETHOD ASN1CBitStr
280  (OSRTMessageBufferIF& msgbuf, OSOCTET* bitStr, OSUINT32 maxNumbits_);
281 
282  /*
283  * Creates bits string from ASN1TDynBitStr. Ctor does not copy bytes,
284  * just assign array (data member of ASN1TDynBitStr) to units.
285  * This string will be extendable.
286  */
287  EXTRTMETHOD ASN1CBitStr
288  (OSRTMessageBufferIF& msgBuf, ASN1TDynBitStr& bitStr);
289 
290  /*
291  * Creates empty bit string. If nbits == 0 then bit string is dynamic;
292  * else capacity will be fixed to nbits.
293  */
294  EXTRTMETHOD ASN1CBitStr (OSRTContext& ctxt, OSUINT32 nbits);
295 
296  /*
297  * Creates bits string from array of bits. Ctor does not copy bytes,
298  * just assign passed array to units.
299  * Param bitStr - pointer to static byte array
300  * Param numbits - reference to length of bit string (in bits);
301  * Param maxNumbits_ - sets maximum length in bits
302  */
303  EXTRTMETHOD ASN1CBitStr
304  (OSRTContext& ctxt, OSOCTET* bitStr,
305  OSUINT32& octsNumbits, OSUINT32 maxNumbits_);
306 
307  EXTRTMETHOD ASN1CBitStr
308  (OSRTContext& ctxt, OSOCTET* bitStr,
309  OSUINT8& octsNumbits, OSUINT32 maxNumbits_);
310 
311  EXTRTMETHOD ASN1CBitStr
312  (OSRTContext& ctxt, OSOCTET* bitStr,
313  OSUINT16& octsNumbits, OSUINT32 maxNumbits_);
314 
315  EXTRTMETHOD ASN1CBitStr
316  (OSRTContext& ctxt, OSOCTET* bitStr, OSUINT32 maxNumbits_);
317 
318  /*
319  * Creates bits string from ASN1TDynBitStr. Ctor does not copy bytes,
320  * just assign array (data member of ASN1TDynBitStr) to units.
321  * This string will be extendable.
322  */
323  EXTRTMETHOD ASN1CBitStr (OSRTContext& ctxt, ASN1TDynBitStr& bitStr);
324 
325  /*
326  * Copy constructor.
327  */
328  EXTRTMETHOD ASN1CBitStr (const ASN1CBitStr& bitStr);
329 
330  /*
331  * Copy constructor, but allows to specify 'extendable' parameter
332  */
333  EXTRTMETHOD ASN1CBitStr (const ASN1CBitStr& bitStr, OSBOOL extendable);
334 
335 
336  EXTRTMETHOD ~ASN1CBitStr();
337 
349  /*
350  * Sets the bit at the specified index to 1.
351  *
352  * Returns: 0 - if succeed, or
353  * RTERR_OUTOFBND - if 'bitIndex' is out of bounds, or
354  * other error codes (see asn1type.h).
355  */
356  EXTRTMETHOD int set (OSUINT32 bitIndex);
357 
374  /*
375  * Sets the bits from the specified fromIndex(inclusive) to the
376  * specified toIndex(exclusive) to 1.
377  *
378  * Returns: 0 - if succeed, or
379  * RTERR_OUTOFBND - if one of indexes is out of bounds, or
380  * RTERR_RANGERR - if fromIndex > toIndex, or
381  * other error codes (see asn1type.h).
382  */
383  EXTRTMETHOD int set (OSUINT32 fromIndex, OSUINT32 toIndex);
384 
397  /*Returns (details): RTERR_OUTOFBND - if 'bitIndex' is out of bounds, or
398  * other error codes (see asn1type.h).
399  */
400  inline int change (OSUINT32 bitIndex, OSBOOL value) {
401  return (value) ? set (bitIndex) : clear (bitIndex);
402  }
403 
416  /*
417  * Sets the bit specified by the index to 0.
418  *
419  * Returns: 0 - if succeed, or
420  * RTERR_OUTOFBND - if 'bitIndex' is out of bounds, or
421  * other error codes (see asn1type.h).
422  */
423  EXTRTMETHOD int clear (OSUINT32 bitIndex);
424 
441  /*
442  * Sets the bits from the specified fromIndex(inclusive) to the
443  * specified toIndex(exclusive) to 0.
444  *
445  * Returns: 0 - if succeed, or
446  * RTERR_OUTOFBND - if one of indexes is out of bounds, or
447  * RTERR_RANGERR - if fromIndex > toIndex, or
448  * other error codes (see asn1type.h).
449  */
450  EXTRTMETHOD int clear (OSUINT32 fromIndex, OSUINT32 toIndex);
451 
460  /*
461  * Sets all of the bits in this bit string to 0.
462  */
463  EXTRTMETHOD void clear();
464 
480  /*
481  * Sets the bit at the specified index to to the complement of its
482  * current value.
483  *
484  * Returns: 0 - if succeed, or
485  * RTERR_OUTOFBND - if 'bitIndex' is out of bounds, or
486  * other error codes (see asn1type.h).
487  */
488  EXTRTMETHOD int invert (OSUINT32 bitIndex);
489 
509  /*
510  * Sets each bit from the specified fromIndex(inclusive) to the
511  * specified toIndex(exclusive) to the complement of its current
512  * value.
513  *
514  * Returns: 0 - if succeed, or
515  * RTERR_OUTOFBND - if one of indexes is out of bounds, or
516  * RTERR_RANGERR - if fromIndex > toIndex, or
517  * other error codes (see asn1type.h).
518  */
519  EXTRTMETHOD int invert(OSUINT32 fromIndex, OSUINT32 toIndex);
520 
532  /*
533  * Returns the value of the bit with the specified index.
534  */
535  EXTRTMETHOD OSBOOL get(OSUINT32 bitIndex);
536 
542  /*
543  * Returns TRUE if the value of the bit with the specified index is set to 1.
544  */
545  inline OSBOOL isSet(OSUINT32 bitIndex) { return get(bitIndex); }
546 
555  /*
556  * Returns true if this bit string contains no bits that are set
557  * to 1.
558  */
559  inline OSBOOL isEmpty() { return (mUnitsUsed == 0); }
560 
569  /*
570  * Returns the number of bytes of space actually in use by this
571  * bit string to represent bit values.
572  */
573  EXTRTMETHOD OSUINT32 size() const;
574 
586  /*
587  * Returns the "logical size" of this bit string: the index of
588  * the highest set bit in the bit string plus one. Returns zero
589  * if the bit string contains no set bits.
590  */
591  EXTRTMETHOD OSUINT32 length() const;
592 
602  /*
603  * Returns the number of bits set to 1 in this
604  * bit string.
605  */
606  EXTRTMETHOD OSUINT32 cardinality() const;
607 
621  /*
622  * Copies bit string to buffer (pBuf).
623  */
624  EXTRTMETHOD int getBytes (OSOCTET* pBuf, OSUINT32 bufSz);
625 
651  EXTRTMETHOD int get
652  (OSUINT32 fromIndex, OSUINT32 toIndex, OSOCTET* pBuf, OSUINT32 bufSz);
653 
671  EXTRTMETHOD int doAnd (const OSOCTET* pOctstr, OSUINT32 octsNumbits);
672 
684  inline int doAnd (const ASN1TDynBitStr& bitStr) {
685  return doAnd(bitStr.data, bitStr.numbits);
686  }
687 
698  inline int doAnd(const ASN1CBitStr& bitStr) {
699  return doAnd(*bitStr.mpUnits, bitStr.length());
700  }
701 
702 
719  EXTRTMETHOD int doOr(const OSOCTET* pOctstr, OSUINT32 octsNumbits);
720 
732  inline int doOr(const ASN1TDynBitStr& bitStr) {
733  return doOr(bitStr.data, bitStr.numbits);
734  }
735 
746  inline int doOr(const ASN1CBitStr& bitStr) {
747  return doOr(*bitStr.mpUnits, bitStr.length());
748  }
749 
766  EXTRTMETHOD int doXor(const OSOCTET* pOctstr, OSUINT32 octsNumbits);
767 
779  inline int doXor(const ASN1TDynBitStr& bitStr) {
780  return doXor(bitStr.data, bitStr.numbits);
781  }
782 
793  inline int doXor(const ASN1CBitStr& bitStr) {
794  return doXor(*bitStr.mpUnits, bitStr.length());
795  }
796 
811  /*
812  * Clears all of the bits in this bit string whose corresponding
813  * bit is set in the specified bit string.
814  *
815  * Returns: 0 - if succeed, or
816  * ASN_E_INVLEN - if 'octsNumbits' is negative, or
817  * RTERR_INVPARAM - if pOctstr is null, or
818  * other error codes (see asn1type.h).
819  */
820  EXTRTMETHOD int doAndNot(const OSOCTET* pOctstr, OSUINT32 octsNumbits);
821 
836  inline int doAndNot(const ASN1TDynBitStr& bitStr) {
837  return doAndNot(bitStr.data, bitStr.numbits);
838  }
839 
853  inline int doAndNot(const ASN1CBitStr& bitStr) {
854  return doAndNot(*bitStr.mpUnits, bitStr.length());
855  }
856 
870  /*
871  * Shifts all bits to the left by 'shift' bits.
872  */
873  EXTRTMETHOD int shiftLeft(OSUINT32 shift);
874 
888  /*
889  * Shifts all bits to the right by 'shift' bits.
890  */
891  EXTRTMETHOD int shiftRight(OSUINT32 shift);
892 
899  /*
900  * Returns number of unused bits in last unit.
901  */
902  EXTRTMETHOD OSUINT32 unusedBitsInLastUnit();
903 
914  /*
915  * Returns filled ASN1TDynBitStr. Memory for data is not
916  * reallocated, thus, this ASN1TDynBitStr will be eligible while
917  * this ASN1CBitStr is in scope.
918  */
919  EXTRTMETHOD operator ASN1TDynBitStr();
920 
930  /*
931  * Returns pointer to filled ASN1TDynBitStr. In this method,
932  * memory is allocated, and bits are copied to it.
933  */
934  EXTRTMETHOD operator ASN1TDynBitStr*();
935 } ;
936 #else
937 typedef class _ASN1CBitStr : public ASN1CType {
938  public:
939  _ASN1CBitStr (OSRTMessageBufferIF& msgBuf, OSUINT32 nbits) :
940  ASN1CType (msgBuf) {}
941 
942  _ASN1CBitStr (OSRTMessageBufferIF& msgBuf, OSOCTET* bitStr,
943  OSUINT32& octsNumbits, OSUINT32 maxNumbits_) :
944  ASN1CType (msgBuf) {}
945 
946  _ASN1CBitStr (OSRTMessageBufferIF& msgBuf, ASN1TDynBitStr& bitStr) :
947  ASN1CType (msgBuf) {}
948 } ASN1CBitStr;
949 #endif // _NO_UTILS_CLASSES
950 #endif // _ASN1CBITSTR_H_
951 
EXTRTMETHOD OSUINT32 length() const
int doXor(const ASN1CBitStr &bitStr)
Definition: ASN1CBitStr.h:793
int doOr(const ASN1TDynBitStr &bitStr)
Definition: ASN1CBitStr.h:732
int doAndNot(const ASN1CBitStr &bitStr)
Definition: ASN1CBitStr.h:853
int doAnd(const ASN1CBitStr &bitStr)
Definition: ASN1CBitStr.h:698
int change(OSUINT32 bitIndex, OSBOOL value)
Definition: ASN1CBitStr.h:400
Definition: asn1CppTypes.h:314
int doXor(const ASN1TDynBitStr &bitStr)
Definition: ASN1CBitStr.h:779
OSBOOL isSet(OSUINT32 bitIndex)
Definition: ASN1CBitStr.h:545
Definition: ASN1CBitStr.h:54
int doOr(const ASN1CBitStr &bitStr)
Definition: ASN1CBitStr.h:746
Definition: asn1CppTypes.h:624
Definition: ASN1CBitStr.h:66
Definition: ASN1CBitStr.h:112
Definition: OSRTContext.h:65
Definition: ASN1CBitStr.h:141
int doAnd(const ASN1TDynBitStr &bitStr)
Definition: ASN1CBitStr.h:684
int doAndNot(const ASN1TDynBitStr &bitStr)
Definition: ASN1CBitStr.h:836
Definition: ASN1CBitStr.h:89
OSBOOL isEmpty()
Definition: ASN1CBitStr.h:559