Skip to content

Commit

Permalink
fix: variable binary field encode decode as hex
Browse files Browse the repository at this point in the history
  • Loading branch information
Arun Rajput authored and Arun Rajput committed Aug 27, 2022
1 parent 8da6be8 commit 54dc08b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 11 deletions.
39 changes: 29 additions & 10 deletions lib/pack/assemble0_127_Fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion lib/unpack/unpack_0_127.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
30 changes: 30 additions & 0 deletions test/8583-ext.js
Original file line number Diff line number Diff line change
@@ -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');

});

0 comments on commit 54dc08b

Please sign in to comment.