Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
satella
Manage
Activity
Members
Labels
Plan
Issues
1
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
public
satella
Commits
cbca0b1f
Commit
cbca0b1f
authored
3 years ago
by
Piotr Maślanka
Browse files
Options
Downloads
Patches
Plain Diff
* added `BinaryParser.get_remaining_bytes`
* added `BinaryParser.get_remaining_bytes_count`
parent
95001983
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
CHANGELOG.md
+3
-0
3 additions, 0 deletions
CHANGELOG.md
satella/__init__.py
+1
-1
1 addition, 1 deletion
satella/__init__.py
satella/parsing.py
+24
-1
24 additions, 1 deletion
satella/parsing.py
tests/test_parsing.py
+2
-0
2 additions, 0 deletions
tests/test_parsing.py
with
30 additions
and
2 deletions
CHANGELOG.md
+
3
−
0
View file @
cbca0b1f
# v2.18.1
*
added
`BinaryParser.get_remaining_bytes`
*
added
`BinaryParser.get_remaining_bytes_count`
This diff is collapsed.
Click to expand it.
satella/__init__.py
+
1
−
1
View file @
cbca0b1f
__version__
=
'
2.18.1a
1
'
__version__
=
'
2.18.1a
2
'
This diff is collapsed.
Click to expand it.
satella/parsing.py
+
24
−
1
View file @
cbca0b1f
...
...
@@ -6,7 +6,9 @@ from satella.exceptions import NotEnoughBytes
class
BinaryParser
:
"""
A class that allows parsing binary streams easily
A class that allows parsing binary streams easily.
This supports __len__ to return the amount of bytes remaining.
:param b_stream: an object that allows indiced access, and allows subscripts to
span ranges, which will return items parseable by struct
...
...
@@ -15,6 +17,15 @@ class BinaryParser:
:ivar offset: offset from which bytes will be readed
"""
def
__len__
(
self
)
->
int
:
return
self
.
get_remaining_bytes_count
()
def
get_remaining_bytes_count
(
self
)
->
int
:
"""
Return the amount of bytes remaining. This will not advance the pointer
"""
return
self
.
stream_length
-
self
.
pointer
def
__init__
(
self
,
b_stream
:
tp
.
Union
[
bytes
,
bytearray
],
offset
:
int
=
0
):
self
.
b_stream
=
b_stream
self
.
struct_cache
=
{}
...
...
@@ -57,6 +68,8 @@ class BinaryParser:
This must be a single-character struct!
This will advance the pointer by size of st.
:param st: a single-character struct.Struct or a single character struct specification
:return: a value returned from it
:raises NotEnoughBytes: not enough bytes remain in the stream!
...
...
@@ -85,6 +98,8 @@ class BinaryParser:
"""
Try to obtain as many bytes as this struct requires and return them parsed.
This will advance the pointer by size of st.
:param st: a struct.Struct or a multi character struct specification
:return: a tuple of un-parsed values
:raises NotEnoughBytes: not enough bytes remain in the stream!
...
...
@@ -97,3 +112,11 @@ class BinaryParser:
return
st
.
unpack
(
self
.
b_stream
[
self
.
pointer
:
self
.
pointer
+
st_len
])
finally
:
self
.
pointer
+=
st_len
def
get_remaining_bytes
(
self
)
->
tp
.
Union
[
bytes
,
bytearray
]:
"""
Return the remaining bytes.
This will not advance the pointer
"""
return
self
.
b_stream
[
self
.
pointer
:]
This diff is collapsed.
Click to expand it.
tests/test_parsing.py
+
2
−
0
View file @
cbca0b1f
...
...
@@ -14,3 +14,5 @@ class TestParsing(unittest.TestCase):
self
.
assertRaises
(
NotEnoughBytes
,
lambda
:
bp
.
get_struct
(
'
>L
'
))
self
.
assertRaises
(
NotEnoughBytes
,
lambda
:
bp
.
get_bytes
(
5
))
self
.
assertRaises
(
NotEnoughBytes
,
lambda
:
BinaryParser
(
b
''
,
1
))
self
.
assertEqual
(
bp
.
get_remaining_bytes
(),
b
'
\x00
'
)
self
.
assertEqual
(
len
(
bp
),
1
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment