[Pkg-javascript-commits] [node-asn1.js] 143/202: node: introduce `.contains()` method

Bastien Roucariès rouca at moszumanska.debian.org
Thu Apr 20 19:19:02 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-asn1.js.

commit a2d711e8645333140daf370fe1ab93eccec2671a
Author: Felix Hanley <felix at userspace.com.au>
Date:   Tue Feb 2 13:51:46 2016 +0700

    node: introduce `.contains()` method
---
 lib/asn1/base/node.js   | 25 ++++++++++++++++++++++---
 test/der-decode-test.js | 14 ++++++++++++++
 test/der-encode-test.js | 14 ++++++++++++++
 3 files changed, 50 insertions(+), 3 deletions(-)

diff --git a/lib/asn1/base/node.js b/lib/asn1/base/node.js
index 83fd15d..192a411 100644
--- a/lib/asn1/base/node.js
+++ b/lib/asn1/base/node.js
@@ -1,5 +1,6 @@
 var Reporter = require('../base').Reporter;
 var EncoderBuffer = require('../base').EncoderBuffer;
+var DecoderBuffer = require('../base').DecoderBuffer;
 var assert = require('minimalistic-assert');
 
 // Supported tags
@@ -12,7 +13,7 @@ var tags = [
 // Public methods list
 var methods = [
   'key', 'obj', 'use', 'optional', 'explicit', 'implicit', 'def', 'choice',
-  'any'
+  'any', 'contains'
 ].concat(tags);
 
 // Overrided methods list
@@ -48,6 +49,7 @@ function Node(enc, parent) {
   state['default'] = null;
   state.explicit = null;
   state.implicit = null;
+  state.contains = null;
 
   // Should create new instance on each method
   if (!state.parent) {
@@ -252,6 +254,15 @@ Node.prototype.choice = function choice(obj) {
   return this;
 };
 
+Node.prototype.contains = function contains(item) {
+  var state = this._baseState;
+
+  assert(state.use === null);
+  state.contains = item;
+
+  return this;
+};
+
 //
 // Decoding
 //
@@ -354,6 +365,12 @@ Node.prototype._decode = function decode(input) {
       if (fail)
         return err;
     }
+
+    // Decode contained/encoded by schema, only in bit or octet strings
+    if (state.contains && (state.tag === 'octstr' || state.tag === 'bitstr')) {
+      var data = new DecoderBuffer(result);
+      result = this._getUse(state.contains, input._reporterState.obj)._decode(data);
+    }
   }
 
   // Pop object
@@ -497,6 +514,9 @@ Node.prototype._encodeValue = function encode(data, reporter, parent) {
     result = this._createEncoderBuffer(data);
   } else if (state.choice) {
     result = this._encodeChoice(data, reporter);
+  } else if (state.contains) {
+    content = this._getUse(state.contains, parent)._encode(data, reporter);
+    primitive = true;
   } else if (state.children) {
     content = state.children.map(function(child) {
       if (child._baseState.tag === 'null_')
@@ -516,7 +536,6 @@ Node.prototype._encodeValue = function encode(data, reporter, parent) {
     }, this).filter(function(child) {
       return child;
     });
-
     content = this._createEncoderBuffer(content);
   } else {
     if (state.tag === 'seqof' || state.tag === 'setof') {
@@ -608,4 +627,4 @@ Node.prototype._isNumstr = function isNumstr(str) {
 
 Node.prototype._isPrintstr = function isPrintstr(str) {
   return /^[A-Za-z0-9 '\(\)\+,\-\.\/:=\?]*$/.test(str);
-};
\ No newline at end of file
+};
diff --git a/test/der-decode-test.js b/test/der-decode-test.js
index d976ac4..6da6a9e 100644
--- a/test/der-decode-test.js
+++ b/test/der-decode-test.js
@@ -90,4 +90,18 @@ describe('asn1.js DER decoder', function() {
       '1.2.398.3.10.1.1.1.2.2': 'yes'
     });
   }, '060a2a830e030a0101010202', 'yes');
+
+  it('should decode encapsulated models', function() {
+    var B = asn1.define('B', function() {
+      this.seq().obj(
+        this.key('nested').int()
+      );
+    });
+    var A = asn1.define('A', function() {
+      this.octstr().contains(B);
+    });
+
+    var out = A.decode(new Buffer('04053003020105', 'hex'), 'der');
+    assert.equal(out.nested.toString(10), '5');
+  });
 });
diff --git a/test/der-encode-test.js b/test/der-encode-test.js
index 0700867..d32891b 100644
--- a/test/der-encode-test.js
+++ b/test/der-encode-test.js
@@ -100,4 +100,18 @@ describe('asn1.js DER encoder', function() {
   test('should properly encode bmpstr with cyrillic chars', function() {
     this.bmpstr();
   }, 'Привет', '1e0c041f04400438043204350442');
+
+  it('should encode encapsulated models', function() {
+    var B = asn1.define('B', function() {
+      this.seq().obj(
+        this.key('nested').int()
+      );
+    });
+    var A = asn1.define('A', function() {
+      this.octstr().contains(B);
+    });
+
+    var out = A.encode({ nested: 5 }, 'der')
+    assert.equal(out.toString('hex'), '04053003020105');
+  });
 });

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-asn1.js.git



More information about the Pkg-javascript-commits mailing list