@@ -4,117 +4,101 @@ BIT STRING
44==========
55
66The ASN.1 ``BIT STRING `` type represents an arbitrary-length sequence of bits.
7- It is used to model flags, bitmasks, and other bit-level data. These types store
8- their bit data internally as a little-endian encoded byte array.
7+ It is used to model flags, bitmasks, and other bit-level data. In the generated
8+ Python bindings, **all ** ``BIT STRING `` types implement type behavior described
9+ in :class: `_Asn1BitStrType `.
910
10- Named BIT STRING types are special variants where each bit is assigned a
11- meaningful name, represented as members of an ``enum.IntFlag `` class called
12- ``VALUES `` in the generated Python class.
11+ Named ``BIT STRING `` types additionally expose named boolean attributes for
12+ convenient flag access.
1313
1414Example ASN.1 definitions:
1515
1616.. code-block :: asn1
1717
18- MyBitString ::= BIT STRING
18+ ExampleBitString ::= BIT STRING
1919
20- MyFlags ::= BIT STRING {
21- flag1 (0),
22- flag2 (1),
23- flag3 (2)
20+ ExampleNamedBitString ::= BIT STRING {
21+ zero (0),
22+ one (1),
23+ two (2)
2424 }
2525
2626 These generate Python classes similar to:
2727
2828.. code-block :: python
2929
30- class MyBitString (_Asn1BasicType[bitarray.bitarray] ):
30+ class ExampleBitString ( _Asn1BitStrType ):
3131 pass
3232
33- class MyFlags (_BasicAsn1FlagType ):
34- class VALUES (enum .IntFlag ):
35- V_flag1 = 0
36- V_flag2 = 1
37- V_flag3 = 2
33+ class ExampleNamedBitString (_Asn1BitStrType ):
34+ V_zero: bool # bit 0
35+ V_one: bool # bit 1
36+ V_two: bool # bit 2
3837
3938 Conceptual Representation
4039--------------------------
4140
42- .. py :class :: _Asn1BasicType[bitarray]
43- :no-index:
41+ .. py :class :: _Asn1BitStrType
4442
45- Represents a basic ASN.1 BIT STRING.
43+ Base class for all ASN.1 `` BIT STRING `` types .
4644
47- .. py :method :: __init__ (self , value: bytes | bitarray.bitarray | None = None ) -> None
48- :no-index:
45+ .. py :method :: __init__ (self , size: int = ... ) -> None
4946
50- Initializes the BIT STRING instance with optional initial bits .
47+ Initializes a `` BIT STRING `` with the given number of ** bytes ** .
5148
52- The value can be:
49+ .. py :property :: value
50+ :type: bitarray.bitarray | bytes
5351
54- - a bytes object representing the raw bits,
55- - a ``bitarray.bitarray `` instance,
56- - or ``None `` to initialize an empty BIT STRING.
52+ Gets or sets the raw value of the ``BIT STRING ``.
5753
58- .. py :property :: value
59- :type: bitarray.bitarray
60- :no-index:
54+ - Assignment accepts either a :class: `bytes ` object or a
55+ :class: `bitarray.bitarray `.
56+ - The returned object can be modified directly, but must be reassigned
57+ to take effect.
6158
62- Gets or sets the bit string value in ** little-endian ** form.
59+ .. py : method :: clear() -> None
6360
64- When setting, you can assign:
61+ Clears all bits (sets them to `` 0 ``).
6562
66- - a bytes object, which will be converted into a bit array,
67- - a ``bitarray.bitarray `` object directly.
63+ .. py :method :: set (bit: int , flag: bool ) -> None
6864
69- .. py :property :: value_BE
70- :type: bitarray.bitarray
71- :no-index:
65+ Sets the given ``bit `` position to ``True `` or ``False ``.
7266
73- Gets or sets the bit string value in ** big-endian ** form.
67+ .. py : method :: get( bit: int ) -> bool
7468
75- Supported for ** all top-level and named BIT STRING types ** .
69+ Returns the boolean state of the given `` bit `` position .
7670
77- Named BIT STRING types will be represented by :class: ` _Asn1FlagType `.
71+ .. py : method :: size() -> int
7872
79- Usage Notes
80- -----------
73+ Returns the current number of **bytes ** in the ``BIT STRING ``.
8174
82- - Bit order within the underlying bit array is **little-endian ** aligned by
83- default, meaning bit 0 corresponds to the least significant bit of the
84- first byte.
75+ .. py :method :: resize(size: int ) -> None
8576
86- - Named bit strings allow intuitive usage of individual flags:
77+ Adjusts the number of ** bits ** in the `` BIT STRING ``.
8778
88- .. code-block :: python
8979
90- flags = MyFlags()
91- # Keep in mind that in-place operations are not possible
92- flags_value = flags.value
93- flags_value[MyFlags.V_flag1] = 1
9480
95- flags.value = flags_value
96- if flags.value[MyFlags.V_flag2]:
97- print (" Flag 2 is set" )
81+ Usage Notes
82+ -----------
9883
99- - The `` value `` property allows seamless conversion between raw bits
100- and named flags .
84+ - Unlike integer masks, bits can be manipulated directly using
85+ :func: ` _Asn1BitStrType.set ` and :func: ` _Asn1BitStrType.get ` .
10186
102- - Direct modifications to the ``bitarray `` returned by ``value `` or ``value_BE `` do not
103- affect the stored value unless reassigned.
87+ .. code-block :: python
10488
105- .. note :: Endian conversion rules:
89+ bs = ExampleBitString(size = 8 )
90+ bs.set(3 , True ) # set bit 3
91+ print (bs.get(3 )) # True
10692
107- - **Top-level and named BIT STRING types ** support both :code: `.value `
108- (little-endian) and :code: `.value_BE ` (big-endian).
109- - **Embedded unnamed BIT STRINGs ** (e.g., inline fields inside ``SEQUENCE ``
110- or ``SET `` without a type alias) only support little-endian access via
111- :code: `.value `.
93+ - Named ``BIT STRING `` types provide boolean attributes for each flag:
11294
113- .. code-block :: asn1
95+ .. code-block :: python
11496
115- MyFlags ::= BIT STRING { read(0), write(1) }
97+ flags = ExampleNamedBitString()
98+ flags.V_one = True
99+ if flags.V_one:
100+ print (" Flag 'one' is set" )
116101
117- MySeq ::= SEQUENCE {
118- f1 BIT STRING, -- only little-endian
119- f2 MyFlags -- supports little-endian and big-endian
120- }
102+ - For very small ``BIT STRING `` types (≤ 1 byte):
103+ If they are encoded using two bytes of space, the queried bit will always be
104+ positioned on the **last octet internally **.
0 commit comments