Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

attribute.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Copyright 2011 Mark Cavage, Inc. All rights reserved.
  2. var assert = require('assert');
  3. var asn1 = require('asn1');
  4. var Protocol = require('./protocol');
  5. ///--- API
  6. function Attribute(options) {
  7. if (options) {
  8. if (typeof (options) !== 'object')
  9. throw new TypeError('options must be an object');
  10. if (options.type && typeof (options.type) !== 'string')
  11. throw new TypeError('options.type must be a string');
  12. } else {
  13. options = {};
  14. }
  15. this.type = options.type || '';
  16. this._vals = [];
  17. if (options.vals !== undefined && options.vals !== null)
  18. this.vals = options.vals;
  19. }
  20. module.exports = Attribute;
  21. Object.defineProperties(Attribute.prototype, {
  22. buffers: {
  23. get: function getBuffers() {
  24. return this._vals;
  25. },
  26. configurable: false
  27. },
  28. json: {
  29. get: function getJson() {
  30. return {
  31. type: this.type,
  32. vals: this.vals
  33. };
  34. },
  35. configurable: false
  36. },
  37. vals: {
  38. get: function getVals() {
  39. var eType = _bufferEncoding(this.type);
  40. return this._vals.map(function (v) {
  41. return v.toString(eType);
  42. });
  43. },
  44. set: function setVals(vals) {
  45. var self = this;
  46. this._vals = [];
  47. if (Array.isArray(vals)) {
  48. vals.forEach(function (v) {
  49. self.addValue(v);
  50. });
  51. } else {
  52. self.addValue(vals);
  53. }
  54. },
  55. configurable: false
  56. }
  57. });
  58. Attribute.prototype.addValue = function addValue(val) {
  59. if (Buffer.isBuffer(val)) {
  60. this._vals.push(val);
  61. } else {
  62. this._vals.push(new Buffer(val + '', _bufferEncoding(this.type)));
  63. }
  64. };
  65. /* BEGIN JSSTYLED */
  66. Attribute.compare = function compare(a, b) {
  67. if (!(Attribute.isAttribute(a)) || !(Attribute.isAttribute(b))) {
  68. throw new TypeError('can only compare Attributes');
  69. }
  70. if (a.type < b.type) return -1;
  71. if (a.type > b.type) return 1;
  72. if (a.vals.length < b.vals.length) return -1;
  73. if (a.vals.length > b.vals.length) return 1;
  74. for (var i = 0; i < a.vals.length; i++) {
  75. if (a.vals[i] < b.vals[i]) return -1;
  76. if (a.vals[i] > b.vals[i]) return 1;
  77. }
  78. return 0;
  79. };
  80. /* END JSSTYLED */
  81. Attribute.prototype.parse = function parse(ber) {
  82. assert.ok(ber);
  83. ber.readSequence();
  84. this.type = ber.readString();
  85. if (ber.peek() === Protocol.LBER_SET) {
  86. if (ber.readSequence(Protocol.LBER_SET)) {
  87. var end = ber.offset + ber.length;
  88. while (ber.offset < end)
  89. this._vals.push(ber.readString(asn1.Ber.OctetString, true));
  90. }
  91. }
  92. return true;
  93. };
  94. Attribute.prototype.toBer = function toBer(ber) {
  95. assert.ok(ber);
  96. ber.startSequence();
  97. ber.writeString(this.type);
  98. ber.startSequence(Protocol.LBER_SET);
  99. if (this._vals.length) {
  100. this._vals.forEach(function (b) {
  101. ber.writeByte(asn1.Ber.OctetString);
  102. ber.writeLength(b.length);
  103. for (var i = 0; i < b.length; i++)
  104. ber.writeByte(b[i]);
  105. });
  106. } else {
  107. ber.writeStringArray([]);
  108. }
  109. ber.endSequence();
  110. ber.endSequence();
  111. return ber;
  112. };
  113. Attribute.prototype.toString = function () {
  114. return JSON.stringify(this.json);
  115. };
  116. Attribute.toBer = function (attr, ber) {
  117. return Attribute.prototype.toBer.call(attr, ber);
  118. };
  119. Attribute.isAttribute = function isAttribute(attr) {
  120. if (!attr || typeof (attr) !== 'object') {
  121. return false;
  122. }
  123. if (attr instanceof Attribute) {
  124. return true;
  125. }
  126. if ((typeof (attr.toBer) === 'function') &&
  127. (typeof (attr.type) === 'string') &&
  128. (Array.isArray(attr.vals)) &&
  129. (attr.vals.filter(function (item) {
  130. return (typeof (item) === 'string' ||
  131. Buffer.isBuffer(item));
  132. }).length === attr.vals.length)) {
  133. return true;
  134. }
  135. return false;
  136. };
  137. function _bufferEncoding(type) {
  138. /* JSSTYLED */
  139. return /;binary$/.test(type) ? 'base64' : 'utf8';
  140. }