-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmark.go
118 lines (113 loc) · 3.41 KB
/
mark.go
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
115
116
117
118
// 2019, Georg Sauthoff <[email protected]>
// SPDX-License-Identifier: GPL-3.0-or-later
package main
import (
"io"
)
type mark_copy_header_writer struct {
out io.WriteCloser
prefix byte
name []byte
state int
}
func new_mark_copy_header_writer(prefix byte, out io.WriteCloser) *mark_copy_header_writer {
w := new(mark_copy_header_writer)
w.out = out
w.prefix = prefix
return w
}
// XXX optimize the small allocations? e.g. use large allocations and
// cut slices from those?
// expects full words - i.e. to be chained after the word_split_writer
func (w *mark_copy_header_writer) Write(word []byte) (int, error) {
const ( AT_NAME = iota
WRITE_LINE
)
too_long := []byte("h:X-gonzo-fake-too-long:")
n := len(word)
switch w.state {
case AT_NAME:
if n == 1 && word[0] == byte('\n') {
w.name = w.name[:0]
w.name = append(w.name, too_long...)
t := make([]byte, len(w.name))
copy(t, w.name)
if _, err := w.out.Write(t); err != nil {
return 0, err
}
} else if n > 0 && word[n-1] != byte(':') {
w.name = w.name[:0]
w.name = append(w.name, too_long...)
t := make([]byte, len(w.name))
copy(t, w.name)
if _, err := w.out.Write(t); err != nil {
return 0, err
}
t = make([]byte, len(w.name) + n)
copy(t, w.name)
copy(t[len(w.name):], word)
if _, err := w.out.Write(t); err != nil {
return 0, err
}
w.state = WRITE_LINE
} else {
w.name = w.name[:0]
// yes, Go doesn't support mixing word... with the other args
w.name = append(w.name, w.prefix, byte(':'))
w.name = append(w.name, word...)
t := make([]byte, len(w.name))
copy(t, w.name)
if _, err := w.out.Write(t); err != nil {
return 0, err
}
w.state = WRITE_LINE
}
case WRITE_LINE:
if n == 1 && word[0] == byte('\n') {
w.state = AT_NAME
} else if n < min_word_len {
// skip it
} else {
t := make([]byte, len(w.name) + n)
copy(t, w.name)
copy(t[len(w.name):], word)
if _, err := w.out.Write(t); err != nil {
return 0, err
}
}
}
return n, nil
}
func (w *mark_copy_header_writer) Close() error {
return w.out.Close()
}
type mark_copy_body_writer struct {
out io.WriteCloser
prefix byte
}
func new_mark_copy_body_writer(prefix byte, out io.WriteCloser) *mark_copy_body_writer {
w := new(mark_copy_body_writer)
w.out = out
w.prefix = prefix
return w
}
// XXX optimize the small allocations? e.g. use large allocations and
// cut slices from those?
// expects full words - i.e. to be chained after the word_split_writer
func (w *mark_copy_body_writer) Write(word []byte) (int, error) {
n := len(word)
if n == 1 && word[0] == byte('\n') {
return n, nil
}
t := make([]byte, 2 + len(word))
t[0] = w.prefix
t[1] = byte(':')
copy(t[2:], word)
if _, err := w.out.Write(t); err != nil {
return 0, err
}
return n, nil
}
func (w *mark_copy_body_writer) Close() error {
return w.out.Close()
}