1from __future__ import annotations
2
3import bitstring
4from bitstring.bitstream import BitStream
5from bitstring.utils import tokenparser
6from bitstring.exceptions import CreationError
7from typing import Union, List
8from bitstring.bitstore import BitStore
9from bitstring.bitstore_helpers import bitstore_from_token
10
11
12def pack(fmt: Union[str, List[str]], *values, **kwargs) -> BitStream:
13 """Pack the values according to the format string and return a new BitStream.
14
15 fmt -- A single string or a list of strings with comma separated tokens
16 describing how to create the BitStream.
17 values -- Zero or more values to pack according to the format.
18 kwargs -- A dictionary or keyword-value pairs - the keywords used in the
19 format string will be replaced with their given value.
20
21 Token examples: 'int:12' : 12 bits as a signed integer
22 'uint:8' : 8 bits as an unsigned integer
23 'float:64' : 8 bytes as a big-endian float
24 'intbe:16' : 2 bytes as a big-endian signed integer
25 'uintbe:16' : 2 bytes as a big-endian unsigned integer
26 'intle:32' : 4 bytes as a little-endian signed integer
27 'uintle:32' : 4 bytes as a little-endian unsigned integer
28 'floatle:64': 8 bytes as a little-endian float
29 'intne:24' : 3 bytes as a native-endian signed integer
30 'uintne:24' : 3 bytes as a native-endian unsigned integer
31 'floatne:32': 4 bytes as a native-endian float
32 'hex:80' : 80 bits as a hex string
33 'oct:9' : 9 bits as an octal string
34 'bin:1' : single bit binary string
35 'ue' / 'uie': next bits as unsigned exp-Golomb code
36 'se' / 'sie': next bits as signed exp-Golomb code
37 'bits:5' : 5 bits as a bitstring object
38 'bytes:10' : 10 bytes as a bytes object
39 'bool' : 1 bit as a bool
40 'pad:3' : 3 zero bits as padding
41
42 >>> s = pack('uint:12, bits', 100, '0xffe')
43 >>> t = pack(['bits', 'bin:3'], s, '111')
44 >>> u = pack('uint:8=a, uint:8=b, uint:55=a', a=6, b=44)
45
46 """
47 tokens = []
48 if isinstance(fmt, str):
49 fmt = [fmt]
50 try:
51 for f_item in fmt:
52 _, tkns = tokenparser(f_item, tuple(sorted(kwargs.keys())))
53 tokens.extend(tkns)
54 except ValueError as e:
55 raise CreationError(*e.args)
56 value_iter = iter(values)
57 bsl: List[BitStore] = []
58 try:
59 for name, length, value in tokens:
60 # If the value is in the kwd dictionary then it takes precedence.
61 value = kwargs.get(value, value)
62 # If the length is in the kwd dictionary then use that too.
63 length = kwargs.get(length, length)
64 # Also if we just have a dictionary name then we want to use it
65 if name in kwargs and length is None and value is None:
66 bsl.append(BitStream(kwargs[name])._bitstore)
67 continue
68 if length is not None:
69 length = int(length)
70 if value is None and name != 'pad':
71 # Take the next value from the ones provided
72 value = next(value_iter)
73 if name == 'bits':
74 value = bitstring.bits.Bits(value)
75 if length is not None and length != len(value):
76 raise CreationError(f"Token with length {length} packed with value of length {len(value)}.")
77 bsl.append(value._bitstore)
78 continue
79 bsl.append(bitstore_from_token(name, length, value))
80 except StopIteration:
81 raise CreationError(f"Not enough parameters present to pack according to the "
82 f"format. {len(tokens)} values are needed.")
83
84 try:
85 next(value_iter)
86 except StopIteration:
87 # Good, we've used up all the *values.
88 s = BitStream()
89 if bitstring.options.lsb0:
90 bsl.reverse()
91 for b in bsl:
92 s._bitstore += b
93 return s
94
95 raise CreationError(f"Too many parameters present to pack according to the format. Only {len(tokens)} values were expected.")