Skip to content

Commit 8618846

Browse files
committed
deploy: 07d2476
1 parent edd0b05 commit 8618846

14 files changed

Lines changed: 189 additions & 395 deletions

_sources/reference/basic/bit_string.rst.txt

Lines changed: 53 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -4,117 +4,101 @@ BIT STRING
44
==========
55

66
The 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

1414
Example 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**.

_sources/reference/basic/boolean.rst.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.. _reference_basic_boolean:
1+
.. _reference_basic_bool:
22

33
BOOLEAN
44
=======

_sources/reference/basic/time.rst.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.. _reference_basic_time_types:
1+
.. _reference_basic_time:
22

33
TIME
44
====

_sources/reference/object_model.rst.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ value is converted **on-demand** to the corresponding Python object, for example
1717
- ASN.1 ``INTEGER`` converts to a Python ``int``
1818
- ASN.1 ``BOOLEAN`` converts to a Python ``bool``
1919
- ASN.1 ``OCTET STRING`` converts to Python ``bytes``
20-
- ASN.1 named ``BIT STRING`` converts to an ``enum.IntFlag`` or
21-
:class:`bitarray.bitarray`, depending on the type
20+
- ASN.1 ``BIT STRING`` always generates a new class optionally with named members
2221

2322
This conversion produces a **new** Python object representing the current state.
2423

_sources/reference/overview.rst.txt

Lines changed: 0 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -433,114 +433,6 @@ All generated Python ENUMERATED classes conform to the following conceptual API:
433433
If ``None`` is provided, the instance is created in an *unset* state.
434434

435435

436-
ASN.1 Conceptual Named BIT STRING Type
437-
--------------------------------------
438-
439-
The ASN.1 ``BIT STRING`` type can be defined with named bit positions, often
440-
used to represent a set of boolean flags. In the generated Python bindings,
441-
these named ``BIT STRING`` types are represented by classes that:
442-
443-
- Contain an embedded ``VALUES`` enumeration (a subclass of :class:`enum.IntEnum`).
444-
- Store their current value internally as a :py:mod:`bitarray.bitarray`.
445-
- Use **little-endian alignment by default**, while exposing a ``value_BE`` property
446-
to query or assign the *big-endian* aligned variant.
447-
448-
.. note::
449-
Both ``value`` and ``value_BE`` return the raw :class:`bitarray.bitarray`
450-
instance rather than an integer mask or enumeration.
451-
452-
.. warning::
453-
Big-endian representation is only available via ``value_BE``. The internal
454-
storage is always little-endian. *Big-endian* representation is **not** supported
455-
for anonymous inner named ``BIT STRING`` types.
456-
457-
**Example:**
458-
459-
.. code-block:: asn1
460-
461-
MyFlags ::= BIT STRING {
462-
read(0),
463-
write(1),
464-
execute(2)
465-
}
466-
467-
will generate a Python class:
468-
469-
.. code-block:: python
470-
471-
class MyFlags(_BasicAsn1FlagType):
472-
class VALUES(enum.IntFlag):
473-
V_read = 0
474-
V_write = 1
475-
V_execute = 2
476-
477-
Because the generated ``VALUES`` enumeration now stores the **bit position**
478-
directly (rather than the full mask), checking whether a flag is set is done
479-
by indexing into the underlying ``bitarray``:
480-
481-
.. code-block:: python
482-
483-
obj = MyFlags()
484-
485-
# Check if the 'read' flag is set
486-
bool(obj.value[MyFlags.VALUES.V_read]) # True or False
487-
488-
489-
All generated Python named BIT STRING classes conform to the following conceptual API:
490-
491-
.. py:class:: _Asn1FlagType
492-
493-
*Inherits from* :class:`_Asn1Type`.
494-
495-
Conceptual base class for all ASN.1 named ``BIT STRING`` types.
496-
497-
.. py:class:: VALUES(enum.IntFlag)
498-
499-
:text-req:`Required.`
500-
501-
Inner enumeration containing **all** named flag values defined in the
502-
ASN.1 type. Each member is prefixed with ``V_`` to avoid name clashes.
503-
504-
Each flag's value is the **bit index** in the underlying ``bitarray``.
505-
506-
.. py:property:: value
507-
:type: bitarray.bitarray
508-
509-
:text-req:`Required.`
510-
511-
Holds the current flag state as a **little-endian** :class:`bitarray.bitarray`.
512-
513-
This property accepts assignment using:
514-
515-
- A :class:`bitarray.bitarray` object (must be little-endian aligned)
516-
- A :class:`bytes` object containing the encoded ``BIT STRING``
517-
518-
Accessing individual flags is done by indexing with a ``VALUES`` member.
519-
520-
.. warning::
521-
Assigning a value containing bits that are not defined in
522-
``VALUES`` will not raise an error, but those bits will be preserved
523-
and treated as *unnamed* flags.
524-
525-
.. py:property:: value_BE
526-
:type: bitarray.bitarray
527-
528-
:text-req:`Required.`
529-
530-
Holds the current flag state as a **big-endian** :class:`bitarray.bitarray`.
531-
532-
Similar to ``value``, but the bit ordering is reversed. Assigning or
533-
querying this property transparently handles the endian conversion.
534-
535-
536-
.. py:method:: __init__(self, value: _BasicAsn1FlagType.VALUES | int | bytes | bitarray.bitarray | None = None) -> None
537-
538-
:text-req:`Required.`
539-
540-
Initializes a new BIT STRING instance with the given value.
541-
If ``None`` is provided, the instance is created in an *unset* state.
542-
543-
544436
ASN.1 Conceptual CHOICE Type
545437
----------------------------
546438

0 commit comments

Comments
 (0)