File: common.go

package info (click to toggle)
golang-github-protonmail-gopenpgp-v3 3.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,028 kB
  • sloc: sh: 87; makefile: 2
file content (114 lines) | stat: -rw-r--r-- 2,597 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Package internal contains internal methods and constants.
package internal

import (
	"bytes"
	"errors"
	"io"
	"strings"

	"github.com/ProtonMail/gopenpgp/v3/constants"
)

var nl []byte = []byte("\n")
var rnl []byte = []byte("\r\n")

func Canonicalize(text string) string {
	return strings.ReplaceAll(strings.ReplaceAll(text, "\r\n", "\n"), "\n", "\r\n")
}

func CanonicalizeBytes(text []byte) []byte {
	return bytes.ReplaceAll(bytes.ReplaceAll(text, rnl, nl), nl, rnl)
}

func TrimEachLine(text string) string {
	lines := strings.Split(text, "\n")

	for i := range lines {
		lines[i] = strings.TrimRight(lines[i], " \t\r")
	}

	return strings.Join(lines, "\n")
}

func TrimEachLineBytes(text []byte) []byte {
	lines := bytes.Split(text, nl)

	for i := range lines {
		lines[i] = bytes.TrimRight(lines[i], " \t\r")
	}

	return bytes.Join(lines, nl)
}

// ArmorHeaders is a map of default armor headers.
var ArmorHeaders = map[string]string{}

func init() {
	if constants.ArmorHeaderEnabled {
		ArmorHeaders = map[string]string{
			"Version": constants.ArmorHeaderVersion,
			"Comment": constants.ArmorHeaderComment,
		}
	}
}

// ResetReader is a reader that can be reset by buffering data internally.
type ResetReader struct {
	Reader     io.Reader
	buffer     *bytes.Buffer
	bufferData bool
}

// NewResetReader creates a new ResetReader with the default state.
func NewResetReader(reader io.Reader) *ResetReader {
	return &ResetReader{
		Reader:     reader,
		buffer:     bytes.NewBuffer(nil),
		bufferData: true,
	}
}

func (rr *ResetReader) Read(b []byte) (n int, err error) {
	n, err = rr.Reader.Read(b)
	if rr.bufferData {
		rr.buffer.Write(b[:n])
	}
	return
}

// DisableBuffering disables the internal buffering.
// After the disable, a Reset is not allowed anymore.
func (rr *ResetReader) DisableBuffering() {
	rr.bufferData = false
}

// Reset creates a reader that reads again from the beginning and
// resets the internal state.
func (rr *ResetReader) Reset() (io.Reader, error) {
	if !rr.bufferData {
		return nil, errors.New("reset not possible if buffering is disabled")
	}
	rr.Reader = io.MultiReader(rr.buffer, rr.Reader)
	rr.buffer = bytes.NewBuffer(nil)
	return rr.Reader, nil
}

type noOpWriteCloser struct {
	writer io.Writer
}

// NewNoOpWriteCloser creates a WriteCloser form a Writer that performs no operation on close.
func NewNoOpWriteCloser(writer io.Writer) io.WriteCloser {
	return &noOpWriteCloser{
		writer,
	}
}

func (w *noOpWriteCloser) Write(p []byte) (n int, err error) {
	return w.writer.Write(p)
}

func (w *noOpWriteCloser) Close() error {
	return nil
}