2 * Copyright (c) 2014-2015 Sylvain Peyrefitte
4 * This file is part of node-rdpjs.
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.
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.
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/>.
20var type = require('../../core').type;
21var error = require('../../core').error;
24 * @param s {type.Stream} read value from stream
25 * @returns read length from per format
27function readLength(s) {
28 var byte = new type.UInt8().read(s).value;
33 size += new type.UInt8().read(s).value;
42 * @param value {raw} value to convert to per format
43 * @returns type objects per encoding value
45function writeLength(value) {
47 return new type.UInt16Be(value | 0x8000);
50 return new type.UInt8(value);
55 * @param s {type.Stream}
56 * @returns {integer} choice decoding from per encoding
58function readChoice(s) {
59 return new type.UInt8().read(s).value;
63 * @param choice {integer}
64 * @returns {type.UInt8} choice per encoded
66function writeChoice(choice) {
67 return new type.UInt8(choice);
71 * @param s {type.Stream}
72 * @returns {integer} number represent selection
74function readSelection(s) {
75 return new type.UInt8().read(s).value;
79 * @param selection {integer}
80 * @returns {type.UInt8} per encoded selection
82function writeSelection(selection) {
83 return new type.UInt8(selection);
87 * @param s {type.Stream}
88 * @returns {integer} number of sets
90function readNumberOfSet(s) {
91 return new type.UInt8().read(s).value;
95 * @param numberOfSet {integer}
96 * @returns {type.UInt8} per encoded nuimber of sets
98function writeNumberOfSet(numberOfSet) {
99 return new type.UInt8(numberOfSet);
103 * @param s {type.Stream}
104 * @returns {integer} enumerates number
106function readEnumerates(s) {
107 return new type.UInt8().read(s).value;
111 * @param enumerate {integer}
112 * @returns {type.UInt8} per encoded enumerate
114function writeEnumerates(enumerate) {
115 return new type.UInt8(enumerate);
119 * @param s {type.Stream}
120 * @returns {integer} integer per decoded
122function readInteger(s) {
124 var size = readLength(s);
127 result = new type.UInt8();
130 result = new type.UInt16Be();
133 result = new type.UInt32Be();
136 throw new error.UnexpectedFatalError('NODE_RDP_PROTOCOL_T125_PER_BAD_INTEGER_LENGTH');
138 return result.read(s).value;
142 * @param value {integer}
143 * @returns {type.Component} per encoded integer
145function writeInteger(value) {
147 return new type.Component([writeLength(1), new type.UInt8(value)]);
149 else if(value < 0xffff) {
150 return new type.Component([writeLength(2), new type.UInt16Be(value)]);
153 return new type.Component([writeLength(4), new type.UInt32Be(value)]);
158 * @param s {type.Stream}
159 * @param minimum {integer} increment (default 0)
160 * @returns {integer} per decoded integer 16 bits
162function readInteger16(s, minimum) {
163 return new type.UInt16Be().read(s).value + (minimum || 0);
167 * @param value {integer}
168 * @param minimum {integer} decrement (default 0)
169 * @returns {type.UInt16Be} per encoded integer 16 bits
171function writeInteger16(value, minimum) {
172 return new type.UInt16Be(value - (minimum || 0));
176 * Check object identifier
177 * @param s {type.Stream}
178 * @param oid {array} object identifier to check
180function readObjectIdentifier(s, oid) {
181 var size = readLength(s);
186 var a_oid = [0, 0, 0, 0, 0, 0];
187 var t12 = new type.UInt8().read(s).value;
189 a_oid[1] = t12 & 0x0f;
190 a_oid[2] = new type.UInt8().read(s).value;
191 a_oid[3] = new type.UInt8().read(s).value;
192 a_oid[4] = new type.UInt8().read(s).value;
193 a_oid[5] = new type.UInt8().read(s).value;
196 if(oid[i] !== a_oid[i]) return false;
203 * @param oid {array} oid to write
204 * @returns {type.Component} per encoded object identifier
206function writeObjectIdentifier(oid) {
207 return new type.Component([new type.UInt8(5), new type.UInt8((oid[0] << 4) & (oid[1] & 0x0f)), new type.UInt8(oid[2]), new type.UInt8(oid[3]), new type.UInt8(oid[4]), new type.UInt8(oid[5])]);
212 * @param s {type.Stream}
215function readNumericString(s, minValue) {
216 var length = readLength(s);
217 length = (length + minValue + 1) / 2;
218 s.readPadding(length);
222 * @param nStr {String}
223 * @param minValue {integer}
224 * @returns {type.Component} per encoded numeric string
226function writeNumericString(nStr, minValue) {
227 var length = nStr.length;
228 var mlength = minValue;
229 if(length - minValue >= 0) {
230 mlength = length - minValue;
235 for(var i = 0; i < length; i += 2) {
236 var c1 = nStr.charCodeAt(i);
239 c2 = nStr.charCodeAt(i + 1);
244 c1 = (c1 - 0x30) % 10;
245 c2 = (c2 - 0x30) % 10;
247 result[result.length] = new type.UInt8((c1 << 4) | c2);
250 return new type.Component([writeLength(mlength), new type.Component(result)]);
254 * @param s {type.Stream}
255 * @param length {integer} length of padding
257function readPadding(s, length) {
258 s.readPadding(length);
262 * @param length {integer} length of padding
263 * @returns {type.BinaryString} per encoded padding
265function writePadding(length) {
266 return new type.BinaryString(Buffer.from(Array(length + 1).join("\x00")));
270 * @param s {type.Stream}
271 * @param octetStream {String}
272 * @param minValue {integer} default 0
273 * @returns {Boolean} true if read octectStream is equal to octetStream
275function readOctetStream(s, octetStream, minValue) {
276 var size = readLength(s) + (minValue || 0);
277 if(size !== octetStream.length) {
280 for(var i = 0; i < size; i++) {
281 var c = new type.UInt8().read(s);
282 if(octetStream.charCodeAt(i) !== c.value) {
291 * @param oStr {String}
292 * @param minValue {integer} default 0
293 * @returns {type.Component} per encoded octet stream
295function writeOctetStream(oStr, minValue) {
296 minValue = minValue || 0;
297 var length = oStr.length;
298 var mlength = minValue;
300 if(length - minValue >= 0) {
301 mlength = length - minValue;
305 for(var i = 0; i < length; i++) {
306 result[result.length] = new type.UInt8(oStr[i]);
309 return new type.Component([writeLength(mlength), new type.Component(result)]);
316 readLength : readLength,
317 writeLength : writeLength,
318 readChoice : readChoice,
319 writeChoice : writeChoice,
320 readSelection : readSelection,
321 writeSelection : writeSelection,
322 readNumberOfSet : readNumberOfSet,
323 writeNumberOfSet : writeNumberOfSet,
324 readEnumerates : readEnumerates,
325 writeEnumerates : writeEnumerates,
326 readInteger : readInteger,
327 writeInteger : writeInteger,
328 readInteger16 : readInteger16,
329 writeInteger16 : writeInteger16,
330 readObjectIdentifier : readObjectIdentifier,
331 writeObjectIdentifier : writeObjectIdentifier,
332 readNumericString : readNumericString,
333 writeNumericString : writeNumericString,
334 readPadding : readPadding,
335 writePadding : writePadding,
336 readOctetStream : readOctetStream,
337 writeOctetStream : writeOctetStream