Core GHC compression and decompression engine per RFC 7400. More...
#include "sixlowpan-ghc.h"
Static Public Member Functions | |
| static GhcBytecodeType | ClassifyBytecode (uint8_t byte) |
| Classify a GHC bytecode instruction. | |
| static uint32_t | Compress (const Ipv6Address &srcAddr, const Ipv6Address &dstAddr, const uint8_t *input, uint32_t inputLen, uint8_t *output, uint32_t outputMaxLen, bool emitStopCode=false) |
| Compress a byte stream using the GHC LZ77 algorithm. | |
| static uint32_t | Decompress (const Ipv6Address &srcAddr, const Ipv6Address &dstAddr, const uint8_t *compressed, uint32_t compressedLen, uint8_t *output, uint32_t outputMaxLen, bool useStopCode=false) |
| Decompress a GHC-compressed byte stream. | |
Static Public Attributes | |
| static constexpr uint32_t | DICTIONARY_SIZE = 48 |
| Predefined dictionary size (48 bytes). | |
| static constexpr uint32_t | MAX_BACKREF_LEN = 17 |
| Maximum copy length of one 11nnnkkk backreference. | |
| static constexpr uint32_t | MAX_LITERAL_RUN = 95 |
| Maximum bytes copied by one 0kkkkkkk literal bytecode. | |
| static constexpr uint32_t | MAX_ZERO_RUN = 17 |
| Maximum zeros inserted by one 1000nnnn bytecode: n+2 with n <= 15. | |
Static Private Member Functions | |
| static uint32_t | CountBackrefExtArgBytes (uint32_t matchLength, uint32_t matchOffset) |
| Count the extended-args bytes a backreference encoding needs. | |
| static uint32_t | CountZeros (const uint8_t *input, uint32_t remaining) |
| Count consecutive zero bytes in input. | |
| static bool | EmitBackref (uint8_t *output, uint32_t &outPos, uint32_t outputMaxLen, uint32_t matchLength, uint32_t matchOffset) |
| Emit a backreference bytecode (with optional extended args). | |
| static bool | FindLongestMatch (const uint8_t *buffer, uint32_t bufLen, const uint8_t *input, uint32_t inputRemaining, uint32_t &matchOffset, uint32_t &matchLength) |
| Find the longest match in the buffer for LZ77 compression. | |
| static void | InitDictionary (uint8_t *dict, const Ipv6Address &srcAddr, const Ipv6Address &dstAddr) |
| Initialize the 48-byte decompression dictionary. | |
Core GHC compression and decompression engine per RFC 7400.
This class implements the LZ77-style bytecode compression defined in RFC 7400 Section 3. The engine operates on raw byte buffers and is used by the GHC header classes for actual data transformation.
The decompression buffer is pre-initialized with a 48-byte dictionary: Bytes 0-15: Source IPv6 address from the packet Bytes 16-31: Destination IPv6 address from the packet Bytes 32-47: Static dictionary (16 bytes from RFC 7400 Section 3.1)
The static dictionary contains: 0x16 0xfe 0xfd 0x17 0xfe 0xfd 0x00 0x01 0x00 0x00 0x00 0x00 0x00 0x01 0x00 0x00
Definition at line 71 of file sixlowpan-ghc.h.
|
static |
Classify a GHC bytecode instruction.
| [in] | byte | The bytecode byte to classify. |
Definition at line 65 of file sixlowpan-ghc.cc.
References ns3::BACKREF, ns3::EXTENDED_ARGS, ns3::LITERAL, ns3::RESERVED, ns3::STOP_CODE, and ns3::ZERO_INSERT.
Referenced by Decompress(), and SixlowpanGhcEngineTest::DoRun().
|
static |
Compress a byte stream using the GHC LZ77 algorithm.
The compressor scans the dictionary and previously output data for the longest match, then emits backreference bytecodes. This is a full LZ77 compressor implementation.
| [in] | srcAddr | Source IPv6 address for dictionary initialization. |
| [in] | dstAddr | Destination IPv6 address for dictionary initialization. |
| [in] | input | Pointer to uncompressed data. |
| [in] | inputLen | Length of uncompressed data. |
| [out] | output | Buffer to receive compressed bytecodes. |
| [in] | outputMaxLen | Maximum output buffer size. |
| [in] | emitStopCode | If true, append Stop Code at end (for extension headers). |
Definition at line 446 of file sixlowpan-ghc.cc.
References CountZeros(), DICTIONARY_SIZE, EmitBackref(), FindLongestMatch(), InitDictionary(), MAX_LITERAL_RUN, MAX_ZERO_RUN, NS_LOG_DEBUG, and NS_LOG_FUNCTION.
Referenced by ns3::SixLowPanNetDevice::CompressLowPanGhcIcmpv6(), ns3::SixLowPanNetDevice::CompressLowPanGhcNhc(), SixlowpanGhcEngineTest::DoRun(), and SixlowpanGhcAppendixATest::RunVector().
|
staticprivate |
Count the extended-args bytes a backreference encoding needs.
Each 101nssss extended-args byte carries up to 15*8 = 120 of the start-adjust and 8 of the number-adjust accumulator (RFC 7400 Section 3.2).
| [in] | matchLength | Number of bytes to copy (>= 2). |
| [in] | matchOffset | Offset from current position (>= matchLength). |
Definition at line 270 of file sixlowpan-ghc.cc.
Referenced by EmitBackref(), and FindLongestMatch().
|
staticprivate |
Count consecutive zero bytes in input.
| [in] | input | Pointer to current input position. |
| [in] | remaining | Remaining bytes in input. |
Definition at line 259 of file sixlowpan-ghc.cc.
Referenced by Compress().
|
static |
Decompress a GHC-compressed byte stream.
| [in] | srcAddr | Source IPv6 address for dictionary initialization. |
| [in] | dstAddr | Destination IPv6 address for dictionary initialization. |
| [in] | compressed | Pointer to compressed bytecode data. |
| [in] | compressedLen | Length of compressed data. |
| [out] | output | Buffer to receive decompressed output. |
| [in] | outputMaxLen | Maximum output buffer size. |
| [in] | useStopCode | If true, decompression terminates on Stop Code (used for extension headers where Length is elided). |
Definition at line 100 of file sixlowpan-ghc.cc.
References ns3::BACKREF, ClassifyBytecode(), DICTIONARY_SIZE, ns3::EXTENDED_ARGS, InitDictionary(), ns3::LITERAL, MAX_LITERAL_RUN, NS_LOG_DEBUG, NS_LOG_FUNCTION, NS_LOG_WARN, ns3::RESERVED, ns3::STOP_CODE, and ns3::ZERO_INSERT.
Referenced by ns3::SixLowPanNetDevice::DecompressLowPanGhcIcmpv6(), ns3::SixLowPanNetDevice::DecompressLowPanGhcNhc(), SixlowpanGhcEngineTest::DoRun(), and SixlowpanGhcAppendixATest::RunVector().
|
staticprivate |
Emit a backreference bytecode (with optional extended args).
| [out] | output | Output bytecode buffer. |
| [in,out] | outPos | Current output position (advanced on write). |
| [in] | outputMaxLen | Maximum output buffer size. |
| [in] | matchLength | Number of bytes to copy (>= 2). |
| [in] | matchOffset | Offset from current position (>= matchLength). |
Definition at line 354 of file sixlowpan-ghc.cc.
References CountBackrefExtArgBytes(), NS_ASSERT, and NS_LOG_WARN.
Referenced by Compress().
|
staticprivate |
Find the longest match in the buffer for LZ77 compression.
Searches backward from the current position for the longest substring match, respecting GHC backreference encoding limits.
| [in] | buffer | Full buffer (dictionary + output so far). |
| [in] | bufLen | Current buffer length. |
| [in] | input | Input data at current position. |
| [in] | inputRemaining | Remaining input bytes. |
| [out] | matchOffset | Offset of best match (bytes back from bufLen). |
| [out] | matchLength | Length of best match. |
Definition at line 293 of file sixlowpan-ghc.cc.
References CountBackrefExtArgBytes(), and MAX_BACKREF_LEN.
Referenced by Compress().
|
staticprivate |
Initialize the 48-byte decompression dictionary.
| [out] | dict | Buffer to receive dictionary (must be >= 48 bytes). |
| [in] | srcAddr | Source IPv6 address. |
| [in] | dstAddr | Destination IPv6 address. |
Definition at line 34 of file sixlowpan-ghc.cc.
References ns3::Ipv6Address::GetBytes().
Referenced by Compress(), and Decompress().
|
staticconstexpr |
Predefined dictionary size (48 bytes).
16 bytes source address + 16 bytes destination address + 16 bytes static.
Definition at line 79 of file sixlowpan-ghc.h.
Referenced by Compress(), and Decompress().
|
staticconstexpr |
Maximum copy length of one 11nnnkkk backreference.
n = nnn (max 7) + na (max 8 from one extended-args byte) + 2 = 17. Longer repeats become consecutive backreferences.
Definition at line 99 of file sixlowpan-ghc.h.
Referenced by FindLongestMatch().
|
staticconstexpr |
Maximum bytes copied by one 0kkkkkkk literal bytecode.
k is in [1, 95]; values 96-127 are reserved (RFC 7400 Section 3).
Definition at line 86 of file sixlowpan-ghc.h.
Referenced by Compress(), and Decompress().
|
staticconstexpr |
Maximum zeros inserted by one 1000nnnn bytecode: n+2 with n <= 15.
Definition at line 91 of file sixlowpan-ghc.h.
Referenced by Compress().