-
Notifications
You must be signed in to change notification settings - Fork 15
/
NullTransform.go
77 lines (62 loc) · 2.19 KB
/
NullTransform.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
/*
Copyright 2011-2024 Frederic Langlet
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
you may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package transform
import (
"errors"
"fmt"
)
// NullTransform is a pass through byte function
type NullTransform struct {
}
// NewNullTransform creates a new instance of NullTransform
func NewNullTransform() (*NullTransform, error) {
this := &NullTransform{}
return this, nil
}
// NewNullTransformWithCtx creates a new instance of NullTransform using a
// configuration map as parameter.
func NewNullTransformWithCtx(ctx *map[string]any) (*NullTransform, error) {
this := &NullTransform{}
return this, nil
}
func doCopy(src, dst []byte) (uint, uint, error) {
if len(src) == 0 {
return 0, 0, nil
}
if len(src) > len(dst) {
return uint(0), uint(0), errors.New("Destination buffer too small")
}
if &src[0] != &dst[0] {
copy(dst, src)
}
return uint(len(src)), uint(len(src)), nil
}
// Forward applies the function to the src and writes the result
// to the destination. Returns number of bytes read, number of bytes
// written and possibly an error.
func (this *NullTransform) Forward(src, dst []byte) (uint, uint, error) {
if n := this.MaxEncodedLen(len(src)); len(dst) < n {
return 0, 0, fmt.Errorf("Output buffer is too small - size: %d, required %d", len(dst), n)
}
return doCopy(src, dst)
}
// Inverse applies the reverse function to the src and writes the result
// to the destination. Returns number of bytes read, number of bytes
// written and possibly an error.
func (this *NullTransform) Inverse(src, dst []byte) (uint, uint, error) {
return doCopy(src, dst)
}
// MaxEncodedLen returns the max size required for the encoding output buffer
func (this *NullTransform) MaxEncodedLen(srcLen int) int {
return srcLen
}