1
1
package ldap
2
2
3
3
import (
4
- "bytes"
5
4
"encoding/asn1"
6
5
"encoding/hex"
7
6
"errors"
@@ -72,29 +71,12 @@ type RelativeDN struct {
72
71
// String returns a normalized string representation of this relative DN which
73
72
// is the a join of all attributes (sorted in increasing order) with a "+".
74
73
func (r * RelativeDN ) String () string {
75
- builder := strings.Builder {}
76
- sortedAttributes := make ([]* AttributeTypeAndValue , len (r .Attributes ))
77
- copy (sortedAttributes , r .Attributes )
78
- sortAttributes (sortedAttributes )
79
- for i , atv := range sortedAttributes {
80
- builder .WriteString (atv .String ())
81
- if i < len (sortedAttributes )- 1 {
82
- builder .WriteByte ('+' )
83
- }
74
+ attrs := make ([]string , len (r .Attributes ))
75
+ for i := range r .Attributes {
76
+ attrs [i ] = r .Attributes [i ].String ()
84
77
}
85
- return builder .String ()
86
- }
87
-
88
- func sortAttributes (atvs []* AttributeTypeAndValue ) {
89
- sort .Slice (atvs , func (i , j int ) bool {
90
- ti := foldString (atvs [i ].Type )
91
- tj := foldString (atvs [j ].Type )
92
- if ti != tj {
93
- return ti < tj
94
- }
95
-
96
- return atvs [i ].Value < atvs [j ].Value
97
- })
78
+ sort .Strings (attrs )
79
+ return strings .Join (attrs , "+" )
98
80
}
99
81
100
82
// DN represents a distinguishedName from https://tools.ietf.org/html/rfc4514
@@ -105,14 +87,11 @@ type DN struct {
105
87
// String returns a normalized string representation of this DN which is the
106
88
// join of all relative DNs with a ",".
107
89
func (d * DN ) String () string {
108
- builder := strings.Builder {}
109
- for i , rdn := range d .RDNs {
110
- builder .WriteString (rdn .String ())
111
- if i < len (d .RDNs )- 1 {
112
- builder .WriteByte (',' )
113
- }
90
+ rdns := make ([]string , len (d .RDNs ))
91
+ for i := range d .RDNs {
92
+ rdns [i ] = d .RDNs [i ].String ()
114
93
}
115
- return builder . String ( )
94
+ return strings . Join ( rdns , "," )
116
95
}
117
96
118
97
func stripLeadingAndTrailingSpaces (inVal string ) string {
@@ -194,18 +173,21 @@ func decodeString(str string) (string, error) {
194
173
195
174
// Escape a string according to RFC 4514
196
175
func encodeString (value string , isValue bool ) string {
197
- encodedBuf := bytes. Buffer {}
176
+ builder := strings. Builder {}
198
177
199
178
escapeChar := func (c byte ) {
200
- encodedBuf .WriteByte ('\\' )
201
- encodedBuf .WriteByte (c )
179
+ builder .WriteByte ('\\' )
180
+ builder .WriteByte (c )
202
181
}
203
182
204
183
escapeHex := func (c byte ) {
205
- encodedBuf .WriteByte ('\\' )
206
- encodedBuf .WriteString (hex .EncodeToString ([]byte {c }))
184
+ builder .WriteByte ('\\' )
185
+ builder .WriteString (hex .EncodeToString ([]byte {c }))
207
186
}
208
187
188
+ // Loop through each byte and escape as necessary.
189
+ // Runes that take up more than one byte are escaped
190
+ // byte by byte (since both bytes are non-ASCII).
209
191
for i := 0 ; i < len (value ); i ++ {
210
192
char := value [i ]
211
193
if i == 0 && (char == ' ' || char == '#' ) {
@@ -242,10 +224,10 @@ func encodeString(value string, isValue bool) string {
242
224
}
243
225
244
226
// Any other character does not require escaping.
245
- encodedBuf .WriteByte (char )
227
+ builder .WriteByte (char )
246
228
}
247
229
248
- return encodedBuf .String ()
230
+ return builder .String ()
249
231
}
250
232
251
233
func decodeEncodedString (str string ) (string , error ) {
@@ -283,13 +265,16 @@ func ParseDN(str string) (*DN, error) {
283
265
rdn .Attributes = append (rdn .Attributes , attr )
284
266
attr = & AttributeTypeAndValue {}
285
267
if end {
286
- sortAttributes (rdn .Attributes )
287
268
dn .RDNs = append (dn .RDNs , rdn )
288
269
rdn = & RelativeDN {}
289
270
}
290
271
}
291
272
)
292
273
274
+ // Loop through each character in the string and
275
+ // build up the attribute type and value pairs.
276
+ // We only check for ascii characters here, which
277
+ // allows us to iterate over the string byte by byte.
293
278
for i := 0 ; i < len (str ); i ++ {
294
279
char := str [i ]
295
280
switch {
0 commit comments