| 1 | // SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly> |
| 2 | // SPDX-License-Identifier: MIT |
| 3 | |
| 4 | package rtcp |
| 5 | |
| 6 | import ( |
| 7 | "encoding/binary" |
| 8 | "reflect" |
| 9 | "unsafe" |
| 10 | ) |
| 11 | |
| 12 | // These functions implement an introspective structure |
| 13 | // serializer/deserializer, designed to allow RTCP packet |
| 14 | // Structs to be self-describing. They currently work with |
| 15 | // fields of type uint8, uint16, uint32, and uint64 (and |
| 16 | // types derived from them). |
| 17 | // |
| 18 | // - Unexported fields will take up space in the encoded |
| 19 | // array, but wil be set to zero when written, and ignore |
| 20 | // when read. |
| 21 | // |
| 22 | // - Fields that are marked with the tag `encoding:"omit"` |
| 23 | // will be ignored when reading and writing data. |
| 24 | // |
| 25 | // For example: |
| 26 | // |
| 27 | // type Example struct { |
| 28 | // A uint32 |
| 29 | // B bool `encoding:"omit"` |
| 30 | // _ uint64 |
| 31 | // C uint16 |
| 32 | // } |
| 33 | // |
| 34 | // "A" will be encoded as four bytes, in network order. "B" |
| 35 | // will not be encoded at all. The anonymous uint64 will |
| 36 | // encode as 8 bytes of value "0", followed by two bytes |
| 37 | // encoding "C" in network order. |
| 38 | |
| 39 | type packetBuffer struct { |
| 40 | bytes []byte |
| 41 | } |
| 42 | |
| 43 | const omit = "omit" |
| 44 | |
| 45 | // Writes the structure passed to into the buffer that |
| 46 | // PacketBuffer is initialized with. This function will |
| 47 | // modify the PacketBuffer.bytes slice to exclude those |
| 48 | // bytes that have been written into. |
| 49 | // |
| 50 | //nolint:gocognit,cyclop |
| 51 | func (b *packetBuffer) write(v any) error { |
| 52 | value := reflect.ValueOf(v) |
| 53 | |
| 54 | // Indirect is safe to call on non-pointers, and |
| 55 | // will simply return the same value in such cases |
| 56 | value = reflect.Indirect(value) |
| 57 | |
| 58 | switch value.Kind() { |
| 59 | case reflect.Uint8: |
| 60 | if len(b.bytes) < 1 { |
| 61 | return errWrongMarshalSize |
| 62 | } |
| 63 | if value.CanInterface() { |
| 64 | b.bytes[0] = byte(value.Uint()) //nolint:gosec // value.Kind() == reflect.Uint8 guarantees range |
| 65 | } |
| 66 | b.bytes = b.bytes[1:] |
| 67 | case reflect.Uint16: |
| 68 | if len(b.bytes) < 2 { |
| 69 | return errWrongMarshalSize |
| 70 | } |
| 71 | if value.CanInterface() { |
| 72 | binary.BigEndian.PutUint16(b.bytes, uint16(value.Uint())) //nolint:gosec // G115 |
| 73 | } |
| 74 | b.bytes = b.bytes[2:] |
| 75 | case reflect.Uint32: |
| 76 | if len(b.bytes) < 4 { |
| 77 | return errWrongMarshalSize |
| 78 | } |
| 79 | if value.CanInterface() { |
| 80 | binary.BigEndian.PutUint32(b.bytes, uint32(value.Uint())) //nolint:gosec // G115 |
| 81 | } |
| 82 | b.bytes = b.bytes[4:] |
| 83 | case reflect.Uint64: |
| 84 | if len(b.bytes) < 8 { |
| 85 | return errWrongMarshalSize |
| 86 | } |
| 87 | if value.CanInterface() { |
| 88 | binary.BigEndian.PutUint64(b.bytes, value.Uint()) |
| 89 | } |
| 90 | b.bytes = b.bytes[8:] |
| 91 | case reflect.Slice: |
| 92 | for i := 0; i < value.Len(); i++ { |
| 93 | if value.Index(i).CanInterface() { |
| 94 | if err := b.write(value.Index(i).Interface()); err != nil { |
| 95 | return err |
| 96 | } |
| 97 | } else { |
| 98 | b.bytes = b.bytes[value.Index(i).Type().Size():] |
| 99 | } |
| 100 | } |
| 101 | case reflect.Struct: |
| 102 | for i := 0; i < value.NumField(); i++ { |
| 103 | encoding := value.Type().Field(i).Tag.Get("encoding") |
| 104 | if encoding == omit { |
| 105 | continue |
| 106 | } |
| 107 | if value.Field(i).CanInterface() { |
| 108 | if err := b.write(value.Field(i).Interface()); err != nil { |
| 109 | return err |
| 110 | } |
| 111 | } else { |
| 112 | advance := int(value.Field(i).Type().Size()) //nolint:gosec // RTCP struct field sizes are small and controlled |
| 113 | if len(b.bytes) < advance { |
| 114 | return errWrongMarshalSize |
| 115 | } |
| 116 | b.bytes = b.bytes[advance:] |
| 117 | } |
| 118 | } |
| 119 | default: |
| 120 | return errBadStructMemberType |
| 121 | } |
| 122 | |
| 123 | return nil |
| 124 | } |
| 125 | |
| 126 | // Reads bytes from the buffer as necessary to populate |
| 127 | // the structure passed as a parameter. This function will |
| 128 | // modify the PacketBuffer.bytes slice to exclude those |
| 129 | // bytes that have already been read. |
| 130 | // |
| 131 | //nolint:gocognit,cyclop |
| 132 | func (b *packetBuffer) read(v any) error { |
| 133 | ptr := reflect.ValueOf(v) |
| 134 | if ptr.Kind() != reflect.Ptr { |
| 135 | return errBadReadParameter |
| 136 | } |
| 137 | value := reflect.Indirect(ptr) |
| 138 | |
| 139 | // If this is an interface, we need to make it concrete before using it |
| 140 | if value.Kind() == reflect.Interface { |
| 141 | value = reflect.ValueOf(value.Interface()) |
| 142 | } |
| 143 | value = reflect.Indirect(value) |
| 144 | |
| 145 | switch value.Kind() { |
| 146 | case reflect.Uint8: |
| 147 | if len(b.bytes) < 1 { |
| 148 | return errWrongMarshalSize |
| 149 | } |
| 150 | value.SetUint(uint64(b.bytes[0])) |
| 151 | b.bytes = b.bytes[1:] |
| 152 | |
| 153 | case reflect.Uint16: |
| 154 | if len(b.bytes) < 2 { |
| 155 | return errWrongMarshalSize |
| 156 | } |
| 157 | value.SetUint(uint64(binary.BigEndian.Uint16(b.bytes))) |
| 158 | b.bytes = b.bytes[2:] |
| 159 | |
| 160 | case reflect.Uint32: |
| 161 | if len(b.bytes) < 4 { |
| 162 | return errWrongMarshalSize |
| 163 | } |
| 164 | value.SetUint(uint64(binary.BigEndian.Uint32(b.bytes))) |
| 165 | b.bytes = b.bytes[4:] |
| 166 | |
| 167 | case reflect.Uint64: |
| 168 | if len(b.bytes) < 8 { |
| 169 | return errWrongMarshalSize |
| 170 | } |
| 171 | value.SetUint(binary.BigEndian.Uint64(b.bytes)) |
| 172 | b.bytes = b.bytes[8:] |
| 173 | |
| 174 | case reflect.Slice: |
| 175 | // If we encounter a slice, we consume the rest of the data |
| 176 | // in the buffer and load it into the slice. |
| 177 | for len(b.bytes) > 0 { |
| 178 | newElementPtr := reflect.New(value.Type().Elem()) |
| 179 | if err := b.read(newElementPtr.Interface()); err != nil { |
| 180 | return err |
| 181 | } |
| 182 | if value.CanSet() { |
| 183 | value.Set(reflect.Append(value, reflect.Indirect(newElementPtr))) |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | case reflect.Struct: |
| 188 | for i := 0; i < value.NumField(); i++ { |
| 189 | encoding := value.Type().Field(i).Tag.Get("encoding") |
| 190 | if encoding == omit { |
| 191 | continue |
| 192 | } |
| 193 | if value.Field(i).CanInterface() { |
| 194 | field := value.Field(i) |
| 195 | newFieldPtr := reflect.NewAt( |
| 196 | //nolint:gosec // This is the only way to get a typed pointer to a structure's field |
| 197 | field.Type(), unsafe.Pointer(field.UnsafeAddr()), |
| 198 | ) |
| 199 | if err := b.read(newFieldPtr.Interface()); err != nil { |
| 200 | return err |
| 201 | } |
| 202 | } else { |
| 203 | advance := int(value.Field(i).Type().Size()) //nolint:gosec //Size comes from type system and is bounded |
| 204 | if len(b.bytes) < advance { |
| 205 | return errWrongMarshalSize |
| 206 | } |
| 207 | b.bytes = b.bytes[advance:] |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | default: |
| 212 | return errBadStructMemberType |
| 213 | } |
| 214 | |
| 215 | return nil |
| 216 | } |
| 217 | |
| 218 | // Consumes `size` bytes and returns them as an |
| 219 | // independent PacketBuffer. |
| 220 | func (b *packetBuffer) split(size int) packetBuffer { |
| 221 | if size > len(b.bytes) { |
| 222 | size = len(b.bytes) |
| 223 | } |
| 224 | newBuffer := packetBuffer{bytes: b.bytes[:size]} |
| 225 | |
| 226 | b.bytes = b.bytes[size:] |
| 227 | |
| 228 | return newBuffer |
| 229 | } |
| 230 | |
| 231 | // Returns the size that a structure will encode into. |
| 232 | // This fuction doesn't check that Write() will succeed, |
| 233 | // and may return unexpectedly large results for those |
| 234 | // structures that Write() will fail on. |
| 235 | func wireSize(v any) int { |
| 236 | value := reflect.ValueOf(v) |
| 237 | // Indirect is safe to call on non-pointers, and |
| 238 | // will simply return the same value in such cases |
| 239 | value = reflect.Indirect(value) |
| 240 | size := int(0) |
| 241 | |
| 242 | switch value.Kind() { |
| 243 | case reflect.Slice: |
| 244 | for i := 0; i < value.Len(); i++ { |
| 245 | if value.Index(i).CanInterface() { |
| 246 | size += wireSize(value.Index(i).Interface()) |
| 247 | } else { |
| 248 | size += int(value.Index(i).Type().Size()) //nolint:gosec // RTCP element sizes are small and bounded |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | case reflect.Struct: |
| 253 | for i := 0; i < value.NumField(); i++ { |
| 254 | encoding := value.Type().Field(i).Tag.Get("encoding") |
| 255 | if encoding == omit { |
| 256 | continue |
| 257 | } |
| 258 | if value.Field(i).CanInterface() { |
| 259 | size += wireSize(value.Field(i).Interface()) |
| 260 | } else { |
| 261 | size += int(value.Field(i).Type().Size()) // nolint:gosec // Size comes from type system and is bounded |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | default: |
| 266 | size = int(value.Type().Size()) // nolint:gosec // Size comes from type system and is bounde |
| 267 | } |
| 268 | |
| 269 | return size |
| 270 | } |