[Pkg-javascript-commits] [node-asn1.js] 145/202: lib: lots of ...str methods

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 4d52f536616d347f88874d06e441de16a5f75c1f
Author: Felix Hanley <felix at userspace.com.au>
Date:   Sat Feb 20 00:35:24 2016 -0500

    lib: lots of ...str methods
---
 lib/asn1/base/node.js    | 19 ++++++-------------
 lib/asn1/decoders/der.js | 35 ++++++++++++++++-------------------
 lib/asn1/encoders/der.js | 10 +++-------
 test/der-decode-test.js  | 16 ++++++++++++++++
 test/der-encode-test.js  | 16 ++++++++++++++++
 5 files changed, 57 insertions(+), 39 deletions(-)

diff --git a/lib/asn1/base/node.js b/lib/asn1/base/node.js
index 192a411..62dcaa4 100644
--- a/lib/asn1/base/node.js
+++ b/lib/asn1/base/node.js
@@ -5,9 +5,10 @@ var assert = require('minimalistic-assert');
 
 // Supported tags
 var tags = [
-  'seq', 'seqof', 'set', 'setof', 'octstr', 'bitstr', 'objid', 'bool',
-  'gentime', 'utctime', 'null_', 'enum', 'int', 'ia5str', 'utf8str', 'bmpstr',
-  'numstr', 'printstr'
+  'seq', 'seqof', 'set', 'setof', 'objid', 'bool',
+  'gentime', 'utctime', 'null_', 'enum', 'int',
+  'bitstr', 'bmpstr', 'charstr', 'genstr', 'graphstr', 'ia5str', 'iso646str',
+  'numstr', 'octstr', 'printstr', 't61str', 'unistr', 'utf8str', 'videostr'
 ];
 
 // Public methods list
