Use osmocom.construct.{Bytes,GreedyBytes} for hexstring input support

The upstream construct.{Bytes,GreedyBytes} only support bytes/bytearray
input data for the encoder, while the [newly-created]
osmocom.construct.{Bytes,GreedyBytes} support alternatively hex-string input.

This is important in the context of encoding construct-based types from
JSON, where our osmocom.utils.JsonEncoder will automatically convert any
bytes to hex-string, while re-encoding those hex-strings will fail prior
to this patch.

Change-Id: I1c8df6350c68aa408ec96ff6cd1e405ceb1a4fbb
Closes: OS#6774
This commit is contained in:
Harald Welte
2025-04-23 13:28:01 +02:00
parent 19f3759306
commit 949c2a2d57
15 changed files with 22 additions and 20 deletions

View File

@@ -29,7 +29,7 @@ import abc
import typing import typing
from typing import List, Dict, Optional from typing import List, Dict, Optional
from termcolor import colored from termcolor import colored
from construct import Byte, GreedyBytes from construct import Byte
from construct import Optional as COptional from construct import Optional as COptional
from osmocom.construct import * from osmocom.construct import *
from osmocom.utils import * from osmocom.utils import *

View File

@@ -9,7 +9,7 @@ APDU commands of 3GPP TS 31.102 V16.6.0
""" """
from typing import Dict from typing import Dict
from construct import BitStruct, Enum, BitsInteger, Int8ub, Bytes, this, Struct, If, Switch, Const from construct import BitStruct, Enum, BitsInteger, Int8ub, this, Struct, If, Switch, Const
from construct import Optional as COptional from construct import Optional as COptional
from osmocom.construct import * from osmocom.construct import *

View File

@@ -26,7 +26,7 @@ Support for the Secure Element Access Control, specifically the ARA-M inside an
# #
from construct import GreedyBytes, GreedyString, Struct, Enum, Int8ub, Int16ub from construct import GreedyString, Struct, Enum, Int8ub, Int16ub
from construct import Optional as COptional from construct import Optional as COptional
from osmocom.construct import * from osmocom.construct import *
from osmocom.tlv import * from osmocom.tlv import *

View File

@@ -20,11 +20,11 @@ as described in 3GPP TS 31.111."""
from typing import List from typing import List
from bidict import bidict from bidict import bidict
from construct import Int8ub, Int16ub, Byte, Bytes, BitsInteger from construct import Int8ub, Int16ub, Byte, BitsInteger
from construct import Struct, Enum, BitStruct, this from construct import Struct, Enum, BitStruct, this
from construct import GreedyBytes, Switch, GreedyRange, FlagsEnum from construct import Switch, GreedyRange, FlagsEnum
from osmocom.tlv import TLV_IE, COMPR_TLV_IE, BER_TLV_IE, TLV_IE_Collection from osmocom.tlv import TLV_IE, COMPR_TLV_IE, BER_TLV_IE, TLV_IE_Collection
from osmocom.construct import PlmnAdapter, BcdAdapter, HexAdapter, GsmStringAdapter, TonNpi, GsmString from osmocom.construct import PlmnAdapter, BcdAdapter, HexAdapter, GsmStringAdapter, TonNpi, GsmString, Bytes, GreedyBytes
from osmocom.utils import b2h from osmocom.utils import b2h
from pySim.utils import dec_xplmn_w_act from pySim.utils import dec_xplmn_w_act

View File

@@ -17,9 +17,10 @@ Also known as SCP81 for SIM/USIM/UICC/eUICC/eSIM OTA.
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from construct import Struct, Int8ub, Int16ub, Bytes, GreedyBytes, GreedyString, BytesInteger from construct import Struct, Int8ub, Int16ub, GreedyString, BytesInteger
from construct import this, len_, Rebuild, Const from construct import this, len_, Rebuild, Const
from construct import Optional as COptional from construct import Optional as COptional
from osmocom.construct import Bytes, GreedyBytes
from osmocom.tlv import BER_TLV_IE from osmocom.tlv import BER_TLV_IE
from pySim import cat from pySim import cat

View File

@@ -20,8 +20,9 @@ import logging
from typing import Optional from typing import Optional
from Cryptodome.Cipher import DES3, DES from Cryptodome.Cipher import DES3, DES
from Cryptodome.Util.strxor import strxor from Cryptodome.Util.strxor import strxor
from construct import Struct, Bytes, Int8ub, Int16ub, Const from construct import Struct, Int8ub, Int16ub, Const
from construct import Optional as COptional from construct import Optional as COptional
from osmocom.construct import Bytes
from osmocom.utils import b2h from osmocom.utils import b2h
from osmocom.tlv import bertlv_parse_len, bertlv_encode_len from osmocom.tlv import bertlv_parse_len, bertlv_encode_len
from pySim.utils import parse_command_apdu from pySim.utils import parse_command_apdu

View File

@@ -26,7 +26,7 @@ order to describe the files specified in UIC Reference P38 T 9001 5.0 "FFFIS for
from pySim.utils import * from pySim.utils import *
from struct import pack, unpack from struct import pack, unpack
from construct import Struct, Bytes, Int8ub, Int16ub, Int24ub, Int32ub, FlagsEnum from construct import Struct, Int8ub, Int16ub, Int24ub, Int32ub, FlagsEnum
from construct import Optional as COptional from construct import Optional as COptional
from osmocom.construct import * from osmocom.construct import *

View File

@@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
""" """
from construct import GreedyBytes, GreedyString from construct import GreedyString
from osmocom.tlv import * from osmocom.tlv import *
from osmocom.construct import * from osmocom.construct import *

View File

@@ -19,7 +19,7 @@ import zlib
import abc import abc
import struct import struct
from typing import Optional, Tuple from typing import Optional, Tuple
from construct import Enum, Int8ub, Int16ub, Struct, Bytes, GreedyBytes, BitsInteger, BitStruct from construct import Enum, Int8ub, Int16ub, Struct, BitsInteger, BitStruct
from construct import Flag, Padding, Switch, this, PrefixedArray, GreedyRange from construct import Flag, Padding, Switch, this, PrefixedArray, GreedyRange
from osmocom.construct import * from osmocom.construct import *
from osmocom.utils import b2h from osmocom.utils import b2h

View File

@@ -20,10 +20,10 @@
import typing import typing
import abc import abc
from bidict import bidict from bidict import bidict
from construct import Int8ub, Byte, Bytes, Bit, Flag, BitsInteger from construct import Int8ub, Byte, Bit, Flag, BitsInteger
from construct import Struct, Enum, Tell, BitStruct, this, Padding from construct import Struct, Enum, Tell, BitStruct, this, Padding
from construct import Prefixed, GreedyRange, GreedyBytes from construct import Prefixed, GreedyRange
from osmocom.construct import HexAdapter, BcdAdapter, TonNpi from osmocom.construct import HexAdapter, BcdAdapter, TonNpi, Bytes, GreedyBytes
from osmocom.utils import Hexstr, h2b, b2h from osmocom.utils import Hexstr, h2b, b2h
from smpp.pdu import pdu_types, operations from smpp.pdu import pdu_types, operations

View File

@@ -18,7 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
""" """
from struct import unpack from struct import unpack
from construct import FlagsEnum, Byte, Struct, Int8ub, Bytes, Mapping, Enum, Padding, BitsInteger from construct import FlagsEnum, Byte, Struct, Int8ub, Mapping, Enum, Padding, BitsInteger
from construct import Bit, this, Int32ub, Int16ub, Nibble, BytesInteger, GreedyRange, Const from construct import Bit, this, Int32ub, Int16ub, Nibble, BytesInteger, GreedyRange, Const
from construct import Optional as COptional from construct import Optional as COptional
from osmocom.utils import * from osmocom.utils import *

View File

@@ -22,7 +22,7 @@ Various constants from 3GPP TS 31.103 V18.1.0
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
from construct import Struct, Switch, this, Bytes, GreedyString from construct import Struct, Switch, this, GreedyString
from osmocom.utils import * from osmocom.utils import *
from osmocom.tlv import * from osmocom.tlv import *
from osmocom.construct import * from osmocom.construct import *

View File

@@ -19,9 +19,9 @@ hence need to be in a separate python module to avoid circular dependencies.
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
from construct import Struct, Switch, Bytes, GreedyString, GreedyBytes, Int8ub, Prefixed, Enum, Byte from construct import Struct, Switch, GreedyString, Int8ub, Prefixed, Enum, Byte
from osmocom.tlv import BER_TLV_IE, TLV_IE_Collection from osmocom.tlv import BER_TLV_IE, TLV_IE_Collection
from osmocom.construct import HexAdapter, Utf8Adapter from osmocom.construct import Bytes, HexAdapter, Utf8Adapter, GreedyBytes
from pySim.filesystem import * from pySim.filesystem import *
# TS 31.103 Section 4.2.16 # TS 31.103 Section 4.2.16

View File

@@ -5,7 +5,7 @@ cmd2>=1.5
jsonpath-ng jsonpath-ng
construct>=2.10.70 construct>=2.10.70
bidict bidict
pyosmocom>=0.0.8 pyosmocom>=0.0.9
pyyaml>=5.1 pyyaml>=5.1
termcolor termcolor
colorlog colorlog

View File

@@ -25,7 +25,7 @@ setup(
"jsonpath-ng", "jsonpath-ng",
"construct >= 2.10.70", "construct >= 2.10.70",
"bidict", "bidict",
"pyosmocom >= 0.0.8", "pyosmocom >= 0.0.9",
"pyyaml >= 5.1", "pyyaml >= 5.1",
"termcolor", "termcolor",
"colorlog", "colorlog",