// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT

// Package obu implements tools for working with the Open Bitstream Unit.
package obu import const ( sevenLsbBitmask = uint(0b01111111) msbBitmask = uint(0b10000000) ) // ErrFailedToReadLEB128 indicates that a buffer ended before a LEB128 value could be successfully read. var ErrFailedToReadLEB128 = errors.New("payload ended before LEB128 was finished") // EncodeLEB128 encodes a uint as LEB128. func ( uint) ( uint) { for { // Copy seven bits from in and discard // what we have copied from in |= ( & sevenLsbBitmask) >>= 7 // If we have more bits to encode set MSB // otherwise we are done if != 0 { |= msbBitmask <<= 8 } else { return } } } func decodeLEB128( uint) ( uint) { for { // Take 7 LSB from in |= ( & sevenLsbBitmask) // Discard the MSB >>= 8 if == 0 { return } <<= 7 } } // ReadLeb128 scans an buffer and decodes a Leb128 value. // If the end of the buffer is reached and all MSB are set // an error is returned. func ( []byte) (uint, uint, error) { var uint for := range { |= uint([]) if []&byte(msbBitmask) == 0 { return decodeLEB128(), uint( + 1), nil // nolint: gosec // G115 } // Make more room for next read <<= 8 } return 0, 0, ErrFailedToReadLEB128 } // WriteToLeb128 writes a uint to a LEB128 encoded byte slice. func ( uint) []byte { := make([]byte, 10) for := 0; < len(); ++ { [] = byte( & 0x7f) >>= 7 if == 0 { return [:+1] } [] |= 0x80 } return // unreachable }