From 20922c5a9e9254e84ee5a153ed01695770de7e9b Mon Sep 17 00:00:00 2001 From: Christopher Puschmann Date: Sun, 15 Aug 2021 04:06:51 +0200 Subject: [PATCH] Refactor search attribute extraction --- search.go | 37 +++++++++++++++++++++++++++---------- v3/search.go | 37 +++++++++++++++++++++++++++---------- 2 files changed, 54 insertions(+), 20 deletions(-) diff --git a/search.go b/search.go index 4fcc794a..915e4203 100644 --- a/search.go +++ b/search.go @@ -376,16 +376,9 @@ func (l *Conn) Search(searchRequest *SearchRequest) (*SearchResult, error) { switch packet.Children[1].Tag { case 4: - entry := new(Entry) - entry.DN = packet.Children[1].Children[0].Value.(string) - for _, child := range packet.Children[1].Children[1].Children { - attr := new(EntryAttribute) - attr.Name = child.Children[0].Value.(string) - for _, value := range child.Children[1].Children { - attr.Values = append(attr.Values, value.Value.(string)) - attr.ByteValues = append(attr.ByteValues, value.ByteValue) - } - entry.Attributes = append(entry.Attributes, attr) + entry := &Entry{ + DN: packet.Children[1].Children[0].Value.(string), + Attributes: unpackAttributes(packet.Children[1].Children[1].Children), } result.Entries = append(result.Entries, entry) case 5: @@ -408,3 +401,27 @@ func (l *Conn) Search(searchRequest *SearchRequest) (*SearchResult, error) { } } } + +// unpackAttributes will extract all given LDAP attributes and it's values +// from the ber.Packet +func unpackAttributes(children []*ber.Packet) []*EntryAttribute { + entries := make([]*EntryAttribute, len(children)) + for i, child := range children { + length := len(child.Children[1].Children) + entry := &EntryAttribute{ + Name: child.Children[0].Value.(string), + // pre-allocate the slice since we can determine + // the number of attributes at this point + Values: make([]string, length), + ByteValues: make([][]byte, length), + } + + for i, value := range child.Children[1].Children { + entry.ByteValues[i] = value.ByteValue + entry.Values[i] = value.Value.(string) + } + entries[i] = entry + } + + return entries +} diff --git a/v3/search.go b/v3/search.go index 4fcc794a..915e4203 100644 --- a/v3/search.go +++ b/v3/search.go @@ -376,16 +376,9 @@ func (l *Conn) Search(searchRequest *SearchRequest) (*SearchResult, error) { switch packet.Children[1].Tag { case 4: - entry := new(Entry) - entry.DN = packet.Children[1].Children[0].Value.(string) - for _, child := range packet.Children[1].Children[1].Children { - attr := new(EntryAttribute) - attr.Name = child.Children[0].Value.(string) - for _, value := range child.Children[1].Children { - attr.Values = append(attr.Values, value.Value.(string)) - attr.ByteValues = append(attr.ByteValues, value.ByteValue) - } - entry.Attributes = append(entry.Attributes, attr) + entry := &Entry{ + DN: packet.Children[1].Children[0].Value.(string), + Attributes: unpackAttributes(packet.Children[1].Children[1].Children), } result.Entries = append(result.Entries, entry) case 5: @@ -408,3 +401,27 @@ func (l *Conn) Search(searchRequest *SearchRequest) (*SearchResult, error) { } } } + +// unpackAttributes will extract all given LDAP attributes and it's values +// from the ber.Packet +func unpackAttributes(children []*ber.Packet) []*EntryAttribute { + entries := make([]*EntryAttribute, len(children)) + for i, child := range children { + length := len(child.Children[1].Children) + entry := &EntryAttribute{ + Name: child.Children[0].Value.(string), + // pre-allocate the slice since we can determine + // the number of attributes at this point + Values: make([]string, length), + ByteValues: make([][]byte, length), + } + + for i, value := range child.Children[1].Children { + entry.ByteValues[i] = value.ByteValue + entry.Values[i] = value.Value.(string) + } + entries[i] = entry + } + + return entries +}