pemstream

package
v0.0.0-...-3746c95 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 11, 2024 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTrailingGarbage         = errors.New("additional garbage at end of the line")
	ErrInconsistentLineEndings = errors.New("a mix of Linux and Windows line endings has been detected")
	ErrInconsistentIndentation = errors.New("inconsistent indentation")
	ErrTooManyBlankLines       = errors.New("too many blank lines between PEM headers and block")
)
View Source
var (
	ErrNoB64       = errors.New("expected some base64 encoded data")
	ErrB64LenWrong = errors.New("wrong number of base64 bytes on line")
	ErrB64EndTwice = errors.New("base64 encoding ended more than once")
)

Functions

This section is empty.

Types

type PemBlock

type PemBlock struct {
	pem.Block

	// WinLineEndings is true if lines end in "\r\n", false if they end in "\n"
	WinLineEndings bool

	// The white space at the beginning of the -----BEGIN line. All other
	// lines must have exactly this white space or the parser errors. This
	// allows parsing of PEM block in config files such as YAML
	Indent []byte

	// Sometimes there is a blank line between the PEM headers and the encoded
	// data. If there is a blank line then PostHeaderBlankLine will be non nil.
	// If it is non nil the only values the parser will accept are a zero length
	// string or the same white space as the rest of the block.
	PostHeaderBlankLine []byte

	// B64LineLength is number of characters of base64 encoding on the first
	// line of encoded data. The parser wil ensure that this is consistent
	// across the block.
	B64LineLen int

	// StartLine and EndLine are the line numbers on which the -----BEGIN and
	// ----END lines reside. If known, will be greater than zero.
	StartLine, EndLine int
}

PemBlock is an encoding/pem.Block with additional information about how it was parsed from the file it was found in. There is almost enough information to convert it back to the original byte stream. Information that is not captured: - The order of the PEM headers. - Any trailing white space.

type PemReader

type PemReader struct {
	// contains filtered or unexported fields
}

PemReader wraps an io.Reader and reads PEM blocks from it. The input stream contains blocks and data between the blocks (called gaps here). gaps and blocks can be extracted from the stream alternately by calling GetPem*() and GetGap*() functions. A PemReader always starts with a gap even if it is zero bytes long.

To tell if the end of the input stream has been reached, both a GetPem*() function and a GetGap*() will return nil.

func NewPemReader

func NewPemReader(upstream io.Reader) *PemReader

NewPemReader initializes a new PemReader to read from upstream

func (*PemReader) Err

func (ps *PemReader) Err() error

Err returns the last error form the parser or underlying io.Reader.

func (*PemReader) GetGapDataStream

func (ps *PemReader) GetGapDataStream() (io.ReadCloser, error)

GetGapDataStream returns an io.ReadCloser that will read all the bytes of the stream up to the beginning of the next PEM block or the input stream EOF. When this stream is Closed it will read and discard all remaining data. If the returned ReadCloser is nil then the PemReader is part way through reading a PEM block.

Calling GetGapDataStream() repeatedly will return the same stream unmodified.

func (*PemReader) GetPemBlock

func (ps *PemReader) GetPemBlock() (block *PemBlock, err error)

GetPemBlock reads, decodes and returns the nex PEM block. It discards all data prior to the PEM block. Calling GetPemBlock() repeatedly will return each PEM block in the upstream reader until there are no more in which nil will be returned.

func (*PemReader) GetPemDataStream

func (ps *PemReader) GetPemDataStream() (blockInfo *PemBlock, blockData io.ReadCloser, err error)

GetPemDataStream finds the beginning of the next PEM block and returns information about it and an io.ReadCloser that will produce the decoded content of the PEM block. As such blockInfo.Bytes will be nil and blockInfo.EndLine will be zero until the io.ReadCloser has read the entire block.

If no PEM block is available nil is returned for both the stream and the block. Calling GetPemDataStream() repeatedly will return the same stream.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL