UNPKG

1.73 kBJavaScriptView Raw
1// Copyright 2011 Mark Cavage, Inc. All rights reserved.
2
3var assert = require('assert');
4var util = require('util');
5
6var parents = require('ldap-filter');
7
8var Filter = require('./filter');
9
10
11///--- API
12
13function SubstringFilter(options) {
14 parents.SubstringFilter.call(this, options);
15}
16util.inherits(SubstringFilter, parents.SubstringFilter);
17Filter.mixin(SubstringFilter);
18module.exports = SubstringFilter;
19
20
21SubstringFilter.prototype.parse = function (ber) {
22 assert.ok(ber);
23
24 this.attribute = ber.readString().toLowerCase();
25 ber.readSequence();
26 var end = ber.offset + ber.length;
27
28 while (ber.offset < end) {
29 var tag = ber.peek();
30 switch (tag) {
31 case 0x80: // Initial
32 this.initial = ber.readString(tag);
33 if (this.attribute === 'objectclass')
34 this.initial = this.initial.toLowerCase();
35 break;
36 case 0x81: // Any
37 var anyVal = ber.readString(tag);
38 if (this.attribute === 'objectclass')
39 anyVal = anyVal.toLowerCase();
40 this.any.push(anyVal);
41 break;
42 case 0x82: // Final
43 this.final = ber.readString(tag);
44 if (this.attribute === 'objectclass')
45 this.final = this.final.toLowerCase();
46 break;
47 default:
48 throw new Error('Invalid substrings filter type: 0x' + tag.toString(16));
49 }
50 }
51
52 return true;
53};
54
55
56SubstringFilter.prototype._toBer = function (ber) {
57 assert.ok(ber);
58
59 ber.writeString(this.attribute);
60 ber.startSequence();
61
62 if (this.initial)
63 ber.writeString(this.initial, 0x80);
64
65 if (this.any && this.any.length)
66 this.any.forEach(function (s) {
67 ber.writeString(s, 0x81);
68 });
69
70 if (this.final)
71 ber.writeString(this.final, 0x82);
72
73 ber.endSequence();
74
75 return ber;
76};