Copyright 2011 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

package packet

import (
	
	
)
Reader reads packets from an io.Reader and allows packets to be 'unread' so that they result from the next call to Next.
type Reader struct {
	q       []Packet
	readers []io.Reader
}
New io.Readers are pushed when a compressed or encrypted packet is processed and recursively treated as a new source of packets. However, a carefully crafted packet can trigger an infinite recursive sequence of packets. See http://mumble.net/~campbell/misc/pgp-quine https://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2013-4402 This constant limits the number of recursive packets that may be pushed.
const maxReaders = 32
Next returns the most recently unread Packet, or reads another packet from the top-most io.Reader. Unknown packet types are skipped.
func ( *Reader) () ( Packet,  error) {
	if len(.q) > 0 {
		 = .q[len(.q)-1]
		.q = .q[:len(.q)-1]
		return
	}

	for len(.readers) > 0 {
		,  = Read(.readers[len(.readers)-1])
		if  == nil {
			return
		}
		if  == io.EOF {
			.readers = .readers[:len(.readers)-1]
			continue
		}
		if ,  := .(errors.UnknownPacketTypeError); ! {
			return nil, 
		}
	}

	return nil, io.EOF
}
Push causes the Reader to start reading from a new io.Reader. When an EOF error is seen from the new io.Reader, it is popped and the Reader continues to read from the next most recent io.Reader. Push returns a StructuralError if pushing the reader would exceed the maximum recursion level, otherwise it returns nil.
func ( *Reader) ( io.Reader) ( error) {
	if len(.readers) >= maxReaders {
		return errors.StructuralError("too many layers of packets")
	}
	.readers = append(.readers, )
	return nil
}
Unread causes the given Packet to be returned from the next call to Next.
func ( *Reader) ( Packet) {
	.q = append(.q, )
}

func ( io.Reader) *Reader {
	return &Reader{
		q:       nil,
		readers: []io.Reader{},
	}