spec.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2014-2015 Sylvain Peyrefitte
  3. *
  4. * This file is part of node-rdpjs.
  5. *
  6. * node-rdpjs is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. var inherits = require('util').inherits;
  20. var type = require('../core').type;
  21. var error = require('../core').error;
  22. /**
  23. * Tag Class
  24. */
  25. var TagClass = {
  26. Universal : 0x00,
  27. Application : 0x40,
  28. Context : 0x80,
  29. Private : 0xC0
  30. };
  31. /**
  32. * Tag Format
  33. */
  34. var TagFormat = {
  35. Primitive : 0x00,
  36. Constructed : 0x20
  37. };
  38. /**
  39. * ASN.1 tag
  40. * @param tagClass {TagClass}
  41. * @param tagFormat {TagFormat}
  42. * @param tagNumber {integer}
  43. */
  44. function Asn1Tag(tagClass, tagFormat, tagNumber) {
  45. this.tagClass = tagClass;
  46. this.tagFormat = tagFormat;
  47. this.tagNumber = tagNumber;
  48. }
  49. /**
  50. * ASN.1 Specification
  51. * @param tag {Asn1Tag}
  52. */
  53. function Asn1Spec(tag) {
  54. this.tag = tag;
  55. this.opt = false;
  56. }
  57. /**
  58. * Add an implicit tag
  59. * override tag
  60. * @param tag {Asn1Tag}
  61. * @returns {Asn1Spec}
  62. */
  63. Asn1Spec.prototype.implicitTag = function(tag) {
  64. this.tag = tag;
  65. return this;
  66. };
  67. /**
  68. * Set optional to true
  69. * @returns {Asn1Spec}
  70. */
  71. Asn1Spec.prototype.optional = function() {
  72. this.opt = true;
  73. return this;
  74. };
  75. /**
  76. * Add explicit tag
  77. * Append new tag header to existing tag
  78. * @param tag {Asn1Tag}
  79. * @returns {Asn1SpecExplicitTag}
  80. */
  81. Asn1Spec.prototype.explicitTag = function(tag) {
  82. return new Asn1SpecExplicitTag(tag, this);
  83. };
  84. /**
  85. * Decode must be implemented by all sub type
  86. * @param s {type.Stream}
  87. * @param decoder
  88. */
  89. Asn1Spec.prototype.decode = function(s, decoder) {
  90. throw new error.FatalError('NODE_RDP_AS1_SPEC_DECODE_NOT_IMPLEMENTED');
  91. };
  92. /**
  93. * Encode must be implemented by all sub type
  94. * @param decoder
  95. */
  96. Asn1Spec.prototype.encode = function(encoder) {
  97. throw new error.FatalError('NODE_RDP_AS1_SPEC_ENCODE_NOT_IMPLEMENTED');
  98. };
  99. /**
  100. * Component Asn1Spec object
  101. */
  102. function Asn1SpecExplicitTag(tag, spec) {
  103. Asn1Spec.call(this, tag);
  104. this.spec = spec;
  105. }
  106. inherits(Asn1SpecExplicitTag, Asn1Spec);
  107. /**
  108. * Decode first header
  109. * @param s {type.Stream}
  110. * @param decoder
  111. */
  112. Asn1Spec.prototype.decode = function(s, decoder) {
  113. var specStream = new type.Stream(decoder.decode(s, this.tag).value);
  114. this.spec.decode(specStream, decoder);
  115. };
  116. /**
  117. * Module exports
  118. */
  119. module.exports = {
  120. TagClass : TagClass,
  121. TagFormat : TagFormat,
  122. Asn1Tag : Asn1Tag,
  123. Asn1Spec : Asn1Spec,
  124. Asn1SpecExplicitTag : Asn1SpecExplicitTag
  125. };