Skip to content

Commit 3e13e64

Browse files
authored
Merge pull request #42 from gmacon/41-write-big-endian
Pass the endianness to all writing locations
2 parents cd0f197 + ddcae68 commit 3e13e64

File tree

9 files changed

+30
-20
lines changed

9 files changed

+30
-20
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ repos:
1212
additional_dependencies:
1313
- toml
1414
- repo: https://github.com/psf/black
15-
rev: 20.8b1
15+
rev: 22.3.0
1616
hooks:
1717
- id: black
1818
- repo: https://gitlab.com/pycqa/flake8

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
History
22
#######
33

4+
Unreleased
5+
==========
6+
7+
- Use the explicit endianness everywhere when writing.
8+
49
v0.1
510
====
611

examples/dump_tcpip_stats.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def human_number(num, k=1000):
3434
assert isinstance(num, int)
3535
for i, suffix in enumerate(powers):
3636
if (num < (k ** (i + 1))) or (i == len(powers) - 1):
37-
return "{0:d}{1}".format(int(round(num / (k ** i))), suffix)
37+
return "{0:d}{1}".format(int(round(num / (k**i))), suffix)
3838
raise AssertionError("Should never reach this")
3939

4040

pcapng/blocks.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ def __init__(self, **kwargs):
5454
for key, packed_type, default in self.schema:
5555
if key == "options":
5656
self._decoded[key] = Options(
57-
schema=packed_type.options_schema, data={}, endianness="="
57+
schema=packed_type.options_schema,
58+
data={},
59+
endianness=kwargs["endianness"],
5860
)
5961
if "options" in kwargs:
6062
for oky, ovl in kwargs["options"].items():
@@ -83,10 +85,10 @@ def _write(self, outstream):
8385
block_length = 12 + subblock_length
8486
if subblock_length % 4 != 0:
8587
block_length += 4 - (subblock_length % 4)
86-
write_int(self.magic_number, outstream, 32)
87-
write_int(block_length, outstream, 32)
88+
write_int(self.magic_number, outstream, 32, endianness=self.section.endianness)
89+
write_int(block_length, outstream, 32, endianness=self.section.endianness)
8890
write_bytes_padded(outstream, encoded_block)
89-
write_int(block_length, outstream, 32)
91+
write_int(block_length, outstream, 32, endianness=self.section.endianness)
9092

9193
def _encode(self, outstream):
9294
"""Encodes the fields of this block into raw data"""

pcapng/flags.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,14 @@ def __init__(self, owner, offset, size, extra=None):
8484
"{cls} needs an iterable of values".format(cls=self.__class__.__name__)
8585
)
8686
extra = list(extra)
87-
if len(extra) > 2 ** size:
87+
if len(extra) > 2**size:
8888
raise TypeError(
8989
"{cls} iterable has too many values (got {got}, "
9090
"{size} bits only address {max})".format(
9191
cls=self.__class__.__name__,
9292
got=len(extra),
9393
size=size,
94-
max=2 ** size,
94+
max=2**size,
9595
)
9696
)
9797

tests/test_parse_enhanced_packet.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def test_read_block_enhanced_packet_bigendian():
9393
def _generate_file_with_tsresol(base, exponent):
9494
tsresol = pack_timestamp_resolution(base, exponent)
9595
base_timestamp = 1420070400.0 # 2015-01-01 00:00 UTC
96-
timestamp = base_timestamp / (base ** exponent)
96+
timestamp = base_timestamp / (base**exponent)
9797

9898
stream = io.BytesIO()
9999

@@ -167,6 +167,6 @@ def test_read_block_enhanced_packet_tsresol_bigendian(tsr_base, tsr_exp):
167167
assert blocks[2].interface_id == 0
168168
assert blocks[2].interface == blocks[1]
169169

170-
resol = tsr_base ** tsr_exp
170+
resol = tsr_base**tsr_exp
171171
assert blocks[2].timestamp_resolution == resol
172172
assert blocks[2].timestamp == 1420070400.0

