ASN1C C/C++ Common Runtime  ASN1C v7.3.x
ASN1CBitStr.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 1997-2019 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 #ifndef _NO_UTILS_CLASSES
45 
52  public:
53  virtual ASN1CBitStrSizeHolder* clone () = 0;
54  virtual OSSIZE getValue () const = 0;
55 
62  virtual int setValue (OSSIZE value) = 0;
63 
64  virtual ~ASN1CBitStrSizeHolder() {}
65 } ;
66 
71  protected:
72  OSUINT8& mSize;
73 
74  public:
75  ASN1CBitStrSizeHolder8 (OSUINT8& value) : mSize(value) {}
76  virtual ASN1CBitStrSizeHolder* clone () {
77  return new ASN1CBitStrSizeHolder8 (mSize);
78  }
79  virtual OSSIZE getValue () const { return mSize; }
80  virtual int setValue (OSSIZE value);
81 
82  virtual ~ASN1CBitStrSizeHolder8() {}
83 
84  private:
86  return *this;
87  }
88 } ;
89 
94  protected:
95  OSUINT16& mSize;
96 
97  public:
98  ASN1CBitStrSizeHolder16 (OSUINT16& value) : mSize(value) {}
99  virtual ASN1CBitStrSizeHolder* clone () {
100  return new ASN1CBitStrSizeHolder16 (mSize);
101  }
102  virtual OSSIZE getValue () const { return mSize; }
103  virtual int setValue (OSSIZE value);
104 
105  virtual ~ASN1CBitStrSizeHolder16() {}
106 
107  private:
109  return *this;
110  }
111 } ;
112 
117  protected:
118  OSUINT32& mSize;
119 
120  public:
121  ASN1CBitStrSizeHolder32 (OSUINT32& value) : mSize(value) {}
122  virtual ASN1CBitStrSizeHolder* clone () {
123  return new ASN1CBitStrSizeHolder32 (mSize);
124  }
125  virtual OSSIZE getValue () const { return mSize; }
126  virtual int setValue (OSSIZE value);
127 
128  virtual ~ASN1CBitStrSizeHolder32() {}
129 
130  private:
132  return *this;
133  }
134 } ;
135 
140  protected:
141  OSSIZE& mSize;
142 
143  public:
144  ASN1CBitStrSizeHolder64 (OSSIZE& value) : mSize(value) {}
145  virtual ASN1CBitStrSizeHolder* clone () {
146  return new ASN1CBitStrSizeHolder64 (mSize);
147  }
148  virtual OSSIZE getValue () const { return mSize; }
149  virtual int setValue (OSSIZE value);
150 
151  virtual ~ASN1CBitStrSizeHolder64() {}
152 
153  private:
155  return *this;
156  }
157 } ;
158 
159 
169 class EXTRTCLASS ASN1CBitStr : public ASN1CType {
170  private:
171  OSSIZE _numbits; /* used when constructor does not provide
172  a value by reference to give us something to
173  refer to */
174  OSOCTET* _units; // placeholder
175 
176  protected:
177  OSOCTET** mpUnits; /* pointer to pointer to data*/
178  OSOCTET** mppExtData; // extended bit string data
179  OSSIZE mMaxNumBits;
180  ASN1CBitStrSizeHolder* mpNumBits;
181  OSSIZE mUnitsUsed; /* number of bytes used in *mpUnits */
182  OSSIZE mUnitsAllocated;
183  OSBOOL mDynAlloc; /* true if *mpUnits is owned by me */
184 
185  private:
186  OSOCTET* allocateMemory (OSSIZE sz);
187  OSOCTET* reallocateMemory
188  (OSOCTET* old, OSSIZE oldBufSz, OSSIZE newBufSz);
189  void freeMemory (OSOCTET* mem);
190 
191  /*
192  * Set the field mUnitsUsed with the logical size in units of the bit
193  * set. WARNING:This function assumes that the number of units actually
194  * in use is less than or equal to the current value of mUnitsUsed!
195  */
196  void recalculateUnitsUsed();
197 
198  /*
199  * Ensures that the bit string can hold enough units.
200  * param unitsRequired the minimum acceptable number of units.
201  */
202  int checkCapacity (OSSIZE unitsRequired);
203 
204  /*
205  * Returns the unit of this bitset at index j as if this bit string had an
206  * infinite amount of storage.
207  */
208  OSOCTET getBits (OSSIZE j);
209 
210  void privateInit (OSSIZE nbits);
211  void privateInit (OSOCTET* bitStr, OSSIZE maxNumbits_, OSOCTET** ppExtData);
212  void privateInit (ASN1TDynBitStr& bitStr);
213  void privateInit (ASN1TDynBitStr64& bitStr);
214 
215  protected:
216  /*
217  * Creates an empty bit string. The string should be additionaly initialized
218  * by call to 'init' method.
219  */
220  EXTRTMETHOD ASN1CBitStr (OSRTMessageBufferIF& msgBuf);
221 
222  /*
223  * Default protected ctor. mpContext should be set and the string should be
224  * additionaly initialized by call to 'init' method.
225  */
226  EXTRTMETHOD ASN1CBitStr();
227 
228  /*
229  * This constructor creates an empty bit string.
230  *
231  * @param ctxt - Reference to reference-counted context object.
232  */
233  EXTRTMETHOD ASN1CBitStr (OSRTContext& ctxt);
234 
235 #if SIZE_MAX > ULONG_MAX //if 64 bit system
236  /*
237  * This constructor initializes the internal bit string to the
238  * given value.
239  *
240  * @param pBits - Pointer to binary bit string data.
241  * @param numbits - Reference to counter to hold number of bits.
242  * @param maxNumbits - Maximum size (in bits) of this bit string.
243  */
244  EXTRTMETHOD ASN1CBitStr
245  (OSOCTET* pBits, OSSIZE& numbits, OSSIZE maxNumbits,
246  OSOCTET** ppExtData = 0);
247 #endif
248 
249  /*
250  * This constructor initializes the internal bit string to the
251  * given value.
252  *
253  * @param pBits - Pointer to binary bit string data.
254  * @param numbits - Reference to counter to hold number of bits.
255  * @param maxNumbits - Maximum size (in bits) of this bit string.
256  */
257  EXTRTMETHOD ASN1CBitStr
258  (OSOCTET* pBits, OSUINT32& numbits, OSSIZE maxNumbits,
259  OSOCTET** ppExtData = 0);
260 
261  EXTRTMETHOD ASN1CBitStr
262  (OSOCTET* pBits, OSUINT8& numbits, OSSIZE maxNumbits,
263  OSOCTET** ppExtData = 0);
264 
265  EXTRTMETHOD ASN1CBitStr
266  (OSOCTET* pBits, OSUINT16& numbits, OSSIZE maxNumbits,
267  OSOCTET** ppExtData = 0);
268 
269  // Fixed-size bit string
270  EXTRTMETHOD ASN1CBitStr (OSOCTET* pBits, OSSIZE maxNumbits);
271 
272  /*
273  * This constructor initializes the internal bit string to the
274  * given value.
275  *
276  * @param bitstr - Reference to a bit string variable.
277  */
278  EXTRTMETHOD ASN1CBitStr (ASN1TDynBitStr& bitStr);
279 
280  /*
281  * Creates bits string from ASN1TDynBitStr64. Ctor does not copy bytes,
282  * just assign array (data member of ASN1TDynBitStr64) to units.
283  * This string will be extendable.
284  */
285  EXTRTMETHOD ASN1CBitStr (ASN1TDynBitStr64& bitStr);
286 
287  void initBase (OSOCTET* pBits, OSSIZE numbits, OSSIZE maxNumbits);
288 
289  EXTRTMETHOD void init
290  (OSOCTET* pBits, OSUINT32& numbits, OSSIZE maxNumbits);
291 
292  EXTRTMETHOD void init
293  (OSOCTET* pBits, OSUINT8& numbits, OSSIZE maxNumbits);
294 
295  EXTRTMETHOD void init
296  (OSOCTET* pBits, OSUINT16& numbits, OSSIZE maxNumbits);
297 
298  EXTRTMETHOD void init (ASN1TDynBitStr& bitStr);
299 
300  public:
310  EXTRTMETHOD ASN1CBitStr (OSRTMessageBufferIF& msgbuf, OSSIZE nbits);
311 
312 #if SIZE_MAX > ULONG_MAX //if 64 bit system
313 
324  EXTRTMETHOD ASN1CBitStr
325  (OSRTMessageBufferIF& msgbuf, OSOCTET* bitStr,
326  OSSIZE& numbits, OSSIZE maxNumbits_, OSOCTET** ppExtData = 0);
327 #endif
328 
340  EXTRTMETHOD ASN1CBitStr
341  (OSRTMessageBufferIF& msgbuf, OSOCTET* bitStr,
342  OSUINT32& numbits, OSSIZE maxNumbits_, OSOCTET** ppExtData = 0);
343 
344  EXTRTMETHOD ASN1CBitStr
345  (OSRTMessageBufferIF& msgbuf, OSOCTET* bitStr,
346  OSUINT8& numbits, OSSIZE maxNumbits_, OSOCTET** ppExtData = 0);
347 
348  EXTRTMETHOD ASN1CBitStr
349  (OSRTMessageBufferIF& msgbuf, OSOCTET* bitStr,
350  OSUINT16& numbits, OSSIZE maxNumbits_, OSOCTET** ppExtData = 0);
351 
352  EXTRTMETHOD ASN1CBitStr
353  (OSRTMessageBufferIF& msgbuf, OSOCTET* bitStr, OSSIZE maxNumbits_);
354 
355  /*
356  * Creates bits string from ASN1TDynBitStr. Ctor does not copy bytes,
357  * just assign array (data member of ASN1TDynBitStr) to units.
358  * This string will be extendable.
359  */
360  EXTRTMETHOD ASN1CBitStr
361  (OSRTMessageBufferIF& msgBuf, ASN1TDynBitStr& bitStr);
362 
363  /*
364  * Creates bits string from ASN1TDynBitStr64. Ctor does not copy bytes,
365  * just assign array (data member of ASN1TDynBitStr64) to units.
366  * This string will be extendable.
367  */
368  EXTRTMETHOD ASN1CBitStr
369  (OSRTMessageBufferIF& msgBuf, ASN1TDynBitStr64& bitStr);
370 
371  /*
372  * Creates empty bit string. If nbits == 0 then bit string is dynamic;
373  * else capacity will be fixed to nbits.
374  */
375  EXTRTMETHOD ASN1CBitStr (OSRTContext& ctxt, OSSIZE nbits);
376 
377  /*
378  * Creates bits string from array of bits. Ctor does not copy bytes,
379  * just assign passed array to units.
380  * Param bitStr - pointer to static byte array
381  * Param numbits - reference to length of bit string (in bits);
382  * Param maxNumbits_ - sets maximum length in bits
383  */
384  EXTRTMETHOD ASN1CBitStr
385  (OSRTContext& ctxt, OSOCTET* bitStr,
386  OSUINT32& octsNumbits, OSSIZE maxNumbits_, OSOCTET** ppExtData = 0);
387 
388  EXTRTMETHOD ASN1CBitStr
389  (OSRTContext& ctxt, OSOCTET* bitStr,
390  OSUINT8& octsNumbits, OSSIZE maxNumbits_, OSOCTET** ppExtData = 0);
391 
392  EXTRTMETHOD ASN1CBitStr
393  (OSRTContext& ctxt, OSOCTET* bitStr,
394  OSUINT16& octsNumbits, OSSIZE maxNumbits_, OSOCTET** ppExtData = 0);
395 
396  EXTRTMETHOD ASN1CBitStr
397  (OSRTContext& ctxt, OSOCTET* bitStr, OSSIZE maxNumbits_);
398 
399  /*
400  * Creates bits string from ASN1TDynBitStr. Ctor does not copy bytes,
401  * just assign array (data member of ASN1TDynBitStr) to units.
402  * This string will be extendable.
403  */
404  EXTRTMETHOD ASN1CBitStr (OSRTContext& ctxt, ASN1TDynBitStr& bitStr);
405 
406  /*
407  * Copy constructor.
408  */
409  EXTRTMETHOD ASN1CBitStr (const ASN1CBitStr& bitStr);
410 
411  /*
412  * Copy constructor, but allows to specify 'extendable' parameter
413  */
414  EXTRTMETHOD ASN1CBitStr (const ASN1CBitStr& bitStr, OSBOOL extendable);
415 
416 
417  EXTRTMETHOD ~ASN1CBitStr();
418 
430  /*
431  * Sets the bit at the specified index to 1.
432  *
433  * Returns: 0 - if succeed, or
434  * RTERR_OUTOFBND - if 'bitIndex' is out of bounds, or
435  * other error codes (see asn1type.h).
436  */
437  EXTRTMETHOD int set (OSSIZE bitIndex);
438 
455  /*
456  * Sets the bits from the specified fromIndex(inclusive) to the
457  * specified toIndex(exclusive) to 1.
458  *
459  * Returns: 0 - if succeed, or
460  * RTERR_OUTOFBND - if one of indexes is out of bounds, or
461  * RTERR_RANGERR - if fromIndex > toIndex, or
462  * other error codes (see asn1type.h).
463  */
464  EXTRTMETHOD int set (OSSIZE fromIndex, OSSIZE toIndex);
465 
478  /*Returns (details): RTERR_OUTOFBND - if 'bitIndex' is out of bounds, or
479  * other error codes (see asn1type.h).
480  */
481  inline int change (OSSIZE bitIndex, OSBOOL value) {
482  return (value) ? set (bitIndex) : clear (bitIndex);
483  }
484 
497  /*
498  * Sets the bit specified by the index to 0.
499  *
500  * Returns: 0 - if succeed, or
501  * RTERR_OUTOFBND - if 'bitIndex' is out of bounds, or
502  * other error codes (see asn1type.h).
503  */
504  EXTRTMETHOD int clear (OSSIZE bitIndex);
505 
522  /*
523  * Sets the bits from the specified fromIndex(inclusive) to the
524  * specified toIndex(exclusive) to 0.
525  *
526  * Returns: 0 - if succeed, or
527  * RTERR_OUTOFBND - if one of indexes is out of bounds, or
528  * RTERR_RANGERR - if fromIndex > toIndex, or
529  * other error codes (see asn1type.h).
530  */
531  EXTRTMETHOD int clear (OSSIZE fromIndex, OSSIZE toIndex);
532 
541  /*
542  * Sets all of the bits in this bit string to 0.
543  */
544  EXTRTMETHOD void clear();
545 
561  /*
562  * Sets the bit at the specified index to to the complement of its
563  * current value.
564  *
565  * Returns: 0 - if succeed, or
566  * RTERR_OUTOFBND - if 'bitIndex' is out of bounds, or
567  * other error codes (see asn1type.h).
568  */
569  EXTRTMETHOD int invert (OSSIZE bitIndex);
570 
590  /*
591  * Sets each bit from the specified fromIndex(inclusive) to the
592  * specified toIndex(exclusive) to the complement of its current
593  * value.
594  *
595  * Returns: 0 - if succeed, or
596  * RTERR_OUTOFBND - if one of indexes is out of bounds, or
597  * RTERR_RANGERR - if fromIndex > toIndex, or
598  * other error codes (see asn1type.h).
599  */
600  EXTRTMETHOD int invert(OSSIZE fromIndex, OSSIZE toIndex);
601 
613  /*
614  * Returns the value of the bit with the specified index.
615  */
616  EXTRTMETHOD OSBOOL get(OSSIZE bitIndex);
617 
623  /*
624  * Returns TRUE if the value of the bit with the specified index is set to 1.
625  */
626  inline OSBOOL isSet(OSSIZE bitIndex) { return get(bitIndex); }
627 
636  /*
637  * Returns true if this bit string contains no bits that are set
638  * to 1.
639  */
640  inline OSBOOL isEmpty() { return (mUnitsUsed == 0); }
641 
650  /*
651  * Returns the number of bytes of space actually in use by this
652  * bit string to represent bit values.
653  */
654  EXTRTMETHOD OSSIZE size() const;
655 
667  EXTRTMETHOD OSSIZE length() const;
668 
678  /*
679  * Returns the number of bits set to 1 in this
680  * bit string.
681  */
682  EXTRTMETHOD OSSIZE cardinality() const;
683 
697  /*
698  * Copies bit string to buffer (pBuf).
699  */
700  EXTRTMETHOD int getBytes (OSOCTET* pBuf, OSSIZE bufSz);
701 
727  EXTRTMETHOD int get
728  (OSSIZE fromIndex, OSSIZE toIndex, OSOCTET* pBuf, OSSIZE bufSz);
729 
747  EXTRTMETHOD int doAnd (const OSOCTET* pOctstr, OSSIZE octsNumbits);
748 
760  inline int doAnd (const ASN1TDynBitStr& bitStr) {
761  return doAnd(bitStr.data, bitStr.numbits);
762  }
763 
774  inline int doAnd(const ASN1CBitStr& bitStr) {
775  return doAnd(*bitStr.mpUnits, bitStr.length());
776  }
777 
778 
795  EXTRTMETHOD int doOr(const OSOCTET* pOctstr, OSSIZE octsNumbits);
796 
808  inline int doOr(const ASN1TDynBitStr& bitStr) {
809  return doOr(bitStr.data, bitStr.numbits);
810  }
811 
822  inline int doOr(const ASN1CBitStr& bitStr) {
823  return doOr(*bitStr.mpUnits, bitStr.length());
824  }
825 
842  EXTRTMETHOD int doXor(const OSOCTET* pOctstr, OSSIZE octsNumbits);
843 
855  inline int doXor(const ASN1TDynBitStr& bitStr) {
856  return doXor(bitStr.data, bitStr.numbits);
857  }
858 
869  inline int doXor(const ASN1CBitStr& bitStr) {
870  return doXor(*bitStr.mpUnits, bitStr.length());
871  }
872 
887  /*
888  * Clears all of the bits in this bit string whose corresponding
889  * bit is set in the specified bit string.
890  *
891  * Returns: 0 - if succeed, or
892  * ASN_E_INVLEN - if 'octsNumbits' is negative, or
893  * RTERR_INVPARAM - if pOctstr is null, or
894  * other error codes (see asn1type.h).
895  */
896  EXTRTMETHOD int doAndNot(const OSOCTET* pOctstr, OSSIZE octsNumbits);
897 
912  inline int doAndNot(const ASN1TDynBitStr& bitStr) {
913  return doAndNot(bitStr.data, bitStr.numbits);
914  }
915 
929  inline int doAndNot(const ASN1CBitStr& bitStr) {
930  return doAndNot(*bitStr.mpUnits, bitStr.length());
931  }
932 
946  /*
947  * Shifts all bits to the left by 'shift' bits.
948  */
949  EXTRTMETHOD int shiftLeft(OSSIZE shift);
950 
964  /*
965  * Shifts all bits to the right by 'shift' bits.
966  */
967  EXTRTMETHOD int shiftRight(OSSIZE shift);
968 
975  /*
976  * Returns number of unused bits in last unit.
977  */
978  EXTRTMETHOD OSUINT32 unusedBitsInLastUnit();
979 
994  EXTRTMETHOD operator ASN1TDynBitStr();
995 
1009  EXTRTMETHOD operator ASN1TDynBitStr*();
1010 } ;
1011 #else
1012 typedef class _ASN1CBitStr : public ASN1CType {
1013  public:
1014  _ASN1CBitStr (OSRTMessageBufferIF& msgBuf, OSUINT32 nbits) :
1015  ASN1CType (msgBuf) {}
1016 
1017  _ASN1CBitStr (OSRTMessageBufferIF& msgBuf, OSOCTET* bitStr,
1018  OSUINT32& octsNumbits, OSUINT32 maxNumbits_) :
1019  ASN1CType (msgBuf) {}
1020 
1021  _ASN1CBitStr (OSRTMessageBufferIF& msgBuf, ASN1TDynBitStr& bitStr) :
1022  ASN1CType (msgBuf) {}
1023 } ASN1CBitStr;
1024 #endif // _NO_UTILS_CLASSES
1025 #endif // _ASN1CBITSTR_H_
1026 
Definition: asn1CppTypes.h:656
int doXor(const ASN1CBitStr &bitStr)
Definition: ASN1CBitStr.h:869
int doOr(const ASN1TDynBitStr &bitStr)
Definition: ASN1CBitStr.h:808
int doAndNot(const ASN1CBitStr &bitStr)
Definition: ASN1CBitStr.h:929
int doAnd(const ASN1CBitStr &bitStr)
Definition: ASN1CBitStr.h:774
OSBOOL isSet(OSSIZE bitIndex)
Definition: ASN1CBitStr.h:626
EXTRTMETHOD OSSIZE length() const
Definition: asn1CppTypes.h:313
int doXor(const ASN1TDynBitStr &bitStr)
Definition: ASN1CBitStr.h:855
Definition: ASN1CBitStr.h:51
int doOr(const ASN1CBitStr &bitStr)
Definition: ASN1CBitStr.h:822
Definition: asn1CppTypes.h:623
Definition: ASN1CBitStr.h:139
Definition: ASN1CBitStr.h:70
Definition: ASN1CBitStr.h:116
int change(OSSIZE bitIndex, OSBOOL value)
Definition: ASN1CBitStr.h:481
Definition: OSRTContext.h:65
virtual int setValue(OSSIZE value)=0
Definition: ASN1CBitStr.h:169
int doAnd(const ASN1TDynBitStr &bitStr)
Definition: ASN1CBitStr.h:760
int doAndNot(const ASN1TDynBitStr &bitStr)
Definition: ASN1CBitStr.h:912
Definition: ASN1CBitStr.h:93
OSBOOL isEmpty()
Definition: ASN1CBitStr.h:640