github.com/pion/dtls/v2@v2.2.12/pkg/protocol/compression_method.go (about)

     1  // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
     2  // SPDX-License-Identifier: MIT
     3  
     4  package protocol
     5  
     6  // CompressionMethodID is the ID for a CompressionMethod
     7  type CompressionMethodID byte
     8  
     9  const (
    10  	compressionMethodNull CompressionMethodID = 0
    11  )
    12  
    13  // CompressionMethod represents a TLS Compression Method
    14  type CompressionMethod struct {
    15  	ID CompressionMethodID
    16  }
    17  
    18  // CompressionMethods returns all supported CompressionMethods
    19  func CompressionMethods() map[CompressionMethodID]*CompressionMethod {
    20  	return map[CompressionMethodID]*CompressionMethod{
    21  		compressionMethodNull: {ID: compressionMethodNull},
    22  	}
    23  }
    24  
    25  // DecodeCompressionMethods the given compression methods
    26  func DecodeCompressionMethods(buf []byte) ([]*CompressionMethod, error) {
    27  	if len(buf) < 1 {
    28  		return nil, errBufferTooSmall
    29  	}
    30  	compressionMethodsCount := int(buf[0])
    31  	c := []*CompressionMethod{}
    32  	for i := 0; i < compressionMethodsCount; i++ {
    33  		if len(buf) <= i+1 {
    34  			return nil, errBufferTooSmall
    35  		}
    36  		id := CompressionMethodID(buf[i+1])
    37  		if compressionMethod, ok := CompressionMethods()[id]; ok {
    38  			c = append(c, compressionMethod)
    39  		}
    40  	}
    41  	return c, nil
    42  }
    43  
    44  // EncodeCompressionMethods the given compression methods
    45  func EncodeCompressionMethods(c []*CompressionMethod) []byte {
    46  	out := []byte{byte(len(c))}
    47  	for i := len(c); i > 0; i-- {
    48  		out = append(out, byte(c[i-1].ID))
    49  	}
    50  	return out
    51  }