| 1234567891011121314151617181920212223242526272829303132333435 |
- package protocol
- import (
- "fmt"
- "time"
- )
- // ResidualUntrusted classifies pre-transaction UART leftovers.
- // Any complete late frame, parser leftover, half-frame, or unexplained
- // residual byte is untrusted. Empty input is clean.
- func ResidualUntrusted(chunks [][]byte, now time.Time) error {
- drain := NewStreamParser(DefaultMaxPayload, 0)
- total := 0
- for _, c := range chunks {
- if len(c) == 0 {
- continue
- }
- total += len(c)
- for _, fr := range drain.Push(c, now) {
- if fr.Err != nil {
- return fmt.Errorf("untrusted residual parse: %w", fr.Err)
- }
- if len(fr.Raw) > 0 {
- return fmt.Errorf("late frame (%d bytes)", total)
- }
- }
- }
- if drain.Buffered() > 0 {
- return fmt.Errorf("half-frame leftover %d bytes after %d drained", drain.Buffered(), total)
- }
- if total > 0 {
- return fmt.Errorf("%d residual bytes", total)
- }
- return nil
- }
|