aboutsummaryrefslogtreecommitdiff
path: root/goldap/control.go
blob: 3f94b67cedae9c10bff6c54ddbc90b6142654c5a (plain) (blame)
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
package message

import (
	"errors"
	"fmt"
)

//
//        Control ::= SEQUENCE {
//             controlType             LDAPOID,
//             criticality             BOOLEAN DEFAULT FALSE,
//             controlValue            OCTET STRING OPTIONAL }

func (control *Control) ControlType() LDAPOID {
	return control.controlType
}

func (control *Control) Criticality() BOOLEAN {
	return control.criticality
}

func (control *Control) ControlValue() *OCTETSTRING {
	return control.controlValue
}

func readControl(bytes *Bytes) (control Control, err error) {
	err = bytes.ReadSubBytes(classUniversal, tagSequence, control.readComponents)
	if err != nil {
		err = LdapError{fmt.Sprintf("readControl:\n%s", err.Error())}
		return
	}
	return
}

func (control *Control) readComponents(bytes *Bytes) (err error) {
	control.controlType, err = readLDAPOID(bytes)
	if err != nil {
		err = LdapError{fmt.Sprintf("readComponents:\n%s", err.Error())}
		return
	}
	if bytes.HasMoreData() {
		var tag TagAndLength
		tag, err = bytes.PreviewTagAndLength()
		if err != nil {
			err = LdapError{fmt.Sprintf("readComponents:\n%s", err.Error())}
			return
		}
		if tag.Tag == tagBoolean {
			control.criticality, err = readBOOLEAN(bytes)
			if err != nil {
				err = LdapError{fmt.Sprintf("readComponents:\n%s", err.Error())}
				return
			}
			if control.criticality == false {
				err = errors.New(fmt.Sprintf("readComponents: criticality default value FALSE should not be specified"))
				return
			}
		}
	}
	if bytes.HasMoreData() {
		var octetstring OCTETSTRING
		octetstring, err = readOCTETSTRING(bytes)
		if err != nil {
			err = LdapError{fmt.Sprintf("readComponents:\n%s", err.Error())}
			return
		}
		control.controlValue = octetstring.Pointer()
	}
	return
}

func (control Control) write(bytes *Bytes) (size int) {
	if control.controlValue != nil {
		size += control.controlValue.write(bytes)
	}
	if control.criticality != BOOLEAN(false) {
		size += control.criticality.write(bytes)
	}
	size += control.controlType.write(bytes)
	size += bytes.WriteTagAndLength(classUniversal, isCompound, tagSequence, size)
	return
}

func (control Control) size() (size int) {
	if control.controlValue != nil {
		size += control.controlValue.size()
	}
	if control.criticality != BOOLEAN(false) {
		size += control.criticality.size()
	}
	size += control.controlType.size()
	size += sizeTagAndLength(tagSequence, size)
	return
}