dns-packet-typescript
Version:
An abstract-encoding compliant module for encoding / decoding DNS packets
52 lines • 1.67 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.encodingLength = exports.decode = exports.encode = exports.ISSUER_CRITICAL = void 0;
var string = require("./string");
exports.ISSUER_CRITICAL = 1 << 7;
function encode(data, buf, offset) {
if (offset === void 0) { offset = 0; }
var len = encodingLength(data);
if (!buf)
buf = Buffer.allocUnsafe(encodingLength(data));
if (!offset)
offset = 0;
if (data.issuerCritical) {
data.flags = exports.ISSUER_CRITICAL;
}
buf.writeUInt16BE(len - 2, offset);
offset += 2;
buf.writeUInt8(data.flags || 0, offset);
offset += 1;
string.encode(data.tag, buf, offset);
offset += string.encode.bytes;
buf.write(data.value, offset);
offset += Buffer.byteLength(data.value);
encode.bytes = len;
return buf;
}
exports.encode = encode;
encode.bytes = 0;
function decode(buf, offset) {
if (offset === void 0) { offset = 0; }
if (!offset)
offset = 0;
var len = buf.readUInt16BE(offset);
offset += 2;
var oldOffset = offset;
var data = {};
data.flags = buf.readUInt8(offset);
offset += 1;
data.tag = string.decode(buf, offset);
offset += string.decode.bytes;
data.value = buf.toString('utf-8', offset, oldOffset + len);
data.issuerCritical = !!(data.flags & exports.ISSUER_CRITICAL);
decode.bytes = len + 2;
return data;
}
exports.decode = decode;
decode.bytes = 0;
function encodingLength(data) {
return string.encodingLength(data.tag) + string.encodingLength(data.value) + 2;
}
exports.encodingLength = encodingLength;
//# sourceMappingURL=rcaa.js.map
;