lists.osmocom.org
Sign In Sign Up
Manage this list Sign In Sign Up

Keyboard Shortcuts

Thread View

  • j: Next unread message
  • k: Previous unread message
  • j a: Jump to all threads
  • j l: Jump to MailingList overview

gerrit-log

Thread Start a new thread
Download
Threads by month
  • ----- 2026 -----
  • November
  • October
  • September
  • August
  • July
  • June
  • May
  • April
  • March
  • February
  • January
  • ----- 2025 -----
  • December
  • November
  • October
  • September
  • August
  • July
  • June
  • May
  • April
  • March
  • February
  • January
  • ----- 2024 -----
  • December
  • November
  • October
  • September
  • August
  • July
  • June
  • May
  • April
  • March
  • February
  • January
  • ----- 2023 -----
  • December
  • November
  • October
  • September
  • August
  • July
  • June
  • May
  • April
  • March
  • February
  • January
  • ----- 2022 -----
  • December
  • November
  • October
  • September
  • August
  • July
  • June
  • May
  • April
  • March
  • February
  • January
gerrit-log@lists.osmocom.org

January 2025

  • 1 participants
  • 1491 discussions
[S] Change in libosmocore[master]: osmo_iofd_register: fix the case of changing fd
by pespin 09 Jan '25

