| 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 type = require('../core').type; |
| 21 | var log = require('../core').log; |
| 22 | var error = require('../core').error; |
| 23 | |
| 24 | /** |
| 25 | * Parse tag(T) field of BER TLV |
| 26 | * And check with expected tag |
| 27 | * @param s {type.Stream} |
| 28 | * @param tag {spec.tag} |
| 29 | * @returns {Boolean} True for valid tag matching |
| 30 | */ |
| 31 | function decodeTag(s, tag) { |
| 32 | var nextTag = new type.UInt8().read(s).value; |
| 33 | if (tag.tagNumber > 30) { |
| 34 | nextTagNumber = new type.UInt8().read(s).value; |
| 35 | } |
| 36 | else { |
| 37 | nextTagNumber = nextTag & 0x1F; |
| 38 | } |
| 39 | |
| 40 | return ((nextTag & 0xE0) === (tag.tagClass | tag.tagFormat)) && (nextTagNumber === tag.tagNumber); |
| 41 | }; |
| 42 | |
| 43 | /** |
| 44 | * Parse length(L) field of BER TLV |
| 45 | * @param s {type.Stream} |
| 46 | * @returns {integer} |
| 47 | */ |
| 48 | function decodeLength(s) { |
| 49 | var size = new type.UInt8().read(s).value; |
| 50 | if(size & 0x80) { |
| 51 | size &= ~0x80; |
| 52 | if(size === 1) { |
| 53 | size = new type.UInt8().read(s).value; |
| 54 | } |
| 55 | else if(size === 2) { |
| 56 | size = new type.UInt16Be().read(s).value; |
| 57 | } |
| 58 | else{ |
| 59 | throw new error.ProtocolError('NODE_RDP_ASN1_BER_INVALID_LENGTH'); |
| 60 | } |
| 61 | } |
| 62 | return size; |
| 63 | }; |
| 64 | |
| 65 | /** |
| 66 | * Decode tuple TLV (Tag Length Value) of BER |
| 67 | * @param s {type.Stream} |
| 68 | * @param tag {spec.Asn1Tag} expected tag |
| 69 | * @returns {type.BinaryString} Value of tuple |
| 70 | */ |
| 71 | function decode(s, tag) { |
| 72 | if (!decodeTag(s, tag)) { |
| 73 | throw new error.ProtocolError('NODE_RDP_ASN1_BER_INVALID_TAG'); |
| 74 | } |
| 75 | var length = decodeLength(s); |
| 76 | |
| 77 | if (length === 0) { |
| 78 | return new type.Stream(0); |
| 79 | } |
| 80 | return new type.BinaryString(null,{ readLength : new type.CallableValue(length) }).read(s); |
| 81 | }; |
| 82 | |
| 83 | function encodeTag(tag) { |
| 84 | if(tag.tagNumber > 30) { |
| 85 | return new type.Component([new type.UInt8(tag.tagClass | tag.tagFormat | 0x1F), new type.UInt8(tag.tagNumber)]); |
| 86 | } |
| 87 | else { |
| 88 | return new type.UInt8((tag.tagClass | tag.tagFormat) | (tag.tagNumber & 0x1F)); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | function encodeLength(length) { |
| 93 | if(length > 0x7f) { |
| 94 | return new type.Component([new type.UInt8(0x82), new type.UInt16Be(length)]); |
| 95 | } |
| 96 | else { |
| 97 | return new type.UInt8(length); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | function encode(tag, buffer) { |
| 102 | return new type.Component([encodeTag(tag), encodeLength(buffer.size()), buffer]); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Module Export |
| 107 | */ |
| 108 | module.exports = { |
| 109 | decode : decode, |
| 110 | encode : encode |
| 111 | }; |