1
2
3
4
5 package cryptobyte
6
7 import (
8 "errors"
9 "fmt"
10 )
11
12
13
14
15
16
17
18
19
20
21
22
23 type Builder struct {
24 err error
25 result []byte
26 fixedSize bool
27 child *Builder
28 offset int
29 pendingLenLen int
30 pendingIsASN1 bool
31 inContinuation *bool
32 }
33
34
35
36
37 func NewBuilder(buffer []byte) *Builder {
38 return &Builder{
39 result: buffer,
40 }
41 }
42
43
44
45
46 func NewFixedBuilder(buffer []byte) *Builder {
47 return &Builder{
48 result: buffer,
49 fixedSize: true,
50 }
51 }
52
53
54
55 func (b *Builder) SetError(err error) {
56 b.err = err
57 }
58
59
60
61 func (b *Builder) Bytes() ([]byte, error) {
62 if b.err != nil {
63 return nil, b.err
64 }
65 return b.result[b.offset:], nil
66 }
67
68
69
70 func (b *Builder) BytesOrPanic() []byte {
71 if b.err != nil {
72 panic(b.err)
73 }
74 return b.result[b.offset:]
75 }
76
77
78 func (b *Builder) AddUint8(v uint8) {
79 b.add(byte(v))
80 }
81
82
83 func (b *Builder) AddUint16(v uint16) {
84 b.add(byte(v>>8), byte(v))
85 }
86
87
88
89 func (b *Builder) AddUint24(v uint32) {
90 b.add(byte(v>>16), byte(v>>8), byte(v))
91 }
92
93
94 func (b *Builder) AddUint32(v uint32) {
95 b.add(byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
96 }
97
98
99 func (b *Builder) AddUint48(v uint64) {
100 b.add(byte(v>>40), byte(v>>32), byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
101 }
102
103
104 func (b *Builder) AddUint64(v uint64) {
105 b.add(byte(v>>56), byte(v>>48), byte(v>>40), byte(v>>32), byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
106 }
107
108
109 func (b *Builder) AddBytes(v []byte) {
110 b.add(v...)
111 }
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135 type BuilderContinuation func(child *Builder)
136
137
138
139
140 type BuildError struct {
141 Err error
142 }
143
144
145 func (b *Builder) AddUint8LengthPrefixed(f BuilderContinuation) {
146 b.addLengthPrefixed(1, false, f)
147 }
148
149
150 func (b *Builder) AddUint16LengthPrefixed(f BuilderContinuation) {
151 b.addLengthPrefixed(2, false, f)
152 }
153
154
155 func (b *Builder) AddUint24LengthPrefixed(f BuilderContinuation) {
156 b.addLengthPrefixed(3, false, f)
157 }
158
159
160 func (b *Builder) AddUint32LengthPrefixed(f BuilderContinuation) {
161 b.addLengthPrefixed(4, false, f)
162 }
163
164 func (b *Builder) callContinuation(f BuilderContinuation, arg *Builder) {
165 if !*b.inContinuation {
166 *b.inContinuation = true
167
168 defer func() {
169 *b.inContinuation = false
170
171 r := recover()
172 if r == nil {
173 return
174 }
175
176 if buildError, ok := r.(BuildError); ok {
177 b.err = buildError.Err
178 } else {
179 panic(r)
180 }
181 }()
182 }
183
184 f(arg)
185 }
186
187 func (b *Builder) addLengthPrefixed(lenLen int, isASN1 bool, f BuilderContinuation) {
188
189 if b.err != nil {
190 return
191 }
192
193 offset := len(b.result)
194 b.add(make([]byte, lenLen)...)
195
196 if b.inContinuation == nil {
197 b.inContinuation = new(bool)
198 }
199
200 b.child = &Builder{
201 result: b.result,
202 fixedSize: b.fixedSize,
203 offset: offset,
204 pendingLenLen: lenLen,
205 pendingIsASN1: isASN1,
206 inContinuation: b.inContinuation,
207 }
208
209 b.callContinuation(f, b.child)
210 b.flushChild()
211 if b.child != nil {
212 panic("cryptobyte: internal error")
213 }
214 }
215
216 func (b *Builder) flushChild() {
217 if b.child == nil {
218 return
219 }
220 b.child.flushChild()
221 child := b.child
222 b.child = nil
223
224 if child.err != nil {
225 b.err = child.err
226 return
227 }
228
229 length := len(child.result) - child.pendingLenLen - child.offset
230
231 if length < 0 {
232 panic("cryptobyte: internal error")
233 }
234
235 if child.pendingIsASN1 {
236
237
238
239 if child.pendingLenLen != 1 {
240 panic("cryptobyte: internal error")
241 }
242 var lenLen, lenByte uint8
243 if int64(length) > 0xfffffffe {
244 b.err = errors.New("pending ASN.1 child too long")
245 return
246 } else if length > 0xffffff {
247 lenLen = 5
248 lenByte = 0x80 | 4
249 } else if length > 0xffff {
250 lenLen = 4
251 lenByte = 0x80 | 3
252 } else if length > 0xff {
253 lenLen = 3
254 lenByte = 0x80 | 2
255 } else if length > 0x7f {
256 lenLen = 2
257 lenByte = 0x80 | 1
258 } else {
259 lenLen = 1
260 lenByte = uint8(length)
261 length = 0
262 }
263
264
265
266 child.result[child.offset] = lenByte
267 extraBytes := int(lenLen - 1)
268 if extraBytes != 0 {
269 child.add(make([]byte, extraBytes)...)
270 childStart := child.offset + child.pendingLenLen
271 copy(child.result[childStart+extraBytes:], child.result[childStart:])
272 }
273 child.offset++
274 child.pendingLenLen = extraBytes
275 }
276
277 l := length
278 for i := child.pendingLenLen - 1; i >= 0; i-- {
279 child.result[child.offset+i] = uint8(l)
280 l >>= 8
281 }
282 if l != 0 {
283 b.err = fmt.Errorf("cryptobyte: pending child length %d exceeds %d-byte length prefix", length, child.pendingLenLen)
284 return
285 }
286
287 if b.fixedSize && &b.result[0] != &child.result[0] {
288 panic("cryptobyte: BuilderContinuation reallocated a fixed-size buffer")
289 }
290
291 b.result = child.result
292 }
293
294 func (b *Builder) add(bytes ...byte) {
295 if b.err != nil {
296 return
297 }
298 if b.child != nil {
299 panic("cryptobyte: attempted write while child is pending")
300 }
301 if len(b.result)+len(bytes) < len(bytes) {
302 b.err = errors.New("cryptobyte: length overflow")
303 }
304 if b.fixedSize && len(b.result)+len(bytes) > cap(b.result) {
305 b.err = errors.New("cryptobyte: Builder is exceeding its fixed-size buffer")
306 return
307 }
308 b.result = append(b.result, bytes...)
309 }
310
311
312
313
314 func (b *Builder) Unwrite(n int) {
315 if b.err != nil {
316 return
317 }
318 if b.child != nil {
319 panic("cryptobyte: attempted unwrite while child is pending")
320 }
321 length := len(b.result) - b.pendingLenLen - b.offset
322 if length < 0 {
323 panic("cryptobyte: internal error")
324 }
325 if n < 0 {
326 panic("cryptobyte: attempted to unwrite negative number of bytes")
327 }
328 if n > length {
329 panic("cryptobyte: attempted to unwrite more than was written")
330 }
331 b.result = b.result[:len(b.result)-n]
332 }
333
334
335 type MarshalingValue interface {
336
337
338
339 Marshal(b *Builder) error
340 }
341
342
343
344
345 func (b *Builder) AddValue(v MarshalingValue) {
346 err := v.Marshal(b)
347 if err != nil {
348 b.err = err
349 }
350 }
351
View as plain text