-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathencoding.go
More file actions
96 lines (84 loc) · 2.34 KB
/
encoding.go
File metadata and controls
96 lines (84 loc) · 2.34 KB
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package textutils // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/textutils"
import (
"errors"
"fmt"
"strings"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/ianaindex"
"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
)
// NewBasicConfig creates a new Encoding config
func NewEncodingConfig() EncodingConfig {
return EncodingConfig{
Encoding: "utf-8",
}
}
// EncodingConfig is the configuration of a Encoding helper
type EncodingConfig struct {
Encoding string `mapstructure:"encoding,omitempty"`
}
// Build will build an Encoding operator.
func (c EncodingConfig) Build() (Encoding, error) {
enc, err := lookupEncoding(c.Encoding)
if err != nil {
return Encoding{}, err
}
return Encoding{
Encoding: enc,
decodeBuffer: make([]byte, 1<<12),
decoder: enc.NewDecoder(),
}, nil
}
type Encoding struct {
Encoding encoding.Encoding
decoder *encoding.Decoder
decodeBuffer []byte
}
// Decode converts the bytes in msgBuf to utf-8 from the configured encoding
func (e *Encoding) Decode(msgBuf []byte) ([]byte, error) {
for {
e.decoder.Reset()
nDst, _, err := e.decoder.Transform(e.decodeBuffer, msgBuf, true)
if err == nil {
return e.decodeBuffer[:nDst], nil
}
if errors.Is(err, transform.ErrShortDst) {
e.decodeBuffer = make([]byte, len(e.decodeBuffer)*2)
continue
}
return nil, fmt.Errorf("transform encoding: %w", err)
}
}
var encodingOverrides = map[string]encoding.Encoding{
"utf-16": unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM),
"utf16": unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM),
"utf-8": unicode.UTF8,
"utf8": unicode.UTF8,
"ascii": unicode.UTF8,
"us-ascii": unicode.UTF8,
"nop": encoding.Nop,
"": unicode.UTF8,
}
func lookupEncoding(enc string) (encoding.Encoding, error) {
if e, ok := encodingOverrides[strings.ToLower(enc)]; ok {
return e, nil
}
e, err := ianaindex.IANA.Encoding(enc)
if err != nil {
return nil, fmt.Errorf("unsupported encoding '%s'", enc)
}
if e == nil {
return nil, fmt.Errorf("no charmap defined for encoding '%s'", enc)
}
return e, nil
}
func IsNop(enc string) bool {
e, err := lookupEncoding(enc)
if err != nil {
return false
}
return e == encoding.Nop
}