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
65d9a80b
Commit
65d9a80b
authored
3 years ago
by
Piotr Maślanka
Browse files
Options
Downloads
Patches
Plain Diff
bugfix
parent
23943514
No related branches found
Branches containing commit
Tags
v2.18.2
Tags containing commit
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
CHANGELOG.md
+2
-4
2 additions, 4 deletions
CHANGELOG.md
satella/__init__.py
+1
-1
1 addition, 1 deletion
satella/__init__.py
satella/parsing.py
+27
-11
27 additions, 11 deletions
satella/parsing.py
tests/test_parsing.py
+5
-0
5 additions, 0 deletions
tests/test_parsing.py
with
35 additions
and
16 deletions
CHANGELOG.md
+
2
−
4
View file @
65d9a80b
# v2.18.
1
# v2.18.
2
*
added
`BinaryParser.get_remaining_bytes`
*
bugfix
*
added
`BinaryParser.get_remaining_bytes_count`
*
added
`BinaryParser.get_parser`
This diff is collapsed.
Click to expand it.
satella/__init__.py
+
1
−
1
View file @
65d9a80b
__version__
=
'
2.18.
1
'
__version__
=
'
2.18.
2
'
This diff is collapsed.
Click to expand it.
satella/parsing.py
+
27
−
11
View file @
65d9a80b
...
@@ -27,7 +27,7 @@ class BinaryParser:
...
@@ -27,7 +27,7 @@ class BinaryParser:
"""
"""
Return the amount of bytes remaining. This will not advance the pointer
Return the amount of bytes remaining. This will not advance the pointer
"""
"""
return
self
.
length
-
self
.
pointer
return
self
.
length
-
self
.
pointer
+
self
.
init_ofs
def
__init__
(
self
,
b_stream
:
tp
.
Union
[
bytes
,
bytearray
],
offset
:
int
=
0
,
def
__init__
(
self
,
b_stream
:
tp
.
Union
[
bytes
,
bytearray
],
offset
:
int
=
0
,
length
:
tp
.
Optional
[
int
]
=
None
):
length
:
tp
.
Optional
[
int
]
=
None
):
...
@@ -41,6 +41,26 @@ class BinaryParser:
...
@@ -41,6 +41,26 @@ class BinaryParser:
def
__bytes__
(
self
)
->
bytes
:
def
__bytes__
(
self
)
->
bytes
:
return
self
.
b_stream
[
self
.
init_ofs
:
self
.
init_ofs
+
self
.
length
]
return
self
.
b_stream
[
self
.
init_ofs
:
self
.
init_ofs
+
self
.
length
]
def
skip
(
self
,
n
:
int
)
->
None
:
"""
Advance the pointer by n bytes
:param n: bytes to advance
:raises NotEnoughBytes: not enough bytes remain in the stream!
"""
self
.
assert_has_bytes
(
n
)
self
.
pointer
+=
n
def
assert_has_bytes
(
self
,
n
:
int
)
->
None
:
"""
Assert that we have at least n bytes to consume
:param n: amount of bytes to consume
:raises NotEnoughBytes: not enough bytes remain in the stream!
"""
if
self
.
length
+
self
.
init_ofs
<
self
.
pointer
+
n
:
raise
NotEnoughBytes
(
'
Not enough bytes
'
)
def
get_parser
(
self
,
length
:
int
)
->
'
BinaryParser
'
:
def
get_parser
(
self
,
length
:
int
)
->
'
BinaryParser
'
:
"""
"""
Return a subclassed binary parser providing a window to another binary parser
'
s data.
Return a subclassed binary parser providing a window to another binary parser
'
s data.
...
@@ -49,9 +69,9 @@ class BinaryParser:
...
@@ -49,9 +69,9 @@ class BinaryParser:
:param length: amount of bytes to view
:param length: amount of bytes to view
:return: a BinaryParser
:return: a BinaryParser
:raises NotEnoughBytes: not enough bytes remain in the stream!
"""
"""
if
self
.
length
<
self
.
pointer
+
length
:
self
.
assert_has_bytes
(
length
)
raise
NotEnoughBytes
(
'
Not enough bytes
'
)
try
:
try
:
return
BinaryParser
(
self
.
b_stream
,
self
.
pointer
,
length
)
return
BinaryParser
(
self
.
b_stream
,
self
.
pointer
,
length
)
finally
:
finally
:
...
@@ -85,8 +105,7 @@ class BinaryParser:
...
@@ -85,8 +105,7 @@ class BinaryParser:
:return: bytes returned
:return: bytes returned
:raises NotEnoughBytes: not enough bytes remain in the stream!
:raises NotEnoughBytes: not enough bytes remain in the stream!
"""
"""
if
self
.
length
<
self
.
pointer
+
n
:
self
.
assert_has_bytes
(
n
)
raise
NotEnoughBytes
(
'
Not enough bytes
'
)
try
:
try
:
return
self
.
b_stream
[
self
.
pointer
:
self
.
pointer
+
n
]
return
self
.
b_stream
[
self
.
pointer
:
self
.
pointer
+
n
]
finally
:
finally
:
...
@@ -115,9 +134,7 @@ class BinaryParser:
...
@@ -115,9 +134,7 @@ class BinaryParser:
'
get_structs for multiples!
'
'
get_structs for multiples!
'
st_len
=
st
.
size
st_len
=
st
.
size
if
self
.
length
<
self
.
pointer
+
st_len
:
self
.
assert_has_bytes
(
st_len
)
raise
NotEnoughBytes
(
'
Not enough bytes
'
)
try
:
try
:
return
st
.
unpack
(
self
.
b_stream
[
self
.
pointer
:
self
.
pointer
+
st_len
])[
0
]
return
st
.
unpack
(
self
.
b_stream
[
self
.
pointer
:
self
.
pointer
+
st_len
])[
0
]
finally
:
finally
:
...
@@ -136,8 +153,7 @@ class BinaryParser:
...
@@ -136,8 +153,7 @@ class BinaryParser:
"""
"""
st
=
self
.
_to_struct
(
st
)
st
=
self
.
_to_struct
(
st
)
st_len
=
st
.
size
st_len
=
st
.
size
if
self
.
length
<
self
.
pointer
+
st_len
:
self
.
assert_has_bytes
(
st_len
)
raise
NotEnoughBytes
(
'
Not enough bytes
'
)
try
:
try
:
return
st
.
unpack
(
self
.
b_stream
[
self
.
pointer
:
self
.
pointer
+
st_len
])
return
st
.
unpack
(
self
.
b_stream
[
self
.
pointer
:
self
.
pointer
+
st_len
])
finally
:
finally
:
...
@@ -149,4 +165,4 @@ class BinaryParser:
...
@@ -149,4 +165,4 @@ class BinaryParser:
This will not advance the pointer
This will not advance the pointer
"""
"""
return
self
.
b_stream
[
self
.
pointer
:]
return
self
.
b_stream
[
self
.
pointer
:
self
.
pointer
+
self
.
length
]
This diff is collapsed.
Click to expand it.
tests/test_parsing.py
+
5
−
0
View file @
65d9a80b
...
@@ -23,3 +23,8 @@ class TestParsing(unittest.TestCase):
...
@@ -23,3 +23,8 @@ class TestParsing(unittest.TestCase):
self
.
assertEqual
(
bp2
.
get_struct
(
'
>L
'
),
258
)
self
.
assertEqual
(
bp2
.
get_struct
(
'
>L
'
),
258
)
self
.
assertRaises
(
NotEnoughBytes
,
lambda
:
bp2
.
get_struct
(
'
>L
'
))
self
.
assertRaises
(
NotEnoughBytes
,
lambda
:
bp2
.
get_struct
(
'
>L
'
))
self
.
assertEqual
(
bp
.
pointer
,
4
)
self
.
assertEqual
(
bp
.
pointer
,
4
)
bp
.
reset
()
bp
.
skip
(
4
)
bp3
=
bp
.
get_parser
(
4
)
self
.
assertEqual
(
bytes
(
bp3
),
b
'
\x00\x00\x00\xFF
'
)
self
.
assertRaises
(
NotEnoughBytes
,
lambda
:
bp2
.
get_parser
(
4
))
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