mirror of
https://gitea.osmocom.org/sim-card/pysim.git
synced 2026-03-18 03:18:34 +03:00
global_platform 'put_key': constrain ranges of KVN + KID in argparse
The earlier we catch errors in user input, the better. Change-Id: Icee656f1373a993b6883ffaab441fe178c0fe8cb
This commit is contained in:
@@ -7,6 +7,7 @@ import json
|
||||
import abc
|
||||
import string
|
||||
import datetime
|
||||
import argparse
|
||||
from io import BytesIO
|
||||
from typing import Optional, List, Dict, Any, Tuple, NewType
|
||||
|
||||
@@ -913,6 +914,18 @@ def auto_int(x):
|
||||
"""Helper function for argparse to accept hexadecimal integers."""
|
||||
return int(x, 0)
|
||||
|
||||
def _auto_uint(x, max_val: int):
|
||||
"""Helper function for argparse to accept hexadecimal or decimal integers."""
|
||||
ret = int(x, 0)
|
||||
if ret < 0 or ret > max_val:
|
||||
raise argparse.ArgumentTypeError('Number exceeds permited value range (0, %u)' % max_val)
|
||||
return ret
|
||||
|
||||
def auto_uint7(x):
|
||||
return _auto_uint(x, 127)
|
||||
|
||||
def auto_uint8(x):
|
||||
return _auto_uint(x, 255)
|
||||
|
||||
def expand_hex(hexstring, length):
|
||||
"""Expand a given hexstring to a specified length by replacing "." or ".."
|
||||
|
||||
Reference in New Issue
Block a user