From 54dc08b688cc3621c660c355cf121c40f0acac75 Mon Sep 17 00:00:00 2001 From: Arun Rajput Date: Sat, 27 Aug 2022 09:21:52 +0530 Subject: [PATCH] fix: variable binary field encode decode as hex --- lib/pack/assemble0_127_Fields.js | 39 ++++++++++++++++++++++++-------- lib/unpack/unpack_0_127.js | 6 ++++- test/8583-ext.js | 30 ++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 11 deletions(-) create mode 100644 test/8583-ext.js diff --git a/lib/pack/assemble0_127_Fields.js b/lib/pack/assemble0_127_Fields.js index 1cba89a..cb5b35d 100644 --- a/lib/pack/assemble0_127_Fields.js +++ b/lib/pack/assemble0_127_Fields.js @@ -90,17 +90,36 @@ function assemble0_127_Fields () { if (thisLen === 0) { return T.toErrorObject(['field', field, ' has no field implementation']); } else { - let actualLength = this.Msg[field].length; - let padCount = thisLen - actualLength.toString().length; - let lenIndicator = actualLength.toString(); - for (let i = 0; i < padCount; i++) { - lenIndicator = 0 + lenIndicator; + + if (this_format.ContentType === 'b') { + + let actualLength = this.Msg[field].length/2; + + let padCount = thisLen - actualLength.toString().length; + let lenIndicator = actualLength.toString(); + for (let i = 0; i < padCount; i++) { + lenIndicator = 0 + lenIndicator; + } + + let thisBuff = Buffer.alloc(actualLength, this.Msg[field], 'hex'); + thisBuff = Buffer.concat([Buffer.from(lenIndicator), thisBuff]); + buff = Buffer.concat([buff, thisBuff]); + + + } else { + let actualLength = this.Msg[field].length; + + let padCount = thisLen - actualLength.toString().length; + let lenIndicator = actualLength.toString(); + for (let i = 0; i < padCount; i++) { + lenIndicator = 0 + lenIndicator; + } + let thisBuff = Buffer.alloc( + this.Msg[field].length + lenIndicator.length, + lenIndicator + this.Msg[field] + ); + buff = Buffer.concat([buff, thisBuff]); } - let thisBuff = Buffer.alloc( - this.Msg[field].length + lenIndicator.length, - lenIndicator + this.Msg[field] - ); - buff = Buffer.concat([buff, thisBuff]); } } } else { diff --git a/lib/unpack/unpack_0_127.js b/lib/unpack/unpack_0_127.js index 5d906ca..f9aa3fe 100644 --- a/lib/unpack/unpack_0_127.js +++ b/lib/unpack/unpack_0_127.js @@ -93,7 +93,11 @@ module.exports = function (incoming, isoJSON, config) { } else { let len = thisBuff.slice(0, thisLen).toString(); thisBuff = thisBuff.slice(thisLen, thisBuff.byteLength); - isoJSON[field] = thisBuff.slice(0, Number(len)).toString(); + if (this_format.ContentType === 'b') { + isoJSON[field] = thisBuff.slice(0, Number(len)).toString('hex').toUpperCase(); + } else { + isoJSON[field] = thisBuff.slice(0, Number(len)).toString(); + } thisBuff = thisBuff.slice(Number(len), thisBuff.byteLength); } } diff --git a/test/8583-ext.js b/test/8583-ext.js new file mode 100644 index 0000000..efc5a9b --- /dev/null +++ b/test/8583-ext.js @@ -0,0 +1,30 @@ +import test from 'ava'; +import iso8583 from '../lib/8583.js'; + +/* +Support custom iso 8583 formats +Support Case: Binary var field encode decode +*/ +test('iso8583 Ext - binary var field must be encoded', t => { + const format = { + 2: { + ContentType: 'b', + Label: 'F1', + LenType: 'llvar', + MaxLen: 99 + }, + + }; + const data = { + 0: '0800', + '2': '1004000000000000303030303030303030343048' + // #2 this is prepard by encodng subfields - 8bytes bitmap followed by subfields data + }; + let isopack = new iso8583(data, format); + let buff = isopack.getRawMessage(); + const iso1 = new iso8583(undefined, format).getIsoJSON(buff, {lenHeader: false}); + t.deepEqual(iso1, data); + let isopack1 = new iso8583(iso1); + t.is(isopack1.Msg['2'], '1004000000000000303030303030303030343048'); + +});