Skip to content

Commit dd7fb2c

Browse files
committed
Pass the endianness to all writing locations
Issue: rshk#41
1 parent cd0f197 commit dd7fb2c

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

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

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"""

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)