[Pkg-javascript-commits] [node-asn1.js] 170/202: node: stub out `track` APIs

Bastien Roucariès rouca at moszumanska.debian.org
Thu Apr 20 19:19:05 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 a94f938750257242d2bc957e8660574e5ca64034
Author: Fedor Indutny <fedor at indutny.com>
Date:   Sun Jul 10 23:24:16 2016 -0400

    node: stub out `track` APIs
---
 lib/asn1/base/node.js     | 55 ++++++++++++++++++++++++++++-------------------
 lib/asn1/base/reporter.js |  4 ++++
 lib/asn1/decoders/der.js  |  5 +++--
 test/tracking-test.js     | 47 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 87 insertions(+), 24 deletions(-)

diff --git a/lib/asn1/base/node.js b/lib/asn1/base/node.js
index 91ec6dd..4c7943c 100644
--- a/lib/asn1/base/node.js
+++ b/lib/asn1/base/node.js
@@ -268,12 +268,12 @@ Node.prototype.contains = function contains(item) {
 // Decoding
 //
 
-Node.prototype._decode = function decode(input) {
+Node.prototype._decode = function decode(input, options) {
   var state = this._baseState;
 
   // Decode root node
   if (state.parent === null)
-    return input.wrapResult(state.children[0]._decode(input));
+    return input.wrapResult(state.children[0]._decode(input, options));
 
   var result = state['default'];
   var present = true;
@@ -297,9 +297,9 @@ Node.prototype._decode = function decode(input) {
       var save = input.save();
       try {
         if (state.choice === null)
-          this._decodeGeneric(state.tag, input);
+          this._decodeGeneric(state.tag, input, options);
         else
-          this._decodeChoice(input);
+          this._decodeChoice(input, options);
         present = true;
       } catch (e) {
         present = false;
@@ -327,6 +327,8 @@ Node.prototype._decode = function decode(input) {
       input = explicit;
     }
 
+    var start = input.offset;
+
     // Unwrap implicit and normal values
     if (state.use === null && state.choice === null) {
       if (state.any)
@@ -349,9 +351,9 @@ Node.prototype._decode = function decode(input) {
     if (state.any)
       result = result;
     else if (state.choice === null)
-      result = this._decodeGeneric(state.tag, input);
+      result = this._decodeGeneric(state.tag, input, options);
     else
-      result = this._decodeChoice(input);
+      result = this._decodeChoice(input, options);
 
     if (input.isError(result))
       return result;
@@ -361,15 +363,21 @@ Node.prototype._decode = function decode(input) {
       state.children.forEach(function decodeChildren(child) {
         // NOTE: We are ignoring errors here, to let parser continue with other
         // parts of encoded data
-        child._decode(input);
+        child._decode(input, options);
       });
     }
 
     // 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);
+      result = this._getUse(state.contains, input._reporterState.obj)
+          ._decode(data, options);
     }
+
+    var end = input.offset;
+
+    if (options && options.track)
+      options.track(input.path(), start, end);
   }
 
   // Pop object
@@ -383,31 +391,34 @@ Node.prototype._decode = function decode(input) {
   return result;
 };
 
-Node.prototype._decodeGeneric = function decodeGeneric(tag, input) {
+Node.prototype._decodeGeneric = function decodeGeneric(tag, input, options) {
   var state = this._baseState;
 
   if (tag === 'seq' || tag === 'set')
     return null;
   if (tag === 'seqof' || tag === 'setof')
-    return this._decodeList(input, tag, state.args[0]);
+    return this._decodeList(input, tag, state.args[0], options);
   else if (/str$/.test(tag))
-    return this._decodeStr(input, tag);
+    return this._decodeStr(input, tag, options);
   else if (tag === 'objid' && state.args)
-    return this._decodeObjid(input, state.args[0], state.args[1]);
+    return this._decodeObjid(input, state.args[0], state.args[1], options);
   else if (tag === 'objid')
-    return this._decodeObjid(input, null, null);
+    return this._decodeObjid(input, null, null, options);
   else if (tag === 'gentime' || tag === 'utctime')
-    return this._decodeTime(input, tag);
+    return this._decodeTime(input, tag, options);
   else if (tag === 'null_')
-    return this._decodeNull(input);
+    return this._decodeNull(input, options);
   else if (tag === 'bool')
-    return this._decodeBool(input);
+    return this._decodeBool(input, options);
   else if (tag === 'int' || tag === 'enum')
-    return this._decodeInt(input, state.args && state.args[0]);
-  else if (state.use !== null)
-    return this._getUse(state.use, input._reporterState.obj)._decode(input);
-  else
+    return this._decodeInt(input, state.args && state.args[0], options);
+
+  if (state.use !== null) {
+    return this._getUse(state.use, input._reporterState.obj)
+        ._decode(input, options);
+  } else {
     return input.error('unknown tag: ' + tag);
+  }
 };
 
 Node.prototype._getUse = function _getUse(entity, obj) {
@@ -424,7 +435,7 @@ Node.prototype._getUse = function _getUse(entity, obj) {
   return state.useDecoder;
 };
 
-Node.prototype._decodeChoice = function decodeChoice(input) {
+Node.prototype._decodeChoice = function decodeChoice(input, options) {
   var state = this._baseState;
   var result = null;
   var match = false;
@@ -433,7 +444,7 @@ Node.prototype._decodeChoice = function decodeChoice(input) {
     var save = input.save();
     var node = state.choice[key];
     try {
-      var value = node._decode(input);
+      var value = node._decode(input, options);
       if (input.isError(value))
         return false;
 
diff --git a/lib/asn1/base/reporter.js b/lib/asn1/base/reporter.js
index 62a2bcb..287655f 100644
--- a/lib/asn1/base/reporter.js
+++ b/lib/asn1/base/reporter.js
@@ -39,6 +39,10 @@ Reporter.prototype.leaveKey = function leaveKey(index, key, value) {
     state.obj[key] = value;
 };
 
+Reporter.prototype.path = function path() {
+  return this._reporterState.path.join('/');
+};
+
 Reporter.prototype.enterObject = function enterObject() {
   var state = this._reporterState;
 
diff --git a/lib/asn1/decoders/der.js b/lib/asn1/decoders/der.js
index 92d892d..a0cfb6f 100644
--- a/lib/asn1/decoders/der.js
+++ b/lib/asn1/decoders/der.js
@@ -108,14 +108,15 @@ DERNode.prototype._skipUntilEnd = function skipUntilEnd(buffer, fail) {
   }
 };
 
-DERNode.prototype._decodeList = function decodeList(buffer, tag, decoder) {
+DERNode.prototype._decodeList = function decodeList(buffer, tag, decoder,
+                                                    options) {
   var result = [];
   while (!buffer.isEmpty()) {
     var possibleEnd = this._peekTag(buffer, 'end');
     if (buffer.isError(possibleEnd))
       return possibleEnd;
 
-    var res = decoder.decode(buffer, 'der');
+    var res = decoder.decode(buffer, 'der', options);
     if (buffer.isError(res) && possibleEnd)
       break;
     result.push(res);
diff --git a/test/tracking-test.js b/test/tracking-test.js
new file mode 100644
index 0000000..1d0a273
--- /dev/null
+++ b/test/tracking-test.js
@@ -0,0 +1,47 @@
+var assert = require('assert');
+var asn1 = require('..');
+var fixtures = require('./fixtures');
+var jsonEqual = fixtures.jsonEqual;
+
+describe('asn1.js tracking', function() {
+  it('should track nested offsets', () => {
+    var B = asn1.define('B', function() {
+      this.seq().obj(
+        this.key('x').int(),
+        this.key('y').int()
+      );
+    });
+
+    var A = asn1.define('A', function() {
+      this.seq().obj(
+        this.key('a').explicit(0).use(B),
+        this.key('b').use(B)
+      );
+    });
+
+    var input = {
+      a: { x: 1, y: 2 },
+      b: { x: 3, y: 4 }
+    };
+
+    var tracked = {};
+
+    var encoded = A.encode(input, 'der');
+    var decoded = A.decode(encoded, 'der', {
+      track: function(path, start, end) {
+        tracked[path] = [ start, end ];
+      }
+    });
+
+    jsonEqual(input, decoded);
+    assert.deepEqual(tracked, {
+      '': [ 0, 20 ],
+      a: [ 4, 12 ],
+      'a/x': [ 6, 8 ],
+      'a/y': [ 9, 11 ],
+       b: [ 12, 20 ],
+      'b/x': [ 14, 16 ],
+      'b/y': [ 17, 19 ]
+    });
+  });
+});

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