09 Jan '25
Attention is currently required from: falconia. pespin has posted comments on this change by falconia. ( https://gerrit.osmocom.org/c/libosmocore/+/39276?usp=email ) Change subject: osmo_iofd_register: fix the case of changing fd ...................................................................... Patch Set 2: Code-Review+1 (1 comment) File src/core/osmo_io.c: https://gerrit.osmocom.org/c/libosmocore/+/39276/comment/13113700_83d7f1a3?… : PS2, Line 759: if (fd >= 0) I think you don't even need this "if" line, you can do "iofd->fd = fd;" directly. -- To view, visit https://gerrit.osmocom.org/c/libosmocore/+/39276?usp=email To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings?usp=email Gerrit-MessageType: comment Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Change-Id: If8b8486ad7934afa203dfe1e996c9217373a017b Gerrit-Change-Number: 39276 Gerrit-PatchSet: 2 Gerrit-Owner: falconia <falcon(a)freecalypso.org> Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: pespin <pespin(a)sysmocom.de> Gerrit-Attention: falconia <falcon(a)freecalypso.org> Gerrit-Comment-Date: Thu, 09 Jan 2025 23:45:49 +0000 Gerrit-HasComments: Yes Gerrit-Has-Labels: Yes
1 0
0 0
[S] Change in libosmocore[master]: osmo_iofd_register: fix the case of changing fd
by falconia 09 Jan '25

09 Jan '25
Attention is currently required from: pespin. falconia has posted comments on this change by falconia. ( https://gerrit.osmocom.org/c/libosmocore/+/39276?usp=email ) Change subject: osmo_iofd_register: fix the case of changing fd ...................................................................... Patch Set 2: (2 comments) Commit Message: https://gerrit.osmocom.org/c/libosmocore/+/39276/comment/d68b7023_d0106d95?… : PS1, Line 22: > OK, I'll add this notation in the next iteration. Done File src/core/osmo_io.c: https://gerrit.osmocom.org/c/libosmocore/+/39276/comment/1dde1537_45deaf46?… : PS1, Line 756: else if (iofd->fd < 0) > Right after the `else if` clause I removed, the code checks `IOFD_FLAG_FD_REGISTERED`. […] Please look at the new version of my patch. Following the principle of "leave the campsite cleaner than you found it", I decided to make a larger restructuring, and now IMO there is a cleaner distinction between the already-registered case and the more ordinary case of not already registered. -- To view, visit https://gerrit.osmocom.org/c/libosmocore/+/39276?usp=email To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings?usp=email Gerrit-MessageType: comment Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Change-Id: If8b8486ad7934afa203dfe1e996c9217373a017b Gerrit-Change-Number: 39276 Gerrit-PatchSet: 2 Gerrit-Owner: falconia <falcon(a)freecalypso.org> Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: pespin <pespin(a)sysmocom.de> Gerrit-Attention: pespin <pespin(a)sysmocom.de> Gerrit-Comment-Date: Thu, 09 Jan 2025 21:23:33 +0000 Gerrit-HasComments: Yes Gerrit-Has-Labels: No Comment-In-Reply-To: falconia <falcon(a)freecalypso.org> Comment-In-Reply-To: pespin <pespin(a)sysmocom.de>
1 0
0 0
[S] Change in libosmocore[master]: osmo_iofd_register: fix the case of changing fd
by falconia 09 Jan '25

09 Jan '25
Attention is currently required from: pespin. Hello Jenkins Builder, pespin, I'd like you to reexamine a change. Please visit https://gerrit.osmocom.org/c/libosmocore/+/39276?usp=email to look at the new patch set (#2). The following approvals got outdated and were removed: Code-Review-1 by pespin, Verified+1 by Jenkins Builder Change subject: osmo_iofd_register: fix the case of changing fd ...................................................................... osmo_iofd_register: fix the case of changing fd Doxygen description for this function states: \param[in] fd the system fd number that will be registered. If you did not yet specify the file descriptor number during osmo_fd_setup(), or if it has changed since then, you can state the [new] file descriptor number as argument. If you wish to proceed with the previously specified file descriptor number, pass -1. However, the case where a new fd is passed to osmo_iofd_register() while the structure contains an old (but unregistered) fd was not handled correctly: the code would proceed with re-registering the old fd, ignoring the newly passed one. Fixes: df1ee8568b97dbf6d5268a83d1715a1c1fffb2de Change-Id: If8b8486ad7934afa203dfe1e996c9217373a017b --- M src/core/osmo_io.c 1 file changed, 13 insertions(+), 11 deletions(-) git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/76/39276/2 -- To view, visit https://gerrit.osmocom.org/c/libosmocore/+/39276?usp=email To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings?usp=email Gerrit-MessageType: newpatchset Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Change-Id: If8b8486ad7934afa203dfe1e996c9217373a017b Gerrit-Change-Number: 39276 Gerrit-PatchSet: 2 Gerrit-Owner: falconia <falcon(a)freecalypso.org> Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: pespin <pespin(a)sysmocom.de> Gerrit-Attention: pespin <pespin(a)sysmocom.de>
1 0
0 0
[XS] Change in libosmocore[master]: osmo_iofd_register: fix the case of changing fd
by falconia 09 Jan '25

09 Jan '25
Attention is currently required from: pespin. falconia has posted comments on this change by falconia. ( https://gerrit.osmocom.org/c/libosmocore/+/39276?usp=email ) Change subject: osmo_iofd_register: fix the case of changing fd ...................................................................... Patch Set 1: (6 comments) Patchset: PS1: > @falcon@freecalypso. […] I am working on OS#6474. I got this code: https://gitea.osmocom.org/themwi/twrtp-proto which I am currently reworking into a series of patches for libosmo-netif, with the goal of bringing my new RTP implementation into Osmocom proper and thus making it available to OsmoBTS and others in the future. The version which I plan to submit as patches to libosmo-netif will be a little different from the just-linked external/standalone version: aside from renaming headers and APIs to fit into Osmocom namespace, I am reworking the core structures (`struct twrtp_jibuf_inst` which will become `struct osmo_twjit` and `struct twrtp_endp` which will become `struct osmo_twrtp`) to be opaque instead of fully exposed - hence I need to provide APIs for all explicitly-allowed external operations. In the new version there will be an API that takes in OS-level fds for RTP and RTCP sockets and feeds them to `osmo_iofd_register()` on `osmo_io_fd` instances that are internal to the opaque twrtp instance. The problem is - what happens if one of those `osmo_iofd_register()` operations fails? My twrtp API needs to fail cleanly, and leave the twrtp instance in a state where the user could try again and potentially succeed next time. I reviewed osmo_io code to see if it does what I expect, and noticed that if `osmo_iofd_register()` fails, the failed fd will remain stored, and the next `osmo_iofd_register()` attempt will ignore the fd passed to it. Commit Message: https://gerrit.osmocom.org/c/libosmocore/+/39276/comment/0926db90_88079472?… : PS1, Line 13: osmo_fd_setup(), or if it has changed since then, you can state > "or if it has changed since then" I probably missed that part when I caused the regression recently, […] Acknowledged https://gerrit.osmocom.org/c/libosmocore/+/39276/comment/96f838a1_3c01affc?… : PS1, Line 22: > Fixes: df1ee8568b97dbf6d5268a83d1715a1c1fffb2de OK, I'll add this notation in the next iteration. File src/core/osmo_io.c: https://gerrit.osmocom.org/c/libosmocore/+/39276/comment/5f8b3498_bb0eb7e5?… : PS1, Line 756: else if (iofd->fd < 0) > I think you need to leave this as is. […] Right after the `else if` clause I removed, the code checks `IOFD_FLAG_FD_REGISTERED`. How can `iofd->fd` be -1 if the registered flag is set? And if the registered flag is not set, then my code sets `iofd->fd` to the new fd unconditionally. But maybe I'll leave this code unchanged in the next iteration - let me think about it. https://gerrit.osmocom.org/c/libosmocore/+/39276/comment/e7c591f3_e3a32f94?… : PS1, Line 760: return iofd->fd == fd ? 0 : -ENOTSUP; > since you are not setting iofd->fd in the code path you removed, this check is also wrong now? See my other comment above. https://gerrit.osmocom.org/c/libosmocore/+/39276/comment/7e3efd03_e4238e8b?… : PS1, Line 761: } else { > if clause is an early return, this "else" can be removed and the assignment done directly. Acknowledged -- To view, visit https://gerrit.osmocom.org/c/libosmocore/+/39276?usp=email To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings?usp=email Gerrit-MessageType: comment Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Change-Id: If8b8486ad7934afa203dfe1e996c9217373a017b Gerrit-Change-Number: 39276 Gerrit-PatchSet: 1 Gerrit-Owner: falconia <falcon(a)freecalypso.org> Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: pespin <pespin(a)sysmocom.de> Gerrit-Attention: pespin <pespin(a)sysmocom.de> Gerrit-Comment-Date: Thu, 09 Jan 2025 20:34:51 +0000 Gerrit-HasComments: Yes Gerrit-Has-Labels: No Comment-In-Reply-To: pespin <pespin(a)sysmocom.de>
1 0
0 0
[XS] Change in libosmocore[master]: osmo_iofd_register: fix the case of changing fd
by pespin 09 Jan '25

09 Jan '25
Attention is currently required from: falconia. pespin has posted comments on this change by falconia. ( https://gerrit.osmocom.org/c/libosmocore/+/39276?usp=email ) Change subject: osmo_iofd_register: fix the case of changing fd ...................................................................... Patch Set 1: (1 comment) Patchset: PS1: @falcon@freecalypso.org can you share how did you notice this bug? did you see something failing somewhere? -- To view, visit https://gerrit.osmocom.org/c/libosmocore/+/39276?usp=email To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings?usp=email Gerrit-MessageType: comment Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Change-Id: If8b8486ad7934afa203dfe1e996c9217373a017b Gerrit-Change-Number: 39276 Gerrit-PatchSet: 1 Gerrit-Owner: falconia <falcon(a)freecalypso.org> Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: pespin <pespin(a)sysmocom.de> Gerrit-Attention: falconia <falcon(a)freecalypso.org> Gerrit-Comment-Date: Thu, 09 Jan 2025 19:51:29 +0000 Gerrit-HasComments: Yes Gerrit-Has-Labels: No
1 0
0 0
[XS] Change in libosmocore[master]: osmo_iofd_register: fix the case of changing fd
by pespin 09 Jan '25

09 Jan '25
Attention is currently required from: falconia. pespin has posted comments on this change by falconia. ( https://gerrit.osmocom.org/c/libosmocore/+/39276?usp=email ) Change subject: osmo_iofd_register: fix the case of changing fd ...................................................................... Patch Set 1: (2 comments) Patchset: PS1: I think the proper fix is simply to add a line after the last "if" code path check: "iofd->fd = fd;" File src/core/osmo_io.c: https://gerrit.osmocom.org/c/libosmocore/+/39276/comment/692a9e53_daf00168?… : PS1, Line 756: else if (iofd->fd < 0) I think you need to leave this as is. This code block basically makes sure that iofd->fd == fd if any of them was not set (-1). -- To view, visit https://gerrit.osmocom.org/c/libosmocore/+/39276?usp=email To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings?usp=email Gerrit-MessageType: comment Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Change-Id: If8b8486ad7934afa203dfe1e996c9217373a017b Gerrit-Change-Number: 39276 Gerrit-PatchSet: 1 Gerrit-Owner: falconia <falcon(a)freecalypso.org> Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: pespin <pespin(a)sysmocom.de> Gerrit-Attention: falconia <falcon(a)freecalypso.org> Gerrit-Comment-Date: Thu, 09 Jan 2025 19:50:48 +0000 Gerrit-HasComments: Yes Gerrit-Has-Labels: No
1 0
0 0
[XS] Change in libosmocore[master]: osmo_iofd_register: fix the case of changing fd
by pespin 09 Jan '25

09 Jan '25
Attention is currently required from: falconia. pespin has posted comments on this change by falconia. ( https://gerrit.osmocom.org/c/libosmocore/+/39276?usp=email ) Change subject: osmo_iofd_register: fix the case of changing fd ...................................................................... Patch Set 1: Code-Review-1 (4 comments) Commit Message: https://gerrit.osmocom.org/c/libosmocore/+/39276/comment/c246cfbd_dba1f3ce?… : PS1, Line 13: osmo_fd_setup(), or if it has changed since then, you can state "or if it has changed since then" I probably missed that part when I caused the regression recently, thanks for noticing. https://gerrit.osmocom.org/c/libosmocore/+/39276/comment/4b93886b_2c0a0c75?… : PS1, Line 22: Fixes: df1ee8568b97dbf6d5268a83d1715a1c1fffb2de File src/core/osmo_io.c: https://gerrit.osmocom.org/c/libosmocore/+/39276/comment/e18c5373_bd05e625?… : PS1, Line 760: return iofd->fd == fd ? 0 : -ENOTSUP; since you are not setting iofd->fd in the code path you removed, this check is also wrong now? https://gerrit.osmocom.org/c/libosmocore/+/39276/comment/0ca71e1b_45f322fe?… : PS1, Line 761: } else { if clause is an early return, this "else" can be removed and the assignment done directly. -- To view, visit https://gerrit.osmocom.org/c/libosmocore/+/39276?usp=email To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings?usp=email Gerrit-MessageType: comment Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Change-Id: If8b8486ad7934afa203dfe1e996c9217373a017b Gerrit-Change-Number: 39276 Gerrit-PatchSet: 1 Gerrit-Owner: falconia <falcon(a)freecalypso.org> Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: pespin <pespin(a)sysmocom.de> Gerrit-Attention: falconia <falcon(a)freecalypso.org> Gerrit-Comment-Date: Thu, 09 Jan 2025 19:48:45 +0000 Gerrit-HasComments: Yes Gerrit-Has-Labels: Yes
1 0
0 0
[XS] Change in libosmocore[master]: osmo_iofd_register: fix the case of changing fd
by falconia 09 Jan '25

09 Jan '25
falconia has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmocore/+/39276?usp=email ) Change subject: osmo_iofd_register: fix the case of changing fd ...................................................................... osmo_iofd_register: fix the case of changing fd Doxygen description for this function states: \param[in] fd the system fd number that will be registered. If you did not yet specify the file descriptor number during osmo_fd_setup(), or if it has changed since then, you can state the [new] file descriptor number as argument. If you wish to proceed with the previously specified file descriptor number, pass -1. However, the case where a new fd is passed to osmo_iofd_register() while the structure contains an old (but unregistered) fd was not handled correctly: the code would proceed with re-registering the old fd, ignoring the newly passed one. Change-Id: If8b8486ad7934afa203dfe1e996c9217373a017b --- M src/core/osmo_io.c 1 file changed, 2 insertions(+), 2 deletions(-) git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/76/39276/1 diff --git a/src/core/osmo_io.c b/src/core/osmo_io.c index e109cdf..1e3e114 100644 --- a/src/core/osmo_io.c +++ b/src/core/osmo_io.c @@ -753,13 +753,13 @@ } if (fd < 0) fd = iofd->fd; - else if (iofd->fd < 0) - iofd->fd = fd; if (IOFD_FLAG_ISSET(iofd, IOFD_FLAG_FD_REGISTERED)) { /* If re-registering same fd, handle as NO-OP. * New FD should go through unregister() first. */ return iofd->fd == fd ? 0 : -ENOTSUP; + } else { + iofd->fd = fd; } rc = osmo_iofd_ops.register_fd(iofd); -- To view, visit https://gerrit.osmocom.org/c/libosmocore/+/39276?usp=email To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings?usp=email Gerrit-MessageType: newchange Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Change-Id: If8b8486ad7934afa203dfe1e996c9217373a017b Gerrit-Change-Number: 39276 Gerrit-PatchSet: 1 Gerrit-Owner: falconia <falcon(a)freecalypso.org>
1 0
0 0
[S] Change in ...pyosmocom[master]: construct: add length steps to StripTrailerAdapter
by laforge 09 Jan '25

09 Jan '25
laforge has submitted this change. ( https://gerrit.osmocom.org/c/python/pyosmocom/+/39162?usp=email ) Change subject: construct: add length steps to StripTrailerAdapter ...................................................................... construct: add length steps to StripTrailerAdapter The class StripTrailerAdapter allows to remove trailing bytes that match a specified value from the encoding result of a sub-construct. The result is always the shortest possible remainder of bytes that do not match the secified value. Unfortunately there are specifications that explicitly require the length of the result to fit into a limited set of possible length values. For example: A result may be either one byte or three byte long. To cover those cases as well, let's add an array parameter where we can configure the allowed length values of the encoding result. Related: OS#6679 Change-Id: I86df064fa41db85923eeb0d83cc399504fdd4488 --- M src/osmocom/construct.py M tests/test_construct.py 2 files changed, 41 insertions(+), 3 deletions(-) Approvals: laforge: Looks good to me, approved Jenkins Builder: Verified diff --git a/src/osmocom/construct.py b/src/osmocom/construct.py index c69b44f..d4284d9 100644 --- a/src/osmocom/construct.py +++ b/src/osmocom/construct.py @@ -413,16 +413,20 @@ Encoder removes all trailing bytes matching the default_value Decoder pads input data up to total_length with default_value + In case the encoding restricts the length of the result to specific values, the API user may set those restrictions + using the steps parameter. (e.g. encoded result must be either 1 or 3 byte long, steps would be set to [1,3]) + This is used in constellations like "FlagsEnum(StripTrailerAdapter(GreedyBytes, 3), ..." where you have a bit-mask that may have 1, 2 or 3 bytes, depending on whether or not any of the LSBs are actually set. """ - def __init__(self, subcon, total_length:int, default_value=b'\x00', min_len=1): + def __init__(self, subcon, total_length:int, default_value=b'\x00', min_len=1, steps:typing.List[int]=[]): super().__init__(subcon) assert len(default_value) == 1 self.total_length = total_length self.default_value = default_value self.min_len = min_len + self.steps = steps def _decode(self, obj, context, path): assert isinstance(obj, bytes) @@ -435,9 +439,17 @@ assert isinstance(obj, int) obj = obj.to_bytes(self.total_length, 'big') # remove trailing bytes if they are zero + + obj_step_aligned = obj while len(obj) > self.min_len and obj[-1] == self.default_value[0]: obj = obj[:-1] - return obj + if len(obj) in self.steps: + obj_step_aligned = obj + + if self.steps == []: + return obj + else: + return obj_step_aligned; def filter_dict(d, exclude_prefix='_'): diff --git a/tests/test_construct.py b/tests/test_construct.py index bcb2cf8..e4234db 100755 --- a/tests/test_construct.py +++ b/tests/test_construct.py @@ -90,13 +90,39 @@ final_application=0x0200, global_service=0x0100, receipt_generation=0x80, ciphered_load_file_data_block=0x40, contactless_activation=0x20, contactless_self_activation=0x10) + PrivilegesSteps = FlagsEnum(StripTrailerAdapter(GreedyBytes, 3, steps = [1,3]), security_domain=0x800000, + dap_verification=0x400000, + delegated_management=0x200000, card_lock=0x100000, + card_terminate=0x080000, card_reset=0x040000, + cvm_management=0x020000, mandated_dap_verification=0x010000, + trusted_path=0x8000, authorized_management=0x4000, + token_management=0x2000, global_delete=0x1000, + global_lock=0x0800, global_registry=0x0400, + final_application=0x0200, global_service=0x0100, + receipt_generation=0x80, ciphered_load_file_data_block=0x40, + contactless_activation=0x20, contactless_self_activation=0x10) examples = ['00', '80', '8040', '400010'] - def test_examples(self): + def test_encdec(self): for e in self.examples: dec = self.Privileges.parse(h2b(e)) reenc = self.Privileges.build(dec) self.assertEqual(e, b2h(reenc)) + def test_enc(self): + enc = self.Privileges.build({'dap_verification' : True}) + self.assertEqual(b2h(enc), '40') + enc = self.Privileges.build({'dap_verification' : True, 'global_service' : True}) + self.assertEqual(b2h(enc), '4001') + enc = self.Privileges.build({'dap_verification' : True, 'global_service' : True, 'contactless_self_activation' : True}) + self.assertEqual(b2h(enc), '400110') + + enc = self.PrivilegesSteps.build({'dap_verification' : True}) + self.assertEqual(b2h(enc), '40') + enc = self.PrivilegesSteps.build({'dap_verification' : True, 'global_service' : True}) + self.assertEqual(b2h(enc), '400100') + enc = self.PrivilegesSteps.build({'dap_verification' : True, 'global_service' : True, 'contactless_self_activation' : True}) + self.assertEqual(b2h(enc), '400110') + class TestAdapters(unittest.TestCase): def test_dns_adapter(self): -- To view, visit https://gerrit.osmocom.org/c/python/pyosmocom/+/39162?usp=email To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings?usp=email Gerrit-MessageType: merged Gerrit-Project: python/pyosmocom Gerrit-Branch: master Gerrit-Change-Id: I86df064fa41db85923eeb0d83cc399504fdd4488 Gerrit-Change-Number: 39162 Gerrit-PatchSet: 2 Gerrit-Owner: dexter <pmaier(a)sysmocom.de> Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: fixeria <vyanitskiy(a)sysmocom.de> Gerrit-Reviewer: laforge <laforge(a)osmocom.org> Gerrit-Reviewer: pespin <pespin(a)sysmocom.de>
1 0
0 0
[M] Change in ...pyosmocom[master]: construct: allow stripping of leading zeros with StripHeaderAdapter
by laforge 09 Jan '25

09 Jan '25
laforge has submitted this change. ( https://gerrit.osmocom.org/c/python/pyosmocom/+/39193?usp=email ) Change subject: construct: allow stripping of leading zeros with StripHeaderAdapter ...................................................................... construct: allow stripping of leading zeros with StripHeaderAdapter This patch adds a new class StripHeaderAdapter. This adapter class can be used to strip leading zeros in the same way that StripTrailerAdapter strips trailing zeros. Related: OS#6679 Change-Id: I1a9fff17abbbef0c5f6d45f58198debfa12e78b6 --- M src/osmocom/construct.py M tests/test_construct.py 2 files changed, 89 insertions(+), 0 deletions(-) Approvals: Jenkins Builder: Verified laforge: Looks good to me, approved diff --git a/src/osmocom/construct.py b/src/osmocom/construct.py index d4284d9..7a2851a 100644 --- a/src/osmocom/construct.py +++ b/src/osmocom/construct.py @@ -451,6 +451,48 @@ else: return obj_step_aligned; +class StripHeaderAdapter(Adapter): + """ + Encoder removes all leading bytes matching the default_value + Decoder pads input data up to total_length with default_value + + In case the encoding restricts the length of the result to specific values, the API user may set those restrictions + using the steps parameter. (e.g. encoded result must be either 1 or 3 byte long, steps would be set to [1,3]) + + This is used in constellations like "FlagsEnum(StripHeaderAdapter(GreedyBytes, 3), ..." + where you have a bit-mask that may have 1, 2 or 3 bytes, depending on whether or not any + of the MSBs are actually set. + """ + def __init__(self, subcon, total_length:int, default_value=b'\x00', min_len=1, steps:typing.List[int]=[]): + super().__init__(subcon) + assert len(default_value) == 1 + self.total_length = total_length + self.default_value = default_value + self.min_len = min_len + self.steps = steps + + def _decode(self, obj, context, path): + assert isinstance(obj, bytes) + # pad with suppressed/missing bytes + if len(obj) < self.total_length: + obj = self.default_value * (self.total_length - len(obj)) + obj + return int.from_bytes(obj, 'big') + + def _encode(self, obj, context, path): + assert isinstance(obj, int) + obj = obj.to_bytes(self.total_length, 'big') + # remove trailing bytes if they are zero + + obj_step_aligned = obj + while len(obj) > self.min_len and obj[0] == self.default_value[0]: + obj = obj[1:] + if len(obj) in self.steps: + obj_step_aligned = obj + + if self.steps == []: + return obj + else: + return obj_step_aligned; def filter_dict(d, exclude_prefix='_'): """filter the input dict to ensure no keys starting with 'exclude_prefix' remain.""" diff --git a/tests/test_construct.py b/tests/test_construct.py index e4234db..94ae71f 100755 --- a/tests/test_construct.py +++ b/tests/test_construct.py @@ -101,6 +101,9 @@ final_application=0x0200, global_service=0x0100, receipt_generation=0x80, ciphered_load_file_data_block=0x40, contactless_activation=0x20, contactless_self_activation=0x10) + IntegerSteps = StripTrailerAdapter(GreedyBytes, 4, steps = [2,4]) + Integer = StripTrailerAdapter(GreedyBytes, 4) + examples = ['00', '80', '8040', '400010'] def test_encdec(self): for e in self.examples: @@ -108,6 +111,25 @@ reenc = self.Privileges.build(dec) self.assertEqual(e, b2h(reenc)) + def test_encdec_integer(self): + enc = self.IntegerSteps.build(0x10000000) + self.assertEqual(b2h(enc), '1000') + enc = self.IntegerSteps.build(0x10200000) + self.assertEqual(b2h(enc), '1020') + enc = self.IntegerSteps.build(0x10203000) + self.assertEqual(b2h(enc), '10203000') + enc = self.IntegerSteps.build(0x10203040) + self.assertEqual(b2h(enc), '10203040') + + enc = self.Integer.build(0x10000000) + self.assertEqual(b2h(enc), '10') + enc = self.Integer.build(0x10200000) + self.assertEqual(b2h(enc), '1020') + enc = self.Integer.build(0x10203000) + self.assertEqual(b2h(enc), '102030') + enc = self.Integer.build(0x10203040) + self.assertEqual(b2h(enc), '10203040') + def test_enc(self): enc = self.Privileges.build({'dap_verification' : True}) self.assertEqual(b2h(enc), '40') @@ -124,6 +146,31 @@ self.assertEqual(b2h(enc), '400110') +class TestStripHeaderAdapter(unittest.TestCase): + + IntegerSteps = StripHeaderAdapter(GreedyBytes, 4, steps = [2,4]) + Integer = StripHeaderAdapter(GreedyBytes, 4) + + def test_encdec_integer_reverse(self): + enc = self.IntegerSteps.build(0x40) + self.assertEqual(b2h(enc), '0040') + enc = self.IntegerSteps.build(0x3040) + self.assertEqual(b2h(enc), '3040') + enc = self.IntegerSteps.build(0x203040) + self.assertEqual(b2h(enc), '00203040') + enc = self.IntegerSteps.build(0x10203040) + self.assertEqual(b2h(enc), '10203040') + + enc = self.Integer.build(0x40) + self.assertEqual(b2h(enc), '40') + enc = self.Integer.build(0x3040) + self.assertEqual(b2h(enc), '3040') + enc = self.Integer.build(0x203040) + self.assertEqual(b2h(enc), '203040') + enc = self.Integer.build(0x10203040) + self.assertEqual(b2h(enc), '10203040') + + class TestAdapters(unittest.TestCase): def test_dns_adapter(self): ad = DnsAdapter(GreedyBytes) -- To view, visit https://gerrit.osmocom.org/c/python/pyosmocom/+/39193?usp=email To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings?usp=email Gerrit-MessageType: merged Gerrit-Project: python/pyosmocom Gerrit-Branch: master Gerrit-Change-Id: I1a9fff17abbbef0c5f6d45f58198debfa12e78b6 Gerrit-Change-Number: 39193 Gerrit-PatchSet: 5 Gerrit-Owner: dexter <pmaier(a)sysmocom.de> Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: fixeria <vyanitskiy(a)sysmocom.de> Gerrit-Reviewer: laforge <laforge(a)osmocom.org> Gerrit-Reviewer: pespin <pespin(a)sysmocom.de>
1 0
0 0
  • ← Newer
  • 1
  • ...
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • ...
  • 150
  • Older →

HyperKitty Powered by HyperKitty version 1.3.12.