[Pkg-javascript-commits] [node-asn1.js] 38/202: node: propagate implicit tag to uses

Bastien Roucariès rouca at moszumanska.debian.org
Thu Apr 20 19:18:52 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 edbc42f3a9d8eeca454f1aebd5b5ec596f76b35c
Author: Fedor Indutny <fedor at indutny.com>
Date:   Mon Jun 9 18:24:52 2014 -0700

    node: propagate implicit tag to uses
    
    fix #7
---
 lib/asn1/api.js          | 16 ++++++++++++----
 lib/asn1/base/node.js    | 39 ++++++++++++++++++++++++++++++++++++---
 lib/asn1/decoders/der.js |  4 ++--
 lib/asn1/encoders/der.js |  4 ++--
 test/der-decode-test.js  | 23 +++++++++++++++++++++++
 test/ping-pong-test.js   |  2 +-
 6 files changed, 76 insertions(+), 12 deletions(-)

diff --git a/lib/asn1/api.js b/lib/asn1/api.js
index 7468edc..a1032c2 100644
--- a/lib/asn1/api.js
+++ b/lib/asn1/api.js
@@ -28,16 +28,24 @@ Entity.prototype._createNamed = function createNamed(base) {
   return new named(this);
 };
 
-Entity.prototype.decode = function decode(data, enc, options) {
+Entity.prototype._getDecoder = function _getDecoder(enc) {
   // Lazily create decoder
   if (!this.decoders.hasOwnProperty(enc))
     this.decoders[enc] = this._createNamed(asn1.decoders[enc]);
-  return this.decoders[enc].decode(data, options);
+  return this.decoders[enc];
 };
 
-Entity.prototype.encode = function encode(data, enc, /* internal */ reporter) {
+Entity.prototype.decode = function decode(data, enc, options) {
+  return this._getDecoder(enc).decode(data, options);
+};
+
+Entity.prototype._getEncoder = function _getEncoder(enc) {
   // Lazily create encoder
   if (!this.encoders.hasOwnProperty(enc))
     this.encoders[enc] = this._createNamed(asn1.encoders[enc]);
-  return this.encoders[enc].encode(data, reporter);
+  return this.encoders[enc];
+};
+
+Entity.prototype.encode = function encode(data, enc, /* internal */ reporter) {
+  return this._getEncoder(enc).encode(data, reporter);
 };
diff --git a/lib/asn1/base/node.js b/lib/asn1/base/node.js
index dbfe2a5..2a60a67 100644
--- a/lib/asn1/base/node.js
+++ b/lib/asn1/base/node.js
@@ -42,6 +42,7 @@ function Node(enc, parent) {
   state.any = false;
   state.obj = false;
   state.use = null;
+  state.useDecoder = null;
   state.key = null;
   state['default'] = null;
   state.explicit = null;
@@ -55,6 +56,23 @@ function Node(enc, parent) {
 }
 module.exports = Node;
 
+var stateProps = [
+  'enc', 'parent', 'children', 'tag', 'args', 'reverseArgs', 'choice',
+  'optional', 'any', 'obj', 'use', 'alteredUse', 'key', 'default', 'explicit',
+  'implicit'
+];
+
+Node.prototype.clone = function clone() {
+  var state = this._baseState;
+  var cstate = {};
+  stateProps.forEach(function(prop) {
+    cstate[prop] = state[prop];
+  });
+  var res = new this.constructor(cstate.parent);
+  res._baseState = cstate;
+  return res;
+};
+
 Node.prototype._wrap = function wrap() {
   var state = this._baseState;
   methods.forEach(function(method) {
@@ -351,13 +369,28 @@ Node.prototype._decodeGeneric = function decodeGeneric(tag, input) {
   else if (tag === 'int' || tag === 'enum')
     return this._decodeInt(input, state.args && state.args[0]);
   else if (state.use !== null)
-    return this._use(input, state.use);
+    return this._getUse(state.use)._decode(input);
   else
     return input.error('unknown tag: ' + tag);
 
   return null;
 };
 
+Node.prototype._getUse = function _getUse(entity) {
+  var state = this._baseState;
+  if (!state.useDecoder) {
+    // Create altered use decoder if implicit is set
+    state.useDecoder = this._use(entity);
+    assert(state.useDecoder._baseState.parent === null);
+    state.useDecoder = state.useDecoder._baseState.children[0];
+    if (state.implicit !== null) {
+      state.useDecoder = state.useDecoder.clone();
+      state.useDecoder._baseState.implicit = state.implicit;
+    }
+  }
+  return state.useDecoder;
+};
+
 Node.prototype._decodeChoice = function decodeChoice(input) {
   var state = this._baseState;
   var result = null;
@@ -453,10 +486,10 @@ Node.prototype._encode = function encode(data, reporter) {
           return reporter.error('seqof/setof, but data is not Array');
 
         content = this._createEncoderBuffer(data.map(function(item) {
-          return this._use(state.args[0], item);
+          return this._getUse(state.args[0])._encode(item, reporter);
         }, this));
       } else if (state.use !== null) {
-        result = this._use(state.use, data);
+        result = this._getUse(state.use)._encode(data, reporter);
       } else {
         content = this._encodePrimitive(state.tag, data);
         primitive = true;
diff --git a/lib/asn1/decoders/der.js b/lib/asn1/decoders/der.js
index 1475d66..3c86697 100644
--- a/lib/asn1/decoders/der.js
+++ b/lib/asn1/decoders/der.js
@@ -227,8 +227,8 @@ DERNode.prototype._decodeInt = function decodeInt(buffer, values) {
   return res;
 };
 
-DERNode.prototype._use = function use(buffer, decoder) {
-  return decoder.decode(buffer, 'der');
+DERNode.prototype._use = function use(entity) {
+  return entity._getDecoder('der').tree;
 };
 
 // Utility methods
diff --git a/lib/asn1/encoders/der.js b/lib/asn1/encoders/der.js
index 68b7ca6..c8424ed 100644
--- a/lib/asn1/encoders/der.js
+++ b/lib/asn1/encoders/der.js
@@ -200,8 +200,8 @@ DERNode.prototype._encodeBool = function encodeBool(value) {
   return this._createEncoderBuffer(value ? 0xff : 0);
 };
 
-DERNode.prototype._use = function use(encoder, data) {
-  return encoder.encode(data, 'der', this.reporter);
+DERNode.prototype._use = function use(entity) {
+  return entity._getEncoder('der').tree;
 };
 
 // Utility methods
diff --git a/test/der-decode-test.js b/test/der-decode-test.js
new file mode 100644
index 0000000..4957744
--- /dev/null
+++ b/test/der-decode-test.js
@@ -0,0 +1,23 @@
+var assert = require('assert');
+var asn1 = require('..');
+
+var Buffer = require('buffer').Buffer;
+
+describe('asn1.js DER decoder', function() {
+  it('should propagate implicit tag', function() {
+    var B = asn1.define('B', function() {
+      this.seq().obj(
+        this.key('b').octstr()
+      );
+    });
+
+    var A = asn1.define('Bug', function() {
+      this.seq().obj(
+        this.key('a').implicit(0).use(B)
+      );
+    });
+
+    var out = A.decode(new Buffer('300720050403313233', 'hex'), 'der');
+    assert.equal(out.a.b.toString(), '123');
+  })
+});
diff --git a/test/ping-pong-test.js b/test/ping-pong-test.js
index 7a82364..c6d9a01 100644
--- a/test/ping-pong-test.js
+++ b/test/ping-pong-test.js
@@ -121,7 +121,7 @@ describe('asn1.js ping/pong', function() {
       );
     }, { hello: 'devs' }, { hello: 'devs', how: 'are you' });
 
-    test('setof', function() {
+    test('seqof', function() {
       var S = asn1.define('S', function() {
         this.seq().obj(
           this.key('a').def('b').int({ 0: 'a', 1: 'b' }),

-- 
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