2011-08-04 20:32:01 +00:00
|
|
|
// Copyright 2011 Mark Cavage, Inc. All rights reserved.
|
|
|
|
|
|
|
|
var assert = require('assert');
|
|
|
|
|
2011-10-11 20:56:16 +00:00
|
|
|
var asn1 = require('asn1');
|
|
|
|
|
2011-08-04 20:32:01 +00:00
|
|
|
var Protocol = require('./protocol');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///--- API
|
|
|
|
|
|
|
|
function Attribute(options) {
|
|
|
|
if (options) {
|
2012-02-18 08:15:52 +00:00
|
|
|
if (typeof (options) !== 'object')
|
2011-08-04 20:32:01 +00:00
|
|
|
throw new TypeError('options must be an object');
|
2012-02-18 08:15:52 +00:00
|
|
|
if (options.type && typeof (options.type) !== 'string')
|
2011-08-04 20:32:01 +00:00
|
|
|
throw new TypeError('options.type must be a string');
|
|
|
|
} else {
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
2011-10-11 20:56:16 +00:00
|
|
|
var self = this;
|
|
|
|
|
2011-08-04 20:32:01 +00:00
|
|
|
this.type = options.type || '';
|
2011-10-11 20:56:16 +00:00
|
|
|
this._vals = [];
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
this.__defineGetter__('vals', function () {
|
2011-10-11 20:56:16 +00:00
|
|
|
var _vals = [];
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
self._vals.forEach(function (v) {
|
|
|
|
/* JSSTYLED */
|
2011-10-11 20:56:16 +00:00
|
|
|
if (/;binary$/.test(self.type)) {
|
|
|
|
_vals.push(v.toString('base64'));
|
|
|
|
} else {
|
|
|
|
_vals.push(v.toString('utf8'));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return _vals;
|
|
|
|
});
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
this.__defineSetter__('vals', function (vals) {
|
2011-10-11 20:56:16 +00:00
|
|
|
if (Array.isArray(vals)) {
|
2012-02-18 08:15:52 +00:00
|
|
|
vals.forEach(function (v) {
|
2011-10-11 20:56:16 +00:00
|
|
|
self.addValue(v);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
self.addValue(vals);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
this.__defineGetter__('buffers', function () {
|
2011-10-11 20:56:16 +00:00
|
|
|
return self._vals;
|
|
|
|
});
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
this.__defineGetter__('json', function () {
|
2011-08-04 20:32:01 +00:00
|
|
|
return {
|
|
|
|
type: self.type,
|
|
|
|
vals: self.vals
|
|
|
|
};
|
|
|
|
});
|
2011-10-11 20:56:16 +00:00
|
|
|
|
|
|
|
if (options.vals)
|
|
|
|
this.vals = options.vals;
|
|
|
|
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
module.exports = Attribute;
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
Attribute.prototype.addValue = function (val) {
|
2011-10-11 20:56:16 +00:00
|
|
|
if (Buffer.isBuffer(val)) {
|
|
|
|
this._vals.push(val);
|
|
|
|
} else {
|
|
|
|
var encoding = 'utf8';
|
2012-02-18 08:15:52 +00:00
|
|
|
/* JSSTYLED */
|
2011-10-11 20:56:16 +00:00
|
|
|
if (/;binary$/.test(this.type))
|
|
|
|
encoding = 'base64';
|
|
|
|
this._vals.push(new Buffer(val + '', encoding));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
/* BEGIN JSSTYLED */
|
|
|
|
Attribute.compare = function compare(a, b) {
|
2011-08-08 15:03:00 +00:00
|
|
|
if (!(a instanceof Attribute) || !(b instanceof Attribute))
|
|
|
|
throw new TypeError('can only compare Attributes');
|
|
|
|
|
|
|
|
if (a.type < b.type) return -1;
|
|
|
|
if (a.type > b.type) return 1;
|
|
|
|
if (a.vals.length < b.vals.length) return -1;
|
|
|
|
if (a.vals.length > b.vals.length) return 1;
|
|
|
|
|
|
|
|
for (var i = 0; i < a.vals.length; i++) {
|
|
|
|
if (a.vals[i] < b.vals[i]) return -1;
|
|
|
|
if (a.vals[i] > b.vals[i]) return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
};
|
2012-02-18 08:15:52 +00:00
|
|
|
/* END JSSTYLED */
|
2011-08-08 15:03:00 +00:00
|
|
|
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
Attribute.prototype.parse = function (ber) {
|
2011-08-04 20:32:01 +00:00
|
|
|
assert.ok(ber);
|
|
|
|
|
|
|
|
ber.readSequence();
|
2011-10-04 17:27:43 +00:00
|
|
|
this.type = ber.readString();
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2011-09-23 22:47:02 +00:00
|
|
|
if (ber.peek() === Protocol.LBER_SET) {
|
|
|
|
if (ber.readSequence(Protocol.LBER_SET)) {
|
|
|
|
var end = ber.offset + ber.length;
|
|
|
|
while (ber.offset < end)
|
2011-10-11 20:56:16 +00:00
|
|
|
this._vals.push(ber.readString(asn1.Ber.OctetString, true));
|
2011-09-23 22:47:02 +00:00
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
Attribute.prototype.toBer = function (ber) {
|
2011-08-04 20:32:01 +00:00
|
|
|
assert.ok(ber);
|
|
|
|
|
|
|
|
ber.startSequence();
|
|
|
|
ber.writeString(this.type);
|
2011-09-30 17:48:56 +00:00
|
|
|
ber.startSequence(Protocol.LBER_SET);
|
2011-10-11 22:24:00 +00:00
|
|
|
if (this._vals.length) {
|
2012-02-18 08:15:52 +00:00
|
|
|
this._vals.forEach(function (b) {
|
2011-10-11 22:24:00 +00:00
|
|
|
ber.writeByte(asn1.Ber.OctetString);
|
|
|
|
ber.writeLength(b.length);
|
|
|
|
for (var i = 0; i < b.length; i++)
|
|
|
|
ber.writeByte(b[i]);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
ber.writeStringArray([]);
|
|
|
|
}
|
2011-09-30 17:48:56 +00:00
|
|
|
ber.endSequence();
|
2011-08-04 20:32:01 +00:00
|
|
|
ber.endSequence();
|
|
|
|
|
|
|
|
return ber;
|
|
|
|
};
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
Attribute.toBer = function (attr, ber) {
|
2011-08-04 20:32:01 +00:00
|
|
|
return Attribute.prototype.toBer.call(attr, ber);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
/* BEGIN JSSTYLED */
|
|
|
|
Attribute.isAttribute = function (attr) {
|
2011-08-04 21:25:12 +00:00
|
|
|
if (!attr) return false;
|
2012-02-18 08:15:52 +00:00
|
|
|
if (typeof (attr) !== 'object') return false;
|
2011-08-04 20:32:01 +00:00
|
|
|
if (attr instanceof Attribute) return true;
|
2012-02-18 08:15:52 +00:00
|
|
|
if (!attr.type || typeof (attr.type) !== 'string') return false;
|
2011-08-04 20:32:01 +00:00
|
|
|
if (!attr.vals || !Array.isArray(attr.vals)) return false;
|
2012-01-24 17:43:46 +00:00
|
|
|
for (var i = 0; i < attr.vals.length; i++) {
|
2012-02-18 08:15:52 +00:00
|
|
|
if (typeof (attr.vals[i]) !== 'string' && !Buffer.isBuffer(attr.vals[i]))
|
2011-10-11 20:56:16 +00:00
|
|
|
return false;
|
2012-01-24 17:43:46 +00:00
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
2012-02-18 08:15:52 +00:00
|
|
|
/* END JSSTLYED */
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
Attribute.prototype.toString = function () {
|
2011-08-04 20:32:01 +00:00
|
|
|
return JSON.stringify(this.json);
|
|
|
|
};
|