pcapGlobalHeader(endianness?: PcapEndianness): Coder<PcapGlobalHeader>
Creates a coder for the pcap global header in the requested byte order.
Encode and decode a little-endian global header
Encode and decode a little-endian global header
import { assertEquals } from "@std/assert"; import { pcapGlobalHeader, PCAP_MAGIC_MICROS, LINKTYPE, } from "@binstruct/pcap"; const header = pcapGlobalHeader("le"); const value = { magic: PCAP_MAGIC_MICROS, versionMajor: 2, versionMinor: 4, thisZone: 0, sigFigs: 0, snapLen: 65535, network: LINKTYPE.ETHERNET, }; const buffer = new Uint8Array(24); const written = header.encode(value, buffer); const [decoded, read] = header.decode(buffer); assertEquals(written, 24); assertEquals(read, 24); assertEquals(decoded, value);
Big-endian header round trip
Big-endian header round trip
import { assertEquals } from "@std/assert"; import { pcapGlobalHeader, PCAP_MAGIC_MICROS } from "@binstruct/pcap"; const header = pcapGlobalHeader("be"); const value = { magic: PCAP_MAGIC_MICROS, versionMajor: 2, versionMinor: 4, thisZone: 0, sigFigs: 0, snapLen: 1500, network: 1, }; const buffer = new Uint8Array(24); const written = header.encode(value, buffer); const [decoded] = header.decode(buffer); assertEquals(written, 24); assertEquals(decoded.magic, PCAP_MAGIC_MICROS); assertEquals(decoded.snapLen, 1500);
Omitting the argument round-trips using the default byte order
Omitting the argument round-trips using the default byte order
import { assertEquals } from "@std/assert"; import { detectPcapMagic, LINKTYPE, PCAP_MAGIC_MICROS, pcapGlobalHeader, } from "@binstruct/pcap"; const header = pcapGlobalHeader(); const value = { magic: PCAP_MAGIC_MICROS, versionMajor: 2, versionMinor: 4, thisZone: 0, sigFigs: 0, snapLen: 65535, network: LINKTYPE.ETHERNET, }; const buffer = new Uint8Array(24); const written = header.encode(value, buffer); const [decoded, read] = header.decode(buffer); assertEquals(written, 24); assertEquals(read, 24); assertEquals(decoded, value); assertEquals(detectPcapMagic(buffer), { endianness: "le", nanos: false });
optional
endianness: PcapEndianness = PCAP_DEFAULT_ENDIANNESS
Byte order used for the multi-byte fields. Pass "le" or
"be" to match the file you are reading or producing. Defaults to
PCAP_DEFAULT_ENDIANNESS. This coder is fixed to one byte order —
to decode a header of unknown endianness, probe it with
detectPcapMagic first, or use pcapFile, which does that
for you.
Coder<PcapGlobalHeader>
A coder that encodes/decodes a PcapGlobalHeader.