tests/test_parse_wireshark_capture_files.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def test_sample_test005_ntar():
105105
blocks[1].options.get_raw("if_speed") == b"\x00\xe4\x0b\x54\x02\x00\x00\x00"
106106
) # noqa
107107
assert blocks[1].options["if_speed"] == 0x00000002540BE400
108-
assert blocks[1].options["if_speed"] == (10 ** 10) # 10Gbit
108+
assert blocks[1].options["if_speed"] == (10**10) # 10Gbit
109109

110110
assert blocks[1].options["if_description"] == "Stupid ethernet interface\x00"
111111

@@ -143,7 +143,7 @@ def test_sample_test006_ntar(filename):
143143
assert blocks[1].snaplen == 96
144144
assert len(blocks[1].options) == 2
145145

146-
assert blocks[1].options["if_speed"] == (10 ** 8) # 100Mbit
146+
assert blocks[1].options["if_speed"] == (10**8) # 100Mbit
147147

148148
assert blocks[1].options["if_description"] == "Stupid ethernet interface\x00"
149149

tests/test_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ def test_unpack_tsresol():
3838
assert unpack_timestamp_resolution(bytes((100,))) == 1e-100
3939

4040
assert unpack_timestamp_resolution(bytes((0 | 0b10000000,))) == 1
41-
assert unpack_timestamp_resolution(bytes((1 | 0b10000000,))) == 2 ** -1
42-
assert unpack_timestamp_resolution(bytes((6 | 0b10000000,))) == 2 ** -6
43-
assert unpack_timestamp_resolution(bytes((100 | 0b10000000,))) == 2 ** -100
41+
assert unpack_timestamp_resolution(bytes((1 | 0b10000000,))) == 2**-1
42+
assert unpack_timestamp_resolution(bytes((6 | 0b10000000,))) == 2**-6
43+
assert unpack_timestamp_resolution(bytes((100 | 0b10000000,))) == 2**-100
4444

4545

4646
def test_pack_tsresol():

tests/test_write_support.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,19 @@ def compare_blocklists(list1, list2):
1717
assert list1[i] == list2[i], "block #{} mismatch".format(i)
1818

1919

20-
def test_write_read_all_blocks():
20+
@pytest.mark.parametrize("endianness", ["<", ">"])
21+
def test_write_read_all_blocks(endianness):
2122
# Track the blocks we're writing
2223
out_blocks = []
2324

2425
# Build our original/output session
2526
o_shb = blocks.SectionHeader(
27+
endianness=endianness,
2628
options={
2729
"shb_hardware": "pytest",
2830
"shb_os": "python",
2931
"shb_userappl": "python-pcapng",
30-
}
32+
},
3133
)
3234
out_blocks.append(o_shb)
3335

@@ -129,7 +131,8 @@ def test_write_read_all_blocks():
129131
compare_blocklists(in_blocks, out_blocks)
130132

131133

132-
def test_spb_snap_lengths():
134+
@pytest.mark.parametrize("endianness", ["<", ">"])
135+
def test_spb_snap_lengths(endianness):
133136
"""
134137
Simple Packet Blocks present a unique challenge in parsing. The packet does not
135138
contain an explicit "captured length" indicator, only the original observed
@@ -147,7 +150,7 @@ def test_spb_snap_lengths():
147150
data = bytes(range(0, 256))
148151

149152
# First session: no snap length
150-
o_shb = blocks.SectionHeader()
153+
o_shb = blocks.SectionHeader(endianness=endianness)
151154
o_idb = o_shb.new_member(blocks.InterfaceDescription) # noqa: F841
152155
o_blk1 = o_shb.new_member(blocks.SimplePacket, packet_data=data)
153156

@@ -162,7 +165,7 @@ def test_spb_snap_lengths():
162165
assert i_blk1.packet_data == data
163166

164167
# Second session: with snap length
165-
o_shb = blocks.SectionHeader()
168+
o_shb = blocks.SectionHeader(endianness=endianness)
166169
o_idb = o_shb.new_member(blocks.InterfaceDescription, snaplen=32) # noqa: F841
167170
o_blk1 = o_shb.new_member(blocks.SimplePacket, packet_data=data[:16])
168171
o_blk2 = o_shb.new_member(blocks.SimplePacket, packet_data=data[:32])

0 commit comments

Comments
 (0)