@@ -391,11 +392,7 @@ Node.prototype._decodeGeneric = function decodeGeneric(tag, input) {
     return null;
   if (tag === 'seqof' || tag === 'setof')
     return this._decodeList(input, tag, state.args[0]);
-  else if (tag === 'octstr' || tag === 'bitstr')
-    return this._decodeStr(input, tag);
-  else if (tag === 'ia5str' || tag === 'utf8str' || tag === 'bmpstr')
-    return this._decodeStr(input, tag);
-  else if (tag === 'numstr' || tag === 'printstr')
+  else if (/str$/.test(tag))
     return this._decodeStr(input, tag);
   else if (tag === 'objid' && state.args)
     return this._decodeObjid(input, state.args[0], state.args[1]);
@@ -599,11 +596,7 @@ Node.prototype._encodeChoice = function encodeChoice(data, reporter) {
 Node.prototype._encodePrimitive = function encodePrimitive(tag, data) {
   var state = this._baseState;
 
-  if (tag === 'octstr' || tag === 'bitstr' || tag === 'ia5str')
-    return this._encodeStr(data, tag);
-  else if (tag === 'utf8str' || tag === 'bmpstr')
-    return this._encodeStr(data, tag);
-  else if (tag === 'numstr' || tag === 'printstr')
+  if (/str$/.test(tag))
     return this._encodeStr(data, tag);
   else if (tag === 'objid' && state.args)
     return this._encodeObjid(data, state.reverseArgs[0], state.args[1]);
diff --git a/lib/asn1/decoders/der.js b/lib/asn1/decoders/der.js
index 31e103e..9acace6 100644
--- a/lib/asn1/decoders/der.js
+++ b/lib/asn1/decoders/der.js
@@ -123,42 +123,39 @@ DERNode.prototype._decodeList = function decodeList(buffer, tag, decoder) {
 };
 
 DERNode.prototype._decodeStr = function decodeStr(buffer, tag) {
-  if (tag === 'octstr') {
-    return buffer.raw();
-  } else if (tag === 'bitstr') {
+  if (tag === 'bitstr') {
     var unused = buffer.readUInt8();
     if (buffer.isError(unused))
       return unused;
-
     return { unused: unused, data: buffer.raw() };
-  } else if (tag === 'ia5str' || tag === 'utf8str') {
-    return buffer.raw().toString();
-  } else if(tag === 'numstr') {
+  } else if (tag === 'bmpstr') {
+    var raw = buffer.raw();
+    if (raw.length % 2 === 1)
+      return buffer.error('Decoding of string type: bmpstr length mismatch');
+
+    var str = '';
+    for (var i = 0; i < raw.length / 2; i++) {
+      str += String.fromCharCode(raw.readUInt16BE(i * 2));
+    }
+    return str;
+  } else if (tag === 'numstr') {
     var numstr = buffer.raw().toString('ascii');
     if (!this._isNumstr(numstr)) {
       return buffer.error('Decoding of string type: ' +
                           'numstr unsupported characters');
     }
-
     return numstr;
+  } else if (tag === 'octstr') {
+    return buffer.raw();
   } else if (tag === 'printstr') {
     var printstr = buffer.raw().toString('ascii');
     if (!this._isPrintstr(printstr)) {
       return buffer.error('Decoding of string type: ' +
                           'printstr unsupported characters');
     }
-
     return printstr;
-  } else if(tag === 'bmpstr') {
-    var raw = buffer.raw();
-    if (raw.length % 2 === 1)
-      return buffer.error('Decoding of string type: bmpstr length mismatch');
-
-    var str = '';
-    for (var i = 0; i < raw.length / 2; i++) {
-      str += String.fromCharCode(raw.readUInt16BE(i * 2));
-    }
-    return str;
+  } else if (/str$/.test(tag)) {
+    return buffer.raw().toString();
   } else {
     return buffer.error('Decoding of string type: ' + tag + ' unsupported');
   }
diff --git a/lib/asn1/encoders/der.js b/lib/asn1/encoders/der.js
index ec6f833..fb0b39f 100644
--- a/lib/asn1/encoders/der.js
+++ b/lib/asn1/encoders/der.js
@@ -61,12 +61,8 @@ DERNode.prototype._encodeComposite = function encodeComposite(tag,
 };
 
 DERNode.prototype._encodeStr = function encodeStr(str, tag) {
-  if (tag === 'octstr') {
-    return this._createEncoderBuffer(str);
-  } else if (tag === 'bitstr') {
+  if (tag === 'bitstr') {
     return this._createEncoderBuffer([ str.unused | 0, str.data ]);
-  } else if (tag === 'ia5str' || tag === 'utf8str') {
-    return this._createEncoderBuffer(str);
   } else if (tag === 'bmpstr') {
     var buf = new Buffer(str.length * 2);
     for (var i = 0; i < str.length; i++) {
@@ -78,7 +74,6 @@ DERNode.prototype._encodeStr = function encodeStr(str, tag) {
       return this.reporter.error('Encoding of string type: numstr supports ' +
                                  'only digits and space');
     }
-
     return this._createEncoderBuffer(str);
   } else if (tag === 'printstr') {
     if (!this._isPrintstr(str)) {
@@ -89,7 +84,8 @@ DERNode.prototype._encodeStr = function encodeStr(str, tag) {
                                  'dot, slash, colon, equal sign, ' +
                                  'question mark');
     }
-
+    return this._createEncoderBuffer(str);
+  } else if (/str$/.test(tag)) {
     return this._createEncoderBuffer(str);
   } else {
     return this.reporter.error('Encoding of string type: ' + tag +
diff --git a/test/der-decode-test.js b/test/der-decode-test.js
index 6da6a9e..3eba72e 100644
--- a/test/der-decode-test.js
+++ b/test/der-decode-test.js
@@ -104,4 +104,20 @@ describe('asn1.js DER decoder', function() {
     var out = A.decode(new Buffer('04053003020105', 'hex'), 'der');
     assert.equal(out.nested.toString(10), '5');
   });
+
+  test('should decode IA5 string', function() {
+    this.ia5str();
+  }, '160C646F6720616E6420626F6E65', 'dog and bone');
+
+  test('should decode printable string', function() {
+    this.printstr();
+  }, '1310427261686D7320616E64204C69737A74', 'Brahms and Liszt');
+
+  test('should decode T61 string', function() {
+    this.t61str();
+  }, '140C4F6C69766572205477697374', 'Oliver Twist');
+
+  test('should decode ISO646 string', function() {
+    this.iso646str();
+  }, '1A0B7365707469632074616E6B', 'septic tank');
 });
diff --git a/test/der-encode-test.js b/test/der-encode-test.js
index d32891b..af37293 100644
--- a/test/der-encode-test.js
+++ b/test/der-encode-test.js
@@ -114,4 +114,20 @@ describe('asn1.js DER encoder', function() {
     var out = A.encode({ nested: 5 }, 'der')
     assert.equal(out.toString('hex'), '04053003020105');
   });
+
+  test('should properly encode IA5 string', function() {
+    this.ia5str();
+  }, 'dog and bone', '160C646F6720616E6420626F6E65');
+
+  test('should properly encode printable string', function() {
+    this.printstr();
+  }, 'Brahms and Liszt', '1310427261686D7320616E64204C69737A74');
+
+  test('should properly encode T61 string', function() {
+    this.t61str();
+  }, 'Oliver Twist', '140C4F6C69766572205477697374');
+
+  test('should properly encode ISO646 string', function() {
+    this.iso646str();
+  }, 'septic tank', '1A0B7365707469632074616E6B');
 });

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