def set_data(self, ef, tag: int, value: str, verify: bool = False, conserve: bool = False): """Execute SET DATA. Args ef : string or list of strings indicating name or path of transparent EF tag : BER-TLV Tag of value to be stored value : BER-TLV value to be stored """ r = self.select_path(ef) if len(r[-1]) == 0: return (None, None) # in case of deleting the data, we only have 'tag' but no 'value' if not value: return self._set_data('%02x' % tag, first=True) # FIXME: proper BER-TLV encode tl = '%02x%s' % (tag, b2h(bertlv_encode_len(len(value) // 2))) tlv = tl + value tlv_bin = h2b(tlv) first = True total_len = len(tlv_bin) remaining = tlv_bin while len(remaining) > 0: fragment = remaining[:255] rdata, sw = self._set_data(fragment, first=first) first = False remaining = remaining[255:] return rdata, sw
def test_BerTlvLenDec(self): self.assertEqual(utils.bertlv_encode_len(1), b'\x01') self.assertEqual(utils.bertlv_encode_len(127), b'\x7f') self.assertEqual(utils.bertlv_encode_len(128), b'\x81\x80') self.assertEqual(utils.bertlv_encode_len(0x123456), b'\x83\x12\x34\x56')
def _encode_len(self, val: bytes) -> bytes: return bertlv_encode_len(len(val))