ASN1C C/C++ Common Runtime  ASN1C v7.6.x
ASN1CBitStr.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 1997-2022 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 > OSUINT32_MAX //if size_t > 32 bits
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 > OSUINT32_MAX //if size_t > 32 bits
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 #if SIZE_MAX > OSUINT32_MAX //if size_t > 32 bits
378  /*
379  * Creates bits string from array of bits. Ctor does not copy bytes,
380  * just assign passed array to units.
381  * Param bitStr - pointer to static byte array
382  * Param numbits - reference to length of bit string (in bits);
383  * Param maxNumbits_ - sets maximum length in bits
384  */
385  EXTRTMETHOD ASN1CBitStr
386  (OSRTContext& ctxt, OSOCTET* bitStr,
387  OSSIZE& octsNumbits, OSSIZE maxNumbits_, OSOCTET** ppExtData = 0);
388 #endif
389 
390  EXTRTMETHOD ASN1CBitStr
391  (OSRTContext& ctxt, OSOCTET* bitStr,
392  OSUINT32& octsNumbits, OSSIZE maxNumbits_, OSOCTET** ppExtData = 0);
393 
394  EXTRTMETHOD ASN1CBitStr
395  (OSRTContext& ctxt, OSOCTET* bitStr,
396  OSUINT8& octsNumbits, OSSIZE maxNumbits_, OSOCTET** ppExtData = 0);
397 
398  EXTRTMETHOD ASN1CBitStr
399  (OSRTContext& ctxt, OSOCTET* bitStr,
400  OSUINT16& octsNumbits, OSSIZE maxNumbits_, OSOCTET** ppExtData = 0);
401 
402  EXTRTMETHOD ASN1CBitStr
403  (OSRTContext& ctxt, OSOCTET* bitStr, OSSIZE maxNumbits_);
404 
405  /*
406  * Creates bits string from ASN1TDynBitStr. Ctor does not copy bytes,
407  * just assign array (data member of ASN1TDynBitStr) to units.
408  * This string will be extendable.
409  */
410  EXTRTMETHOD ASN1CBitStr (OSRTContext& ctxt, ASN1TDynBitStr& bitStr);
411 
412  /*
413  * Creates bits string from ASN1TDynBitStr64. Ctor does not copy bytes,
414  * just assign array (data member of ASN1TDynBitStr64) to units.
415  * This string will be extendable.
416  */
417  EXTRTMETHOD ASN1CBitStr(OSRTContext& ctxt, ASN1TDynBitStr64& bitStr);
418 
419  /*
420  * Copy constructor.
421  */
422  EXTRTMETHOD ASN1CBitStr (const ASN1CBitStr& bitStr);
423 
424  /*
425  * Copy constructor, but allows to specify 'extendable' parameter
426  */
427  EXTRTMETHOD ASN1CBitStr (const ASN1CBitStr& bitStr, OSBOOL extendable);
428 
429 
430  EXTRTMETHOD ~ASN1CBitStr();
431 
443  /*
444  * Sets the bit at the specified index to 1.
445  *
446  * Returns: 0 - if succeed, or
447  * RTERR_OUTOFBND - if 'bitIndex' is out of bounds, or
448  * other error codes (see asn1type.h).
449  */
450  EXTRTMETHOD int set (OSSIZE bitIndex);
451 
468  /*
469  * Sets the bits from the specified fromIndex(inclusive) to the
470  * specified toIndex(exclusive) to 1.
471  *
472  * Returns: 0 - if succeed, or
473  * RTERR_OUTOFBND - if one of indexes is out of bounds, or
474  * RTERR_RANGERR - if fromIndex > toIndex, or
475  * other error codes (see asn1type.h).
476  */
477  EXTRTMETHOD int set (OSSIZE fromIndex, OSSIZE toIndex);
478 
491  /*Returns (details): RTERR_OUTOFBND - if 'bitIndex' is out of bounds, or
492  * other error codes (see asn1type.h).
493  */
494  inline int change (OSSIZE bitIndex, OSBOOL value) {
495  return (value) ? set (bitIndex) : clear (bitIndex);
496  }
497 
510  /*
511  * Sets the bit specified by the index to 0.
512  *
513  * Returns: 0 - if succeed, or
514  * RTERR_OUTOFBND - if 'bitIndex' is out of bounds, or
515  * other error codes (see asn1type.h).
516  */
517  EXTRTMETHOD int clear (OSSIZE bitIndex);
518 
535  /*
536  * Sets the bits from the specified fromIndex(inclusive) to the
537  * specified toIndex(exclusive) to 0.
538  *
539  * Returns: 0 - if succeed, or
540  * RTERR_OUTOFBND - if one of indexes is out of bounds, or
541  * RTERR_RANGERR - if fromIndex > toIndex, or
542  * other error codes (see asn1type.h).
543  */
544  EXTRTMETHOD int clear (OSSIZE fromIndex, OSSIZE toIndex);
545 
554  /*
555  * Sets all of the bits in this bit string to 0.
556  */
557  EXTRTMETHOD void clear();
558 
574  /*
575  * Sets the bit at the specified index to to the complement of its
576  * current value.
577  *
578  * Returns: 0 - if succeed, or
579  * RTERR_OUTOFBND - if 'bitIndex' is out of bounds, or
580  * other error codes (see asn1type.h).
581  */
582  EXTRTMETHOD int invert (OSSIZE bitIndex);
583 
603  /*
604  * Sets each bit from the specified fromIndex(inclusive) to the
605  * specified toIndex(exclusive) to the complement of its current
606  * value.
607  *
608  * Returns: 0 - if succeed, or
609  * RTERR_OUTOFBND - if one of indexes is out of bounds, or
610  * RTERR_RANGERR - if fromIndex > toIndex, or
611  * other error codes (see asn1type.h).
612  */
613  EXTRTMETHOD int invert(OSSIZE fromIndex, OSSIZE toIndex);
614 
626  /*
627  * Returns the value of the bit with the specified index.
628  */
629  EXTRTMETHOD OSBOOL get(OSSIZE bitIndex);
630 
636  /*
637  * Returns TRUE if the value of the bit with the specified index is set to 1.
638  */
639  inline OSBOOL isSet(OSSIZE bitIndex) { return get(bitIndex); }
640 
649  /*
650  * Returns true if this bit string contains no bits that are set
651  * to 1.
652  */
653  inline OSBOOL isEmpty() { return (mUnitsUsed == 0); }
654 
663  /*
664  * Returns the number of bytes of space actually in use by this
665  * bit string to represent bit values.
666  */
667  EXTRTMETHOD OSSIZE size() const;
668 
680  EXTRTMETHOD OSSIZE length() const;
681 
691  /*
692  * Returns the number of bits set to 1 in this
693  * bit string.
694  */
695  EXTRTMETHOD OSSIZE cardinality() const;
696 
710  /*
711  * Copies bit string to buffer (pBuf).
712  */
713  EXTRTMETHOD int getBytes (OSOCTET* pBuf, OSSIZE bufSz);
714 
740  EXTRTMETHOD int get
741  (OSSIZE fromIndex, OSSIZE toIndex, OSOCTET* pBuf, OSSIZE bufSz);
742 
760  EXTRTMETHOD int doAnd (const OSOCTET* pOctstr, OSSIZE octsNumbits);
761 
773  inline int doAnd (const ASN1TDynBitStr& bitStr) {
774  return doAnd(bitStr.data, bitStr.numbits);
775  }
776 
787  inline int doAnd(const ASN1CBitStr& bitStr) {
788  return doAnd(*bitStr.mpUnits, bitStr.length());
789  }
790 
791 
808  EXTRTMETHOD int doOr(const OSOCTET* pOctstr, OSSIZE octsNumbits);
809 
821  inline int doOr(const ASN1TDynBitStr& bitStr) {
822  return doOr(bitStr.data, bitStr.numbits);
823  }
824 
835  inline int doOr(const ASN1CBitStr& bitStr) {
836  return doOr(*bitStr.mpUnits, bitStr.length());
837  }
838 
855  EXTRTMETHOD int doXor(const OSOCTET* pOctstr, OSSIZE octsNumbits);
856 
868  inline int doXor(const ASN1TDynBitStr& bitStr) {
869  return doXor(bitStr.data, bitStr.numbits);
870  }
871 
882  inline int doXor(const ASN1CBitStr& bitStr) {
883  return doXor(*bitStr.mpUnits, bitStr.length());
884  }
885 
900  /*
901  * Clears all of the bits in this bit string whose corresponding
902  * bit is set in the specified bit string.
903  *
904  * Returns: 0 - if succeed, or
905  * ASN_E_INVLEN - if 'octsNumbits' is negative, or
906  * RTERR_INVPARAM - if pOctstr is null, or
907  * other error codes (see asn1type.h).
908  */
909  EXTRTMETHOD int doAndNot(const OSOCTET* pOctstr, OSSIZE octsNumbits);
910 
925  inline int doAndNot(const ASN1TDynBitStr& bitStr) {
926  return doAndNot(bitStr.data, bitStr.numbits);
927  }
928 
942  inline int doAndNot(const ASN1CBitStr& bitStr) {
943  return doAndNot(*bitStr.mpUnits, bitStr.length());
944  }
945 
959  /*
960  * Shifts all bits to the left by 'shift' bits.
961  */
962  EXTRTMETHOD int shiftLeft(OSSIZE shift);
963 
977  /*
978  * Shifts all bits to the right by 'shift' bits.
979  */
980  EXTRTMETHOD int shiftRight(OSSIZE shift);
981 
988  /*
989  * Returns number of unused bits in last unit.
990  */
991  EXTRTMETHOD OSUINT32 unusedBitsInLastUnit();
992 
1007  EXTRTMETHOD operator ASN1TDynBitStr();
1008 
1022  EXTRTMETHOD operator ASN1TDynBitStr*();
1023 } ;
1024 #else
1025 typedef class _ASN1CBitStr : public ASN1CType {
1026  public:
1027  _ASN1CBitStr (OSRTMessageBufferIF& msgBuf, OSUINT32 nbits) :
1028  ASN1CType (msgBuf) {}
1029 
1030  _ASN1CBitStr (OSRTMessageBufferIF& msgBuf, OSOCTET* bitStr,
1031  OSUINT32& octsNumbits, OSUINT32 maxNumbits_) :
1032  ASN1CType (msgBuf) {}
1033 
1034  _ASN1CBitStr (OSRTMessageBufferIF& msgBuf, ASN1TDynBitStr& bitStr) :
1035  ASN1CType (msgBuf) {}
1036 } ASN1CBitStr;
1037 #endif // _NO_UTILS_CLASSES
1038 #endif // _ASN1CBITSTR_H_
1039 
Definition: asn1CppTypes.h:674
int doXor(const ASN1CBitStr &bitStr)
Definition: ASN1CBitStr.h:882
int doOr(const ASN1TDynBitStr &bitStr)
Definition: ASN1CBitStr.h:821
int doAndNot(const ASN1CBitStr &bitStr)
Definition: ASN1CBitStr.h:942
int doAnd(const ASN1CBitStr &bitStr)
Definition: ASN1CBitStr.h:787
OSBOOL isSet(OSSIZE bitIndex)
Definition: ASN1CBitStr.h:639
EXTRTMETHOD OSSIZE length() const
Definition: asn1CppTypes.h:321
int doXor(const ASN1TDynBitStr &bitStr)
Definition: ASN1CBitStr.h:868
Definition: ASN1CBitStr.h:51
int doOr(const ASN1CBitStr &bitStr)
Definition: ASN1CBitStr.h:835
Definition: asn1CppTypes.h:641
Definition: ASN1CBitStr.h:139
Definition: ASN1CBitStr.h:70
Definition: ASN1CBitStr.h:116
int change(OSSIZE bitIndex, OSBOOL value)
Definition: ASN1CBitStr.h:494
Definition: OSRTContext.h:64
virtual int setValue(OSSIZE value)=0
Definition: ASN1CBitStr.h:169
int doAnd(const ASN1TDynBitStr &bitStr)
Definition: ASN1CBitStr.h:773
int doAndNot(const ASN1TDynBitStr &bitStr)
Definition: ASN1CBitStr.h:925
Definition: ASN1CBitStr.h:93
OSBOOL isEmpty()
Definition: ASN1CBitStr.h:653