master
js 139 lines 2.9 KB
Raw
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
20 var inherits = require('util').inherits;
21 var type = require('../core').type;
22 var error = require('../core').error;
23
24 /**
25 * Tag Class
26 */
27 var TagClass = {
28 Universal : 0x00,
29 Application : 0x40,
30 Context : 0x80,
31 Private : 0xC0
32 };
33
34 /**
35 * Tag Format
36 */
37 var TagFormat = {
38 Primitive : 0x00,
39 Constructed : 0x20
40 };
41
42 /**
43 * ASN.1 tag
44 * @param tagClass {TagClass}
45 * @param tagFormat {TagFormat}
46 * @param tagNumber {integer}
47 */
48 function Asn1Tag(tagClass, tagFormat, tagNumber) {
49 this.tagClass = tagClass;
50 this.tagFormat = tagFormat;
51 this.tagNumber = tagNumber;
52 }
53
54 /**
55 * ASN.1 Specification
56 * @param tag {Asn1Tag}
57 */
58 function Asn1Spec(tag) {
59 this.tag = tag;
60 this.opt = false;
61 }
62
63 /**
64 * Add an implicit tag
65 * override tag
66 * @param tag {Asn1Tag}
67 * @returns {Asn1Spec}
68 */
69 Asn1Spec.prototype.implicitTag = function(tag) {
70 this.tag = tag;
71 return this;
72 };
73
74 /**
75 * Set optional to true
76 * @returns {Asn1Spec}
77 */
78 Asn1Spec.prototype.optional = function() {
79 this.opt = true;
80 return this;
81 };
82
83 /**
84 * Add explicit tag
85 * Append new tag header to existing tag
86 * @param tag {Asn1Tag}
87 * @returns {Asn1SpecExplicitTag}
88 */
89 Asn1Spec.prototype.explicitTag = function(tag) {
90 return new Asn1SpecExplicitTag(tag, this);
91 };
92
93 /**
94 * Decode must be implemented by all sub type
95 * @param s {type.Stream}
96 * @param decoder
97 */
98 Asn1Spec.prototype.decode = function(s, decoder) {
99 throw new error.FatalError('NODE_RDP_AS1_SPEC_DECODE_NOT_IMPLEMENTED');
100 };
101
102 /**
103 * Encode must be implemented by all sub type
104 * @param decoder
105 */
106 Asn1Spec.prototype.encode = function(encoder) {
107 throw new error.FatalError('NODE_RDP_AS1_SPEC_ENCODE_NOT_IMPLEMENTED');
108 };
109
110 /**
111 * Component Asn1Spec object
112 */
113 function Asn1SpecExplicitTag(tag, spec) {
114 Asn1Spec.call(this, tag);
115 this.spec = spec;
116 }
117
118 inherits(Asn1SpecExplicitTag, Asn1Spec);
119
120 /**
121 * Decode first header
122 * @param s {type.Stream}
123 * @param decoder
124 */
125 Asn1Spec.prototype.decode = function(s, decoder) {
126 var specStream = new type.Stream(decoder.decode(s, this.tag).value);
127 this.spec.decode(specStream, decoder);
128 };
129
130 /**
131 * Module exports
132 */
133 module.exports = {
134 TagClass : TagClass,
135 TagFormat : TagFormat,
136 Asn1Tag : Asn1Tag,
137 Asn1Spec : Asn1Spec,
138 Asn1SpecExplicitTag : Asn1SpecExplicitTag
139 };