mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-14 05:11:19 -04:00
remove PacketPaddingContainer abstraction
This commit is contained in:
parent
955cbc847a
commit
1bc310c2e9
6 changed files with 42 additions and 62 deletions
|
@ -1,22 +1,36 @@
|
|||
// Package packetpadding is an package that defines methods to pad packets
|
||||
// with a given number of bytes, and to unpack the padding from a padded packet.
|
||||
// The packet format is as follows if the desired output length is greater than
|
||||
// 2 bytes:
|
||||
// | data | padding | data length |
|
||||
// The data length is a 16-bit big-endian integer that represents the length of
|
||||
// the data in bytes.
|
||||
// If the desired output length is 2 bytes or less, the packet format is as
|
||||
// follows:
|
||||
// | padding |
|
||||
// No payload will be included in the packet.
|
||||
package packetpadding
|
||||
|
||||
import "encoding/binary"
|
||||
|
||||
func New() PacketPaddingContainer {
|
||||
return packetPaddingContainer{}
|
||||
}
|
||||
|
||||
type packetPaddingContainer struct {
|
||||
}
|
||||
|
||||
func (c packetPaddingContainer) Pack(data_OWNERSHIP_RELINQUISHED []byte, paddingLength int) []byte {
|
||||
// Pack pads the given data with the given number of bytes, and appends the
|
||||
// length of the data to the end of the data. The returned byte slice
|
||||
// contains the padded data.
|
||||
// This generates a packet with a length of
|
||||
// len(data_OWNERSHIP_RELINQUISHED) + padding + 2
|
||||
// @param data_OWNERSHIP_RELINQUISHED - The payload, this reference is consumed and should not be used after this call.
|
||||
// @param padding - The number of padding bytes to add to the data.
|
||||
func Pack(data_OWNERSHIP_RELINQUISHED []byte, paddingLength int) []byte {
|
||||
data := append(data_OWNERSHIP_RELINQUISHED, make([]byte, paddingLength)...)
|
||||
dataLength := len(data_OWNERSHIP_RELINQUISHED)
|
||||
data = binary.BigEndian.AppendUint16(data, uint16(dataLength))
|
||||
return data
|
||||
}
|
||||
|
||||
func (c packetPaddingContainer) Pad(paddingLength int) []byte {
|
||||
// Pad returns a padding packet of padding length.
|
||||
// If the padding length is less than 0, nil is returned.
|
||||
// @param padding - The number of padding bytes to add to the data.
|
||||
func Pad(paddingLength int) []byte {
|
||||
if assertPaddingLengthIsNotNegative := paddingLength < 0; assertPaddingLengthIsNotNegative {
|
||||
return nil
|
||||
}
|
||||
|
@ -33,7 +47,11 @@ func (c packetPaddingContainer) Pad(paddingLength int) []byte {
|
|||
|
||||
}
|
||||
|
||||
func (c packetPaddingContainer) Unpack(wrappedData_OWNERSHIP_RELINQUISHED []byte) ([]byte, int) {
|
||||
// Unpack extracts the data and padding from the given padded data. It
|
||||
// returns the data and the number of padding bytes.
|
||||
// the data may be nil.
|
||||
// @param wrappedData_OWNERSHIP_RELINQUISHED - The packet, this reference is consumed and should not be used after this call.
|
||||
func Unpack(wrappedData_OWNERSHIP_RELINQUISHED []byte) ([]byte, int) {
|
||||
dataLength := len(wrappedData_OWNERSHIP_RELINQUISHED)
|
||||
if dataLength < 2 {
|
||||
return nil, dataLength
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue