Copyright 2012 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 (
	
	
	

	
)
OpaquePacket represents an OpenPGP packet as raw, unparsed data. This is useful for splitting and storing the original packet contents separately, handling unsupported packet types or accessing parts of the packet not yet implemented by this package.
Packet type
Reason why the packet was parsed opaquely
Binary contents of the packet data
	Contents []byte
}

func ( *OpaquePacket) ( io.Reader) ( error) {
	.Contents,  = ioutil.ReadAll()
	return
}
Serialize marshals the packet to a writer in its original form, including the packet header.
func ( *OpaquePacket) ( io.Writer) ( error) {
	 = serializeHeader(, packetType(.Tag), len(.Contents))
	if  == nil {
		_,  = .Write(.Contents)
	}
	return
}
Parse attempts to parse the opaque contents into a structure supported by this package. If the packet is not known then the result will be another OpaquePacket.
func ( *OpaquePacket) () ( Packet,  error) {
	 := bytes.NewBuffer(nil)
	 = serializeHeader(, packetType(.Tag), len(.Contents))
	if  != nil {
		.Reason = 
		return , 
	}
	,  = Read(io.MultiReader(, bytes.NewBuffer(.Contents)))
	if  != nil {
		.Reason = 
		 = 
	}
	return
}
OpaqueReader reads OpaquePackets from an io.Reader.
type OpaqueReader struct {
	r io.Reader
}

func ( io.Reader) *OpaqueReader {
	return &OpaqueReader{r: }
}
Read the next OpaquePacket.
func ( *OpaqueReader) () ( *OpaquePacket,  error) {
	, , ,  := readHeader(.r)
	if  != nil {
		return
	}
	 = &OpaquePacket{Tag: uint8(), Reason: }
	 = .parse()
	if  != nil {
		consumeAll()
	}
	return
}
OpaqueSubpacket represents an unparsed OpenPGP subpacket, as found in signature and user attribute packets.
OpaqueSubpackets extracts opaque, unparsed OpenPGP subpackets from their byte representation.
func ( []byte) ( []*OpaqueSubpacket,  error) {
	var (
		 int
		    *OpaqueSubpacket
	)
	for len() > 0 {
		, ,  = nextSubpacket()
		if  != nil {
			break
		}
		 = append(, )
		 = [+len(.Contents):]
	}
	return
}

RFC 4880, section 5.2.3.1
	var  uint32
	if len() < 1 {
		goto 
	}
	 = &OpaqueSubpacket{}
	switch {
	case [0] < 192:
		 = 2 // 1 length byte, 1 subtype byte
		if len() <  {
			goto 
		}
		 = uint32([0])
		 = [1:]
	case [0] < 255:
		 = 3 // 2 length bytes, 1 subtype
		if len() <  {
			goto 
		}
		 = uint32([0]-192)<<8 + uint32([1]) + 192
		 = [2:]
	default:
		 = 6 // 5 length bytes, 1 subtype
		if len() <  {
			goto 
		}
		 = uint32([1])<<24 |
			uint32([2])<<16 |
			uint32([3])<<8 |
			uint32([4])
		 = [5:]
	}
	if  > uint32(len()) ||  == 0 {
		goto 
	}
	.SubType = [0]
	.Contents = [1:]
	return
:
	 = errors.StructuralError("subpacket truncated")
	return
}

func ( *OpaqueSubpacket) ( io.Writer) ( error) {
	 := make([]byte, 6)
	 := serializeSubpacketLength(, len(.Contents)+1)
	[] = .SubType
	if _,  = .Write([:+1]);  != nil {
		return
	}
	_,  = .Write(.Contents)
	return